mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2025-10-16 12:05:38 +00:00
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. git-svn-id: https://develop.svn.wordpress.org/trunk@57683 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
668cbbf7a5
commit
a2e34fea96
@ -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
|
||||
|
||||
@ -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 );
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Loading…
Reference in New Issue
Block a user