Automattic\WooCommerce\Blocks\Templates
ArchiveProductTemplatesCompatibility::inject_hooks
Inject hooks to rendered content of corresponding blocks.
Method of the class: ArchiveProductTemplatesCompatibility{}
No Hooks.
Returns
String.
Usage
$ArchiveProductTemplatesCompatibility = new ArchiveProductTemplatesCompatibility(); $ArchiveProductTemplatesCompatibility->inject_hooks( $block_content, $block );
- $block_content(mixed) (required)
- The rendered block content.
- $block(mixed) (required)
- The parsed block data.
ArchiveProductTemplatesCompatibility::inject_hooks() ArchiveProductTemplatesCompatibility::inject hooks code WC 10.3.5
public function inject_hooks( $block_content, $block ) {
if ( ! $this->is_archive_template() ) {
return $block_content;
}
/**
* If the block is not inherited, we don't need to inject hooks.
*/
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 );
}
);
// We want to inject hooks to the core/post-template or product 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 ) ) {
$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
) {
/**
* `core/query-no-result` is a special case because it can return two
* different content depending on the context. We need to check if the
* block content is empty to determine if we need to inject hooks.
*/
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' )
);
}