Make WordPress Core

Changeset 55553


Ignore:
Timestamp:
03/15/2023 11:36:52 AM (12 months ago)
Author:
spacedmonkey
Message:

Widgets: Defer register inline script in WP_Widget_Custom_HTML and WP_Widget_Text.

In [41376] an inline script was added to push idBases for the custom html and text widgets. However, this script is not used unless the widget script is output in the widget screen / customizer. Deferring registering this script until it is needed, results in a faster server response times.

Props spacedmonkey, sakibmd, flixos90, westonruter.
Fixes #57864.

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/package-lock.json

    r55542 r55553  
    1194211942            "version": "1.1.0",
    1194311943            "resolved": "https://registry.npmjs.org/from2-string/-/from2-string-1.1.0.tgz",
    11944             "integrity": "sha512-m8vCh+KnXXXBtfF2VUbiYlQ+nczLcntB0BrtNgpmLkHylhObe9WF1b2LZjBBzrZzA6P4mkEla6ZYQoOUTG8cYA==",
     11944            "integrity": "sha1-GCgrJ9CKJnyzAwzSuLSw8hKvdSo=",
    1194511945            "requires": {
    1194611946                "from2": "^2.0.3"
     
    2110021100            "version": "1.1.6",
    2110121101            "resolved": "https://registry.npmjs.org/promise-polyfill/-/promise-polyfill-1.1.6.tgz",
    21102             "integrity": "sha512-7rrONfyLkDEc7OJ5QBkqa4KI4EBhCd340xRuIUPGCfu13znS+vx+VDdrT9ODAJHlXm7w4lbxN3DRjyv58EuzDg=="
     21102            "integrity": "sha1-zQTv9G9clcOn0EVZHXm14+AfEtc="
    2110321103        },
    2110421104        "prompts": {
     
    2323923239            "version": "1.0.0",
    2324023240            "resolved": "https://registry.npmjs.org/stream-from-promise/-/stream-from-promise-1.0.0.tgz",
    23241             "integrity": "sha512-j84KLkudt+gr8KJ21RB02btPLx61uGbrLnewsWz6QKmsz8/c4ZFqXw6mJh5+G4oRN7DgDxdbjPxnpySpg1mUig=="
     23241            "integrity": "sha1-djaH9913fkyJT2QIMz/Gs/yKYbs="
    2324223242        },
    2324323243        "stream-to-string": {
     
    2450724507            "version": "2.0.2",
    2450824508            "resolved": "https://registry.npmjs.org/toposort/-/toposort-2.0.2.tgz",
    24509             "integrity": "sha512-0a5EOkAUp8D4moMi2W8ZF8jcga7BgZd91O/yabJCFY8az+XSzeGyTKs0Aoo897iV1Nj6guFq8orWDS96z91oGg=="
     24509            "integrity": "sha1-riF2gXXRVZ1IvvNUILL0li8JwzA="
    2451024510        },
    2451124511        "totalist": {
  • trunk/src/wp-includes/widgets/class-wp-widget-custom-html.php

    r55276 r55553  
    6969        }
    7070        $this->registered = true;
    71 
    72         wp_add_inline_script( 'custom-html-widgets', sprintf( 'wp.customHtmlWidgets.idBases.push( %s );', wp_json_encode( $this->id_base ) ) );
    7371
    7472        // Note that the widgets component in the customizer will also do
     
    217215
    218216        wp_enqueue_script( 'custom-html-widgets' );
     217        wp_add_inline_script( 'custom-html-widgets', sprintf( 'wp.customHtmlWidgets.idBases.push( %s );', wp_json_encode( $this->id_base ) ) );
     218
    219219        if ( empty( $settings ) ) {
    220220            $settings = array(
  • trunk/src/wp-includes/widgets/class-wp-widget-text.php

    r55276 r55553  
    5656        }
    5757        $this->registered = true;
    58 
    59         wp_add_inline_script( 'text-widgets', sprintf( 'wp.textWidgets.idBases.push( %s );', wp_json_encode( $this->id_base ) ) );
    6058
    6159        if ( $this->is_preview() ) {
     
    437435        wp_enqueue_media();
    438436        wp_enqueue_script( 'text-widgets' );
     437        wp_add_inline_script( 'text-widgets', sprintf( 'wp.textWidgets.idBases.push( %s );', wp_json_encode( $this->id_base ) ) );
    439438        wp_add_inline_script( 'text-widgets', 'wp.textWidgets.init();', 'after' );
    440439    }
  • trunk/tests/phpunit/tests/widgets/wpWidgetCustomHtml.php

    r54088 r55553  
    6868        $this->assertSame( 10, has_action( 'admin_footer-widgets.php', array( 'WP_Widget_Custom_HTML', 'render_control_template_scripts' ) ) );
    6969        $this->assertSame( 10, has_action( 'admin_head-widgets.php', array( 'WP_Widget_Custom_HTML', 'add_help_text' ) ) );
    70         $this->assertContains( 'wp.customHtmlWidgets.idBases.push( "custom_html" );', wp_scripts()->registered['custom-html-widgets']->extra['after'] );
    7170    }
    7271
  • trunk/tests/phpunit/tests/widgets/wpWidgetText.php

    r54088 r55553  
    6868        $this->assertSame( 10, has_action( 'admin_print_scripts-widgets.php', array( $widget, 'enqueue_admin_scripts' ) ) );
    6969        $this->assertSame( 10, has_action( 'admin_footer-widgets.php', array( 'WP_Widget_Text', 'render_control_template_scripts' ) ) );
    70         $this->assertContains( 'wp.textWidgets.idBases.push( "text" );', wp_scripts()->registered['text-widgets']->extra['after'] );
    7170        $this->assertFalse( has_action( 'wp_enqueue_scripts', array( $widget, 'enqueue_preview_scripts' ) ) );
    7271    }
Note: See TracChangeset for help on using the changeset viewer.