-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
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 fa4e1f5
Core Data: add getBlockPatterns selector (with resolver and state)
jsnajdr 78cbd56
Editor: fetch block patterns from REST if not supplied in editor sett…
jsnajdr e810c38
Site Editor: fetch block patterns from REST, don't preload in settings
jsnajdr 4b43ef7
Remove __experimentalBlockPatterns from settings generated by Core
jsnajdr f7d681a
Re-add the request param
jsnajdr 3fc42fd
Move the patterns endpoint to experimental namespace, change name
jsnajdr 84d3c4e
Disable lint error about unused parameter
jsnajdr 65735f5
Sync schema descriptions with pattern directory endpoint
jsnajdr 2d01beb
Move the REST endpoint to lib/compat/wordpress-6.0
jsnajdr 2368d78
Load remote block patterns when processing the REST request
jsnajdr c7c6fbf
Implement prepare_item_for_response
jsnajdr c1a55d9
Prevent loading remote block patterns in site/post editor, limit to REST
jsnajdr a71161e
Fix name of the REST controller file
jsnajdr 28f9c91
Fix code style nits reported by linter
jsnajdr 3642a83
Add context support
jsnajdr 92b5c2a
Stop calling gutenberg_register_remote_theme_patterns in site/post ed…
jsnajdr b6cd4dd
Document filter param
jsnajdr 9663f48
Add REST endpoint for block pattern categories
jsnajdr b864fa0
Core Data: add getBlockPatternCategories selector (with resolver and …
jsnajdr 124196e
Load block pattern categories from REST instead of settings
jsnajdr f5be609
Remove block pattern categories from server-generated editor settings
jsnajdr e1f3e3e
Fix spacing issues
jsnajdr 8d41a92
Return only requested fields
jsnajdr 5896ea3
Unit tests for the REST controllers
jsnajdr 0f35b2c
Fixup action field name when receiving categories
jsnajdr 71d6c78
Use mock registries in unit tests
jsnajdr bbe130c
Add missing pattern properties
jsnajdr e8c1a9c
Restore original registry instance after test is finished
jsnajdr f2edbd5
Add PHP docs comments to unit test suites
jsnajdr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
lib/compat/wordpress-6.0/class-wp-rest-block-pattern-categories-controller.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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( | ||
$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 ), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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/