Skip to content

Commit

Permalink
Add fields to WP_Block_Type
Browse files Browse the repository at this point in the history
As part of #47620 and the RFC for block registeration. Server registered blocks are missing some fields. These changeset includes them.

Props spacedmonkey, aduth.

Fixes #48529.



git-svn-id: https://develop.svn.wordpress.org/trunk@47875 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
gziolo committed Jun 1, 2020
1 parent e0ede89 commit 822ca9e
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/wp-admin/includes/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -2234,7 +2234,7 @@ function get_block_categories( $post ) {
function get_block_editor_server_block_settings() {
$block_registry = WP_Block_Type_Registry::get_instance();
$blocks = array();
$keys_to_pick = array( 'title', 'description', 'icon', 'category', 'keywords', 'parent', 'supports', 'attributes', 'styles' );
$keys_to_pick = array( 'title', 'description', 'icon', 'category', 'keywords', 'parent', 'supports', 'attributes', 'styles', 'textdomain', 'example' );

foreach ( $block_registry->get_all_registered() as $block_name => $block_type ) {
foreach ( $keys_to_pick as $key ) {
Expand Down
84 changes: 73 additions & 11 deletions src/wp-includes/class-wp-block-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* @see register_block_type()
*/
class WP_Block_Type {

/**
* Block type key.
*
Expand All @@ -23,66 +24,127 @@ class WP_Block_Type {
*/
public $name;

/**
* @since 5.5.0
* @var string
*/
public $title = '';

/**
* @since 5.5.0
* @var string
*/
public $category = '';

/**
* @since 5.5.0
* @var array|null
*/
public $parent = null;

/**
* @since 5.5.0
* @var string
*/
public $icon = '';

/**
* @since 5.5.0
* @var string
*/
public $description = '';

/**
* @since 5.5.0
* @var array
*/
public $keywords = array();

/**
* @since 5.5.0
* @var string|null
*/
public $textdomain = null;

/**
* @since 5.5.0
* @var array
*/
public $styles = array();

/**
* @since 5.5.0
* @var array
*/
public $supports = array();

/**
* @since 5.5.0
* @var array|null
*/
public $example = null;

/**
* Block type render callback.
*
* @since 5.0.0
* @var callable
*/
public $render_callback;
public $render_callback = null;

/**
* Block type attributes property schemas.
*
* @since 5.0.0
* @var array
* @var array|null
*/
public $attributes;
public $attributes = null;

/**
* Block type editor script handle.
*
* @since 5.0.0
* @var string
*/
public $editor_script;
public $editor_script = '';

/**
* Block type front end script handle.
*
* @since 5.0.0
* @var string
*/
public $script;
public $script = '';

/**
* Block type editor style handle.
*
* @since 5.0.0
* @var string
*/
public $editor_style;
public $editor_style = '';

/**
* Block type front end style handle.
*
* @since 5.0.0
* @var string
*/
public $style;
public $style = '';

/**
* Constructor.
*
* Will populate object properties from the provided arguments.
*
* @since 5.0.0
*
* @see register_block_type()
*
* @param string $block_type Block type name including namespace.
* @param array|string $args Optional. Array or string of arguments for registering a block type.
* Default empty array.
*
* @since 5.0.0
*
* @see register_block_type()
*
*/
public function __construct( $block_type, $args = array() ) {
$this->name = $block_type;
Expand Down
11 changes: 10 additions & 1 deletion tests/phpunit/tests/admin/includesPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,7 @@ function test_use_block_editor_for_post() {
function test_get_block_editor_server_block_settings() {
$name = 'core/test';
$settings = array(
'category' => 'common',
'icon' => 'text',
'render_callback' => 'foo',
);
Expand All @@ -836,7 +837,15 @@ function test_get_block_editor_server_block_settings() {
unregister_block_type( $name );

$this->assertArrayHasKey( $name, $blocks );
$this->assertSame( array( 'icon' => 'text' ), $blocks[ $name ] );
$this->assertEquals( array(
'title' => '',
'description' => '',
'category' => 'common',
'icon' => 'text',
'keywords' => array(),
'supports' => array(),
'styles' => array(),
), $blocks[ $name ] );
}

/**
Expand Down
25 changes: 25 additions & 0 deletions tests/phpunit/tests/blocks/block-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,31 @@ public function render_fake_block_with_content( $attributes, $content ) {
return json_encode( $attributes );
}

/**
* @ticket 48529
*/
public function test_register_block() {
$block_type = new WP_Block_Type( 'core/fake', array(
'title' => 'Test title',
'category' => 'Test category',
'parent' => array( 'core/third-party' ),
'icon' => 'icon.png',
'description' => 'test description',
'keywords' => array( 'test keyword' ),
'textdomain' => 'test_domain',
'supports' => array( 'alignment' => true ),
) );

$this->assertSame( 'Test title', $block_type->title );
$this->assertSame( 'Test category', $block_type->category );
$this->assertEqualSets( array( 'core/third-party' ), $block_type->parent );
$this->assertSame( 'icon.png', $block_type->icon );
$this->assertSame( 'test description', $block_type->description );
$this->assertEqualSets( array( 'test keyword' ), $block_type->keywords );
$this->assertSame( 'test_domain', $block_type->textdomain );
$this->assertEqualSets( array( 'alignment' => true ), $block_type->supports );
}

/**
* Testing the block version.
*
Expand Down

0 comments on commit 822ca9e

Please sign in to comment.