Skip to content

Commit

Permalink
Fix legacy widget block preview iframe in plugin (#32300)
Browse files Browse the repository at this point in the history
  • Loading branch information
noisysocks authored and youknowriad committed Jun 7, 2021
1 parent f4d704f commit 1c7337e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
18 changes: 18 additions & 0 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,24 @@
* @package gutenberg
*/

/*
* Fixes the priority of register_block_core_legacy_widget().
*
* This hook was incorrectly added to Core with priority 20. #32300 fixes this
* but causes block registration warnings in the Gutenberg plugin until the
* changes are made in Core.
*
* This temporary fix can be removed after the changes to
* @wordpress/block-library in #32300 have been published to npm and updated in
* Core.
*
* See https://github.com/WordPress/gutenberg/pull/32300.
*/
if ( 20 === has_action( 'init', 'register_block_core_legacy_widget' ) ) {
remove_action( 'init', 'register_block_core_legacy_widget', 20 );
add_action( 'init', 'register_block_core_legacy_widget', 10 );
}

/**
* Substitutes the implementation of a core-registered block type, if exists,
* with the built result from the plugin.
Expand Down
23 changes: 15 additions & 8 deletions packages/block-library/src/legacy-widget/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,25 @@ function render_block_core_legacy_widget( $attributes ) {
}

/**
* On application init this does two things:
*
* - Registers the 'core/legacy-widget' block.
* - Intercepts any request with legacy-widget-preview in the query param and,
* if set, renders a page containing a preview of the requested Legacy Widget
* block.
* Registers the 'core/legacy-widget' block.
*/
function init_legacy_widget_block() {
function register_block_core_legacy_widget() {
register_block_type_from_metadata(
__DIR__ . '/legacy-widget',
array(
'render_callback' => 'render_block_core_legacy_widget',
)
);
}

add_action( 'init', 'register_block_core_legacy_widget' );

/**
* Intercepts any request with legacy-widget-preview in the query param and, if
* set, renders a page containing a preview of the requested Legacy Widget
* block.
*/
function handle_legacy_widget_preview_iframe() {
if ( empty( $_GET['legacy-widget-preview'] ) ) {
return;
}
Expand Down Expand Up @@ -110,4 +114,7 @@ function init_legacy_widget_block() {
exit;
}

add_action( 'init', 'init_legacy_widget_block' );
// Ensure handle_legacy_widget_preview_iframe() is called after Core's
// register_block_core_legacy_widget() (priority = 10) and after Gutenberg's
// register_block_core_legacy_widget() (priority = 20).
add_action( 'init', 'handle_legacy_widget_preview_iframe', 21 );

0 comments on commit 1c7337e

Please sign in to comment.