Skip to content

Commit

Permalink
Block Library: Register all block with "block.json" on the server' (#…
Browse files Browse the repository at this point in the history
…22491)

* Block Library: Register all block with "block.json" on the server'

* Add missing blocks
  • Loading branch information
gziolo authored May 27, 2020
1 parent c31475a commit a271af4
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 8 deletions.
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(
'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

0 comments on commit a271af4

Please sign in to comment.