diff --git a/bin/build-plugin-zip.sh b/bin/build-plugin-zip.sh index 6c6d9d10512ee..a518e83b0e311 100755 --- a/bin/build-plugin-zip.sh +++ b/bin/build-plugin-zip.sh @@ -107,14 +107,13 @@ npm run build php bin/generate-gutenberg-php.php > gutenberg.tmp.php mv gutenberg.tmp.php gutenberg.php -build_files=$(ls build/*/*.{js,css}) +build_files=$(ls build/*/*.{js,css} build/block-library/blocks/*.php) # Generate the plugin zip file. status "Creating archive... 🎁" zip -r gutenberg.zip \ gutenberg.php \ lib/*.php \ - packages/block-library/src/*/*.php \ packages/block-serialization-default-parser/*.php \ post-content.php \ $vendor_scripts \ diff --git a/lib/blocks.php b/lib/blocks.php new file mode 100644 index 0000000000000..fc3adb8a09efa --- /dev/null +++ b/lib/blocks.php @@ -0,0 +1,47 @@ + 'core/archives', + 'block.php' => 'core/block', + 'calendar.php' => 'core/calendar', + 'categories.php' => 'core/categories', + 'latest-comments.php' => 'core/latest-comments', + 'latest-posts.php' => 'core/latest-posts', + 'legacy-widget.php' => 'core/legacy-widget', + 'rss.php' => 'core/rss', + 'shortcode.php' => 'core/shortcode', + 'search.php' => 'core/search', + 'tag-cloud.php' => 'core/tag-cloud', + ); + + $registry = WP_Block_Type_Registry::get_instance(); + + foreach ( $block_names as $file => $block_name ) { + if ( ! file_exists( $blocks_dir . $file ) ) { + return; + } + + if ( $registry->is_registered( $block_name ) ) { + $registry->unregister( $block_name ); + } + + require $blocks_dir . $file; + } +} +add_action( 'init', 'gutenberg_reregister_core_block_types' ); diff --git a/lib/load.php b/lib/load.php index f469513fd83a9..4010f805cad52 100644 --- a/lib/load.php +++ b/lib/load.php @@ -24,55 +24,9 @@ require dirname( __FILE__ ) . '/rest-api.php'; } +require dirname( __FILE__ ) . '/blocks.php'; require dirname( __FILE__ ) . '/client-assets.php'; require dirname( __FILE__ ) . '/i18n.php'; require dirname( __FILE__ ) . '/demo.php'; require dirname( __FILE__ ) . '/widgets.php'; require dirname( __FILE__ ) . '/widgets-page.php'; - -// Register server-side code for individual blocks. -if ( ! function_exists( 'render_block_core_archives' ) ) { - require dirname( __FILE__ ) . '/../packages/block-library/src/archives/index.php'; -} -if ( ! function_exists( 'render_block_core_block' ) ) { - require dirname( __FILE__ ) . '/../packages/block-library/src/block/index.php'; -} -if ( ! function_exists( 'render_block_core_categories' ) ) { - require dirname( __FILE__ ) . '/../packages/block-library/src/categories/index.php'; -} -// Currently merged in core as `gutenberg_render_block_core_latest_comments`, -// expected to change soon. -if ( ! function_exists( 'render_block_core_calendar' ) ) { - require dirname( __FILE__ ) . '/../packages/block-library/src/calendar/index.php'; -} -if ( ! function_exists( 'render_block_core_latest_comments' ) - && ! function_exists( 'gutenberg_render_block_core_latest_comments' ) ) { - require dirname( __FILE__ ) . '/../packages/block-library/src/latest-comments/index.php'; -} -if ( ! function_exists( 'render_block_core_latest_posts' ) ) { - require dirname( __FILE__ ) . '/../packages/block-library/src/latest-posts/index.php'; -} - - -/** - * Start: Include for phase 2 - */ -if ( ! function_exists( 'render_block_legacy_widget' ) ) { - require dirname( __FILE__ ) . '/../packages/block-library/src/legacy-widget/index.php'; -} -/** - * End: Include for phase 2 - */ - -if ( ! function_exists( 'render_block_core_rss' ) ) { - require dirname( __FILE__ ) . '/../packages/block-library/src/rss/index.php'; -} -if ( ! function_exists( 'render_block_core_shortcode' ) ) { - require dirname( __FILE__ ) . '/../packages/block-library/src/shortcode/index.php'; -} -if ( ! function_exists( 'render_block_core_tag_cloud' ) ) { - require dirname( __FILE__ ) . '/../packages/block-library/src/tag-cloud/index.php'; -} -if ( ! function_exists( 'render_block_core_search' ) ) { - require dirname( __FILE__ ) . '/../packages/block-library/src/search/index.php'; -} diff --git a/packages/block-library/src/archives/index.php b/packages/block-library/src/archives/index.php index 3d7b5b42f4ae4..435f7b7536f36 100644 --- a/packages/block-library/src/archives/index.php +++ b/packages/block-library/src/archives/index.php @@ -143,5 +143,4 @@ function register_block_core_archives() { ) ); } - add_action( 'init', 'register_block_core_archives' ); diff --git a/packages/block-library/src/block/index.php b/packages/block-library/src/block/index.php index aa235f170333e..2f68a3750d154 100644 --- a/packages/block-library/src/block/index.php +++ b/packages/block-library/src/block/index.php @@ -29,15 +29,21 @@ function render_block_core_block( $attributes ) { return do_blocks( $reusable_block->post_content ); } -register_block_type( - 'core/block', - array( - 'attributes' => array( - 'ref' => array( - 'type' => 'number', +/** + * Registers the `core/block` block. + */ +function register_block_core_block() { + register_block_type( + 'core/block', + array( + 'attributes' => array( + 'ref' => array( + 'type' => 'number', + ), ), - ), - 'render_callback' => 'render_block_core_block', - ) -); + 'render_callback' => 'render_block_core_block', + ) + ); +} +add_action( 'init', 'register_block_core_block' ); diff --git a/packages/block-library/src/categories/index.php b/packages/block-library/src/categories/index.php index 57042046061d2..a995e447e28c3 100644 --- a/packages/block-library/src/categories/index.php +++ b/packages/block-library/src/categories/index.php @@ -98,5 +98,4 @@ function register_block_core_categories() { ) ); } - add_action( 'init', 'register_block_core_categories' ); diff --git a/packages/block-library/src/latest-comments/index.php b/packages/block-library/src/latest-comments/index.php index ae14d31b6f1c5..504815bf8cbec 100644 --- a/packages/block-library/src/latest-comments/index.php +++ b/packages/block-library/src/latest-comments/index.php @@ -150,36 +150,49 @@ function render_block_core_latest_comments( $attributes = array() ) { return $block_content; } -register_block_type( - 'core/latest-comments', - array( - 'attributes' => array( - 'align' => array( - 'type' => 'string', - 'enum' => array( 'left', 'center', 'right', 'wide', 'full' ), - ), - 'className' => array( - 'type' => 'string', - ), - 'commentsToShow' => array( - 'type' => 'number', - 'default' => 5, - 'minimum' => 1, - 'maximum' => 100, - ), - 'displayAvatar' => array( - 'type' => 'boolean', - 'default' => true, - ), - 'displayDate' => array( - 'type' => 'boolean', - 'default' => true, - ), - 'displayExcerpt' => array( - 'type' => 'boolean', - 'default' => true, +/** + * Registers the `core/latest-comments` block. + */ +function register_block_core_latest_comments() { + register_block_type( + 'core/latest-comments', + array( + 'attributes' => array( + 'align' => array( + 'type' => 'string', + 'enum' => array( + 'left', + 'center', + 'right', + 'wide', + 'full', + ), + ), + 'className' => array( + 'type' => 'string', + ), + 'commentsToShow' => array( + 'type' => 'number', + 'default' => 5, + 'minimum' => 1, + 'maximum' => 100, + ), + 'displayAvatar' => array( + 'type' => 'boolean', + 'default' => true, + ), + 'displayDate' => array( + 'type' => 'boolean', + 'default' => true, + ), + 'displayExcerpt' => array( + 'type' => 'boolean', + 'default' => true, + ), ), - ), - 'render_callback' => 'render_block_core_latest_comments', - ) -); + 'render_callback' => 'render_block_core_latest_comments', + ) + ); +} + +add_action( 'init', 'register_block_core_latest_comments' ); diff --git a/packages/block-library/src/latest-posts/index.php b/packages/block-library/src/latest-posts/index.php index 888f99c887992..070a70cd1860d 100644 --- a/packages/block-library/src/latest-posts/index.php +++ b/packages/block-library/src/latest-posts/index.php @@ -127,5 +127,4 @@ function register_block_core_latest_posts() { ) ); } - add_action( 'init', 'register_block_core_latest_posts' ); diff --git a/packages/block-library/src/rss/index.php b/packages/block-library/src/rss/index.php index b5fe3abaf563b..65a77d9182360 100644 --- a/packages/block-library/src/rss/index.php +++ b/packages/block-library/src/rss/index.php @@ -133,5 +133,4 @@ function register_block_core_rss() { ) ); } - add_action( 'init', 'register_block_core_rss' ); diff --git a/packages/block-library/src/search/index.php b/packages/block-library/src/search/index.php index ac7da463e92f2..3f344da4781a0 100644 --- a/packages/block-library/src/search/index.php +++ b/packages/block-library/src/search/index.php @@ -78,5 +78,4 @@ function register_block_core_search() { ) ); } - add_action( 'init', 'register_block_core_search' ); diff --git a/packages/block-library/src/shortcode/index.php b/packages/block-library/src/shortcode/index.php index 1c0761250d2cf..c3bb4c9449b08 100644 --- a/packages/block-library/src/shortcode/index.php +++ b/packages/block-library/src/shortcode/index.php @@ -28,5 +28,4 @@ function register_block_core_shortcode() { ) ); } - add_action( 'init', 'register_block_core_shortcode' ); diff --git a/packages/block-library/src/tag-cloud/index.php b/packages/block-library/src/tag-cloud/index.php index c60f8a1dd6f54..46cd0e6606220 100644 --- a/packages/block-library/src/tag-cloud/index.php +++ b/packages/block-library/src/tag-cloud/index.php @@ -67,5 +67,4 @@ function register_block_core_tag_cloud() { ) ); } - add_action( 'init', 'register_block_core_tag_cloud' ); diff --git a/phpcs.xml.dist b/phpcs.xml.dist index e328ac8dde3fc..6b1456e982e29 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -33,6 +33,7 @@ ./packages/block-serialization-spec-parser/parser.php + ./build lib/class-wp-rest-block-renderer-controller.php diff --git a/webpack.config.js b/webpack.config.js index dba1661ec492f..a8b11fd9aac6f 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -5,8 +5,8 @@ const { DefinePlugin } = require( 'webpack' ); const WebpackRTLPlugin = require( 'webpack-rtl-plugin' ); const CopyWebpackPlugin = require( 'copy-webpack-plugin' ); const postcss = require( 'postcss' ); -const { get } = require( 'lodash' ); -const { basename } = require( 'path' ); +const { get, escapeRegExp } = require( 'lodash' ); +const { basename, sep } = require( 'path' ); /** * WordPress dependencies @@ -105,5 +105,40 @@ 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(); + + // 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' ); + }, + }, + ] ), ], };