Make WordPress Core

Changeset 63545


Ignore:
Timestamp:
09/08/2026 09:33:53 PM (2 months ago)
Author:
westonruter
Message:

Administration: Wrap more one-liner inline scripts.

Continuing the migration begun in r63481, the raw one-liner SCRIPT tags in wp-admin/admin-footer.php, wp-admin/install.php and wp-admin/async-upload.php now print through wp_print_inline_script_tag(), so wp_inline_script_attributes can attach a per-request nonce to these as well.

The upload error handler is reworked in the process:

  1. Its data now crosses into JavaScript as a single wp_json_encode() object rather than being interpolated into the script text, which also removes the raw interpolation of the dismiss button's generated id.
  2. The jQuery object arrives as an argument instead of being read from the global scope four times.
  3. setTimeout() replaces the lone _.delay() call, which was all the snippet wanted from Underscore.

The two blocks that take an argument carry a JSDoc block naming its type, following the convention already used for the jQuery wrapper in wp-admin/js. These particular blocks are not reached by lint:jsdoc, which reads JavaScript files rather than heredocs embedded in PHP, but IDEs do check them.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/13393.
Follow-up to r60263, r60520, r60681, r63481.

Props thanhtinpk, westonruter.
See #59446, #66033.

Location:
trunk/src/wp-admin
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/admin-footer.php

    r62649 r63545  
    115115
    116116<div class="clear"></div></div><!-- wpwrap -->
    117 <script>if(typeof wpOnload==='function')wpOnload();</script>
     117<?php
     118wp_print_inline_script_tag(
     119        <<<'JS'
     120        if ( typeof wpOnload === 'function' ) {
     121                wpOnload();
     122        }
     123        JS
     124);
     125?>
    118126</body>
    119127</html>
  • trunk/src/wp-admin/async-upload.php

    r60681 r63545  
    146146        );
    147147
    148         echo '<script>_.delay(function() {wp.a11y.speak(' . wp_json_encode( $speak_message, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ) . ");}, 1500);jQuery( 'button#{$button_unique_id}' ).on( 'click', function() {jQuery(this).parents('div.media-item').slideUp(200, function(){jQuery(this).remove();wp.a11y.speak( wp.i18n.__( 'Error dismissed.' ) );jQuery( '#plupload-browse-button' ).trigger( 'focus' );})});</script>\n";
     148        $js_function = <<<'JS'
     149                /**
     150                 * Announces the upload failure and wires up its dismiss button.
     151                 *
     152                 * @param {JQueryStatic} $                   The jQuery object.
     153                 * @param {Object}       args                PHP exports.
     154                 * @param {string}       args.speakMessage   Message announced to assistive technology.
     155                 * @param {string}       args.buttonSelector Selector for the notice's dismiss button.
     156                 * @return {void}
     157                 */
     158                ( $, { speakMessage, buttonSelector } ) => {
     159                        setTimeout( () => wp.a11y.speak( speakMessage ), 1500 );
     160                        $( buttonSelector ).on( 'click', function () {
     161                                $( this )
     162                                        .parents( 'div.media-item' )
     163                                        .slideUp( 200, function () {
     164                                                $( this ).remove();
     165                                                wp.a11y.speak( wp.i18n.__( 'Error dismissed.' ) );
     166                                                $( '#plupload-browse-button' ).trigger( 'focus' );
     167                                        } );
     168                        } );
     169                }
     170        JS;
     171        wp_print_inline_script_tag(
     172                sprintf(
     173                        '( %s )( jQuery, %s )',
     174                        $js_function,
     175                        wp_json_encode(
     176                                array(
     177                                        'speakMessage'   => $speak_message,
     178                                        'buttonSelector' => 'button#' . $button_unique_id,
     179                                ),
     180                                JSON_HEX_TAG | JSON_UNESCAPED_SLASHES
     181                        )
     182                )
     183        );
    149184        exit;
    150185}
  • trunk/src/wp-admin/install.php

    r62967 r63545  
    471471
    472472if ( ! wp_is_mobile() ) {
    473         ?>
    474 <script>var t = document.getElementById('weblog_title'); if (t){ t.focus(); }</script>
    475         <?php
     473        wp_print_inline_script_tag(
     474                <<<'JS'
     475                const t = document.getElementById( 'weblog_title' );
     476                if ( t ) {
     477                        t.focus();
     478                }
     479                JS
     480        );
    476481}
    477482
    478483wp_print_scripts( $scripts_to_print );
     484
     485wp_print_inline_script_tag(
     486        <<<'JS'
     487        /**
     488         * Shows the content intended only for browsers with JS enabled.
     489         *
     490         * @param {JQueryStatic} $ The jQuery object.
     491         */
     492        jQuery( function ( $ ) {
     493                $( '.hide-if-no-js' ).removeClass( 'hide-if-no-js' );
     494        } );
     495        JS
     496);
    479497?>
    480 <script>
    481 jQuery( function( $ ) {
    482         $( '.hide-if-no-js' ).removeClass( 'hide-if-no-js' );
    483 } );
    484 </script>
    485498</body>
    486499</html>
Note: See TracChangeset for help on using the changeset viewer.