Skip to content

Commit

Permalink
Templates: only resolve patterns for REST API endpoints (#61757)
Browse files Browse the repository at this point in the history
Co-authored-by: ellatrix <ellatrix@git.wordpress.org>
Co-authored-by: youknowriad <youknowriad@git.wordpress.org>
Co-authored-by: joemcgill <joemcgill@git.wordpress.org>
Co-authored-by: westonruter <westonruter@git.wordpress.org>
Co-authored-by: justintadlock <greenshady@git.wordpress.org>
Co-authored-by: talldan <talldanwp@git.wordpress.org>
Co-authored-by: ryelle <ryelle@git.wordpress.org>
  • Loading branch information
8 people committed May 29, 2024
1 parent 3ce3bbb commit d8351e4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 23 deletions.
6 changes: 6 additions & 0 deletions backport-changelog/6.6/6559.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
https://github.com/WordPress/wordpress-develop/pull/6559

* https://github.com/WordPress/gutenberg/pull/60349
* https://github.com/WordPress/gutenberg/pull/60464
* https://github.com/WordPress/gutenberg/pull/60491
* https://github.com/WordPress/gutenberg/pull/61757
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,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 );
}
}
23 changes: 0 additions & 23 deletions lib/compat/wordpress-6.6/resolve-patterns.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 );

0 comments on commit d8351e4

Please sign in to comment.