Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pattern Directory API: Pass through the pattern's blockTypes to support pattern transforms #32113

Closed
wants to merge 6 commits into from
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
79 changes: 68 additions & 11 deletions lib/block-patterns.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,17 +199,31 @@ function gutenberg_remove_core_patterns() {
}
}

/**
* Normalize the settings from the API (snake_case) to the format expected by `register_block_pattern` (camelCase).
*
* @param array $settings Settings as returned from the API.
*/
function gutenber_pattern_normalize_settings( $settings ) {
if ( isset( $settings['block_types'] ) ) {
$settings['blockTypes'] = $settings['block_types'];
unset( $settings['block_types'] );
}

if ( isset( $settings['viewport_width'] ) ) {
$settings['viewportWidth'] = $settings['viewport_width'];
unset( $settings['viewport_width'] );
}

return (array) $settings;
}

/**
* Register Core's official patterns from wordpress.org/patterns.
*
* @since 5.8.0
*/
function gutenberg_load_remote_core_patterns() {
// This is the core function that provides the same feature.
if ( function_exists( '_load_remote_block_patterns' ) ) {
return;
}

/**
* Filter to disable remote block patterns.
*
Expand All @@ -231,7 +245,11 @@ function gutenberg_load_remote_core_patterns() {

foreach ( $patterns as $settings ) {
$pattern_name = 'core/' . sanitize_title( $settings['title'] );
register_block_pattern( $pattern_name, (array) $settings );
if ( WP_Block_Patterns_Registry::get_instance()->is_registered( $pattern_name ) ) {
unregister_block_pattern( $pattern_name );
}
$normalized_settings = gutenber_pattern_normalize_settings( $settings );
register_block_pattern( $pattern_name, $normalized_settings );
}
}
}
Expand Down Expand Up @@ -269,14 +287,17 @@ function gutenberg_load_remote_featured_patterns() {
$pattern_name = sanitize_title( $pattern['title'] );
$registry = WP_Block_Patterns_Registry::get_instance();
// Some patterns might be already registerd as `core patterns with the `core` prefix.
$is_registered = $registry->is_registered( $pattern_name ) || $registry->is_registered( "core/$pattern_name" );
if ( ! $is_registered ) {
register_block_pattern( $pattern_name, (array) $pattern );
if ( $registry->is_registered( $pattern_name ) ) {
unregister_block_pattern( $pattern_name );
} elseif ( $registry->is_registered( "core/$pattern_name" ) ) {
unregister_block_pattern( "core/$pattern_name" );
}

$normalized_settings = gutenber_pattern_normalize_settings( $pattern );
register_block_pattern( $pattern_name, $normalized_settings );
}
}


add_action(
'init',
function() {
Expand All @@ -300,5 +321,41 @@ function( $current_screen ) {
gutenberg_load_remote_core_patterns();
gutenberg_load_remote_featured_patterns();
}
}
},
15
);

/**
* Inject the block_types value into the API response.
*
* @param WP_REST_Response $response The response object.
* @param object $raw_pattern The unprepared pattern.
*/
function filter_block_pattern_response( $response, $raw_pattern ) {
$data = $response->get_data();
$data['block_types'] = array_map( 'sanitize_text_field', $raw_pattern->meta->wpop_block_types );
$response->set_data( $data );
return $response;
}
add_filter( 'rest_prepare_block_pattern', 'filter_block_pattern_response', 10, 2 );

/**
* Add the schema to the block_types value. The value itself is injected in the
* `rest_prepare_*` filter, so that the raw pattern response is available.
*/
function add_block_pattern_block_types_schema() {
register_rest_field(
'pattern-directory-item',
'block_types',
array(
'schema' => array(
'description' => __( 'The block types which can use this pattern.', 'gutenberg' ),
'type' => 'array',
'uniqueItems' => true,
'items' => array( 'type' => 'string' ),
'context' => array( 'view', 'embed' ),
),
)
);
}
add_filter( 'rest_api_init', 'add_block_pattern_block_types_schema' );
9 changes: 6 additions & 3 deletions phpunit/fixtures/rest-api/pattern-directory/browse-all.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"meta": {
"spay_email": "",
"wpop_description": "A heading preceded by a chapter number, and followed by a paragraph.",
"wpop_viewport_width": 1000
"wpop_viewport_width": 1000,
"wpop_block_types": [ "core/heading" ]
},
"category_slugs": [ "text" ],
"keyword_slugs": [ "core" ],
Expand All @@ -25,7 +26,8 @@
"meta": {
"spay_email": "",
"wpop_description": "A large hero section with an example background image and a heading in the center.",
"wpop_viewport_width": 1000
"wpop_viewport_width": 1000,
"wpop_block_types": []
},
"category_slugs": [ "header" ],
"keyword_slugs": [ "core" ],
Expand All @@ -41,7 +43,8 @@
"meta": {
"spay_email": "",
"wpop_description": "A large hero section with a bright gradient background, a big heading and a filled button.",
"wpop_viewport_width": 1000
"wpop_viewport_width": 1000,
"wpop_block_types": []
},
"category_slugs": [ "header" ],
"keyword_slugs": [ "core" ],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"meta": {
"spay_email": "",
"wpop_description": "Three filled buttons with rounded corners, side by side.",
"wpop_viewport_width": 600
"wpop_viewport_width": 600,
"wpop_block_types": []
},
"category_slugs": [ "buttons" ],
"keyword_slugs": [ "core" ],
Expand All @@ -25,7 +26,8 @@
"meta": {
"spay_email": "",
"wpop_description": "Two buttons, one filled and one outlined, side by side.",
"wpop_viewport_width": 500
"wpop_viewport_width": 500,
"wpop_block_types": []
},
"category_slugs": [ "buttons" ],
"keyword_slugs": [ "core" ],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"meta": {
"spay_email": "",
"wpop_description": "A heading preceded by a chapter number, and followed by a paragraph.",
"wpop_viewport_width": 1000
"wpop_viewport_width": 1000,
"wpop_block_types": []
},
"category_slugs": [ "text" ],
"keyword_slugs": [ "core" ],
Expand All @@ -25,7 +26,8 @@
"meta": {
"spay_email": "",
"wpop_description": "A large hero section with an example background image and a heading in the center.",
"wpop_viewport_width": 1000
"wpop_viewport_width": 1000,
"wpop_block_types": []
},
"category_slugs": [ "header" ],
"keyword_slugs": [ "core" ],
Expand All @@ -41,7 +43,8 @@
"meta": {
"spay_email": "",
"wpop_description": "A large hero section with a bright gradient background, a big heading and a filled button.",
"wpop_viewport_width": 1000
"wpop_viewport_width": 1000,
"wpop_block_types": []
},
"category_slugs": [ "header" ],
"keyword_slugs": [ "core" ],
Expand Down
12 changes: 8 additions & 4 deletions phpunit/fixtures/rest-api/pattern-directory/search-button.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"meta": {
"spay_email": "",
"wpop_description": "A large hero section with a bright gradient background, a big heading and a filled button.",
"wpop_viewport_width": 1000
"wpop_viewport_width": 1000,
"wpop_block_types": []
},
"category_slugs": [ "header" ],
"keyword_slugs": [ "core" ],
Expand All @@ -25,7 +26,8 @@
"meta": {
"spay_email": "",
"wpop_description": "Three small columns of text, each with an outlined button with rounded corners at the bottom.",
"wpop_viewport_width": 1000
"wpop_viewport_width": 1000,
"wpop_block_types": []
},
"category_slugs": [ "columns" ],
"keyword_slugs": [ "core" ],
Expand All @@ -41,7 +43,8 @@
"meta": {
"spay_email": "",
"wpop_description": "Three filled buttons with rounded corners, side by side.",
"wpop_viewport_width": 600
"wpop_viewport_width": 600,
"wpop_block_types": []
},
"category_slugs": [ "buttons" ],
"keyword_slugs": [ "core" ],
Expand All @@ -57,7 +60,8 @@
"meta": {
"spay_email": "",
"wpop_description": "Two buttons, one filled and one outlined, side by side.",
"wpop_viewport_width": 500
"wpop_viewport_width": 500,
"wpop_block_types": []
},
"category_slugs": [ "buttons" ],
"keyword_slugs": [ "core" ],
Expand Down