class-wc-embed.php 4.2 KB

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