Skip to content

Commit

Permalink
Add a way to override core block registration based on metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo committed Mar 5, 2019
1 parent 33a6265 commit 625c7e7
Show file tree
Hide file tree
Showing 21 changed files with 125 additions and 207 deletions.
62 changes: 62 additions & 0 deletions lib/blocks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* Overrides all core blocks.
*
* @package gutenberg
*/

/**
* Registers block type from metadata file.
*
* @param string $file Path to the translation file to load.
*/
function gutenberg_register_block_type_from_metadata( $file ) {
$metadata = json_decode( file_get_contents( $file ), true );
if ( empty( $metadata['name'] ) || empty( $metadata['title'] ) || empty( $metadata['category'] ) ) {
$message = __( 'Required block types must be provided.' );
_doing_it_wrong( __FUNCTION__, $message, '5.2.0' );
return;
}
$block_name = $metadata['name'];
$settings = array(
'title' => $metadata['title'],
'category' => $metadata['category'],
);
foreach( array( 'description', 'keywords', 'attributes', 'supports' ) as $field_name ) {
if ( ! empty( $metadata[ $field_name ] ) ) {
$settings[ $field_name ] = $metadata[ $field_name ];
}
}
if ( ! empty( $metadata['renderCallback'] ) ) {
$render_callback_file = dirname( $file ) . '/' . $metadata['renderCallback'];
if ( file_exists( $render_callback_file ) ) {
require $render_callback_file;
$settings['render_callback'] = 'gutenberg_render_block_' . str_replace( array( '/', '-' ), '_', $block_name );
}
}
$registry = WP_Block_Type_Registry::get_instance();
if ( $registry->is_registered( $block_name ) ) {
$registry->unregister( $block_name );
}
register_block_type( $block_name, $settings );
}

function gutenberg_override_core_blocks() {
$block_types = array(
'archives',
'block',
'calendar',
'categories',
'latest-comments',
'latest-posts',
'rss',
'tag-cloud'
);

$file_format = dirname( dirname( __FILE__ ) ) . '/packages/block-library/src/%s/block.json';
foreach ( $block_types as $name ) {
gutenberg_register_block_type_from_metadata( sprintf( $file_format, $name ) );
}
}

