Skip to content
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
66 changes: 61 additions & 5 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,41 @@ function gutenberg_reregister_core_block_types() {
return;
}

$block_folders = array(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of an explicit array, can we just loop through the folder?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the ideal world, I would do it, but some blocks are behind the flag and other call register_block_from_metadata in PHP file. Well, we could detect the latter, and blacklist the former. Hmmm, I might open a follow-up. I guess I could use glob right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is also this proposed addition to WordPress core from @aduth in https://core.trac.wordpress.org/ticket/49615:
register_block_type_args filter

With that, we could find all block.json file to register blocks and use this hook to set render_callback separately.

'audio',
'button',
'buttons',
'classic',
'code',
'column',
'columns',
'file',
'gallery',
'group',
'heading',
'html',
'image',
'list',
'media-text',
'missing',
'more',
'navigation-link',
'nextpage',
'paragraph',
'preformatted',
'pullquote',
'quote',
'separator',
'social-links',
'spacer',
'subhead',
'table',
'text-columns',
'verse',
'video',
'widget-area',
);

$block_names = array(
'archives.php' => 'core/archives',
'block.php' => 'core/block',
Expand All @@ -27,8 +62,8 @@ function gutenberg_reregister_core_block_types() {
'legacy-widget.php' => 'core/legacy-widget',
'navigation.php' => 'core/navigation',
'rss.php' => 'core/rss',
'shortcode.php' => 'core/shortcode',
'search.php' => 'core/search',
'shortcode.php' => 'core/shortcode',
'social-link.php' => 'core/social-link',
'tag-cloud.php' => 'core/tag-cloud',
);
Expand All @@ -37,27 +72,48 @@ function gutenberg_reregister_core_block_types() {
$block_names = array_merge(
$block_names,
array(
'post-title.php' => 'core/post-title',
'post-content.php' => 'core/post-content',
'post-author.php' => 'core/post-author',
'post-comments.php' => 'core/post-comments',
'post-comments-count.php' => 'core/post-comments-count',
'post-comments-form.php' => 'core/post-comments-form',
'post-content.php' => 'core/post-content',
'post-date.php' => 'core/post-date',
'post-excerpt.php' => 'core/post-excerpt',
'post-featured-image.php' => 'core/post-featured-image',
'post-tags.php' => 'core/post-tags',
'site-title.php' => 'core/site-title',
'template-part.php' => 'core/template-part',
'post-title.php' => 'core/post-title',
'query.php' => 'core/query',
'query-loop.php' => 'core/query-loop',
'query-pagination.php' => 'core/query-pagination',
'site-title.php' => 'core/site-title',
'template-part.php' => 'core/template-part',
)
);
}

$registry = WP_Block_Type_Registry::get_instance();

foreach ( $block_folders as $folder_name ) {
$block_json_file = $blocks_dir . '/' . $folder_name . '/block.json';
if ( ! file_exists( $block_json_file ) ) {
return;
}

// Ideally, all paths to block metadata files should be listed in
// WordPress core. In this place we should rather use filter
// to replace paths with overrides defined by the plugin.
$metadata = json_decode( file_get_contents( $block_json_file ), true );
if ( ! is_array( $metadata ) || ! $metadata['name'] ) {
return false;
}

if ( $registry->is_registered( $metadata['name'] ) ) {
$registry->unregister( $metadata['name'] );
}

register_block_type_from_metadata( $block_json_file );
}

foreach ( $block_names as $file => $block_names ) {
if ( ! file_exists( $blocks_dir . $file ) ) {
return;
Expand Down
9 changes: 6 additions & 3 deletions lib/compat.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
*
* @since 7.9.0
*
* @param string $path Path to the folder where the `block.json` file is located.
* @param string $file_or_folder Path to the JSON file with metadata definition for
* the block or path to the folder where the `block.json` file is located.
* @param array $args {
* Optional. Array of block type arguments. Any arguments may be defined, however the
* ones described below are supported by default. Default empty array.
Expand All @@ -23,8 +24,10 @@
* }
* @return WP_Block_Type|false The registered block type on success, or false on failure.
*/
function register_block_type_from_metadata( $path, $args = array() ) {
$file = trailingslashit( $path ) . 'block.json';
function register_block_type_from_metadata( $file_or_folder, $args = array() ) {
$file = ( substr( $file_or_folder, -10 ) !== 'block.json' ) ?
trailingslashit( $file_or_folder ) . 'block.json' :
$file_or_folder;
if ( ! file_exists( $file ) ) {
return false;
}
Expand Down