AbstractPageTemplate.php
1 year ago
AbstractTemplate.php
10 months ago
AbstractTemplateCompatibility.php
1 year ago
AbstractTemplatePart.php
2 years ago
AbstractTemplateWithFallback.php
7 months ago
ArchiveProductTemplatesCompatibility.php
1 year ago
CartTemplate.php
10 months ago
CheckoutHeaderTemplate.php
2 years ago
CheckoutTemplate.php
10 months ago
ClassicTemplatesCompatibility.php
1 year ago
ComingSoonSocialLinksTemplate.php
1 year ago
ComingSoonTemplate.php
1 year ago
ExternalProductAddToCartWithOptionsTemplate.php
1 year ago
GroupedProductAddToCartWithOptionsTemplate.php
1 year ago
MiniCartTemplate.php
2 years ago
OrderConfirmationTemplate.php
1 year ago
ProductAttributeTemplate.php
7 months ago
ProductBrandTemplate.php
7 months ago
ProductCatalogTemplate.php
7 months ago
ProductCategoryTemplate.php
7 months ago
ProductSearchResultsTemplate.php
7 months ago
ProductTagTemplate.php
7 months ago
SimpleProductAddToCartWithOptionsTemplate.php
1 year ago
SingleProductTemplate.php
2 months ago
SingleProductTemplateCompatibility.php
10 months ago
VariableProductAddToCartWithOptionsTemplate.php
1 year ago
SingleProductTemplate.php
287 lines
| 1 | <?php |
| 2 | namespace Automattic\WooCommerce\Blocks\Templates; |
| 3 | |
| 4 | use Automattic\WooCommerce\Blocks\SharedStores\ProductsStore; |
| 5 | use Automattic\WooCommerce\Blocks\Templates\SingleProductTemplateCompatibility; |
| 6 | use Automattic\WooCommerce\Blocks\Utils\BlockTemplateUtils; |
| 7 | |
| 8 | /** |
| 9 | * SingleProductTemplate class. |
| 10 | * |
| 11 | * @internal |
| 12 | */ |
| 13 | class SingleProductTemplate extends AbstractTemplate { |
| 14 | |
| 15 | /** |
| 16 | * The slug of the template. |
| 17 | * |
| 18 | * @var string |
| 19 | */ |
| 20 | const SLUG = 'single-product'; |
| 21 | |
| 22 | /** |
| 23 | * Initialization method. |
| 24 | */ |
| 25 | public function init() { |
| 26 | add_action( 'template_redirect', array( $this, 'render_block_template' ) ); |
| 27 | add_filter( 'get_block_templates', array( $this, 'update_single_product_content' ), 11, 1 ); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Returns the title of the template. |
| 32 | * |
| 33 | * @return string |
| 34 | */ |
| 35 | public function get_template_title() { |
| 36 | return _x( 'Single Product', 'Template name', 'woocommerce' ); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Returns the description of the template. |
| 41 | * |
| 42 | * @return string |
| 43 | */ |
| 44 | public function get_template_description() { |
| 45 | return __( 'Displays a single product.', 'woocommerce' ); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Run template-specific logic when the query matches this template. |
| 50 | */ |
| 51 | public function render_block_template() { |
| 52 | if ( ! is_embed() && is_singular( 'product' ) ) { |
| 53 | global $post; |
| 54 | |
| 55 | $compatibility_layer = new SingleProductTemplateCompatibility(); |
| 56 | $compatibility_layer->init(); |
| 57 | |
| 58 | $valid_slugs = array( self::SLUG ); |
| 59 | $single_product_slug = 'product' === $post->post_type && $post->post_name ? 'single-product-' . $post->post_name : ''; |
| 60 | if ( $single_product_slug ) { |
| 61 | $valid_slugs[] = 'single-product-' . $post->post_name; |
| 62 | } |
| 63 | $templates = get_block_templates( array( 'slug__in' => $valid_slugs ) ); |
| 64 | |
| 65 | if ( count( $templates ) === 0 ) { |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | // Use the first template by default. |
| 70 | $template = reset( $templates ); |
| 71 | |
| 72 | // Check if there is a template matching the slug `single-product-{post_name}`. |
| 73 | if ( count( $valid_slugs ) > 1 && count( $templates ) > 1 ) { |
| 74 | foreach ( $templates as $t ) { |
| 75 | if ( $single_product_slug === $t->slug ) { |
| 76 | $template = $t; |
| 77 | break; |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | if ( isset( $template ) && BlockTemplateUtils::template_has_legacy_template_block( $template ) ) { |
| 83 | add_filter( 'woocommerce_disable_compatibility_layer', '__return_true' ); |
| 84 | } |
| 85 | |
| 86 | $product = wc_get_product( $post->ID ); |
| 87 | if ( $product ) { |
| 88 | $consent = 'I acknowledge that using experimental APIs means my theme or plugin will inevitably break in the next version of WooCommerce'; |
| 89 | |
| 90 | // Load the product data into the products store so derived |
| 91 | // state closures can resolve it during server-side rendering. |
| 92 | ProductsStore::load_product( $consent, $product->get_id() ); |
| 93 | |
| 94 | // Set the current product context. The derived state |
| 95 | // closures (mainProductInContext, productVariationInContext, productInContext) |
| 96 | // are registered by ProductsStore::register_state(). |
| 97 | wp_interactivity_state( |
| 98 | 'woocommerce/products', |
| 99 | array( |
| 100 | 'productId' => $product->get_id(), |
| 101 | 'variationId' => null, |
| 102 | ) |
| 103 | ); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Add the block template objects to be used. |
| 110 | * |
| 111 | * @param array $query_result Array of template objects. |
| 112 | * @return array |
| 113 | */ |
| 114 | public function update_single_product_content( $query_result ) { |
| 115 | $query_result = array_map( |
| 116 | function ( $template ) { |
| 117 | if ( str_contains( $template->slug, self::SLUG ) ) { |
| 118 | // We don't want to add the compatibility layer on the Editor Side. |
| 119 | // The second condition is necessary to not apply the compatibility layer on the REST API. Gutenberg uses the REST API to clone the template. |
| 120 | // More details: https://github.com/woocommerce/woocommerce-blocks/issues/9662. |
| 121 | if ( ( ! is_admin() && ! ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) && ! BlockTemplateUtils::template_has_legacy_template_block( $template ) ) { |
| 122 | // Add the product class to the body. We should move this to a more appropriate place. |
| 123 | add_filter( |
| 124 | 'body_class', |
| 125 | function ( $classes ) { |
| 126 | return array_merge( $classes, wc_get_product_class() ); |
| 127 | } |
| 128 | ); |
| 129 | |
| 130 | global $product; |
| 131 | |
| 132 | if ( ! $product instanceof \WC_Product ) { |
| 133 | $product_id = get_the_ID(); |
| 134 | if ( $product_id ) { |
| 135 | wc_setup_product_data( $product_id ); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | if ( post_password_required() ) { |
| 140 | $template->content = $this->add_password_form( $template->content ); |
| 141 | } else { |
| 142 | $template->content = SingleProductTemplateCompatibility::add_compatibility_layer( $template->content ); |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | return $template; |
| 147 | }, |
| 148 | $query_result |
| 149 | ); |
| 150 | |
| 151 | return $query_result; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Replace the first single product template block with the password form. Remove all other single product template blocks. |
| 156 | * |
| 157 | * @param array $parsed_blocks Array of parsed block objects. |
| 158 | * @param boolean $is_already_replaced If the password form has already been added. |
| 159 | * @return array Parsed blocks |
| 160 | */ |
| 161 | private static function replace_first_single_product_template_block_with_password_form( $parsed_blocks, $is_already_replaced ) { |
| 162 | // We want to replace the first single product template block with the password form. We also want to remove all other single product template blocks. |
| 163 | // This array doesn't contains all the blocks. For example, it missing the breadcrumbs blocks: it doesn't make sense replace the breadcrumbs with the password form. |
| 164 | $single_product_template_blocks = array( |
| 165 | 'woocommerce/product-image-gallery', |
| 166 | 'woocommerce/product-details', |
| 167 | 'woocommerce/add-to-cart-form', |
| 168 | 'woocommerce/product-meta', |
| 169 | 'woocommerce/product-rating', |
| 170 | 'woocommerce/product-price', |
| 171 | 'woocommerce/related-products', |
| 172 | 'woocommerce/add-to-cart-with-options', |
| 173 | 'woocommerce/product-gallery', |
| 174 | 'woocommerce/product-collection', |
| 175 | 'core/post-title', |
| 176 | 'core/post-excerpt', |
| 177 | ); |
| 178 | |
| 179 | return array_reduce( |
| 180 | $parsed_blocks, |
| 181 | function ( $carry, $block ) use ( $single_product_template_blocks ) { |
| 182 | if ( in_array( $block['blockName'], $single_product_template_blocks, true ) || ( 'core/pattern' === $block['blockName'] && isset( $block['attrs']['slug'] ) && 'woocommerce-blocks/related-products' === $block['attrs']['slug'] ) ) { |
| 183 | if ( $carry['is_already_replaced'] ) { |
| 184 | return array( |
| 185 | 'blocks' => $carry['blocks'], |
| 186 | 'html_block' => null, |
| 187 | 'removed' => true, |
| 188 | 'is_already_replaced' => true, |
| 189 | |
| 190 | ); |
| 191 | } |
| 192 | |
| 193 | return array( |
| 194 | 'blocks' => $carry['blocks'], |
| 195 | 'html_block' => parse_blocks( '<!-- wp:html -->' . get_the_password_form() . '<!-- /wp:html -->' )[0], |
| 196 | 'removed' => false, |
| 197 | 'is_already_replaced' => $carry['is_already_replaced'], |
| 198 | ); |
| 199 | |
| 200 | } |
| 201 | |
| 202 | if ( isset( $block['innerBlocks'] ) && count( $block['innerBlocks'] ) > 0 ) { |
| 203 | $index = 0; |
| 204 | $new_inner_blocks = array(); |
| 205 | $new_inner_contents = $block['innerContent']; |
| 206 | foreach ( $block['innerContent'] as $inner_content ) { |
| 207 | // Don't process the closing tag of the block. |
| 208 | if ( count( $block['innerBlocks'] ) === $index ) { |
| 209 | break; |
| 210 | } |
| 211 | |
| 212 | $blocks = self::replace_first_single_product_template_block_with_password_form( array( $block['innerBlocks'][ $index ] ), $carry['is_already_replaced'] ); |
| 213 | $new_blocks = $blocks['blocks']; |
| 214 | $html_block = $blocks['html_block']; |
| 215 | $is_removed = $blocks['removed']; |
| 216 | $carry['is_already_replaced'] = $blocks['is_already_replaced']; |
| 217 | |
| 218 | if ( isset( $html_block ) ) { |
| 219 | $new_inner_blocks = array_merge( $new_inner_blocks, $new_blocks, array( $html_block ) ); |
| 220 | $carry['is_already_replaced'] = true; |
| 221 | } else { |
| 222 | $new_inner_blocks = array_merge( $new_inner_blocks, $new_blocks ); |
| 223 | } |
| 224 | |
| 225 | if ( $is_removed ) { |
| 226 | unset( $new_inner_contents[ $index ] ); |
| 227 | // The last element of the inner contents contains the closing tag of the block. We don't want to remove it. |
| 228 | if ( $index + 1 < count( $new_inner_contents ) ) { |
| 229 | unset( $new_inner_contents[ $index + 1 ] ); |
| 230 | } |
| 231 | $new_inner_contents = array_values( $new_inner_contents ); |
| 232 | } |
| 233 | |
| 234 | $index++; |
| 235 | } |
| 236 | |
| 237 | $block['innerBlocks'] = $new_inner_blocks; |
| 238 | $block['innerContent'] = $new_inner_contents; |
| 239 | |
| 240 | if ( count( $new_inner_blocks ) === 0 ) { |
| 241 | return array( |
| 242 | 'blocks' => $carry['blocks'], |
| 243 | 'html_block' => null, |
| 244 | 'removed' => true, |
| 245 | 'is_already_replaced' => $carry['is_already_replaced'], |
| 246 | ); |
| 247 | } |
| 248 | |
| 249 | return array( |
| 250 | 'blocks' => array_merge( $carry['blocks'], array( $block ) ), |
| 251 | 'html_block' => null, |
| 252 | 'removed' => false, |
| 253 | 'is_already_replaced' => $carry['is_already_replaced'], |
| 254 | ); |
| 255 | } |
| 256 | |
| 257 | return array( |
| 258 | 'blocks' => array_merge( $carry['blocks'], array( $block ) ), |
| 259 | 'html_block' => null, |
| 260 | 'removed' => false, |
| 261 | 'is_already_replaced' => $carry['is_already_replaced'], |
| 262 | ); |
| 263 | }, |
| 264 | array( |
| 265 | 'blocks' => array(), |
| 266 | 'html_block' => null, |
| 267 | 'removed' => false, |
| 268 | 'is_already_replaced' => $is_already_replaced, |
| 269 | ) |
| 270 | ); |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Add password form to the Single Product Template. |
| 275 | * |
| 276 | * @param string $content The content of the template. |
| 277 | * @return string |
| 278 | */ |
| 279 | public static function add_password_form( $content ) { |
| 280 | $parsed_blocks = parse_blocks( $content ); |
| 281 | $blocks = self::replace_first_single_product_template_block_with_password_form( $parsed_blocks, false ); |
| 282 | $serialized_blocks = serialize_blocks( $blocks['blocks'] ); |
| 283 | |
| 284 | return $serialized_blocks; |
| 285 | } |
| 286 | } |
| 287 |