Skip to content

Commit

Permalink
Changes from WordPress#4655
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronrobertshaw committed Jun 26, 2023
1 parent f571f45 commit 49d01ac
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/wp-includes/block-patterns.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* Registers the core block patterns and categories.
*
* @since 5.5.0
* @since 6.3.0 Added source to core block patterns.
* @access private
*/
function _register_core_block_patterns_and_categories() {
Expand All @@ -29,10 +30,9 @@ function _register_core_block_patterns_and_categories() {
);

foreach ( $core_block_patterns as $core_block_pattern ) {
register_block_pattern(
'core/' . $core_block_pattern,
require __DIR__ . '/block-patterns/' . $core_block_pattern . '.php'
);
$pattern = require ABSPATH . WPINC . '/block-patterns/' . $core_block_pattern . '.php';
$pattern['source'] = 'core'; // Added in 6.3.0.
register_block_pattern( 'core/' . $core_block_pattern, $pattern );
}
}

Expand Down Expand Up @@ -190,6 +190,7 @@ function wp_normalize_remote_block_pattern( $pattern ) {
* @since 5.9.0 The $current_screen argument was removed.
* @since 6.2.0 Normalize the pattern from the API (snake_case) to the
* 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.
*/
Expand Down Expand Up @@ -224,6 +225,7 @@ function _load_remote_block_patterns( $deprecated = null ) {
$patterns = $response->get_data();

foreach ( $patterns as $pattern ) {
$pattern['source'] = 'pattern-directory/core'; // Added in 6.3.0.
$normalized_pattern = wp_normalize_remote_block_pattern( $pattern );
$pattern_name = 'core/' . sanitize_title( $normalized_pattern['title'] );
register_block_pattern( $pattern_name, $normalized_pattern );
Expand All @@ -237,6 +239,7 @@ function _load_remote_block_patterns( $deprecated = null ) {
* @since 5.9.0
* @since 6.2.0 Normalized the pattern from the API (snake_case) to the
* 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() {
$supports_core_patterns = get_theme_support( 'core-block-patterns' );
Expand All @@ -258,6 +261,7 @@ function _load_remote_featured_patterns() {
$patterns = $response->get_data();
$registry = WP_Block_Patterns_Registry::get_instance();
foreach ( $patterns as $pattern ) {
$pattern['source'] = 'pattern-directory/featured'; // Added in 6.3.0.
$normalized_pattern = wp_normalize_remote_block_pattern( $pattern );
$pattern_name = sanitize_title( $normalized_pattern['title'] );
// Some patterns might be already registered as core patterns with the `core` prefix.
Expand All @@ -275,6 +279,7 @@ function _load_remote_featured_patterns() {
* @since 6.0.0
* @since 6.2.0 Normalized the pattern from the API (snake_case) to the
* format expected by `register_block_pattern()` (camelCase).
* @since 6.3.0 Add 'pattern-directory/theme' to the pattern's 'source'.
* @access private
*/
function _register_remote_theme_patterns() {
Expand All @@ -301,6 +306,7 @@ function _register_remote_theme_patterns() {
$patterns = $response->get_data();
$patterns_registry = WP_Block_Patterns_Registry::get_instance();
foreach ( $patterns as $pattern ) {
$pattern['source'] = 'pattern-directory/theme'; // Added in 6.3.0.
$normalized_pattern = wp_normalize_remote_block_pattern( $pattern );
$pattern_name = sanitize_title( $normalized_pattern['title'] );
// Some patterns might be already registered as core patterns with the `core` prefix.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ protected function migrate_pattern_categories( $pattern ) {
* Prepare a raw block pattern before it gets output in a REST API response.
*
* @since 6.0.0
* @since 6.3.0 Added `source` property.
*
* @param array $item Raw pattern as registered, before any changes.
* @param WP_REST_Request $request Request object.
Expand All @@ -174,6 +175,7 @@ public function prepare_item_for_response( $item, $request ) {
'blockTypes' => 'block_types',
'postTypes' => 'post_types',
'templateTypes' => 'template_types',
'source' => 'source',
);
$data = array();
foreach ( $keys as $item_key => $rest_key ) {
Expand All @@ -192,6 +194,7 @@ public function prepare_item_for_response( $item, $request ) {
* Retrieves the block pattern schema, conforming to JSON Schema.
*
* @since 6.0.0
* @since 6.3.0 Added `source` property.
*
* @return array Item schema data.
*/
Expand Down Expand Up @@ -267,6 +270,20 @@ public function get_item_schema() {
'readonly' => true,
'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',
),
),
),
);

Expand Down

0 comments on commit 49d01ac

Please sign in to comment.