rebase code on oct-10-2023
This commit is contained in:
@@ -103,7 +103,7 @@ abstract class AbstractTemplateCompatibility {
|
||||
* The array format:
|
||||
* [
|
||||
* <hook-name> => [
|
||||
* block_names => [ <block-name>, ... ],
|
||||
* block_name => <block-name>,
|
||||
* position => before|after,
|
||||
* hooked => [
|
||||
* <function-name> => <priority>,
|
||||
@@ -113,7 +113,7 @@ abstract class AbstractTemplateCompatibility {
|
||||
* ]
|
||||
* Where:
|
||||
* - hook-name is the name of the hook that will be replaced.
|
||||
* - block-names is the array block names that hook will be attached to.
|
||||
* - block-name is the name of the block that will replace the hook.
|
||||
* - position is the position of the block relative to the hook.
|
||||
* - hooked is an array of functions hooked to the hook that will be
|
||||
* replaced. The key is the function name and the value is the
|
||||
|
||||
@@ -47,7 +47,7 @@ class ArchiveProductTemplatesCompatibility extends AbstractTemplateCompatibility
|
||||
return $parsed_block;
|
||||
}
|
||||
|
||||
$this->inner_blocks_walker( $parsed_block );
|
||||
array_walk( $parsed_block['innerBlocks'], array( $this, 'inner_blocks_walker' ) );
|
||||
|
||||
return $parsed_block;
|
||||
}
|
||||
@@ -72,20 +72,16 @@ class ArchiveProductTemplatesCompatibility extends AbstractTemplateCompatibility
|
||||
|
||||
$block_name = $block['blockName'];
|
||||
|
||||
if ( $this->is_null_post_template( $block ) ) {
|
||||
$block_name = self::LOOP_ITEM_ID;
|
||||
}
|
||||
|
||||
$block_hooks = array_filter(
|
||||
$this->hook_data,
|
||||
function( $hook ) use ( $block_name ) {
|
||||
return in_array( $block_name, $hook['block_names'], true );
|
||||
return $hook['block_name'] === $block_name;
|
||||
}
|
||||
);
|
||||
|
||||
// We want to inject hooks to the core/post-template or product template block only when the products exist:
|
||||
// We want to inject hooks to the core/post-template block only when the products exist:
|
||||
// https://github.com/woocommerce/woocommerce-blocks/issues/9463.
|
||||
if ( $this->is_post_or_product_template( $block_name ) && ! empty( $block_content ) ) {
|
||||
if ( 'core/post-template' === $block_name && ! empty( $block_content ) ) {
|
||||
$this->restore_default_hooks();
|
||||
$content = sprintf(
|
||||
'%1$s%2$s%3$s',
|
||||
@@ -97,14 +93,24 @@ class ArchiveProductTemplatesCompatibility extends AbstractTemplateCompatibility
|
||||
return $content;
|
||||
}
|
||||
|
||||
$supported_blocks = array_merge(
|
||||
[],
|
||||
...array_map(
|
||||
function( $hook ) {
|
||||
return $hook['block_names'];
|
||||
},
|
||||
array_values( $this->hook_data )
|
||||
)
|
||||
/**
|
||||
* The core/post-template has two different block names:
|
||||
* - core/post-template when the wrapper is rendered.
|
||||
* - core/null when the loop item is rendered.
|
||||
*/
|
||||
if (
|
||||
'core/null' === $block_name &&
|
||||
isset( $block['attrs']['__woocommerceNamespace'] ) &&
|
||||
'woocommerce/product-query/product-template' === $block['attrs']['__woocommerceNamespace']
|
||||
) {
|
||||
$block_name = self::LOOP_ITEM_ID;
|
||||
}
|
||||
|
||||
$supported_blocks = array_map(
|
||||
function( $hook ) {
|
||||
return $hook['block_name'];
|
||||
},
|
||||
array_values( $this->hook_data )
|
||||
);
|
||||
|
||||
if ( ! in_array( $block_name, $supported_blocks, true ) ) {
|
||||
@@ -138,10 +144,6 @@ class ArchiveProductTemplatesCompatibility extends AbstractTemplateCompatibility
|
||||
return $content;
|
||||
}
|
||||
|
||||
if ( empty( $block_content ) ) {
|
||||
return $block_content;
|
||||
}
|
||||
|
||||
return sprintf(
|
||||
'%1$s%2$s%3$s',
|
||||
$this->get_hooks_buffer( $block_hooks, 'before' ),
|
||||
@@ -180,60 +182,60 @@ class ArchiveProductTemplatesCompatibility extends AbstractTemplateCompatibility
|
||||
protected function set_hook_data() {
|
||||
$this->hook_data = array(
|
||||
'woocommerce_before_main_content' => array(
|
||||
'block_names' => array( 'core/query', 'woocommerce/product-collection' ),
|
||||
'position' => 'before',
|
||||
'hooked' => array(
|
||||
'block_name' => 'core/query',
|
||||
'position' => 'before',
|
||||
'hooked' => array(
|
||||
'woocommerce_output_content_wrapper' => 10,
|
||||
'woocommerce_breadcrumb' => 20,
|
||||
),
|
||||
),
|
||||
'woocommerce_after_main_content' => array(
|
||||
'block_names' => array( 'core/query', 'woocommerce/product-collection' ),
|
||||
'position' => 'after',
|
||||
'hooked' => array(
|
||||
'block_name' => 'core/query',
|
||||
'position' => 'after',
|
||||
'hooked' => array(
|
||||
'woocommerce_output_content_wrapper_end' => 10,
|
||||
),
|
||||
),
|
||||
'woocommerce_before_shop_loop_item_title' => array(
|
||||
'block_names' => array( 'core/post-title' ),
|
||||
'position' => 'before',
|
||||
'hooked' => array(
|
||||
'block_name' => 'core/post-title',
|
||||
'position' => 'before',
|
||||
'hooked' => array(
|
||||
'woocommerce_show_product_loop_sale_flash' => 10,
|
||||
'woocommerce_template_loop_product_thumbnail' => 10,
|
||||
),
|
||||
),
|
||||
'woocommerce_shop_loop_item_title' => array(
|
||||
'block_names' => array( 'core/post-title' ),
|
||||
'position' => 'after',
|
||||
'hooked' => array(
|
||||
'block_name' => 'core/post-title',
|
||||
'position' => 'after',
|
||||
'hooked' => array(
|
||||
'woocommerce_template_loop_product_title' => 10,
|
||||
),
|
||||
),
|
||||
'woocommerce_after_shop_loop_item_title' => array(
|
||||
'block_names' => array( 'core/post-title' ),
|
||||
'position' => 'after',
|
||||
'hooked' => array(
|
||||
'block_name' => 'core/post-title',
|
||||
'position' => 'after',
|
||||
'hooked' => array(
|
||||
'woocommerce_template_loop_rating' => 5,
|
||||
'woocommerce_template_loop_price' => 10,
|
||||
),
|
||||
),
|
||||
'woocommerce_before_shop_loop_item' => array(
|
||||
'block_names' => array( self::LOOP_ITEM_ID ),
|
||||
'position' => 'before',
|
||||
'hooked' => array(
|
||||
'block_name' => self::LOOP_ITEM_ID,
|
||||
'position' => 'before',
|
||||
'hooked' => array(
|
||||
'woocommerce_template_loop_product_link_open' => 10,
|
||||
),
|
||||
),
|
||||
'woocommerce_after_shop_loop_item' => array(
|
||||
'block_names' => array( self::LOOP_ITEM_ID ),
|
||||
'position' => 'after',
|
||||
'hooked' => array(
|
||||
'block_name' => self::LOOP_ITEM_ID,
|
||||
'position' => 'after',
|
||||
'hooked' => array(
|
||||
'woocommerce_template_loop_product_link_close' => 5,
|
||||
'woocommerce_template_loop_add_to_cart' => 10,
|
||||
),
|
||||
),
|
||||
'woocommerce_before_shop_loop' => array(
|
||||
'block_names' => array( 'core/post-template', 'woocommerce/product-template' ),
|
||||
'block_name' => 'core/post-template',
|
||||
'position' => 'before',
|
||||
'hooked' => array(
|
||||
'woocommerce_output_all_notices' => 10,
|
||||
@@ -247,7 +249,7 @@ class ArchiveProductTemplatesCompatibility extends AbstractTemplateCompatibility
|
||||
),
|
||||
),
|
||||
'woocommerce_after_shop_loop' => array(
|
||||
'block_names' => array( 'core/post-template', 'woocommerce/product-template' ),
|
||||
'block_name' => 'core/post-template',
|
||||
'position' => 'after',
|
||||
'hooked' => array(
|
||||
'woocommerce_pagination' => 10,
|
||||
@@ -257,7 +259,7 @@ class ArchiveProductTemplatesCompatibility extends AbstractTemplateCompatibility
|
||||
),
|
||||
),
|
||||
'woocommerce_no_products_found' => array(
|
||||
'block_names' => array( 'core/query-no-results' ),
|
||||
'block_name' => 'core/query-no-results',
|
||||
'position' => 'before',
|
||||
'hooked' => array(
|
||||
'wc_no_products_found' => 10,
|
||||
@@ -267,9 +269,9 @@ class ArchiveProductTemplatesCompatibility extends AbstractTemplateCompatibility
|
||||
),
|
||||
),
|
||||
'woocommerce_archive_description' => array(
|
||||
'block_names' => array( 'core/term-description' ),
|
||||
'position' => 'before',
|
||||
'hooked' => array(
|
||||
'block_name' => 'core/term-description',
|
||||
'position' => 'before',
|
||||
'hooked' => array(
|
||||
'woocommerce_taxonomy_archive_description' => 10,
|
||||
'woocommerce_product_archive_description' => 10,
|
||||
),
|
||||
@@ -292,7 +294,7 @@ class ArchiveProductTemplatesCompatibility extends AbstractTemplateCompatibility
|
||||
*/
|
||||
private function inner_blocks_walker( &$block ) {
|
||||
if (
|
||||
$this->is_products_block_with_inherit_query( $block ) || $this->is_product_collection_block_with_inherit_query( $block )
|
||||
$this->is_products_block_with_inherit_query( $block )
|
||||
) {
|
||||
$this->inject_attribute( $block );
|
||||
$this->remove_default_hooks();
|
||||
@@ -319,69 +321,6 @@ class ArchiveProductTemplatesCompatibility extends AbstractTemplateCompatibility
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if block is within the product-query namespace
|
||||
*
|
||||
* @param array $block Parsed block data.
|
||||
*/
|
||||
private function is_block_within_namespace( $block ) {
|
||||
$attributes = $block['attrs'];
|
||||
|
||||
return isset( $attributes['__woocommerceNamespace'] ) && 'woocommerce/product-query/product-template' === $attributes['__woocommerceNamespace'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if block has isInherited attribute asigned
|
||||
*
|
||||
* @param array $block Parsed block data.
|
||||
*/
|
||||
private function is_block_inherited( $block ) {
|
||||
$attributes = $block['attrs'];
|
||||
|
||||
$outcome = isset( $attributes['isInherited'] ) && 1 === $attributes['isInherited'];
|
||||
|
||||
return $outcome;
|
||||
}
|
||||
|
||||
/**
|
||||
* The core/post-template has two different block names:
|
||||
* - core/post-template when the wrapper is rendered.
|
||||
* - core/null when the loop item is rendered.
|
||||
*
|
||||
* @param array $block Parsed block data.
|
||||
*/
|
||||
private function is_null_post_template( $block ) {
|
||||
$block_name = $block['blockName'];
|
||||
|
||||
return 'core/null' === $block_name && ( $this->is_block_inherited( $block ) || $this->is_block_within_namespace( $block ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if block is a Post template
|
||||
*
|
||||
* @param string $block_name Block name.
|
||||
*/
|
||||
private function is_post_template( $block_name ) {
|
||||
return 'core/post-template' === $block_name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if block is a Product Template
|
||||
*
|
||||
* @param string $block_name Block name.
|
||||
*/
|
||||
private function is_product_template( $block_name ) {
|
||||
return 'woocommerce/product-template' === $block_name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if block is eaither a Post template or Product Template
|
||||
*
|
||||
* @param string $block_name Block name.
|
||||
*/
|
||||
private function is_post_or_product_template( $block_name ) {
|
||||
return $this->is_post_template( $block_name ) || $this->is_product_template( $block_name );
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the block is a Products block that inherits query from template.
|
||||
@@ -396,18 +335,6 @@ class ArchiveProductTemplatesCompatibility extends AbstractTemplateCompatibility
|
||||
$block['attrs']['query']['inherit'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the block is a Product Collection block that inherits query from template.
|
||||
*
|
||||
* @param array $block Parsed block data.
|
||||
*/
|
||||
private function is_product_collection_block_with_inherit_query( $block ) {
|
||||
return 'woocommerce/product-collection' === $block['blockName'] &&
|
||||
isset( $block['attrs']['query']['inherit'] ) &&
|
||||
$block['attrs']['query']['inherit'];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Recursively inject the custom attribute to all nested blocks.
|
||||
*
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
<?php
|
||||
namespace Automattic\WooCommerce\Blocks\Templates;
|
||||
|
||||
use Automattic\WooCommerce\Blocks\Utils\BlockTemplateMigrationUtils;
|
||||
|
||||
/**
|
||||
* CartTemplate class.
|
||||
*
|
||||
@@ -34,11 +32,6 @@ class CartTemplate extends AbstractPageTemplate {
|
||||
* @return boolean
|
||||
*/
|
||||
protected function is_active_template() {
|
||||
|
||||
if ( ! BlockTemplateMigrationUtils::has_migrated_page( 'cart' ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
global $post;
|
||||
$placeholder = $this->get_placeholder_page();
|
||||
return null !== $placeholder && $post instanceof \WP_Post && $placeholder->post_name === $post->post_name;
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
<?php
|
||||
namespace Automattic\WooCommerce\Blocks\Templates;
|
||||
|
||||
use Automattic\WooCommerce\Blocks\Utils\BlockTemplateMigrationUtils;
|
||||
|
||||
/**
|
||||
* CheckoutTemplate class.
|
||||
*
|
||||
@@ -43,11 +41,6 @@ class CheckoutTemplate extends AbstractPageTemplate {
|
||||
* @return boolean
|
||||
*/
|
||||
public function is_active_template() {
|
||||
|
||||
if ( ! BlockTemplateMigrationUtils::has_migrated_page( 'checkout' ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
global $post;
|
||||
$placeholder = $this->get_placeholder_page();
|
||||
return null !== $placeholder && $post instanceof \WP_Post && $placeholder->post_name === $post->post_name;
|
||||
|
||||
@@ -60,7 +60,7 @@ class ClassicTemplatesCompatibility {
|
||||
global $pagenow;
|
||||
|
||||
if ( is_shop() || is_product_taxonomy() || 'widgets.php' === $pagenow ) {
|
||||
$this->asset_data_registry->add( 'hasFilterableProducts', true, true );
|
||||
$this->asset_data_registry->add( 'has_filterable_products', true, true );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,7 +75,7 @@ class ClassicTemplatesCompatibility {
|
||||
*/
|
||||
public function set_php_template_data() {
|
||||
if ( is_shop() || is_product_taxonomy() ) {
|
||||
$this->asset_data_registry->add( 'isRenderingPhpTemplate', true, true );
|
||||
$this->asset_data_registry->add( 'is_rendering_php_template', true, true );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ class SingleProductTemplateCompatibility extends AbstractTemplateCompatibility {
|
||||
$block_hooks = array_filter(
|
||||
$this->hook_data,
|
||||
function( $hook ) use ( $block_name ) {
|
||||
return in_array( $block_name, $hook['block_names'], true );
|
||||
return $hook['block_name'] === $block_name;
|
||||
}
|
||||
);
|
||||
|
||||
@@ -170,46 +170,46 @@ class SingleProductTemplateCompatibility extends AbstractTemplateCompatibility {
|
||||
protected function set_hook_data() {
|
||||
$this->hook_data = array(
|
||||
'woocommerce_before_main_content' => array(
|
||||
'block_names' => array(),
|
||||
'position' => 'before',
|
||||
'hooked' => array(
|
||||
'block_name' => '',
|
||||
'position' => 'before',
|
||||
'hooked' => array(
|
||||
'woocommerce_output_content_wrapper' => 10,
|
||||
'woocommerce_breadcrumb' => 20,
|
||||
),
|
||||
),
|
||||
'woocommerce_after_main_content' => array(
|
||||
'block_names' => array(),
|
||||
'position' => 'after',
|
||||
'hooked' => array(
|
||||
'block_name' => '',
|
||||
'position' => 'after',
|
||||
'hooked' => array(
|
||||
'woocommerce_output_content_wrapper_end' => 10,
|
||||
),
|
||||
),
|
||||
'woocommerce_sidebar' => array(
|
||||
'block_names' => array(),
|
||||
'position' => 'after',
|
||||
'hooked' => array(
|
||||
'block_name' => '',
|
||||
'position' => 'after',
|
||||
'hooked' => array(
|
||||
'woocommerce_get_sidebar' => 10,
|
||||
),
|
||||
),
|
||||
'woocommerce_before_single_product' => array(
|
||||
'block_names' => array(),
|
||||
'position' => 'before',
|
||||
'hooked' => array(
|
||||
'block_name' => '',
|
||||
'position' => 'before',
|
||||
'hooked' => array(
|
||||
'woocommerce_output_all_notices' => 10,
|
||||
),
|
||||
),
|
||||
'woocommerce_before_single_product_summary' => array(
|
||||
'block_names' => array(),
|
||||
'position' => 'before',
|
||||
'hooked' => array(
|
||||
'block_name' => '',
|
||||
'position' => 'before',
|
||||
'hooked' => array(
|
||||
'woocommerce_show_product_sale_flash' => 10,
|
||||
'woocommerce_show_product_images' => 20,
|
||||
),
|
||||
),
|
||||
'woocommerce_single_product_summary' => array(
|
||||
'block_names' => array(),
|
||||
'position' => 'before',
|
||||
'hooked' => array(
|
||||
'block_name' => '',
|
||||
'position' => 'before',
|
||||
'hooked' => array(
|
||||
'woocommerce_template_single_title' => 5,
|
||||
'woocommerce_template_single_rating' => 10,
|
||||
'woocommerce_template_single_price' => 10,
|
||||
@@ -220,29 +220,29 @@ class SingleProductTemplateCompatibility extends AbstractTemplateCompatibility {
|
||||
),
|
||||
),
|
||||
'woocommerce_after_single_product' => array(
|
||||
'block_names' => array(),
|
||||
'position' => 'after',
|
||||
'hooked' => array(),
|
||||
'block_name' => '',
|
||||
'position' => 'after',
|
||||
'hooked' => array(),
|
||||
),
|
||||
'woocommerce_product_meta_start' => array(
|
||||
'block_names' => array( 'woocommerce/product-meta' ),
|
||||
'position' => 'before',
|
||||
'hooked' => array(),
|
||||
'block_name' => 'woocommerce/product-meta',
|
||||
'position' => 'before',
|
||||
'hooked' => array(),
|
||||
),
|
||||
'woocommerce_product_meta_end' => array(
|
||||
'block_names' => array( 'woocommerce/product-meta' ),
|
||||
'position' => 'after',
|
||||
'hooked' => array(),
|
||||
'block_name' => 'woocommerce/product-meta',
|
||||
'position' => 'after',
|
||||
'hooked' => array(),
|
||||
),
|
||||
'woocommerce_share' => array(
|
||||
'block_names' => array( 'woocommerce/product-details' ),
|
||||
'position' => 'before',
|
||||
'hooked' => array(),
|
||||
'block_name' => 'woocommerce/product-details',
|
||||
'position' => 'before',
|
||||
'hooked' => array(),
|
||||
),
|
||||
'woocommerce_after_single_product_summary' => array(
|
||||
'block_names' => array( 'woocommerce/product-details' ),
|
||||
'position' => 'after',
|
||||
'hooked' => array(
|
||||
'block_name' => 'woocommerce/product-details',
|
||||
'position' => 'after',
|
||||
'hooked' => array(
|
||||
'woocommerce_output_product_data_tabs' => 10,
|
||||
// We want to display the upsell products after the last block that belongs to the Single Product.
|
||||
// 'woocommerce_upsell_display' => 15.
|
||||
@@ -269,6 +269,7 @@ class SingleProductTemplateCompatibility extends AbstractTemplateCompatibility {
|
||||
$wrapped_blocks = self::wrap_single_product_template( $template_content );
|
||||
$template = self::inject_custom_attributes_to_first_and_last_block_single_product_template( $wrapped_blocks );
|
||||
return self::serialize_blocks( $template );
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user