-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Add block types REST API. #21065
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
Merged
spacedmonkey
merged 37 commits into
WordPress:master
from
spacedmonkey:feautre/block-api
May 26, 2020
Merged
Add block types REST API. #21065
Changes from 5 commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
4c0c2dc
Add block types REST API.
spacedmonkey 32b995d
Remove space
spacedmonkey 4ec1eea
Change comment.
spacedmonkey 81ae460
Change comment.
spacedmonkey 1be9aaa
Change title.
spacedmonkey 371ce8d
Change comment.
spacedmonkey 9afefda
Remove array_shift.
spacedmonkey 348ff88
Change comment to 5.5.0
spacedmonkey df16a9f
Fixes.
spacedmonkey 98b5e99
Formatting.
spacedmonkey a86db36
Merge branch 'master' into feautre/block-api
spacedmonkey c83d05b
More tweaks
spacedmonkey 730acc5
Fix lints
spacedmonkey 3905157
Tweak fields.
spacedmonkey 8f32a72
Feedback.
spacedmonkey 3479ffc
Add block category.
spacedmonkey acf6cbe
Feedback
spacedmonkey bfc2e07
Add back composer.lock.
spacedmonkey 0e89f71
Merge branch 'master' into feautre/block-api
spacedmonkey ce8ea79
Tweak descriptions.
spacedmonkey e75b647
Add supports.
spacedmonkey 6c2b6a6
Add unit tests.
spacedmonkey 9eea8a3
More tweaks
spacedmonkey f11b18d
Fix tests
spacedmonkey a45c48c
Tweak test.
spacedmonkey dbcca03
Reuse fake block
spacedmonkey 0ea4d71
Add in new fields and improve tests.
spacedmonkey 50efa40
Fix lint
spacedmonkey f9878fd
Reuse wp_error.
spacedmonkey b9675fd
Add more tests
spacedmonkey 62f604e
Improve asserts.
spacedmonkey 6db18c8
Refactor permision check
spacedmonkey fcb6a1d
Add more tests.
spacedmonkey 296efe6
Remove unused method.
spacedmonkey b98d04c
Fix tests.
spacedmonkey bfd9650
Use correct name.
spacedmonkey 2744ebf
Improve tests.
spacedmonkey 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 hidden or 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,326 @@ | ||||||
| <?php | ||||||
| /** | ||||||
| * REST API: WP_REST_Block_Types_Controller class | ||||||
| * | ||||||
| * @since 5.4.0 | ||||||
| * @subpackage REST_API | ||||||
| * @package WordPress | ||||||
| */ | ||||||
|
|
||||||
| /** | ||||||
| * Core class used to access block types via the REST API. | ||||||
| * | ||||||
| * @see WP_REST_Controller | ||||||
| */ | ||||||
| class WP_REST_Block_Types_Controller extends WP_REST_Controller { | ||||||
|
|
||||||
| /** | ||||||
| * Constructor. | ||||||
| */ | ||||||
| public function __construct() { | ||||||
| $this->namespace = '__experimental'; | ||||||
| $this->rest_base = 'block-types'; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * 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' ), | ||||||
| 'args' => $this->get_collection_params(), | ||||||
| ), | ||||||
| 'schema' => array( $this, 'get_public_item_schema' ), | ||||||
| ) | ||||||
| ); | ||||||
|
|
||||||
spacedmonkey marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| register_rest_route( | ||||||
| $this->namespace, | ||||||
| '/' . $this->rest_base . '/(?P<namespace>[a-zA-Z0-9_-]+)/(?P<name>[a-zA-Z0-9_-]+)', | ||||||
| array( | ||||||
| 'args' => array( | ||||||
| 'name' => array( | ||||||
| 'description' => __( 'Block name', 'gutenberg' ), | ||||||
gziolo marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| 'type' => 'string', | ||||||
| ), | ||||||
| 'namespace' => array( | ||||||
| 'description' => __( 'Block namsspace', 'gutenberg' ), | ||||||
spacedmonkey marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| 'type' => 'string', | ||||||
| ), | ||||||
| ), | ||||||
| array( | ||||||
| 'methods' => WP_REST_Server::READABLE, | ||||||
| 'callback' => array( $this, 'get_item' ), | ||||||
| 'permission_callback' => array( $this, 'get_item_permissions_check' ), | ||||||
| 'args' => array( | ||||||
| 'context' => $this->get_context_param( array( 'default' => 'view' ) ), | ||||||
| ), | ||||||
| ), | ||||||
| 'schema' => array( $this, 'get_public_item_schema' ), | ||||||
| ) | ||||||
| ); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Checks whether a given request has permission to read post block types. | ||||||
| * | ||||||
| * @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 | ||||||
aduth marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| if ( ! current_user_can( 'edit_posts' ) ) { | ||||||
aduth marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| return new WP_Error( 'rest_cannot_view', __( 'Sorry, you are not allowed to manage block types.', 'gutenberg' ), array( 'status' => rest_authorization_required_code() ) ); | ||||||
spacedmonkey marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| } | ||||||
|
|
||||||
| return true; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Retrieves all post block types, depending on user context. | ||||||
| * | ||||||
| * @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 ) { | ||||||
| $data = array(); | ||||||
| $block_types = WP_Block_Type_Registry::get_instance()->get_all_registered(); | ||||||
|
|
||||||
| // Retrieve the list of registered collection query parameters. | ||||||
| $registered = $this->get_collection_params(); | ||||||
| $namespace = ''; | ||||||
| if ( isset( $registered['namespace'] ) && ! empty( $request['namespace'] ) ) { | ||||||
| $namespace = $request['namespace']; | ||||||
spacedmonkey marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| } | ||||||
|
|
||||||
| foreach ( $block_types as $slug => $obj ) { | ||||||
| $ret = $this->check_read_permission(); | ||||||
spacedmonkey marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
|
|
||||||
| if ( ! $ret ) { | ||||||
| continue; | ||||||
| } | ||||||
|
|
||||||
| $block_type = $this->prepare_item_for_response( $obj, $request ); | ||||||
|
|
||||||
| if ( $namespace ) { | ||||||
spacedmonkey marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| $pieces = explode( '/', $obj->name ); | ||||||
| $block_namespace = array_shift( $pieces ); | ||||||
spacedmonkey marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| if ( $namespace !== $block_namespace ) { | ||||||
| continue; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| $data[ $obj->name ] = $this->prepare_response_for_collection( $block_type ); | ||||||
spacedmonkey marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| } | ||||||
|
|
||||||
| return rest_ensure_response( $data ); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Checks if a given request has access to read a block type. | ||||||
| * | ||||||
| * @param WP_REST_Request $request Full details about the request. | ||||||
| * | ||||||
| * @return WP_Error|bool True if the request has read access for the item, WP_Error object otherwise. | ||||||
| */ | ||||||
| public function get_item_permissions_check( $request ) { | ||||||
| $block_name = $request['namespace'] . '/' . $request['name']; | ||||||
| $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block_name ); | ||||||
|
|
||||||
| if ( empty( $block_type ) ) { | ||||||
| return new WP_Error( 'rest_block_type_invalid', __( 'Invalid block type.', 'gutenberg' ), array( 'status' => 404 ) ); | ||||||
| } | ||||||
spacedmonkey marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
|
|
||||||
| $check = $this->check_read_permission(); | ||||||
|
|
||||||
| if ( ! $check ) { | ||||||
| return new WP_Error( 'rest_cannot_read_block_type', __( 'Cannot view block type.', 'gutenberg' ), array( 'status' => rest_authorization_required_code() ) ); | ||||||
spacedmonkey marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| } | ||||||
|
|
||||||
| return true; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Checks whether a given block type should be visible. | ||||||
| * | ||||||
| * @return WP_Error|bool True if the block type is visible, otherwise false. | ||||||
| */ | ||||||
| protected function check_read_permission() { | ||||||
| if ( ! current_user_can( 'edit_posts' ) ) { | ||||||
| return new WP_Error( 'rest_cannot_view', __( 'Sorry, you are not allowed to manage block types.', 'gutenberg' ), array( 'status' => rest_authorization_required_code() ) ); | ||||||
spacedmonkey marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| } | ||||||
|
|
||||||
| return true; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Retrieves a specific block type. | ||||||
| * | ||||||
| * @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_item( $request ) { | ||||||
| $block_name = $request['namespace'] . '/' . $request['name']; | ||||||
| $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block_name ); | ||||||
|
|
||||||
| if ( empty( $block_type ) ) { | ||||||
| return new WP_Error( 'rest_block_type_invalid', __( 'Invalid block type.', 'gutenberg' ), array( 'status' => 404 ) ); | ||||||
| } | ||||||
|
|
||||||
| $data = $this->prepare_item_for_response( $block_type, $request ); | ||||||
|
|
||||||
| return rest_ensure_response( $data ); | ||||||
TimothyBJacobs marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Prepares a block type object for serialization. | ||||||
| * | ||||||
| * @param stdClass $block_type block type data. | ||||||
|
||||||
| * @param stdClass $block_type block type data. | |
| * @param WP_Block_Type $block_type block type data. |
https://developer.wordpress.org/reference/classes/wp_block_type/
spacedmonkey marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
spacedmonkey marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
spacedmonkey marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
spacedmonkey marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
spacedmonkey marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
spacedmonkey marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
spacedmonkey marked this conversation as resolved.
Show resolved
Hide resolved
spacedmonkey marked this conversation as resolved.
Show resolved
Hide resolved
spacedmonkey marked this conversation as resolved.
Show resolved
Hide resolved
spacedmonkey marked this conversation as resolved.
Show resolved
Hide resolved
spacedmonkey marked this conversation as resolved.
Show resolved
Hide resolved
spacedmonkey marked this conversation as resolved.
Show resolved
Hide resolved
spacedmonkey marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
spacedmonkey marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
spacedmonkey marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
spacedmonkey marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
spacedmonkey marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
spacedmonkey marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
This file contains hidden or 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 hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.