From a2e34fea9663bf062e67fc9f06d0dee39efd8c96 Mon Sep 17 00:00:00 2001 From: Joe McGill Date: Wed, 21 Feb 2024 18:48:30 +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. git-svn-id: https://develop.svn.wordpress.org/trunk@57683 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/block-patterns.php | 8 +-- .../class-wp-block-patterns-registry.php | 52 ++++++++++++++----- 2 files changed, 41 insertions(+), 19 deletions(-) diff --git a/src/wp-includes/block-patterns.php b/src/wp-includes/block-patterns.php index e5c770e1d4..cb58d9842c 100644 --- a/src/wp-includes/block-patterns.php +++ b/src/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/src/wp-includes/class-wp-block-patterns-registry.php b/src/wp-includes/class-wp-block-patterns-registry.php index a11bac06be..1402819c84 100644 --- a/src/wp-includes/class-wp-block-patterns-registry.php +++ b/src/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 ); } /**