diff options
Diffstat (limited to 'wp-includes/class-wp-block-patterns-registry.php')
-rw-r--r-- | wp-includes/class-wp-block-patterns-registry.php | 58 |
1 files changed, 45 insertions, 13 deletions
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( @@ -178,6 +184,30 @@ final class WP_Block_Patterns_Registry { } /** + * 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. * * @since 5.5.0 @@ -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 ); } /** |