Editor: allow filtering block patterns by source.

Extends the REST API endpoint for block patterns to provide a source attribute that can be used to filter patterns.

Props aaronrobertshaw, mukesh27, talldanwp, ramonopoly.
Fixes 58622.

Built from https://develop.svn.wordpress.org/trunk@56063


git-svn-id: http://core.svn.wordpress.org/trunk@55575 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
isabel_brison 2023-06-27 11:13:27 +00:00
parent 033bca66f8
commit d7bda1a6be
3 changed files with 28 additions and 5 deletions

View File

@ -12,6 +12,7 @@ add_theme_support( 'core-block-patterns' );
* Registers the core block patterns and categories. * Registers the core block patterns and categories.
* *
* @since 5.5.0 * @since 5.5.0
* @since 6.3.0 Added source to core block patterns.
* @access private * @access private
*/ */
function _register_core_block_patterns_and_categories() { function _register_core_block_patterns_and_categories() {
@ -29,10 +30,9 @@ function _register_core_block_patterns_and_categories() {
); );
foreach ( $core_block_patterns as $core_block_pattern ) { foreach ( $core_block_patterns as $core_block_pattern ) {
register_block_pattern( $pattern = require __DIR__ . '/block-patterns/' . $core_block_pattern . '.php';
'core/' . $core_block_pattern, $pattern['source'] = 'core';
require __DIR__ . '/block-patterns/' . $core_block_pattern . '.php' register_block_pattern( 'core/' . $core_block_pattern, $pattern );
);
} }
} }
@ -190,6 +190,7 @@ function wp_normalize_remote_block_pattern( $pattern ) {
* @since 5.9.0 The $current_screen argument was removed. * @since 5.9.0 The $current_screen argument was removed.
* @since 6.2.0 Normalize the pattern from the API (snake_case) to the * @since 6.2.0 Normalize the pattern from the API (snake_case) to the
* format expected by `register_block_pattern` (camelCase). * format expected by `register_block_pattern` (camelCase).
* @since 6.3.0 Add 'pattern-directory/core' to the pattern's 'source'.
* *
* @param WP_Screen $deprecated Unused. Formerly the screen that the current request was triggered from. * @param WP_Screen $deprecated Unused. Formerly the screen that the current request was triggered from.
*/ */
@ -224,6 +225,7 @@ function _load_remote_block_patterns( $deprecated = null ) {
$patterns = $response->get_data(); $patterns = $response->get_data();
foreach ( $patterns as $pattern ) { foreach ( $patterns as $pattern ) {
$pattern['source'] = 'pattern-directory/core';
$normalized_pattern = wp_normalize_remote_block_pattern( $pattern ); $normalized_pattern = wp_normalize_remote_block_pattern( $pattern );
$pattern_name = 'core/' . sanitize_title( $normalized_pattern['title'] ); $pattern_name = 'core/' . sanitize_title( $normalized_pattern['title'] );
register_block_pattern( $pattern_name, $normalized_pattern ); register_block_pattern( $pattern_name, $normalized_pattern );
@ -237,6 +239,7 @@ function _load_remote_block_patterns( $deprecated = null ) {
* @since 5.9.0 * @since 5.9.0
* @since 6.2.0 Normalized the pattern from the API (snake_case) to the * @since 6.2.0 Normalized the pattern from the API (snake_case) to the
* format expected by `register_block_pattern()` (camelCase). * format expected by `register_block_pattern()` (camelCase).
* @since 6.3.0 Add 'pattern-directory/featured' to the pattern's 'source'.
*/ */
function _load_remote_featured_patterns() { function _load_remote_featured_patterns() {
$supports_core_patterns = get_theme_support( 'core-block-patterns' ); $supports_core_patterns = get_theme_support( 'core-block-patterns' );
@ -258,6 +261,7 @@ function _load_remote_featured_patterns() {
$patterns = $response->get_data(); $patterns = $response->get_data();
$registry = WP_Block_Patterns_Registry::get_instance(); $registry = WP_Block_Patterns_Registry::get_instance();
foreach ( $patterns as $pattern ) { foreach ( $patterns as $pattern ) {
$pattern['source'] = 'pattern-directory/featured';
$normalized_pattern = wp_normalize_remote_block_pattern( $pattern ); $normalized_pattern = wp_normalize_remote_block_pattern( $pattern );
$pattern_name = sanitize_title( $normalized_pattern['title'] ); $pattern_name = sanitize_title( $normalized_pattern['title'] );
// Some patterns might be already registered as core patterns with the `core` prefix. // Some patterns might be already registered as core patterns with the `core` prefix.
@ -275,6 +279,7 @@ function _load_remote_featured_patterns() {
* @since 6.0.0 * @since 6.0.0
* @since 6.2.0 Normalized the pattern from the API (snake_case) to the * @since 6.2.0 Normalized the pattern from the API (snake_case) to the
* format expected by `register_block_pattern()` (camelCase). * format expected by `register_block_pattern()` (camelCase).
* @since 6.3.0 Add 'pattern-directory/theme' to the pattern's 'source'.
* @access private * @access private
*/ */
function _register_remote_theme_patterns() { function _register_remote_theme_patterns() {
@ -301,6 +306,7 @@ function _register_remote_theme_patterns() {
$patterns = $response->get_data(); $patterns = $response->get_data();
$patterns_registry = WP_Block_Patterns_Registry::get_instance(); $patterns_registry = WP_Block_Patterns_Registry::get_instance();
foreach ( $patterns as $pattern ) { foreach ( $patterns as $pattern ) {
$pattern['source'] = 'pattern-directory/theme';
$normalized_pattern = wp_normalize_remote_block_pattern( $pattern ); $normalized_pattern = wp_normalize_remote_block_pattern( $pattern );
$pattern_name = sanitize_title( $normalized_pattern['title'] ); $pattern_name = sanitize_title( $normalized_pattern['title'] );
// Some patterns might be already registered as core patterns with the `core` prefix. // Some patterns might be already registered as core patterns with the `core` prefix.

View File

@ -155,6 +155,7 @@ class WP_REST_Block_Patterns_Controller extends WP_REST_Controller {
* Prepare a raw block pattern before it gets output in a REST API response. * Prepare a raw block pattern before it gets output in a REST API response.
* *
* @since 6.0.0 * @since 6.0.0
* @since 6.3.0 Added `source` property.
* *
* @param array $item Raw pattern as registered, before any changes. * @param array $item Raw pattern as registered, before any changes.
* @param WP_REST_Request $request Request object. * @param WP_REST_Request $request Request object.
@ -174,6 +175,7 @@ class WP_REST_Block_Patterns_Controller extends WP_REST_Controller {
'blockTypes' => 'block_types', 'blockTypes' => 'block_types',
'postTypes' => 'post_types', 'postTypes' => 'post_types',
'templateTypes' => 'template_types', 'templateTypes' => 'template_types',
'source' => 'source',
); );
$data = array(); $data = array();
foreach ( $keys as $item_key => $rest_key ) { foreach ( $keys as $item_key => $rest_key ) {
@ -192,6 +194,7 @@ class WP_REST_Block_Patterns_Controller extends WP_REST_Controller {
* Retrieves the block pattern schema, conforming to JSON Schema. * Retrieves the block pattern schema, conforming to JSON Schema.
* *
* @since 6.0.0 * @since 6.0.0
* @since 6.3.0 Added `source` property.
* *
* @return array Item schema data. * @return array Item schema data.
*/ */
@ -267,6 +270,20 @@ class WP_REST_Block_Patterns_Controller extends WP_REST_Controller {
'readonly' => true, 'readonly' => true,
'context' => array( 'view', 'edit', 'embed' ), 'context' => array( 'view', 'edit', 'embed' ),
), ),
'source' => array(
'description' => __( 'Where the pattern comes from e.g. core' ),
'type' => 'string',
'readonly' => true,
'context' => array( 'view', 'edit', 'embed' ),
'enum' => array(
'core',
'plugin',
'theme',
'pattern-directory/core',
'pattern-directory/theme',
'pattern-directory/featured',
),
),
), ),
); );

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '6.3-alpha-56062'; $wp_version = '6.3-alpha-56063';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.