Skip to content

Commit

Permalink
Blocks: Re-register core blocks via build copy prefixing
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Jan 26, 2019
1 parent 033e664 commit 411021f
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 73 deletions.
54 changes: 31 additions & 23 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,37 @@
require dirname( __FILE__ ) . '/i18n.php';
require dirname( __FILE__ ) . '/register.php';

/**
* Unregisters the core set of blocks. This should occur on the default
* priority, immediately prior to Gutenberg's own action binding.
*/
function gutenberg_unregister_core_block_types() {
$registry = WP_Block_Type_Registry::get_instance();
$block_names = array(
'core/archives',
'core/block',
'core/categories',
'core/latest-comments',
'core/latest-posts',
'core/rss',
'core/shortcode',
);

// 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_latest_comments' )
&& ! function_exists( 'gutenberg_render_block_core_latest_comments' ) ) {
require dirname( __FILE__ ) . '/../packages/block-library/src/latest-comments/index.php';
foreach ( $block_names as $block_name ) {
if ( $registry->is_registered( $block_name ) ) {
$registry->unregister( $block_name );
}
}
}
if ( ! function_exists( 'render_block_core_latest_posts' ) ) {
require dirname( __FILE__ ) . '/../packages/block-library/src/latest-posts/index.php';
}
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 ( file_exists( dirname( __FILE__ ) . '/../build/block-library/blocks' ) ) {
add_action( 'init', 'gutenberg_unregister_core_block_types' );

require dirname( __FILE__ ) . '/../build/block-library/blocks/archives.php';
require dirname( __FILE__ ) . '/../build/block-library/blocks/block.php';
require dirname( __FILE__ ) . '/../build/block-library/blocks/categories.php';
require dirname( __FILE__ ) . '/../build/block-library/blocks/latest-comments.php';
require dirname( __FILE__ ) . '/../build/block-library/blocks/latest-posts.php';
require dirname( __FILE__ ) . '/../build/block-library/blocks/rss.php';
require dirname( __FILE__ ) . '/../build/block-library/blocks/shortcode.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 on server.
*/
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' );
71 changes: 39 additions & 32 deletions packages/block-library/src/latest-comments/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,36 +147,43 @@ function render_block_core_latest_comments( $attributes = array() ) {
return $block_content;
}

register_block_type(
'core/latest-comments',
array(
'attributes' => array(
'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,
),
'align' => array(
'type' => 'string',
'enum' => array( 'center', 'left', 'right', 'wide', 'full', '' ),

/**
* Registers the `core/latest-comments` block on server.
*/
function register_block_core_latest_comments() {
register_block_type(
'core/latest-comments',
array(
'attributes' => array(
'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,
),
'align' => array(
'type' => 'string',
'enum' => array( 'center', 'left', 'right', 'wide', 'full', '' ),
),
),
),
'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 @@ -128,5 +128,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/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' );
31 changes: 28 additions & 3 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ const WebpackRTLPlugin = require( 'webpack-rtl-plugin' );
const LiveReloadPlugin = require( 'webpack-livereload-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 @@ -162,6 +161,32 @@ const config = {
},
} ) )
),
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 );
},
},
] ),
// GUTENBERG_BUNDLE_ANALYZER global variable enables utility that represents bundle content
// as convenient interactive zoomable treemap.
process.env.GUTENBERG_BUNDLE_ANALYZER && new BundleAnalyzerPlugin(),
Expand Down

0 comments on commit 411021f

Please sign in to comment.