From a271af487eb8e04945048cca856c211454b6e56c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Zi=C3=B3=C5=82kowski?= Date: Wed, 27 May 2020 10:31:38 +0200 Subject: [PATCH] Block Library: Register all block with "block.json" on the server' (#22491) * Block Library: Register all block with "block.json" on the server' * Add missing blocks --- lib/blocks.php | 66 ++++++++++++++++++++++++++++++++++++++++++++++---- lib/compat.php | 9 ++++--- 2 files changed, 67 insertions(+), 8 deletions(-) diff --git a/lib/blocks.php b/lib/blocks.php index cdbc41fcdf0e91..80522bda953672 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -16,6 +16,41 @@ function gutenberg_reregister_core_block_types() { return; } + $block_folders = array( + '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', @@ -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', ); @@ -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; diff --git a/lib/compat.php b/lib/compat.php index 9b8c33d3efee97..fa99b15dcbc11a 100644 --- a/lib/compat.php +++ b/lib/compat.php @@ -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. @@ -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; }