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rc.2 3.1.0 6.2.1 3.1.1 6.2.2 3.1.2 6.2.3 3.2.0 6.3.0 3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / src / Blocks / Templates / SingleProductTemplate.php
woocommerce / src / Blocks / Templates Last commit date
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