Make WordPress Core

Changeset 63481


Ignore:
Timestamp:
09/04/2026 06:35:44 PM (2 months ago)
Author:
westonruter
Message:

Administration: Use wp_print_inline_script_tag() in more places.

Continuing the migration away from manually constructed SCRIPT markup, a subset of the admin's raw inline scripts now print through wp_print_inline_script_tag(): those in wp-admin/admin-header.php, those in wp-admin/includes/media.php, and those in iframe_header()/iframe_footer() in wp-admin/includes/template.php. For those screens this is what r56687 did for the frontend and the login screen, making wp_inline_script_attributes the single point at which a per-request nonce can be attached. This is one step toward an admin Content Security Policy opt-in rather than the whole of it.

The data these scripts carry is now serialized with wp_json_encode() using JSON_HEX_TAG | JSON_UNESCAPED_SLASHES instead of being assembled from esc_js() string fragments. This corrects the values that reach JavaScript: esc_js() passes the text through _wp_specialchars(), so ajaxurl, pagenow, typenow, adminpage, thousandsSeparator, and decimalPoint previously arrived carrying HTML entities in place of the literal characters (e.g. a thousands_sep of & was delivered as &).

Inlined JS is also unminified and reformatted, with improvements to strict-mode compatibility. Static blocks use nowdoc heredocs so that editors syntax-check the JavaScript.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/13319.
Follow-up to r56687, r60681, r60909, r60913.

Props thanhtinpk, westonruter.
See #59444, #59446, #63851.

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

