From 6daeb23005d028f4106b73cd70347b6d5d79d1ac Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Tue, 11 Jul 2017 22:46:45 +0000 Subject: [PATCH] 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. git-svn-id: https://develop.svn.wordpress.org/trunk@41028 602fd350-edb4-49c9-b593-d223f7449a82 --- .../widgets/class-wp-widget-media.php | 20 +++++++++++++++--- .../widgets/class-wp-widget-text.php | 21 ++++++++++++++----- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/wp-includes/widgets/class-wp-widget-media.php b/src/wp-includes/widgets/class-wp-widget-media.php index c76989c873..10f92abcea 100644 --- a/src/wp-includes/widgets/class-wp-widget-media.php +++ b/src/wp-includes/widgets/class-wp-widget-media.php @@ -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(); } /** diff --git a/src/wp-includes/widgets/class-wp-widget-text.php b/src/wp-includes/widgets/class-wp-widget-text.php index 7d149c0a0f..faa79674f6 100644 --- a/src/wp-includes/widgets/class-wp-widget-text.php +++ b/src/wp-includes/widgets/class-wp-widget-text.php @@ -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(); } /**