diff --git a/src/wp-includes/class-wp-block-styles-registry.php b/src/wp-includes/class-wp-block-styles-registry.php index f56d0e2d72..1b4e9fff4c 100644 --- a/src/wp-includes/class-wp-block-styles-registry.php +++ b/src/wp-includes/class-wp-block-styles-registry.php @@ -34,7 +34,7 @@ final class WP_Block_Styles_Registry { * * @since 5.3.0 * - * @param string $block_name Block type name including namespace. + * @param string $block_name Block ty name including namespace. * @param array $style_properties Array containing the properties of the style name, label, * style (name of the stylesheet to be enqueued), * inline_style (string containing the CSS to be added). diff --git a/src/wp-includes/rest-api.php b/src/wp-includes/rest-api.php index 43569e3c1a..e906727697 100644 --- a/src/wp-includes/rest-api.php +++ b/src/wp-includes/rest-api.php @@ -273,6 +273,10 @@ function create_initial_rest_routes() { $controller = new WP_REST_Block_Renderer_Controller; $controller->register_routes(); + // Block Types. + $controller = new WP_REST_Block_Types_Controller(); + $controller->register_routes(); + // Settings. $controller = new WP_REST_Settings_Controller; $controller->register_routes(); diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php new file mode 100644 index 0000000000..0306a195e9 --- /dev/null +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php @@ -0,0 +1,593 @@ +namespace = 'wp/v2'; + $this->rest_base = 'block-types'; + $this->block_registry = WP_Block_Type_Registry::get_instance(); + $this->style_registry = WP_Block_Styles_Registry::get_instance(); + } + + /** + * Registers the routes for the objects of the controller. + * + * @since 5.5.0 + * + * @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_-]+)', + 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' ), + 'type' => 'string', + ), + 'namespace' => array( + 'description' => __( 'Block namespace' ), + '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. + * + * @since 5.5.0 + * + * @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 ) { + return $this->check_read_permission(); + } + + /** + * Retrieves all post block types, depending on user context. + * + * @since 5.5.0 + * + * @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 = $this->block_registry->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 ) { + if ( $namespace ) { + list ( $block_namespace ) = explode( '/', $obj->name ); + + if ( $namespace !== $block_namespace ) { + continue; + } + } + $block_type = $this->prepare_item_for_response( $obj, $request ); + $data[] = $this->prepare_response_for_collection( $block_type ); + } + + return rest_ensure_response( $data ); + } + + /** + * Checks if a given request has access to read a block type. + * + * @since 5.5.0 + * + * @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 ) { + $check = $this->check_read_permission(); + if ( is_wp_error( $check ) ) { + return $check; + } + $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; + } + + /** + * Checks whether a given block type should be visible. + * + * @since 5.5.0 + * + * @return WP_Error|bool True if the block type is visible, WP_Error otherwise. + */ + protected function check_read_permission() { + if ( current_user_can( 'edit_posts' ) ) { + return true; + } + foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $post_type ) { + if ( current_user_can( $post_type->cap->edit_posts ) ) { + return true; + } + } + + return new WP_Error( 'rest_block_type_cannot_view', __( 'Sorry, you are not allowed to manage block types.' ), array( 'status' => rest_authorization_required_code() ) ); + } + + /** + * Get the block, if the name is valid. + * + * @since 5.5.0 + * + * @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_registry->get_registered( $name ); + if ( empty( $block_type ) ) { + return new WP_Error( 'rest_block_type_invalid', __( 'Invalid block type.' ), array( 'status' => 404 ) ); + } + + return $block_type; + } + + /** + * Retrieves a specific block type. + * + * @since 5.5.0 + * + * @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 = 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 ); + + return rest_ensure_response( $data ); + } + + /** + * Prepares a block type object for serialization. + * + * @since 5.5.0 + * + * @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. + */ + public function prepare_item_for_response( $block_type, $request ) { + + $fields = $this->get_fields_for_response( $request ); + $data = array(); + + if ( rest_is_field_included( 'attributes', $fields ) ) { + $data['attributes'] = $block_type->get_attributes(); + } + + if ( rest_is_field_included( 'is_dynamic', $fields ) ) { + $data['is_dynamic'] = $block_type->is_dynamic(); + } + + $schema = $this->get_item_schema(); + $extra_fields = array( + 'name', + 'title', + 'description', + 'icon', + 'category', + 'keywords', + 'parent', + 'provides_context', + 'uses_context', + 'supports', + 'styles', + 'textdomain', + 'example', + 'editor_script', + 'script', + 'editor_style', + 'style', + ); + foreach ( $extra_fields as $extra_field ) { + if ( rest_is_field_included( $extra_field, $fields ) ) { + if ( isset( $block_type->$extra_field ) ) { + $field = $block_type->$extra_field; + } elseif ( array_key_exists( 'default', $schema['properties'][ $extra_field ] ) ) { + $field = $schema['properties'][ $extra_field ]['default']; + } else { + $field = ''; + } + $data[ $extra_field ] = rest_sanitize_value_from_schema( $field, $schema['properties'][ $extra_field ] ); + } + } + + if ( rest_is_field_included( 'styles', $fields ) ) { + $styles = $this->style_registry->get_registered_styles_for_block( $block_type->name ); + $styles = array_values( $styles ); + $data['styles'] = wp_parse_args( $styles, $data['styles'] ); + $data['styles'] = array_filter( $data['styles'] ); + } + + $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( $this->prepare_links( $block_type ) ); + + /** + * Filters a block type returned from the REST API. + * + * Allows modification of the block type data right before it is returned. + * + * @since 5.5.0 + * + * @param WP_REST_Response $response The response object. + * @param WP_Block_Type $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 ); + } + + /** + * Prepares links for the request. + * + * @since 5.5.0 + * + * @param WP_Block_Type $block_type Block type data. + * @return array Links for the given block type. + */ + protected function prepare_links( $block_type ) { + list( $namespace ) = explode( '/', $block_type->name ); + + $links = array( + 'collection' => array( + 'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ), + ), + 'self' => array( + 'href' => rest_url( sprintf( '%s/%s/%s', $this->namespace, $this->rest_base, $block_type->name ) ), + ), + 'up' => array( + 'href' => rest_url( sprintf( '%s/%s/%s', $this->namespace, $this->rest_base, $namespace ) ), + ), + ); + + if ( $block_type->is_dynamic() ) { + $links['https://api.w.org/render-block'] = array( + 'href' => add_query_arg( 'context', 'edit', rest_url( sprintf( '%s/%s/%s', 'wp/v2', 'block-renderer', $block_type->name ) ) ), + ); + } + + return $links; + } + + /** + * Retrieves the block type' schema, conforming to JSON Schema. + * + * @since 5.5.0 + * + * @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( + 'title' => array( + 'description' => __( 'Title of block type.' ), + 'type' => 'string', + 'default' => '', + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), + 'name' => array( + 'description' => __( 'Unique name identifying the block type.' ), + 'type' => 'string', + 'default' => '', + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), + 'description' => array( + 'description' => __( 'Description of block type.' ), + 'type' => 'string', + 'default' => '', + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), + 'icon' => array( + 'description' => __( 'Icon of block type.' ), + 'type' => array( 'string', 'null' ), + 'default' => null, + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), + 'attributes' => array( + 'description' => __( 'Block attributes.' ), + 'type' => array( 'object', 'null' ), + 'properties' => array(), + 'default' => null, + 'additionalProperties' => array( + 'type' => 'object', + ), + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), + 'provides_context' => array( + 'description' => __( 'Context provided by blocks of this type.' ), + 'type' => 'object', + 'properties' => array(), + 'additionalProperties' => array( + 'type' => 'string', + ), + 'default' => array(), + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), + 'uses_context' => array( + 'description' => __( 'Context values inherited by blocks of this type.' ), + 'type' => 'array', + 'default' => array(), + 'items' => array( + 'type' => 'string', + ), + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), + 'supports' => array( + 'description' => __( 'Block supports.' ), + 'type' => 'object', + 'default' => array(), + 'properties' => array(), + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), + 'category' => array( + 'description' => __( 'Block category.' ), + 'type' => array( 'string', 'null' ), + 'default' => null, + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), + 'is_dynamic' => array( + 'description' => __( 'Is the block dynamically rendered.' ), + 'type' => 'boolean', + 'default' => false, + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), + 'editor_script' => array( + 'description' => __( 'Editor script handle.' ), + 'type' => array( 'string', 'null' ), + 'default' => null, + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), + 'script' => array( + 'description' => __( 'Public facing script handle.' ), + 'type' => array( 'string', 'null' ), + 'default' => null, + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), + 'editor_style' => array( + 'description' => __( 'Editor style handle.' ), + 'type' => array( 'string', 'null' ), + 'default' => null, + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), + 'style' => array( + 'description' => __( 'Public facing style handle.' ), + 'type' => array( 'string', 'null' ), + 'default' => null, + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), + 'styles' => array( + 'description' => __( 'Block style variations.' ), + 'type' => 'array', + 'items' => array( + 'type' => 'object', + 'properties' => array( + 'name' => array( + 'description' => __( 'Unique name identifying the style.' ), + 'type' => 'string', + 'required' => true, + ), + 'label' => array( + 'description' => __( 'The human-readable label for the style.' ), + 'type' => 'string', + ), + 'inline_style' => array( + 'description' => __( 'Inline CSS code that registers the CSS class required for the style.' ), + 'type' => 'string', + ), + 'style_handle' => array( + 'description' => __( 'Contains the handle that defines the block style.' ), + 'type' => 'string', + ), + ), + ), + 'default' => array(), + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), + 'textdomain' => array( + 'description' => __( 'Public text domain.' ), + 'type' => array( 'string', 'null' ), + 'default' => null, + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), + 'parent' => array( + 'description' => __( 'Parent blocks.' ), + 'type' => array( 'array', 'null' ), + 'items' => array( + 'type' => 'string', + ), + 'default' => null, + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), + 'keywords' => array( + 'description' => __( 'Block keywords.' ), + 'type' => 'array', + 'items' => array( + 'type' => 'string', + ), + 'default' => array(), + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), + 'example' => array( + 'description' => __( 'Block example.' ), + 'type' => array( 'object', 'null' ), + 'default' => null, + 'properties' => array( + 'attributes' => array( + 'description' => __( 'The attributes used in the example.' ), + 'type' => 'object', + ), + 'innerBlocks' => array( + 'description' => __( 'The list of inner blocks used in the example.' ), + 'type' => 'array', + 'items' => array( + 'type' => 'object', + 'properties' => array( + 'name' => array( + 'description' => __( 'The name of the inner block.' ), + 'type' => 'string', + ), + 'attributes' => array( + 'description' => __( 'The attributes of the inner block.' ), + 'type' => 'object', + ), + 'innerBlocks' => array( + 'description' => __( "A list of the inner block's own inner blocks. This is a recursive definition following the parent innerBlocks schema." ), + 'type' => 'array', + ), + ), + ), + ), + ), + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), + ), + ); + + $this->schema = $schema; + + return $this->add_additional_fields_schema( $this->schema ); + } + + /** + * Retrieves the query params for collections. + * + * @since 5.5.0 + * + * @return array Collection parameters. + */ + public function get_collection_params() { + return array( + 'context' => $this->get_context_param( array( 'default' => 'view' ) ), + 'namespace' => array( + 'description' => __( 'Block namespace.' ), + 'type' => 'string', + ), + ); + } + +} diff --git a/src/wp-settings.php b/src/wp-settings.php index 6fbef53916..b221012bcd 100644 --- a/src/wp-settings.php +++ b/src/wp-settings.php @@ -253,6 +253,7 @@ require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-users-controller.ph require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-comments-controller.php'; require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-search-controller.php'; require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-blocks-controller.php'; +require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-block-types-controller.php'; require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-block-renderer-controller.php'; require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-settings-controller.php'; require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-themes-controller.php'; diff --git a/tests/phpunit/tests/rest-api/rest-block-type-controller.php b/tests/phpunit/tests/rest-api/rest-block-type-controller.php new file mode 100644 index 0000000000..cac5b67f4d --- /dev/null +++ b/tests/phpunit/tests/rest-api/rest-block-type-controller.php @@ -0,0 +1,481 @@ +user->create( + array( + 'role' => 'administrator', + ) + ); + self::$subscriber_id = $factory->user->create( + array( + 'role' => 'subscriber', + ) + ); + + $name = 'fake/test'; + $settings = array( + 'icon' => 'text', + ); + + register_block_type( $name, $settings ); + } + + public static function wpTearDownAfterClass() { + self::delete_user( self::$admin_id ); + self::delete_user( self::$subscriber_id ); + unregister_block_type( 'fake/test' ); + } + + /** + * @ticket 47620 + */ + public function test_register_routes() { + $routes = rest_get_server()->get_routes(); + $this->assertArrayHasKey( '/wp/v2/block-types', $routes ); + $this->assertCount( 1, $routes['/wp/v2/block-types'] ); + $this->assertArrayHasKey( '/wp/v2/block-types/(?P[a-zA-Z0-9_-]+)', $routes ); + $this->assertCount( 1, $routes['/wp/v2/block-types/(?P[a-zA-Z0-9_-]+)'] ); + $this->assertArrayHasKey( '/wp/v2/block-types/(?P[a-zA-Z0-9_-]+)/(?P[a-zA-Z0-9_-]+)', $routes ); + $this->assertCount( 1, $routes['/wp/v2/block-types/(?P[a-zA-Z0-9_-]+)/(?P[a-zA-Z0-9_-]+)'] ); + } + + /** + * @ticket 47620 + */ + public function test_context_param() { + // Collection. + $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/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'] ); + // Single. + $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/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'] ); + } + + /** + * @ticket 47620 + */ + public function test_get_items() { + $block_name = 'fake/test'; + wp_set_current_user( self::$admin_id ); + $request = new WP_REST_Request( 'GET', '/wp/v2/block-types/fake' ); + $response = rest_get_server()->dispatch( $request ); + $data = $response->get_data(); + $this->assertCount( 1, $data ); + $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block_name ); + $this->check_block_type_object( $block_type, $data[0], $data[0]['_links'] ); + } + + /** + * @ticket 47620 + */ + public function test_get_item() { + $block_name = 'fake/test'; + wp_set_current_user( self::$admin_id ); + $request = new WP_REST_Request( 'GET', '/wp/v2/block-types/' . $block_name ); + $response = rest_get_server()->dispatch( $request ); + $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() ); + } + + /** + * @ticket 47620 + */ + public function test_get_item_with_styles() { + $block_name = 'fake/styles'; + $block_styles = array( + 'name' => 'fancy-quote', + 'label' => 'Fancy Quote', + 'style_handle' => 'myguten-style', + ); + register_block_type( $block_name ); + register_block_style( $block_name, $block_styles ); + wp_set_current_user( self::$admin_id ); + $request = new WP_REST_Request( 'GET', '/wp/v2/block-types/' . $block_name ); + $response = rest_get_server()->dispatch( $request ); + $data = $response->get_data(); + $this->assertEqualSets( array( $block_styles ), $data['styles'] ); + + } + + /** + * @ticket 47620 + */ + public function test_get_item_with_styles_merge() { + $block_name = 'fake/styles2'; + $block_styles = array( + 'name' => 'fancy-quote', + 'label' => 'Fancy Quote', + 'style_handle' => 'myguten-style', + ); + $settings = array( + 'styles' => array( + array( + 'name' => 'blue-quote', + 'label' => 'Blue Quote', + 'style_handle' => 'myguten-style', + ), + ), + ); + register_block_type( $block_name, $settings ); + register_block_style( $block_name, $block_styles ); + wp_set_current_user( self::$admin_id ); + $request = new WP_REST_Request( 'GET', '/wp/v2/block-types/' . $block_name ); + $response = rest_get_server()->dispatch( $request ); + $data = $response->get_data(); + $expected = array( + array( + 'name' => 'fancy-quote', + 'label' => 'Fancy Quote', + 'style_handle' => 'myguten-style', + ), + array( + 'name' => 'blue-quote', + 'label' => 'Blue Quote', + 'style_handle' => 'myguten-style', + ), + ); + $this->assertEqualSets( $expected, $data['styles'] ); + + } + + /** + * @ticket 47620 + */ + public function test_get_block_invalid_name() { + $block_type = 'fake/block'; + wp_set_current_user( self::$admin_id ); + $request = new WP_REST_Request( 'GET', '/wp/v2/block-types/' . $block_type ); + $response = rest_get_server()->dispatch( $request ); + + $this->assertErrorResponse( 'rest_block_type_invalid', $response, 404 ); + } + + /** + * @ticket 47620 + */ + public function test_get_item_invalid() { + $block_type = 'fake/invalid'; + $settings = array( + 'title' => true, + 'description' => true, + 'icon' => true, + 'attributes' => 'invalid_attributes', + 'provides_context' => 'invalid_provides_context', + 'uses_context' => 'invalid_uses_context', + 'category' => true, + 'editor_script' => true, + 'script' => true, + 'editor_style' => true, + 'style' => true, + 'keywords' => 'invalid_keywords', + 'example' => 'invalid_example', + 'parent' => 'invalid_parent', + 'supports' => 'invalid_supports', + 'styles' => 'invalid_styles', + 'render_callback' => 'invalid_callback', + 'textdomain' => true, + ); + register_block_type( $block_type, $settings ); + wp_set_current_user( self::$admin_id ); + $request = new WP_REST_Request( 'GET', '/wp/v2/block-types/' . $block_type ); + $response = rest_get_server()->dispatch( $request ); + $data = $response->get_data(); + $this->assertEquals( $block_type, $data['name'] ); + $this->assertEquals( '1', $data['title'] ); + $this->assertEquals( '1', $data['description'] ); + $this->assertEquals( null, $data['icon'] ); + $this->assertEquals( null, $data['editor_script'] ); + $this->assertEquals( null, $data['script'] ); + $this->assertEquals( null, $data['editor_style'] ); + $this->assertEquals( null, $data['style'] ); + $this->assertEqualSets( array(), $data['provides_context'] ); + $this->assertEqualSets( array(), $data['attributes'] ); + $this->assertEqualSets( array( 'invalid_uses_context' ), $data['uses_context'] ); + $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->assertEquals( null, $data['example'] ); + $this->assertEquals( null, $data['category'] ); + $this->assertEquals( null, $data['textdomain'] ); + $this->assertFalse( false, $data['is_dynamic'] ); + } + + /** + * @ticket 47620 + */ + public function test_get_item_defaults() { + $block_type = 'fake/false'; + $settings = array( + 'title' => false, + 'description' => false, + 'icon' => false, + 'attributes' => false, + 'provides_context' => false, + 'uses_context' => false, + 'category' => false, + 'editor_script' => false, + 'script' => false, + 'editor_style' => false, + 'style' => false, + 'keywords' => false, + 'parent' => false, + 'supports' => false, + 'styles' => false, + 'render_callback' => false, + 'textdomain' => false, + 'example' => false, + ); + register_block_type( $block_type, $settings ); + wp_set_current_user( self::$admin_id ); + $request = new WP_REST_Request( 'GET', '/wp/v2/block-types/' . $block_type ); + $response = rest_get_server()->dispatch( $request ); + $data = $response->get_data(); + $this->assertEquals( $block_type, $data['name'] ); + $this->assertEquals( '', $data['title'] ); + $this->assertEquals( '', $data['description'] ); + $this->assertEquals( null, $data['icon'] ); + $this->assertEquals( null, $data['editor_script'] ); + $this->assertEquals( null, $data['script'] ); + $this->assertEquals( null, $data['editor_style'] ); + $this->assertEquals( null, $data['style'] ); + $this->assertEqualSets( array(), $data['attributes'] ); + $this->assertEqualSets( array(), $data['provides_context'] ); + $this->assertEqualSets( array(), $data['uses_context'] ); + $this->assertEqualSets( array(), $data['keywords'] ); + $this->assertEqualSets( array(), $data['parent'] ); + $this->assertEqualSets( array(), $data['supports'] ); + $this->assertEqualSets( array(), $data['styles'] ); + $this->assertEquals( null, $data['example'] ); + $this->assertEquals( null, $data['category'] ); + $this->assertEquals( null, $data['example'] ); + $this->assertEquals( null, $data['textdomain'] ); + $this->assertFalse( false, $data['is_dynamic'] ); + } + + /** + * @ticket 47620 + */ + public function test_get_item_schema() { + wp_set_current_user( self::$admin_id ); + $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/block-types' ); + $response = rest_get_server()->dispatch( $request ); + $data = $response->get_data(); + $properties = $data['schema']['properties']; + $this->assertCount( 19, $properties ); + $this->assertArrayHasKey( 'title', $properties ); + $this->assertArrayHasKey( 'icon', $properties ); + $this->assertArrayHasKey( 'description', $properties ); + $this->assertArrayHasKey( 'keywords', $properties ); + $this->assertArrayHasKey( 'styles', $properties ); + $this->assertArrayHasKey( 'textdomain', $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 ); + $this->assertArrayHasKey( 'parent', $properties ); + $this->assertArrayHasKey( 'example', $properties ); + $this->assertArrayHasKey( 'uses_context', $properties ); + $this->assertArrayHasKey( 'provides_context', $properties ); + } + + /** + * @ticket 47620 + */ + public function test_get_items_wrong_permission() { + wp_set_current_user( self::$subscriber_id ); + $request = new WP_REST_Request( 'GET', '/wp/v2/block-types' ); + $response = rest_get_server()->dispatch( $request ); + $this->assertErrorResponse( 'rest_block_type_cannot_view', $response, 403 ); + } + + /** + * @ticket 47620 + */ + public function test_get_item_wrong_permission() { + wp_set_current_user( self::$subscriber_id ); + $request = new WP_REST_Request( 'GET', '/wp/v2/block-types/fake/test' ); + $response = rest_get_server()->dispatch( $request ); + $this->assertErrorResponse( 'rest_block_type_cannot_view', $response, 403 ); + } + + /** + * @ticket 47620 + */ + public function test_get_items_no_permission() { + wp_set_current_user( 0 ); + $request = new WP_REST_Request( 'GET', '/wp/v2/block-types' ); + $response = rest_get_server()->dispatch( $request ); + $this->assertErrorResponse( 'rest_block_type_cannot_view', $response, 401 ); + } + + /** + * @ticket 47620 + */ + public function test_get_item_no_permission() { + wp_set_current_user( 0 ); + $request = new WP_REST_Request( 'GET', '/wp/v2/block-types/fake/test' ); + $response = rest_get_server()->dispatch( $request ); + $this->assertErrorResponse( 'rest_block_type_cannot_view', $response, 401 ); + } + + /** + * @ticket 47620 + */ + 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() ); + } + + /** + * @ticket 47620 + */ + 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. + * + * @since 5.5.0 + * + * @param WP_Block_Type $block_type Sample block type. + * @param array $data Data to compare against. + * @param array $links Links to compare again. + */ + protected function check_block_type_object( $block_type, $data, $links ) { + // Test data. + $this->assertEquals( $data['attributes'], $block_type->get_attributes() ); + $this->assertEquals( $data['is_dynamic'], $block_type->is_dynamic() ); + + $extra_fields = array( + 'name', + 'category', + 'editor_script', + 'script', + 'editor_style', + 'style', + 'title', + 'icon', + 'description', + 'keywords', + 'parent', + 'provides_context', + 'uses_context', + 'supports', + 'styles', + 'textdomain', + 'example', + ); + + foreach ( $extra_fields as $extra_field ) { + if ( isset( $block_type->$extra_field ) ) { + $this->assertEquals( $data[ $extra_field ], $block_type->$extra_field ); + } + } + + // Test links. + $this->assertEquals( rest_url( 'wp/v2/block-types' ), $links['collection'][0]['href'] ); + $this->assertEquals( rest_url( 'wp/v2/block-types/' . $block_type->name ), $links['self'][0]['href'] ); + if ( $block_type->is_dynamic() ) { + $this->assertArrayHasKey( 'https://api.w.org/render-block', $links ); + } + } + + /** + * 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() {} +} diff --git a/tests/phpunit/tests/rest-api/rest-schema-setup.php b/tests/phpunit/tests/rest-api/rest-schema-setup.php index 3561314dbe..7c3d04dc67 100644 --- a/tests/phpunit/tests/rest-api/rest-schema-setup.php +++ b/tests/phpunit/tests/rest-api/rest-schema-setup.php @@ -121,6 +121,9 @@ class WP_Test_REST_Schema_Initialization extends WP_Test_REST_TestCase { '/wp/v2/comments/(?P[\\d]+)', '/wp/v2/search', '/wp/v2/block-renderer/(?P[a-z0-9-]+/[a-z0-9-]+)', + '/wp/v2/block-types', + '/wp/v2/block-types/(?P[a-zA-Z0-9_-]+)', + '/wp/v2/block-types/(?P[a-zA-Z0-9_-]+)/(?P[a-zA-Z0-9_-]+)', '/wp/v2/settings', '/wp/v2/themes', ); diff --git a/tests/qunit/fixtures/wp-api-generated.js b/tests/qunit/fixtures/wp-api-generated.js index 2598fb894f..851f42627f 100644 --- a/tests/qunit/fixtures/wp-api-generated.js +++ b/tests/qunit/fixtures/wp-api-generated.js @@ -4436,6 +4436,111 @@ mockedApiResponse.Schema = { } ] }, + "/wp/v2/block-types": { + "namespace": "wp/v2", + "methods": [ + "GET" + ], + "endpoints": [ + { + "methods": [ + "GET" + ], + "args": { + "context": { + "required": false, + "default": "view", + "enum": [ + "view", + "embed", + "edit" + ], + "description": "Scope under which the request is made; determines fields present in response.", + "type": "string" + }, + "namespace": { + "required": false, + "description": "Block namespace.", + "type": "string" + } + } + } + ], + "_links": { + "self": [ + { + "href": "http://example.org/index.php?rest_route=/wp/v2/block-types" + } + ] + } + }, + "/wp/v2/block-types/(?P[a-zA-Z0-9_-]+)": { + "namespace": "wp/v2", + "methods": [ + "GET" + ], + "endpoints": [ + { + "methods": [ + "GET" + ], + "args": { + "context": { + "required": false, + "default": "view", + "enum": [ + "view", + "embed", + "edit" + ], + "description": "Scope under which the request is made; determines fields present in response.", + "type": "string" + }, + "namespace": { + "required": false, + "description": "Block namespace.", + "type": "string" + } + } + } + ] + }, + "/wp/v2/block-types/(?P[a-zA-Z0-9_-]+)/(?P[a-zA-Z0-9_-]+)": { + "namespace": "wp/v2", + "methods": [ + "GET" + ], + "endpoints": [ + { + "methods": [ + "GET" + ], + "args": { + "name": { + "required": false, + "description": "Block name", + "type": "string" + }, + "namespace": { + "required": false, + "description": "Block namespace", + "type": "string" + }, + "context": { + "required": false, + "default": "view", + "enum": [ + "view", + "embed", + "edit" + ], + "description": "Scope under which the request is made; determines fields present in response.", + "type": "string" + } + } + } + ] + }, "/wp/v2/settings": { "namespace": "wp/v2", "methods": [