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

Commit fc88c49

Browse files
gigituxAljullu
andauthored
Disable compatibility layer via hook (#8550)
* Disable compatibility layer via hook * Update src/Templates/BlockTemplatesCompatibility.php Co-authored-by: Albert Juhé Lluveras <[email protected]> * remove useless import --------- Co-authored-by: Albert Juhé Lluveras <[email protected]>
1 parent 4786e90 commit fc88c49

File tree

5 files changed

+166
-2
lines changed

5 files changed

+166
-2
lines changed

bin/hook-docs/data/filters.json

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,58 @@
734734
},
735735
"args": 1
736736
},
737+
{
738+
"name": "woocommerce_disable_compatibility_layer",
739+
"file": "Templates/BlockTemplatesCompatibility.php",
740+
"type": "filter",
741+
"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.",
744+
"tags": [
745+
{
746+
"name": "since",
747+
"content": "TBD",
748+
"description": "TBD"
749+
},
750+
{
751+
"name": "param",
752+
"content": "",
753+
"types": [
754+
"\\Automattic\\WooCommerce\\Blocks\\Templates\\boolean."
755+
],
756+
"variable": ""
757+
}
758+
],
759+
"long_description_html": "<p>This hooks allows to disable the compatibility layer for the blockified.</p>"
760+
},
761+
"args": 1
762+
},
763+
{
764+
"name": "woocommerce_disable_compatibility_layer",
765+
"file": "Templates/BlockTemplatesCompatibility.php",
766+
"type": "filter",
767+
"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.",
770+
"tags": [
771+
{
772+
"name": "since",
773+
"content": "TBD",
774+
"description": "TBD"
775+
},
776+
{
777+
"name": "param",
778+
"content": "",
779+
"types": [
780+
"\\Automattic\\WooCommerce\\Blocks\\Templates\\boolean."
781+
],
782+
"variable": ""
783+
}
784+
],
785+
"long_description_html": "<p>This hooks allows to disable the compatibility layer for the blockified.</p>"
786+
},
787+
"args": 1
788+
},
737789
{
738790
"name": "woocommerce_ga_disable_tracking",
739791
"file": "Domain/Services/GoogleAnalytics.php",

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
- [woocommerce_blocks_product_grid_item_html](#woocommerce_blocks_product_grid_item_html)
2626
- [woocommerce_blocks_register_script_dependencies](#woocommerce_blocks_register_script_dependencies)
2727
- [woocommerce_cart_contents_changed](#woocommerce_cart_contents_changed)
28+
- [woocommerce_disable_compatibility_layer](#woocommerce_disable_compatibility_layer)
2829
- [woocommerce_ga_disable_tracking](#woocommerce_ga_disable_tracking)
2930
- [woocommerce_get_item_data](#woocommerce_get_item_data)
3031
- [woocommerce_loop_add_to_cart_args](#woocommerce_loop_add_to_cart_args)
@@ -578,6 +579,33 @@ apply_filters( 'woocommerce_cart_contents_changed', array $cart_contents )
578579

579580
---
580581

582+
## woocommerce_disable_compatibility_layer
583+
584+
585+
Filters to disable the compatibility layer for the blockified templates.
586+
587+
```php
588+
apply_filters( 'woocommerce_disable_compatibility_layer', \Automattic\WooCommerce\Blocks\Templates\boolean. $argument0 )
589+
```
590+
591+
### Description
592+
593+
<p>This hooks allows to disable the compatibility layer for the blockified.</p>
594+
595+
### Parameters
596+
597+
| Argument | Type | Description |
598+
| -------- | ---- | ----------- |
599+
| 1 | \Automattic\WooCommerce\Blocks\Templates\boolean. | |
600+
601+
### Source
602+
603+
604+
- [Templates/BlockTemplatesCompatibility.php](../../../../src/Templates/BlockTemplatesCompatibility.php)
605+
- [Templates/BlockTemplatesCompatibility.php](../../../../src/Templates/BlockTemplatesCompatibility.php)
606+
607+
---
608+
581609
## woocommerce_ga_disable_tracking
582610

583611

src/BlockTemplatesController.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,24 +540,48 @@ public function render_block_template() {
540540
! BlockTemplateUtils::theme_has_template( 'single-product' ) &&
541541
$this->block_template_is_available( 'single-product' )
542542
) {
543+
$templates = get_block_templates( array( 'slug__in' => array( 'single-product' ) ) );
544+
545+
if ( isset( $templates[0] ) && BlockTemplateUtils::template_has_legacy_template_block( $templates[0] ) ) {
546+
add_filter( 'woocommerce_disable_compatibility_layer', '__return_true' );
547+
}
548+
543549
add_filter( 'woocommerce_has_block_template', '__return_true', 10, 0 );
544550
} elseif (
545551
( is_product_taxonomy() && is_tax( 'product_cat' ) ) &&
546552
! BlockTemplateUtils::theme_has_template( 'taxonomy-product_cat' ) &&
547553
$this->block_template_is_available( 'taxonomy-product_cat' )
548554
) {
555+
$templates = get_block_templates( array( 'slug__in' => array( 'taxonomy-product_cat' ) ) );
556+
557+
if ( isset( $templates[0] ) && BlockTemplateUtils::template_has_legacy_template_block( $templates[0] ) ) {
558+
add_filter( 'woocommerce_disable_compatibility_layer', '__return_true' );
559+
}
560+
549561
add_filter( 'woocommerce_has_block_template', '__return_true', 10, 0 );
550562
} elseif (
551563
( is_product_taxonomy() && is_tax( 'product_tag' ) ) &&
552564
! BlockTemplateUtils::theme_has_template( 'taxonomy-product_tag' ) &&
553565
$this->block_template_is_available( 'taxonomy-product_tag' )
554566
) {
567+
$templates = get_block_templates( array( 'slug__in' => array( 'taxonomy-product_tag' ) ) );
568+
569+
if ( isset( $templates[0] ) && BlockTemplateUtils::template_has_legacy_template_block( $templates[0] ) ) {
570+
add_filter( 'woocommerce_disable_compatibility_layer', '__return_true' );
571+
}
572+
555573
add_filter( 'woocommerce_has_block_template', '__return_true', 10, 0 );
556574
} elseif (
557575
( is_post_type_archive( 'product' ) || is_page( wc_get_page_id( 'shop' ) ) ) &&
558576
! BlockTemplateUtils::theme_has_template( 'archive-product' ) &&
559577
$this->block_template_is_available( 'archive-product' )
560578
) {
579+
$templates = get_block_templates( array( 'slug__in' => array( 'archive-product' ) ) );
580+
581+
if ( isset( $templates[0] ) && BlockTemplateUtils::template_has_legacy_template_block( $templates[0] ) ) {
582+
add_filter( 'woocommerce_disable_compatibility_layer', '__return_true' );
583+
}
584+
561585
add_filter( 'woocommerce_has_block_template', '__return_true', 10, 0 );
562586
} else {
563587
$queried_object = get_queried_object();
@@ -569,6 +593,12 @@ public function render_block_template() {
569593
! BlockTemplateUtils::theme_has_template( ProductAttributeTemplate::SLUG ) &&
570594
$this->block_template_is_available( ProductAttributeTemplate::SLUG )
571595
) {
596+
$templates = get_block_templates( array( 'slug__in' => array( ProductAttributeTemplate::SLUG ) ) );
597+
598+
if ( isset( $templates[0] ) && BlockTemplateUtils::template_has_legacy_template_block( $templates[0] ) ) {
599+
add_filter( 'woocommerce_disable_compatibility_layer', '__return_true' );
600+
}
601+
572602
add_filter( 'woocommerce_has_block_template', '__return_true', 10, 0 );
573603
}
574604
}

src/Templates/BlockTemplatesCompatibility.php

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,52 @@ protected function init() {
3939
return;
4040
}
4141

42-
add_filter( 'render_block_data', array( $this, 'update_render_block_data' ), 10, 3 );
43-
add_filter( 'render_block', array( $this, 'inject_hooks' ), 10, 2 );
42+
add_filter(
43+
'render_block_data',
44+
function( $parsed_block, $source_block, $parent_block ) {
45+
/**
46+
* Filter to disable the compatibility layer for the blockified templates.
47+
*
48+
* This hook allows to disable the compatibility layer for the blockified templates.
49+
*
50+
* @since TBD
51+
* @param boolean.
52+
*/
53+
$is_disabled_compatility_layer = apply_filters( 'woocommerce_disable_compatibility_layer', false );
54+
55+
if ( $is_disabled_compatility_layer ) {
56+
return $parsed_block;
57+
}
58+
59+
return $this->update_render_block_data( $parsed_block, $source_block, $parent_block );
60+
61+
},
62+
10,
63+
3
64+
);
65+
66+
add_filter(
67+
'render_block',
68+
function ( $block_content, $block ) {
69+
/**
70+
* Filters to disable the compatibility layer for the blockified templates.
71+
*
72+
* This hooks allows to disable the compatibility layer for the blockified.
73+
*
74+
* @since TBD
75+
* @param boolean.
76+
*/
77+
$is_disabled_compatility_layer = apply_filters( 'woocommerce_disable_compatibility_layer', false );
78+
79+
if ( $is_disabled_compatility_layer ) {
80+
return $block_content;
81+
}
82+
83+
return $this->inject_hooks( $block_content, $block );
84+
},
85+
10,
86+
2
87+
);
4488
}
4589

4690
/**

src/Utils/BlockTemplateUtils.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,4 +696,14 @@ public static function should_use_blockified_product_grid_templates() {
696696
public static function template_has_title( $template ) {
697697
return ! empty( $template->title ) && $template->title !== $template->slug;
698698
}
699+
700+
/**
701+
* Returns whether the passed `$template` has the legacy template block.
702+
*
703+
* @param object $template The template object.
704+
* @return boolean
705+
*/
706+
public static function template_has_legacy_template_block( $template ) {
707+
return has_block( 'woocommerce/legacy-template', $template->content );
708+
}
699709
}

0 commit comments

Comments
 (0)