/var/www/html_us/wp-content/plugins/woocommerce/src/Blocks/Templates/SingleProductTemplate.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?php
namespace Automattic\WooCommerce\Blocks\Templates;

use 
Automattic\WooCommerce\Blocks\Templates\SingleProductTemplateCompatibility;
use 
Automattic\WooCommerce\Blocks\Utils\BlockTemplateUtils;

/**
 * SingleProductTemplate class.
 *
 * @internal
 */
class SingleProductTemplate extends AbstractTemplate {

    
/**
     * The slug of the template.
     *
     * @var string
     */
    
const SLUG 'single-product';

    
/**
     * Initialization method.
     */
    
public function init() {
        
add_action'template_redirect', array( $this'render_block_template' ) );
        
add_filter'get_block_templates', array( $this'update_single_product_content' ), 11);
    }

    
/**
     * Returns the title of the template.
     *
     * @return string
     */
    
public function get_template_title() {
        return 
_x'Single Product''Template name''woocommerce' );
    }

    
/**
     * Returns the description of the template.
     *
     * @return string
     */
    
public function get_template_description() {
        return 
__'Displays a single product.''woocommerce' );
    }

    
/**
     * Renders the default block template from Woo Blocks if no theme templates exist.
     */
    
public function render_block_template() {
        if ( ! 
is_embed() && is_singular'product' ) ) {
            global 
$post;

            
$compatibility_layer = new SingleProductTemplateCompatibility();
            
$compatibility_layer->init();

            
$valid_slugs         = array( self::SLUG );
            
$single_product_slug 'product' === $post->post_type && $post->post_name 'single-product-' $post->post_name '';
            if ( 
$single_product_slug ) {
                
$valid_slugs[] = 'single-product-' $post->post_name;
            }
            
$templates get_block_templates( array( 'slug__in' => $valid_slugs ) );

            if ( 
count$templates ) === ) {
                return;
            }

            
// Use the first template by default.
            
$template $templates[0];

            
// Check if there is a template matching the slug `single-product-{post_name}`.
            
if ( count$valid_slugs ) > && count$templates ) > ) {
                foreach ( 
$templates as $t ) {
                    if ( 
$single_product_slug === $t->slug ) {
                        
$template $t;
                        break;
                    }
                }
            }

            if ( isset( 
$template ) && BlockTemplateUtils::template_has_legacy_template_block$template ) ) {
                
add_filter'woocommerce_disable_compatibility_layer''__return_true' );
            }

            
add_filter'woocommerce_has_block_template''__return_true'10);
        }
    }

    
/**
     * Add the block template objects to be used.
     *
     * @param array $query_result Array of template objects.
     * @return array
     */
    
public function update_single_product_content$query_result ) {
        
$query_result array_map(
            function ( 
$template ) {
                if ( 
str_contains$template->slugself::SLUG ) ) {
                    
// We don't want to add the compatibility layer on the Editor Side.
                    // The second condition is necessary to not apply the compatibility layer on the REST API. Gutenberg uses the REST API to clone the template.
                    // More details: https://github.com/woocommerce/woocommerce-blocks/issues/9662.
                    
if ( ( ! is_admin() && ! ( defined'REST_REQUEST' ) && REST_REQUEST ) ) && ! BlockTemplateUtils::template_has_legacy_template_block$template ) ) {
                        
// Add the product class to the body. We should move this to a more appropriate place.
                        
add_filter(
                            
'body_class',
                            function ( 
$classes ) {
                                return 
array_merge$classeswc_get_product_class() );
                            }
                        );

                        global 
$product;

                        if ( ! 
$product instanceof \WC_Product ) {
                            
$product_id get_the_ID();
                            if ( 
$product_id ) {
                                
wc_setup_product_data$product_id );
                            }
                        }

                        if ( 
post_password_required() ) {
                            
$template->content $this->add_password_form$template->content );
                        } else {
                            
$template->content SingleProductTemplateCompatibility::add_compatibility_layer$template->content );
                        }
                    }
                }
                return 
$template;
            },
            
$query_result
        
);
        return 
$query_result;
    }

    
