Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions src/wp-includes/block-patterns.php
Original file line number Diff line number Diff line change
Expand Up @@ -377,13 +377,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
Expand Down
52 changes: 40 additions & 12 deletions src/wp-includes/class-wp-block-patterns-registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,15 @@ public function register( $pattern_name, $pattern_properties ) {
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(
Expand Down Expand Up @@ -177,6 +179,30 @@ private function prepare_content( $pattern, $hooked_blocks ) {
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.
*
Expand All @@ -191,6 +217,7 @@ public function get_registered( $pattern_name ) {
}

$pattern = $this->registered_patterns[ $pattern_name ];
$pattern['content'] = $this->get_content( $pattern_name );
$pattern['content'] = $this->prepare_content( $pattern, get_hooked_blocks() );

return $pattern;
Expand All @@ -206,16 +233,17 @@ public function get_registered( $pattern_name ) {
* and per style.
*/
public function get_all_registered( $outside_init_only = false ) {
$patterns = array_values(
Comment thread
joemcgill marked this conversation as resolved.
$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 );
}

/**
Expand Down