From 30883c26bdceb9eaf32c8d4a1b0c1bce223b5226 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 09:57:26 +0200 Subject: Adding upstream version 6.5+dfsg1. Signed-off-by: Daniel Baumann --- wp-includes/class-wp-block-patterns-registry.php | 58 ++++++++++++++++++------ 1 file changed, 45 insertions(+), 13 deletions(-) (limited to 'wp-includes/class-wp-block-patterns-registry.php') diff --git a/wp-includes/class-wp-block-patterns-registry.php b/wp-includes/class-wp-block-patterns-registry.php index a11bac0..bee9746 100644 --- a/wp-includes/class-wp-block-patterns-registry.php +++ b/wp-includes/class-wp-block-patterns-registry.php @@ -45,13 +45,16 @@ final class WP_Block_Patterns_Registry { * @since 5.8.0 Added support for the `blockTypes` property. * @since 6.1.0 Added support for the `postTypes` property. * @since 6.2.0 Added support for the `templateTypes` property. + * @since 6.5.0 Added support for the `filePath` property. * * @param string $pattern_name Block pattern name including namespace. * @param array $pattern_properties { * List of properties for the block pattern. * * @type string $title Required. A human-readable title for the pattern. - * @type string $content Required. Block HTML markup for the pattern. + * @type string $content Optional. Block HTML markup for the pattern. + * If not provided, the content will be retrieved from the `filePath` if set. + * If both `content` and `filePath` are not set, the pattern will not be registered. * @type string $description Optional. Visually hidden text used to describe the pattern * in the inserter. A description is optional, but is strongly * encouraged when the title does not fully describe what the @@ -79,6 +82,7 @@ final class WP_Block_Patterns_Registry { * of the post types passed on the array. For all the other post types * not part of the array the pattern is not available at all. * @type string[] $templateTypes Optional. An array of template types where the pattern fits. + * @type string $filePath Optional. The full path to the file containing the block pattern content. * } * @return bool True if the pattern was registered with success and false otherwise. */ @@ -101,13 +105,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['filePath'] ) ) { + 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 +183,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 ]['filePath'] ) ) { + ob_start(); + include $patterns[ $pattern_name ]['filePath']; + $patterns[ $pattern_name ]['content'] = ob_get_clean(); + unset( $patterns[ $pattern_name ]['filePath'] ); + } + return $patterns[ $pattern_name ]['content']; + } + /** * Retrieves an array containing the properties of a registered block pattern. * @@ -191,6 +221,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 +237,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 ); } /** -- cgit v1.2.3