Skip to content

Commit

Permalink
Blocks: Re-register core blocks via build copy prefixing (#13521)
Browse files Browse the repository at this point in the history
* Build Tools: Exclude build files from PHPCS

* Blocks: Re-register core blocks via build copy prefixing

* Blocks: Discover block overrides from built artifact

* Blocks: Reimplement block reregistering as hard-coded list

* Blocks: Modify priority for blocks registration during build

* Blocks: Move blocks reregister function to standalone file

* Comma comma comma comma comma chameleon

* Update regex in webpack config to contain gutenberg as well

* Update build plugin zip script to include block PHP files from the new location
  • Loading branch information
aduth authored Mar 25, 2019
1 parent f9027da commit bd381c4
Show file tree
Hide file tree
Showing 14 changed files with 148 additions and 100 deletions.
3 changes: 1 addition & 2 deletions bin/build-plugin-zip.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
47 changes: 47 additions & 0 deletions lib/blocks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* Block registration functions.
*
* @package gutenberg
*/

/**
* Substitutes the implementation of a core-registered block type, if exists,
* with the built result from the plugin.
*/
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_names = array(
'archives.php' => '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' );
48 changes: 1 addition & 47 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
1 change: 0 additions & 1 deletion packages/block-library/src/archives/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,4 @@ function register_block_core_archives() {
)
);
}

add_action( 'init', 'register_block_core_archives' );
26 changes: 16 additions & 10 deletions packages/block-library/src/block/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
1 change: 0 additions & 1 deletion packages/block-library/src/categories/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,4 @@ function register_block_core_categories() {
)
);
}

add_action( 'init', 'register_block_core_categories' );
77 changes: 45 additions & 32 deletions packages/block-library/src/latest-comments/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
1 change: 0 additions & 1 deletion packages/block-library/src/latest-posts/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,5 +127,4 @@ function register_block_core_latest_posts() {
)
);
}

add_action( 'init', 'register_block_core_latest_posts' );
1 change: 0 additions & 1 deletion packages/block-library/src/rss/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,5 +133,4 @@ function register_block_core_rss() {
)
);
}

add_action( 'init', 'register_block_core_rss' );
1 change: 0 additions & 1 deletion packages/block-library/src/search/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,4 @@ function register_block_core_search() {
)
);
}

add_action( 'init', 'register_block_core_search' );
1 change: 0 additions & 1 deletion packages/block-library/src/shortcode/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,4 @@ function register_block_core_shortcode() {
)
);
}

add_action( 'init', 'register_block_core_shortcode' );
1 change: 0 additions & 1 deletion packages/block-library/src/tag-cloud/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,4 @@ function register_block_core_tag_cloud() {
)
);
}

add_action( 'init', 'register_block_core_tag_cloud' );
1 change: 1 addition & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

<!-- Exclude generated files -->
<exclude-pattern>./packages/block-serialization-spec-parser/parser.php</exclude-pattern>
<exclude-pattern>./build</exclude-pattern>

<rule ref="PHPCompatibility.PHP.NewKeywords.t_namespaceFound">
<exclude-pattern>lib/class-wp-rest-block-renderer-controller.php</exclude-pattern>
Expand Down
39 changes: 37 additions & 2 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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' );
},
},
] ),
],
};

0 comments on commit bd381c4

Please sign in to comment.