WordPress.org

Make WordPress Core

Ticket #39941: inline-js-patch.diff

File inline-js-patch.diff, 14.6 KB (added by tomdxw, 3 years ago)
  • wp-admin/admin-footer.php

    diff --git a/wp-admin/admin-footer.php b/wp-admin/admin-footer.php
    index 2317a03bd..5aae58d88 100644
    a b if ( function_exists('get_site_option') ) { 
    107107?>
    108108
    109109<div class="clear"></div></div><!-- wpwrap -->
    110 <script type="text/javascript">if(typeof wpOnload=='function')wpOnload();</script>
     110<?php inline_js( "if(typeof wpOnload=='function')wpOnload();" ); ?>
    111111</body>
    112112</html>
  • wp-admin/admin-header.php

    diff --git a/wp-admin/admin-header.php b/wp-admin/admin-header.php
    index d5ceeccd6..b712ea67a 100644
    a b wp_enqueue_script('utils'); 
    7373wp_enqueue_script( 'svg-painter' );
    7474
    7575$admin_body_class = preg_replace('/[^a-z0-9_-]+/i', '-', $hook_suffix);
    76 ?>
    77 <script type="text/javascript">
     76
     77$ajaxurl = admin_url( 'admin-ajax.php', 'relative' );
     78$thousands_separator = addslashes( $wp_locale->number_format['thousands_sep'] );
     79$decimal_point = addslashes( $wp_locale->number_format['decimal_point'] );
     80$is_rtl = (int) is_rtl();
     81$js = <<<JS
    7882addLoadEvent = function(func){if(typeof jQuery!="undefined")jQuery(document).ready(func);else if(typeof wpOnload!='function'){wpOnload=func;}else{var oldonload=wpOnload;wpOnload=function(){oldonload();func();}}};
    79 var ajaxurl = '<?php echo admin_url( 'admin-ajax.php', 'relative' ); ?>',
    80         pagenow = '<?php echo $current_screen->id; ?>',
    81         typenow = '<?php echo $current_screen->post_type; ?>',
    82         adminpage = '<?php echo $admin_body_class; ?>',
    83         thousandsSeparator = '<?php echo addslashes( $wp_locale->number_format['thousands_sep'] ); ?>',
    84         decimalPoint = '<?php echo addslashes( $wp_locale->number_format['decimal_point'] ); ?>',
    85         isRtl = <?php echo (int) is_rtl(); ?>;
    86 </script>
     83var ajaxurl = '{$ajaxurl}',
     84        pagenow = '{$current_screen->id}',
     85        typenow = '{$current_screen->post_type}',
     86        adminpage = '{$admin_body_class}',
     87        thousandsSeparator = '{$thousands_separator}',
     88        decimalPoint = '{$decimal_point}',
     89        isRtl = {$is_rtl};
     90JS;
     91inline_js($js);
     92?>
    8793<meta name="viewport" content="width=device-width,initial-scale=1.0">
    8894<?php
    8995
    $admin_body_class .= ' no-customize-support no-svg'; 
    195201$admin_body_classes = apply_filters( 'admin_body_class', '' );
    196202?>
    197203<body class="wp-admin wp-core-ui no-js <?php echo $admin_body_classes . ' ' . $admin_body_class; ?>">
    198 <script type="text/javascript">
    199         document.body.className = document.body.className.replace('no-js','js');
    200 </script>
     204<?php
     205inline_js("document.body.className = document.body.className.replace('no-js','js');");
     206?>
    201207
    202208<?php
    203209// Make sure the customize body classes are correct as early as possible.
  • wp-admin/includes/class-wp-list-table.php

    diff --git a/wp-admin/includes/class-wp-list-table.php b/wp-admin/includes/class-wp-list-table.php
    index dedfec934..67b8e2d20 100644
    a b class WP_List_Table { 
    13721372                        )
    13731373                );
    13741374
    1375                 printf( "<script type='text/javascript'>list_args = %s;</script>\n", wp_json_encode( $args ) );
     1375                inline_js( sprintf( "list_args = %s;", wp_json_encode( $args ) ) );
    13761376        }
    13771377}
  • wp-admin/includes/misc.php

    diff --git a/wp-admin/includes/misc.php b/wp-admin/includes/misc.php
    index 679b9ac5d..e2b21b0fd 100644
    a b function wp_color_scheme_settings() { 
    713713                $icon_colors = array( 'base' => '#82878c', 'focus' => '#00a0d2', 'current' => '#fff' );
    714714        }
    715715
    716         echo '<script type="text/javascript">var _wpColorScheme = ' . wp_json_encode( array( 'icons' => $icon_colors ) ) . ";</script>\n";
     716        inline_js( 'var _wpColorScheme = ' . wp_json_encode( array( 'icons' => $icon_colors ) ) . ';' );
    717717}
    718718
    719719/**
    function wp_admin_canonical_url() { 
    911911        $filtered_url = remove_query_arg( $removable_query_args, $current_url );
    912912        ?>
    913913        <link id="wp-admin-canonical" rel="canonical" href="<?php echo esc_url( $filtered_url ); ?>" />
    914         <script>
     914        <?php
     915        $js = <<<JS
    915916                if ( window.history.replaceState ) {
    916917                        window.history.replaceState( null, null, document.getElementById( 'wp-admin-canonical' ).href + window.location.hash );
    917918                }
    918         </script>
    919 <?php
     919JS;
     920        inline_js($js);
    920921}
    921922
    922923/**
  • wp-includes/class-wp-editor.php

    diff --git a/wp-includes/class-wp-editor.php b/wp-includes/class-wp-editor.php
    index 83777cfd8..b3d7d99f4 100644
    a b final class _WP_Editors { 
    12191219                 * @param array $mce_settings TinyMCE settings array.
    12201220                 */
    12211221                do_action( 'before_wp_tiny_mce', self::$mce_settings );
    1222                 ?>
    12231222
    1224                 <script type="text/javascript">
     1223                $baseurl = self::$baseurl;
     1224                $drag_drop_upload = self::$drag_drop_upload ? 'dragDropUpload: true,' : '';
     1225                $ref = self::_parse_init( $ref );
     1226                $js = <<<JS
    12251227                tinyMCEPreInit = {
    1226                         baseURL: "<?php echo self::$baseurl; ?>",
    1227                         suffix: "<?php echo $suffix; ?>",
    1228                         <?php
    1229 
    1230                         if ( self::$drag_drop_upload ) {
    1231                                 echo 'dragDropUpload: true,';
    1232                         }
    1233 
    1234                         ?>
    1235                         mceInit: <?php echo $mceInit; ?>,
    1236                         qtInit: <?php echo $qtInit; ?>,
    1237                         ref: <?php echo self::_parse_init( $ref ); ?>,
     1228                        baseURL: "{$baseurl}",
     1229                        suffix: "{$suffix}",
     1230                        {$drag_drop_upload}
     1231                        mceInit: {$mceInit},
     1232                        qtInit: {$qtInit},
     1233                        ref: {$ref},
    12381234                        load_ext: function(url,lang){var sl=tinymce.ScriptLoader;sl.markDone(url+'/langs/'+lang+'.js');sl.markDone(url+'/langs/'+lang+'_dlg.js');}
    12391235                };
    1240                 </script>
    1241                 <?php
     1236JS;
     1237                inline_js( $js );
    12421238
    12431239                $baseurl = self::$baseurl;
    12441240                // Load tinymce.js when running from /src, else load wp-tinymce.js.gz (production) or tinymce.min.js (SCRIPT_DEBUG)
    final class _WP_Editors { 
    12701266                 */
    12711267                do_action( 'wp_tiny_mce_init', self::$mce_settings );
    12721268
    1273                 ?>
    1274                 <script type="text/javascript">
    1275                 <?php
     1269                $js = '';
    12761270
    12771271                if ( self::$ext_plugins )
    1278                         echo self::$ext_plugins . "\n";
     1272                        $js .= self::$ext_plugins . "\n";
    12791273
    12801274                if ( ! is_admin() )
    1281                         echo 'var ajaxurl = "' . admin_url( 'admin-ajax.php', 'relative' ) . '";';
     1275                        $js .= 'var ajaxurl = "' . admin_url( 'admin-ajax.php', 'relative' ) . '";';
    12821276
    1283                 ?>
     1277                $js .= <<<JS
    12841278
    12851279                ( function() {
    1286                         var init, id, $wrap;
     1280                        var init, id, \$wrap;
    12871281
    12881282                        if ( typeof tinymce !== 'undefined' ) {
    12891283                                for ( id in tinyMCEPreInit.mceInit ) {
    12901284                                        init = tinyMCEPreInit.mceInit[id];
    1291                                         $wrap = tinymce.$( '#wp-' + id + '-wrap' );
     1285                                        \$wrap = tinymce.\$( '#wp-' + id + '-wrap' );
    12921286
    1293                                         if ( ( $wrap.hasClass( 'tmce-active' ) || ! tinyMCEPreInit.qtInit.hasOwnProperty( id ) ) && ! init.wp_skip_init ) {
     1287                                        if ( ( \$wrap.hasClass( 'tmce-active' ) || ! tinyMCEPreInit.qtInit.hasOwnProperty( id ) ) && ! init.wp_skip_init ) {
    12941288                                                tinymce.init( init );
    12951289
    12961290                                                if ( ! window.wpActiveEditor ) {
    final class _WP_Editors { 
    13101304                                }
    13111305                        }
    13121306                }());
    1313                 </script>
    1314                 <?php
     1307JS;
     1308                inline_js($js);
    13151309
    13161310                if ( in_array( 'wplink', self::$plugins, true ) || in_array( 'link', self::$qt_buttons, true ) ) {
    13171311                        self::wp_link_dialog();
  • wp-includes/class.wp-scripts.php

    diff --git a/wp-includes/class.wp-scripts.php b/wp-includes/class.wp-scripts.php
    index 81634ece4..15bbd4fe7 100644
    a b class WP_Scripts extends WP_Dependencies { 
    219219                if ( !$echo )
    220220                        return $output;
    221221
    222                 echo "<script type='text/javascript'>\n"; // CDATA and type='text/javascript' is not needed for HTML 5
    223                 echo "/* <![CDATA[ */\n";
    224                 echo "$output\n";
    225                 echo "/* ]]> */\n";
    226                 echo "</script>\n";
     222                inline_js("/* <![CDATA[ */\n$output\n/* ]]> */\n");
    227223
    228224                return true;
    229225        }
    class WP_Scripts extends WP_Dependencies { 
    277273                $after_handle = $this->print_inline_script( $handle, 'after', false );
    278274
    279275                if ( $before_handle ) {
    280                         $before_handle = sprintf( "<script type='text/javascript'>\n%s\n</script>\n", $before_handle );
     276                        $before_handle = inline_js( $before_handle, false );
    281277                }
    282278
    283279                if ( $after_handle ) {
    284                         $after_handle = sprintf( "<script type='text/javascript'>\n%s\n</script>\n", $after_handle );
     280                        $after_handle = inline_js( $after_handle, false );
    285281                }
    286282
    287283                if ( $this->do_concat ) {
  • wp-includes/formatting.php

    diff --git a/wp-includes/formatting.php b/wp-includes/formatting.php
    index 7ad8020e3..b2bdde578 100644
    a b function _print_emoji_detection_script() { 
    50745074                 * minified JavaScript. If you need to debug it, please turn on SCRIPT_DEBUG
    50755075                 * and edit wp-emoji-loader.js directly.
    50765076                 */
    5077                 ?>
    5078                 <script type="text/javascript">
    5079                         window._wpemojiSettings = <?php echo wp_json_encode( $settings ); ?>;
    5080                         !function(a,b,c){function d(a){var b,c,d,e,f=String.fromCharCode;if(!k||!k.fillText)return!1;switch(k.clearRect(0,0,j.width,j.height),k.textBaseline="top",k.font="600 32px Arial",a){case"flag":return k.fillText(f(55356,56826,55356,56819),0,0),!(j.toDataURL().length<3e3)&&(k.clearRect(0,0,j.width,j.height),k.fillText(f(55356,57331,65039,8205,55356,57096),0,0),b=j.toDataURL(),k.clearRect(0,0,j.width,j.height),k.fillText(f(55356,57331,55356,57096),0,0),c=j.toDataURL(),b!==c);case"emoji4":return k.fillText(f(55357,56425,55356,57341,8205,55357,56507),0,0),d=j.toDataURL(),k.clearRect(0,0,j.width,j.height),k.fillText(f(55357,56425,55356,57341,55357,56507),0,0),e=j.toDataURL(),d!==e}return!1}function e(a){var c=b.createElement("script");c.src=a,c.defer=c.type="text/javascript",b.getElementsByTagName("head")[0].appendChild(c)}var f,g,h,i,j=b.createElement("canvas"),k=j.getContext&&j.getContext("2d");for(i=Array("flag","emoji4"),c.supports={everything:!0,everythingExceptFlag:!0},h=0;h<i.length;h++)c.supports[i[h]]=d(i[h]),c.supports.everything=c.supports.everything&&c.supports[i[h]],"flag"!==i[h]&&(c.supports.everythingExceptFlag=c.supports.everythingExceptFlag&&c.supports[i[h]]);c.supports.everythingExceptFlag=c.supports.everythingExceptFlag&&!c.supports.flag,c.DOMReady=!1,c.readyCallback=function(){c.DOMReady=!0},c.supports.everything||(g=function(){c.readyCallback()},b.addEventListener?(b.addEventListener("DOMContentLoaded",g,!1),a.addEventListener("load",g,!1)):(a.attachEvent("onload",g),b.attachEvent("onreadystatechange",function(){"complete"===b.readyState&&c.readyCallback()})),f=c.source||{},f.concatemoji?e(f.concatemoji):f.wpemoji&&f.twemoji&&(e(f.twemoji),e(f.wpemoji)))}(window,document,window._wpemojiSettings);
    5081                 </script>
    5082                 <?php
     5077                inline_js('window._wpemojiSettings = '.wp_json_encode( $settings ).';!function(a,b,c){function d(a){var b,c,d,e,f=String.fromCharCode;if(!k||!k.fillText)return!1;switch(k.clearRect(0,0,j.width,j.height),k.textBaseline="top",k.font="600 32px Arial",a){case"flag":return k.fillText(f(55356,56826,55356,56819),0,0),!(j.toDataURL().length<3e3)&&(k.clearRect(0,0,j.width,j.height),k.fillText(f(55356,57331,65039,8205,55356,57096),0,0),b=j.toDataURL(),k.clearRect(0,0,j.width,j.height),k.fillText(f(55356,57331,55356,57096),0,0),c=j.toDataURL(),b!==c);case"emoji4":return k.fillText(f(55357,56425,55356,57341,8205,55357,56507),0,0),d=j.toDataURL(),k.clearRect(0,0,j.width,j.height),k.fillText(f(55357,56425,55356,57341,55357,56507),0,0),e=j.toDataURL(),d!==e}return!1}function e(a){var c=b.createElement("script");c.src=a,c.defer=c.type="text/javascript",b.getElementsByTagName("head")[0].appendChild(c)}var f,g,h,i,j=b.createElement("canvas"),k=j.getContext&&j.getContext("2d");for(i=Array("flag","emoji4"),c.supports={everything:!0,everythingExceptFlag:!0},h=0;h<i.length;h++)c.supports[i[h]]=d(i[h]),c.supports.everything=c.supports.everything&&c.supports[i[h]],"flag"!==i[h]&&(c.supports.everythingExceptFlag=c.supports.everythingExceptFlag&&c.supports[i[h]]);c.supports.everythingExceptFlag=c.supports.everythingExceptFlag&&!c.supports.flag,c.DOMReady=!1,c.readyCallback=function(){c.DOMReady=!0},c.supports.everything||(g=function(){c.readyCallback()},b.addEventListener?(b.addEventListener("DOMContentLoaded",g,!1),a.addEventListener("load",g,!1)):(a.attachEvent("onload",g),b.attachEvent("onreadystatechange",function(){"complete"===b.readyState&&c.readyCallback()})),f=c.source||{},f.concatemoji?e(f.concatemoji):f.wpemoji&&f.twemoji&&(e(f.twemoji),e(f.wpemoji)))}(window,document,window._wpemojiSettings);');
    50835078        }
    50845079}
    50855080
  • wp-includes/functions.php

    diff --git a/wp-includes/functions.php b/wp-includes/functions.php
    index 865c0d29b..eb52fbc9c 100644
    a b function wp_cache_get_last_changed( $group ) { 
    56305630
    56315631        return $last_changed;
    56325632}
     5633
     5634/**
     5635 * Documentation goes here.
     5636 */
     5637function inline_js( $javascript, $echo=true ) {
     5638    $attributes = "type='text/javascript'";
     5639    $attributes = apply_filters('inline_js_attributes', $attributes);
     5640
     5641    $output = sprintf("<script %s>%s</script>", $attributes, $javascript);
     5642
     5643    if ( $echo ) {
     5644        echo $output;
     5645    } else {
     5646        return $output;
     5647    }
     5648}
  • wp-includes/script-loader.php

    diff --git a/wp-includes/script-loader.php b/wp-includes/script-loader.php
    index b2e24d6fe..a61f7be79 100644
    a b function _print_scripts() { 
    11391139        if ( $concat = trim( $wp_scripts->concat, ', ' ) ) {
    11401140
    11411141                if ( !empty($wp_scripts->print_code) ) {
    1142                         echo "\n<script type='text/javascript'>\n";
    1143                         echo "/* <![CDATA[ */\n"; // not needed in HTML 5
    1144                         echo $wp_scripts->print_code;
    1145                         echo "/* ]]> */\n";
    1146                         echo "</script>\n";
     1142                        $js = "/* <![CDATA[ */\n"; // not needed in HTML 5
     1143                        $js .= $wp_scripts->print_code;
     1144                        $js .= "/* ]]> */\n";
     1145                        inline_js($js);
    11471146                }
    11481147
    11491148                $concat = str_split( $concat, 128 );
  • wp-includes/theme.php

    diff --git a/wp-includes/theme.php b/wp-includes/theme.php
    index bf53205c2..6050185d8 100644
    a b function wp_customize_support_script() { 
    30263026                </script>
    30273027        <![endif]-->
    30283028        <!--[if gte IE 9]><!-->
    3029                 <script type="text/javascript">
     3029        <?php
     3030        $js = <<<JS
    30303031                        (function() {
    30313032                                var request, b = document.body, c = 'className', cs = 'customize-support', rcs = new RegExp('(^|\\s+)(no-)?'+cs+'(\\s+|$)');
    3032 
    3033                 <?php   if ( $cross_domain ) : ?>
    3034                                 request = (function(){ var xhr = new XMLHttpRequest(); return ('withCredentials' in xhr); })();
    3035                 <?php   else : ?>
    3036                                 request = true;
    3037                 <?php   endif; ?>
    3038 
     3033JS;
     3034        if ( $cross_domain ) {
     3035            $js .= "request = (function(){ var xhr = new XMLHttpRequest(); return ('withCredentials' in xhr); })();";
     3036        } else {
     3037            $js .= "request = true;";
     3038        }
     3039
     3040        $js .= <<<JS
    30393041                                b[c] = b[c].replace( rcs, ' ' );
    30403042                                // The customizer requires postMessage and CORS (if the site is cross domain)
    30413043                                b[c] += ( window.postMessage && request ? ' ' : ' no-' ) + cs;
    30423044                        }());
    3043                 </script>
     3045JS;
     3046
     3047        inline_js($js);
     3048        ?>
    30443049        <!--<![endif]-->
    30453050        <?php
    30463051}