Block Hooks: `apply_block_hooks_to_content` in Patterns, Templates.

In the Patterns registry, use `apply_block_hooks_to_content` (introduced in [58291])  instead of the `WP_Block_Patterns_Registry` class's private `get_content` method. (The latter is removed as part of this changeset.)

In a similar vein, use `apply_block_hooks_to_content` in the `_build_block_template_result_from_file` and `_build_block_template_result_from_post` functions, respectively.

For that to work, `apply_block_hooks_to_content` is amended to inject the `theme` attribute into Template Part blocks, even if no hooked blocks are present.

This kind of centralization is required as a preparation for #61902.

Props bernhard-reiter, jonsurrell.
See #61902.
Built from https://develop.svn.wordpress.org/trunk@59101


git-svn-id: http://core.svn.wordpress.org/trunk@58497 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Bernhard Reiter 2024-09-27 09:20:18 +00:00
parent c73731e74e
commit 020fde0d1d
4 changed files with 54 additions and 76 deletions

View File

@ -615,17 +615,7 @@ function _build_block_template_result_from_file( $template_file, $template_type
$template->area = $template_file['area']; $template->area = $template_file['area'];
} }
$hooked_blocks = get_hooked_blocks(); if ( 'wp_template_part' === $template->type ) {
$has_hooked_blocks = ! empty( $hooked_blocks ) || has_filter( 'hooked_block_types' );
$before_block_visitor = '_inject_theme_attribute_in_template_part_block';
$after_block_visitor = null;
if ( $has_hooked_blocks ) {
$before_block_visitor = make_before_block_visitor( $hooked_blocks, $template, 'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata' );
$after_block_visitor = make_after_block_visitor( $hooked_blocks, $template, 'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata' );
}
if ( 'wp_template_part' === $template->type && $has_hooked_blocks ) {
/* /*
* In order for hooked blocks to be inserted at positions first_child and last_child in a template part, * In order for hooked blocks to be inserted at positions first_child and last_child in a template part,
* we need to wrap its content a mock template part block and traverse it. * we need to wrap its content a mock template part block and traverse it.
@ -635,13 +625,17 @@ function _build_block_template_result_from_file( $template_file, $template_type
array(), array(),
$template->content $template->content
); );
$content = traverse_and_serialize_blocks( parse_blocks( $content ), $before_block_visitor, $after_block_visitor ); $content = apply_block_hooks_to_content(
$content,
$template,
'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata'
);
$template->content = remove_serialized_parent_block( $content ); $template->content = remove_serialized_parent_block( $content );
} else { } else {
$template->content = traverse_and_serialize_blocks( $template->content = apply_block_hooks_to_content(
parse_blocks( $template->content ), $template->content,
$before_block_visitor, $template,
$after_block_visitor 'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata'
); );
} }
@ -1036,10 +1030,6 @@ function _build_block_template_result_from_post( $post ) {
} }
} }
$hooked_blocks = get_hooked_blocks();
if ( ! empty( $hooked_blocks ) || has_filter( 'hooked_block_types' ) ) {
$before_block_visitor = make_before_block_visitor( $hooked_blocks, $template, 'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata' );
$after_block_visitor = make_after_block_visitor( $hooked_blocks, $template, 'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata' );
if ( 'wp_template_part' === $template->type ) { if ( 'wp_template_part' === $template->type ) {
$existing_ignored_hooked_blocks = get_post_meta( $post->ID, '_wp_ignored_hooked_blocks', true ); $existing_ignored_hooked_blocks = get_post_meta( $post->ID, '_wp_ignored_hooked_blocks', true );
$attributes = ! empty( $existing_ignored_hooked_blocks ) ? array( 'metadata' => array( 'ignoredHookedBlocks' => json_decode( $existing_ignored_hooked_blocks, true ) ) ) : array(); $attributes = ! empty( $existing_ignored_hooked_blocks ) ? array( 'metadata' => array( 'ignoredHookedBlocks' => json_decode( $existing_ignored_hooked_blocks, true ) ) ) : array();
@ -1053,16 +1043,19 @@ function _build_block_template_result_from_post( $post ) {
$attributes, $attributes,
$template->content $template->content
); );
$content = traverse_and_serialize_blocks( parse_blocks( $content ), $before_block_visitor, $after_block_visitor ); $content = apply_block_hooks_to_content(
$content,
$template,
'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata'
);
$template->content = remove_serialized_parent_block( $content ); $template->content = remove_serialized_parent_block( $content );
} else { } else {
$template->content = traverse_and_serialize_blocks( $template->content = apply_block_hooks_to_content(
parse_blocks( $template->content ), $template->content,
$before_block_visitor, $template,
$after_block_visitor 'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata'
); );
} }
}
return $template; return $template;
} }

View File

@ -1035,6 +1035,7 @@ function set_ignored_hooked_blocks_metadata( &$parsed_anchor_block, $relative_po
* Runs the hooked blocks algorithm on the given content. * Runs the hooked blocks algorithm on the given content.
* *
* @since 6.6.0 * @since 6.6.0
* @since 6.7.0 Injects the `theme` attribute into Template Part blocks, even if no hooked blocks are registered.
* @access private * @access private
* *
* @param string $content Serialized content. * @param string $content Serialized content.
@ -1047,15 +1048,16 @@ function set_ignored_hooked_blocks_metadata( &$parsed_anchor_block, $relative_po
*/ */
function apply_block_hooks_to_content( $content, $context, $callback = 'insert_hooked_blocks' ) { function apply_block_hooks_to_content( $content, $context, $callback = 'insert_hooked_blocks' ) {
$hooked_blocks = get_hooked_blocks(); $hooked_blocks = get_hooked_blocks();
if ( empty( $hooked_blocks ) && ! has_filter( 'hooked_block_types' ) ) {
return $content; $before_block_visitor = '_inject_theme_attribute_in_template_part_block';
$after_block_visitor = null;
if ( ! empty( $hooked_blocks ) || has_filter( 'hooked_block_types' ) ) {
$before_block_visitor = make_before_block_visitor( $hooked_blocks, $context, $callback );
$after_block_visitor = make_after_block_visitor( $hooked_blocks, $context, $callback );
} }
$blocks = parse_blocks( $content ); $blocks = parse_blocks( $content );
$before_block_visitor = make_before_block_visitor( $hooked_blocks, $context, $callback );
$after_block_visitor = make_after_block_visitor( $hooked_blocks, $context, $callback );
return traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor ); return traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor );
} }

