ArchiveProductTemplatesCompatibility.php
<?php
namespace Automattic\WooCommerce\Blocks\Templates;
class ArchiveProductTemplatesCompatibility extends AbstractTemplateCompatibility {
const LOOP_ITEM_ID = 'product-loop-item';
protected $hook_data;
public function update_render_block_data( $parsed_block, $source_block, $parent_block ) {
if ( ! $this->is_archive_template() ) {
return $parsed_block;
}
if ( $parent_block ) {
return $parsed_block;
}
$this->inner_blocks_walker( $parsed_block );
return $parsed_block;
}
public function inject_hooks( $block_content, $block ) {
if ( ! $this->is_archive_template() ) {
return $block_content;
}
if ( empty( $block['attrs']['isInherited'] ) ) {
return $block_content;
}
$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 );
}
);
if ( $this->is_post_or_product_template( $block_name ) && ! empty( $block_content ) ) {
$this->restore_default_hooks();
$content = sprintf(
'%1$s%2$s%3$s',
$this->get_hooks_buffer( $block_hooks, 'before' ),
$block_content,
$this->get_hooks_buffer( $block_hooks, 'after' )
);
$this->remove_default_hooks();
return $content;
}
$supported_blocks = array_merge(
array(),
...array_map(
function ( $hook ) {
return $hook['block_names'];
},
array_values( $this->hook_data )
)
);
if ( ! in_array( $block_name, $supported_blocks, true ) ) {
return $block_content;
}
if (
'core/query-no-results' === $block_name
) {
if ( empty( trim( $block_content ) ) ) {
return $block_content;
}
$this->restore_default_hooks();
$content = sprintf(
'%1$s%2$s%3$s',
$this->get_hooks_buffer( $block_hooks, 'before' ),
$block_content,
$this->get_hooks_buffer( $block_hooks, 'after' )
);
$this->remove_default_hooks();
return $content;
}
if ( empty( $block_content ) ) {
return $block_content;
}
return sprintf(
'%1$s%2$s%3$s',
$this->get_hooks_buffer( $block_hooks, 'before' ),
$block_content,
$this->get_hooks_buffer( $block_hooks, 'after' )
);
}
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(
'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(
'woocommerce_output_content_wrapper_end' => 10,
),
),
'woocommerce_before_shop_loop_item_title' => array(
'block_names' => array( '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(
'woocommerce_template_loop_product_title' => 10,
),
),
'woocommerce_after_shop_loop_item_title' => array(
'block_names' => array( '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(
'woocommerce_template_loop_product_link_open' => 10,
),
),
'woocommerce_after_shop_loop_item' => array(
'block_names' => array( 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' ),
'position' => 'before',
'hooked' => array(
'woocommerce_output_all_notices' => 10,
'woocommerce_result_count' => 20,
'woocommerce_catalog_ordering' => 30,
),
'permanently_removed_actions' => array(
'woocommerce_output_all_notices',
'woocommerce_result_count',
'woocommerce_catalog_ordering',
),
),
'woocommerce_after_shop_loop' => array(
'block_names' => array( 'core/post-template', 'woocommerce/product-template' ),
'position' => 'after',
'hooked' => array(
'woocommerce_pagination' => 10,
),
'permanently_removed_actions' => array(
'woocommerce_pagination',
),
),
'woocommerce_no_products_found' => array(
'block_names' => array( 'core/query-no-results' ),
'position' => 'before',
'hooked' => array(
'wc_no_products_found' => 10,
),
'permanently_removed_actions' => array(
'wc_no_products_found',
),
),
'woocommerce_archive_description' => array(
'block_names' => array( 'core/term-description' ),
'position' => 'before',
'hooked' => array(
'woocommerce_taxonomy_archive_description' => 10,
'woocommerce_product_archive_description' => 10,
),
),
);
}
private function is_archive_template() {
return is_shop() || is_product_taxonomy();
}
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->inject_attribute( $block );
$this->remove_default_hooks();
}
if ( ! empty( $block['innerBlocks'] ) ) {
array_walk( $block['innerBlocks'], array( $this, 'inner_blocks_walker' ) );
}
}
private function restore_default_hooks() {
foreach ( $this->hook_data as $hook => $data ) {
if ( ! isset( $data['hooked'] ) ) {
continue;
}
foreach ( $data['hooked'] as $callback => $priority ) {
if ( ! in_array( $callback, $data['permanently_removed_actions'] ?? array(), true ) ) {
add_action( $hook, $callback, $priority );
}
}
}
}
private function is_block_within_namespace( $block ) {
$attributes = $block['attrs'];
return isset( $attributes['__woocommerceNamespace'] ) && 'woocommerce/product-query/product-template' === $attributes['__woocommerceNamespace'];
}
private function is_block_inherited( $block ) {
$attributes = $block['attrs'];
$outcome = isset( $attributes['isInherited'] ) && 1 === $attributes['isInherited'];
return $outcome;
}
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 ) );
}
private function is_post_template( $block_name ) {
return 'core/post-template' === $block_name;
}
private function is_product_template( $block_name ) {
return 'woocommerce/product-template' === $block_name;
}
private function is_post_or_product_template( $block_name ) {
return $this->is_post_template( $block_name ) || $this->is_product_template( $block_name );
}
private function is_products_block_with_inherit_query( $block ) {
return 'core/query' === $block['blockName'] &&
isset( $block['attrs']['namespace'] ) &&
'woocommerce/product-query' === $block['attrs']['namespace'] &&
isset( $block['attrs']['query']['inherit'] ) &&
$block['attrs']['query']['inherit'];
}
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'];
}
private function inject_attribute( &$block ) {
$block['attrs']['isInherited'] = 1;
if ( ! empty( $block['innerBlocks'] ) ) {
array_walk( $block['innerBlocks'], array( $this, 'inject_attribute' ) );
}
}
}