kollapsminoriteten/wp-includes/class-wp-block-patterns-reg...

317 lines
11 KiB
PHP
Raw Normal View History

2020-09-15 14:29:22 +02:00
<?php
/**
* Blocks API: WP_Block_Patterns_Registry class
*
* @package WordPress
* @subpackage Blocks
* @since 5.5.0
*/
/**
2022-04-02 10:26:41 +02:00
* Class used for interacting with block patterns.
2020-09-15 14:29:22 +02:00
*
* @since 5.5.0
*/
2022-12-15 17:47:31 +01:00
#[AllowDynamicProperties]
2020-09-15 14:29:22 +02:00
final class WP_Block_Patterns_Registry {
/**
2022-04-02 10:26:41 +02:00
* Registered block patterns array.
2020-09-15 14:29:22 +02:00
*
2020-12-10 14:06:04 +01:00
* @since 5.5.0
2022-06-16 14:03:35 +02:00
* @var array[]
2020-09-15 14:29:22 +02:00
*/
private $registered_patterns = array();
2022-06-16 14:03:35 +02:00
/**
* Patterns registered outside the `init` action.
*
* @since 6.0.0
* @var array[]
*/
private $registered_patterns_outside_init = array();
2020-09-15 14:29:22 +02:00
/**
* Container for the main instance of the class.
*
2020-12-10 14:06:04 +01:00
* @since 5.5.0
2020-09-15 14:29:22 +02:00
* @var WP_Block_Patterns_Registry|null
*/
private static $instance = null;
/**
2022-04-02 10:26:41 +02:00
* Registers a block pattern.
2020-09-15 14:29:22 +02:00
*
* @since 5.5.0
2022-06-16 14:03:35 +02:00
* @since 5.8.0 Added support for the `blockTypes` property.
2023-04-26 17:39:43 +02:00
* @since 6.1.0 Added support for the `postTypes` property.
* @since 6.2.0 Added support for the `templateTypes` property.
2024-04-17 11:32:24 +02:00
* @since 6.5.0 Added support for the `filePath` property.
2020-09-15 14:29:22 +02:00
*
2022-04-02 10:26:41 +02:00
* @param string $pattern_name Block pattern name including namespace.
* @param array $pattern_properties {
* List of properties for the block pattern.
*
2023-09-26 10:33:34 +02:00
* @type string $title Required. A human-readable title for the pattern.
2024-04-17 11:32:24 +02:00
* @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.
2023-09-26 10:33:34 +02:00
* @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
* pattern does. The description will help users discover the
* pattern while searching.
* @type int $viewportWidth Optional. The intended width of the pattern to allow for a scaled
* preview within the pattern inserter.
* @type bool $inserter Optional. Determines whether the pattern is visible in inserter.
* To hide a pattern so that it can only be inserted programmatically,
* set this to false. Default true.
* @type string[] $categories Optional. A list of registered pattern categories used to group
* block patterns. Block patterns can be shown on multiple categories.
* A category must be registered separately in order to be used here.
* @type string[] $keywords Optional. A list of aliases or keywords that help users discover
* the pattern while searching.
* @type string[] $blockTypes Optional. A list of block names including namespace that could use
* the block pattern in certain contexts (placeholder, transforms).
* The block pattern is available in the block editor inserter
* regardless of this list of block names.
* Certain blocks support further specificity besides the block name
* (e.g. for `core/template-part` you can specify areas
* like `core/template-part/header` or `core/template-part/footer`).
* @type string[] $postTypes Optional. An array of post types that the pattern is restricted
* to be used with. The pattern will only be available when editing one
* 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.
2024-04-17 11:32:24 +02:00
* @type string $filePath Optional. The full path to the file containing the block pattern content.
2022-04-02 10:26:41 +02:00
* }
2020-09-15 14:29:22 +02:00
* @return bool True if the pattern was registered with success and false otherwise.
*/
public function register( $pattern_name, $pattern_properties ) {
if ( ! isset( $pattern_name ) || ! is_string( $pattern_name ) ) {
2021-07-23 11:58:50 +02:00
_doing_it_wrong(
__METHOD__,
__( 'Pattern name must be a string.' ),
'5.5.0'
);
2020-09-15 14:29:22 +02:00
return false;
}
if ( ! isset( $pattern_properties['title'] ) || ! is_string( $pattern_properties['title'] ) ) {
2021-07-23 11:58:50 +02:00
_doing_it_wrong(
__METHOD__,
__( 'Pattern title must be a string.' ),
'5.5.0'
);
2020-09-15 14:29:22 +02:00
return false;
}
2024-04-17 11:32:24 +02:00
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;
}
2020-09-15 14:29:22 +02:00
}
2022-06-16 14:03:35 +02:00
$pattern = array_merge(
2020-09-15 14:29:22 +02:00
$pattern_properties,
array( 'name' => $pattern_name )
);
2022-12-15 17:47:31 +01:00
2022-06-16 14:03:35 +02:00
$this->registered_patterns[ $pattern_name ] = $pattern;
// If the pattern is registered inside an action other than `init`, store it
// also to a dedicated array. Used to detect deprecated registrations inside
// `admin_init` or `current_screen`.
if ( current_action() && 'init' !== current_action() ) {
$this->registered_patterns_outside_init[ $pattern_name ] = $pattern;
}
2020-09-15 14:29:22 +02:00
return true;
}
/**
2022-04-02 10:26:41 +02:00
* Unregisters a block pattern.
2020-09-15 14:29:22 +02:00
*
* @since 5.5.0
*
2022-04-02 10:26:41 +02:00
* @param string $pattern_name Block pattern name including namespace.
2020-09-15 14:29:22 +02:00
* @return bool True if the pattern was unregistered with success and false otherwise.
*/
public function unregister( $pattern_name ) {
if ( ! $this->is_registered( $pattern_name ) ) {
2021-07-23 11:58:50 +02:00
_doing_it_wrong(
__METHOD__,
/* translators: %s: Pattern name. */
sprintf( __( 'Pattern "%s" not found.' ), $pattern_name ),
'5.5.0'
);
2020-09-15 14:29:22 +02:00
return false;
}
unset( $this->registered_patterns[ $pattern_name ] );
2022-06-16 14:03:35 +02:00
unset( $this->registered_patterns_outside_init[ $pattern_name ] );
2020-09-15 14:29:22 +02:00
return true;
}
2024-04-17 11:32:24 +02:00
/**
* 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;
}
2026-03-31 11:30:59 +02:00
$file_path = $patterns[ $pattern_name ]['filePath'] ?? '';
$is_stringy = is_string( $file_path ) || ( is_object( $file_path ) && method_exists( $file_path, '__toString' ) );
$pattern_path = $is_stringy ? realpath( (string) $file_path ) : null;
if (
! isset( $patterns[ $pattern_name ]['content'] ) &&
is_string( $pattern_path ) &&
( str_ends_with( $pattern_path, '.php' ) || str_ends_with( $pattern_path, '.html' ) ) &&
is_file( $pattern_path ) &&
is_readable( $pattern_path )
) {
2024-04-17 11:32:24 +02:00
ob_start();
include $patterns[ $pattern_name ]['filePath'];
$patterns[ $pattern_name ]['content'] = ob_get_clean();
unset( $patterns[ $pattern_name ]['filePath'] );
}
2026-03-31 11:30:59 +02:00
2024-04-17 11:32:24 +02:00
return $patterns[ $pattern_name ]['content'];
}
2020-09-15 14:29:22 +02:00
/**
2022-04-02 10:26:41 +02:00
* Retrieves an array containing the properties of a registered block pattern.
2020-09-15 14:29:22 +02:00
*
* @since 5.5.0
*
2022-04-02 10:26:41 +02:00
* @param string $pattern_name Block pattern name including namespace.
2025-12-12 13:15:55 +01:00
* @return array|null Registered pattern properties or `null` if the pattern is not registered.
2020-09-15 14:29:22 +02:00
*/
public function get_registered( $pattern_name ) {
if ( ! $this->is_registered( $pattern_name ) ) {
return null;
}
2023-12-07 09:44:11 +01:00
$pattern = $this->registered_patterns[ $pattern_name ];
2025-02-28 08:42:11 +01:00
$content = $this->get_content( $pattern_name );
$pattern['content'] = apply_block_hooks_to_content(
$content,
$pattern,
'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata'
);
2023-12-07 09:44:11 +01:00
return $pattern;
2020-09-15 14:29:22 +02:00
}
/**
2022-04-02 10:26:41 +02:00
* Retrieves all registered block patterns.
2020-09-15 14:29:22 +02:00
*
* @since 5.5.0
*
2022-06-16 14:03:35 +02:00
* @param bool $outside_init_only Return only patterns registered outside the `init` action.
* @return array[] Array of arrays containing the registered block patterns properties,
* and per style.
2020-09-15 14:29:22 +02:00
*/
2022-06-16 14:03:35 +02:00
public function get_all_registered( $outside_init_only = false ) {
2024-04-17 11:32:24 +02:00
$patterns = $outside_init_only
2022-06-16 14:03:35 +02:00
? $this->registered_patterns_outside_init
2024-04-17 11:32:24 +02:00
: $this->registered_patterns;
2023-12-07 09:44:11 +01:00
$hooked_blocks = get_hooked_blocks();
2024-04-17 11:32:24 +02:00
2023-12-07 09:44:11 +01:00
foreach ( $patterns as $index => $pattern ) {
2025-02-28 08:42:11 +01:00
$content = $this->get_content( $pattern['name'], $outside_init_only );
$patterns[ $index ]['content'] = apply_block_hooks_to_content(
$content,
$pattern,
'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata'
);
2023-12-07 09:44:11 +01:00
}
2024-04-17 11:32:24 +02:00
return array_values( $patterns );
2020-09-15 14:29:22 +02:00
}
/**
2022-04-02 10:26:41 +02:00
* Checks if a block pattern is registered.
2020-09-15 14:29:22 +02:00
*
* @since 5.5.0
*
2025-12-12 13:15:55 +01:00
* @param string|null $pattern_name Block pattern name including namespace.
2020-09-15 14:29:22 +02:00
* @return bool True if the pattern is registered, false otherwise.
*/
public function is_registered( $pattern_name ) {
2025-12-12 13:15:55 +01:00
return isset( $pattern_name, $this->registered_patterns[ $pattern_name ] );
2020-09-15 14:29:22 +02:00
}
2023-12-07 09:44:11 +01:00
public function __wakeup() {
if ( ! $this->registered_patterns ) {
return;
}
if ( ! is_array( $this->registered_patterns ) ) {
throw new UnexpectedValueException();
}
foreach ( $this->registered_patterns as $value ) {
if ( ! is_array( $value ) ) {
throw new UnexpectedValueException();
}
}
$this->registered_patterns_outside_init = array();
}
2020-09-15 14:29:22 +02:00
/**
* Utility method to retrieve the main instance of the class.
*
* The instance will be created if it does not exist yet.
*
* @since 5.5.0
*
* @return WP_Block_Patterns_Registry The main instance.
*/
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
}
/**
2022-04-02 10:26:41 +02:00
* Registers a new block pattern.
2020-09-15 14:29:22 +02:00
*
* @since 5.5.0
*
2022-04-02 10:26:41 +02:00
* @param string $pattern_name Block pattern name including namespace.
* @param array $pattern_properties List of properties for the block pattern.
* See WP_Block_Patterns_Registry::register() for accepted arguments.
2020-09-15 14:29:22 +02:00
* @return bool True if the pattern was registered with success and false otherwise.
*/
function register_block_pattern( $pattern_name, $pattern_properties ) {
return WP_Block_Patterns_Registry::get_instance()->register( $pattern_name, $pattern_properties );
}
/**
2022-04-02 10:26:41 +02:00
* Unregisters a block pattern.
2020-09-15 14:29:22 +02:00
*
* @since 5.5.0
*
2022-04-02 10:26:41 +02:00
* @param string $pattern_name Block pattern name including namespace.
2020-09-15 14:29:22 +02:00
* @return bool True if the pattern was unregistered with success and false otherwise.
*/
function unregister_block_pattern( $pattern_name ) {
return WP_Block_Patterns_Registry::get_instance()->unregister( $pattern_name );
}