From 60d9f7fc283a8267143540ea9c8c799ed46b58af Mon Sep 17 00:00:00 2001 From: Ella Date: Fri, 17 May 2024 10:23:45 +0200 Subject: [PATCH] Patterns: only resolve patterns for REST API endpoints --- ...utenberg-rest-templates-controller-6-6.php | 10 ++++++++ lib/compat/wordpress-6.6/resolve-patterns.php | 23 ------------------- 2 files changed, 10 insertions(+), 23 deletions(-) diff --git a/lib/compat/wordpress-6.6/class-gutenberg-rest-templates-controller-6-6.php b/lib/compat/wordpress-6.6/class-gutenberg-rest-templates-controller-6-6.php index 656e38ffe933f..961d6c33534b9 100644 --- a/lib/compat/wordpress-6.6/class-gutenberg-rest-templates-controller-6-6.php +++ b/lib/compat/wordpress-6.6/class-gutenberg-rest-templates-controller-6-6.php @@ -117,4 +117,14 @@ public function get_template_fallback( $request ) { return rest_ensure_response( $response ); } + + /** + * See WP_REST_Templates_Controller::prepare_item_for_response + */ + public function prepare_item_for_response( $item, $request ) { + $blocks = parse_blocks( $item->content ); + $blocks = gutenberg_replace_pattern_blocks( $blocks ); + $item->content = serialize_blocks( $blocks ); + return parent::prepare_item_for_response( $item, $request ); + } } diff --git a/lib/compat/wordpress-6.6/resolve-patterns.php b/lib/compat/wordpress-6.6/resolve-patterns.php index dafda83266768..5105619c42613 100644 --- a/lib/compat/wordpress-6.6/resolve-patterns.php +++ b/lib/compat/wordpress-6.6/resolve-patterns.php @@ -61,25 +61,6 @@ function gutenberg_replace_pattern_blocks( $blocks, &$inner_content = null ) { return $blocks; } -function gutenberg_replace_pattern_blocks_get_block_templates( $templates ) { - foreach ( $templates as $template ) { - $blocks = parse_blocks( $template->content ); - $blocks = gutenberg_replace_pattern_blocks( $blocks ); - $template->content = serialize_blocks( $blocks ); - } - return $templates; -} - -function gutenberg_replace_pattern_blocks_get_block_template( $template ) { - if ( null === $template ) { - return $template; - } - $blocks = parse_blocks( $template->content ); - $blocks = gutenberg_replace_pattern_blocks( $blocks ); - $template->content = serialize_blocks( $blocks ); - return $template; -} - function gutenberg_replace_pattern_blocks_patterns_endpoint( $result, $server, $request ) { if ( $request->get_route() !== '/wp/v2/block-patterns/patterns' ) { return $result; @@ -98,10 +79,6 @@ function gutenberg_replace_pattern_blocks_patterns_endpoint( $result, $server, $ return $result; } -// For core merge, we should avoid the double parse and replace the patterns in templates here: -// https://github.com/WordPress/wordpress-develop/blob/02fb53498f1ce7e63d807b9bafc47a7dba19d169/src/wp-includes/block-template-utils.php#L558 -add_filter( 'get_block_templates', 'gutenberg_replace_pattern_blocks_get_block_templates' ); -add_filter( 'get_block_template', 'gutenberg_replace_pattern_blocks_get_block_template' ); // Similarly, for patterns, we can avoid the double parse here: // https://github.com/WordPress/wordpress-develop/blob/02fb53498f1ce7e63d807b9bafc47a7dba19d169/src/wp-includes/class-wp-block-patterns-registry.php#L175 add_filter( 'rest_post_dispatch', 'gutenberg_replace_pattern_blocks_patterns_endpoint', 10, 3 );