2015-11-26 00:03:37 +00:00
|
|
|
<?php
|
2015-12-02 13:04:12 +00:00
|
|
|
/**
|
|
|
|
* WooCommerce Product Embed.
|
|
|
|
*
|
|
|
|
* @version 2.4.11
|
|
|
|
* @package WooCommerce/Classes/Embed
|
|
|
|
* @category Class
|
|
|
|
* @author WooThemes
|
|
|
|
*/
|
2015-11-26 00:03:37 +00:00
|
|
|
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
2015-12-02 11:50:41 +00:00
|
|
|
exit;
|
2015-11-26 00:03:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-12-02 13:04:12 +00:00
|
|
|
* Embed Class which handles any WooCommerce Products that are embedded on this site or another site.
|
2015-11-26 00:03:37 +00:00
|
|
|
*
|
2015-12-02 13:04:12 +00:00
|
|
|
* @class WC_Embed
|
|
|
|
* @version 2.4.11
|
|
|
|
* @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
|
|
|
*
|
2015-12-02 13:04:12 +00:00
|
|
|
* @since 2.4.11
|
2015-11-26 00:03:37 +00:00
|
|
|
*/
|
|
|
|
public static function init() {
|
|
|
|
|
2015-12-02 13:04:12 +00:00
|
|
|
// Filter all of the content that's going to be embedded.
|
|
|
|
add_filter( 'the_excerpt_embed', array( __CLASS__, 'the_excerpt' ), 10 );
|
2015-11-26 00:03:37 +00:00
|
|
|
|
2015-12-02 13:04:12 +00:00
|
|
|
// Make sure no comments display. Doesn't make sense for products.
|
|
|
|
remove_action( 'embed_content_meta', 'print_embed_comments_button' );
|
2015-11-30 22:58:23 +00:00
|
|
|
|
2015-12-02 13:04:12 +00:00
|
|
|
// In the comments place let's display the product rating.
|
|
|
|
add_action( 'embed_content_meta', array( __CLASS__, 'get_ratings' ), 5 );
|
2015-12-02 12:29:47 +00:00
|
|
|
|
2015-12-02 13:04:12 +00:00
|
|
|
// Add some basic styles.
|
2015-12-02 12:29:47 +00:00
|
|
|
add_action( 'embed_head', array( __CLASS__, 'print_embed_styles' ) );
|
2015-11-26 00:03:37 +00:00
|
|
|
}
|
|
|
|
|
2015-12-02 13:04:12 +00:00
|
|
|
/**
|
|
|
|
* Check if this is an embedded product - to make sure we don't mess up regular posts.
|
|
|
|
*
|
|
|
|
* @since 2.4.11
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
* @since 2.4.11
|
|
|
|
* @param string $excerpt Embed short description.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function the_excerpt( $excerpt ) {
|
2015-12-02 12:03:04 +00:00
|
|
|
global $post;
|
2015-11-26 00:03:37 +00:00
|
|
|
|
2016-03-21 10:07:26 +00:00
|
|
|
// Get product.
|
|
|
|
$_product = wc_get_product( get_the_ID() );
|
|
|
|
|
2015-12-02 13:04:12 +00:00
|
|
|
// Make sure we're only affecting embedded products.
|
|
|
|
if ( self::is_embedded_product() ) {
|
2016-03-21 10:07:26 +00:00
|
|
|
echo '<p><span class="wc-embed-price">' . $_product->get_price_html() . '</span></p>';
|
|
|
|
|
2015-12-02 12:03:04 +00:00
|
|
|
if ( ! empty( $post->post_excerpt ) ) {
|
|
|
|
ob_start();
|
2015-12-02 13:04:12 +00:00
|
|
|
woocommerce_template_single_excerpt();
|
|
|
|
$excerpt = ob_get_clean();
|
2015-12-02 12:03:04 +00:00
|
|
|
}
|
|
|
|
|
2015-12-02 13:04:12 +00:00
|
|
|
// Add the button.
|
|
|
|
$excerpt .= self::product_buttons();
|
|
|
|
}
|
|
|
|
return $excerpt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create the button to go to the product page for embedded products.
|
|
|
|
*
|
|
|
|
* @since 2.4.11
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function product_buttons() {
|
2015-12-02 12:29:47 +00:00
|
|
|
$_product = wc_get_product( get_the_ID() );
|
|
|
|
$buttons = array();
|
|
|
|
$button = '<a href="%s" class="wp-embed-more wc-embed-button">%s</a>';
|
|
|
|
|
2015-12-02 12:36:56 +00:00
|
|
|
if ( $_product->is_type( 'simple' ) && $_product->is_purchasable() && $_product->is_in_stock() ) {
|
2015-12-02 13:04:12 +00:00
|
|
|
$buttons[] = sprintf( $button, esc_url( add_query_arg( 'add-to-cart', get_the_ID(), wc_get_cart_url() ) ), esc_html__( 'Buy Now', 'woocommerce' ) );
|
2015-12-02 12:29:47 +00:00
|
|
|
}
|
|
|
|
|
2015-12-02 13:04:12 +00:00
|
|
|
$buttons[] = sprintf( $button, get_the_permalink(), esc_html__( 'Read More', 'woocommerce' ) );
|
2015-12-02 12:29:47 +00:00
|
|
|
|
|
|
|
return '<p>' . implode( ' ', $buttons ) . '</p>';
|
2015-12-02 13:04:12 +00:00
|
|
|
}
|
2015-11-26 00:03:37 +00:00
|
|
|
|
2015-12-02 12:29:47 +00:00
|
|
|
/**
|
2015-12-02 13:04:12 +00:00
|
|
|
* Prints the markup for the rating stars.
|
|
|
|
*
|
|
|
|
* @since 2.4.11
|
|
|
|
*/
|
|
|
|
public static function get_ratings() {
|
|
|
|
// Make sure we're only affecting embedded products.
|
|
|
|
if ( self::is_embedded_product() && ( $_product = wc_get_product( get_the_ID() ) ) && $_product->get_average_rating() > 0 ) {
|
|
|
|
?>
|
|
|
|
<div class="wc-embed-rating">
|
|
|
|
<?php echo esc_html( sprintf( __( 'Rated %s out of 5', 'woocommerce' ), $_product->get_average_rating() ) ); ?>
|
|
|
|
</div>
|
|
|
|
<?php
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Basic styling.
|
2015-12-02 12:29:47 +00:00
|
|
|
*/
|
|
|
|
public static function print_embed_styles() {
|
|
|
|
if ( ! self::is_embedded_product() ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
?>
|
|
|
|
<style type="text/css">
|
|
|
|
a.wc-embed-button {
|
|
|
|
border-radius: 4px;
|
2015-12-02 13:04:12 +00:00
|
|
|
border: 1px solid #ddd;
|
|
|
|
box-shadow: 0px 1px 0 0px rgba(0, 0, 0, 0.05);
|
2015-12-02 12:29:47 +00:00
|
|
|
display:inline-block;
|
2015-12-02 13:04:12 +00:00
|
|
|
padding: .5em;
|
2015-12-02 12:29:47 +00:00
|
|
|
}
|
|
|
|
a.wc-embed-button:hover, a.wc-embed-button:focus {
|
|
|
|
border: 1px solid #ccc;
|
2015-12-02 13:04:12 +00:00
|
|
|
box-shadow: 0px 1px 0 0px rgba(0, 0, 0, 0.1);
|
2015-12-02 12:29:47 +00:00
|
|
|
color: #999;
|
2015-12-02 13:04:12 +00:00
|
|
|
text-decoration: none;
|
2015-12-02 12:29:47 +00:00
|
|
|
}
|
|
|
|
.wp-embed-excerpt p {
|
|
|
|
margin: 0 0 1em;
|
|
|
|
}
|
|
|
|
.wc-embed-price {
|
2016-03-21 10:07:26 +00:00
|
|
|
display: block;
|
|
|
|
opacity: .75;
|
|
|
|
font-weight: 700;
|
|
|
|
margin-top: -.75em;
|
2015-12-02 12:29:47 +00:00
|
|
|
}
|
|
|
|
.wc-embed-rating {
|
|
|
|
display: inline-block;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
<?php
|
|
|
|
}
|
2015-11-26 00:03:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
WC_Embed::init();
|