WordPress.org

Make WordPress Core

Ticket #41540: 41540.1.diff

File 41540.1.diff, 4.1 KB (added by westonruter, 4 years ago)

Prevent Text widget underscore templates from being output more than once: https://github.com/xwp/wordpress-develop/pull/244/commits/6844c84

  • src/wp-admin/js/widgets/text-widgets.js

    diff --git src/wp-admin/js/widgets/text-widgets.js src/wp-admin/js/widgets/text-widgets.js
    index 692a0642c9..bb19f2ffb2 100644
    wp.textWidgets = ( function( $ ) { 
    44        'use strict';
    55
    66        var component = {
    7                 dismissedPointers: []
     7                dismissedPointers: [],
     8                idBases: [ 'text' ]
    89        };
    910
    1011        /**
    wp.textWidgets = ( function( $ ) { 
    357358                widgetForm = widgetContainer.find( '> .widget-inside > .form, > .widget-inside > form' ); // Note: '.form' appears in the customizer, whereas 'form' on the widgets admin screen.
    358359
    359360                idBase = widgetForm.find( '> .id_base' ).val();
    360                 if ( 'text' !== idBase ) {
     361                if ( -1 === component.idBases.indexOf( idBase ) ) {
    361362                        return;
    362363                }
    363364
    wp.textWidgets = ( function( $ ) { 
    424425                }
    425426
    426427                idBase = widgetForm.find( '> .widget-control-actions > .id_base' ).val();
    427                 if ( 'text' !== idBase ) {
     428                if ( -1 === component.idBases.indexOf( idBase ) ) {
    428429                        return;
    429430                }
    430431
    wp.textWidgets = ( function( $ ) { 
    461462                widgetForm = widgetContainer.find( '> .widget-inside > .form, > .widget-inside > form' );
    462463
    463464                idBase = widgetForm.find( '> .id_base' ).val();
    464                 if ( 'text' !== idBase ) {
     465                if ( -1 === component.idBases.indexOf( idBase ) ) {
    465466                        return;
    466467                }
    467468
  • src/wp-includes/widgets/class-wp-widget-text.php

    diff --git src/wp-includes/widgets/class-wp-widget-text.php src/wp-includes/widgets/class-wp-widget-text.php
    index c28ed1f317..b996595585 100644
    class WP_Widget_Text extends WP_Widget { 
    5555                }
    5656                $this->registered = true;
    5757
     58                wp_add_inline_script( 'text-widgets', sprintf( 'wp.textWidgets.idBases.push( %s );', wp_json_encode( $this->id_base ) ) );
     59
    5860                // Note that the widgets component in the customizer will also do the 'admin_print_scripts-widgets.php' action in WP_Customize_Widgets::print_scripts().
    5961                add_action( 'admin_print_scripts-widgets.php', array( $this, 'enqueue_admin_scripts' ) );
    6062
    6163                // Note that the widgets component in the customizer will also do the 'admin_footer-widgets.php' action in WP_Customize_Widgets::print_footer_scripts().
    62                 add_action( 'admin_footer-widgets.php', array( $this, 'render_control_template_scripts' ) );
     64                add_action( 'admin_footer-widgets.php', array( 'WP_Widget_Text', 'render_control_template_scripts' ) );
    6365        }
    6466
    6567        /**
    class WP_Widget_Text extends WP_Widget { 
    376378         * Render form template scripts.
    377379         *
    378380         * @since 4.8.0
     381         * @since 4.8.2 The method is now static.
    379382         */
    380         public function render_control_template_scripts() {
     383        static public function render_control_template_scripts() {
    381384                $dismissed_pointers = explode( ',', (string) get_user_meta( get_current_user_id(), 'dismissed_wp_pointers', true ) );
    382385                ?>
    383386                <script type="text/html" id="tmpl-widget-text-control-fields">
  • tests/phpunit/tests/widgets/text-widget.php

    diff --git tests/phpunit/tests/widgets/text-widget.php tests/phpunit/tests/widgets/text-widget.php
    index 41aadb6af7..6c7ec1f102 100644
    class Test_WP_Widget_Text extends WP_UnitTestCase { 
    6464                $widget->_register();
    6565
    6666                $this->assertEquals( 10, has_action( 'admin_print_scripts-widgets.php', array( $widget, 'enqueue_admin_scripts' ) ) );
    67                 $this->assertEquals( 10, has_action( 'admin_footer-widgets.php', array( $widget, 'render_control_template_scripts' ) ) );
     67                $this->assertEquals( 10, has_action( 'admin_footer-widgets.php', array( 'WP_Widget_Text', 'render_control_template_scripts' ) ) );
     68                $this->assertContains( 'wp.textWidgets.idBases.push( "text" );', wp_scripts()->registered['text-widgets']->extra['after'] );
    6869        }
    6970
    7071        /**
    class Test_WP_Widget_Text extends WP_UnitTestCase { 
    745746         * @covers WP_Widget_Text::render_control_template_scripts
    746747         */
    747748        function test_render_control_template_scripts() {
    748                 $widget = new WP_Widget_Text();
    749 
    750749                ob_start();
    751                 $widget->render_control_template_scripts();
     750                WP_Widget_Text::render_control_template_scripts();
    752751                $output = ob_get_clean();
    753752
    754753                $this->assertContains( '<script type="text/html" id="tmpl-widget-text-control-fields">', $output );