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

__experimentalBlockPatterns: load them with REST API #39185

Merged
merged 30 commits into from
Mar 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
528325b
Add REST endpoint to fetch registered block patterns
jsnajdr Mar 1, 2022
fa4e1f5
Core Data: add getBlockPatterns selector (with resolver and state)
jsnajdr Mar 1, 2022
78cbd56
Editor: fetch block patterns from REST if not supplied in editor sett…
jsnajdr Mar 1, 2022
e810c38
Site Editor: fetch block patterns from REST, don't preload in settings
jsnajdr Mar 3, 2022
4b43ef7
Remove __experimentalBlockPatterns from settings generated by Core
jsnajdr Mar 3, 2022
f7d681a
Re-add the request param
jsnajdr Mar 4, 2022
3fc42fd
Move the patterns endpoint to experimental namespace, change name
jsnajdr Mar 16, 2022
84d3c4e
Disable lint error about unused parameter
jsnajdr Mar 17, 2022
65735f5
Sync schema descriptions with pattern directory endpoint
jsnajdr Mar 17, 2022
2d01beb
Move the REST endpoint to lib/compat/wordpress-6.0
jsnajdr Mar 17, 2022
2368d78
Load remote block patterns when processing the REST request
jsnajdr Mar 17, 2022
c7c6fbf
Implement prepare_item_for_response
jsnajdr Mar 17, 2022
c1a55d9
Prevent loading remote block patterns in site/post editor, limit to REST
jsnajdr Mar 17, 2022
a71161e
Fix name of the REST controller file
jsnajdr Mar 17, 2022
28f9c91
Fix code style nits reported by linter
jsnajdr Mar 17, 2022
3642a83
Add context support
jsnajdr Mar 17, 2022
92b5c2a
Stop calling gutenberg_register_remote_theme_patterns in site/post ed…
jsnajdr Mar 17, 2022
b6cd4dd
Document filter param
jsnajdr Mar 17, 2022
9663f48
Add REST endpoint for block pattern categories
jsnajdr Mar 17, 2022
b864fa0
Core Data: add getBlockPatternCategories selector (with resolver and …
jsnajdr Mar 17, 2022
124196e
Load block pattern categories from REST instead of settings
jsnajdr Mar 17, 2022
f5be609
Remove block pattern categories from server-generated editor settings
jsnajdr Mar 17, 2022
e1f3e3e
Fix spacing issues
jsnajdr Mar 17, 2022
8d41a92
Return only requested fields
jsnajdr Mar 22, 2022
5896ea3
Unit tests for the REST controllers
jsnajdr Mar 22, 2022
0f35b2c
Fixup action field name when receiving categories
jsnajdr Mar 23, 2022
71d6c78
Use mock registries in unit tests
jsnajdr Mar 24, 2022
bbe130c
Add missing pattern properties
jsnajdr Mar 24, 2022
e8c1a9c
Restore original registry instance after test is finished
jsnajdr Mar 28, 2022
f2edbd5
Add PHP docs comments to unit test suites
jsnajdr Mar 28, 2022
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
24 changes: 24 additions & 0 deletions docs/reference-guides/data/data-core.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,30 @@ _Returns_

- `?Array`: An array of autosaves for the post, or undefined if there is none.

### getBlockPatternCategories

Retrieve the list of registered block pattern categories.

_Parameters_

- _state_ `Object`: Data state.

_Returns_

- `Array`: Block pattern category list.

### getBlockPatterns

Retrieve the list of registered block patterns.

_Parameters_

- _state_ `Object`: Data state.

_Returns_

- `Array`: Block pattern list.

### getCurrentTheme

Return the current theme.
Expand Down
20 changes: 4 additions & 16 deletions lib/compat/wordpress-5.9/block-patterns.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ function _load_remote_featured_patterns() {
*
* @param bool $should_load_remote
*/
$should_load_remote = apply_filters( 'should_load_remote_block_patterns', true );
if ( ! apply_filters( 'should_load_remote_block_patterns', true ) ) {
return;
}

if ( ! $should_load_remote ) {
if ( ! get_theme_support( 'core-block-patterns' ) ) {
return;
}

Expand All @@ -43,18 +45,4 @@ function _load_remote_featured_patterns() {
}
}
}

add_action(
'current_screen',
function( $current_screen ) {
if ( ! get_theme_support( 'core-block-patterns' ) ) {
return;
}

$is_site_editor = ( function_exists( 'gutenberg_is_edit_site_page' ) && gutenberg_is_edit_site_page( $current_screen->id ) );
if ( $current_screen->is_block_editor || $is_site_editor ) {
_load_remote_featured_patterns();
}
}
);
}
15 changes: 15 additions & 0 deletions lib/compat/wordpress-6.0/block-editor-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,18 @@ function_exists( 'gutenberg_is_edit_site_page' ) &&
}

add_filter( 'block_editor_settings_all', 'gutenberg_get_block_editor_settings', PHP_INT_MAX );

/**
* Removes the unwanted block patterns fields from block editor settings.
*
* @param array $settings Existing block editor settings.
*
* @return array New block editor settings.
*/
function gutenberg_remove_block_patterns_settings( $settings ) {
unset( $settings['__experimentalBlockPatterns'] );
unset( $settings['__experimentalBlockPatternCategories'] );
return $settings;
}

