Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eliminate manual construction of script tags in WP_Scripts and pass other scripts through wp_print_inline_script_tag() #4773

Closed
wants to merge 28 commits into from
Closed
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
0d3ae2d
Use script tag helper functions instead of manual construction
westonruter Jul 1, 2023
86d74f0
WIP: Updating tests
westonruter Jul 1, 2023
61c877c
Merge branch 'trunk' of https://github.com/WordPress/wordpress-develo…
westonruter Aug 31, 2023
9ad9e6b
Fix Squiz.Strings.DoubleQuoteUsage.NotRequired
westonruter Aug 31, 2023
b52c335
Fix Tests_Dependencies_Scripts
westonruter Aug 31, 2023
8355a8c
Remove extra hyphen in id for translations script
westonruter Aug 31, 2023
b9ebf8b
Use wp_print_inline_script_tag() in the_block_template_skip_link()
westonruter Aug 31, 2023
d05efbe
Use wp_print_inline_script_tag() for various scripts in admin screens
westonruter Sep 1, 2023
3ac9a6e
Use wp_print_inline_script_tag() for admin scripts
westonruter Sep 1, 2023
adfef39
Fix inline script for list table
westonruter Sep 1, 2023
6ba21ef
Merge branch 'trunk' of https://github.com/WordPress/wordpress-develo…
westonruter Sep 1, 2023
c19f75d
Extend script tag printing functions to accept closure
westonruter Sep 1, 2023
50aafac
Use wp_print_inline_script_tag in more places
westonruter Sep 1, 2023
0e27a38
Utilize language injections to annotate JS strings
westonruter Sep 1, 2023
3b9b278
Remove incorrect static closures
westonruter Sep 1, 2023
96b1e7a
Update test_remove_frameless_preview_messenger_channel
westonruter Sep 6, 2023
96788c1
Merge branch 'trunk' of https://github.com/WordPress/wordpress-develo…
westonruter Sep 13, 2023
def2778
Revert wp-admin changes
westonruter Sep 13, 2023
8bcd7a2
Revert changes to wp-admin, closure on wp_print_script_inline_tag, an…
westonruter Sep 13, 2023
1194afe
Use wp_print_inline_script_tag() on login screen
westonruter Sep 13, 2023
205b8de
Enqueue script-link script instead of printing
westonruter Sep 19, 2023
fd9028d
Merge branch 'trunk' of https://github.com/WordPress/wordpress-develo…
westonruter Sep 20, 2023
2db59e5
Suggest missing ext-dom in composer.json
westonruter Sep 20, 2023
3ba5135
Use DOM to normalize scripts in document fragment
westonruter Sep 20, 2023
ecc29a9
Escape wrapped CDATA sections
westonruter Sep 20, 2023
832c315
Merge branch 'trunk' into trac-58664
westonruter Sep 25, 2023
3840c54
Remove language injection comments for now
westonruter Sep 25, 2023
97ad256
Revert Gutenberg upstream change
westonruter Sep 25, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
10 changes: 4 additions & 6 deletions src/wp-admin/edit-form-comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,7 @@
</div>
</form>

<?php if ( ! wp_is_mobile() ) : ?>
<script type="text/javascript">
try{document.post.name.focus();}catch(e){}
</script>
<?php
endif;
<?php
if ( ! wp_is_mobile() ) {
wp_print_inline_script_tag( 'try{document.post.name.focus();}catch(e){}' );
}
10 changes: 4 additions & 6 deletions src/wp-admin/edit-tag-form.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,7 @@
</form>
</div>

<?php if ( ! wp_is_mobile() ) : ?>
<script type="text/javascript">
try{document.forms.edittag.name.focus();}catch(e){}
</script>
<?php
endif;
<?php
if ( ! wp_is_mobile() ) {
wp_print_inline_script_tag( 'try{document.forms.edittag.name.focus();}catch(e){}' );
}
10 changes: 4 additions & 6 deletions src/wp-admin/edit-tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -677,12 +677,10 @@

</div><!-- /wrap -->

