Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
4c0c2dc
Add block types REST API.
spacedmonkey Mar 22, 2020
32b995d
Remove space
spacedmonkey May 7, 2020
4ec1eea
Change comment.
spacedmonkey May 7, 2020
81ae460
Change comment.
spacedmonkey May 7, 2020
1be9aaa
Change title.
spacedmonkey May 7, 2020
371ce8d
Change comment.
spacedmonkey May 8, 2020
9afefda
Remove array_shift.
spacedmonkey May 8, 2020
348ff88
Change comment to 5.5.0
spacedmonkey May 8, 2020
df16a9f
Fixes.
spacedmonkey May 8, 2020
98b5e99
Formatting.
spacedmonkey May 8, 2020
a86db36
Merge branch 'master' into feautre/block-api
spacedmonkey May 9, 2020
c83d05b
More tweaks
spacedmonkey May 9, 2020
730acc5
Fix lints
spacedmonkey May 9, 2020
3905157
Tweak fields.
spacedmonkey May 13, 2020
8f32a72
Feedback.
spacedmonkey May 14, 2020
3479ffc
Add block category.
spacedmonkey May 20, 2020
acf6cbe
Feedback
spacedmonkey May 21, 2020
bfc2e07
Add back composer.lock.
spacedmonkey May 21, 2020
0e89f71
Merge branch 'master' into feautre/block-api
spacedmonkey May 21, 2020
ce8ea79
Tweak descriptions.
spacedmonkey May 21, 2020
e75b647
Add supports.
spacedmonkey May 21, 2020
6c2b6a6
Add unit tests.
spacedmonkey May 21, 2020
9eea8a3
More tweaks
spacedmonkey May 21, 2020
f11b18d
Fix tests
spacedmonkey May 22, 2020
a45c48c
Tweak test.
spacedmonkey May 22, 2020
dbcca03
Reuse fake block
spacedmonkey May 22, 2020
0ea4d71
Add in new fields and improve tests.
spacedmonkey May 22, 2020
50efa40
Fix lint
spacedmonkey May 22, 2020
f9878fd
Reuse wp_error.
spacedmonkey May 22, 2020
b9675fd
Add more tests
spacedmonkey May 22, 2020
62f604e
Improve asserts.
spacedmonkey May 24, 2020
6db18c8
Refactor permision check
spacedmonkey May 25, 2020
fcb6a1d
Add more tests.
spacedmonkey May 25, 2020
296efe6
Remove unused method.
spacedmonkey May 25, 2020
b98d04c
Fix tests.
spacedmonkey May 25, 2020
bfd9650
Use correct name.
spacedmonkey May 25, 2020
2744ebf
Improve tests.
spacedmonkey May 25, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
More tweaks
  • Loading branch information
spacedmonkey committed May 9, 2020
commit c83d05ba16cf16dea4c8ffa4926fc8b019ee7d76
51 changes: 35 additions & 16 deletions lib/class-wp-rest-block-types-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ class WP_REST_Block_Types_Controller extends WP_REST_Controller {
*
* @var WP_Block_Type_Registry
*/
protected $block_type_registry;
protected $registry;

/**
* Constructor.
*/
public function __construct() {
$this->namespace = '__experimental';
$this->rest_base = 'block-types';
$this->block_type_registry = WP_Block_Type_Registry::get_instance();
$this->namespace = '__experimental';
$this->rest_base = 'block-types';
$this->registry = WP_Block_Type_Registry::get_instance();
}

/**
Expand Down Expand Up @@ -102,7 +102,7 @@ public function get_items_permissions_check( $request ) { // phpcs:ignore Variab
*/
public function get_items( $request ) {
$data = array();
$block_types = $this->block_type_registry->get_all_registered();
$block_types = $this->registry->get_all_registered();

// Retrieve the list of registered collection query parameters.
$registered = $this->get_collection_params();
Expand Down Expand Up @@ -167,7 +167,7 @@ protected function check_read_permission() {
* @return WP_Block_Type|WP_Error Block type object if name is valid, WP_Error otherwise.
*/
protected function get_block( $name ) {
$block_type = $this->block_type_registry->get_registered( $name );
$block_type = $this->registry->get_registered( $name );
if ( empty( $block_type ) ) {
return new WP_Error( 'rest_block_type_invalid', __( 'Invalid block type.', 'gutenberg' ), array( 'status' => 404 ) );
}
Expand Down Expand Up @@ -230,16 +230,7 @@ public function prepare_item_for_response( $block_type, $request ) {

$response = rest_ensure_response( $data );

$response->add_links(
array(
'collection' => array(
'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
),
'https://api.w.org/items' => array(
'href' => rest_url( sprintf( '%s/%s/%s', $this->namespace, $this->rest_base, $block_type->name ) ),
),
)
);
$response->add_links( $this->prepare_links( $block_type ) );

/**
* Filters a block type returned from the REST API.
Expand All @@ -253,6 +244,25 @@ public function prepare_item_for_response( $block_type, $request ) {
return apply_filters( 'rest_prepare_block_type', $response, $block_type, $request );
}

/**
* Prepares links for the request.
*
* @param WP_Block_Type $block_type block type data.
* @return array Links for the given block type.
*/
protected function prepare_links( $block_type ) {
$links = array(
'collection' => array(
'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
),
'about' => array(
'href' => rest_url( sprintf( '%s/%s/%s', $this->namespace, $this->rest_base, $block_type->name ) ),
),
);

return $links;
}

/**
* Retrieves the block type' schema, conforming to JSON Schema.
*
Expand All @@ -277,6 +287,15 @@ public function get_item_schema() {
'attributes' => array(
'description' => __( 'Block attributes.', 'gutenberg' ),
'type' => 'object',
'properties' => array(
'layout' => array(
'description' => __( 'Block layout.', 'gutenberg' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
),
'additionalProperties' => true,
'context' => array( 'embed', 'view', 'edit' ),
'readonly' => true,
),
Expand Down