Skip to content

Commit

Permalink
Add block styles. (#22680)
Browse files Browse the repository at this point in the history
* Add block styles.

* Test merge.
  • Loading branch information
spacedmonkey authored May 29, 2020
1 parent 9f45d0b commit e2c7bb2
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 6 deletions.
26 changes: 20 additions & 6 deletions lib/class-wp-rest-block-types-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,23 @@ class WP_REST_Block_Types_Controller extends WP_REST_Controller {
*
* @var WP_Block_Type_Registry
*/
protected $registry;
protected $block_registry;

/**
* Instance of WP_Block_Styles_Registry.
*
* @var WP_Block_Styles_Registry
*/
protected $style_registry;

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

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

// Retrieve the list of registered collection query parameters.
$registered = $this->get_collection_params();
Expand Down Expand Up @@ -182,7 +190,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->registry->get_registered( $name );
$block_type = $this->block_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 @@ -259,6 +267,12 @@ public function prepare_item_for_response( $block_type, $request ) {
}
}

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'] );
}

$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );
Expand Down
61 changes: 61 additions & 0 deletions phpunit/class-wp-rest-block-types-controller-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,67 @@ public function test_get_item() {
$this->check_block_type_object( $block_type, $response->get_data(), $response->get_links() );
}

/**
*
*/
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', '/__experimental/block-types/' . $block_name );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertEqualSets( array( $block_styles ), $data['styles'] );

}

/**
*
*/
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(
'styleVariations' => 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', '/__experimental/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'] );

}

public function test_get_block_invalid_name() {
$block_type = 'fake/block';
wp_set_current_user( self::$admin_id );
Expand Down

0 comments on commit e2c7bb2

Please sign in to comment.