Editor: Load pattern content only when used.

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
This commit is contained in:
Joe McGill 2024-02-21 18:50:07 +00:00
parent 26673814df
commit f599968388
3 changed files with 42 additions and 20 deletions

View File

@ -391,13 +391,7 @@ function _register_theme_block_patterns() {
continue; continue;
} }
// The actual pattern content is the output of the file. $pattern_data['file_path'] = $file_path;
ob_start();
include $file_path;
$pattern_data['content'] = ob_get_clean();
if ( ! $pattern_data['content'] ) {
continue;
}
// Translate the pattern metadata. // Translate the pattern metadata.
// phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText,WordPress.WP.I18n.NonSingularStringLiteralDomain,WordPress.WP.I18n.LowLevelTranslationFunction // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText,WordPress.WP.I18n.NonSingularStringLiteralDomain,WordPress.WP.I18n.LowLevelTranslationFunction

View File

@ -101,13 +101,15 @@ final class WP_Block_Patterns_Registry {
return false; return false;
} }
if ( ! isset( $pattern_properties['content'] ) || ! is_string( $pattern_properties['content'] ) ) { if ( ! isset( $pattern_properties['file_path'] ) ) {
_doing_it_wrong( if ( ! isset( $pattern_properties['content'] ) || ! is_string( $pattern_properties['content'] ) ) {
__METHOD__, _doing_it_wrong(
__( 'Pattern content must be a string.' ), __METHOD__,
'5.5.0' __( 'Pattern content must be a string.' ),
); '5.5.0'
return false; );
return false;
}
} }
$pattern = array_merge( $pattern = array_merge(
@ -177,6 +179,30 @@ final class WP_Block_Patterns_Registry {
return $content; 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. * 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 = $this->registered_patterns[ $pattern_name ];
$pattern['content'] = $this->get_content( $pattern_name );
$pattern['content'] = $this->prepare_content( $pattern, get_hooked_blocks() ); $pattern['content'] = $this->prepare_content( $pattern, get_hooked_blocks() );
return $pattern; return $pattern;
@ -206,16 +233,17 @@ final class WP_Block_Patterns_Registry {
* and per style. * and per style.
*/ */
public function get_all_registered( $outside_init_only = false ) { public function get_all_registered( $outside_init_only = false ) {
$patterns = array_values( $patterns = $outside_init_only
$outside_init_only
? $this->registered_patterns_outside_init ? $this->registered_patterns_outside_init
: $this->registered_patterns : $this->registered_patterns;
);
$hooked_blocks = get_hooked_blocks(); $hooked_blocks = get_hooked_blocks();
foreach ( $patterns as $index => $pattern ) { foreach ( $patterns as $index => $pattern ) {
$pattern['content'] = $this->get_content( $pattern['name'], $outside_init_only );
$patterns[ $index ]['content'] = $this->prepare_content( $pattern, $hooked_blocks ); $patterns[ $index ]['content'] = $this->prepare_content( $pattern, $hooked_blocks );
} }
return $patterns;
return array_values( $patterns );
} }
/** /**

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @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. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.