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

Widgets editor: Improve compatibility with WP 5.8 #32296

Closed
wants to merge 3 commits into from
Closed
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
1 change: 1 addition & 0 deletions lib/compat/wordpress-5.8/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
}

require_once __DIR__ . '/block-editor.php';
require_once __DIR__ . '/widgets.php';

if ( ! function_exists( 'build_query_vars_from_query_block' ) ) {
/**
Expand Down
29 changes: 26 additions & 3 deletions lib/widgets-api.php → lib/compat/wordpress-5.8/widgets.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php
/**
* Functions for working with widgets in WordPress. These should ultimately live
* in wp-includes/widgets.php, wp-admin/includes/widgets.php, etc. when merged
* to Core.
* Functions for working with widgets in WordPress. These exist in core and
* should be removed when WordPress 5.8.0 becomes the lowest supported version
* by this plugin.
*
* @package gutenberg
*/
Expand Down Expand Up @@ -242,3 +242,26 @@ function gutenberg_get_widget_object( $id_base ) {
return null;
}
}

if ( ! function_exists( 'gutenberg_use_widgets_block_editor' ) ) {
/**
* Whether or not to use the block editor to manage widgets. Defaults to true
* unless a theme has removed support for widgets-block-editor or a plugin has
* filtered the return value of this function.
*
* Can be removed when minimum WordPress version is 5.8.
*
* @return boolean Whether or not to use the block editor to manage widgets.
*/
function gutenberg_use_widgets_block_editor() {
/**
* Filters whether or not to use the block editor to manage widgets.
*
* @param boolean $use_widgets_block_editor Whether or not to use the block editor to manage widgets.
*/
return apply_filters(
'gutenberg_use_widgets_block_editor',
get_theme_support( 'widgets-block-editor' )
);
}
}
17 changes: 13 additions & 4 deletions lib/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ function gutenberg_menu() {
'gutenberg'
);

