/includes/class-wc-embed.php

  1. <?php 
  2. /** 
  3. * WooCommerce Product Embed. 
  4. * 
  5. * @version 2.4.11 
  6. * @package WooCommerce/Classes/Embed 
  7. * @category Class 
  8. * @author WooThemes 
  9. */ 
  10.  
  11. if ( ! defined( 'ABSPATH' ) ) { 
  12. exit; 
  13.  
  14. /** 
  15. * Embed Class which handles any WooCommerce Products that are embedded on this site or another site. 
  16. * 
  17. * @class WC_Embed 
  18. * @version 2.4.11 
  19. * @author WooThemes 
  20. */ 
  21. class WC_Embed { 
  22.  
  23. /** 
  24. * Init embed class. 
  25. * 
  26. * @since 2.4.11 
  27. */ 
  28. public static function init() { 
  29.  
  30. // Filter all of the content that's going to be embedded. 
  31. add_filter( 'the_excerpt_embed', array( __CLASS__, 'the_excerpt' ), 10 ); 
  32.  
  33. // Make sure no comments display. Doesn't make sense for products. 
  34. add_action( 'embed_content_meta', array( __CLASS__, 'remove_comments_button' ), 5 ); 
  35.  
  36. // In the comments place let's display the product rating. 
  37. add_action( 'embed_content_meta', array( __CLASS__, 'get_ratings' ), 5 ); 
  38.  
  39. // Add some basic styles. 
  40. add_action( 'embed_head', array( __CLASS__, 'print_embed_styles' ) ); 
  41.  
  42. /** 
  43. * Remove comments button on product embeds. 
  44. * 
  45. * @since 2.6.0 
  46. */ 
  47. public static function remove_comments_button() { 
  48. if ( self::is_embedded_product() ) { 
  49. remove_action( 'embed_content_meta', 'print_embed_comments_button' ); 
  50.  
  51. /** 
  52. * Check if this is an embedded product - to make sure we don't mess up regular posts. 
  53. * 
  54. * @since 2.4.11 
  55. * @return bool 
  56. */ 
  57. public static function is_embedded_product() { 
  58. if ( function_exists( 'is_embed' ) && is_embed() && is_product() ) { 
  59. return true; 
  60. return false; 
  61.  
  62. /** 
  63. * Create the excerpt for embedded products - we want to add the buy button to it. 
  64. * 
  65. * @since 2.4.11 
  66. * @param string $excerpt Embed short description. 
  67. * @return string 
  68. */ 
  69. public static function the_excerpt( $excerpt ) { 
  70. global $post; 
  71.  
  72. // Get product. 
  73. $_product = wc_get_product( get_the_ID() ); 
  74.  
  75. // Make sure we're only affecting embedded products. 
  76. if ( self::is_embedded_product() ) { 
  77. echo '<p><span class="wc-embed-price">' . $_product->get_price_html() . '</span></p>'; 
  78.  
  79. if ( ! empty( $post->post_excerpt ) ) { 
  80. ob_start(); 
  81. woocommerce_template_single_excerpt(); 
  82. $excerpt = ob_get_clean(); 
  83.  
  84. // Add the button. 
  85. $excerpt .= self::product_buttons(); 
  86. return $excerpt; 
  87.  
  88. /** 
  89. * Create the button to go to the product page for embedded products. 
  90. * 
  91. * @since 2.4.11 
  92. * @return string 
  93. */ 
  94. public static function product_buttons() { 
  95. $_product = wc_get_product( get_the_ID() ); 
  96. $buttons = array(); 
  97. $button = '<a href="%s" class="wp-embed-more wc-embed-button">%s</a>'; 
  98.  
  99. if ( $_product->is_type( 'simple' ) && $_product->is_purchasable() && $_product->is_in_stock() ) { 
  100. $buttons[] = sprintf( $button, esc_url( add_query_arg( 'add-to-cart', get_the_ID(), wc_get_cart_url() ) ), esc_html__( 'Buy now', 'woocommerce' ) ); 
  101.  
  102. $buttons[] = sprintf( $button, get_the_permalink(), esc_html__( 'Read more', 'woocommerce' ) ); 
  103.  
  104. return '<p>' . implode( ' ', $buttons ) . '</p>'; 
  105.  
  106. /** 
  107. * Prints the markup for the rating stars. 
  108. * 
  109. * @since 2.4.11 
  110. */ 
  111. public static function get_ratings() { 
  112. // Make sure we're only affecting embedded products. 
  113. if ( self::is_embedded_product() && ( $_product = wc_get_product( get_the_ID() ) ) && $_product->get_average_rating() > 0 ) { 
  114. ?> 
  115. <div class="wc-embed-rating"> 
  116. <?php 
  117. /** translators: %s: average rating */ 
  118. printf( 
  119. esc_html_( 'Rated %s out of 5', 'woocommerce' ),  
  120. esc_html( $_product->get_average_rating() ) 
  121. ); 
  122. ?> 
  123. </div> 
  124. <?php 
  125.  
  126. /** 
  127. * Basic styling. 
  128. */ 
  129. public static function print_embed_styles() { 
  130. if ( ! self::is_embedded_product() ) { 
  131. return; 
  132. ?> 
  133. <style type="text/css"> 
  134. a.wc-embed-button { 
  135. border-radius: 4px; 
  136. border: 1px solid #ddd; 
  137. box-shadow: 0px 1px 0 0px rgba(0, 0, 0, 0.05); 
  138. display:inline-block; 
  139. padding: .5em; 
  140. a.wc-embed-button:hover, a.wc-embed-button:focus { 
  141. border: 1px solid #ccc; 
  142. box-shadow: 0px 1px 0 0px rgba(0, 0, 0, 0.1); 
  143. color: #999; 
  144. text-decoration: none; 
  145. .wp-embed-excerpt p { 
  146. margin: 0 0 1em; 
  147. .wc-embed-price { 
  148. display: block; 
  149. opacity: .75; 
  150. font-weight: 700; 
  151. margin-top: -.75em; 
  152. .wc-embed-rating { 
  153. display: inline-block; 
  154. </style> 
  155. <?php 
  156.  
  157. WC_Embed::init(); 
.