Skip to content

Bundled Themes: Use wp_print_inline_script_tag() for CSP compatibility #9415

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

Draft
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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-content/themes/twentyfifteen/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ function twentyfifteen_fonts_url() {
* @since Twenty Fifteen 1.1
*/
function twentyfifteen_javascript_detection() {
echo "<script>(function(html){html.className = html.className.replace(/\bno-js\b/,'js')})(document.documentElement);</script>\n";
wp_print_inline_script_tag( "(function(html){html.className = html.className.replace(/\bno-js\b/,'js')})(document.documentElement);" );
}
add_action( 'wp_head', 'twentyfifteen_javascript_detection', 0 );

Expand Down
6 changes: 1 addition & 5 deletions src/wp-content/themes/twentynineteen/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,11 +303,7 @@ function twentynineteen_scripts() {
*/
function twentynineteen_skip_link_focus_fix() {
// The following is minified via `terser --compress --mangle -- js/skip-link-focus-fix.js`.
?>
<script>
/(trident|msie)/i.test(navigator.userAgent)&&document.getElementById&&window.addEventListener&&window.addEventListener("hashchange",function(){var t,e=location.hash.substring(1);/^[A-z0-9_-]+$/.test(e)&&(t=document.getElementById(e))&&(/^(?:a|select|input|button|textarea)$/i.test(t.tagName)||(t.tabIndex=-1),t.focus())},!1);
</script>
<?php
wp_print_inline_script_tag( '/(trident|msie)/i.test(navigator.userAgent)&&document.getElementById&&window.addEventListener&&window.addEventListener("hashchange",function(){var t,e=location.hash.substring(1);/^[A-z0-9_-]+$/.test(e)&&(t=document.getElementById(e))&&(/^(?:a|select|input|button|textarea)$/i.test(t.tagName)||(t.tabIndex=-1),t.focus())},!1);' );
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/wp-content/themes/twentyseventeen/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ function twentyseventeen_excerpt_more( $link ) {
* @since Twenty Seventeen 1.0
*/
function twentyseventeen_javascript_detection() {
echo "<script>(function(html){html.className = html.className.replace(/\bno-js\b/,'js')})(document.documentElement);</script>\n";
wp_print_inline_script_tag( "(function(html){html.className = html.className.replace(/\bno-js\b/,'js')})(document.documentElement);" );
}
add_action( 'wp_head', 'twentyseventeen_javascript_detection', 0 );

Expand Down
2 changes: 1 addition & 1 deletion src/wp-content/themes/twentysixteen/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ function twentysixteen_fonts_url() {
* @since Twenty Sixteen 1.0
*/
function twentysixteen_javascript_detection() {
echo "<script>(function(html){html.className = html.className.replace(/\bno-js\b/,'js')})(document.documentElement);</script>\n";
wp_print_inline_script_tag( "(function(html){html.className = html.className.replace(/\bno-js\b/,'js')})(document.documentElement);" );
}
add_action( 'wp_head', 'twentysixteen_javascript_detection', 0 );

Expand Down
6 changes: 1 addition & 5 deletions src/wp-content/themes/twentytwenty/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,11 +252,7 @@ function twentytwenty_register_scripts() {
*/
function twentytwenty_skip_link_focus_fix() {
// The following is minified via `terser --compress --mangle -- assets/js/skip-link-focus-fix.js`.
?>
<script>
/(trident|msie)/i.test(navigator.userAgent)&&document.getElementById&&window.addEventListener&&window.addEventListener("hashchange",function(){var t,e=location.hash.substring(1);/^[A-z0-9_-]+$/.test(e)&&(t=document.getElementById(e))&&(/^(?:a|select|input|button|textarea)$/i.test(t.tagName)||(t.tabIndex=-1),t.focus())},!1);
</script>
<?php
wp_print_inline_script_tag( '/(trident|msie)/i.test(navigator.userAgent)&&document.getElementById&&window.addEventListener&&window.addEventListener("hashchange",function(){var t,e=location.hash.substring(1);/^[A-z0-9_-]+$/.test(e)&&(t=document.getElementById(e))&&(/^(?:a|select|input|button|textarea)$/i.test(t.tagName)||(t.tabIndex=-1),t.focus())},!1) ' );
}

/**
Expand Down
4 changes: 1 addition & 3 deletions src/wp-content/themes/twentytwenty/inc/template-tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -661,9 +661,7 @@ function twentytwenty_nav_menu_social_icons( $item_output, $item, $depth, $args
*/
function twentytwenty_no_js_class() {

?>
<script>document.documentElement.className = document.documentElement.className.replace( 'no-js', 'js' );</script>
<?php
wp_print_inline_script_tag( "document.documentElement.className = document.documentElement.className.replace( 'no-js', 'js' );" );
}

add_action( 'wp_head', 'twentytwenty_no_js_class' );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,9 +363,8 @@ public function the_html( $attrs = array() ) {
* @return void
*/
public function the_script() {
echo '<script>';
include get_template_directory() . '/assets/js/dark-mode-toggler.js'; // phpcs:ignore WPThemeReview.CoreFunctionality.FileInclude
echo '</script>';
$script = file_get_contents( get_template_directory() . '/assets/js/dark-mode-toggler.js' ); // phpcs:ignore WPThemeReview.CoreFunctionality.FileInclude
wp_print_inline_script_tag( $script );
}

/**
Expand Down
23 changes: 8 additions & 15 deletions src/wp-content/themes/twentytwentyone/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -496,17 +496,12 @@ function twenty_twenty_one_skip_link_focus_fix() {

// If SCRIPT_DEBUG is defined and true, print the unminified file.
if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) {
echo '<script>';
include get_template_directory() . '/assets/js/skip-link-focus-fix.js';
echo '</script>';
$script = file_get_contents( get_template_directory() . '/assets/js/skip-link-focus-fix.js' );
} else {
// The following is minified via `npx terser --compress --mangle -- assets/js/skip-link-focus-fix.js`.
?>
<script>
/(trident|msie)/i.test(navigator.userAgent)&&document.getElementById&&window.addEventListener&&window.addEventListener("hashchange",(function(){var t,e=location.hash.substring(1);/^[A-z0-9_-]+$/.test(e)&&(t=document.getElementById(e))&&(/^(?:a|select|input|button|textarea)$/i.test(t.tagName)||(t.tabIndex=-1),t.focus())}),!1);
</script>
<?php
$script = '/(trident|msie)/i.test(navigator.userAgent)&&document.getElementById&&window.addEventListener&&window.addEventListener("hashchange",(function(){var t,e=location.hash.substring(1);/^[A-z0-9_-]+$/.test(e)&&(t=document.getElementById(e))&&(/^(?:a|select|input|button|textarea)$/i.test(t.tagName)||(t.tabIndex=-1),t.focus())}),!1);';
}
wp_print_inline_script_tag( $script );
}

/**
Expand Down Expand Up @@ -630,13 +625,11 @@ function twentytwentyone_the_html_classes() {
* @return void
*/
function twentytwentyone_add_ie_class() {
?>
<script>
if ( -1 !== navigator.userAgent.indexOf( 'MSIE' ) || -1 !== navigator.appVersion.indexOf( 'Trident/' ) ) {
document.body.classList.add( 'is-IE' );
}
</script>
<?php
wp_print_inline_script_tag(
"if ( -1 !== navigator.userAgent.indexOf( 'MSIE' ) || -1 !== navigator.appVersion.indexOf( 'Trident/' ) ) {
document.body.classList.add( 'is-IE' );
}"
);
}
add_action( 'wp_footer', 'twentytwentyone_add_ie_class' );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ function twenty_twenty_one_pingback_header() {
* @return void
*/
function twenty_twenty_one_supports_js() {
echo '<script>document.body.classList.remove("no-js");</script>';
wp_print_inline_script_tag( "document.body.classList.remove('no-js');" );
}
add_action( 'wp_footer', 'twenty_twenty_one_supports_js' );

Expand Down
Loading