Legend:

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

    r62649 r63481  
    101101
    102102$admin_body_class = preg_replace( '/[^a-z0-9_-]+/i', '-', $hook_suffix );
    103 ?>
    104 <script>
    105 addLoadEvent = function(func){if(typeof jQuery!=='undefined')jQuery(function(){func();});else if(typeof wpOnload!=='function'){wpOnload=func;}else{var oldonload=wpOnload;wpOnload=function(){oldonload();func();}}};
    106 var ajaxurl = '<?php echo esc_js( admin_url( 'admin-ajax.php', 'relative' ) ); ?>',
    107         pagenow = '<?php echo esc_js( $current_screen->id ); ?>',
    108         typenow = '<?php echo esc_js( $current_screen->post_type ); ?>',
    109         adminpage = '<?php echo esc_js( $admin_body_class ); ?>',
    110         thousandsSeparator = '<?php echo esc_js( $wp_locale->number_format['thousands_sep'] ); ?>',
    111         decimalPoint = '<?php echo esc_js( $wp_locale->number_format['decimal_point'] ); ?>',
    112         isRtl = <?php echo (int) is_rtl(); ?>;
    113 </script>
    114 <?php
     103
     104// Print the global admin inline scripts through the script tag API so the
     105// `wp_inline_script_attributes` filter (e.g. a CSP nonce) applies.
     106wp_print_inline_script_tag(
     107        <<<'JS'
     108        function addLoadEvent( func ) {
     109                if ( typeof jQuery !== 'undefined' ) {
     110                        jQuery( function () {
     111                                func();
     112                        } );
     113                } else if ( typeof wpOnload !== 'function' ) {
     114                        window.wpOnload = func;
     115                } else {
     116                        const oldOnload = window.wpOnload;
     117                        window.wpOnload = function () {
     118                                oldOnload();
     119                                func();
     120                        };
     121                }
     122        }
     123        JS
     124);
     125wp_print_inline_script_tag(
     126        sprintf(
     127                'Object.assign( window, %s );',
     128                wp_json_encode(
     129                        array(
     130                                'ajaxurl'            => admin_url( 'admin-ajax.php', 'relative' ),
     131                                'pagenow'            => $current_screen->id,
     132                                'typenow'            => $current_screen->post_type,
     133                                'adminpage'          => $admin_body_class,
     134                                'thousandsSeparator' => $wp_locale->number_format['thousands_sep'],
     135                                'decimalPoint'       => $wp_locale->number_format['decimal_point'],
     136                                'isRtl'              => (int) is_rtl(),
     137                        ),
     138                        JSON_HEX_TAG | JSON_UNESCAPED_SLASHES
     139                )
     140        )
     141);
    115142
    116143/**
    … …  
    254281?>
    255282<body class="wp-admin wp-core-ui no-js <?php echo esc_attr( $admin_body_classes ); ?>">
    256 <script>
    257         document.body.className = document.body.className.replace('no-js','js');
    258 </script>
    259 
    260 <?php
     283<?php
     284wp_print_inline_script_tag(
     285        <<<'JS'
     286        document.body.className = document.body.className.replace( 'no-js', 'js' );
     287        JS
     288);
     289
    261290// Make sure the customize body classes are correct as early as possible.
    262291if ( current_user_can( 'customize' ) ) {
  • trunk/src/wp-admin/includes/media.php

    r63381 r63481  
    273273 */
    274274function media_send_to_editor( $html ) {
    275         ?>
    276         <script>
    277         var win = window.dialogArguments || opener || parent || top;
    278         win.send_to_editor( <?php echo wp_json_encode( $html, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ); ?> );
    279         </script>
    280         <?php
     275        wp_print_inline_script_tag(
     276                sprintf(
     277                        '( window.dialogArguments || opener || parent || top ).send_to_editor( %s );',
     278                        wp_json_encode( $html, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES )
     279                )
     280        );
    281281        exit;
    282282}
    … …  
    568568        }
    569569
    570         ?>
    571         <script>
    572         addLoadEvent = function(func){if(typeof jQuery!=='undefined')jQuery(function(){func();});else if(typeof wpOnload!=='function'){wpOnload=func;}else{var oldonload=wpOnload;wpOnload=function(){oldonload();func();}}};
    573         var ajaxurl = '<?php echo esc_js( admin_url( 'admin-ajax.php', 'relative' ) ); ?>', pagenow = 'media-upload-popup', adminpage = 'media-upload-popup',
    574         isRtl = <?php echo (int) is_rtl(); ?>;
    575         </script>
    576         <?php
     570        wp_print_inline_script_tag(
     571                <<<'JS'
     572                function addLoadEvent( func ) {
     573                        if ( typeof jQuery !== 'undefined' ) {
     574                                jQuery( function () {
     575                                        func();
     576                                } );
     577                        } else if ( typeof wpOnload !== 'function' ) {
     578                                window.wpOnload = func;
     579                        } else {
     580                                const oldOnload = window.wpOnload;
     581                                window.wpOnload = function () {
     582                                        oldOnload();
     583                                        func();
     584                                };
     585                        }
     586                }
     587                JS
     588        );
     589        wp_print_inline_script_tag(
     590                sprintf(
     591                        'Object.assign( window, %s );',
     592                        wp_json_encode(
     593                                array(
     594                                        'ajaxurl'   => admin_url( 'admin-ajax.php', 'relative' ),
     595                                        'pagenow'   => 'media-upload-popup',
     596                                        'adminpage' => 'media-upload-popup',
     597                                        'isRtl'     => (int) is_rtl(),
     598                                ),
     599                                JSON_HEX_TAG | JSON_UNESCAPED_SLASHES
     600                        )
     601                )
     602        );
    577603        /** This action is documented in wp-admin/admin-header.php */
    578604        do_action( 'admin_enqueue_scripts', 'media-upload-popup' );
    … …  
    631657        </head>
    632658        <body<?php echo $body_id_attr; ?> class="wp-core-ui no-js <?php echo 'admin-color-' . sanitize_html_class( get_user_option( 'admin_color' ), 'modern' ); ?>">
    633         <script>
    634         document.body.className = document.body.className.replace('no-js', 'js');
    635         </script>
    636659        <?php
     660        wp_print_inline_script_tag(
     661                <<<'JS'
     662                document.body.className = document.body.className.replace( 'no-js', 'js' );
     663                JS
     664        );
    637665
    638666        call_user_func_array( $content_func, $args );
    … …  
    641669        do_action( 'admin_print_footer_scripts' );
    642670
     671        wp_print_inline_script_tag(
     672                <<<'JS'
     673                if ( typeof wpOnload === 'function' ) {
     674                        wpOnload();
     675                }
     676                JS
     677        );
    643678        ?>
    644         <script>if(typeof wpOnload==='function')wpOnload();</script>
    645679        </body>
    646680        </html>
    … …  
    839873
    840874        if ( isset( $_POST['insert-gallery'] ) || isset( $_POST['update-gallery'] ) ) {
    841                 ?>
    842                 <script>
    843                 var win = window.dialogArguments || opener || parent || top;
    844                 win.tb_remove();
    845                 </script>
    846                 <?php
     875                wp_print_inline_script_tag(
     876                        <<<'JS'
     877                        ( window.dialogArguments || opener || parent || top ).tb_remove();
     878                        JS
     879                );
    847880
    848881                exit;
    … …  
    21002133        $post_id = isset( $_REQUEST['post_id'] ) ? (int) $_REQUEST['post_id'] : 0;
    21012134
    2102         echo '<script>post_id = ' . $post_id . ';</script>';
     2135        wp_print_inline_script_tag(
     2136                sprintf( 'var post_id = %s;', wp_json_encode( $post_id, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ) )
     2137        );
    21032138
    21042139        if ( empty( $_GET['chromeless'] ) ) {
    … …  
    22482283        $plupload_init = apply_filters( 'plupload_init', $plupload_init );
    22492284
    2250         ?>
    2251         <script>
    2252         <?php
    22532285        // Verify size is an int. If not return default value.
    22542286        $large_size_h = absint( get_option( 'large_size_h' ) );
    … …  
    22642296        }
    22652297
     2298        wp_print_inline_script_tag(
     2299                sprintf(
     2300                        'Object.assign( window, %s );',
     2301                        wp_json_encode(
     2302                                array(
     2303                                        'resize_height'  => $large_size_h,
     2304                                        'resize_width'   => $large_size_w,
     2305                                        'wpUploaderInit' => $plupload_init,
     2306                                ),
     2307                                JSON_HEX_TAG | JSON_UNESCAPED_SLASHES
     2308                        )
     2309                )
     2310        );
    22662311        ?>
    2267         var resize_height = <?php echo $large_size_h; ?>, resize_width = <?php echo $large_size_w; ?>,
    2268         wpUploaderInit = <?php echo wp_json_encode( $plupload_init, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ); ?>;
    2269         </script>
    22702312
    22712313        <div id="plupload-upload-ui" class="hide-if-no-js">
    … …  
    23912433        <?php media_upload_form( $errors ); ?>
    23922434
    2393         <script>
    2394         jQuery(function($){
    2395                 var preloaded = $(".media-item.preloaded");
    2396                 if ( preloaded.length > 0 ) {
    2397                         preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
    2398                 }
    2399                 updateMediaForm();
    2400         });
    2401         </script>
     2435        <?php
     2436        wp_print_inline_script_tag(
     2437                <<<'JS'
     2438                jQuery( function ( $ ) {
     2439                        var preloaded = $( '.media-item.preloaded' );
     2440                        if ( preloaded.length > 0 ) {
     2441                                preloaded.each( function () {
     2442                                        prepareMediaItem( { id: this.id.replace( /[^0-9]/g, '' ) }, '' );
     2443                                } );
     2444                        }
     2445                        updateMediaForm();
     2446                } );
     2447                JS
     2448        );
     2449        ?>
    24022450        <div id="media-items">
    24032451        <?php
    … …  
    24572505        <h3 class="media-title"><?php _e( 'Insert media from another website' ); ?></h3>
    24582506
     2507        <?php ob_start(); ?>
    24592508        <script>
    24602509        var addExtImage = {
    … …  
    25502599        } );
    25512600        </script>
     2601        <?php wp_print_inline_script_tag( wp_remove_surrounding_empty_script_tags( (string) ob_get_clean() ) ); ?>
    25522602
    25532603        <div id="media-items">
    … …  
    25972647        }
    25982648
     2649        wp_print_inline_script_tag(
     2650                <<<'JS'
     2651                jQuery( function ( $ ) {
     2652                        var preloaded = $( '.media-item.preloaded' );
     2653                        if ( preloaded.length > 0 ) {
     2654                                preloaded.each( function () {
     2655                                        prepareMediaItem( { id: this.id.replace( /[^0-9]/g, '' ) }, '' );
     2656                                } );
     2657                                updateMediaForm();
     2658                        }
     2659                } );
     2660                JS
     2661        );
    25992662        ?>
    2600         <script>
    2601         jQuery(function($){
    2602                 var preloaded = $(".media-item.preloaded");
    2603                 if ( preloaded.length > 0 ) {
    2604                         preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
    2605                         updateMediaForm();
    2606                 }
    2607         });
    2608         </script>
    26092663        <div id="sort-buttons" class="hide-if-no-js">
    26102664        <span>
    … …  
    29262980        <?php wp_nonce_field( 'media-form' ); ?>
    29272981
    2928         <script>
    2929         jQuery(function($){
    2930                 var preloaded = $(".media-item.preloaded");
    2931                 if ( preloaded.length > 0 ) {
    2932                         preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
    2933                         updateMediaForm();
    2934                 }
    2935         });
    2936         </script>
     2982        <?php
     2983        wp_print_inline_script_tag(
     2984                <<<'JS'
     2985                jQuery( function ( $ ) {
     2986                        var preloaded = $( '.media-item.preloaded' );
     2987                        if ( preloaded.length > 0 ) {
     2988                                preloaded.each( function () {
     2989                                        prepareMediaItem( { id: this.id.replace( /[^0-9]/g, '' ) }, '' );
     2990                                } );
     2991                                updateMediaForm();
     2992                        }
     2993                } );
     2994                JS
     2995        );
     2996        ?>
    29372997
    29382998        <div id="media-items">
  • trunk/src/wp-admin/includes/template.php

    r62934 r63481  
    21412141        <?php
    21422142        wp_enqueue_style( 'colors' );
    2143         ?>
    2144 <script>
    2145 addLoadEvent = function(func){if(typeof jQuery!=='undefined')jQuery(function(){func();});else if(typeof wpOnload!=='function'){wpOnload=func;}else{var oldonload=wpOnload;wpOnload=function(){oldonload();func();}}};
    2146 function tb_close(){var win=window.dialogArguments||opener||parent||top;win.tb_remove();}
    2147 var ajaxurl = '<?php echo esc_js( admin_url( 'admin-ajax.php', 'relative' ) ); ?>',
    2148         pagenow = '<?php echo esc_js( $current_screen->id ); ?>',
    2149         typenow = '<?php echo esc_js( $current_screen->post_type ); ?>',
    2150         adminpage = '<?php echo esc_js( $admin_body_class ); ?>',
    2151         thousandsSeparator = '<?php echo esc_js( $wp_locale->number_format['thousands_sep'] ); ?>',
    2152         decimalPoint = '<?php echo esc_js( $wp_locale->number_format['decimal_point'] ); ?>',
    2153         isRtl = <?php echo (int) is_rtl(); ?>;
    2154 </script>
    2155         <?php
     2143
     2144        // Print the global admin inline scripts through the script tag API so the
     2145        // `wp_inline_script_attributes` filter (e.g. a CSP nonce) applies.
     2146        wp_print_inline_script_tag(
     2147                <<<'JS'
     2148                function addLoadEvent( func ) {
     2149                        if ( typeof jQuery !== 'undefined' ) {
     2150                                jQuery( function () {
     2151                                        func();
     2152                                } );
     2153                        } else if ( typeof wpOnload !== 'function' ) {
     2154                                window.wpOnload = func;
     2155                        } else {
     2156                                const oldOnload = window.wpOnload;
     2157                                window.wpOnload = function () {
     2158                                        oldOnload();
     2159                                        func();
     2160                                };
     2161                        }
     2162                }
     2163
     2164                function tb_close() {
     2165                        ( window.dialogArguments || opener || parent || top ).tb_remove();
     2166                }
     2167                JS
     2168        );
     2169        wp_print_inline_script_tag(
     2170                sprintf(
     2171                        'Object.assign( window, %s );',
     2172                        wp_json_encode(
     2173                                array(
     2174                                        'ajaxurl'            => admin_url( 'admin-ajax.php', 'relative' ),
     2175                                        'pagenow'            => $current_screen->id ?? '',
     2176                                        'typenow'            => $current_screen->post_type ?? '',
     2177                                        'adminpage'          => $admin_body_class,
     2178                                        'thousandsSeparator' => $wp_locale->number_format['thousands_sep'],
     2179                                        'decimalPoint'       => $wp_locale->number_format['decimal_point'],
     2180                                        'isRtl'              => (int) is_rtl(),
     2181                                ),
     2182                                JSON_HEX_TAG | JSON_UNESCAPED_SLASHES
     2183                        )
     2184                )
     2185        );
     2186
    21562187        /** This action is documented in wp-admin/admin-header.php */
    21572188        do_action( 'admin_enqueue_scripts', $hook_suffix );
    … …  
    21922223        ?>
    21932224<body <?php echo $admin_body_id; ?>class="wp-admin wp-core-ui no-js iframe <?php echo esc_attr( $admin_body_classes ); ?>">
    2194 <script>
    2195 (function(){
    2196 var c = document.body.className;
    2197 c = c.replace(/no-js/, 'js');
    2198 document.body.className = c;
    2199 })();
    2200 </script>
    22012225        <?php
     2226        wp_print_inline_script_tag(
     2227                <<<'JS'
     2228                document.body.className = document.body.className.replace( 'no-js', 'js' );
     2229                JS
     2230        );
    22022231}
    22032232
    … …  
    22312260        ?>
    22322261        </div>
    2233 <script>if(typeof wpOnload==='function')wpOnload();</script>
     2262        <?php
     2263        wp_print_inline_script_tag(
     2264                <<<'JS'
     2265                if ( typeof wpOnload === 'function' ) {
     2266                        wpOnload();
     2267                }
     2268                JS
     2269        );
     2270        ?>
    22342271</body>
    22352272</html>
Note: See TracChangeset for help on using the changeset viewer.