Make WordPress Core

Changeset 41052


Ignore:
Timestamp:
07/14/2017 05:22:54 PM (7 years ago)
Author:
westonruter
Message:

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.

Location:
branches/4.8
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/4.8

  • branches/4.8/src/wp-includes/widgets/class-wp-widget-media.php

    r40812 r41052  
    3333        'add_media' => '',
    3434    );
     35
     36    /**
     37     * Whether or not the widget has been registered yet.
     38     *
     39     * @since 4.8.1
     40     * @var bool
     41     */
     42    protected $registered = false;
    3543
    3644    /**
     
    8795     * @since 4.8.0
    8896     * @access public
    89      */
    90     public function _register() {
     97     *
     98     * @param integer $number Optional. The unique order number of this widget instance
     99     *                        compared to other instances of the same class. Default -1.
     100     */
     101    public function _register_one( $number = -1 ) {
     102        parent::_register_one( $number );
     103        if ( $this->registered ) {
     104            return;
     105        }
     106        $this->registered = true;
    91107
    92108        // Note that the widgets component in the customizer will also do the 'admin_print_scripts-widgets.php' action in WP_Customize_Widgets::print_scripts().
     
    101117
    102118        add_filter( 'display_media_states', array( $this, 'display_media_state' ), 10, 2 );
    103 
    104         parent::_register();
    105119    }
    106120
  • branches/4.8/src/wp-includes/widgets/class-wp-widget-text.php

    r41044 r41052  
    1616 */
    1717class WP_Widget_Text extends WP_Widget {
     18
     19    /**
     20     * Whether or not the widget has been registered yet.
     21     *
     22     * @since 4.8.1
     23     * @var bool
     24     */
     25    protected $registered = false;
    1826
    1927    /**
     
    3947     * Add hooks for enqueueing assets when registering all widget instances of this widget class.
    4048     *
    41      * @since 4.8.0
    42      * @access public
    43      */
    44     public function _register() {
     49     * @param integer $number Optional. The unique order number of this widget instance
     50     *                        compared to other instances of the same class. Default -1.
     51     */
     52    public function _register_one( $number = -1 ) {
     53        parent::_register_one( $number );
     54        if ( $this->registered ) {
     55            return;
     56        }
     57        $this->registered = true;
    4558
    4659        // Note that the widgets component in the customizer will also do the 'admin_print_scripts-widgets.php' action in WP_Customize_Widgets::print_scripts().
     
    4962        // Note that the widgets component in the customizer will also do the 'admin_footer-widgets.php' action in WP_Customize_Widgets::print_footer_scripts().
    5063        add_action( 'admin_footer-widgets.php', array( $this, 'render_control_template_scripts' ) );
    51 
    52         parent::_register();
    5364    }
    5465
Note: See TracChangeset for help on using the changeset viewer.