Skip to content

Commit

Permalink
Handle block metadata attribute and related experimental APIs (#47791)
Browse files Browse the repository at this point in the history
* Move PHP attribute registration to experiments dir

* Limit __experimentalMetadata to Core blocks

* fix check

---------

Co-authored-by: ntsekouras <ntsekouras@outlook.com>
  • Loading branch information
getdave and ntsekouras committed Feb 13, 2023
1 parent f1bc31a commit a1fd207
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 25 deletions.
25 changes: 0 additions & 25 deletions lib/compat/wordpress-6.1/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,28 +323,3 @@ function gutenberg_block_type_metadata_render_template( $settings, $metadata ) {
return $settings;
}
add_filter( 'block_type_metadata_settings', 'gutenberg_block_type_metadata_render_template', 10, 2 );

/**
* Registers the metadata block attribute for block types.
*
* Once 6.1 is the minimum supported WordPress version for the Gutenberg
* plugin, this shim can be removed
*
* @param array $args Array of arguments for registering a block type.
* @return array $args
*/
function gutenberg_register_metadata_attribute( $args ) {
// Setup attributes if needed.
if ( ! isset( $args['attributes'] ) || ! is_array( $args['attributes'] ) ) {
$args['attributes'] = array();
}

if ( ! array_key_exists( 'metadata', $args['attributes'] ) ) {
$args['attributes']['metadata'] = array(
'type' => 'object',
);
}

return $args;
}
add_filter( 'register_block_type_args', 'gutenberg_register_metadata_attribute' );
23 changes: 23 additions & 0 deletions lib/experimental/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,26 @@ function wp_enqueue_block_view_script( $block_name, $args ) {
add_filter( 'render_block', $callback, 10, 2 );
}
}


/**
* Registers the metadata block attribute for block types.
*
* @param array $args Array of arguments for registering a block type.
* @return array $args
*/
function gutenberg_register_metadata_attribute( $args ) {
// Setup attributes if needed.
if ( ! isset( $args['attributes'] ) || ! is_array( $args['attributes'] ) ) {
$args['attributes'] = array();
}

if ( ! array_key_exists( 'metadata', $args['attributes'] ) ) {
$args['attributes']['metadata'] = array(
'type' => 'object',
);
}

return $args;
}
add_filter( 'register_block_type_args', 'gutenberg_register_metadata_attribute' );
4 changes: 4 additions & 0 deletions packages/block-editor/src/hooks/metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import { getBlockSupport } from '@wordpress/blocks';
const META_ATTRIBUTE_NAME = 'metadata';

export function hasBlockMetadataSupport( blockType, feature = '' ) {
// Only core blocks are allowed to use __experimentalMetadata until the fetaure is stablised.
if ( ! blockType.name.startsWith( 'core/' ) ) {
return false;
}
const support = getBlockSupport( blockType, '__experimentalMetadata' );
return !! ( true === support || support?.[ feature ] );
}
Expand Down

0 comments on commit a1fd207

Please sign in to comment.