From 6c43311037e39005ded6aa7ca736c81e67fc4150 Mon Sep 17 00:00:00 2001 From: Darren Ethier Date: Fri, 10 Nov 2023 14:23:18 -0500 Subject: [PATCH 1/2] Expose serialized template content on WP_Block_Template. When building the template, initialize WP_Block_Template with the serialized block content so callbacks registered to the `hooked_block_types` filter can access the content. --- src/wp-includes/block-template-utils.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index a4e54432d2aa2..73861ec0ac1fc 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -532,6 +532,7 @@ function _build_block_template_result_from_file( $template_file, $template_type $template->has_theme_file = true; $template->is_custom = true; $template->modified = null; + $template->content = $template_content; if ( 'wp_template' === $template_type && isset( $default_template_types[ $template_file['slug'] ] ) ) { $template->description = $default_template_types[ $template_file['slug'] ]['description']; From 9eebbbed451cd63ac52f3a04ad52dfd5df6a3b20 Mon Sep 17 00:00:00 2001 From: Darren Ethier Date: Mon, 13 Nov 2023 07:38:57 -0500 Subject: [PATCH 2/2] Remove unnecessary variable Consolidates initial read from template file into the `content` property in the `WP_Block_Template` instance removing the need for a separate `$template_content` variable. --- src/wp-includes/block-template-utils.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index 73861ec0ac1fc..a03765676f121 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -518,7 +518,6 @@ function _remove_theme_attribute_from_template_part_block( &$block ) { */ function _build_block_template_result_from_file( $template_file, $template_type ) { $default_template_types = get_default_block_template_types(); - $template_content = file_get_contents( $template_file['path'] ); $theme = get_stylesheet(); $template = new WP_Block_Template(); @@ -532,7 +531,7 @@ function _build_block_template_result_from_file( $template_file, $template_type $template->has_theme_file = true; $template->is_custom = true; $template->modified = null; - $template->content = $template_content; + $template->content = file_get_contents( $template_file['path'] ); if ( 'wp_template' === $template_type && isset( $default_template_types[ $template_file['slug'] ] ) ) { $template->description = $default_template_types[ $template_file['slug'] ]['description']; @@ -555,7 +554,7 @@ function _build_block_template_result_from_file( $template_file, $template_type $before_block_visitor = make_before_block_visitor( $hooked_blocks, $template ); $after_block_visitor = make_after_block_visitor( $hooked_blocks, $template ); } - $blocks = parse_blocks( $template_content ); + $blocks = parse_blocks( $template->content ); $template->content = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor ); return $template;