add_action( 'init', 'gutenberg_override_core_blocks' );
36 changes: 1 addition & 35 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,38 +20,4 @@
require dirname( __FILE__ ) . '/register.php';
require dirname( __FILE__ ) . '/demo.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';
}
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';
}
require dirname( __FILE__ ) . '/blocks.php';
7 changes: 4 additions & 3 deletions packages/block-library/src/archives/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"title": "Archives",
"category": "widgets",
"icon": {
"src": "./icon.js"
"src": "icon.js"
},
"description": "Display a monthly archive of your posts.",
"attributes": {
Expand All @@ -22,10 +22,11 @@
"default": false
}
},
"edit": "./edit.js",
"edit": "edit.js",
"supports": {
"align": true,
"alignWide": false,
"html": false
}
},
"renderCallback": "index.php"
}
16 changes: 1 addition & 15 deletions packages/block-library/src/archives/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*
* @return string Returns the post content with archives added.
*/
function render_block_core_archives( $attributes ) {
function gutenberg_render_block_core_archives( $attributes ) {
$show_post_count = ! empty( $attributes['showPostCounts'] );

$class = 'wp-block-archives';
Expand Down Expand Up @@ -115,17 +115,3 @@ function render_block_core_archives( $attributes ) {

return $block_content;
}

/**
* Registers `core/archives` block.
*/
function register_block_core_archives() {
register_block_type_from_metadata(
dirname( __FILE__ ),
array(
'render_callback' => 'render_block_core_archives',
)
);
}

add_action( 'init', 'register_block_core_archives' );
5 changes: 3 additions & 2 deletions packages/block-library/src/block/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
"type": "number"
}
},
"edit": "./edit.js",
"edit": "edit.js",
"supports": {
"customClassName": false,
"html": false,
"inserter": false
}
},
"renderCallback": "index.php"
}
16 changes: 1 addition & 15 deletions packages/block-library/src/block/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* @return string Rendered HTML of the referenced block.
*/
function render_block_core_block( $attributes ) {
function gutenberg_render_block_core_block( $attributes ) {
if ( empty( $attributes['ref'] ) ) {
return '';
}
Expand All @@ -28,17 +28,3 @@ function render_block_core_block( $attributes ) {

return do_blocks( $reusable_block->post_content );
}

/**
* Registers `core/block` block.
*/
function register_block_core_block() {
register_block_type_from_metadata(
dirname( __FILE__ ),
array(
'render_callback' => 'render_block_core_block',
)
);
}

add_action( 'init', 'register_block_core_block' );
5 changes: 3 additions & 2 deletions packages/block-library/src/calendar/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
"type": "integer"
}
},
"edit": "./edit.js",
"edit": "edit.js",
"supports": {
"align": true
}
},
"renderCallback": "index.php"
}
16 changes: 1 addition & 15 deletions packages/block-library/src/calendar/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* @return string Returns the block content.
*/
function render_block_core_calendar( $attributes ) {
function gutenberg_render_block_core_calendar( $attributes ) {
global $monthnum, $year;

$previous_monthnum = $monthnum;
Expand Down Expand Up @@ -45,17 +45,3 @@ function render_block_core_calendar( $attributes ) {
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
$year = $previous_year;
}

/**
* Registers the `core/calendar` block on server.
*/
function register_block_core_calendar() {
register_block_type_from_metadata(
dirname( __FILE__ ),
array(
'render_callback' => 'render_block_core_calendar',
)
);
}

add_action( 'init', 'register_block_core_calendar' );
7 changes: 4 additions & 3 deletions packages/block-library/src/categories/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"title": "Categories",
"category": "widgets",
"icon": {
"src": "./icon.js"
"src": "icon.js"
},
"description": "Display a list of all categories.",
"attributes": {
Expand All @@ -26,10 +26,11 @@
"default": false
}
},
"edit": "./edit.js",
"edit": "edit.js",
"supports": {
"align": true,
"alignWide": false,
"html": false
}
},
"renderCallback": "index.php"
}
20 changes: 3 additions & 17 deletions packages/block-library/src/categories/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* @return string Returns the categories list/dropdown markup.
*/
function render_block_core_categories( $attributes ) {
function gutenberg_render_block_core_categories( $attributes ) {
static $block_id = 0;
$block_id++;

Expand All @@ -33,7 +33,7 @@ function render_block_core_categories( $attributes ) {
$type = 'dropdown';

if ( ! is_admin() ) {
$wrapper_markup .= build_dropdown_script_block_core_categories( $id );
$wrapper_markup .= gutenberg_build_dropdown_script_block_core_categories( $id );
}
} else {
$wrapper_markup = '<ul class="%1$s">%2$s</ul>';
Expand Down Expand Up @@ -67,7 +67,7 @@ function render_block_core_categories( $attributes ) {
*
* @return string Returns the dropdown onChange redirection script.
*/
function build_dropdown_script_block_core_categories( $dropdown_id ) {
function gutenberg_build_dropdown_script_block_core_categories( $dropdown_id ) {
ob_start();
?>
<script type='text/javascript'>
Expand All @@ -86,17 +86,3 @@ function onCatChange() {
<?php
return ob_get_clean();
}

/**
* Registers the `core/categories` block on server.
*/
function register_block_core_categories() {
register_block_type_from_metadata(
dirname( __FILE__ ),
array(
'render_callback' => 'render_block_core_categories',
)
);
}

add_action( 'init', 'register_block_core_categories' );
32 changes: 16 additions & 16 deletions packages/block-library/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ import * as image from './image';
import * as heading from './heading';
import * as quote from './quote';
import * as gallery from './gallery';
import * as archives from './archives';
// import * as archives from './archives';
import * as audio from './audio';
import * as button from './button';
import * as calendar from './calendar';
import * as categories from './categories';
// import * as calendar from './calendar';
// import * as categories from './categories';
import * as code from './code';
import * as columns from './columns';
import * as column from './columns/column';
Expand All @@ -32,16 +32,16 @@ import * as embed from './embed';
import * as file from './file';
import * as html from './html';
import * as mediaText from './media-text';
import * as latestComments from './latest-comments';
import * as latestPosts from './latest-posts';
// import * as latestComments from './latest-comments';
// import * as latestPosts from './latest-posts';
import * as list from './list';
import * as missing from './missing';
import * as more from './more';
import * as nextpage from './nextpage';
import * as preformatted from './preformatted';
import * as pullquote from './pullquote';
import * as reusableBlock from './block';
import * as rss from './rss';
// import * as reusableBlock from './block';
// import * as rss from './rss';
import * as search from './search';
import * as separator from './separator';
import * as shortcode from './shortcode';
Expand All @@ -52,7 +52,7 @@ import * as template from './template';
import * as textColumns from './text-columns';
import * as verse from './verse';
import * as video from './video';
import * as tagCloud from './tag-cloud';
// import * as tagCloud from './tag-cloud';

import * as classic from './classic';

Expand All @@ -69,11 +69,11 @@ export const registerCoreBlocks = () => {

// Register all remaining core blocks.
shortcode,
archives,
// archives,
audio,
button,
calendar,
categories,
// calendar,
// categories,
code,
columns,
column,
Expand All @@ -85,21 +85,21 @@ export const registerCoreBlocks = () => {
window.wp && window.wp.oldEditor ? classic : null, // Only add the classic block in WP Context
html,
mediaText,
latestComments,
latestPosts,
// latestComments,
// latestPosts,
missing,
more,
nextpage,
preformatted,
pullquote,
rss,
// rss,
search,
separator,
reusableBlock,
// reusableBlock,
spacer,
subhead,
table,
tagCloud,
// tagCloud,
template,
textColumns,
verse,
Expand Down
Loading

0 comments on commit 625c7e7

Please sign in to comment.