tions' => 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, ), ), ); } /** * Check if current page is a product archive template. */ private function is_archive_template() { return is_shop() || is_product_taxonomy(); } /** * Loop through inner blocks recursively to find the Products blocks that * inherits query from template. * * @param array $block Parsed block data. */ 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' ) ); } } /** * Restore default hooks except the ones that are not supposed to be re-added. */ 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 ); } } } } /** * 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. * * @param array $block Parsed block data. */ 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']; } /** * 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. * * @param array $block Parsed block data. */ private function inject_attribute( &$block ) { $block['attrs']['isInherited'] = 1; if ( ! empty( $block['innerBlocks'] ) ) { array_walk( $block['innerBlocks'], array( $this, 'inject_attribute' ) ); } } }