<?php if ( ! wp_is_mobile() ) : ?>
<script type="text/javascript">
try{document.forms.addtag['tag-name'].focus();}catch(e){}
</script>
<?php
endif;
<?php
if ( ! wp_is_mobile() ) {
wp_print_inline_script_tag( "try{document.forms.addtag['tag-name'].focus();}catch(e){}" );
}

$wp_list_table->inline_edit();

Expand Down
4 changes: 3 additions & 1 deletion src/wp-admin/export.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@
* @since 3.5.0
*/
function export_add_js() {
ob_start();
?>
<script type="text/javascript">
<script>
jQuery( function($) {
var form = $('#export-filters'),
filters = form.find('.export-filters');
Expand All @@ -42,6 +43,7 @@ function export_add_js() {
} );
</script>
<?php
wp_print_inline_script_tag( trim( str_replace( array( '<script>', '</script>' ), '', ob_get_clean() ) ) );
}
add_action( 'admin_head', 'export_add_js' );

Expand Down
2 changes: 1 addition & 1 deletion src/wp-admin/includes/class-wp-list-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -1851,6 +1851,6 @@ public function _js_vars() {
),
);

printf( "<script type='text/javascript'>list_args = %s;</script>\n", wp_json_encode( $args ) );
wp_print_inline_script_tag( sprintf( 'var list_args = %s;', wp_json_encode( $args ) ) );
}
}
2 changes: 1 addition & 1 deletion src/wp-admin/includes/class-wp-themes-list-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ public function _js_vars( $extra_args = array() ) {
$args = array_merge( $args, $extra_args );
}

printf( "<script type='text/javascript'>var theme_list_args = %s;</script>\n", wp_json_encode( $args ) );
wp_print_inline_script_tag( sprintf( 'var theme_list_args = %s;', wp_json_encode( $args ) ) );
parent::_js_vars();
}
}
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() ) ) );
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not really happy with the boilerplate ob_start() followed by an inline script followed by:

wp_print_inline_script_tag( trim( str_replace( array( '<script>', '</script>' ), '', ob_get_clean() ) ) );

I think ideally there would be a helper to do this automatically. Consider, for example, if wp_print_inline_script_tag() actually allowed a closure to be provided for the $javascript parameter in addition to a string. It could handle the output buffering automatically, for instance:

wp_print_inline_script_tag( static function () {
	?>
	<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
} );

When a closure is passed, it could automatically start and end output buffering, strip the script start and end tags, and trim whitespace. If the output buffer lacks a <script> it could issue a _doing_it_wrong().

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only purpose for having the script tags in the PHP code is to enable IDEs to do syntax-highlighting, syntax checking, autocompletion, etc. This is valuable so I think we should facilitate it somehow.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This modification to wp_get_inline_script_tag() has since been reverted in this PR.


/** 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
8 changes: 7 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 Expand Up @@ -1440,13 +1444,15 @@ function wp_admin_headers() {
* @since 4.6.0
*/
function wp_page_reload_on_back_button_js() {
ob_start();
?>
<script>
if ( typeof performance !== 'undefined' && performance.navigation && performance.navigation.type === 2 ) {
document.location.reload( true );
}
</script>
<?php
wp_print_inline_script_tag( trim( str_replace( array( '<script>', '</script>' ), '', ob_get_clean() ) ) );
}

