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2.0-rc.1 3.0.9 6.2.0-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 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