inject hooks on the first block after the main div wrapper (https://github.com/woocommerce/woocommerce-blocks/pull/8702)

This commit is contained in:
Luigi Teschio 2023-03-15 15:12:31 +01:00 committed by GitHub
parent 9f3f460372
commit c893a795b1
1 changed files with 31 additions and 10 deletions

View File

@ -88,16 +88,15 @@ class SingleProductTemplateCompatibility extends AbstractTemplateCompatibility {
if ( isset( $block['attrs'][ self::IS_FIRST_BLOCK ] ) && isset( $block['attrs'][ self::IS_LAST_BLOCK ] ) ) {
return sprintf(
'%1$s%2$s%3$s',
$this->get_hooks_buffer(
'%1$s%2$s',
$this->inject_hooks_after_the_wrapper(
$block_content,
array_merge(
$first_block_hook['before'],
$block_hooks,
$last_block_hook['before']
),
'before'
)
),
$block_content,
$this->get_hooks_buffer(
array_merge(
$first_block_hook['after'],
@ -111,15 +110,14 @@ class SingleProductTemplateCompatibility extends AbstractTemplateCompatibility {
if ( isset( $block['attrs'][ self::IS_FIRST_BLOCK ] ) ) {
return sprintf(
'%1$s%2$s%3$s',
$this->get_hooks_buffer(
'%1$s%2$s',
$this->inject_hooks_after_the_wrapper(
$block_content,
array_merge(
$first_block_hook['before'],
$block_hooks
),
'before'
)
),
$block_content,
$this->get_hooks_buffer(
array_merge(
$first_block_hook['after'],
@ -449,4 +447,27 @@ class SingleProductTemplateCompatibility extends AbstractTemplateCompatibility {
array()
);
}
/**
* Inject the hooks after the div wrapper.
*
* @param string $block_content Block Content.
* @param array $hooks Hooks to inject.
* @return array
*/
private function inject_hooks_after_the_wrapper( $block_content, $hooks ) {
$closing_tag_position = strpos( $block_content, '>' );
return substr_replace(
$block_content,
$this->get_hooks_buffer(
$hooks,
'before'
),
// Add 1 to the position to inject the content after the closing tag.
$closing_tag_position + 1,
0
);
}
}