2015-11-26 00:03:37 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
2015-12-02 11:50:41 +00:00
|
|
|
exit;
|
2015-11-26 00:03:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Embed Class which handles any WooCommerce Products that are embedded on this site or another site
|
|
|
|
*
|
2015-11-30 22:58:23 +00:00
|
|
|
* @class WC_Embed
|
2015-12-02 11:50:41 +00:00
|
|
|
* @version 2.4.11
|
2015-11-30 22:58:23 +00:00
|
|
|
* @package WooCommerce/Classes/Embed
|
|
|
|
* @category Class
|
|
|
|
* @author WooThemes
|
2015-11-26 00:03:37 +00:00
|
|
|
*/
|
|
|
|
class WC_Embed {
|
|
|
|
|
|
|
|
/**
|
2015-11-26 00:24:48 +00:00
|
|
|
* Init embed class.
|
2015-12-02 11:50:41 +00:00
|
|
|
*
|
|
|
|
* @since 2.4.11
|
2015-11-26 00:03:37 +00:00
|
|
|
*/
|
|
|
|
public static function init() {
|
|
|
|
|
|
|
|
// filter all of the content that's going to be embedded
|
|
|
|
add_filter( 'the_title', array( 'WC_Embed', 'the_title' ), 10 );
|
|
|
|
add_filter( 'the_excerpt_embed', array( 'WC_Embed', 'the_excerpt' ), 10 );
|
|
|
|
|
|
|
|
// make sure no comments display. Doesn't make sense for products
|
2015-11-30 22:58:23 +00:00
|
|
|
remove_action( 'embed_content_meta', 'print_embed_comments_button' );
|
|
|
|
|
|
|
|
// in the comments place let's display the product rating
|
|
|
|
add_action( 'embed_content_meta', array( 'WC_Embed', 'get_ratings' ), 5 );
|
2015-11-26 00:03:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create the title for embedded products - we want to add the price to it
|
|
|
|
*
|
|
|
|
* @return string
|
2015-12-02 11:50:41 +00:00
|
|
|
* @since 2.4.11
|
2015-11-26 00:03:37 +00:00
|
|
|
*/
|
|
|
|
public static function the_title( $title ) {
|
|
|
|
// make sure we're only affecting embedded products
|
2015-12-02 12:03:04 +00:00
|
|
|
if ( self::is_embedded_product() ) {
|
2015-11-26 00:03:37 +00:00
|
|
|
|
|
|
|
// get product
|
2015-12-02 12:03:04 +00:00
|
|
|
$_product = wc_get_product( get_the_ID() );
|
2015-11-26 00:03:37 +00:00
|
|
|
|
|
|
|
// add the price
|
|
|
|
$title = $title . '<span class="price" style="float: right;">' . $_product->get_price_html() . '</span>';
|
|
|
|
}
|
|
|
|
return $title;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if this is an embedded product - to make sure we don't mess up regular posts
|
|
|
|
*
|
|
|
|
* @return bool
|
2015-12-02 11:50:41 +00:00
|
|
|
* @since 2.4.11
|
2015-11-26 00:03:37 +00:00
|
|
|
*/
|
|
|
|
public static function is_embedded_product() {
|
|
|
|
if ( function_exists( 'is_embed' ) && is_embed() && is_product() ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create the excerpt for embedded products - we want to add the buy button to it
|
|
|
|
*
|
|
|
|
* @return string
|
2015-12-02 11:50:41 +00:00
|
|
|
* @since 2.4.11
|
2015-11-26 00:03:37 +00:00
|
|
|
*/
|
|
|
|
public static function the_excerpt( $excerpt ) {
|
2015-12-02 12:03:04 +00:00
|
|
|
global $post;
|
2015-11-26 00:03:37 +00:00
|
|
|
|
2015-12-02 12:03:04 +00:00
|
|
|
// make sure we're only affecting embedded products
|
|
|
|
if ( self::is_embedded_product() ) {
|
|
|
|
if ( ! empty( $post->post_excerpt ) ) {
|
|
|
|
ob_start();
|
|
|
|
woocommerce_template_single_excerpt();
|
|
|
|
$excerpt = ob_get_clean();
|
|
|
|
}
|
|
|
|
|
|
|
|
// add the button
|
|
|
|
$excerpt.= self::product_button();
|
2015-11-26 00:03:37 +00:00
|
|
|
}
|
|
|
|
return $excerpt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create the button to go to the product page for embedded products.
|
|
|
|
*
|
|
|
|
* @return string
|
2015-12-02 11:50:41 +00:00
|
|
|
* @since 2.4.11
|
2015-11-26 00:03:37 +00:00
|
|
|
*/
|
|
|
|
public static function product_button( ) {
|
2015-12-02 12:03:04 +00:00
|
|
|
$button = '<p><a href="%s" class="wp-embed-more button">%s →</a></p>';
|
|
|
|
return sprintf( $button, get_the_permalink(), __( 'View Product', 'woocommerce' ) );
|
2015-11-26 00:03:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-30 22:58:23 +00:00
|
|
|
* Prints the markup for the rating stars
|
2015-11-26 00:03:37 +00:00
|
|
|
*
|
|
|
|
* @return string
|
2015-12-02 11:50:41 +00:00
|
|
|
* @since 2.4.11
|
2015-11-26 00:03:37 +00:00
|
|
|
*/
|
2015-11-30 22:58:23 +00:00
|
|
|
public static function get_ratings( $comments ) {
|
2015-11-26 00:03:37 +00:00
|
|
|
// make sure we're only affecting embedded products
|
2015-12-02 12:03:04 +00:00
|
|
|
if ( self::is_embedded_product() ) {
|
2015-11-30 22:58:23 +00:00
|
|
|
?>
|
|
|
|
<div style="display:inline-block;">
|
|
|
|
<?php
|
2015-12-02 11:50:41 +00:00
|
|
|
$_product = wc_get_product( get_the_ID() );
|
|
|
|
printf( __( 'Rated %s out of 5', 'woocommerce' ), $_product->get_average_rating() );
|
2015-11-30 22:58:23 +00:00
|
|
|
?>
|
|
|
|
</div>
|
|
|
|
<?php
|
2015-11-26 00:03:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
WC_Embed::init();
|