<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[48173] trunk: REST API: Introduce Block Types endpoint.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/48173">48173</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/48173","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>TimothyBlynJacobs</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2020-06-26 00:44:44 +0000 (Fri, 26 Jun 2020)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>REST API: Introduce Block Types endpoint.

This endpoint allows a user to retrieve the block type definition for all server-side registered block types.

Props spacedmonkey, aduth, gziolo, ocean90, TimothyBlynJacobs.
Fixes <a href="https://core.trac.wordpress.org/ticket/47620">#47620</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesclasswpblockstylesregistryphp">trunk/src/wp-includes/class-wp-block-styles-registry.php</a></li>
<li><a href="#trunksrcwpincludesrestapiphp">trunk/src/wp-includes/rest-api.php</a></li>
<li><a href="#trunksrcwpsettingsphp">trunk/src/wp-settings.php</a></li>
<li><a href="#trunktestsphpunittestsrestapirestschemasetupphp">trunk/tests/phpunit/tests/rest-api/rest-schema-setup.php</a></li>
<li><a href="#trunktestsqunitfixtureswpapigeneratedjs">trunk/tests/qunit/fixtures/wp-api-generated.js</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesrestapiendpointsclasswprestblocktypescontrollerphp">trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php</a></li>
<li><a href="#trunktestsphpunittestsrestapirestblocktypecontrollerphp">trunk/tests/phpunit/tests/rest-api/rest-block-type-controller.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesclasswpblockstylesregistryphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-wp-block-styles-registry.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-block-styles-registry.php  2020-06-26 00:25:15 UTC (rev 48172)
+++ trunk/src/wp-includes/class-wp-block-styles-registry.php    2020-06-26 00:44:44 UTC (rev 48173)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -34,7 +34,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 5.3.0
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @param string $block_name       Block type name including namespace.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @param string $block_name       Block ty name including namespace.
</ins><span class="cx" style="display: block; padding: 0 10px">          * @param array  $style_properties Array containing the properties of the style name, label,
</span><span class="cx" style="display: block; padding: 0 10px">         *                                 style (name of the stylesheet to be enqueued),
</span><span class="cx" style="display: block; padding: 0 10px">         *                                 inline_style (string containing the CSS to be added).
</span></span></pre></div>
<a id="trunksrcwpincludesrestapiendpointsclasswprestblocktypescontrollerphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php                         (rev 0)
+++ trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php   2020-06-26 00:44:44 UTC (rev 48173)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,593 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * REST API: WP_REST_Block_Types_Controller class
+ *
+ * @package WordPress
+ * @subpackage REST_API
+ * @since 5.5.0
+ */
+
+/**
+ * Core class used to access block types via the REST API.
+ *
+ * @since 5.5.0
+ *
+ * @see WP_REST_Controller
+ */
+class WP_REST_Block_Types_Controller extends WP_REST_Controller {
+
+       /**
+        * Instance of WP_Block_Type_Registry.
+        *
+        * @since 5.5.0
+        * @var WP_Block_Type_Registry
+        */
+       protected $block_registry;
+
+       /**
+        * Instance of WP_Block_Styles_Registry.
+        *
+        * @since 5.5.0
+        * @var WP_Block_Styles_Registry
+        */
+       protected $style_registry;
+
+       /**
+        * Constructor.
+        *
+        * @since 5.5.0
+        */
+       public function __construct() {
+               $this->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<namespace>[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<namespace>[a-zA-Z0-9_-]+)/(?P<name>[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',
+                       ),
+               );
+       }
+
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="trunksrcwpincludesrestapiphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/rest-api.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/rest-api.php        2020-06-26 00:25:15 UTC (rev 48172)
+++ trunk/src/wp-includes/rest-api.php  2020-06-26 00:44:44 UTC (rev 48173)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -273,6 +273,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">        $controller = new WP_REST_Block_Renderer_Controller;
</span><span class="cx" style="display: block; padding: 0 10px">        $controller->register_routes();
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        // Block Types.
+       $controller = new WP_REST_Block_Types_Controller();
+       $controller->register_routes();
+
</ins><span class="cx" style="display: block; padding: 0 10px">         // Settings.
</span><span class="cx" style="display: block; padding: 0 10px">        $controller = new WP_REST_Settings_Controller;
</span><span class="cx" style="display: block; padding: 0 10px">        $controller->register_routes();
</span></span></pre></div>
<a id="trunksrcwpsettingsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-settings.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-settings.php 2020-06-26 00:25:15 UTC (rev 48172)
+++ trunk/src/wp-settings.php   2020-06-26 00:44:44 UTC (rev 48173)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -253,6 +253,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-comments-controller.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-search-controller.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-blocks-controller.php';
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-block-types-controller.php';
</ins><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-block-renderer-controller.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-settings-controller.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-themes-controller.php';
</span></span></pre></div>
<a id="trunktestsphpunittestsrestapirestblocktypecontrollerphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/rest-api/rest-block-type-controller.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/rest-api/rest-block-type-controller.php                         (rev 0)
+++ trunk/tests/phpunit/tests/rest-api/rest-block-type-controller.php   2020-06-26 00:44:44 UTC (rev 48173)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,481 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * WP_REST_Block_Types_Controller tests.
+ *
+ * @package WordPress
+ * @subpackage REST_API
+ * @since 5.5.0
+ */
+
+/**
+ * Tests for WP_REST_Block_Types_Controller.
+ *
+ * @since 5.5.0
+ *
+ * @covers WP_REST_Block_Types_Controller
+ *
+ * @group restapi-blocks
+ * @group restapi
+ */
+class REST_Block_Type_Controller_Test extends WP_Test_REST_Controller_Testcase {
+
+       /**
+        * Admin user ID.
+        *
+        * @since 5.5.0
+        *
+        * @var int $subscriber_id
+        */
+       protected static $admin_id;
+
+       /**
+        * Subscriber user ID.
+        *
+        * @since 5.5.0
+        *
+        * @var int $subscriber_id
+        */
+       protected static $subscriber_id;
+
+       /**
+        * Create fake data before our tests run.
+        *
+        * @since 5.5.0
+        *
+        * @param WP_UnitTest_Factory $factory Helper that lets us create fake data.
+        */
+       public static function wpSetUpBeforeClass( $factory ) {
+               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',
+               );
+
+               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<namespace>[a-zA-Z0-9_-]+)', $routes );
+               $this->assertCount( 1, $routes['/wp/v2/block-types/(?P<namespace>[a-zA-Z0-9_-]+)'] );
+               $this->assertArrayHasKey( '/wp/v2/block-types/(?P<namespace>[a-zA-Z0-9_-]+)/(?P<name>[a-zA-Z0-9_-]+)', $routes );
+               $this->assertCount( 1, $routes['/wp/v2/block-types/(?P<namespace>[a-zA-Z0-9_-]+)/(?P<name>[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() {}
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/tests/rest-api/rest-block-type-controller.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="trunktestsphpunittestsrestapirestschemasetupphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/rest-api/rest-schema-setup.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/rest-api/rest-schema-setup.php  2020-06-26 00:25:15 UTC (rev 48172)
+++ trunk/tests/phpunit/tests/rest-api/rest-schema-setup.php    2020-06-26 00:44:44 UTC (rev 48173)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -121,6 +121,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        '/wp/v2/comments/(?P<id>[\\d]+)',
</span><span class="cx" style="display: block; padding: 0 10px">                        '/wp/v2/search',
</span><span class="cx" style="display: block; padding: 0 10px">                        '/wp/v2/block-renderer/(?P<name>[a-z0-9-]+/[a-z0-9-]+)',
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        '/wp/v2/block-types',
+                       '/wp/v2/block-types/(?P<namespace>[a-zA-Z0-9_-]+)',
+                       '/wp/v2/block-types/(?P<namespace>[a-zA-Z0-9_-]+)/(?P<name>[a-zA-Z0-9_-]+)',
</ins><span class="cx" style="display: block; padding: 0 10px">                         '/wp/v2/settings',
</span><span class="cx" style="display: block; padding: 0 10px">                        '/wp/v2/themes',
</span><span class="cx" style="display: block; padding: 0 10px">                );
</span></span></pre></div>
<a id="trunktestsqunitfixtureswpapigeneratedjs"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/qunit/fixtures/wp-api-generated.js</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/qunit/fixtures/wp-api-generated.js    2020-06-26 00:25:15 UTC (rev 48172)
+++ trunk/tests/qunit/fixtures/wp-api-generated.js      2020-06-26 00:44:44 UTC (rev 48173)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -4436,6 +4436,111 @@
</span><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px">             ]
</span><span class="cx" style="display: block; padding: 0 10px">         },
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        "/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<namespace>[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<namespace>[a-zA-Z0-9_-]+)/(?P<name>[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"
+                        }
+                    }
+                }
+            ]
+        },
</ins><span class="cx" style="display: block; padding: 0 10px">         "/wp/v2/settings": {
</span><span class="cx" style="display: block; padding: 0 10px">             "namespace": "wp/v2",
</span><span class="cx" style="display: block; padding: 0 10px">             "methods": [
</span></span></pre>
</div>
</div>

</body>
</html>