/**
     * Replace the first single product template block with the password form. Remove all other single product template blocks.
     *
     * @param array   $parsed_blocks Array of parsed block objects.
     * @param boolean $is_already_replaced If the password form has already been added.
     * @return array Parsed blocks
     */
    
private static function replace_first_single_product_template_block_with_password_form$parsed_blocks$is_already_replaced ) {
        
// 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.
        // 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.
        
$single_product_template_blocks = array( 'woocommerce/product-image-gallery''woocommerce/product-details''woocommerce/add-to-cart-form''woocommerce/product-meta''woocommerce/product-rating''woocommerce/product-price''woocommerce/related-products' );
        return 
array_reduce(
            
$parsed_blocks,
            function ( 
$carry$block ) use ( $single_product_template_blocks ) {
                if ( 
in_array$block['blockName'], $single_product_template_blockstrue ) ) {
                    if ( 
$carry['is_already_replaced'] ) {
                        return array(
                            
'blocks'              => $carry['blocks'],
                            
'html_block'          => null,
                            
'removed'             => true,
                            
'is_already_replaced' => true,

                        );
                    }

                    return array(
                        
'blocks'              => $carry['blocks'],
                        
'html_block'          => parse_blocks'<!-- wp:html -->' get_the_password_form() . '<!-- /wp:html -->' )[0],
                        
'removed'             => false,
                        
'is_already_replaced' => $carry['is_already_replaced'],
                    );

                }

                if ( isset( 
$block['innerBlocks'] ) && count$block['innerBlocks'] ) > ) {
                    
$index              0;
                    
$new_inner_blocks   = array();
                    
$new_inner_contents $block['innerContent'];
                    foreach ( 
$block['innerContent'] as $inner_content ) {
                        
// Don't process the closing tag of the block.
                        
if ( count$block['innerBlocks'] ) === $index ) {
                            break;
                        }

                        
$blocks                       self::replace_first_single_product_template_block_with_password_form( array( $block['innerBlocks'][ $index ] ), $carry['is_already_replaced'] );
                        
$new_blocks                   $blocks['blocks'];
                        
$html_block                   $blocks['html_block'];
                        
$is_removed                   $blocks['removed'];
                        
$carry['is_already_replaced'] = $blocks['is_already_replaced'];

                        if ( isset( 
$html_block ) ) {
                            
$new_inner_blocks             array_merge$new_inner_blocks$new_blocks, array( $html_block ) );
                            
$carry['is_already_replaced'] = true;
                        } else {
                            
$new_inner_blocks array_merge$new_inner_blocks$new_blocks );
                        }

                        if ( 
$is_removed ) {
                            unset( 
$new_inner_contents$index ] );
                            
// The last element of the inner contents contains the closing tag of the block. We don't want to remove it.
                            
if ( $index count$new_inner_contents ) ) {
                                unset( 
$new_inner_contents$index ] );
                            }
                            
$new_inner_contents array_values$new_inner_contents );
                        }

                        
$index++;
                    }

                    
$block['innerBlocks']  = $new_inner_blocks;
                    
$block['innerContent'] = $new_inner_contents;

                    return array(
                        
'blocks'              => array_merge$carry['blocks'], array( $block ) ),
                        
'html_block'          => null,
                        
'removed'             => false,
                        
'is_already_replaced' => $carry['is_already_replaced'],
                    );
                }

                return array(
                    
'blocks'              => array_merge$carry['blocks'], array( $block ) ),
                    
'html_block'          => null,
                    
'removed'             => false,
                    
'is_already_replaced' => $carry['is_already_replaced'],
                );
            },
            array(
                
'blocks'              => array(),
                
'html_block'          => null,
                
'removed'             => false,
                
'is_already_replaced' => $is_already_replaced,
            )
        );
    }

    
/**
     * Add password form to the Single Product Template.
     *
     * @param string $content The content of the template.
     * @return string
     */
    
public static function add_password_form$content ) {
        
$parsed_blocks     parse_blocks$content );
        
$blocks            self::replace_first_single_product_template_block_with_password_form$parsed_blocksfalse );
        
$serialized_blocks serialize_blocks$blocks['blocks'] );

        return 
$serialized_blocks;
    }
}