Make WordPress Core

Changeset 51393


Ignore:
Timestamp:
07/09/2021 05:56:43 PM (3 years ago)
Author:
desrosj
Message:

Widgets: Warn when wp-editor script or wp-edit-post style is enqueued in widgets editor

It is common that plugins erroneously have wp-editor or wp-edit-post as a
dependency in a script that is loaded in the new widgets editor. This is a smell
since both @wordpress/editor and @wordpress/edit-post assume the existence
of a global "post" object which the widgets editor does not have.

[51387] fixes the user-facing errors typically caused by this mistake, but we
can go a step further and warn developers about this by calling
_doing_it_wrong() when we detect that the wp-editor script or wp-edit-post
style is enqueued alongside wp-edit-widgets or wp-customize-widgets.

Props zieladam, spacedmonkey, TimothyBlynJacobs, andraganescu, dlh, noisysocks.
Merges [51388] to the 5.8 branch.
Fixes #53569. See #53437.

Location:
branches/5.8
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/5.8

  • branches/5.8/src/wp-includes/default-filters.php

    r51347 r51393  
    567567add_filter( 'print_scripts_array', 'wp_prototype_before_jquery' );
    568568add_filter( 'customize_controls_print_styles', 'wp_resource_hints', 1 );
     569add_action( 'admin_head', 'wp_check_widget_editor_deps' );
    569570
    570571// Global styles can be enqueued in both the header and the footer. See https://core.trac.wordpress.org/ticket/53494.
  • branches/5.8/src/wp-includes/widgets.php

    r51219 r51393  
    20072007    return ob_get_clean();
    20082008}
     2009
     2010/**
     2011 * The 'wp-editor' script module is exposed as window.wp.editor. This overrides
     2012 * the legacy TinyMCE editor module which is required by the widgets editor.
     2013 * Because of that conflict, these two shouldn't be enqueued together. See
     2014 * https://core.trac.wordpress.org/ticket/53569.
     2015 *
     2016 * There is also another conflict related to styles where the block widgets
     2017 * editor is hidden if a block enqueues 'wp-edit-post' stylesheet. See
     2018 * https://core.trac.wordpress.org/ticket/53569.
     2019 *
     2020 * @since 5.8.0
     2021 * @access private
     2022 */
     2023function wp_check_widget_editor_deps() {
     2024    global $wp_scripts, $wp_styles;
     2025    if (
     2026        $wp_scripts->query( 'wp-edit-widgets', 'enqueued' ) ||
     2027        $wp_scripts->query( 'wp-customize-widgets', 'enqueued' )
     2028    ) {
     2029        if ( $wp_scripts->query( 'wp-editor', 'enqueued' ) ) {
     2030            _doing_it_wrong(
     2031                'enqueue_script',
     2032                '"wp-editor" script should not be enqueued together with the new widgets editor (wp-edit-widgets or wp-customize-widgets).',
     2033                '5.8.0'
     2034            );
     2035        }
     2036        if ( $wp_styles->query( 'wp-edit-post', 'enqueued' ) ) {
     2037            _doing_it_wrong(
     2038                'enqueue_style',
     2039                '"wp-edit-post" style should not be enqueued together with the new widgets editor (wp-edit-widgets or wp-customize-widgets).',
     2040                '5.8.0'
     2041            );
     2042        }
     2043    }
     2044}
Note: See TracChangeset for help on using the changeset viewer.