Automattic\WooCommerce\Blocks\Templates
SingleProductTemplateCompatibility::inject_hook_to_first_and_last_blocks()
Inject custom hooks to the first and last blocks. Since that there is a custom logic for the first and last block, we have to inject the hooks manually. The first block supports the following hooks: woocommerce_before_single_product woocommerce_before_single_product_summary
The last block supports the following hooks: woocommerce_after_single_product
Method of the class: SingleProductTemplateCompatibility{}
No Hooks.
Return
String
.
Usage
// private - for code of main (parent) class only $result = $this->inject_hook_to_first_and_last_blocks( $block_content, $block, $block_hooks );
- $block_content(mixed) (required)
- The rendered block content.
- $block(mixed) (required)
- The parsed block data.
- $block_hooks(array) (required)
- The hooks that should be injected to the block.
SingleProductTemplateCompatibility::inject_hook_to_first_and_last_blocks() SingleProductTemplateCompatibility::inject hook to first and last blocks code WC 9.4.2
private function inject_hook_to_first_and_last_blocks( $block_content, $block, $block_hooks ) { $first_block_hook = array( 'before' => array( 'woocommerce_before_main_content' => $this->hook_data['woocommerce_before_main_content'], 'woocommerce_before_single_product' => $this->hook_data['woocommerce_before_single_product'], 'woocommerce_before_single_product_summary' => $this->hook_data['woocommerce_before_single_product_summary'], ), 'after' => array(), ); $last_block_hook = array( 'before' => array(), 'after' => array( 'woocommerce_after_single_product' => $this->hook_data['woocommerce_after_single_product'], 'woocommerce_after_main_content' => $this->hook_data['woocommerce_after_main_content'], 'woocommerce_sidebar' => $this->hook_data['woocommerce_sidebar'], ), ); if ( isset( $block['attrs'][ self::IS_FIRST_BLOCK ] ) && isset( $block['attrs'][ self::IS_LAST_BLOCK ] ) ) { return sprintf( '%1$s%2$s', $this->inject_hooks_after_the_wrapper( $block_content, array_merge( $first_block_hook['before'], $block_hooks, $last_block_hook['before'] ) ), $this->get_hooks_buffer( array_merge( $first_block_hook['after'], $block_hooks, $last_block_hook['after'] ), 'after' ) ); } if ( isset( $block['attrs'][ self::IS_FIRST_BLOCK ] ) ) { return sprintf( '%1$s%2$s', $this->inject_hooks_after_the_wrapper( $block_content, array_merge( $first_block_hook['before'], $block_hooks ) ), $this->get_hooks_buffer( array_merge( $first_block_hook['after'], $block_hooks ), 'after' ) ); } if ( isset( $block['attrs'][ self::IS_LAST_BLOCK ] ) ) { return sprintf( '%1$s%2$s%3$s', $this->get_hooks_buffer( array_merge( $last_block_hook['before'], $block_hooks ), 'before' ), $block_content, $this->get_hooks_buffer( array_merge( $block_hooks, $last_block_hook['after'] ), 'after' ) ); } }