class-wc-embed.php 4.24 KB
Newer Older
imac's avatar
imac committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
<?php
/**
 * WooCommerce Product Embed.
 *
 * @version  2.4.11
 * @package  WooCommerce/Classes/Embed
 * @category Class
 * @author   WooThemes
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Embed Class which handles any WooCommerce Products that are embedded on this site or another site.
 *
 * @class    WC_Embed
 * @version  2.4.11
 * @author   WooThemes
 */
class WC_Embed {

	/**
	 * Init embed class.
	 *
	 * @since 2.4.11
	 */
	public static function init() {

		// Filter all of the content that's going to be embedded.
		add_filter( 'the_excerpt_embed', array( __CLASS__, 'the_excerpt' ), 10 );

		// Make sure no comments display. Doesn't make sense for products.
		add_action( 'embed_content_meta', array( __CLASS__, 'remove_comments_button' ), 5 );

		// In the comments place let's display the product rating.
		add_action( 'embed_content_meta', array( __CLASS__, 'get_ratings' ), 5 );

		// Add some basic styles.
		add_action( 'embed_head', array( __CLASS__, 'print_embed_styles' ) );
	}

	/**
	 * Remove comments button on product embeds.
	 *
	 * @since 2.6.0
	 */
	public static function remove_comments_button() {
		if ( self::is_embedded_product() ) {
			remove_action( 'embed_content_meta', 'print_embed_comments_button' );
		}
	}

	/**
	 * 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 ) {
		global $post;

		// Get product.
		$_product = wc_get_product( get_the_ID() );

		// Make sure we're only affecting embedded products.
		if ( self::is_embedded_product() ) {
			echo '<p><span class="wc-embed-price">' . $_product->get_price_html() . '</span></p>';

			if ( ! empty( $post->post_excerpt ) ) {
				ob_start();
				woocommerce_template_single_excerpt();
				$excerpt = ob_get_clean();
			}

			// 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() {
		$_product = wc_get_product( get_the_ID() );
		$buttons  = array();
		$button   = '<a href="%s" class="wp-embed-more wc-embed-button">%s</a>';

		if ( $_product->is_type( 'simple' ) && $_product->is_purchasable() && $_product->is_in_stock() ) {
			$buttons[] = sprintf( $button, esc_url( add_query_arg( 'add-to-cart', get_the_ID(), wc_get_cart_url() ) ), esc_html__( 'Buy now', 'woocommerce' ) );
		}

		$buttons[] = sprintf( $button, get_the_permalink(), esc_html__( 'Read more', 'woocommerce' ) );

		return '<p>' . implode( ' ', $buttons ) . '</p>';
	}

	/**
	 * 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
					/* translators: %s: average rating */
					printf(
						esc_html__( 'Rated %s out of 5', 'woocommerce' ),
						esc_html( $_product->get_average_rating() )
					);
				?>
			</div>
			<?php
		}
	}

	/**
	 * Basic styling.
	 */
	public static function print_embed_styles() {
		if ( ! self::is_embedded_product() ) {
			return;
		}
		?>
		<style type="text/css">
			a.wc-embed-button {
				border-radius: 4px;
				border: 1px solid #ddd;
				box-shadow: 0px 1px 0 0px rgba(0, 0, 0, 0.05);
				display:inline-block;
				padding: .5em;
			}
			a.wc-embed-button:hover, a.wc-embed-button:focus {
				border: 1px solid #ccc;
				box-shadow: 0px 1px 0 0px rgba(0, 0, 0, 0.1);
				color: #999;
				text-decoration: none;
			}
			.wp-embed-excerpt p {
				margin: 0 0 1em;
			}
			.wc-embed-price {
				display: block;
				opacity: .75;
				font-weight: 700;
				margin-top: -.75em;
			}
			.wc-embed-rating {
				display: inline-block;
			}
		</style>
		<?php
	}
}

WC_Embed::init();