/**
Expand Down
10 changes: 8 additions & 2 deletions src/wp-admin/includes/options.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* @since 4.2.0
*/
function options_discussion_add_js() {
ob_start();
?>
<script>
(function($){
Expand All @@ -24,6 +25,7 @@ function options_discussion_add_js() {
})(jQuery);
</script>
<?php
wp_print_inline_script_tag( trim( str_replace( array( '<script>', '</script>' ), '', ob_get_clean() ) ) );
}

/**
Expand All @@ -32,8 +34,9 @@ function options_discussion_add_js() {
* @since 3.5.0
*/
function options_general_add_js() {
ob_start();
?>
<script type="text/javascript">
<script>
jQuery( function($) {
var $siteName = $( '#wp-admin-bar-site-name' ).children( 'a' ).first(),
homeURL = ( <?php echo wp_json_encode( get_home_url() ); ?> || '' ).replace( /^(https?:\/\/)?(www\.)?/, '' );
Expand Down Expand Up @@ -101,6 +104,7 @@ function options_general_add_js() {
} );
</script>
<?php
wp_print_inline_script_tag( trim( str_replace( array( '<script>', '</script>' ), '', ob_get_clean() ) ) );
}

/**
Expand All @@ -109,8 +113,9 @@ function options_general_add_js() {
* @since 3.5.0
*/
function options_reading_add_js() {
ob_start();
?>
<script type="text/javascript">
<script>
jQuery( function($) {
var section = $('#front-static-pages'),
staticPage = section.find('input:radio[value="page"]'),
Expand All @@ -123,6 +128,7 @@ function options_reading_add_js() {
} );
</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
11 changes: 9 additions & 2 deletions src/wp-admin/user-edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -910,14 +910,20 @@
<?php
break;
}

ob_start();
?>
<script type="text/javascript">
<script>
if (window.location.hash == '#password') {
document.getElementById('pass1').focus();
}
</script>
<?php
wp_print_inline_script_tag( trim( str_replace( array( '<script>', '</script>' ), '', ob_get_clean() ) ) );

<script type="text/javascript">
ob_start();
?>
<script>
jQuery( function( $ ) {
var languageSelect = $( '#locale' );
$( 'form' ).on( 'submit', function() {
Expand All @@ -931,6 +937,7 @@
});
} );
</script>
<?php wp_print_inline_script_tag( trim( str_replace( array( '<script>', '</script>' ), '', ob_get_clean() ) ) ); ?>

<?php if ( isset( $application_passwords_list_table ) ) : ?>
<script type="text/html" id="tmpl-new-application-password">
Expand Down
15 changes: 10 additions & 5 deletions src/wp-includes/class-wp-editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -973,8 +973,9 @@ public static function print_default_editor_scripts() {
$settings = '{}';
}

ob_start();
?>
<script type="text/javascript">
<script>
window.wp = window.wp || {};
window.wp.editor = window.wp.editor || {};
window.wp.editor.getDefaultSettings = function() {
Expand Down Expand Up @@ -1005,6 +1006,7 @@ public static function print_default_editor_scripts() {
?>
</script>
<?php
wp_print_inline_script_tag( trim( str_replace( array( '<script>', '</script>' ), '', ob_get_clean() ) ) );

if ( $user_can_richedit ) {
self::print_tinymce_scripts();
Expand Down Expand Up @@ -1560,7 +1562,7 @@ public static function print_tinymce_scripts() {

wp_print_scripts( array( 'wp-tinymce' ) );

echo "<script type='text/javascript'>\n" . self::wp_mce_translation() . "</script>\n";
wp_print_inline_script_tag( self::wp_mce_translation() );
}

/**
Expand Down Expand Up @@ -1615,9 +1617,9 @@ public static function editor_js() {
* @param array $mce_settings TinyMCE settings array.
*/
do_action( 'before_wp_tiny_mce', self::$mce_settings );
ob_start();
?>

<script type="text/javascript">
<script>
tinyMCEPreInit = {
baseURL: "<?php echo $baseurl; ?>",
suffix: "<?php echo $suffix; ?>",
Expand All @@ -1635,6 +1637,7 @@ public static function editor_js() {
};
</script>
<?php
wp_print_inline_script_tag( trim( str_replace( array( '<script>', '</script>' ), '', ob_get_clean() ) ) );

if ( $tmce_on ) {
self::print_tinymce_scripts();
Expand All @@ -1655,8 +1658,9 @@ public static function editor_js() {
*/
do_action( 'wp_tiny_mce_init', self::$mce_settings );

ob_start();
?>
<script type="text/javascript">
<script>
<?php

if ( self::$ext_plugins ) {
Expand Down Expand Up @@ -1727,6 +1731,7 @@ public static function editor_js() {
}());
</script>
<?php
wp_print_inline_script_tag( trim( str_replace( array( '<script>', '</script>' ), '', ob_get_clean() ) ) );

if ( in_array( 'wplink', self::$plugins, true ) || in_array( 'link', self::$qt_buttons, true ) ) {
self::wp_link_dialog();
Expand Down
Loading
Loading