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

Disable compatibility layer when the WooCommerce Product Grid Block block and WooCommerce Single Product Block are used #8538

Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions bin/hook-docs/data/actions.json
Original file line number Diff line number Diff line change
Expand Up @@ -307,15 +307,15 @@
},
{
"name": "woocommerce_blocks_cart_enqueue_data",
"file": "BlockTypes/MiniCart.php",
"file": "BlockTypes/Cart.php",
"type": "action",
"doc": {
"description": "Fires after cart block data is registered.",
"long_description": "",
"tags": [
{
"name": "since",
"content": "5.8.0"
"content": "2.6.0"
}
],
"long_description_html": ""
Expand All @@ -324,15 +324,15 @@
},
{
"name": "woocommerce_blocks_cart_enqueue_data",
"file": "BlockTypes/Cart.php",
"file": "BlockTypes/MiniCart.php",
"type": "action",
"doc": {
"description": "Fires after cart block data is registered.",
"long_description": "",
"tags": [
{
"name": "since",
"content": "2.6.0"
"content": "5.8.0"
}
],
"long_description_html": ""
Expand Down Expand Up @@ -1056,9 +1056,14 @@
"file": "Templates/AbstractTemplateCompatibility.php",
"type": "action",
"doc": {
"description": "",
"description": "Action to render the content of a hook.",
"long_description": "",
"tags": [],
"tags": [
{
"name": "since",
"content": "9.5.0"
}
],
"long_description_html": ""
},
"args": 0
Expand Down
21 changes: 21 additions & 0 deletions bin/hook-docs/data/filters.json
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,27 @@
},
"args": 1
},
{
"name": "woocommerce_disable_compatibility_layer",
"file": "Templates/AbstractTemplateCompatibility.php",
"type": "filter",
"doc": {
"description": "Filters to disable the compatibility layer for the blockified templates.",
"long_description": "This hooks allows to disable the compatibility layer for the blockified.",
"tags": [
{
"name": "param",
"content": "",
"types": [
"\\Automattic\\WooCommerce\\Blocks\\Templates\\boolean."
],
"variable": ""
}
],
"long_description_html": "<p>This hooks allows to disable the compatibility layer for the blockified.</p>"
},
"args": 1
},
{
"name": "woocommerce_ga_disable_tracking",
"file": "Domain/Services/GoogleAnalytics.php",
Expand Down
4 changes: 2 additions & 2 deletions docs/third-party-developers/extensibility/hooks/actions.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,8 @@ do_action( 'woocommerce_blocks_cart_enqueue_data' )
### Source


- [BlockTypes/MiniCart.php](../../../../src/BlockTypes/MiniCart.php)
- [BlockTypes/Cart.php](../../../../src/BlockTypes/Cart.php)
- [BlockTypes/MiniCart.php](../../../../src/BlockTypes/MiniCart.php)

---

Expand Down Expand Up @@ -874,7 +874,7 @@ do_action( 'woocommerce_{$product->get_type()}_add_to_cart' )
## {$hook}



Action to render the content of a hook.

```php
do_action( '{$hook}' )
Expand Down
27 changes: 27 additions & 0 deletions docs/third-party-developers/extensibility/hooks/filters.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
- [woocommerce_blocks_product_grid_item_html](#woocommerce_blocks_product_grid_item_html)
- [woocommerce_blocks_register_script_dependencies](#woocommerce_blocks_register_script_dependencies)
- [woocommerce_cart_contents_changed](#woocommerce_cart_contents_changed)
- [woocommerce_disable_compatibility_layer](#woocommerce_disable_compatibility_layer)
- [woocommerce_ga_disable_tracking](#woocommerce_ga_disable_tracking)
- [woocommerce_get_item_data](#woocommerce_get_item_data)
- [woocommerce_loop_add_to_cart_args](#woocommerce_loop_add_to_cart_args)
Expand Down Expand Up @@ -578,6 +579,32 @@ apply_filters( 'woocommerce_cart_contents_changed', array $cart_contents )

---

## woocommerce_disable_compatibility_layer


Filters to disable the compatibility layer for the blockified templates.

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

### Description

<p>This hooks allows to disable the compatibility layer for the blockified.</p>

### Parameters

| Argument | Type | Description |
| -------- | ---- | ----------- |
| 1 | \Automattic\WooCommerce\Blocks\Templates\boolean. | |

### Source


- [Templates/AbstractTemplateCompatibility.php](../../../../src/Templates/AbstractTemplateCompatibility.php)

---

## woocommerce_ga_disable_tracking


Expand Down
18 changes: 15 additions & 3 deletions src/BlockTemplatesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use Automattic\WooCommerce\Blocks\Domain\Package;
use Automattic\WooCommerce\Blocks\Templates\ProductAttributeTemplate;
use Automattic\WooCommerce\Blocks\Templates\ProductSearchResultsTemplate;
use Automattic\WooCommerce\Blocks\Templates\SingleProductTemplateCompatibility;
use Automattic\WooCommerce\Blocks\Utils\BlockTemplateUtils;

Expand Down Expand Up @@ -325,11 +326,22 @@ function( $template ) {
$template->description = BlockTemplateUtils::get_block_template_description( $template->slug );
}

if ( in_array( $template->slug, array( 'archive-product', ProductSearchResultsTemplate::SLUG, ProductAttributeTemplate::SLUG, 'taxonomy-product_cat', 'taxonomy-product_tag' ) ) ) {

if ( ! is_admin() && BlockTemplateUtils::template_has_legacy_template_block( $template ) ) {
add_filter( 'woocommerce_disable_compatibility_layer', '__return_true' );
return $template;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: this if clause is the same here and in line 339-342. I wonder if it would make sense to move it before their parent if clauses, in line 328, so we avoid repeating it.

return $template;
}

if ( 'single-product' === $template->slug ) {
if ( ! is_admin() ) {
$new_content = SingleProductTemplateCompatibility::add_compatibility_layer( $template->content );
$template->content = $new_content;
if ( ! is_admin() && BlockTemplateUtils::template_has_legacy_template_block( $template ) ) {
add_filter( 'woocommerce_disable_compatibility_layer', '__return_true' );
return $template;
}
$new_content = SingleProductTemplateCompatibility::add_compatibility_layer( $template->content );
$template->content = $new_content;
return $template;
}

Expand Down
24 changes: 23 additions & 1 deletion src/Templates/AbstractTemplateCompatibility.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,29 @@ public function init() {
$this->set_hook_data();

add_filter( 'render_block_data', array( $this, 'update_render_block_data' ), 10, 3 );
add_filter( 'render_block', array( $this, 'inject_hooks' ), 10, 2 );
add_filter(
'render_block',
function ( $block_content, $block ) {
/**
* Filters to disable the compatibility layer for the blockified templates.
*
* This hooks allows to disable the compatibility layer for the blockified.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* This hooks allows to disable the compatibility layer for the blockified.
* This hook allows to disable the compatibility layer for the blockified templates.

*
* @since TBD
* @param boolean.
*/
$is_disabled_compatility_layer = apply_filters( 'woocommerce_disable_compatibility_layer', false );

if ( $is_disabled_compatility_layer ) {
return $block_content;
}

$this->inject_hooks( $block_content, $block );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this value be returned as well?

Suggested change
$this->inject_hooks( $block_content, $block );
return $this->inject_hooks( $block_content, $block );

},
10,
2
);

}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/Utils/BlockTemplateUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -696,4 +696,14 @@ public static function should_use_blockified_product_grid_templates() {
public static function template_has_title( $template ) {
return ! empty( $template->title ) && $template->title !== $template->slug;
}

/**
* Returns whether the passed `$template` has the legacy template block.
*
* @param object $template The template object.
* @return boolean
*/
public static function template_has_legacy_template_block( $template ) {
return has_block( 'woocommerce/legacy-template', $template->content );
}
}