if ( gutenberg_use_widgets_block_editor() ) {
if (
gutenberg_use_widgets_block_editor() &&
! function_exists( 'wp_use_widgets_block_editor' )
) {
add_theme_page(
__( 'Widgets', 'gutenberg' ),
__( 'Widgets', 'gutenberg' ),
Expand Down Expand Up @@ -113,7 +116,11 @@ function gutenberg_site_editor_menu() {
* @param WP_Admin_Bar $wp_admin_bar Core class used to implement the Toolbar API.
*/
function modify_admin_bar( $wp_admin_bar ) {
if ( gutenberg_use_widgets_block_editor() && $wp_admin_bar->get_node( 'widgets' ) !== null ) {
if (
gutenberg_use_widgets_block_editor() &&
! function_exists( 'wp_use_widgets_block_editor' ) &&
$wp_admin_bar->get_node( 'widgets' ) !== null
) {
$wp_admin_bar->add_menu(
array(
'id' => 'widgets',
Expand All @@ -137,7 +144,10 @@ function modify_welcome_panel() {
ob_start();
wp_welcome_panel();
$welcome_panel = ob_get_clean();
if ( gutenberg_use_widgets_block_editor() ) {
if (
gutenberg_use_widgets_block_editor() &&
! function_exists( 'wp_use_widgets_block_editor' )
) {
echo str_replace(
admin_url( 'widgets.php' ),
admin_url( 'themes.php?page=gutenberg-widgets' ),
Expand Down Expand Up @@ -177,5 +187,4 @@ function register_site_icon_url( $response ) {

add_filter( 'rest_index', 'register_site_icon_url' );

add_theme_support( 'widgets-block-editor' );
add_theme_support( 'block-templates' );
13 changes: 5 additions & 8 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/rest-api.php';
}

// We can't use class_exists( 'WP_Widget_Block' ) because core loads widgets
// *after* plugins, so test for wp_use_widgets_block_editor() which we know
// implies the existence of WP_Widget_Block.
// These files can be removed when 5.8.0 is the minimum supported version by
// this plugin.
if ( ! function_exists( 'wp_use_widgets_block_editor' ) ) {
require_once __DIR__ . '/class-wp-widget-block.php';
require_once __DIR__ . '/widgets-page.php';
require __DIR__ . '/widgets.php';
require __DIR__ . '/widgets-customize.php';
}

require_once __DIR__ . '/widgets-page.php';

require __DIR__ . '/compat.php';
require __DIR__ . '/compat/wordpress-5.8/index.php';
require __DIR__ . '/utils.php';
Expand Down Expand Up @@ -111,9 +111,6 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/block-patterns.php';
require __DIR__ . '/client-assets.php';
require __DIR__ . '/demo.php';
require __DIR__ . '/widgets.php';
require __DIR__ . '/widgets-api.php';
require __DIR__ . '/widgets-customize.php';
require __DIR__ . '/navigation.php';
require __DIR__ . '/navigation-page.php';
require __DIR__ . '/experiments-page.php';
Expand Down
12 changes: 4 additions & 8 deletions lib/widgets-customize.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,7 @@ function gutenberg_widgets_customize_load_block_editor_scripts_and_styles( $is_b
return $is_block_editor_screen;
}

// Test for wp_use_widgets_block_editor(), as the existence of this in core
// implies that the Customizer already supports the widgets block editor.
if ( ! function_exists( 'wp_use_widgets_block_editor' ) ) {
add_action( 'customize_register', 'gutenberg_widgets_customize_register' );
add_filter( 'widget_customizer_setting_args', 'gutenberg_widgets_customize_add_unstable_instance', 10, 2 );
add_action( 'customize_controls_enqueue_scripts', 'gutenberg_customize_widgets_init' );
add_filter( 'should_load_block_editor_scripts_and_styles', 'gutenberg_widgets_customize_load_block_editor_scripts_and_styles' );
}
add_action( 'customize_register', 'gutenberg_widgets_customize_register' );
add_filter( 'widget_customizer_setting_args', 'gutenberg_widgets_customize_add_unstable_instance', 10, 2 );
add_action( 'customize_controls_enqueue_scripts', 'gutenberg_customize_widgets_init' );
add_filter( 'should_load_block_editor_scripts_and_styles', 'gutenberg_widgets_customize_load_block_editor_scripts_and_styles' );
21 changes: 2 additions & 19 deletions lib/widgets.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,6 @@ function gutenberg_is_block_editor() {
);
}

/**
* Whether or not to use the block editor to manage widgets. Defaults to true
* unless a theme has removed support for widgets-block-editor or a plugin has
* filtered the return value of this function.
*
* @return boolean Whether or not to use the block editor to manage widgets.
*/
function gutenberg_use_widgets_block_editor() {
/**
* Filters whether or not to use the block editor to manage widgets.
*
* @param boolean $use_widgets_block_editor Whether or not to use the block editor to manage widgets.
*/
return apply_filters(
'gutenberg_use_widgets_block_editor',
get_theme_support( 'widgets-block-editor' )
);
}

/**
* Emulates the Widgets screen `admin_print_styles` when at the block editor
* screen.
Expand Down Expand Up @@ -309,3 +290,5 @@ function gutenberg_set_show_instance_in_rest_on_core_widgets() {
}
}
add_action( 'widgets_init', 'gutenberg_set_show_instance_in_rest_on_core_widgets' );

add_theme_support( 'widgets-block-editor' );
3 changes: 1 addition & 2 deletions packages/block-library/src/legacy-widget/edit/preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ export default function Preview( { idBase, instance, isVisible } ) {
// TODO: This chokes when the query param is too big.
// Ideally, we'd render a <ServerSideRender>. Maybe by
// rendering one in an iframe via a portal.
src={ addQueryArgs( 'themes.php', {
page: 'gutenberg-widgets',
src={ addQueryArgs( 'widgets.php', {
'legacy-widget-preview': {
idBase,
instance,
Expand Down
26 changes: 18 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,10 @@ function init_legacy_widget_block() {
exit;
}

add_action( 'init', 'init_legacy_widget_block' );
// Run handle_legacy_widget_preview_iframe() on 'init' at the same priority as
// register_block_core_legacy_widget(). This ensures that the block will be
// registered by time we intercept the iframe request. The priority will be 20
// in the Gutenberg plugin (as opposed to 10 in Core) because of a find and
// replace in Gutenberg's webpack config.
$priority = has_action( 'init', 'register_block_core_legacy_widget' );
add_action( 'init', 'handle_legacy_widget_preview_iframe', $priority );
Loading