From f5999683883d97a62f1248fe06a69d7a7adc8d98 Mon Sep 17 00:00:00 2001 From: Joe McGill Date: Wed, 21 Feb 2024 18:50:07 +0000 Subject: [PATCH] Editor: Load pattern content only when used. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously, the content for all registered patterns would get loaded on each request when the patterns are registered. Instead, this stores the path the pattern file during registration and reads the content the first time the pattern is used, which is a performance optimization. Props thekt12, spacedmonkey, gziolo, aristath, joemcgill. Fixes #59532. Built from https://develop.svn.wordpress.org/trunk@57683 git-svn-id: http://core.svn.wordpress.org/trunk@57184 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/block-patterns.php | 8 +-- .../class-wp-block-patterns-registry.php | 52 ++++++++++++++----- wp-includes/version.php | 2 +- 3 files changed, 42 insertions(+), 20 deletions(-) diff --git a/wp-includes/block-patterns.php b/wp-includes/block-patterns.php index e5c770e1d4..cb58d9842c 100644 --- a/wp-includes/block-patterns.php +++ b/wp-includes/block-patterns.php @@ -391,13 +391,7 @@ function _register_theme_block_patterns() { continue; } - // The actual pattern content is the output of the file. - ob_start(); - include $file_path; - $pattern_data['content'] = ob_get_clean(); - if ( ! $pattern_data['content'] ) { - continue; - } + $pattern_data['file_path'] = $file_path; // Translate the pattern metadata. // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText,WordPress.WP.I18n.NonSingularStringLiteralDomain,WordPress.WP.I18n.LowLevelTranslationFunction diff --git a/wp-includes/class-wp-block-patterns-registry.php b/wp-includes/class-wp-block-patterns-registry.php index a11bac06be..1402819c84 100644 --- a/wp-includes/class-wp-block-patterns-registry.php +++ b/wp-includes/class-wp-block-patterns-registry.php @@ -101,13 +101,15 @@ final class WP_Block_Patterns_Registry { return false; } - if ( ! isset( $pattern_properties['content'] ) || ! is_string( $pattern_properties['content'] ) ) { - _doing_it_wrong( - __METHOD__, - __( 'Pattern content must be a string.' ), - '5.5.0' - ); - return false; + if ( ! isset( $pattern_properties['file_path'] ) ) { + if ( ! isset( $pattern_properties['content'] ) || ! is_string( $pattern_properties['content'] ) ) { + _doing_it_wrong( + __METHOD__, + __( 'Pattern content must be a string.' ), + '5.5.0' + ); + return false; + } } $pattern = array_merge( @@ -177,6 +179,30 @@ final class WP_Block_Patterns_Registry { return $content; } + /** + * Retrieves the content of a registered block pattern. + * + * @since 6.5.0 + * + * @param string $pattern_name Block pattern name including namespace. + * @param bool $outside_init_only Optional. Return only patterns registered outside the `init` action. Default false. + * @return string The content of the block pattern. + */ + private function get_content( $pattern_name, $outside_init_only = false ) { + if ( $outside_init_only ) { + $patterns = &$this->registered_patterns_outside_init; + } else { + $patterns = &$this->registered_patterns; + } + if ( ! isset( $patterns[ $pattern_name ]['content'] ) && isset( $patterns[ $pattern_name ]['file_path'] ) ) { + ob_start(); + include $patterns[ $pattern_name ]['file_path']; + $patterns[ $pattern_name ]['content'] = ob_get_clean(); + unset( $patterns[ $pattern_name ]['file_path'] ); + } + return $patterns[ $pattern_name ]['content']; + } + /** * Retrieves an array containing the properties of a registered block pattern. * @@ -191,6 +217,7 @@ final class WP_Block_Patterns_Registry { } $pattern = $this->registered_patterns[ $pattern_name ]; + $pattern['content'] = $this->get_content( $pattern_name ); $pattern['content'] = $this->prepare_content( $pattern, get_hooked_blocks() ); return $pattern; @@ -206,16 +233,17 @@ final class WP_Block_Patterns_Registry { * and per style. */ public function get_all_registered( $outside_init_only = false ) { - $patterns = array_values( - $outside_init_only + $patterns = $outside_init_only ? $this->registered_patterns_outside_init - : $this->registered_patterns - ); + : $this->registered_patterns; $hooked_blocks = get_hooked_blocks(); + foreach ( $patterns as $index => $pattern ) { + $pattern['content'] = $this->get_content( $pattern['name'], $outside_init_only ); $patterns[ $index ]['content'] = $this->prepare_content( $pattern, $hooked_blocks ); } - return $patterns; + + return array_values( $patterns ); } /** diff --git a/wp-includes/version.php b/wp-includes/version.php index d5cee36232..d2e65f3c01 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.5-beta2-57682'; +$wp_version = '6.5-beta2-57683'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.