2015-11-26 00:03:37 +00:00
|
|
|
<?php
|
2015-12-02 13:04:12 +00:00
|
|
|
/**
|
2018-03-21 03:02:57 +00:00
|
|
|
* WooCommerce product embed
|
2015-12-02 13:04:12 +00:00
|
|
|
*
|
|
|
|
* @version 2.4.11
|
|
|
|
* @package WooCommerce/Classes/Embed
|
|
|
|
*/
|
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
|
|
|
*/
|
|
|
|
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.
|
2016-06-02 13:02:39 +00:00
|
|
|
add_action( 'embed_content_meta', array( __CLASS__, 'remove_comments_button' ), 5 );
|
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
|
|
|
}
|
2016-06-02 13:56:13 +00:00
|
|
|
|
2016-06-02 13:02:39 +00:00
|
|
|
/**
|
|
|
|
* Remove comments button on product embeds.
|
2016-06-02 13:56:13 +00:00
|
|
|
*
|
2016-06-02 17:43:59 +00:00
|
|
|
* @since 2.6.0
|
2016-06-02 13:02:39 +00:00
|
|
|
*/
|
|
|
|
public static function remove_comments_button() {
|
2016-06-02 13:56:13 +00:00
|
|
|
if ( self::is_embedded_product() ) {
|
|
|
|
remove_action( 'embed_content_meta', 'print_embed_comments_button' );
|
2016-06-02 13:02:39 +00:00
|
|
|
}
|
|
|
|
}
|
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() ) {
|
2018-03-21 03:02:57 +00:00
|
|
|
echo '<p><span class="wc-embed-price">' . $_product->get_price_html() . '</span></p>'; // WPCS: XSS ok.
|
2016-03-21 10:07:26 +00:00
|
|
|
|
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() ) {
|
2016-10-12 10:16:30 +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
|
|
|
}
|
|
|
|
|
2016-06-30 18:16:26 +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.
|
2018-03-21 03:02:57 +00:00
|
|
|
if ( ! self::is_embedded_product() ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$_product = wc_get_product( get_the_ID() );
|
|
|
|
|
|
|
|
if ( $_product && $_product->get_average_rating() > 0 ) {
|
2015-12-02 13:04:12 +00:00
|
|
|
?>
|
|
|
|
<div class="wc-embed-rating">
|
2016-10-29 17:32:38 +00:00
|
|
|
<?php
|
|
|
|
printf(
|
2018-03-21 03:02:57 +00:00
|
|
|
/* translators: %s: average rating */
|
2017-05-16 08:04:47 +00:00
|
|
|
esc_html__( 'Rated %s out of 5', 'woocommerce' ),
|
2017-01-06 12:45:26 +00:00
|
|
|
esc_html( $_product->get_average_rating() )
|
2016-10-29 17:32:38 +00:00
|
|
|
);
|
|
|
|
?>
|
2015-12-02 13:04:12 +00:00
|
|
|
</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();
|