From f02eebd805da718e105362955299f8e5744b10c7 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 4 Oct 2023 14:37:35 +0200 Subject: [PATCH] Patterns: Insert hooked blocks upon registration --- .../class-wp-block-patterns-registry.php | 24 +++++++------------ 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/src/wp-includes/class-wp-block-patterns-registry.php b/src/wp-includes/class-wp-block-patterns-registry.php index 56bcd8fd7785c..454a7c4a64249 100644 --- a/src/wp-includes/class-wp-block-patterns-registry.php +++ b/src/wp-includes/class-wp-block-patterns-registry.php @@ -115,6 +115,12 @@ public function register( $pattern_name, $pattern_properties ) { array( 'name' => $pattern_name ) ); + // Set `theme` attribute in Template Part blocks, and insert hooked blocks. + $blocks = parse_blocks( $pattern['content'] ); + $before_block_visitor = make_before_block_visitor( $pattern ); + $after_block_visitor = make_after_block_visitor( $pattern ); + $pattern['content'] = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor ); + $this->registered_patterns[ $pattern_name ] = $pattern; // If the pattern is registered inside an action other than `init`, store it @@ -165,13 +171,7 @@ public function get_registered( $pattern_name ) { return null; } - $pattern = $this->registered_patterns[ $pattern_name ]; - $blocks = parse_blocks( $pattern['content'] ); - $before_block_visitor = make_before_block_visitor( $pattern ); - $after_block_visitor = make_after_block_visitor( $pattern ); - $pattern['content'] = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor ); - - return $pattern; + return $this->registered_patterns[ $pattern_name ]; } /** @@ -184,19 +184,11 @@ public function get_registered( $pattern_name ) { * and per style. */ public function get_all_registered( $outside_init_only = false ) { - $patterns = array_values( + return array_values( $outside_init_only ? $this->registered_patterns_outside_init : $this->registered_patterns ); - - foreach ( $patterns as $index => $pattern ) { - $blocks = parse_blocks( $pattern['content'] ); - $before_block_visitor = make_before_block_visitor( $pattern ); - $after_block_visitor = make_after_block_visitor( $pattern ); - $patterns[ $index ]['content'] = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor ); - } - return $patterns; } /**