AbstractPageTemplate.php
1 year ago
AbstractTemplate.php
10 months ago
AbstractTemplateCompatibility.php
4 days 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
4 days ago
ProductBrandTemplate.php
4 days ago
ProductCatalogTemplate.php
4 days ago
ProductCategoryTemplate.php
4 days ago
ProductSearchResultsTemplate.php
4 days ago
ProductTagTemplate.php
4 days ago
SimpleProductAddToCartWithOptionsTemplate.php
1 year ago
SingleProductTemplate.php
4 days ago
SingleProductTemplateCompatibility.php
10 months ago
VariableProductAddToCartWithOptionsTemplate.php
1 year ago
SingleProductTemplate.php
259 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 | $product = wc_get_product( $post->ID ); |
| 59 | if ( $product ) { |
| 60 | $consent = 'I acknowledge that using experimental APIs means my theme or plugin will inevitably break in the next version of WooCommerce'; |
| 61 | |
| 62 | // Load the product data into the products store so derived |
| 63 | // state closures can resolve it during server-side rendering. |
| 64 | ProductsStore::load_product( $consent, $product->get_id() ); |
| 65 | |
| 66 | // Set the current product context. The derived state |
| 67 | // closures (mainProductInContext, productVariationInContext, productInContext) |
| 68 | // are registered by ProductsStore::register_state(). |
| 69 | wp_interactivity_state( |
| 70 | 'woocommerce/products', |
| 71 | array( |
| 72 | 'productId' => $product->get_id(), |
| 73 | 'variationId' => null, |
| 74 | ) |
| 75 | ); |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Add the block template objects to be used. |
| 82 | * |
| 83 | * @param array $query_result Array of template objects. |
| 84 | * @return array |
| 85 | */ |
| 86 | public function update_single_product_content( $query_result ) { |
| 87 | $query_result = array_map( |
| 88 | function ( $template ) { |
| 89 | if ( str_contains( $template->slug, self::SLUG ) ) { |
| 90 | // We don't want to add the compatibility layer on the Editor Side. |
| 91 | // The second condition is necessary to not apply the compatibility layer on the REST API. Gutenberg uses the REST API to clone the template. |
| 92 | // More details: https://github.com/woocommerce/woocommerce-blocks/issues/9662. |
| 93 | if ( ( ! is_admin() && ! ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) && ! BlockTemplateUtils::template_has_legacy_template_block( $template ) ) { |
| 94 | // Add the product class to the body. We should move this to a more appropriate place. |
| 95 | add_filter( |
| 96 | 'body_class', |
| 97 | function ( $classes ) { |
| 98 | return array_merge( $classes, wc_get_product_class() ); |
| 99 | } |
| 100 | ); |
| 101 | |
| 102 | global $product; |
| 103 | |
| 104 | if ( ! $product instanceof \WC_Product ) { |
| 105 | $product_id = get_the_ID(); |
| 106 | if ( $product_id ) { |
| 107 | wc_setup_product_data( $product_id ); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | if ( post_password_required() ) { |
| 112 | $template->content = $this->add_password_form( $template->content ); |
| 113 | } else { |
| 114 | $template->content = SingleProductTemplateCompatibility::add_compatibility_layer( $template->content ); |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | return $template; |
| 119 | }, |
| 120 | $query_result |
| 121 | ); |
| 122 | |
| 123 | return $query_result; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Replace the first single product template block with the password form. Remove all other single product template blocks. |
| 128 | * |
| 129 | * @param array $parsed_blocks Array of parsed block objects. |
| 130 | * @param boolean $is_already_replaced If the password form has already been added. |
| 131 | * @return array Parsed blocks |
| 132 | */ |
| 133 | private static function replace_first_single_product_template_block_with_password_form( $parsed_blocks, $is_already_replaced ) { |
| 134 | // 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. |
| 135 | // 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. |
| 136 | $single_product_template_blocks = array( |
| 137 | 'woocommerce/product-image-gallery', |
| 138 | 'woocommerce/product-details', |
| 139 | 'woocommerce/add-to-cart-form', |
| 140 | 'woocommerce/product-meta', |
| 141 | 'woocommerce/product-rating', |
| 142 | 'woocommerce/product-price', |
| 143 | 'woocommerce/related-products', |
| 144 | 'woocommerce/add-to-cart-with-options', |
| 145 | 'woocommerce/product-gallery', |
| 146 | 'woocommerce/product-collection', |
| 147 | 'core/post-title', |
| 148 | 'core/post-excerpt', |
| 149 | ); |
| 150 | |
| 151 | return array_reduce( |
| 152 | $parsed_blocks, |
| 153 | function ( $carry, $block ) use ( $single_product_template_blocks ) { |
| 154 | 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'] ) ) { |
| 155 | if ( $carry['is_already_replaced'] ) { |
| 156 | return array( |
| 157 | 'blocks' => $carry['blocks'], |
| 158 | 'html_block' => null, |
| 159 | 'removed' => true, |
| 160 | 'is_already_replaced' => true, |
| 161 | |
| 162 | ); |
| 163 | } |
| 164 | |
| 165 | return array( |
| 166 | 'blocks' => $carry['blocks'], |
| 167 | 'html_block' => parse_blocks( '<!-- wp:html -->' . get_the_password_form() . '<!-- /wp:html -->' )[0], |
| 168 | 'removed' => false, |
| 169 | 'is_already_replaced' => $carry['is_already_replaced'], |
| 170 | ); |
| 171 | |
| 172 | } |
| 173 | |
| 174 | if ( isset( $block['innerBlocks'] ) && count( $block['innerBlocks'] ) > 0 ) { |
| 175 | $index = 0; |
| 176 | $new_inner_blocks = array(); |
| 177 | $new_inner_contents = $block['innerContent']; |
| 178 | foreach ( $block['innerContent'] as $inner_content ) { |
| 179 | // Don't process the closing tag of the block. |
| 180 | if ( count( $block['innerBlocks'] ) === $index ) { |
| 181 | break; |
| 182 | } |
| 183 | |
| 184 | $blocks = self::replace_first_single_product_template_block_with_password_form( array( $block['innerBlocks'][ $index ] ), $carry['is_already_replaced'] ); |
| 185 | $new_blocks = $blocks['blocks']; |
| 186 | $html_block = $blocks['html_block']; |
| 187 | $is_removed = $blocks['removed']; |
| 188 | $carry['is_already_replaced'] = $blocks['is_already_replaced']; |
| 189 | |
| 190 | if ( isset( $html_block ) ) { |
| 191 | $new_inner_blocks = array_merge( $new_inner_blocks, $new_blocks, array( $html_block ) ); |
| 192 | $carry['is_already_replaced'] = true; |
| 193 | } else { |
| 194 | $new_inner_blocks = array_merge( $new_inner_blocks, $new_blocks ); |
| 195 | } |
| 196 | |
| 197 | if ( $is_removed ) { |
| 198 | unset( $new_inner_contents[ $index ] ); |
| 199 | // The last element of the inner contents contains the closing tag of the block. We don't want to remove it. |
| 200 | if ( $index + 1 < count( $new_inner_contents ) ) { |
| 201 | unset( $new_inner_contents[ $index + 1 ] ); |
| 202 | } |
| 203 | $new_inner_contents = array_values( $new_inner_contents ); |
| 204 | } |
| 205 | |
| 206 | $index++; |
| 207 | } |
| 208 | |
| 209 | $block['innerBlocks'] = $new_inner_blocks; |
| 210 | $block['innerContent'] = $new_inner_contents; |
| 211 | |
| 212 | if ( count( $new_inner_blocks ) === 0 ) { |
| 213 | return array( |
| 214 | 'blocks' => $carry['blocks'], |
| 215 | 'html_block' => null, |
| 216 | 'removed' => true, |
| 217 | 'is_already_replaced' => $carry['is_already_replaced'], |
| 218 | ); |
| 219 | } |
| 220 | |
| 221 | return array( |
| 222 | 'blocks' => array_merge( $carry['blocks'], array( $block ) ), |
| 223 | 'html_block' => null, |
| 224 | 'removed' => false, |
| 225 | 'is_already_replaced' => $carry['is_already_replaced'], |
| 226 | ); |
| 227 | } |
| 228 | |
| 229 | return array( |
| 230 | 'blocks' => array_merge( $carry['blocks'], array( $block ) ), |
| 231 | 'html_block' => null, |
| 232 | 'removed' => false, |
| 233 | 'is_already_replaced' => $carry['is_already_replaced'], |
| 234 | ); |
| 235 | }, |
| 236 | array( |
| 237 | 'blocks' => array(), |
| 238 | 'html_block' => null, |
| 239 | 'removed' => false, |
| 240 | 'is_already_replaced' => $is_already_replaced, |
| 241 | ) |
| 242 | ); |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Add password form to the Single Product Template. |
| 247 | * |
| 248 | * @param string $content The content of the template. |
| 249 | * @return string |
| 250 | */ |
| 251 | public static function add_password_form( $content ) { |
| 252 | $parsed_blocks = parse_blocks( $content ); |
| 253 | $blocks = self::replace_first_single_product_template_block_with_password_form( $parsed_blocks, false ); |
| 254 | $serialized_blocks = serialize_blocks( $blocks['blocks'] ); |
| 255 | |
| 256 | return $serialized_blocks; |
| 257 | } |
| 258 | } |
| 259 |