From 4c0c2dc0987c008473e8ec838ac135c652100674 Mon Sep 17 00:00:00 2001 From: spacedmonkey Date: Sun, 22 Mar 2020 13:00:00 +0000 Subject: [PATCH 01/35] Add block types REST API. --- lib/class-wp-rest-block-types-controller.php | 326 +++++++++++++++++++ lib/load.php | 3 + lib/rest-api.php | 8 + 3 files changed, 337 insertions(+) create mode 100644 lib/class-wp-rest-block-types-controller.php diff --git a/lib/class-wp-rest-block-types-controller.php b/lib/class-wp-rest-block-types-controller.php new file mode 100644 index 00000000000000..9dfcbe6061ae96 --- /dev/null +++ b/lib/class-wp-rest-block-types-controller.php @@ -0,0 +1,326 @@ +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' ), + ) + ); + + register_rest_route( + $this->namespace, + '/' . $this->rest_base . '/(?P[a-zA-Z0-9_-]+)/(?P[a-zA-Z0-9_-]+)', + array( + 'args' => array( + 'name' => array( + 'description' => __( 'Block name', 'gutenberg' ), + 'type' => 'string', + ), + 'namespace' => array( + 'description' => __( 'Block namsspace', 'gutenberg' ), + '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 + 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() ) ); + } + + 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']; + } + + foreach ( $block_types as $slug => $obj ) { + $ret = $this->check_read_permission(); + + if ( ! $ret ) { + continue; + } + + $block_type = $this->prepare_item_for_response( $obj, $request ); + + if ( $namespace ) { + $pieces = explode( '/', $obj->name ); + $block_namespace = array_shift( $pieces ); + if ( $namespace !== $block_namespace ) { + continue; + } + } + + $data[ $obj->name ] = $this->prepare_response_for_collection( $block_type ); + } + + 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 ) ); + } + + $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() ) ); + } + + 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() ) ); + } + + 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 ); + } + + /** + * Prepares a block type object for serialization. + * + * @param stdClass $block_type block type data. + * @param WP_REST_Request $request Full details about the request. + * + * @return WP_REST_Response block type data. + */ + public function prepare_item_for_response( $block_type, $request ) { + + $fields = $this->get_fields_for_response( $request ); + $data = array(); + if ( in_array( 'name', $fields, true ) ) { + $data['name'] = $block_type->name; + } + + if ( in_array( 'attributes', $fields, true ) ) { + $data['attributes'] = $block_type->get_attributes(); + } + + if ( in_array( 'is_dynamic', $fields, true ) ) { + $data['is_dynamic'] = $block_type->is_dynamic(); + } + + $extra_fields = array( 'editor_script', 'script', 'editor_style', 'style' ); + foreach ( $extra_fields as $extra_field ) { + if ( in_array( $extra_field, $fields, true ) ) { + $data[ $extra_field ] = (string) $block_type->$extra_field; + } + } + + $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; + $data = $this->add_additional_fields_to_object( $data, $request ); + $data = $this->filter_response_by_context( $data, $context ); + + $response = rest_ensure_response( $data ); + + $response->add_links( + array( + 'collection' => array( + 'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ), + ), + 'https://api.w.org/items' => array( + 'href' => rest_url( sprintf( '%s/%s/%s', $this->namespace, $this->rest_base, $block_type->name ) ), + ), + ) + ); + + /** + * Filters a block type returned from the REST API. + * + * Allows modification of the block type data right before it is returned. + * + * @param WP_REST_Response $response The response object. + * @param object $block_type The original block type object. + * @param WP_REST_Request $request Request used to generate the response. + */ + return apply_filters( 'rest_prepare_block_type', $response, $block_type, $request ); + } + + /** + * Retrieves the block type' schema, conforming to JSON Schema. + * + * @return array Item schema data. + */ + public function get_item_schema() { + if ( $this->schema ) { + return $this->add_additional_fields_schema( $this->schema ); + } + + $schema = array( + '$schema' => 'http://json-schema.org/draft-04/schema#', + 'title' => 'block_type', + 'type' => 'object', + 'properties' => array( + 'name' => array( + 'description' => __( 'The title for the block type.', 'gutenberg' ), + 'type' => 'string', + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), + 'attributes' => array( + 'description' => __( 'Block attributes.', 'gutenberg' ), + 'type' => 'object', + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), + 'is_dynamic' => array( + 'description' => __( 'Is the block dynamically rendered.', 'gutenberg' ), + 'type' => 'boolean', + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), + 'editor_script' => array( + 'description' => __( 'URL of editor script js file.', 'gutenberg' ), + 'type' => 'string', + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), + 'script' => array( + 'description' => __( 'URL of script js file.', 'gutenberg' ), + 'type' => 'string', + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), + 'editor_style' => array( + 'description' => __( 'URL of editor style css file.', 'gutenberg' ), + 'type' => 'string', + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), + 'style' => array( + 'description' => __( 'URL of style css file.', 'gutenberg' ), + 'type' => 'string', + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), + ), + ); + + $this->schema = $schema; + + return $this->add_additional_fields_schema( $this->schema ); + } + + /** + * Retrieves the query params for collections. + * + * @return array Collection parameters. + */ + public function get_collection_params() { + $new_params = array(); + $new_params['context'] = $this->get_context_param( array( 'default' => 'view' ) ); + $new_params['namespace'] = array( + 'description' => __( 'Block namespace.', 'gutenberg' ), + 'type' => 'string', + ); + return $new_params; + } + +} diff --git a/lib/load.php b/lib/load.php index f1037f11d36be2..04ee5dd91dd872 100644 --- a/lib/load.php +++ b/lib/load.php @@ -39,6 +39,9 @@ function gutenberg_is_experiment_enabled( $name ) { if ( ! class_exists( 'WP_REST_Block_Directory_Controller' ) ) { require dirname( __FILE__ ) . '/class-wp-rest-block-directory-controller.php'; } + if ( ! class_exists( 'WP_REST_Block_Types_Controller' ) ) { + require dirname( __FILE__ ) . '/class-wp-rest-block-types-controller.php'; + } if ( ! class_exists( 'WP_REST_Menus_Controller' ) ) { require_once dirname( __FILE__ ) . '/class-wp-rest-menus-controller.php'; } diff --git a/lib/rest-api.php b/lib/rest-api.php index eba3685e4cdd7a..a2f82b006cd056 100644 --- a/lib/rest-api.php +++ b/lib/rest-api.php @@ -94,6 +94,14 @@ function gutenberg_register_rest_block_directory() { } add_filter( 'rest_api_init', 'gutenberg_register_rest_block_directory' ); +/** + * Registers the Block types REST API routes. + */ +function gutenberg_register_block_type() { + $block_types = new WP_REST_Block_Types_Controller(); + $block_types->register_routes(); +} +add_action( 'rest_api_init', 'gutenberg_register_block_type' ); /** * Registers the menu locations area REST API routes. */ From 32b995d917ac280fad30cf9d4405838cb0e7af4e Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Thu, 7 May 2020 16:50:26 +0100 Subject: [PATCH 02/35] Remove space Co-authored-by: Andrew Duthie --- lib/class-wp-rest-block-types-controller.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/class-wp-rest-block-types-controller.php b/lib/class-wp-rest-block-types-controller.php index 9dfcbe6061ae96..6fbd7e346eaa05 100644 --- a/lib/class-wp-rest-block-types-controller.php +++ b/lib/class-wp-rest-block-types-controller.php @@ -170,7 +170,7 @@ protected function check_read_permission() { * * @return WP_Error|WP_REST_Response Response object on success, or WP_Error object on failure. */ - public function get_item( $request ) { + public function get_item( $request ) { $block_name = $request['namespace'] . '/' . $request['name']; $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block_name ); From 4ec1eea6fb99e9e456772a27bbf866be70ff52a9 Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Thu, 7 May 2020 16:50:49 +0100 Subject: [PATCH 03/35] Change comment. Co-authored-by: Andrew Duthie --- lib/class-wp-rest-block-types-controller.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/class-wp-rest-block-types-controller.php b/lib/class-wp-rest-block-types-controller.php index 6fbd7e346eaa05..f26a3db4eff498 100644 --- a/lib/class-wp-rest-block-types-controller.php +++ b/lib/class-wp-rest-block-types-controller.php @@ -277,7 +277,7 @@ public function get_item_schema() { 'readonly' => true, ), 'editor_script' => array( - 'description' => __( 'URL of editor script js file.', 'gutenberg' ), + 'description' => __( 'Editor script handle.', 'gutenberg' ), 'type' => 'string', 'context' => array( 'embed', 'view', 'edit' ), 'readonly' => true, From 81ae460c2364d9aa35dba9843cf5ba31083755dc Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Thu, 7 May 2020 16:52:16 +0100 Subject: [PATCH 04/35] Change comment. Co-authored-by: Andrew Duthie --- lib/class-wp-rest-block-types-controller.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/class-wp-rest-block-types-controller.php b/lib/class-wp-rest-block-types-controller.php index f26a3db4eff498..0f83aba06df978 100644 --- a/lib/class-wp-rest-block-types-controller.php +++ b/lib/class-wp-rest-block-types-controller.php @@ -259,7 +259,7 @@ public function get_item_schema() { 'type' => 'object', 'properties' => array( 'name' => array( - 'description' => __( 'The title for the block type.', 'gutenberg' ), + 'description' => __( 'Unique name identifying the block type.', 'gutenberg' ), 'type' => 'string', 'context' => array( 'embed', 'view', 'edit' ), 'readonly' => true, From 1be9aaad9932993c1a93bf3ec70459b6bc41a290 Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Thu, 7 May 2020 16:53:57 +0100 Subject: [PATCH 05/35] Change title. Co-authored-by: Andrew Duthie --- lib/class-wp-rest-block-types-controller.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/class-wp-rest-block-types-controller.php b/lib/class-wp-rest-block-types-controller.php index 0f83aba06df978..ae5920dff68895 100644 --- a/lib/class-wp-rest-block-types-controller.php +++ b/lib/class-wp-rest-block-types-controller.php @@ -255,7 +255,7 @@ public function get_item_schema() { $schema = array( '$schema' => 'http://json-schema.org/draft-04/schema#', - 'title' => 'block_type', + 'title' => 'block-type', 'type' => 'object', 'properties' => array( 'name' => array( From 371ce8d24357f01bcaf1603186e775c704b2bf71 Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Fri, 8 May 2020 17:41:11 +0100 Subject: [PATCH 06/35] Change comment. Co-authored-by: Andrew Duthie --- lib/class-wp-rest-block-types-controller.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/class-wp-rest-block-types-controller.php b/lib/class-wp-rest-block-types-controller.php index ae5920dff68895..695a16e8d8474b 100644 --- a/lib/class-wp-rest-block-types-controller.php +++ b/lib/class-wp-rest-block-types-controller.php @@ -53,7 +53,7 @@ public function register_routes() { 'type' => 'string', ), 'namespace' => array( - 'description' => __( 'Block namsspace', 'gutenberg' ), + 'description' => __( 'Block namespace', 'gutenberg' ), 'type' => 'string', ), ), From 9afefda1f6abdb847997de1497024bbffef76af6 Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Fri, 8 May 2020 17:48:51 +0100 Subject: [PATCH 07/35] Remove array_shift. Co-authored-by: Andrew Duthie --- lib/class-wp-rest-block-types-controller.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/class-wp-rest-block-types-controller.php b/lib/class-wp-rest-block-types-controller.php index 695a16e8d8474b..41af2980559261 100644 --- a/lib/class-wp-rest-block-types-controller.php +++ b/lib/class-wp-rest-block-types-controller.php @@ -114,7 +114,7 @@ public function get_items( $request ) { if ( $namespace ) { $pieces = explode( '/', $obj->name ); - $block_namespace = array_shift( $pieces ); + $block_namespace = $pieces[0]; if ( $namespace !== $block_namespace ) { continue; } From 348ff88e991023261f4d564d0ba54d6e52f7fc93 Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Fri, 8 May 2020 17:49:23 +0100 Subject: [PATCH 08/35] Change comment to 5.5.0 --- lib/class-wp-rest-block-types-controller.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/class-wp-rest-block-types-controller.php b/lib/class-wp-rest-block-types-controller.php index 41af2980559261..df530ab768f20d 100644 --- a/lib/class-wp-rest-block-types-controller.php +++ b/lib/class-wp-rest-block-types-controller.php @@ -2,7 +2,7 @@ /** * REST API: WP_REST_Block_Types_Controller class * - * @since 5.4.0 + * @since 5.5.0 * @subpackage REST_API * @package WordPress */ From df16a9f0baf5c9fc942aba61e2c62ff6aba8d7a4 Mon Sep 17 00:00:00 2001 From: spacedmonkey Date: Fri, 8 May 2020 17:56:59 +0100 Subject: [PATCH 09/35] Fixes. --- lib/class-wp-rest-block-types-controller.php | 23 +++++--------------- 1 file changed, 5 insertions(+), 18 deletions(-) diff --git a/lib/class-wp-rest-block-types-controller.php b/lib/class-wp-rest-block-types-controller.php index df530ab768f20d..5e61f26c1a23b2 100644 --- a/lib/class-wp-rest-block-types-controller.php +++ b/lib/class-wp-rest-block-types-controller.php @@ -104,14 +104,7 @@ public function get_items( $request ) { } foreach ( $block_types as $slug => $obj ) { - $ret = $this->check_read_permission(); - - if ( ! $ret ) { - continue; - } - $block_type = $this->prepare_item_for_response( $obj, $request ); - if ( $namespace ) { $pieces = explode( '/', $obj->name ); $block_namespace = $pieces[0]; @@ -119,7 +112,6 @@ public function get_items( $request ) { continue; } } - $data[ $obj->name ] = $this->prepare_response_for_collection( $block_type ); } @@ -134,19 +126,16 @@ public function get_items( $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 ) { + $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() ) ); + } $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 ) ); } - $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() ) ); - } - return true; } @@ -173,11 +162,9 @@ protected function check_read_permission() { 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 ); @@ -186,7 +173,7 @@ public function get_item( $request ) { /** * Prepares a block type object for serialization. * - * @param stdClass $block_type block type data. + * @param WP_Block_Type $block_type block type data. * @param WP_REST_Request $request Full details about the request. * * @return WP_REST_Response block type data. From 98b5e99f029ec9f7adc1748462ffd6a8031d2e41 Mon Sep 17 00:00:00 2001 From: spacedmonkey Date: Sat, 9 May 2020 00:07:51 +0100 Subject: [PATCH 10/35] Formatting. --- lib/class-wp-rest-block-types-controller.php | 45 +++++++++++++++----- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/lib/class-wp-rest-block-types-controller.php b/lib/class-wp-rest-block-types-controller.php index 5e61f26c1a23b2..382fbec36a7bbf 100644 --- a/lib/class-wp-rest-block-types-controller.php +++ b/lib/class-wp-rest-block-types-controller.php @@ -14,12 +14,20 @@ */ class WP_REST_Block_Types_Controller extends WP_REST_Controller { + /** + * Instance of WP_Block_Type_Registry. + * + * @var WP_Block_Type_Registry + */ + protected $block_type_registry; + /** * Constructor. */ public function __construct() { - $this->namespace = '__experimental'; - $this->rest_base = 'block-types'; + $this->namespace = '__experimental'; + $this->rest_base = 'block-types'; + $this->block_type_registry = WP_Block_Type_Registry::get_instance(); } /** @@ -94,7 +102,7 @@ public function get_items_permissions_check( $request ) { // phpcs:ignore Variab */ public function get_items( $request ) { $data = array(); - $block_types = WP_Block_Type_Registry::get_instance()->get_all_registered(); + $block_types = $this->block_type_registry->get_all_registered(); // Retrieve the list of registered collection query parameters. $registered = $this->get_collection_params(); @@ -130,10 +138,10 @@ public function get_item_permissions_check( $request ) { if ( ! $check ) { return new WP_Error( 'rest_cannot_read_block_type', __( 'Cannot view block type.', 'gutenberg' ), array( 'status' => rest_authorization_required_code() ) ); } - $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 ) ); + $block_name = sprintf( '%s/%s', $request['namespace'], $request['name'] ); + $block_type = $this->get_block( $block_name ); + if ( is_wp_error( $block_type ) ) { + return $block_type; } return true; @@ -152,6 +160,21 @@ protected function check_read_permission() { return true; } + /** + * Get the block, if the name is valid. + * + * @param string $name Block name + * @return WP_Block_Type|WP_Error Block type object if name is valid, WP_Error otherwise. + */ + protected function get_block( $name ) { + $block_type = $this->block_type_registry->get_registered( $name ); + if ( empty( $block_type ) ) { + return new WP_Error( 'rest_block_type_invalid', __( 'Invalid block type.', 'gutenberg' ), array( 'status' => 404 ) ); + } + + return $block_type; + } + /** * Retrieves a specific block type. * @@ -160,10 +183,10 @@ protected function check_read_permission() { * @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 ) ); + $block_name = sprintf( '%s/%s', $request['namespace'], $request['name'] ); + $block_type = $this->get_block( $block_name ); + if ( is_wp_error( $block_type ) ) { + return $block_type; } $data = $this->prepare_item_for_response( $block_type, $request ); From c83d05ba16cf16dea4c8ffa4926fc8b019ee7d76 Mon Sep 17 00:00:00 2001 From: spacedmonkey Date: Sat, 9 May 2020 02:19:56 +0100 Subject: [PATCH 11/35] More tweaks --- lib/class-wp-rest-block-types-controller.php | 51 ++++++++++++++------ 1 file changed, 35 insertions(+), 16 deletions(-) diff --git a/lib/class-wp-rest-block-types-controller.php b/lib/class-wp-rest-block-types-controller.php index 382fbec36a7bbf..18946e9f810166 100644 --- a/lib/class-wp-rest-block-types-controller.php +++ b/lib/class-wp-rest-block-types-controller.php @@ -19,15 +19,15 @@ class WP_REST_Block_Types_Controller extends WP_REST_Controller { * * @var WP_Block_Type_Registry */ - protected $block_type_registry; + protected $registry; /** * Constructor. */ public function __construct() { - $this->namespace = '__experimental'; - $this->rest_base = 'block-types'; - $this->block_type_registry = WP_Block_Type_Registry::get_instance(); + $this->namespace = '__experimental'; + $this->rest_base = 'block-types'; + $this->registry = WP_Block_Type_Registry::get_instance(); } /** @@ -102,7 +102,7 @@ public function get_items_permissions_check( $request ) { // phpcs:ignore Variab */ public function get_items( $request ) { $data = array(); - $block_types = $this->block_type_registry->get_all_registered(); + $block_types = $this->registry->get_all_registered(); // Retrieve the list of registered collection query parameters. $registered = $this->get_collection_params(); @@ -167,7 +167,7 @@ protected function check_read_permission() { * @return WP_Block_Type|WP_Error Block type object if name is valid, WP_Error otherwise. */ protected function get_block( $name ) { - $block_type = $this->block_type_registry->get_registered( $name ); + $block_type = $this->registry->get_registered( $name ); if ( empty( $block_type ) ) { return new WP_Error( 'rest_block_type_invalid', __( 'Invalid block type.', 'gutenberg' ), array( 'status' => 404 ) ); } @@ -230,16 +230,7 @@ public function prepare_item_for_response( $block_type, $request ) { $response = rest_ensure_response( $data ); - $response->add_links( - array( - 'collection' => array( - 'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ), - ), - 'https://api.w.org/items' => array( - 'href' => rest_url( sprintf( '%s/%s/%s', $this->namespace, $this->rest_base, $block_type->name ) ), - ), - ) - ); + $response->add_links( $this->prepare_links( $block_type ) ); /** * Filters a block type returned from the REST API. @@ -253,6 +244,25 @@ public function prepare_item_for_response( $block_type, $request ) { return apply_filters( 'rest_prepare_block_type', $response, $block_type, $request ); } + /** + * Prepares links for the request. + * + * @param WP_Block_Type $block_type block type data. + * @return array Links for the given block type. + */ + protected function prepare_links( $block_type ) { + $links = array( + 'collection' => array( + 'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ), + ), + 'about' => array( + 'href' => rest_url( sprintf( '%s/%s/%s', $this->namespace, $this->rest_base, $block_type->name ) ), + ), + ); + + return $links; + } + /** * Retrieves the block type' schema, conforming to JSON Schema. * @@ -277,6 +287,15 @@ public function get_item_schema() { 'attributes' => array( 'description' => __( 'Block attributes.', 'gutenberg' ), 'type' => 'object', + 'properties' => array( + 'layout' => array( + 'description' => __( 'Block layout.', 'gutenberg' ), + 'type' => 'string', + 'context' => array( 'view', 'edit' ), + 'readonly' => true, + ), + ), + 'additionalProperties' => true, 'context' => array( 'embed', 'view', 'edit' ), 'readonly' => true, ), From 730acc5dc72cf6ce1be1eda4e165e0436f5aa0a4 Mon Sep 17 00:00:00 2001 From: spacedmonkey Date: Sat, 9 May 2020 02:30:36 +0100 Subject: [PATCH 12/35] Fix lints --- lib/class-wp-rest-block-types-controller.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/class-wp-rest-block-types-controller.php b/lib/class-wp-rest-block-types-controller.php index 18946e9f810166..31408e3a58cf8b 100644 --- a/lib/class-wp-rest-block-types-controller.php +++ b/lib/class-wp-rest-block-types-controller.php @@ -163,7 +163,7 @@ protected function check_read_permission() { /** * Get the block, if the name is valid. * - * @param string $name Block name + * @param string $name Block name. * @return WP_Block_Type|WP_Error Block type object if name is valid, WP_Error otherwise. */ protected function get_block( $name ) { @@ -247,15 +247,15 @@ public function prepare_item_for_response( $block_type, $request ) { /** * Prepares links for the request. * - * @param WP_Block_Type $block_type block type data. + * @param WP_Block_Type $block_type block type data. * @return array Links for the given block type. */ protected function prepare_links( $block_type ) { $links = array( - 'collection' => array( + 'collection' => array( 'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ), ), - 'about' => array( + 'about' => array( 'href' => rest_url( sprintf( '%s/%s/%s', $this->namespace, $this->rest_base, $block_type->name ) ), ), ); @@ -285,8 +285,8 @@ public function get_item_schema() { 'readonly' => true, ), 'attributes' => array( - 'description' => __( 'Block attributes.', 'gutenberg' ), - 'type' => 'object', + 'description' => __( 'Block attributes.', 'gutenberg' ), + 'type' => 'object', 'properties' => array( 'layout' => array( 'description' => __( 'Block layout.', 'gutenberg' ), @@ -296,8 +296,8 @@ public function get_item_schema() { ), ), 'additionalProperties' => true, - 'context' => array( 'embed', 'view', 'edit' ), - 'readonly' => true, + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, ), 'is_dynamic' => array( 'description' => __( 'Is the block dynamically rendered.', 'gutenberg' ), From 3905157a6b9cd342c354f18ee819a6c49c467965 Mon Sep 17 00:00:00 2001 From: spacedmonkey Date: Wed, 13 May 2020 22:54:32 +0100 Subject: [PATCH 13/35] Tweak fields. --- lib/class-wp-rest-block-types-controller.php | 22 ++++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/lib/class-wp-rest-block-types-controller.php b/lib/class-wp-rest-block-types-controller.php index 31408e3a58cf8b..25eeb8af5b477c 100644 --- a/lib/class-wp-rest-block-types-controller.php +++ b/lib/class-wp-rest-block-types-controller.php @@ -205,9 +205,6 @@ public function prepare_item_for_response( $block_type, $request ) { $fields = $this->get_fields_for_response( $request ); $data = array(); - if ( in_array( 'name', $fields, true ) ) { - $data['name'] = $block_type->name; - } if ( in_array( 'attributes', $fields, true ) ) { $data['attributes'] = $block_type->get_attributes(); @@ -217,10 +214,17 @@ public function prepare_item_for_response( $block_type, $request ) { $data['is_dynamic'] = $block_type->is_dynamic(); } - $extra_fields = array( 'editor_script', 'script', 'editor_style', 'style' ); - foreach ( $extra_fields as $extra_field ) { - if ( in_array( $extra_field, $fields, true ) ) { - $data[ $extra_field ] = (string) $block_type->$extra_field; + $schema = $this->get_item_schema(); + $extra_fields = array( + 'name' => 'name', + 'editorScript' => 'editor_script', + 'script' => 'script', + 'editorStyle' => 'editor_style', + 'style' => 'style' + ); + foreach ( $extra_fields as $key => $extra_field ) { + if ( in_array( $key, $fields, true ) ) { + $data[ $key ] = rest_sanitize_value_from_schema( $block_type->$extra_field, $schema['properties'][ $key ] ); } } @@ -305,7 +309,7 @@ public function get_item_schema() { 'context' => array( 'embed', 'view', 'edit' ), 'readonly' => true, ), - 'editor_script' => array( + 'editorScript' => array( 'description' => __( 'Editor script handle.', 'gutenberg' ), 'type' => 'string', 'context' => array( 'embed', 'view', 'edit' ), @@ -317,7 +321,7 @@ public function get_item_schema() { 'context' => array( 'embed', 'view', 'edit' ), 'readonly' => true, ), - 'editor_style' => array( + 'editorStyle' => array( 'description' => __( 'URL of editor style css file.', 'gutenberg' ), 'type' => 'string', 'context' => array( 'embed', 'view', 'edit' ), From 8f32a723e817486be8bcc390605458feee101b60 Mon Sep 17 00:00:00 2001 From: spacedmonkey Date: Thu, 14 May 2020 19:04:36 +0100 Subject: [PATCH 14/35] Feedback. --- lib/class-wp-rest-block-types-controller.php | 32 +++++++++++++------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/lib/class-wp-rest-block-types-controller.php b/lib/class-wp-rest-block-types-controller.php index 25eeb8af5b477c..6da147d1414576 100644 --- a/lib/class-wp-rest-block-types-controller.php +++ b/lib/class-wp-rest-block-types-controller.php @@ -216,15 +216,16 @@ public function prepare_item_for_response( $block_type, $request ) { $schema = $this->get_item_schema(); $extra_fields = array( - 'name' => 'name', - 'editorScript' => 'editor_script', - 'script' => 'script', - 'editorStyle' => 'editor_style', - 'style' => 'style' + 'name', + 'editor_script', + 'script', + 'editor_style', + 'style', ); - foreach ( $extra_fields as $key => $extra_field ) { - if ( in_array( $key, $fields, true ) ) { - $data[ $key ] = rest_sanitize_value_from_schema( $block_type->$extra_field, $schema['properties'][ $key ] ); + foreach ( $extra_fields as $extra_field ) { + if ( in_array( $extra_field, $fields, true ) ) { + $field = ( isset( $block_type->$extra_field ) ) ? $block_type->$extra_field : false; + $data[ $extra_field ] = rest_sanitize_value_from_schema( $field, $schema['properties'][ $extra_field ] ); } } @@ -255,15 +256,24 @@ public function prepare_item_for_response( $block_type, $request ) { * @return array Links for the given block type. */ protected function prepare_links( $block_type ) { - $links = array( + $pieces = explode( '/', $block_type->name ); + $namespace = $pieces[0]; + $links = array( 'collection' => array( 'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ), ), 'about' => array( 'href' => rest_url( sprintf( '%s/%s/%s', $this->namespace, $this->rest_base, $block_type->name ) ), ), + 'namespace' => array( + 'href' => add_query_arg( 'namespace', $namespace, rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ) ), + ), ); + if ( $block_type->is_dynamic() ) { + $links['render']['href'] = add_query_arg( 'context', 'edit', rest_url( sprintf( '%s/%s/%s', 'wp/v2', 'block-renderer', $block_type->name ) ) ); + } + return $links; } @@ -309,7 +319,7 @@ public function get_item_schema() { 'context' => array( 'embed', 'view', 'edit' ), 'readonly' => true, ), - 'editorScript' => array( + 'editor_script' => array( 'description' => __( 'Editor script handle.', 'gutenberg' ), 'type' => 'string', 'context' => array( 'embed', 'view', 'edit' ), @@ -321,7 +331,7 @@ public function get_item_schema() { 'context' => array( 'embed', 'view', 'edit' ), 'readonly' => true, ), - 'editorStyle' => array( + 'editor_style' => array( 'description' => __( 'URL of editor style css file.', 'gutenberg' ), 'type' => 'string', 'context' => array( 'embed', 'view', 'edit' ), From 3479ffc766b51ede4a07e30ed1bd9bd53eb42254 Mon Sep 17 00:00:00 2001 From: spacedmonkey Date: Wed, 20 May 2020 23:04:27 +0100 Subject: [PATCH 15/35] Add block category. --- composer.lock | 13 ++++++++++++- lib/class-wp-rest-block-types-controller.php | 7 +++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/composer.lock b/composer.lock index 3ac63fed05df15..907437ab85f001 100644 --- a/composer.lock +++ b/composer.lock @@ -131,6 +131,16 @@ "zend", "zikula" ], + "funding": [ + { + "url": "https://packagist.com", + "type": "custom" + }, + { + "url": "https://tidelift.com/funding/github/packagist/composer/composer", + "type": "tidelift" + } + ], "time": "2020-04-07T06:57:05+00:00" } ], @@ -410,5 +420,6 @@ "prefer-stable": false, "prefer-lowest": false, "platform": [], - "platform-dev": [] + "platform-dev": [], + "plugin-api-version": "1.1.0" } diff --git a/lib/class-wp-rest-block-types-controller.php b/lib/class-wp-rest-block-types-controller.php index 6da147d1414576..5ec6b8dc7614aa 100644 --- a/lib/class-wp-rest-block-types-controller.php +++ b/lib/class-wp-rest-block-types-controller.php @@ -217,6 +217,7 @@ public function prepare_item_for_response( $block_type, $request ) { $schema = $this->get_item_schema(); $extra_fields = array( 'name', + 'category', 'editor_script', 'script', 'editor_style', @@ -313,6 +314,12 @@ public function get_item_schema() { 'context' => array( 'embed', 'view', 'edit' ), 'readonly' => true, ), + 'category' => array( + 'description' => __( 'Block category.', 'gutenberg' ), + 'type' => 'string', + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), 'is_dynamic' => array( 'description' => __( 'Is the block dynamically rendered.', 'gutenberg' ), 'type' => 'boolean', From acf6cbe706eb3d4a93951a61612e128cdd567015 Mon Sep 17 00:00:00 2001 From: spacedmonkey Date: Thu, 21 May 2020 09:22:15 +0100 Subject: [PATCH 16/35] Feedback --- composer.lock | 425 ------------------- lib/class-wp-rest-block-types-controller.php | 57 +-- 2 files changed, 33 insertions(+), 449 deletions(-) delete mode 100644 composer.lock diff --git a/composer.lock b/composer.lock deleted file mode 100644 index 907437ab85f001..00000000000000 --- a/composer.lock +++ /dev/null @@ -1,425 +0,0 @@ -{ - "_readme": [ - "This file locks the dependencies of your project to a known state", - "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", - "This file is @generated automatically" - ], - "content-hash": "9745a2b1c8d1005bf9f7617061e4fc7d", - "packages": [ - { - "name": "composer/installers", - "version": "v1.9.0", - "source": { - "type": "git", - "url": "https://github.com/composer/installers.git", - "reference": "b93bcf0fa1fccb0b7d176b0967d969691cd74cca" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/composer/installers/zipball/b93bcf0fa1fccb0b7d176b0967d969691cd74cca", - "reference": "b93bcf0fa1fccb0b7d176b0967d969691cd74cca", - "shasum": "" - }, - "require": { - "composer-plugin-api": "^1.0 || ^2.0" - }, - "replace": { - "roundcube/plugin-installer": "*", - "shama/baton": "*" - }, - "require-dev": { - "composer/composer": "1.6.* || 2.0.*@dev", - "composer/semver": "1.0.* || 2.0.*@dev", - "phpunit/phpunit": "^4.8.36", - "sebastian/comparator": "^1.2.4", - "symfony/process": "^2.3" - }, - "type": "composer-plugin", - "extra": { - "class": "Composer\\Installers\\Plugin", - "branch-alias": { - "dev-master": "1.0-dev" - } - }, - "autoload": { - "psr-4": { - "Composer\\Installers\\": "src/Composer/Installers" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Kyle Robinson Young", - "email": "kyle@dontkry.com", - "homepage": "https://github.com/shama" - } - ], - "description": "A multi-framework Composer library installer", - "homepage": "https://composer.github.io/installers/", - "keywords": [ - "Craft", - "Dolibarr", - "Eliasis", - "Hurad", - "ImageCMS", - "Kanboard", - "Lan Management System", - "MODX Evo", - "MantisBT", - "Mautic", - "Maya", - "OXID", - "Plentymarkets", - "Porto", - "RadPHP", - "SMF", - "Thelia", - "Whmcs", - "WolfCMS", - "agl", - "aimeos", - "annotatecms", - "attogram", - "bitrix", - "cakephp", - "chef", - "cockpit", - "codeigniter", - "concrete5", - "croogo", - "dokuwiki", - "drupal", - "eZ Platform", - "elgg", - "expressionengine", - "fuelphp", - "grav", - "installer", - "itop", - "joomla", - "known", - "kohana", - "laravel", - "lavalite", - "lithium", - "magento", - "majima", - "mako", - "mediawiki", - "modulework", - "modx", - "moodle", - "osclass", - "phpbb", - "piwik", - "ppi", - "puppet", - "pxcms", - "reindex", - "roundcube", - "shopware", - "silverstripe", - "sydes", - "sylius", - "symfony", - "typo3", - "wordpress", - "yawik", - "zend", - "zikula" - ], - "funding": [ - { - "url": "https://packagist.com", - "type": "custom" - }, - { - "url": "https://tidelift.com/funding/github/packagist/composer/composer", - "type": "tidelift" - } - ], - "time": "2020-04-07T06:57:05+00:00" - } - ], - "packages-dev": [ - { - "name": "dealerdirect/phpcodesniffer-composer-installer", - "version": "v0.6.2", - "source": { - "type": "git", - "url": "https://github.com/Dealerdirect/phpcodesniffer-composer-installer.git", - "reference": "8001af8eb107fbfcedc31a8b51e20b07d85b457a" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/Dealerdirect/phpcodesniffer-composer-installer/zipball/8001af8eb107fbfcedc31a8b51e20b07d85b457a", - "reference": "8001af8eb107fbfcedc31a8b51e20b07d85b457a", - "shasum": "" - }, - "require": { - "composer-plugin-api": "^1.0", - "php": "^5.3|^7", - "squizlabs/php_codesniffer": "^2|^3" - }, - "require-dev": { - "composer/composer": "*", - "phpcompatibility/php-compatibility": "^9.0", - "sensiolabs/security-checker": "^4.1.0" - }, - "type": "composer-plugin", - "extra": { - "class": "Dealerdirect\\Composer\\Plugin\\Installers\\PHPCodeSniffer\\Plugin" - }, - "autoload": { - "psr-4": { - "Dealerdirect\\Composer\\Plugin\\Installers\\PHPCodeSniffer\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Franck Nijhof", - "email": "franck.nijhof@dealerdirect.com", - "homepage": "http://www.frenck.nl", - "role": "Developer / IT Manager" - } - ], - "description": "PHP_CodeSniffer Standards Composer Installer Plugin", - "homepage": "http://www.dealerdirect.com", - "keywords": [ - "PHPCodeSniffer", - "PHP_CodeSniffer", - "code quality", - "codesniffer", - "composer", - "installer", - "phpcs", - "plugin", - "qa", - "quality", - "standard", - "standards", - "style guide", - "stylecheck", - "tests" - ], - "time": "2020-01-29T20:22:20+00:00" - }, - { - "name": "phpcompatibility/php-compatibility", - "version": "9.3.5", - "source": { - "type": "git", - "url": "https://github.com/PHPCompatibility/PHPCompatibility.git", - "reference": "9fb324479acf6f39452e0655d2429cc0d3914243" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/PHPCompatibility/PHPCompatibility/zipball/9fb324479acf6f39452e0655d2429cc0d3914243", - "reference": "9fb324479acf6f39452e0655d2429cc0d3914243", - "shasum": "" - }, - "require": { - "php": ">=5.3", - "squizlabs/php_codesniffer": "^2.3 || ^3.0.2" - }, - "conflict": { - "squizlabs/php_codesniffer": "2.6.2" - }, - "require-dev": { - "phpunit/phpunit": "~4.5 || ^5.0 || ^6.0 || ^7.0" - }, - "suggest": { - "dealerdirect/phpcodesniffer-composer-installer": "^0.5 || This Composer plugin will sort out the PHPCS 'installed_paths' automatically.", - "roave/security-advisories": "dev-master || Helps prevent installing dependencies with known security issues." - }, - "type": "phpcodesniffer-standard", - "notification-url": "https://packagist.org/downloads/", - "license": [ - "LGPL-3.0-or-later" - ], - "authors": [ - { - "name": "Wim Godden", - "homepage": "https://github.com/wimg", - "role": "lead" - }, - { - "name": "Juliette Reinders Folmer", - "homepage": "https://github.com/jrfnl", - "role": "lead" - }, - { - "name": "Contributors", - "homepage": "https://github.com/PHPCompatibility/PHPCompatibility/graphs/contributors" - } - ], - "description": "A set of sniffs for PHP_CodeSniffer that checks for PHP cross-version compatibility.", - "homepage": "http://techblog.wimgodden.be/tag/codesniffer/", - "keywords": [ - "compatibility", - "phpcs", - "standards" - ], - "time": "2019-12-27T09:44:58+00:00" - }, - { - "name": "sirbrillig/phpcs-variable-analysis", - "version": "v2.8.1", - "source": { - "type": "git", - "url": "https://github.com/sirbrillig/phpcs-variable-analysis.git", - "reference": "5be26b4d719acaf7a433d1cad469159cbf034f2a" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sirbrillig/phpcs-variable-analysis/zipball/5be26b4d719acaf7a433d1cad469159cbf034f2a", - "reference": "5be26b4d719acaf7a433d1cad469159cbf034f2a", - "shasum": "" - }, - "require": { - "php": ">=5.6.0", - "squizlabs/php_codesniffer": "^3.1" - }, - "require-dev": { - "dealerdirect/phpcodesniffer-composer-installer": "^0.4.4 || ^0.5 || ^0.6", - "limedeck/phpunit-detailed-printer": "^3.1", - "phpstan/phpstan": "^0.11.8", - "phpunit/phpunit": "^5.0 || ^6.5", - "sirbrillig/phpcs-import-detection": "^1.1" - }, - "type": "phpcodesniffer-standard", - "autoload": { - "psr-4": { - "VariableAnalysis\\": "VariableAnalysis/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-2-Clause" - ], - "authors": [ - { - "name": "Sam Graham", - "email": "php-codesniffer-variableanalysis@illusori.co.uk" - }, - { - "name": "Payton Swick", - "email": "payton@foolord.com" - } - ], - "description": "A PHPCS sniff to detect problems with variables.", - "time": "2020-02-11T22:18:48+00:00" - }, - { - "name": "squizlabs/php_codesniffer", - "version": "3.5.5", - "source": { - "type": "git", - "url": "https://github.com/squizlabs/PHP_CodeSniffer.git", - "reference": "73e2e7f57d958e7228fce50dc0c61f58f017f9f6" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/73e2e7f57d958e7228fce50dc0c61f58f017f9f6", - "reference": "73e2e7f57d958e7228fce50dc0c61f58f017f9f6", - "shasum": "" - }, - "require": { - "ext-simplexml": "*", - "ext-tokenizer": "*", - "ext-xmlwriter": "*", - "php": ">=5.4.0" - }, - "require-dev": { - "phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0" - }, - "bin": [ - "bin/phpcs", - "bin/phpcbf" - ], - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "3.x-dev" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Greg Sherwood", - "role": "lead" - } - ], - "description": "PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.", - "homepage": "https://github.com/squizlabs/PHP_CodeSniffer", - "keywords": [ - "phpcs", - "standards" - ], - "time": "2020-04-17T01:09:41+00:00" - }, - { - "name": "wp-coding-standards/wpcs", - "version": "2.2.1", - "source": { - "type": "git", - "url": "https://github.com/WordPress/WordPress-Coding-Standards.git", - "reference": "b5a453203114cc2284b1a614c4953456fbe4f546" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/WordPress/WordPress-Coding-Standards/zipball/b5a453203114cc2284b1a614c4953456fbe4f546", - "reference": "b5a453203114cc2284b1a614c4953456fbe4f546", - "shasum": "" - }, - "require": { - "php": ">=5.4", - "squizlabs/php_codesniffer": "^3.3.1" - }, - "require-dev": { - "dealerdirect/phpcodesniffer-composer-installer": "^0.5 || ^0.6", - "phpcompatibility/php-compatibility": "^9.0", - "phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0" - }, - "suggest": { - "dealerdirect/phpcodesniffer-composer-installer": "^0.6 || This Composer plugin will sort out the PHPCS 'installed_paths' automatically." - }, - "type": "phpcodesniffer-standard", - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Contributors", - "homepage": "https://github.com/WordPress/WordPress-Coding-Standards/graphs/contributors" - } - ], - "description": "PHP_CodeSniffer rules (sniffs) to enforce WordPress coding conventions", - "keywords": [ - "phpcs", - "standards", - "wordpress" - ], - "time": "2020-02-04T02:52:06+00:00" - } - ], - "aliases": [], - "minimum-stability": "stable", - "stability-flags": [], - "prefer-stable": false, - "prefer-lowest": false, - "platform": [], - "platform-dev": [], - "plugin-api-version": "1.1.0" -} diff --git a/lib/class-wp-rest-block-types-controller.php b/lib/class-wp-rest-block-types-controller.php index 5ec6b8dc7614aa..42d6f65fb33242 100644 --- a/lib/class-wp-rest-block-types-controller.php +++ b/lib/class-wp-rest-block-types-controller.php @@ -51,6 +51,20 @@ public function register_routes() { ) ); + register_rest_route( + $this->namespace, + '/' . $this->rest_base . '/(?P[a-zA-Z0-9_-]+)', + 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' ), + ) + ); + register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P[a-zA-Z0-9_-]+)/(?P[a-zA-Z0-9_-]+)', @@ -87,7 +101,7 @@ public function register_routes() { */ public function get_items_permissions_check( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable 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() ) ); + return new WP_Error( 'rest_block_type_cannot_view', __( 'Sorry, you are not allowed to manage block types.', 'gutenberg' ), array( 'status' => rest_authorization_required_code() ) ); } return true; @@ -112,7 +126,6 @@ public function get_items( $request ) { } foreach ( $block_types as $slug => $obj ) { - $block_type = $this->prepare_item_for_response( $obj, $request ); if ( $namespace ) { $pieces = explode( '/', $obj->name ); $block_namespace = $pieces[0]; @@ -120,7 +133,8 @@ public function get_items( $request ) { continue; } } - $data[ $obj->name ] = $this->prepare_response_for_collection( $block_type ); + $block_type = $this->prepare_item_for_response( $obj, $request ); + $data[] = $this->prepare_response_for_collection( $block_type ); } return rest_ensure_response( $data ); @@ -136,7 +150,7 @@ public function get_items( $request ) { public function get_item_permissions_check( $request ) { $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() ) ); + return new WP_Error( 'rest_block_type_cannot_view', __( 'Cannot view block type.', 'gutenberg' ), array( 'status' => rest_authorization_required_code() ) ); } $block_name = sprintf( '%s/%s', $request['namespace'], $request['name'] ); $block_type = $this->get_block( $block_name ); @@ -154,7 +168,7 @@ public function get_item_permissions_check( $request ) { */ 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() ) ); + return new WP_Error( 'rest_block_type_cannot_view', __( 'Sorry, you are not allowed to manage block types.', 'gutenberg' ), array( 'status' => rest_authorization_required_code() ) ); } return true; @@ -206,11 +220,11 @@ public function prepare_item_for_response( $block_type, $request ) { $fields = $this->get_fields_for_response( $request ); $data = array(); - if ( in_array( 'attributes', $fields, true ) ) { + if ( rest_is_field_included( 'attributes', $fields ) ) { $data['attributes'] = $block_type->get_attributes(); } - if ( in_array( 'is_dynamic', $fields, true ) ) { + if ( rest_is_field_included( 'is_dynamic', $fields ) ) { $data['is_dynamic'] = $block_type->is_dynamic(); } @@ -224,8 +238,8 @@ public function prepare_item_for_response( $block_type, $request ) { 'style', ); foreach ( $extra_fields as $extra_field ) { - if ( in_array( $extra_field, $fields, true ) ) { - $field = ( isset( $block_type->$extra_field ) ) ? $block_type->$extra_field : false; + if ( rest_is_field_included( $extra_field, $fields ) ) { + $field = isset( $block_type->$extra_field ) ? $block_type->$extra_field : ''; $data[ $extra_field ] = rest_sanitize_value_from_schema( $field, $schema['properties'][ $extra_field ] ); } } @@ -263,16 +277,16 @@ protected function prepare_links( $block_type ) { 'collection' => array( 'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ), ), - 'about' => array( + 'self' => array( 'href' => rest_url( sprintf( '%s/%s/%s', $this->namespace, $this->rest_base, $block_type->name ) ), ), - 'namespace' => array( - 'href' => add_query_arg( 'namespace', $namespace, rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ) ), + 'up' => array( + 'href' => rest_url( sprintf( '%s/%s/%s', $this->namespace, $this->rest_base, $namespace ) ), ), ); if ( $block_type->is_dynamic() ) { - $links['render']['href'] = add_query_arg( 'context', 'edit', rest_url( sprintf( '%s/%s/%s', 'wp/v2', 'block-renderer', $block_type->name ) ) ); + $links['https://api.w.org/render-block']['href'] = add_query_arg( 'context', 'edit', rest_url( sprintf( '%s/%s/%s', 'wp/v2', 'block-renderer', $block_type->name ) ) ); } return $links; @@ -302,15 +316,10 @@ public function get_item_schema() { 'attributes' => array( 'description' => __( 'Block attributes.', 'gutenberg' ), 'type' => 'object', - 'properties' => array( - 'layout' => array( - 'description' => __( 'Block layout.', 'gutenberg' ), - 'type' => 'string', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - ), + 'properties' => array(), + 'additionalProperties' => array( + 'type' => 'object', ), - 'additionalProperties' => true, 'context' => array( 'embed', 'view', 'edit' ), 'readonly' => true, ), @@ -333,19 +342,19 @@ public function get_item_schema() { 'readonly' => true, ), 'script' => array( - 'description' => __( 'URL of script js file.', 'gutenberg' ), + 'description' => __( 'Name of script js file.', 'gutenberg' ), 'type' => 'string', 'context' => array( 'embed', 'view', 'edit' ), 'readonly' => true, ), 'editor_style' => array( - 'description' => __( 'URL of editor style css file.', 'gutenberg' ), + 'description' => __( 'Name of editor style css file.', 'gutenberg' ), 'type' => 'string', 'context' => array( 'embed', 'view', 'edit' ), 'readonly' => true, ), 'style' => array( - 'description' => __( 'URL of style css file.', 'gutenberg' ), + 'description' => __( 'Name of style css file.', 'gutenberg' ), 'type' => 'string', 'context' => array( 'embed', 'view', 'edit' ), 'readonly' => true, From bfc2e07aefd65d69929e4e25e189c5cc277f1c96 Mon Sep 17 00:00:00 2001 From: spacedmonkey Date: Thu, 21 May 2020 09:25:13 +0100 Subject: [PATCH 17/35] Add back composer.lock. --- composer.lock | 457 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 457 insertions(+) create mode 100644 composer.lock diff --git a/composer.lock b/composer.lock new file mode 100644 index 00000000000000..ffaa293f16ce12 --- /dev/null +++ b/composer.lock @@ -0,0 +1,457 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", + "This file is @generated automatically" + ], + "content-hash": "bcdce838e8354d08b1ed65eb1ebf3636", + "packages": [ + { + "name": "composer/installers", + "version": "v1.9.0", + "source": { + "type": "git", + "url": "https://github.com/composer/installers.git", + "reference": "b93bcf0fa1fccb0b7d176b0967d969691cd74cca" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/composer/installers/zipball/b93bcf0fa1fccb0b7d176b0967d969691cd74cca", + "reference": "b93bcf0fa1fccb0b7d176b0967d969691cd74cca", + "shasum": "" + }, + "require": { + "composer-plugin-api": "^1.0 || ^2.0" + }, + "replace": { + "roundcube/plugin-installer": "*", + "shama/baton": "*" + }, + "require-dev": { + "composer/composer": "1.6.* || 2.0.*@dev", + "composer/semver": "1.0.* || 2.0.*@dev", + "phpunit/phpunit": "^4.8.36", + "sebastian/comparator": "^1.2.4", + "symfony/process": "^2.3" + }, + "type": "composer-plugin", + "extra": { + "class": "Composer\\Installers\\Plugin", + "branch-alias": { + "dev-master": "1.0-dev" + } + }, + "autoload": { + "psr-4": { + "Composer\\Installers\\": "src/Composer/Installers" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Kyle Robinson Young", + "email": "kyle@dontkry.com", + "homepage": "https://github.com/shama" + } + ], + "description": "A multi-framework Composer library installer", + "homepage": "https://composer.github.io/installers/", + "keywords": [ + "Craft", + "Dolibarr", + "Eliasis", + "Hurad", + "ImageCMS", + "Kanboard", + "Lan Management System", + "MODX Evo", + "MantisBT", + "Mautic", + "Maya", + "OXID", + "Plentymarkets", + "Porto", + "RadPHP", + "SMF", + "Thelia", + "Whmcs", + "WolfCMS", + "agl", + "aimeos", + "annotatecms", + "attogram", + "bitrix", + "cakephp", + "chef", + "cockpit", + "codeigniter", + "concrete5", + "croogo", + "dokuwiki", + "drupal", + "eZ Platform", + "elgg", + "expressionengine", + "fuelphp", + "grav", + "installer", + "itop", + "joomla", + "known", + "kohana", + "laravel", + "lavalite", + "lithium", + "magento", + "majima", + "mako", + "mediawiki", + "modulework", + "modx", + "moodle", + "osclass", + "phpbb", + "piwik", + "ppi", + "puppet", + "pxcms", + "reindex", + "roundcube", + "shopware", + "silverstripe", + "sydes", + "sylius", + "symfony", + "typo3", + "wordpress", + "yawik", + "zend", + "zikula" + ], + "time": "2020-04-07T06:57:05+00:00" + } + ], + "packages-dev": [ + { + "name": "dealerdirect/phpcodesniffer-composer-installer", + "version": "v0.6.2", + "source": { + "type": "git", + "url": "https://github.com/Dealerdirect/phpcodesniffer-composer-installer.git", + "reference": "8001af8eb107fbfcedc31a8b51e20b07d85b457a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Dealerdirect/phpcodesniffer-composer-installer/zipball/8001af8eb107fbfcedc31a8b51e20b07d85b457a", + "reference": "8001af8eb107fbfcedc31a8b51e20b07d85b457a", + "shasum": "" + }, + "require": { + "composer-plugin-api": "^1.0", + "php": "^5.3|^7", + "squizlabs/php_codesniffer": "^2|^3" + }, + "require-dev": { + "composer/composer": "*", + "phpcompatibility/php-compatibility": "^9.0", + "sensiolabs/security-checker": "^4.1.0" + }, + "type": "composer-plugin", + "extra": { + "class": "Dealerdirect\\Composer\\Plugin\\Installers\\PHPCodeSniffer\\Plugin" + }, + "autoload": { + "psr-4": { + "Dealerdirect\\Composer\\Plugin\\Installers\\PHPCodeSniffer\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Franck Nijhof", + "email": "franck.nijhof@dealerdirect.com", + "homepage": "http://www.frenck.nl", + "role": "Developer / IT Manager" + } + ], + "description": "PHP_CodeSniffer Standards Composer Installer Plugin", + "homepage": "http://www.dealerdirect.com", + "keywords": [ + "PHPCodeSniffer", + "PHP_CodeSniffer", + "code quality", + "codesniffer", + "composer", + "installer", + "phpcs", + "plugin", + "qa", + "quality", + "standard", + "standards", + "style guide", + "stylecheck", + "tests" + ], + "time": "2020-01-29T20:22:20+00:00" + }, + { + "name": "phpcompatibility/php-compatibility", + "version": "9.3.5", + "source": { + "type": "git", + "url": "https://github.com/PHPCompatibility/PHPCompatibility.git", + "reference": "9fb324479acf6f39452e0655d2429cc0d3914243" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/PHPCompatibility/PHPCompatibility/zipball/9fb324479acf6f39452e0655d2429cc0d3914243", + "reference": "9fb324479acf6f39452e0655d2429cc0d3914243", + "shasum": "" + }, + "require": { + "php": ">=5.3", + "squizlabs/php_codesniffer": "^2.3 || ^3.0.2" + }, + "conflict": { + "squizlabs/php_codesniffer": "2.6.2" + }, + "require-dev": { + "phpunit/phpunit": "~4.5 || ^5.0 || ^6.0 || ^7.0" + }, + "suggest": { + "dealerdirect/phpcodesniffer-composer-installer": "^0.5 || This Composer plugin will sort out the PHPCS 'installed_paths' automatically.", + "roave/security-advisories": "dev-master || Helps prevent installing dependencies with known security issues." + }, + "type": "phpcodesniffer-standard", + "notification-url": "https://packagist.org/downloads/", + "license": [ + "LGPL-3.0-or-later" + ], + "authors": [ + { + "name": "Wim Godden", + "homepage": "https://github.com/wimg", + "role": "lead" + }, + { + "name": "Juliette Reinders Folmer", + "homepage": "https://github.com/jrfnl", + "role": "lead" + }, + { + "name": "Contributors", + "homepage": "https://github.com/PHPCompatibility/PHPCompatibility/graphs/contributors" + } + ], + "description": "A set of sniffs for PHP_CodeSniffer that checks for PHP cross-version compatibility.", + "homepage": "http://techblog.wimgodden.be/tag/codesniffer/", + "keywords": [ + "compatibility", + "phpcs", + "standards" + ], + "time": "2019-12-27T09:44:58+00:00" + }, + { + "name": "sirbrillig/phpcs-variable-analysis", + "version": "v2.8.1", + "source": { + "type": "git", + "url": "https://github.com/sirbrillig/phpcs-variable-analysis.git", + "reference": "5be26b4d719acaf7a433d1cad469159cbf034f2a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sirbrillig/phpcs-variable-analysis/zipball/5be26b4d719acaf7a433d1cad469159cbf034f2a", + "reference": "5be26b4d719acaf7a433d1cad469159cbf034f2a", + "shasum": "" + }, + "require": { + "php": ">=5.6.0", + "squizlabs/php_codesniffer": "^3.1" + }, + "require-dev": { + "dealerdirect/phpcodesniffer-composer-installer": "^0.4.4 || ^0.5 || ^0.6", + "limedeck/phpunit-detailed-printer": "^3.1", + "phpstan/phpstan": "^0.11.8", + "phpunit/phpunit": "^5.0 || ^6.5", + "sirbrillig/phpcs-import-detection": "^1.1" + }, + "type": "phpcodesniffer-standard", + "autoload": { + "psr-4": { + "VariableAnalysis\\": "VariableAnalysis/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-2-Clause" + ], + "authors": [ + { + "name": "Sam Graham", + "email": "php-codesniffer-variableanalysis@illusori.co.uk" + }, + { + "name": "Payton Swick", + "email": "payton@foolord.com" + } + ], + "description": "A PHPCS sniff to detect problems with variables.", + "time": "2020-02-11T22:18:48+00:00" + }, + { + "name": "squizlabs/php_codesniffer", + "version": "3.5.5", + "source": { + "type": "git", + "url": "https://github.com/squizlabs/PHP_CodeSniffer.git", + "reference": "73e2e7f57d958e7228fce50dc0c61f58f017f9f6" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/73e2e7f57d958e7228fce50dc0c61f58f017f9f6", + "reference": "73e2e7f57d958e7228fce50dc0c61f58f017f9f6", + "shasum": "" + }, + "require": { + "ext-simplexml": "*", + "ext-tokenizer": "*", + "ext-xmlwriter": "*", + "php": ">=5.4.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0" + }, + "bin": [ + "bin/phpcs", + "bin/phpcbf" + ], + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.x-dev" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Greg Sherwood", + "role": "lead" + } + ], + "description": "PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.", + "homepage": "https://github.com/squizlabs/PHP_CodeSniffer", + "keywords": [ + "phpcs", + "standards" + ], + "time": "2020-04-17T01:09:41+00:00" + }, + { + "name": "wp-coding-standards/wpcs", + "version": "2.2.1", + "source": { + "type": "git", + "url": "https://github.com/WordPress/WordPress-Coding-Standards.git", + "reference": "b5a453203114cc2284b1a614c4953456fbe4f546" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/WordPress/WordPress-Coding-Standards/zipball/b5a453203114cc2284b1a614c4953456fbe4f546", + "reference": "b5a453203114cc2284b1a614c4953456fbe4f546", + "shasum": "" + }, + "require": { + "php": ">=5.4", + "squizlabs/php_codesniffer": "^3.3.1" + }, + "require-dev": { + "dealerdirect/phpcodesniffer-composer-installer": "^0.5 || ^0.6", + "phpcompatibility/php-compatibility": "^9.0", + "phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0" + }, + "suggest": { + "dealerdirect/phpcodesniffer-composer-installer": "^0.6 || This Composer plugin will sort out the PHPCS 'installed_paths' automatically." + }, + "type": "phpcodesniffer-standard", + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Contributors", + "homepage": "https://github.com/WordPress/WordPress-Coding-Standards/graphs/contributors" + } + ], + "description": "PHP_CodeSniffer rules (sniffs) to enforce WordPress coding conventions", + "keywords": [ + "phpcs", + "standards", + "wordpress" + ], + "time": "2020-02-04T02:52:06+00:00" + }, + { + "name": "wp-phpunit/wp-phpunit", + "version": "5.4.0", + "source": { + "type": "git", + "url": "https://github.com/wp-phpunit/wp-phpunit.git", + "reference": "15925e03c8594c08cf2822e24e511af8dc6f779e" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/wp-phpunit/wp-phpunit/zipball/15925e03c8594c08cf2822e24e511af8dc6f779e", + "reference": "15925e03c8594c08cf2822e24e511af8dc6f779e", + "shasum": "" + }, + "type": "library", + "autoload": { + "files": [ + "__loaded.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "GPL-2.0-or-later" + ], + "authors": [ + { + "name": "Evan Mattson", + "email": "me@aaemnnost.tv" + }, + { + "name": "WordPress Community", + "homepage": "https://wordpress.org/about/" + } + ], + "description": "WordPress core PHPUnit library", + "homepage": "https://github.com/wp-phpunit", + "keywords": [ + "phpunit", + "test", + "wordpress" + ], + "time": "2020-04-01T14:35:27+00:00" + } + ], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": [], + "platform-dev": [] +} From ce8ea799cbfcdf64151aacec495e69142e4098e0 Mon Sep 17 00:00:00 2001 From: spacedmonkey Date: Thu, 21 May 2020 09:35:04 +0100 Subject: [PATCH 18/35] Tweak descriptions. --- lib/class-wp-rest-block-types-controller.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/class-wp-rest-block-types-controller.php b/lib/class-wp-rest-block-types-controller.php index 42d6f65fb33242..2a545ea799abd7 100644 --- a/lib/class-wp-rest-block-types-controller.php +++ b/lib/class-wp-rest-block-types-controller.php @@ -342,19 +342,19 @@ public function get_item_schema() { 'readonly' => true, ), 'script' => array( - 'description' => __( 'Name of script js file.', 'gutenberg' ), + 'description' => __( 'Public facing script handle.', 'gutenberg' ), 'type' => 'string', 'context' => array( 'embed', 'view', 'edit' ), 'readonly' => true, ), 'editor_style' => array( - 'description' => __( 'Name of editor style css file.', 'gutenberg' ), + 'description' => __( 'Editor style handle.', 'gutenberg' ), 'type' => 'string', 'context' => array( 'embed', 'view', 'edit' ), 'readonly' => true, ), 'style' => array( - 'description' => __( 'Name of style css file.', 'gutenberg' ), + 'description' => __( 'Public facing style handle.', 'gutenberg' ), 'type' => 'string', 'context' => array( 'embed', 'view', 'edit' ), 'readonly' => true, From e75b647cf328cb270f272c07d0eaf9458e6e6723 Mon Sep 17 00:00:00 2001 From: spacedmonkey Date: Thu, 21 May 2020 10:26:23 +0100 Subject: [PATCH 19/35] Add supports. --- lib/class-wp-rest-block-types-controller.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/class-wp-rest-block-types-controller.php b/lib/class-wp-rest-block-types-controller.php index 2a545ea799abd7..aadafbf01e1a6e 100644 --- a/lib/class-wp-rest-block-types-controller.php +++ b/lib/class-wp-rest-block-types-controller.php @@ -236,6 +236,7 @@ public function prepare_item_for_response( $block_type, $request ) { 'script', 'editor_style', 'style', + 'supports', ); foreach ( $extra_fields as $extra_field ) { if ( rest_is_field_included( $extra_field, $fields ) ) { @@ -323,6 +324,13 @@ public function get_item_schema() { 'context' => array( 'embed', 'view', 'edit' ), 'readonly' => true, ), + 'supports' => array( + 'description' => __( 'Block supports.', 'gutenberg' ), + 'type' => 'object', + 'properties' => array(), + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), 'category' => array( 'description' => __( 'Block category.', 'gutenberg' ), 'type' => 'string', From 6c2b6a6f9e90c53fb5dcd3cd4215c43074fcd54e Mon Sep 17 00:00:00 2001 From: spacedmonkey Date: Fri, 22 May 2020 00:22:26 +0100 Subject: [PATCH 20/35] Add unit tests. --- ...ss-wp-rest-block-types-controller-test.php | 150 ++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 phpunit/class-wp-rest-block-types-controller-test.php diff --git a/phpunit/class-wp-rest-block-types-controller-test.php b/phpunit/class-wp-rest-block-types-controller-test.php new file mode 100644 index 00000000000000..55c462c8c42fc9 --- /dev/null +++ b/phpunit/class-wp-rest-block-types-controller-test.php @@ -0,0 +1,150 @@ +user->create( + array( + 'role' => 'administrator', + ) + ); + $name = 'fake/test'; + $settings = array( + 'icon' => 'text', + ); + + register_block_type( $name, $settings ); + } + + /** + * + */ + public static function wpTearDownAfterClass() { + self::delete_user( self::$admin_id ); + unregister_block_type( 'fake/test' ); + } + + /** + * + */ + public function test_register_routes() { + $routes = rest_get_server()->get_routes(); + $this->assertArrayHasKey( '/__experimental/block-types', $routes ); + $this->assertCount( 1, $routes['/__experimental/block-types'] ); + $this->assertArrayHasKey( '/__experimental/block-types/(?P[a-zA-Z0-9_-]+)', $routes ); + $this->assertCount( 1, $routes['/__experimental/block-types/(?P[a-zA-Z0-9_-]+)'] ); + $this->assertArrayHasKey( '/__experimental/block-types/(?P[a-zA-Z0-9_-]+)/(?P[a-zA-Z0-9_-]+)', $routes ); + $this->assertCount( 1, $routes['/__experimental/block-types/(?P[a-zA-Z0-9_-]+)/(?P[a-zA-Z0-9_-]+)'] ); + } + + /** + * + */ + public function test_context_param() { + // Collection. + $request = new WP_REST_Request( 'OPTIONS', '/__experimental/block-types' ); + $response = rest_get_server()->dispatch( $request ); + $data = $response->get_data(); + $this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] ); + $this->assertEquals( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] ); + $menu = 'primary'; + $this->register_nav_menu_locations( array( $menu ) ); + $request = new WP_REST_Request( 'OPTIONS', '/__experimental/block-types/' . $menu ); + $response = rest_get_server()->dispatch( $request ); + $data = $response->get_data(); + $this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] ); + $this->assertEquals( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] ); + } + + /** + * + */ + public function test_get_items() { + register_block_type( 'test/block-1', [] ); + register_block_type( 'test/block-2', [] ); + wp_set_current_user( self::$admin_id ); + $request = new WP_REST_Request( 'GET', '/__experimental/block-types/test' ); + $response = rest_get_server()->dispatch( $request ); + $data = $response->get_data(); + $data = array_values( $data ); + $this->assertCount( 2, $data ); + $names = wp_list_pluck( $data, 'name' ); + $this->assertEqualSets( array('test/block-1', 'test/block-2'), $names ); + unregister_block_type('test/block-1'); + unregister_block_type('test/block-2'); + } + + /** + * + */ + public function test_get_item() { + $block_type = 'fake/test'; + wp_set_current_user( self::$admin_id ); + $request = new WP_REST_Request( 'GET', '/__experimental/block-types/' . $block_type ); + $response = rest_get_server()->dispatch( $request ); + $data = $response->get_data(); + $this->assertEquals( $block_type, $data['name'] ); + } + + /** + * + */ + public function test_get_item_schema() { + wp_set_current_user( self::$admin_id ); + $request = new WP_REST_Request( 'OPTIONS', '/__experimental/block-types' ); + $response = rest_get_server()->dispatch( $request ); + $data = $response->get_data(); + $properties = $data['schema']['properties']; + $this->assertEquals( 9, count( $properties ) ); + $this->assertArrayHasKey( 'name', $properties ); + $this->assertArrayHasKey( 'attributes', $properties ); + $this->assertArrayHasKey( 'supports', $properties ); + $this->assertArrayHasKey( 'category', $properties ); + $this->assertArrayHasKey( 'is_dynamic', $properties ); + $this->assertArrayHasKey( 'editor_script', $properties ); + $this->assertArrayHasKey( 'script', $properties ); + $this->assertArrayHasKey( 'editor_style', $properties ); + $this->assertArrayHasKey( 'style', $properties ); + } + + /** + * The test_create_item() method does not exist for block types. + */ + public function test_create_item() {} + + /** + * The test_update_item() method does not exist for block types. + */ + public function test_update_item() {} + + /** + * The test_delete_item() method does not exist for block types. + */ + public function test_delete_item() {} + + /** + * The test_prepare_item() method does not exist for block types. + */ + public function test_prepare_item() {} +} From 9eea8a39f82537215b4f5af559272b2903634184 Mon Sep 17 00:00:00 2001 From: spacedmonkey Date: Fri, 22 May 2020 00:23:09 +0100 Subject: [PATCH 21/35] More tweaks --- .../class-wp-rest-block-types-controller-test.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/phpunit/class-wp-rest-block-types-controller-test.php b/phpunit/class-wp-rest-block-types-controller-test.php index 55c462c8c42fc9..523aa1e1f414e3 100644 --- a/phpunit/class-wp-rest-block-types-controller-test.php +++ b/phpunit/class-wp-rest-block-types-controller-test.php @@ -81,18 +81,18 @@ public function test_context_param() { * */ public function test_get_items() { - register_block_type( 'test/block-1', [] ); - register_block_type( 'test/block-2', [] ); + register_block_type( 'test/block-1', array() ); + register_block_type( 'test/block-2', array() ); wp_set_current_user( self::$admin_id ); $request = new WP_REST_Request( 'GET', '/__experimental/block-types/test' ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); $data = array_values( $data ); $this->assertCount( 2, $data ); - $names = wp_list_pluck( $data, 'name' ); - $this->assertEqualSets( array('test/block-1', 'test/block-2'), $names ); - unregister_block_type('test/block-1'); - unregister_block_type('test/block-2'); + $names = wp_list_pluck( $data, 'name' ); + $this->assertEqualSets( array( 'test/block-1', 'test/block-2' ), $names ); + unregister_block_type( 'test/block-1' ); + unregister_block_type( 'test/block-2' ); } /** From f11b18df3450f9118f82654cae8546d1f99e8cef Mon Sep 17 00:00:00 2001 From: spacedmonkey Date: Fri, 22 May 2020 10:02:48 +0100 Subject: [PATCH 22/35] Fix tests --- phpunit/class-wp-rest-block-types-controller-test.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/phpunit/class-wp-rest-block-types-controller-test.php b/phpunit/class-wp-rest-block-types-controller-test.php index 523aa1e1f414e3..728323673b4fc1 100644 --- a/phpunit/class-wp-rest-block-types-controller-test.php +++ b/phpunit/class-wp-rest-block-types-controller-test.php @@ -62,19 +62,20 @@ public function test_register_routes() { * */ public function test_context_param() { + register_block_type( 'test/block-1', array() ); // Collection. $request = new WP_REST_Request( 'OPTIONS', '/__experimental/block-types' ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); $this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] ); $this->assertEquals( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] ); - $menu = 'primary'; - $this->register_nav_menu_locations( array( $menu ) ); - $request = new WP_REST_Request( 'OPTIONS', '/__experimental/block-types/' . $menu ); + // Single. + $request = new WP_REST_Request( 'OPTIONS', '/__experimental/block-types/test/block-1' ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); $this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] ); $this->assertEquals( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] ); + unregister_block_type( 'test/block-1' ); } /** From a45c48cafb6fca49f55081d7f97ed37b1e6c558d Mon Sep 17 00:00:00 2001 From: spacedmonkey Date: Fri, 22 May 2020 10:20:12 +0100 Subject: [PATCH 23/35] Tweak test. --- phpunit/class-wp-rest-block-types-controller-test.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/phpunit/class-wp-rest-block-types-controller-test.php b/phpunit/class-wp-rest-block-types-controller-test.php index 728323673b4fc1..67e3ed6b76681d 100644 --- a/phpunit/class-wp-rest-block-types-controller-test.php +++ b/phpunit/class-wp-rest-block-types-controller-test.php @@ -62,7 +62,7 @@ public function test_register_routes() { * */ public function test_context_param() { - register_block_type( 'test/block-1', array() ); + register_block_type( 'testing/block-1', array() ); // Collection. $request = new WP_REST_Request( 'OPTIONS', '/__experimental/block-types' ); $response = rest_get_server()->dispatch( $request ); @@ -70,12 +70,12 @@ public function test_context_param() { $this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] ); $this->assertEquals( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] ); // Single. - $request = new WP_REST_Request( 'OPTIONS', '/__experimental/block-types/test/block-1' ); + $request = new WP_REST_Request( 'OPTIONS', '/__experimental/block-types/testing/block-1' ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); $this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] ); $this->assertEquals( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] ); - unregister_block_type( 'test/block-1' ); + unregister_block_type( 'testing/block-1' ); } /** From dbcca033fe692b7788cca762eeedb1f16b24033a Mon Sep 17 00:00:00 2001 From: spacedmonkey Date: Fri, 22 May 2020 12:34:36 +0100 Subject: [PATCH 24/35] Reuse fake block --- .../class-wp-rest-block-types-controller-test.php | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/phpunit/class-wp-rest-block-types-controller-test.php b/phpunit/class-wp-rest-block-types-controller-test.php index 67e3ed6b76681d..4170f0a0aed90a 100644 --- a/phpunit/class-wp-rest-block-types-controller-test.php +++ b/phpunit/class-wp-rest-block-types-controller-test.php @@ -62,7 +62,6 @@ public function test_register_routes() { * */ public function test_context_param() { - register_block_type( 'testing/block-1', array() ); // Collection. $request = new WP_REST_Request( 'OPTIONS', '/__experimental/block-types' ); $response = rest_get_server()->dispatch( $request ); @@ -70,30 +69,25 @@ public function test_context_param() { $this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] ); $this->assertEquals( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] ); // Single. - $request = new WP_REST_Request( 'OPTIONS', '/__experimental/block-types/testing/block-1' ); + $request = new WP_REST_Request( 'OPTIONS', '/__experimental/block-types/fake/test' ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); $this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] ); $this->assertEquals( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] ); - unregister_block_type( 'testing/block-1' ); } /** * */ public function test_get_items() { - register_block_type( 'test/block-1', array() ); - register_block_type( 'test/block-2', array() ); wp_set_current_user( self::$admin_id ); - $request = new WP_REST_Request( 'GET', '/__experimental/block-types/test' ); + $request = new WP_REST_Request( 'GET', '/__experimental/block-types/fake' ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); $data = array_values( $data ); - $this->assertCount( 2, $data ); + $this->assertCount( 1, $data ); $names = wp_list_pluck( $data, 'name' ); $this->assertEqualSets( array( 'test/block-1', 'test/block-2' ), $names ); - unregister_block_type( 'test/block-1' ); - unregister_block_type( 'test/block-2' ); } /** From 0ea4d714380c6f2b02b9ea86a77795e05dc347be Mon Sep 17 00:00:00 2001 From: spacedmonkey Date: Fri, 22 May 2020 13:18:44 +0100 Subject: [PATCH 25/35] Add in new fields and improve tests. --- lib/class-wp-rest-block-types-controller.php | 103 ++++++++++++++++-- ...ss-wp-rest-block-types-controller-test.php | 45 +++++++- 2 files changed, 134 insertions(+), 14 deletions(-) diff --git a/lib/class-wp-rest-block-types-controller.php b/lib/class-wp-rest-block-types-controller.php index aadafbf01e1a6e..a1d1bb8399b9e8 100644 --- a/lib/class-wp-rest-block-types-controller.php +++ b/lib/class-wp-rest-block-types-controller.php @@ -230,18 +230,31 @@ public function prepare_item_for_response( $block_type, $request ) { $schema = $this->get_item_schema(); $extra_fields = array( - 'name', - 'category', - 'editor_script', - 'script', - 'editor_style', - 'style', - 'supports', + 'name' => 'name', + 'category' => 'category', + 'editor_script' => 'editor_script', + 'script' => 'script', + 'editor_style' => 'editor_style', + 'style' => 'style', + 'supports' => 'supports', + 'title' => 'title', + 'icon' => 'icon', + 'description' => 'description', + 'keywords' => 'keywords', + 'parent' => 'parent', + 'styles' => 'styleVariations', + 'text_domain' => 'textDomain', ); - foreach ( $extra_fields as $extra_field ) { - if ( rest_is_field_included( $extra_field, $fields ) ) { - $field = isset( $block_type->$extra_field ) ? $block_type->$extra_field : ''; - $data[ $extra_field ] = rest_sanitize_value_from_schema( $field, $schema['properties'][ $extra_field ] ); + foreach ( $extra_fields as $key => $extra_field ) { + if ( rest_is_field_included( $key, $fields ) ) { + if ( isset( $block_type->$extra_field ) ) { + $field = $block_type->$extra_field; + } elseif ( isset( $schema['properties'][ $key ]['default'] ) ) { + $field = $schema['properties'][ $key ]['default']; + } else { + $field = ''; + } + $data[ $key ] = rest_sanitize_value_from_schema( $field, $schema['properties'][ $key ] ); } } @@ -308,9 +321,31 @@ public function get_item_schema() { 'title' => 'block-type', 'type' => 'object', 'properties' => array( + 'title' => array( + 'description' => __( 'Title of block type.', 'gutenberg' ), + 'type' => 'string', + 'default' => '', + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), 'name' => array( 'description' => __( 'Unique name identifying the block type.', 'gutenberg' ), 'type' => 'string', + 'default' => '', + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), + 'description' => array( + 'description' => __( 'Description of block type.', 'gutenberg' ), + 'type' => 'string', + 'default' => '', + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), + 'icon' => array( + 'description' => __( 'Icon of block type.', 'gutenberg' ), + 'type' => 'string', + 'default' => '', 'context' => array( 'embed', 'view', 'edit' ), 'readonly' => true, ), @@ -318,6 +353,7 @@ public function get_item_schema() { 'description' => __( 'Block attributes.', 'gutenberg' ), 'type' => 'object', 'properties' => array(), + 'default' => array(), 'additionalProperties' => array( 'type' => 'object', ), @@ -327,6 +363,7 @@ public function get_item_schema() { 'supports' => array( 'description' => __( 'Block supports.', 'gutenberg' ), 'type' => 'object', + 'default' => array(), 'properties' => array(), 'context' => array( 'embed', 'view', 'edit' ), 'readonly' => true, @@ -334,36 +371,80 @@ public function get_item_schema() { 'category' => array( 'description' => __( 'Block category.', 'gutenberg' ), 'type' => 'string', + 'default' => '', 'context' => array( 'embed', 'view', 'edit' ), 'readonly' => true, ), 'is_dynamic' => array( 'description' => __( 'Is the block dynamically rendered.', 'gutenberg' ), 'type' => 'boolean', + 'default' => false, 'context' => array( 'embed', 'view', 'edit' ), 'readonly' => true, ), 'editor_script' => array( 'description' => __( 'Editor script handle.', 'gutenberg' ), 'type' => 'string', + 'default' => '', 'context' => array( 'embed', 'view', 'edit' ), 'readonly' => true, ), 'script' => array( 'description' => __( 'Public facing script handle.', 'gutenberg' ), 'type' => 'string', + 'default' => '', 'context' => array( 'embed', 'view', 'edit' ), 'readonly' => true, ), 'editor_style' => array( 'description' => __( 'Editor style handle.', 'gutenberg' ), 'type' => 'string', + 'default' => '', 'context' => array( 'embed', 'view', 'edit' ), 'readonly' => true, ), 'style' => array( 'description' => __( 'Public facing style handle.', 'gutenberg' ), 'type' => 'string', + 'default' => '', + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), + 'styles' => array( + 'description' => __( 'Block style variations.', 'gutenberg' ), + 'type' => 'object', + 'properties' => array(), + 'additionalProperties' => array( + 'type' => 'object', + ), + 'default' => array(), + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), + 'text_domain' => array( + 'description' => __( 'Public text domain.', 'gutenberg' ), + 'type' => 'string', + 'default' => '', + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), + 'parent' => array( + 'description' => __( 'Parent blocks, defaults to empty it no parents', 'gutenberg' ), + 'type' => 'array', + 'items' => array( + 'type' => 'string', + ), + 'default' => array(), + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), + 'keywords' => array( + 'description' => __( 'Block keywords.', 'gutenberg' ), + 'type' => 'array', + 'items' => array( + 'type' => 'string', + ), + 'default' => array(), 'context' => array( 'embed', 'view', 'edit' ), 'readonly' => true, ), diff --git a/phpunit/class-wp-rest-block-types-controller-test.php b/phpunit/class-wp-rest-block-types-controller-test.php index 4170f0a0aed90a..fdf5a79db142d4 100644 --- a/phpunit/class-wp-rest-block-types-controller-test.php +++ b/phpunit/class-wp-rest-block-types-controller-test.php @@ -18,17 +18,28 @@ class REST_WP_REST_Block_Types_Controller_Test extends WP_Test_REST_Post_Type_Co */ protected static $admin_id; + /** + * @var int + */ + protected static $subscriber_id; + /** * Create fake data before our tests run. * * @param WP_UnitTest_Factory $factory Helper that lets us create fake data. */ public static function wpSetUpBeforeClass( $factory ) { - self::$admin_id = $factory->user->create( + self::$admin_id = $factory->user->create( array( 'role' => 'administrator', ) ); + self::$subscriber_id = $factory->user->create( + array( + 'role' => 'subscriber', + ) + ); + $name = 'fake/test'; $settings = array( 'icon' => 'text', @@ -42,6 +53,7 @@ public static function wpSetUpBeforeClass( $factory ) { */ public static function wpTearDownAfterClass() { self::delete_user( self::$admin_id ); + self::delete_user( self::$subscriber_id ); unregister_block_type( 'fake/test' ); } @@ -87,7 +99,7 @@ public function test_get_items() { $data = array_values( $data ); $this->assertCount( 1, $data ); $names = wp_list_pluck( $data, 'name' ); - $this->assertEqualSets( array( 'test/block-1', 'test/block-2' ), $names ); + $this->assertEqualSets( array( 'fake/test' ), $names ); } /** @@ -111,7 +123,13 @@ public function test_get_item_schema() { $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); $properties = $data['schema']['properties']; - $this->assertEquals( 9, count( $properties ) ); + $this->assertEquals( 16, count( $properties ) ); + $this->assertArrayHasKey( 'title', $properties ); + $this->assertArrayHasKey( 'icon', $properties ); + $this->assertArrayHasKey( 'description', $properties ); + $this->assertArrayHasKey( 'keywords', $properties ); + $this->assertArrayHasKey( 'styles', $properties ); + $this->assertArrayHasKey( 'text_domain', $properties ); $this->assertArrayHasKey( 'name', $properties ); $this->assertArrayHasKey( 'attributes', $properties ); $this->assertArrayHasKey( 'supports', $properties ); @@ -121,6 +139,27 @@ public function test_get_item_schema() { $this->assertArrayHasKey( 'script', $properties ); $this->assertArrayHasKey( 'editor_style', $properties ); $this->assertArrayHasKey( 'style', $properties ); + $this->assertArrayHasKey( 'parent', $properties ); + } + + /** + * + */ + public function test_get_items_wrong_permission() { + wp_set_current_user( self::$subscriber_id ); + $request = new WP_REST_Request( 'GET', '/__experimental/block-types' ); + $response = rest_get_server()->dispatch( $request ); + $this->assertErrorResponse( 'rest_cannot_view', $response, 403 ); + } + + /** + * + */ + public function test_get_item_wrong_permission() { + wp_set_current_user( self::$subscriber_id ); + $request = new WP_REST_Request( 'GET', '/__experimental/block-types/fake/test' ); + $response = rest_get_server()->dispatch( $request ); + $this->assertErrorResponse( 'rest_cannot_view', $response, 403 ); } /** From 50efa40411ca8864533945ee2105a4dc8b2faf1d Mon Sep 17 00:00:00 2001 From: spacedmonkey Date: Fri, 22 May 2020 13:38:03 +0100 Subject: [PATCH 26/35] Fix lint --- phpunit/class-wp-rest-block-types-controller-test.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/phpunit/class-wp-rest-block-types-controller-test.php b/phpunit/class-wp-rest-block-types-controller-test.php index fdf5a79db142d4..5b81330a16a6b3 100644 --- a/phpunit/class-wp-rest-block-types-controller-test.php +++ b/phpunit/class-wp-rest-block-types-controller-test.php @@ -40,8 +40,8 @@ public static function wpSetUpBeforeClass( $factory ) { ) ); - $name = 'fake/test'; - $settings = array( + $name = 'fake/test'; + $settings = array( 'icon' => 'text', ); From f9878fd1fc6fe15b619a47be53b81f4a9b901377 Mon Sep 17 00:00:00 2001 From: spacedmonkey Date: Fri, 22 May 2020 14:06:35 +0100 Subject: [PATCH 27/35] Reuse wp_error. --- lib/class-wp-rest-block-types-controller.php | 4 ++-- phpunit/class-wp-rest-block-types-controller-test.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/class-wp-rest-block-types-controller.php b/lib/class-wp-rest-block-types-controller.php index a1d1bb8399b9e8..9db0db9793f92d 100644 --- a/lib/class-wp-rest-block-types-controller.php +++ b/lib/class-wp-rest-block-types-controller.php @@ -149,8 +149,8 @@ public function get_items( $request ) { */ public function get_item_permissions_check( $request ) { $check = $this->check_read_permission(); - if ( ! $check ) { - return new WP_Error( 'rest_block_type_cannot_view', __( 'Cannot view block type.', 'gutenberg' ), array( 'status' => rest_authorization_required_code() ) ); + if ( is_wp_error( $check ) ) { + return $check; } $block_name = sprintf( '%s/%s', $request['namespace'], $request['name'] ); $block_type = $this->get_block( $block_name ); diff --git a/phpunit/class-wp-rest-block-types-controller-test.php b/phpunit/class-wp-rest-block-types-controller-test.php index 5b81330a16a6b3..fc87a0fd59b943 100644 --- a/phpunit/class-wp-rest-block-types-controller-test.php +++ b/phpunit/class-wp-rest-block-types-controller-test.php @@ -149,7 +149,7 @@ public function test_get_items_wrong_permission() { wp_set_current_user( self::$subscriber_id ); $request = new WP_REST_Request( 'GET', '/__experimental/block-types' ); $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_cannot_view', $response, 403 ); + $this->assertErrorResponse( 'rest_block_type_cannot_view', $response, 403 ); } /** @@ -159,7 +159,7 @@ public function test_get_item_wrong_permission() { wp_set_current_user( self::$subscriber_id ); $request = new WP_REST_Request( 'GET', '/__experimental/block-types/fake/test' ); $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_cannot_view', $response, 403 ); + $this->assertErrorResponse( 'rest_block_type_cannot_view', $response, 403 ); } /** From b9675fd0d91d32a3b516c863e68427105e136abd Mon Sep 17 00:00:00 2001 From: spacedmonkey Date: Fri, 22 May 2020 21:11:15 +0100 Subject: [PATCH 28/35] Add more tests --- ...ss-wp-rest-block-types-controller-test.php | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/phpunit/class-wp-rest-block-types-controller-test.php b/phpunit/class-wp-rest-block-types-controller-test.php index fc87a0fd59b943..f544c76bca2e20 100644 --- a/phpunit/class-wp-rest-block-types-controller-test.php +++ b/phpunit/class-wp-rest-block-types-controller-test.php @@ -114,6 +114,40 @@ public function test_get_item() { $this->assertEquals( $block_type, $data['name'] ); } + public function test_get_block_invalid_name() { + $block_type = 'fake/block'; + wp_set_current_user( self::$admin_id ); + $request = new WP_REST_Request( 'GET', '/__experimental/block-types/' . $block_type ); + $response = rest_get_server()->dispatch( $request ); + + $this->assertErrorResponse( 'rest_block_type_invalid', $response, 404 ); + } + + /** + * + */ + public function test_get_item_invalid() { + $block_type = 'fake/invalid'; + $settings = array( + 'keywords' => 'invalid_keywords', + 'parent' => 'invalid_parent', + 'supports' => 'invalid_supports', + 'styleVariations' => 'invalid_styles', + 'render_callback' => 'invalid_callback', + ); + register_block_type( $block_type, $settings ); + wp_set_current_user( self::$admin_id ); + $request = new WP_REST_Request( 'GET', '/__experimental/block-types/' . $block_type ); + $response = rest_get_server()->dispatch( $request ); + $data = $response->get_data(); + $this->assertEquals( $block_type, $data['name'] ); + $this->assertEqualSets( array( 'invalid_keywords' ), $data['keywords'] ); + $this->assertEqualSets( array( 'invalid_parent' ), $data['parent'] ); + $this->assertEqualSets( array(), $data['supports'] ); + $this->assertEqualSets( array(), $data['styles'] ); + $this->assertEqualSets( false, $data['is_dynamic'] ); + } + /** * */ @@ -162,6 +196,26 @@ public function test_get_item_wrong_permission() { $this->assertErrorResponse( 'rest_block_type_cannot_view', $response, 403 ); } + /** + * + */ + public function test_get_items_no_permission() { + wp_set_current_user( 0 ); + $request = new WP_REST_Request( 'GET', '/__experimental/block-types' ); + $response = rest_get_server()->dispatch( $request ); + $this->assertErrorResponse( 'rest_block_type_cannot_view', $response, 401 ); + } + + /** + * + */ + public function test_get_item_no_permission() { + wp_set_current_user( 0 ); + $request = new WP_REST_Request( 'GET', '/__experimental/block-types/fake/test/' ); + $response = rest_get_server()->dispatch( $request ); + $this->assertErrorResponse( 'rest_block_type_cannot_view', $response, 401 ); + } + /** * The test_create_item() method does not exist for block types. */ From 62f604e1cd773de04e835c1b331f972e7bd049e9 Mon Sep 17 00:00:00 2001 From: spacedmonkey Date: Sun, 24 May 2020 18:47:10 +0100 Subject: [PATCH 29/35] Improve asserts. --- phpunit/class-wp-rest-block-types-controller-test.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/phpunit/class-wp-rest-block-types-controller-test.php b/phpunit/class-wp-rest-block-types-controller-test.php index f544c76bca2e20..7d9611b9d8af88 100644 --- a/phpunit/class-wp-rest-block-types-controller-test.php +++ b/phpunit/class-wp-rest-block-types-controller-test.php @@ -145,7 +145,7 @@ public function test_get_item_invalid() { $this->assertEqualSets( array( 'invalid_parent' ), $data['parent'] ); $this->assertEqualSets( array(), $data['supports'] ); $this->assertEqualSets( array(), $data['styles'] ); - $this->assertEqualSets( false, $data['is_dynamic'] ); + $this->assertEquals( false, $data['is_dynamic'] ); } /** @@ -157,7 +157,7 @@ public function test_get_item_schema() { $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); $properties = $data['schema']['properties']; - $this->assertEquals( 16, count( $properties ) ); + $this->assertCount( 16, $properties ); $this->assertArrayHasKey( 'title', $properties ); $this->assertArrayHasKey( 'icon', $properties ); $this->assertArrayHasKey( 'description', $properties ); @@ -211,7 +211,7 @@ public function test_get_items_no_permission() { */ public function test_get_item_no_permission() { wp_set_current_user( 0 ); - $request = new WP_REST_Request( 'GET', '/__experimental/block-types/fake/test/' ); + $request = new WP_REST_Request( 'GET', '/__experimental/block-types/fake/test' ); $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_block_type_cannot_view', $response, 401 ); } From 6db18c8f457ab135fe8b4f2e005306d162b4f73e Mon Sep 17 00:00:00 2001 From: spacedmonkey Date: Mon, 25 May 2020 18:12:45 +0100 Subject: [PATCH 30/35] Refactor permision check --- lib/class-wp-rest-block-types-controller.php | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/class-wp-rest-block-types-controller.php b/lib/class-wp-rest-block-types-controller.php index 9db0db9793f92d..e6fde1ed11b161 100644 --- a/lib/class-wp-rest-block-types-controller.php +++ b/lib/class-wp-rest-block-types-controller.php @@ -100,11 +100,7 @@ public function register_routes() { * @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 new WP_Error( 'rest_block_type_cannot_view', __( 'Sorry, you are not allowed to manage block types.', 'gutenberg' ), array( 'status' => rest_authorization_required_code() ) ); - } - - return true; + return $this->check_read_permission(); } /** @@ -167,11 +163,16 @@ public function get_item_permissions_check( $request ) { * @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_block_type_cannot_view', __( 'Sorry, you are not allowed to manage block types.', 'gutenberg' ), array( 'status' => rest_authorization_required_code() ) ); + 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 true; + return new WP_Error( 'rest_block_type_cannot_view', __( 'Sorry, you are not allowed to manage block types.', 'gutenberg' ), array( 'status' => rest_authorization_required_code() ) ); } /** From fcb6a1da2714bd7e79e03611a8d47140530759b2 Mon Sep 17 00:00:00 2001 From: spacedmonkey Date: Mon, 25 May 2020 18:58:45 +0100 Subject: [PATCH 31/35] Add more tests. --- ...ss-wp-rest-block-types-controller-test.php | 93 ++++++++++++++++++- 1 file changed, 88 insertions(+), 5 deletions(-) diff --git a/phpunit/class-wp-rest-block-types-controller-test.php b/phpunit/class-wp-rest-block-types-controller-test.php index 7d9611b9d8af88..1386752946ab1f 100644 --- a/phpunit/class-wp-rest-block-types-controller-test.php +++ b/phpunit/class-wp-rest-block-types-controller-test.php @@ -106,12 +106,12 @@ public function test_get_items() { * */ public function test_get_item() { - $block_type = 'fake/test'; + $block_name = 'fake/test'; wp_set_current_user( self::$admin_id ); - $request = new WP_REST_Request( 'GET', '/__experimental/block-types/' . $block_type ); - $response = rest_get_server()->dispatch( $request ); - $data = $response->get_data(); - $this->assertEquals( $block_type, $data['name'] ); + $request = new WP_REST_Request( 'GET', '/__experimental/block-types/' . $block_name ); + $response = rest_get_server()->dispatch( $request ); + $block_type = WP_Block_Type_Registry::get_instance()->register( $block_name ); + $this->check_block_type_object( $block_type, $response->get_data(), $response->get_links() ); } public function test_get_block_invalid_name() { @@ -216,6 +216,89 @@ public function test_get_item_no_permission() { $this->assertErrorResponse( 'rest_block_type_cannot_view', $response, 401 ); } + /** + * + */ + public function test_prepare_item() { + $registry = new WP_Block_Type_Registry; + $settings = array( + 'icon' => 'text', + 'render_callback' => '__return_null', + ); + $registry->register( 'fake/line', $settings ); + $block_type = $registry->get_registered( 'fake/line' ); + $endpoint = new WP_REST_Block_Types_Controller; + $request = new WP_REST_Request; + $request->set_param( 'context', 'edit' ); + $response = $endpoint->prepare_item_for_response( $block_type, $request ); + $this->check_block_type_object( $block_type, $response->get_data(), $response->get_links() ); + } + + /** + * + */ + public function test_prepare_item_limit_fields() { + $registry = new WP_Block_Type_Registry; + $settings = array( + 'icon' => 'text', + 'render_callback' => '__return_null', + ); + $registry->register( 'fake/line', $settings ); + $block_type = $registry->get_registered( 'fake/line' ); + $request = new WP_REST_Request; + $endpoint = new WP_REST_Block_Types_Controller; + $request->set_param( 'context', 'edit' ); + $request->set_param( '_fields', 'name' ); + $response = $endpoint->prepare_item_for_response( $block_type, $request ); + $this->assertEquals( + array( + 'name', + ), + array_keys( $response->get_data() ) + ); + } + + /** + * Util check block type object against. + * + * @param WP_Block_Type $block_type Sample block type. + * @param array $data Data to compare against. + * @param array $links Links to compare again. + */ + public function check_block_type_object( $block_type, $data, $links ) { + $this->assertEquals( rest_url( '__experimental/block-types' ), $links['collection'][0]['href'] ); + $this->assertArrayHasKey( 'https://api.w.org/items', $links ); + + $this->assertEquals( $data['attributes'], $block_type->get_attributes() ); + $this->assertEquals( $data['is_dynamic'], $block_type->is_dynamic() ); + if ( $block_type->is_dynamic() ) { + $this->assertArrayHasKey( 'https://api.w.org/render-block', $links ); + } + + $extra_fields = array( + 'name' => 'name', + 'category' => 'category', + 'editor_script' => 'editor_script', + 'script' => 'script', + 'editor_style' => 'editor_style', + 'style' => 'style', + 'supports' => 'supports', + 'title' => 'title', + 'icon' => 'icon', + 'description' => 'description', + 'keywords' => 'keywords', + 'parent' => 'parent', + 'styles' => 'styleVariations', + 'text_domain' => 'textDomain', + ); + + foreach ( $extra_fields as $key => $extra_field ) { + if ( isset( $block_type->$extra_field ) ) { + $this->assertEquals( $data[ $key ], $block_type->$extra_field ); + } + } + } + /** * The test_create_item() method does not exist for block types. */ From 296efe640aa7dac4171730609935d45c903bcc65 Mon Sep 17 00:00:00 2001 From: spacedmonkey Date: Mon, 25 May 2020 19:12:29 +0100 Subject: [PATCH 32/35] Remove unused method. --- phpunit/class-wp-rest-block-types-controller-test.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/phpunit/class-wp-rest-block-types-controller-test.php b/phpunit/class-wp-rest-block-types-controller-test.php index 1386752946ab1f..353c4e5c0b65a8 100644 --- a/phpunit/class-wp-rest-block-types-controller-test.php +++ b/phpunit/class-wp-rest-block-types-controller-test.php @@ -313,9 +313,4 @@ public function test_update_item() {} * The test_delete_item() method does not exist for block types. */ public function test_delete_item() {} - - /** - * The test_prepare_item() method does not exist for block types. - */ - public function test_prepare_item() {} } From b98d04c44b30b42f7619ec8746d57769e1234cce Mon Sep 17 00:00:00 2001 From: spacedmonkey Date: Mon, 25 May 2020 19:48:55 +0100 Subject: [PATCH 33/35] Fix tests. --- .../class-wp-rest-block-types-controller-test.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/phpunit/class-wp-rest-block-types-controller-test.php b/phpunit/class-wp-rest-block-types-controller-test.php index 353c4e5c0b65a8..aaa41d00f6ce05 100644 --- a/phpunit/class-wp-rest-block-types-controller-test.php +++ b/phpunit/class-wp-rest-block-types-controller-test.php @@ -266,14 +266,9 @@ public function test_prepare_item_limit_fields() { * @param array $links Links to compare again. */ public function check_block_type_object( $block_type, $data, $links ) { - $this->assertEquals( rest_url( '__experimental/block-types' ), $links['collection'][0]['href'] ); - $this->assertArrayHasKey( 'https://api.w.org/items', $links ); - + // Test data. $this->assertEquals( $data['attributes'], $block_type->get_attributes() ); $this->assertEquals( $data['is_dynamic'], $block_type->is_dynamic() ); - if ( $block_type->is_dynamic() ) { - $this->assertArrayHasKey( 'https://api.w.org/render-block', $links ); - } $extra_fields = array( 'name' => 'name', @@ -297,6 +292,13 @@ public function check_block_type_object( $block_type, $data, $links ) { $this->assertEquals( $data[ $key ], $block_type->$extra_field ); } } + + // Test links. + $this->assertEquals( rest_url( '__experimental/block-types' ), $links['collection'][0]['href'] ); + $this->assertEquals( rest_url( '__experimental/block-types/' . $block_type->name ), $links['self'][0]['href'] ); + if ( $block_type->is_dynamic() ) { + $this->assertArrayHasKey( 'https://api.w.org/render-block', $links ); + } } /** From bfd96507cf17fcc852a19ee897462aec6253a18f Mon Sep 17 00:00:00 2001 From: spacedmonkey Date: Mon, 25 May 2020 20:20:11 +0100 Subject: [PATCH 34/35] Use correct name. --- phpunit/class-wp-rest-block-types-controller-test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpunit/class-wp-rest-block-types-controller-test.php b/phpunit/class-wp-rest-block-types-controller-test.php index aaa41d00f6ce05..37008c4fbcf2f4 100644 --- a/phpunit/class-wp-rest-block-types-controller-test.php +++ b/phpunit/class-wp-rest-block-types-controller-test.php @@ -110,7 +110,7 @@ public function test_get_item() { wp_set_current_user( self::$admin_id ); $request = new WP_REST_Request( 'GET', '/__experimental/block-types/' . $block_name ); $response = rest_get_server()->dispatch( $request ); - $block_type = WP_Block_Type_Registry::get_instance()->register( $block_name ); + $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block_name ); $this->check_block_type_object( $block_type, $response->get_data(), $response->get_links() ); } From 2744ebf9166058f2a5a3ebff8ad40d54eb0c3377 Mon Sep 17 00:00:00 2001 From: spacedmonkey Date: Mon, 25 May 2020 21:08:58 +0100 Subject: [PATCH 35/35] Improve tests. --- phpunit/class-wp-rest-block-types-controller-test.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/phpunit/class-wp-rest-block-types-controller-test.php b/phpunit/class-wp-rest-block-types-controller-test.php index 37008c4fbcf2f4..952d8892e4547b 100644 --- a/phpunit/class-wp-rest-block-types-controller-test.php +++ b/phpunit/class-wp-rest-block-types-controller-test.php @@ -92,14 +92,14 @@ public function test_context_param() { * */ public function test_get_items() { + $block_name = 'fake/test'; wp_set_current_user( self::$admin_id ); $request = new WP_REST_Request( 'GET', '/__experimental/block-types/fake' ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); - $data = array_values( $data ); $this->assertCount( 1, $data ); - $names = wp_list_pluck( $data, 'name' ); - $this->assertEqualSets( array( 'fake/test' ), $names ); + $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block_name ); + $this->check_block_type_object( $block_type, $data[0], $data[0]['_links'] ); } /**