View File

@ -158,31 +158,6 @@ final class WP_Block_Patterns_Registry {
return true; return true;
} }
/**
* Prepares the content of a block pattern. If hooked blocks are registered, they get injected into the pattern,
* when they met the defined criteria.
*
* @since 6.4.0
*
* @param array $pattern Registered pattern properties.
* @param array $hooked_blocks The list of hooked blocks.
* @return string The content of the block pattern.
*/
private function prepare_content( $pattern, $hooked_blocks ) {
$content = $pattern['content'];
$before_block_visitor = '_inject_theme_attribute_in_template_part_block';
$after_block_visitor = null;
if ( ! empty( $hooked_blocks ) || has_filter( 'hooked_block_types' ) ) {
$before_block_visitor = make_before_block_visitor( $hooked_blocks, $pattern, 'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata' );
$after_block_visitor = make_after_block_visitor( $hooked_blocks, $pattern, 'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata' );
}
$blocks = parse_blocks( $content );
$content = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor );
return $content;
}
/** /**
* Retrieves the content of a registered block pattern. * Retrieves the content of a registered block pattern.
* *
@ -221,8 +196,12 @@ final class WP_Block_Patterns_Registry {
} }
$pattern = $this->registered_patterns[ $pattern_name ]; $pattern = $this->registered_patterns[ $pattern_name ];
$pattern['content'] = $this->get_content( $pattern_name ); $content = $this->get_content( $pattern_name );
$pattern['content'] = $this->prepare_content( $pattern, get_hooked_blocks() ); $pattern['content'] = apply_block_hooks_to_content(
$content,
$pattern,
'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata'
);
return $pattern; return $pattern;
} }
@ -243,8 +222,12 @@ final class WP_Block_Patterns_Registry {
$hooked_blocks = get_hooked_blocks(); $hooked_blocks = get_hooked_blocks();
foreach ( $patterns as $index => $pattern ) { foreach ( $patterns as $index => $pattern ) {
$pattern['content'] = $this->get_content( $pattern['name'], $outside_init_only ); $content = $this->get_content( $pattern['name'], $outside_init_only );
$patterns[ $index ]['content'] = $this->prepare_content( $pattern, $hooked_blocks ); $patterns[ $index ]['content'] = apply_block_hooks_to_content(
$content,
$pattern,
'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata'
);
} }
return array_values( $patterns ); return array_values( $patterns );

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '6.7-alpha-59100'; $wp_version = '6.7-alpha-59101';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.