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.

Merges [41028] to the 4.8 branch.
Amends [40631], [40640].
See #35243, #32417.
Fixes #41021 for 4.8.1.

Built from https://develop.svn.wordpress.org/branches/4.8@41052


git-svn-id: http://core.svn.wordpress.org/branches/4.8@40902 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Weston Ruter 2017-07-14 17:23:32 +00:00
parent 2e43bdea3e
commit e5f6b9078e
2 changed files with 33 additions and 8 deletions

View File

@ -33,6 +33,14 @@ abstract class WP_Widget_Media extends WP_Widget {
'add_media' => '',
);
/**
* Whether or not the widget has been registered yet.
*
* @since 4.8.1
* @var bool
*/
protected $registered = false;
/**
* Constructor.
*
@ -86,8 +94,16 @@ abstract class WP_Widget_Media extends WP_Widget {
*
* @since 4.8.0
* @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().
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_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 {
/**
* 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.
*
@ -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.
*
* @since 4.8.0
* @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().
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().
add_action( 'admin_footer-widgets.php', array( $this, 'render_control_template_scripts' ) );
parent::_register();
}
/**