abstracts
3 years ago
admin
3 years ago
blocks
5 years ago
cli
3 years ago
customizer
3 years ago
data-stores
3 years ago
emails
3 years ago
export
3 years ago
gateways
3 years ago
import
3 years ago
integrations
4 years ago
interfaces
3 years ago
legacy
3 years ago
libraries
3 years ago
log-handlers
4 years ago
payment-tokens
5 years ago
queue
4 years ago
react-admin
3 years ago
rest-api
3 years ago
shipping
4 years ago
shortcodes
3 years ago
theme-support
3 years ago
tracks
3 years ago
traits
5 years ago
walkers
5 years ago
wccom-site
5 years ago
widgets
3 years ago
class-wc-ajax.php
3 years ago
class-wc-api.php
4 years ago
class-wc-auth.php
4 years ago
class-wc-autoloader.php
5 years ago
class-wc-background-emailer.php
5 years ago
class-wc-background-updater.php
5 years ago
class-wc-breadcrumb.php
5 years ago
class-wc-cache-helper.php
3 years ago
class-wc-cart-fees.php
5 years ago
class-wc-cart-session.php
3 years ago
class-wc-cart-totals.php
4 years ago
class-wc-cart.php
3 years ago
class-wc-checkout.php
3 years ago
class-wc-cli.php
3 years ago
class-wc-comments.php
3 years ago
class-wc-countries.php
3 years ago
class-wc-coupon.php
3 years ago
class-wc-customer-download-log.php
5 years ago
class-wc-customer-download.php
4 years ago
class-wc-customer.php
4 years ago
class-wc-data-exception.php
8 years ago
class-wc-data-store.php
3 years ago
class-wc-datetime.php
4 years ago
class-wc-deprecated-action-hooks.php
8 years ago
class-wc-deprecated-filter-hooks.php
3 years ago
class-wc-discounts.php
3 years ago
class-wc-download-handler.php
3 years ago
class-wc-emails.php
3 years ago
class-wc-embed.php
5 years ago
class-wc-form-handler.php
4 years ago
class-wc-frontend-scripts.php
3 years ago
class-wc-geo-ip.php
4 years ago
class-wc-geolite-integration.php
6 years ago
class-wc-geolocation.php
3 years ago
class-wc-https.php
4 years ago
class-wc-install.php
3 years ago
class-wc-integrations.php
5 years ago
class-wc-log-levels.php
5 years ago
class-wc-logger.php
4 years ago
class-wc-meta-data.php
4 years ago
class-wc-order-factory.php
3 years ago
class-wc-order-item-coupon.php
4 years ago
class-wc-order-item-fee.php
4 years ago
class-wc-order-item-meta.php
4 years ago
class-wc-order-item-product.php
4 years ago
class-wc-order-item-shipping.php
4 years ago
class-wc-order-item-tax.php
4 years ago
class-wc-order-item.php
4 years ago
class-wc-order-query.php
4 years ago
class-wc-order-refund.php
3 years ago
class-wc-order.php
3 years ago
class-wc-payment-gateways.php
4 years ago
class-wc-payment-tokens.php
3 years ago
class-wc-post-data.php
3 years ago
class-wc-post-types.php
3 years ago
class-wc-privacy-background-process.php
5 years ago
class-wc-privacy-erasers.php
4 years ago
class-wc-privacy-exporters.php
4 years ago
class-wc-privacy.php
4 years ago
class-wc-product-attribute.php
4 years ago
class-wc-product-download.php
4 years ago
class-wc-product-external.php
5 years ago
class-wc-product-factory.php
5 years ago
class-wc-product-grouped.php
8 years ago
class-wc-product-query.php
5 years ago
class-wc-product-simple.php
3 years ago
class-wc-product-variable.php
4 years ago
class-wc-product-variation.php
4 years ago
class-wc-query.php
3 years ago
class-wc-rate-limiter.php
4 years ago
class-wc-regenerate-images-request.php
3 years ago
class-wc-regenerate-images.php
3 years ago
class-wc-register-wp-admin-settings.php
4 years ago
class-wc-rest-authentication.php
5 years ago
class-wc-rest-exception.php
5 years ago
class-wc-session-handler.php
3 years ago
class-wc-shipping-rate.php
3 years ago
class-wc-shipping-zone.php
5 years ago
class-wc-shipping-zones.php
5 years ago
class-wc-shipping.php
4 years ago
class-wc-shortcodes.php
5 years ago
class-wc-structured-data.php
5 years ago
class-wc-tax.php
4 years ago
class-wc-template-loader.php
3 years ago
class-wc-tracker.php
3 years ago
class-wc-validation.php
3 years ago
class-wc-webhook.php
3 years ago
class-woocommerce.php
3 years ago
wc-account-functions.php
3 years ago
wc-attribute-functions.php
3 years ago
wc-cart-functions.php
3 years ago
wc-conditional-functions.php
3 years ago
wc-core-functions.php
3 years ago
wc-coupon-functions.php
3 years ago
wc-deprecated-functions.php
3 years ago
wc-formatting-functions.php
3 years ago
wc-notice-functions.php
5 years ago
wc-order-functions.php
3 years ago
wc-order-item-functions.php
3 years ago
wc-page-functions.php
5 years ago
wc-product-functions.php
3 years ago
wc-rest-functions.php
3 years ago
wc-stock-functions.php
3 years ago
wc-template-functions.php
3 years ago
wc-template-hooks.php
5 years ago
wc-term-functions.php
3 years ago
wc-update-functions.php
3 years ago
wc-user-functions.php
3 years ago
wc-webhook-functions.php
4 years ago
wc-widget-functions.php
5 years ago
class-wc-embed.php
179 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce product embed |
| 4 | * |
| 5 | * @version 2.4.11 |
| 6 | * @package WooCommerce\Classes\Embed |
| 7 | */ |
| 8 | |
| 9 | if ( ! defined( 'ABSPATH' ) ) { |
| 10 | exit; |
| 11 | } |
| 12 | |
| 13 | /** |
| 14 | * Embed Class which handles any WooCommerce Products that are embedded on this site or another site. |
| 15 | */ |
| 16 | class WC_Embed { |
| 17 | |
| 18 | /** |
| 19 | * Init embed class. |
| 20 | * |
| 21 | * @since 2.4.11 |
| 22 | */ |
| 23 | public static function init() { |
| 24 | |
| 25 | // Filter all of the content that's going to be embedded. |
| 26 | add_filter( 'the_excerpt_embed', array( __CLASS__, 'the_excerpt' ), 10 ); |
| 27 | |
| 28 | // Make sure no comments display. Doesn't make sense for products. |
| 29 | add_action( 'embed_content_meta', array( __CLASS__, 'remove_comments_button' ), 5 ); |
| 30 | |
| 31 | // In the comments place let's display the product rating. |
| 32 | add_action( 'embed_content_meta', array( __CLASS__, 'get_ratings' ), 5 ); |
| 33 | |
| 34 | // Add some basic styles. |
| 35 | add_action( 'embed_head', array( __CLASS__, 'print_embed_styles' ) ); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Remove comments button on product embeds. |
| 40 | * |
| 41 | * @since 2.6.0 |
| 42 | */ |
| 43 | public static function remove_comments_button() { |
| 44 | if ( self::is_embedded_product() ) { |
| 45 | remove_action( 'embed_content_meta', 'print_embed_comments_button' ); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Check if this is an embedded product - to make sure we don't mess up regular posts. |
| 51 | * |
| 52 | * @since 2.4.11 |
| 53 | * @return bool |
| 54 | */ |
| 55 | public static function is_embedded_product() { |
| 56 | if ( function_exists( 'is_embed' ) && is_embed() && is_product() ) { |
| 57 | return true; |
| 58 | } |
| 59 | return false; |
| 60 | } |
| 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>'; // WPCS: XSS ok. |
| 78 | |
| 79 | if ( ! empty( $post->post_excerpt ) ) { |
| 80 | ob_start(); |
| 81 | woocommerce_template_single_excerpt(); |
| 82 | $excerpt = ob_get_clean(); |
| 83 | } |
| 84 | |
| 85 | // Add the button. |
| 86 | $excerpt .= self::product_buttons(); |
| 87 | } |
| 88 | return $excerpt; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Create the button to go to the product page for embedded products. |
| 93 | * |
| 94 | * @since 2.4.11 |
| 95 | * @return string |
| 96 | */ |
| 97 | public static function product_buttons() { |
| 98 | $_product = wc_get_product( get_the_ID() ); |
| 99 | $buttons = array(); |
| 100 | $button = '<a href="%s" class="wp-embed-more wc-embed-button">%s</a>'; |
| 101 | |
| 102 | if ( $_product->is_type( 'simple' ) && $_product->is_purchasable() && $_product->is_in_stock() ) { |
| 103 | $buttons[] = sprintf( $button, esc_url( add_query_arg( 'add-to-cart', get_the_ID(), wc_get_cart_url() ) ), esc_html__( 'Buy now', 'woocommerce' ) ); |
| 104 | } |
| 105 | |
| 106 | $buttons[] = sprintf( $button, get_the_permalink(), esc_html__( 'Read more', 'woocommerce' ) ); |
| 107 | |
| 108 | return '<p>' . implode( ' ', $buttons ) . '</p>'; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Prints the markup for the rating stars. |
| 113 | * |
| 114 | * @since 2.4.11 |
| 115 | */ |
| 116 | public static function get_ratings() { |
| 117 | // Make sure we're only affecting embedded products. |
| 118 | if ( ! self::is_embedded_product() ) { |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | $_product = wc_get_product( get_the_ID() ); |
| 123 | |
| 124 | if ( $_product && $_product->get_average_rating() > 0 ) { |
| 125 | ?> |
| 126 | <div class="wc-embed-rating"> |
| 127 | <?php |
| 128 | printf( |
| 129 | /* translators: %s: average rating */ |
| 130 | esc_html__( 'Rated %s out of 5', 'woocommerce' ), |
| 131 | esc_html( $_product->get_average_rating() ) |
| 132 | ); |
| 133 | ?> |
| 134 | </div> |
| 135 | <?php |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Basic styling. |
| 141 | */ |
| 142 | public static function print_embed_styles() { |
| 143 | if ( ! self::is_embedded_product() ) { |
| 144 | return; |
| 145 | } |
| 146 | ?> |
| 147 | <style type="text/css"> |
| 148 | a.wc-embed-button { |
| 149 | border-radius: 4px; |
| 150 | border: 1px solid #ddd; |
| 151 | box-shadow: 0px 1px 0 0px rgba(0, 0, 0, 0.05); |
| 152 | display:inline-block; |
| 153 | padding: .5em; |
| 154 | } |
| 155 | a.wc-embed-button:hover, a.wc-embed-button:focus { |
| 156 | border: 1px solid #ccc; |
| 157 | box-shadow: 0px 1px 0 0px rgba(0, 0, 0, 0.1); |
| 158 | color: #999; |
| 159 | text-decoration: none; |
| 160 | } |
| 161 | .wp-embed-excerpt p { |
| 162 | margin: 0 0 1em; |
| 163 | } |
| 164 | .wc-embed-price { |
| 165 | display: block; |
| 166 | opacity: .75; |
| 167 | font-weight: 700; |
| 168 | margin-top: -.75em; |
| 169 | } |
| 170 | .wc-embed-rating { |
| 171 | display: inline-block; |
| 172 | } |
| 173 | </style> |
| 174 | <?php |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | WC_Embed::init(); |
| 179 |