Widgets: Enqueue assets needed by media and text widgets in their _register_one() methods.

The `WP_Widget::_register_one()` method is more guaranteed to be called as opposed to its wrapper `WP_Widget::_register()` which plugins may bypass for performance reasons.

Amends [40631], [40640].
See #35243, #32417.
Fixes #41021.

Built from https://develop.svn.wordpress.org/trunk@41028


git-svn-id: http://core.svn.wordpress.org/trunk@40878 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Weston Ruter 2017-07-11 22:47:43 +00:00
parent 6724151d41
commit 37cbe7add1
3 changed files with 34 additions and 9 deletions

View File

@ -4,7 +4,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '4.9-alpha-41027'; $wp_version = '4.9-alpha-41028';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.

View File

@ -33,6 +33,14 @@ abstract class WP_Widget_Media extends WP_Widget {
'add_media' => '', 'add_media' => '',
); );
/**
* Whether or not the widget has been registered yet.
*
* @since 4.8.1
* @var bool
*/
protected $registered = false;
/** /**
* Constructor. * Constructor.
* *
@ -86,8 +94,16 @@ abstract class WP_Widget_Media extends WP_Widget {
* *
* @since 4.8.0 * @since 4.8.0
* @access public * @access public
*
* @param integer $number Optional. The unique order number of this widget instance
* compared to other instances of the same class. Default -1.
*/ */
public function _register() { public function _register_one( $number = -1 ) {
parent::_register_one( $number );
if ( $this->registered ) {
return;
}
$this->registered = true;
// Note that the widgets component in the customizer will also do the 'admin_print_scripts-widgets.php' action in WP_Customize_Widgets::print_scripts(). // Note that the widgets component in the customizer will also do the 'admin_print_scripts-widgets.php' action in WP_Customize_Widgets::print_scripts().
add_action( 'admin_print_scripts-widgets.php', array( $this, 'enqueue_admin_scripts' ) ); add_action( 'admin_print_scripts-widgets.php', array( $this, 'enqueue_admin_scripts' ) );
@ -100,8 +116,6 @@ abstract class WP_Widget_Media extends WP_Widget {
add_action( 'admin_footer-widgets.php', array( $this, 'render_control_template_scripts' ) ); add_action( 'admin_footer-widgets.php', array( $this, 'render_control_template_scripts' ) );
add_filter( 'display_media_states', array( $this, 'display_media_state' ), 10, 2 ); add_filter( 'display_media_states', array( $this, 'display_media_state' ), 10, 2 );
parent::_register();
} }
/** /**

View File

@ -16,6 +16,14 @@
*/ */
class WP_Widget_Text extends WP_Widget { class WP_Widget_Text extends WP_Widget {
/**
* Whether or not the widget has been registered yet.
*
* @since 4.8.1
* @var bool
*/
protected $registered = false;
/** /**
* Sets up a new Text widget instance. * Sets up a new Text widget instance.
* *
@ -38,18 +46,21 @@ class WP_Widget_Text extends WP_Widget {
/** /**
* Add hooks for enqueueing assets when registering all widget instances of this widget class. * Add hooks for enqueueing assets when registering all widget instances of this widget class.
* *
* @since 4.8.0 * @param integer $number Optional. The unique order number of this widget instance
* @access public * compared to other instances of the same class. Default -1.
*/ */
public function _register() { public function _register_one( $number = -1 ) {
parent::_register_one( $number );
if ( $this->registered ) {
return;
}
$this->registered = true;
// Note that the widgets component in the customizer will also do the 'admin_print_scripts-widgets.php' action in WP_Customize_Widgets::print_scripts(). // Note that the widgets component in the customizer will also do the 'admin_print_scripts-widgets.php' action in WP_Customize_Widgets::print_scripts().
add_action( 'admin_print_scripts-widgets.php', array( $this, 'enqueue_admin_scripts' ) ); add_action( 'admin_print_scripts-widgets.php', array( $this, 'enqueue_admin_scripts' ) );
// Note that the widgets component in the customizer will also do the 'admin_footer-widgets.php' action in WP_Customize_Widgets::print_footer_scripts(). // Note that the widgets component in the customizer will also do the 'admin_footer-widgets.php' action in WP_Customize_Widgets::print_footer_scripts().
add_action( 'admin_footer-widgets.php', array( $this, 'render_control_template_scripts' ) ); add_action( 'admin_footer-widgets.php', array( $this, 'render_control_template_scripts' ) );
parent::_register();
} }
/** /**