From 625c7e7bf789aa418b720e53d8d044aa3662e4d8 Mon Sep 17 00:00:00 2001 From: Grzegorz Ziolkowski Date: Tue, 5 Mar 2019 15:48:07 +0100 Subject: [PATCH] Add a way to override core block registration based on metadata --- lib/blocks.php | 62 +++++++++++++++++++ lib/load.php | 36 +---------- .../block-library/src/archives/block.json | 7 ++- packages/block-library/src/archives/index.php | 16 +---- packages/block-library/src/block/block.json | 5 +- packages/block-library/src/block/index.php | 16 +---- .../block-library/src/calendar/block.json | 5 +- packages/block-library/src/calendar/index.php | 16 +---- .../block-library/src/categories/block.json | 7 ++- .../block-library/src/categories/index.php | 20 +----- packages/block-library/src/index.js | 32 +++++----- .../src/latest-comments/block.json | 7 ++- .../src/latest-comments/index.php | 20 +----- .../block-library/src/latest-posts/block.json | 7 ++- .../block-library/src/latest-posts/index.php | 16 +---- packages/block-library/src/rss/block.json | 9 ++- packages/block-library/src/rss/index.php | 17 +---- packages/block-library/src/search/index.php | 4 +- .../block-library/src/shortcode/index.php | 4 +- .../block-library/src/tag-cloud/block.json | 9 ++- .../block-library/src/tag-cloud/index.php | 17 +---- 21 files changed, 125 insertions(+), 207 deletions(-) create mode 100644 lib/blocks.php diff --git a/lib/blocks.php b/lib/blocks.php new file mode 100644 index 00000000000000..3049b03c19232f --- /dev/null +++ b/lib/blocks.php @@ -0,0 +1,62 @@ + $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' ); diff --git a/lib/load.php b/lib/load.php index 999523ed52ff68..be3c4d429f96bd 100644 --- a/lib/load.php +++ b/lib/load.php @@ -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'; diff --git a/packages/block-library/src/archives/block.json b/packages/block-library/src/archives/block.json index a75a6f4b9e568a..b0715ec051c146 100644 --- a/packages/block-library/src/archives/block.json +++ b/packages/block-library/src/archives/block.json @@ -3,7 +3,7 @@ "title": "Archives", "category": "widgets", "icon": { - "src": "./icon.js" + "src": "icon.js" }, "description": "Display a monthly archive of your posts.", "attributes": { @@ -22,10 +22,11 @@ "default": false } }, - "edit": "./edit.js", + "edit": "edit.js", "supports": { "align": true, "alignWide": false, "html": false - } + }, + "renderCallback": "index.php" } diff --git a/packages/block-library/src/archives/index.php b/packages/block-library/src/archives/index.php index 017cb4ec21d1d9..59689ebe079864 100644 --- a/packages/block-library/src/archives/index.php +++ b/packages/block-library/src/archives/index.php @@ -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'; @@ -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' ); diff --git a/packages/block-library/src/block/block.json b/packages/block-library/src/block/block.json index bd9c10edce16da..c1cf093e535cba 100644 --- a/packages/block-library/src/block/block.json +++ b/packages/block-library/src/block/block.json @@ -8,10 +8,11 @@ "type": "number" } }, - "edit": "./edit.js", + "edit": "edit.js", "supports": { "customClassName": false, "html": false, "inserter": false - } + }, + "renderCallback": "index.php" } diff --git a/packages/block-library/src/block/index.php b/packages/block-library/src/block/index.php index 72dcfe0fc34fd9..6fdf5cec9a1fa2 100644 --- a/packages/block-library/src/block/index.php +++ b/packages/block-library/src/block/index.php @@ -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 ''; } @@ -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' ); diff --git a/packages/block-library/src/calendar/block.json b/packages/block-library/src/calendar/block.json index f7ffcf528d1743..fee67cd22f2049 100644 --- a/packages/block-library/src/calendar/block.json +++ b/packages/block-library/src/calendar/block.json @@ -21,8 +21,9 @@ "type": "integer" } }, - "edit": "./edit.js", + "edit": "edit.js", "supports": { "align": true - } + }, + "renderCallback": "index.php" } diff --git a/packages/block-library/src/calendar/index.php b/packages/block-library/src/calendar/index.php index ab7b75f380f15b..3f770742ae6448 100644 --- a/packages/block-library/src/calendar/index.php +++ b/packages/block-library/src/calendar/index.php @@ -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; @@ -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' ); diff --git a/packages/block-library/src/categories/block.json b/packages/block-library/src/categories/block.json index 26a5eb57ba13e1..f47f0d6a730ac2 100644 --- a/packages/block-library/src/categories/block.json +++ b/packages/block-library/src/categories/block.json @@ -3,7 +3,7 @@ "title": "Categories", "category": "widgets", "icon": { - "src": "./icon.js" + "src": "icon.js" }, "description": "Display a list of all categories.", "attributes": { @@ -26,10 +26,11 @@ "default": false } }, - "edit": "./edit.js", + "edit": "edit.js", "supports": { "align": true, "alignWide": false, "html": false - } + }, + "renderCallback": "index.php" } diff --git a/packages/block-library/src/categories/index.php b/packages/block-library/src/categories/index.php index 2936ae2d1e0aa5..359d96e20aef7d 100644 --- a/packages/block-library/src/categories/index.php +++ b/packages/block-library/src/categories/index.php @@ -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++; @@ -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 = ''; @@ -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(); ?>