Skip to content

Commit

Permalink
Use wp_print_inline_script_tag() for various scripts in admin screens
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Sep 1, 2023
1 parent b9ebf8b commit d05efbe
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/wp-admin/admin-footer.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,6 @@
?>

<div class="clear"></div></div><!-- wpwrap -->
<script type="text/javascript">if(typeof wpOnload==='function')wpOnload();</script>
<?php wp_print_inline_script_tag( "if(typeof wpOnload==='function')wpOnload();" ); ?>
</body>
</html>
10 changes: 6 additions & 4 deletions src/wp-admin/admin-header.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,10 @@
wp_enqueue_script( 'svg-painter' );

$admin_body_class = preg_replace( '/[^a-z0-9_-]+/i', '-', $hook_suffix );

ob_start();
?>
<script type="text/javascript">
<script>
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();}}};
var ajaxurl = '<?php echo esc_js( admin_url( 'admin-ajax.php', 'relative' ) ); ?>',
pagenow = '<?php echo esc_js( $current_screen->id ); ?>',
Expand All @@ -107,6 +109,7 @@
isRtl = <?php echo (int) is_rtl(); ?>;
</script>
<?php
wp_print_inline_script_tag( trim( str_replace( array( '<script>', '</script>' ), '', ob_get_clean() ) ) );

/**
* Fires when enqueuing scripts for all admin pages.
Expand Down Expand Up @@ -243,9 +246,8 @@
$admin_body_classes = ltrim( $admin_body_classes . ' ' . $admin_body_class );
?>
<body class="wp-admin wp-core-ui no-js <?php echo esc_attr( $admin_body_classes ); ?>">
<script type="text/javascript">
document.body.className = document.body.className.replace('no-js','js');
</script>

<?php wp_print_inline_script_tag( "document.body.className = document.body.className.replace('no-js','js');" ); ?>

<?php
// Make sure the customize body classes are correct as early as possible.
Expand Down
11 changes: 6 additions & 5 deletions src/wp-admin/includes/media.php
Original file line number Diff line number Diff line change
Expand Up @@ -547,13 +547,16 @@ function wp_iframe( $content_func, ...$args ) {
wp_enqueue_style( 'deprecated-media' );
}

ob_start();
?>
<script type="text/javascript">
<script>
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();}}};
var ajaxurl = '<?php echo esc_js( admin_url( 'admin-ajax.php', 'relative' ) ); ?>', pagenow = 'media-upload-popup', adminpage = 'media-upload-popup',
isRtl = <?php echo (int) is_rtl(); ?>;
</script>
<?php
wp_print_inline_script_tag( trim( str_replace( array( '<script>', '</script>' ), '', ob_get_clean() ) ) );

/** This action is documented in wp-admin/admin-header.php */
do_action( 'admin_enqueue_scripts', 'media-upload-popup' );

Expand Down Expand Up @@ -610,9 +613,7 @@ function wp_iframe( $content_func, ...$args ) {
?>
</head>
<body<?php echo $body_id_attr; ?> class="wp-core-ui no-js">
<script type="text/javascript">
document.body.className = document.body.className.replace('no-js', 'js');
</script>
<?php wp_print_inline_script_tag( "document.body.className = document.body.className.replace('no-js','js');" ); ?>
<?php

call_user_func_array( $content_func, $args );
Expand All @@ -621,7 +622,7 @@ function wp_iframe( $content_func, ...$args ) {
do_action( 'admin_print_footer_scripts' );

?>
<script type="text/javascript">if(typeof wpOnload==='function')wpOnload();</script>
<?php wp_print_inline_script_tag( "if(typeof wpOnload==='function')wpOnload();" ); ?>
</body>
</html>
<?php
Expand Down
6 changes: 5 additions & 1 deletion src/wp-admin/includes/misc.php
Original file line number Diff line number Diff line change
Expand Up @@ -1085,7 +1085,9 @@ function wp_color_scheme_settings() {
);
}

echo '<script type="text/javascript">var _wpColorScheme = ' . wp_json_encode( array( 'icons' => $icon_colors ) ) . ";</script>\n";
wp_print_inline_script_tag(
sprintf( 'var _wpColorScheme = %s;', wp_json_encode( array( 'icons' => $icon_colors ) ) )
);
}

