Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix legacy widget block preview iframe #32300

Merged
merged 1 commit into from
Jun 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 );