Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds block supports for metadata #43986

Merged
merged 3 commits into from
Sep 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
25 changes: 25 additions & 0 deletions lib/compat/wordpress-6.1/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -392,3 +392,28 @@ 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'] ) ) {
getdave marked this conversation as resolved.
Show resolved Hide resolved
$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' );
2 changes: 2 additions & 0 deletions packages/block-editor/src/hooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import './font-size';
import './border';
import './layout';
import './content-lock-ui';
import './metadata';
import './metadata-name';

export { useCustomSides } from './dimensions';
export { getBorderClassesAndStyles, useBorderProps } from './use-border-props';
Expand Down
48 changes: 48 additions & 0 deletions packages/block-editor/src/hooks/metadata-name.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* WordPress dependencies
*/
import { addFilter } from '@wordpress/hooks';
/**
* Internal dependencies
*/
import { hasBlockMetadataSupport } from './metadata';

/**
* Filters registered block settings, adding an `__experimentalLabel` callback if one does not already exist.
*
* @param {Object} settings Original block settings.
*
* @return {Object} Filtered block settings.
*/
export function addLabelCallback( settings ) {
// If blocks provide their own label callback, do not override it.
if ( settings.__experimentalLabel ) {
return settings;
}

const supportsBlockNaming = hasBlockMetadataSupport(
settings,
'name',
false // default value
);

// Check whether block metadata is supported before using it.
if ( supportsBlockNaming ) {
settings.__experimentalLabel = ( attributes, { context } ) => {
const { metadata } = attributes;

// In the list view, use the block's name attribute as the label.
if ( context === 'list-view' && metadata?.name ) {
return metadata.name;
}
};
}

return settings;
}

addFilter(
'blocks.registerBlockType',
'core/metadata/addLabelCallback',
addLabelCallback
);
64 changes: 64 additions & 0 deletions packages/block-editor/src/hooks/metadata.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* WordPress dependencies
*/
import { addFilter } from '@wordpress/hooks';
import { getBlockSupport } from '@wordpress/blocks';

const META_ATTRIBUTE_NAME = 'metadata';

export function hasBlockMetadataSupport( blockType, feature = '' ) {
const support = getBlockSupport( blockType, '__experimentalMetadata' );
return !! ( true === support || support?.[ feature ] );
}

/**
* Filters registered block settings, extending attributes to include `metadata`.
*
* see: https://github.com/WordPress/gutenberg/pull/40393/files#r864632012
*
* @param {Object} blockTypeSettings Original block settings.
* @return {Object} Filtered block settings.
*/
export function addMetaAttribute( blockTypeSettings ) {
// Allow blocks to specify their own attribute definition with default values if needed.
if ( blockTypeSettings?.attributes?.[ META_ATTRIBUTE_NAME ]?.type ) {
return blockTypeSettings;
}

const supportsBlockNaming = hasBlockMetadataSupport(
blockTypeSettings,
'name',
false
);

if ( supportsBlockNaming ) {
blockTypeSettings.attributes = {
...blockTypeSettings.attributes,
[ META_ATTRIBUTE_NAME ]: {
type: 'object',
},
};
}

return blockTypeSettings;
}

export function addSaveProps( extraProps, blockType, attributes ) {
if ( hasBlockMetadataSupport( blockType ) ) {
extraProps[ META_ATTRIBUTE_NAME ] = attributes[ META_ATTRIBUTE_NAME ];
}

return extraProps;
}

addFilter(
'blocks.registerBlockType',
'core/metadata/addMetaAttribute',
addMetaAttribute
);

addFilter(
'blocks.getSaveContent.extraProps',
'core/metadata/save-props',
addSaveProps
);