add_filter( 'block_editor_settings_all', 'gutenberg_remove_block_patterns_settings', PHP_INT_MAX );
33 changes: 13 additions & 20 deletions lib/compat/wordpress-6.0/block-patterns.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,23 @@
* `theme.json` file.
*/
function gutenberg_register_remote_theme_patterns() {
if ( ! get_theme_support( 'core-block-patterns' ) ) {
return;
}

if ( ! apply_filters( 'should_load_remote_block_patterns', true ) ) {
return;
}

if ( ! WP_Theme_JSON_Resolver_Gutenberg::theme_has_support() ) {
return;
}

$pattern_settings = WP_Theme_JSON_Resolver_Gutenberg::get_theme_data()->get_patterns();
if ( empty( $pattern_settings ) ) {
return;
}

$request = new WP_REST_Request( 'GET', '/wp/v2/pattern-directory/patterns' );
$request['slug'] = implode( ',', $pattern_settings );
$response = rest_do_request( $request );
Expand All @@ -32,26 +45,6 @@ function gutenberg_register_remote_theme_patterns() {
}
}

add_action(
'current_screen',
function( $current_screen ) {
if ( ! get_theme_support( 'core-block-patterns' ) ) {
return;
}
if ( ! apply_filters( 'should_load_remote_block_patterns', true ) ) {
return;
}
if ( ! WP_Theme_JSON_Resolver_Gutenberg::theme_has_support() ) {
return;
}

$is_site_editor = ( function_exists( 'gutenberg_is_edit_site_page' ) && gutenberg_is_edit_site_page( $current_screen->id ) );
if ( $current_screen->is_block_editor || $is_site_editor ) {
gutenberg_register_remote_theme_patterns();
}
}
);

/**
* Register any patterns that the active theme may provide under its
* `./patterns/` directory. Each pattern is defined as a PHP file and defines
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php
/**
* REST API: WP_REST_Block_Pattern_Catergories_Controller class
*
* @subpackage REST_API
* @package WordPress
*/

/**
* Core class used to access block pattern categories via the REST API.
*
* @see WP_REST_Controller
*/
class WP_REST_Block_Pattern_Categories_Controller extends WP_REST_Controller {

/**
* Constructor.
*/
public function __construct() {
$this->namespace = '__experimental';
$this->rest_base = 'block-patterns/categories';
}

/**
* Registers the routes for the objects of the controller.
*
* @see register_rest_route()
*/
public function register_routes() {
register_rest_route(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there anyway to get a single block pattern categories? Like __experimental/block-patterns/categories/

$this->namespace,
'/' . $this->rest_base,
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
),
'schema' => array( $this, 'get_public_item_schema' ),
'allow_batch' => array( 'v1' => true ),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is batching support needed here.

)
);
}

/**
* Checks whether a given request has permission to read block patterns.
*
* @param WP_REST_Request $request Full details about the request.
*
* @return WP_Error|bool True if the request has read access, WP_Error object otherwise.
*/
public function get_items_permissions_check( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
if ( current_user_can( 'edit_posts' ) ) {
return true;
}

foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $post_type ) {
if ( current_user_can( $post_type->cap->edit_posts ) ) {
return true;
}
}

return new WP_Error(
'rest_cannot_view',
__( 'Sorry, you are not allowed to view the registered block pattern categories.', 'gutenberg' ),
array( 'status' => rest_authorization_required_code() )
);
}

/**
* Retrieves all block pattern categories.
*
* @param WP_REST_Request $request Full details about the request.
*
* @return WP_Error|WP_REST_Response Response object on success, or WP_Error object on failure.
*/
public function get_items( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
$response = array();
$categories = WP_Block_Pattern_Categories_Registry::get_instance()->get_all_registered();
foreach ( $categories as $category ) {
$prepared_category = $this->prepare_item_for_response( $category, $request );
$response[] = $this->prepare_response_for_collection( $prepared_category );
}
return rest_ensure_response( $response );
}

/**
* Prepare a raw block pattern category before it gets output in a REST API response.
*
* @param object $item Raw category as registered, before any changes.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response
*/
public function prepare_item_for_response( $item, $request ) {
$fields = $this->get_fields_for_response( $request );
$keys = array( 'name', 'label' );
$data = array();
foreach ( $keys as $key ) {
if ( rest_is_field_included( $key, $fields ) ) {
$data[ $key ] = $item[ $key ];
}
}

$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );
return rest_ensure_response( $data );
}

/**
* Retrieves the block pattern category schema, conforming to JSON Schema.
*
* @return array Item schema data.
*/
public function get_item_schema() {
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'block-pattern-category',
'type' => 'object',
'properties' => array(
'name' => array(
'description' => __( 'The category name.', 'gutenberg' ),
'type' => 'string',
'readonly' => true,
'context' => array( 'view', 'embed' ),
),
'label' => array(
'description' => __( 'The category label, in human readable format.', 'gutenberg' ),
'type' => 'string',
'readonly' => true,
'context' => array( 'view', 'embed' ),
),
),
);

return $this->add_additional_fields_schema( $schema );
}
}
Loading