Skip to content

Commit

Permalink
[Widgets editor] Update the build process to account for legacy-widge…
Browse files Browse the repository at this point in the history
…t block living in edit-widgets package (#25422)

* Correctly handle legacy-widget block

* Generate CopyWebpackPlugin mapping based on an array

* Improve readability

* Inline transformBlockContent

* Lint
  • Loading branch information
BreadGuy007 committed Sep 18, 2020
1 parent 2b3e306 commit 9098639
Show file tree
Hide file tree
Showing 2 changed files with 183 additions and 164 deletions.
245 changes: 129 additions & 116 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,137 +11,149 @@
*/
function gutenberg_reregister_core_block_types() {
// Blocks directory may not exist if working from a fresh clone.
$blocks_dir = dirname( __FILE__ ) . '/../build/block-library/blocks/';
if ( ! file_exists( $blocks_dir ) ) {
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',
'calendar.php' => 'core/calendar',
'categories.php' => 'core/categories',
'cover.php' => 'core/cover',
'latest-comments.php' => 'core/latest-comments',
'latest-posts.php' => 'core/latest-posts',
'legacy-widget.php' => 'core/legacy-widget',
'navigation.php' => 'core/navigation',
'navigation-link.php' => 'core/navigation-link',
'rss.php' => 'core/rss',
'search.php' => 'core/search',
'shortcode.php' => 'core/shortcode',
'social-link.php' => 'core/social-link',
'tag-cloud.php' => 'core/tag-cloud',
$blocks_dirs = array(
dirname( __FILE__ ) . '/../build/block-library/blocks/' => array(
'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_merge(
array(
'archives.php' => 'core/archives',
'block.php' => 'core/block',
'calendar.php' => 'core/calendar',
'categories.php' => 'core/categories',
'cover.php' => 'core/cover',
'latest-comments.php' => 'core/latest-comments',
'latest-posts.php' => 'core/latest-posts',
'navigation.php' => 'core/navigation',
'navigation-link.php' => 'core/navigation-link',
'rss.php' => 'core/rss',
'search.php' => 'core/search',
'shortcode.php' => 'core/shortcode',
'social-link.php' => 'core/social-link',
'tag-cloud.php' => 'core/tag-cloud',
),
! gutenberg_is_experiment_enabled( 'gutenberg-full-site-editing' )
? array()
:
array(
'post-author.php' => 'core/post-author',
'post-comment.php' => 'core/post-comment',
'post-comment-author.php' => 'core/post-comment-author',
'post-comment-content.php' => 'core/post-comment-content',
'post-comment-date.php' => 'core/post-comment-date',
'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-hierarchical-terms.php' => 'core/post-hierarchical-terms',
'post-tags.php' => 'core/post-tags',
'post-title.php' => 'core/post-title',
'query.php' => 'core/query',
'query-loop.php' => 'core/query-loop',
'query-pagination.php' => 'core/query-pagination',
'site-logo.php' => 'core/site-logo',
'site-tagline.php' => 'core/site-tagline',
'site-title.php' => 'core/site-title',
'template-part.php' => 'core/template-part',
)
),
),
dirname( __FILE__ ) . '/../build/edit-widgets/blocks/' => array(
'block_folders' => array(
'legacy-widget',
),
'block_names' => array(
'legacy-widget.php' => 'core/legacy-widget',
),
),
);

if ( gutenberg_is_experiment_enabled( 'gutenberg-full-site-editing' ) ) {
$block_names = array_merge(
$block_names,
array(
'post-author.php' => 'core/post-author',
'post-comment.php' => 'core/post-comment',
'post-comment-author.php' => 'core/post-comment-author',
'post-comment-content.php' => 'core/post-comment-content',
'post-comment-date.php' => 'core/post-comment-date',
'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-hierarchical-terms.php' => 'core/post-hierarchical-terms',
'post-tags.php' => 'core/post-tags',
'post-title.php' => 'core/post-title',
'query.php' => 'core/query',
'query-loop.php' => 'core/query-loop',
'query-pagination.php' => 'core/query-pagination',
'site-logo.php' => 'core/site-logo',
'site-tagline.php' => 'core/site-tagline',
'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 ) ) {
foreach ( $blocks_dirs as $blocks_dir => $details ) {
if ( ! file_exists( $blocks_dir ) ) {
return;
}
$block_folders = $details['block_folders'];
$block_names = $details['block_names'];

// 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;
}
$registry = WP_Block_Type_Registry::get_instance();

if ( $registry->is_registered( $metadata['name'] ) ) {
$registry->unregister( $metadata['name'] );
}
foreach ( $block_folders as $folder_name ) {
$block_json_file = $blocks_dir . $folder_name . '/block.json';
if ( ! file_exists( $block_json_file ) ) {
return;
}

register_block_type_from_metadata( $block_json_file );
}
// 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;
}

foreach ( $block_names as $file => $block_names ) {
if ( ! file_exists( $blocks_dir . $file ) ) {
return;
if ( $registry->is_registered( $metadata['name'] ) ) {
$registry->unregister( $metadata['name'] );
}

register_block_type_from_metadata( $block_json_file );
}

if ( is_string( $block_names ) ) {
if ( $registry->is_registered( $block_names ) ) {
$registry->unregister( $block_names );
foreach ( $block_names as $file => $block_names ) {
if ( ! file_exists( $blocks_dir . $file ) ) {
return;
}
} elseif ( is_array( $block_names ) ) {
foreach ( $block_names as $block_name ) {
if ( $registry->is_registered( $block_name ) ) {
$registry->unregister( $block_name );

if ( is_string( $block_names ) ) {
if ( $registry->is_registered( $block_names ) ) {
$registry->unregister( $block_names );
}
} elseif ( is_array( $block_names ) ) {
foreach ( $block_names as $block_name ) {
if ( $registry->is_registered( $block_name ) ) {
$registry->unregister( $block_name );
}
}
}
}

require $blocks_dir . $file;
require $blocks_dir . $file;
}
}
}

add_action( 'init', 'gutenberg_reregister_core_block_types' );

/**
Expand Down Expand Up @@ -223,4 +235,5 @@ function gutenberg_register_legacy_social_link_blocks() {
);
}
}

add_action( 'init', 'gutenberg_register_legacy_social_link_blocks' );
102 changes: 54 additions & 48 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,57 +161,63 @@ module.exports = {
},
} ) )
),
new CopyWebpackPlugin( [
{
from: './packages/block-library/src/**/index.php',
test: new RegExp(
`([\\w-]+)${ escapeRegExp( sep ) }index\\.php$`
),
to: 'build/block-library/blocks/[1].php',
transform( content ) {
content = content.toString();
new CopyWebpackPlugin(
Object.entries( {
'./packages/block-library/src/': 'build/block-library/blocks/',
'./packages/edit-widgets/src/blocks/':
'build/edit-widgets/blocks/',
} ).flatMap( ( [ from, to ] ) => [
{
from: `${ from }/**/index.php`,
test: new RegExp(
`([\\w-]+)${ escapeRegExp( sep ) }index\\.php$`
),
to: `${ to }/[1].php`,
transform: ( content ) => {
content = content.toString();

// Within content, search for any function definitions. For
// each, replace every other reference to it in the file.
return (
content
.match( /^function [^\(]+/gm )
.reduce( ( result, functionName ) => {
// Trim leading "function " prefix from match.
functionName = functionName.slice( 9 );
// Within content, search for any function definitions. For
// each, replace every other reference to it in the file.
return (
content
.match( /^function [^\(]+/gm )
.reduce( ( result, functionName ) => {
// Trim leading "function " prefix from match.
functionName = functionName.slice( 9 );

// Prepend the Gutenberg prefix, substituting any
// other core prefix (e.g. "wp_").
return result.replace(
new RegExp( functionName, 'g' ),
( match ) =>
'gutenberg_' +
match.replace( /^wp_/, '' )
);
}, content )
// The core blocks override procedure takes place in
// the init action default priority to ensure that core
// blocks would have been registered already. Since the
// blocks implementations occur at the default priority
// and due to WordPress hooks behavior not considering
// mutations to the same priority during another's
// callback, the Gutenberg build blocks are modified
// to occur at a later priority.
.replace(
/(add_action\(\s*'init',\s*'gutenberg_register_block_[^']+'(?!,))/,
'$1, 20'
)
);
// Prepend the Gutenberg prefix, substituting any
// other core prefix (e.g. "wp_").
return result.replace(
new RegExp( functionName, 'g' ),
( match ) =>
'gutenberg_' +
match.replace( /^wp_/, '' )
);
}, content )
// The core blocks override procedure takes place in
// the init action default priority to ensure that core
// blocks would have been registered already. Since the
// blocks implementations occur at the default priority
// and due to WordPress hooks behavior not considering
// mutations to the same priority during another's
// callback, the Gutenberg build blocks are modified
// to occur at a later priority.
.replace(
/(add_action\(\s*'init',\s*'gutenberg_register_block_[^']+'(?!,))/,
'$1, 20'
)
);
},
},
},
{
from: './packages/block-library/src/*/block.json',
test: new RegExp(
`([\\w-]+)${ escapeRegExp( sep ) }block\\.json$`
),
to: 'build/block-library/blocks/[1]/block.json',
},
] ),
{
from: `${ from }/*/block.json`,
test: new RegExp(
`([\\w-]+)${ escapeRegExp( sep ) }block\\.json$`
),
to: `${ to }/[1]/block.json`,
},
] )
),
new DependencyExtractionWebpackPlugin( { injectPolyfill: true } ),
].filter( Boolean ),
watchOptions: {
Expand Down

0 comments on commit 9098639

Please sign in to comment.