Skip to content

Commit

Permalink
Blocks: Add context fields to WP_Block_Type
Browse files Browse the repository at this point in the history
New block context related fields were added as part of WordPress/gutenberg#22686. This changest backports them to WP_Block_Type class.

Props aduth, spacedmonkey, mcsf, epiqueras.
Fixes #47656.  



git-svn-id: https://develop.svn.wordpress.org/trunk@48117 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
gziolo authored and donmhico committed Jun 26, 2020
1 parent 434d4f4 commit aa64dea
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
22 changes: 18 additions & 4 deletions src/wp-admin/includes/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -2234,19 +2234,33 @@ 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', 'textdomain', 'example' );
$fields_to_pick = array(
'title' => 'title',
'description' => 'description',
'icon' => 'icon',
'category' => 'category',
'keywords' => 'keywords',
'parent' => 'parent',
'supports' => 'supports',
'attributes' => 'attributes',
'provides_context' => 'providesContext',
'uses_context' => 'usesContext',
'styles' => 'styles',
'textdomain' => 'textdomain',
'example' => 'example',
);

foreach ( $block_registry->get_all_registered() as $block_name => $block_type ) {
foreach ( $keys_to_pick as $key ) {
if ( ! isset( $block_type->{ $key } ) ) {
foreach ( $fields_to_pick as $field => $key ) {
if ( ! isset( $block_type->{ $field } ) ) {
continue;
}

if ( ! isset( $blocks[ $block_name ] ) ) {
$blocks[ $block_name ] = array();
}

$blocks[ $block_name ][ $key ] = $block_type->{ $key };
$blocks[ $block_name ][ $key ] = $block_type->{ $field };
}
}

Expand Down
20 changes: 18 additions & 2 deletions src/wp-includes/class-wp-block-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ class WP_Block_Type {

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

/**
* @since 5.5.0
Expand All @@ -100,6 +100,22 @@ class WP_Block_Type {
*/
public $attributes = null;

/**
* Context values inherited by blocks of this type.
*
* @since 5.5.0
* @var array
*/
public $uses_context = array();

/**
* Context provided by blocks of this type.
*
* @since 5.5.0
* @var array|null
*/
public $provides_context = null;

/**
* Block type editor script handle.
*
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/tests/admin/includesPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ function test_get_block_editor_server_block_settings() {
'category' => 'common',
'icon' => 'text',
'keywords' => array(),
'supports' => array(),
'usesContext' => array(),
'styles' => array(),
),
$blocks[ $name ]
Expand Down

0 comments on commit aa64dea

Please sign in to comment.