/**
Expand Down Expand Up @@ -1400,12 +1402,14 @@ function wp_admin_canonical_url() {
$filtered_url = remove_query_arg( $removable_query_args, $current_url );
?>
<link id="wp-admin-canonical" rel="canonical" href="<?php echo esc_url( $filtered_url ); ?>" />
<?php ob_start(); ?>
<script>
if ( window.history.replaceState ) {
window.history.replaceState( null, null, document.getElementById( 'wp-admin-canonical' ).href + window.location.hash );
}
</script>
<?php
wp_print_inline_script_tag( trim( str_replace( array( '<script>', '</script>' ), '', ob_get_clean() ) ) );
}

/**
Expand Down
7 changes: 5 additions & 2 deletions src/wp-admin/includes/template.php
Original file line number Diff line number Diff line change
Expand Up @@ -2124,8 +2124,9 @@ function iframe_header( $title = '', $deprecated = false ) {
<title><?php bloginfo( 'name' ); ?> &rsaquo; <?php echo $title; ?> &#8212; <?php _e( 'WordPress' ); ?></title>
<?php
wp_enqueue_style( 'colors' );
ob_start();
?>
<script type="text/javascript">
<script>
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();}}};
function tb_close(){var win=window.dialogArguments||opener||parent||top;win.tb_remove();}
var ajaxurl = '<?php echo esc_js( admin_url( 'admin-ajax.php', 'relative' ) ); ?>',
Expand All @@ -2137,6 +2138,8 @@ function tb_close(){var win=window.dialogArguments||opener||parent||top;win.tb_r
isRtl = <?php echo (int) is_rtl(); ?>;
</script>
<?php
wp_print_inline_script_tag( trim( str_replace( array( '<script>', '</script>' ), '', ob_get_clean() ) ) );

/** This action is documented in wp-admin/admin-header.php */
do_action( 'admin_enqueue_scripts', $hook_suffix );

Expand Down Expand Up @@ -2216,7 +2219,7 @@ function iframe_footer() {
do_action( 'admin_print_footer_scripts' );
?>
</div>
<script type="text/javascript">if(typeof wpOnload==='function')wpOnload();</script>
<?php wp_print_inline_script_tag( "if(typeof wpOnload==='function')wpOnload();" ); ?>
</body>
</html>
<?php
Expand Down
5 changes: 3 additions & 2 deletions src/wp-includes/theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -3742,9 +3742,9 @@ function wp_customize_support_script() {
$admin_origin = parse_url( admin_url() );
$home_origin = parse_url( home_url() );
$cross_domain = ( strtolower( $admin_origin['host'] ) !== strtolower( $home_origin['host'] ) );
$type_attr = current_theme_supports( 'html5', 'script' ) ? '' : ' type="text/javascript"';
ob_start();
?>
<script<?php echo $type_attr; ?>>
<script>
(function() {
var request, b = document.body, c = 'className', cs = 'customize-support', rcs = new RegExp('(^|\\s+)(no-)?'+cs+'(\\s+|$)');

Expand All @@ -3760,6 +3760,7 @@ function wp_customize_support_script() {
}());
</script>
<?php
wp_print_inline_script_tag( trim( str_replace( array( '<script>', '</script>' ), '', ob_get_clean() ) ) );
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/wp-login.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,7 @@ function login_header( $title = 'Log In', $message = '', $wp_error = null ) {
?>
</head>
<body class="login no-js <?php echo esc_attr( implode( ' ', $classes ) ); ?>">
<script type="text/javascript">
document.body.className = document.body.className.replace('no-js','js');
</script>
<?php wp_print_inline_script_tag( "document.body.className = document.body.className.replace('no-js','js');" ); ?>
<?php
/**
* Fires in the login page header after the body tag is opened.
Expand Down Expand Up @@ -383,12 +381,14 @@ function login_footer( $input_id = '' ) {
<?php

if ( ! empty( $input_id ) ) {
ob_start();
?>
<script type="text/javascript">
<script>
try{document.getElementById('<?php echo $input_id; ?>').focus();}catch(e){}
if(typeof wpOnload==='function')wpOnload();
</script>
<?php
wp_print_inline_script_tag( trim( str_replace( array( '<script>', '</script>' ), '', ob_get_clean() ) ) );
}

/**
Expand Down

0 comments on commit d05efbe

Please sign in to comment.