Skip to content
This repository was archived by the owner on Feb 23, 2024. It is now read-only.

Commit b73fbca

Browse files
gigituxthealexandrelaranefeline
authored
Single Product Template: Add compatibility layer (#8442)
* Add minimum structure for Single Product Details block * Add Product Image Gallery #8233 Add Product Image Gallery * Add tests for Single Product Details block * Add the initial basis for the Add to Cart button * Trigger the single product add to cart action for each product type. * wip: create block structure and add initial styles * Add block details to the SingleProductDetails.php file * Rename the block from add-to-cart-button to add-to-cart-form * Update to use the cart icon. * Implement the skeleton for the editor preview. * Render tabs title with empty content * Use woocommerce_output_product_data_tabs function to retrieve tabs data * Update styles and add Notice for the display in the Editor. * Update CSS. * Add base tests for the new Add to Cart Form component. * Add Product Image Gallery block * remove support global styles * remove support global styles * Update the button CSS. * Remove customizations for the Single Product Details block * Update styles for the cart form. * update td style. * Update divs and CSS. * Use conventional input instead of the experimental InputControl * address CSS feedback * add support for the custom classname * remove save function * Remove unnecessary console.log from the Edit.tsx file * Remove block classname from block wrapper * Remove unnecessary WooCommerce tabs filter from the BlockTemplatesController * Remove attributes property from the block registration * Remove isExperimental flag for the Single Product Details block * Remove get_classes_and_styles_by_attributes method from SingleProductDetails block * Prevent Single Product Details block from apppearing in Pages or Posts * add second parameter to the subscribe function * Implement the new design and copy provided for the editor. * Make the notice compatible with dark themes. * Some additional CSS tweaks * adjust the padding for the input * wrap the Single Product Template in a div with the product class * Fix PHP Coding Standards warnings * improve logic and increase coverage of unit test * improve logic and increase coverage of unit test * fix test * format HTML * fix edge case * update @types/wordpress__data package * update placeholder, icon and description * update tsconfig * update block name * fix SCSS linter error * address feedback * create SingleProductTemplateCompatibility class * Add Hooks compatibility * remove not used file * remove not used files * Add compatibility layer for the Single Product template * fix check * address feedback * remove unused import * double empty line * remove logic in the constructor * remove hook * generate the docs * add missing hooks * fix docs * address feedback * fix linter * fix import * Disable compatibility layer when the WooCommerce Product Grid Block block and WooCommerce Single Product Block are used (#8538) * disable compatibility layer via hook * update docs * generate the docs * fix version * fix import * fix code after merge --------- Co-authored-by: Alexandre Lara <[email protected]> Co-authored-by: Patricia Hillebrandt <[email protected]>
1 parent 50aced3 commit b73fbca

File tree

10 files changed

+694
-315
lines changed

10 files changed

+694
-315
lines changed

bin/hook-docs/data/actions.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1053,7 +1053,7 @@
10531053
},
10541054
{
10551055
"name": "{$hook}",
1056-
"file": "Templates/BlockTemplatesCompatibility.php",
1056+
"file": "Templates/AbstractTemplateCompatibility.php",
10571057
"type": "action",
10581058
"doc": {
10591059
"description": "Action to render the content of a hook.",

bin/hook-docs/data/filters.json

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -547,23 +547,23 @@
547547
},
548548
{
549549
"name": "woocommerce_blocks_hook_compatibility_additional_data",
550-
"file": "Templates/BlockTemplatesCompatibility.php",
550+
"file": "Templates/AbstractTemplateCompatibility.php",
551551
"type": "filter",
552552
"doc": {
553553
"description": "When extensions implement their equivalent blocks of the template hook functions, they can use this filter to register their old hooked data here, so in the blockified template, the old hooked functions can be removed in favor of the new blocks while keeping the old hooked functions working in classic templates.",
554554
"long_description": "Accepts an array of hooked data. The array should be in the following format: [ [ hook => <hook-name>, function => <function-name>, priority => <priority>, ], ... ] Where: - hook-name is the name of the hook that have the functions hooked to. - function-name is the hooked function name. - priority is the priority of the hooked function.",
555555
"tags": [
556+
{
557+
"name": "since",
558+
"content": "9.5.0"
559+
},
556560
{
557561
"name": "param",
558562
"content": "Additional hooked data. Default to empty",
559563
"types": [
560564
"array"
561565
],
562566
"variable": "$data"
563-
},
564-
{
565-
"name": "since",
566-
"content": "9.5.0"
567567
}
568568
],
569569
"long_description_html": "<p>Accepts an array of hooked data. The array should be in the following format: [ [ hook =&gt; <hook-name>, function =&gt; <function-name>, priority =&gt; <priority>, ], ... ] Where:</p> <ul> <li>hook-name is the name of the hook that have the functions hooked to.</li> <li>function-name is the hooked function name.</li> <li>priority is the priority of the hooked function.</li> </ul>"
@@ -736,11 +736,11 @@
736736
},
737737
{
738738
"name": "woocommerce_disable_compatibility_layer",
739-
"file": "Templates/BlockTemplatesCompatibility.php",
739+
"file": "Templates/AbstractTemplateCompatibility.php",
740740
"type": "filter",
741741
"doc": {
742-
"description": "Filters to disable the compatibility layer for the blockified templates.",
743-
"long_description": "This hooks allows to disable the compatibility layer for the blockified.",
742+
"description": "Filter to disable the compatibility layer for the blockified templates.",
743+
"long_description": "This hook allows to disable the compatibility layer for the blockified.",
744744
"tags": [
745745
{
746746
"name": "since",
@@ -756,17 +756,17 @@
756756
"variable": ""
757757
}
758758
],
759-
"long_description_html": "<p>This hooks allows to disable the compatibility layer for the blockified.</p>"
759+
"long_description_html": "<p>This hook allows to disable the compatibility layer for the blockified.</p>"
760760
},
761761
"args": 1
762762
},
763763
{
764764
"name": "woocommerce_disable_compatibility_layer",
765-
"file": "Templates/BlockTemplatesCompatibility.php",
765+
"file": "Templates/AbstractTemplateCompatibility.php",
766766
"type": "filter",
767767
"doc": {
768-
"description": "Filters to disable the compatibility layer for the blockified templates.",
769-
"long_description": "This hooks allows to disable the compatibility layer for the blockified.",
768+
"description": "Filter to disable the compatibility layer for the blockified templates.",
769+
"long_description": "This hook allows to disable the compatibility layer for the blockified templates.",
770770
"tags": [
771771
{
772772
"name": "since",
@@ -782,7 +782,7 @@
782782
"variable": ""
783783
}
784784
],
785-
"long_description_html": "<p>This hooks allows to disable the compatibility layer for the blockified.</p>"
785+
"long_description_html": "<p>This hook allows to disable the compatibility layer for the blockified templates.</p>"
786786
},
787787
"args": 1
788788
},

docs/third-party-developers/extensibility/hooks/actions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -883,7 +883,7 @@ do_action( '{$hook}' )
883883
### Source
884884

885885

886-
- [Templates/BlockTemplatesCompatibility.php](../../../../src/Templates/BlockTemplatesCompatibility.php)
886+
- [Templates/AbstractTemplateCompatibility.php](../../../../src/Templates/AbstractTemplateCompatibility.php)
887887

888888
---
889889
<!-- FEEDBACK -->

docs/third-party-developers/extensibility/hooks/filters.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ apply_filters( 'woocommerce_blocks_hook_compatibility_additional_data', array $d
460460
### Source
461461

462462

463-
- [Templates/BlockTemplatesCompatibility.php](../../../../src/Templates/BlockTemplatesCompatibility.php)
463+
- [Templates/AbstractTemplateCompatibility.php](../../../../src/Templates/AbstractTemplateCompatibility.php)
464464

465465
---
466466

@@ -582,15 +582,15 @@ apply_filters( 'woocommerce_cart_contents_changed', array $cart_contents )
582582
## woocommerce_disable_compatibility_layer
583583

584584

585-
Filters to disable the compatibility layer for the blockified templates.
585+
Filter to disable the compatibility layer for the blockified templates.
586586

587587
```php
588588
apply_filters( 'woocommerce_disable_compatibility_layer', \Automattic\WooCommerce\Blocks\Templates\boolean. $argument0 )
589589
```
590590

591591
### Description
592592

593-
<p>This hooks allows to disable the compatibility layer for the blockified.</p>
593+
<p>This hook allows to disable the compatibility layer for the blockified.</p>
594594

595595
### Parameters
596596

@@ -601,8 +601,8 @@ apply_filters( 'woocommerce_disable_compatibility_layer', \Automattic\WooCommerc
601601
### Source
602602

603603

604-
- [Templates/BlockTemplatesCompatibility.php](../../../../src/Templates/BlockTemplatesCompatibility.php)
605-
- [Templates/BlockTemplatesCompatibility.php](../../../../src/Templates/BlockTemplatesCompatibility.php)
604+
- [Templates/AbstractTemplateCompatibility.php](../../../../src/Templates/AbstractTemplateCompatibility.php)
605+
- [Templates/AbstractTemplateCompatibility.php](../../../../src/Templates/AbstractTemplateCompatibility.php)
606606

607607
---
608608

src/BlockTemplatesController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
namespace Automattic\WooCommerce\Blocks;
33

44
use Automattic\WooCommerce\Blocks\Domain\Package;
5-
use Automattic\WooCommerce\Blocks\Templates\BlockTemplatesCompatibility;
65
use Automattic\WooCommerce\Blocks\Templates\ProductAttributeTemplate;
6+
use Automattic\WooCommerce\Blocks\Templates\SingleProductTemplateCompatibility;
77
use Automattic\WooCommerce\Blocks\Utils\BlockTemplateUtils;
88

99
/**
@@ -327,7 +327,7 @@ function( $template ) {
327327

328328
if ( 'single-product' === $template->slug ) {
329329
if ( ! is_admin() ) {
330-
$new_content = BlockTemplatesCompatibility::wrap_single_product_template( $template->content );
330+
$new_content = SingleProductTemplateCompatibility::add_compatibility_layer( $template->content );
331331
$template->content = $new_content;
332332
}
333333
return $template;

src/Domain/Bootstrap.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
use Automattic\WooCommerce\StoreApi\SchemaController;
2929
use Automattic\WooCommerce\StoreApi\StoreApi;
3030
use Automattic\WooCommerce\Blocks\Shipping\ShippingController;
31+
use Automattic\WooCommerce\Blocks\Templates\SingleProductTemplateCompatibility;
32+
use Automattic\WooCommerce\Blocks\Templates\BlockTemplatesCompatibility;
3133

3234
/**
3335
* Takes care of bootstrapping the plugin.
@@ -129,6 +131,8 @@ function() {
129131
$this->container->get( ProductSearchResultsTemplate::class );
130132
$this->container->get( ProductAttributeTemplate::class );
131133
$this->container->get( ClassicTemplatesCompatibility::class );
134+
$this->container->get( BlockTemplatesCompatibility::class )->init();
135+
$this->container->get( SingleProductTemplateCompatibility::class )->init();
132136
$this->container->get( BlockPatterns::class );
133137
$this->container->get( PaymentsApi::class );
134138
$this->container->get( ShippingController::class )->init();
@@ -274,6 +278,19 @@ function ( Container $container ) {
274278
return new ClassicTemplatesCompatibility( $asset_data_registry );
275279
}
276280
);
281+
$this->container->register(
282+
BlockTemplatesCompatibility::class,
283+
function () {
284+
return new BlockTemplatesCompatibility();
285+
}
286+
);
287+
288+
$this->container->register(
289+
SingleProductTemplateCompatibility::class,
290+
function () {
291+
return new SingleProductTemplateCompatibility();
292+
}
293+
);
277294
$this->container->register(
278295
DraftOrders::class,
279296
function( Container $container ) {
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
<?php
2+
namespace Automattic\WooCommerce\Blocks\Templates;
3+
4+
/**
5+
* BlockTemplatesCompatibility class.
6+
*
7+
* To bridge the gap on compatibility with PHP hooks and blockified templates.
8+
*
9+
* @internal
10+
*/
11+
abstract class AbstractTemplateCompatibility {
12+
/**
13+
* The data of supported hooks, containing the hook name, the block name,
14+
* position, and the callbacks.
15+
*
16+
* @var array $hook_data The hook data.
17+
*/
18+
protected $hook_data;
19+
20+
/**
21+
* Initialization method.
22+
*/
23+
public function init() {
24+
if ( ! wc_current_theme_is_fse_theme() ) {
25+
return;
26+
}
27+
28+
$this->set_hook_data();
29+
30+
add_filter(
31+
'render_block_data',
32+
function( $parsed_block, $source_block, $parent_block ) {
33+
/**
34+
* Filter to disable the compatibility layer for the blockified templates.
35+
*
36+
* This hook allows to disable the compatibility layer for the blockified templates.
37+
*
38+
* @since TBD
39+
* @param boolean.
40+
*/
41+
$is_disabled_compatility_layer = apply_filters( 'woocommerce_disable_compatibility_layer', false );
42+
43+
if ( $is_disabled_compatility_layer ) {
44+
return $parsed_block;
45+
}
46+
47+
return $this->update_render_block_data( $parsed_block, $source_block, $parent_block );
48+
49+
},
50+
10,
51+
3
52+
);
53+
54+
add_filter(
55+
'render_block',
56+
function ( $block_content, $block ) {
57+
/**
58+
* Filter to disable the compatibility layer for the blockified templates.
59+
*
60+
* This hook allows to disable the compatibility layer for the blockified.
61+
*
62+
* @since TBD
63+
* @param boolean.
64+
*/
65+
$is_disabled_compatility_layer = apply_filters( 'woocommerce_disable_compatibility_layer', false );
66+
67+
if ( $is_disabled_compatility_layer ) {
68+
return $block_content;
69+
}
70+
71+
return $this->inject_hooks( $block_content, $block );
72+
},
73+
10,
74+
2
75+
);
76+
}
77+
78+
/**
79+
* Update the render block data to inject our custom attribute needed to
80+
* determine which blocks belong to an inherited Products block.
81+
*
82+
* @param array $parsed_block The block being rendered.
83+
* @param array $source_block An un-modified copy of $parsed_block, as it appeared in the source content.
84+
* @param WP_Block|null $parent_block If this is a nested block, a reference to the parent block.
85+
*
86+
* @return array
87+
*/
88+
abstract public function update_render_block_data( $parsed_block, $source_block, $parent_block );
89+
90+
/**
91+
* Inject hooks to rendered content of corresponding blocks.
92+
*
93+
* @param mixed $block_content The rendered block content.
94+
* @param mixed $block The parsed block data.
95+
* @return string
96+
*/
97+
abstract public function inject_hooks( $block_content, $block );
98+
99+
/**
100+
* The hook data to inject to the rendered content of blocks. This also
101+
* contains hooked functions that will be removed by remove_default_hooks.
102+
*
103+
* The array format:
104+
* [
105+
* <hook-name> => [
106+
* block_name => <block-name>,
107+
* position => before|after,
108+
* hooked => [
109+
* <function-name> => <priority>,
110+
* ...
111+
* ],
112+
* ],
113+
* ]
114+
* Where:
115+
* - hook-name is the name of the hook that will be replaced.
116+
* - block-name is the name of the block that will replace the hook.
117+
* - position is the position of the block relative to the hook.
118+
* - hooked is an array of functions hooked to the hook that will be
119+
* replaced. The key is the function name and the value is the
120+
* priority.
121+
*/
122+
abstract protected function set_hook_data();
123+
124+
125+
/**
126+
* Remove the default callback added by WooCommerce. We replaced these
127+
* callbacks by blocks so we have to remove them to prevent duplicated
128+
* content.
129+
*/
130+
protected function remove_default_hooks() {
131+
foreach ( $this->hook_data as $hook => $data ) {
132+
if ( ! isset( $data['hooked'] ) ) {
133+
continue;
134+
}
135+
foreach ( $data['hooked'] as $callback => $priority ) {
136+
remove_action( $hook, $callback, $priority );
137+
}
138+
}
139+
140+
/**
141+
* When extensions implement their equivalent blocks of the template
142+
* hook functions, they can use this filter to register their old hooked
143+
* data here, so in the blockified template, the old hooked functions
144+
* can be removed in favor of the new blocks while keeping the old
145+
* hooked functions working in classic templates.
146+
*
147+
* Accepts an array of hooked data. The array should be in the following
148+
* format:
149+
* [
150+
* [
151+
* hook => <hook-name>,
152+
* function => <function-name>,
153+
* priority => <priority>,
154+
* ],
155+
* ...
156+
* ]
157+
* Where:
158+
* - hook-name is the name of the hook that have the functions hooked to.
159+
* - function-name is the hooked function name.
160+
* - priority is the priority of the hooked function.
161+
*
162+
* @since 9.5.0
163+
* @param array $data Additional hooked data. Default to empty
164+
*/
165+
$additional_hook_data = apply_filters( 'woocommerce_blocks_hook_compatibility_additional_data', array() );
166+
167+
if ( empty( $additional_hook_data ) || ! is_array( $additional_hook_data ) ) {
168+
return;
169+
}
170+
171+
foreach ( $additional_hook_data as $data ) {
172+
if ( ! isset( $data['hook'], $data['function'], $data['priority'] ) ) {
173+
continue;
174+
}
175+
remove_action( $data['hook'], $data['function'], $data['priority'] );
176+
}
177+
}
178+
179+
/**
180+
* Get the buffer content of the hooks to append/prepend to render content.
181+
*
182+
* @param array $hooks The hooks to be rendered.
183+
* @param string $position The position of the hooks.
184+
*
185+
* @return string
186+
*/
187+
protected function get_hooks_buffer( $hooks, $position ) {
188+
ob_start();
189+
foreach ( $hooks as $hook => $data ) {
190+
if ( $data['position'] === $position ) {
191+
/**
192+
* Action to render the content of a hook.
193+
*
194+
* @since 9.5.0
195+
*/
196+
do_action( $hook );
197+
}
198+
}
199+
return ob_get_clean();
200+
}
201+
}

0 commit comments

Comments
 (0)