-
Notifications
You must be signed in to change notification settings - Fork 3k
Fix: Update Raw <script>
tags with wp_inline_script_tag()
for bundled themes.
#9416
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
Open
Debarghya-Banerjee
wants to merge
2
commits into
WordPress:trunk
Choose a base branch
from
Debarghya-Banerjee:fix/63806-update-themes-to-use-wp_print_script_tag
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -303,11 +303,12 @@ 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()); | ||
}, false);' | ||
); | ||
} | ||
|
||
/** | ||
|
@@ -395,3 +396,115 @@ function twentynineteen_register_block_patterns() { | |
} | ||
|
||
add_action( 'init', 'twentynineteen_register_block_patterns' ); | ||
|
||
if ( ! function_exists( 'wp_get_inline_script_tag' ) ) { | ||
/** | ||
* Constructs an inline script tag. | ||
* | ||
* It is possible to inject attributes in the `<script>` tag via the {@see 'wp_inline_script_attributes'} filter. | ||
* Automatically injects type attribute if needed. | ||
* | ||
* Added for backward compatibility to support pre-5.7.0 WordPress versions. | ||
* | ||
* @since 5.7.0 | ||
* | ||
* @param string $data Data for script tag: JavaScript, importmap, speculationrules, etc. | ||
* @param array $attributes Optional. Key-value pairs representing `<script>` tag attributes. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same goes here. |
||
* @return string String containing inline JavaScript code wrapped around `<script>` tag. | ||
*/ | ||
function wp_get_inline_script_tag( $data, $attributes = array() ) { | ||
$is_html5 = current_theme_supports( 'html5', 'script' ) || is_admin(); | ||
if ( ! isset( $attributes['type'] ) && ! $is_html5 ) { | ||
// Keep the type attribute as the first for legacy reasons (it has always been this way in core). | ||
$attributes = array_merge( | ||
array( 'type' => 'text/javascript' ), | ||
$attributes | ||
); | ||
} | ||
|
||
/* | ||
* XHTML extracts the contents of the SCRIPT element and then the XML parser | ||
* decodes character references and other syntax elements. This can lead to | ||
* misinterpretation of the script contents or invalid XHTML documents. | ||
* | ||
* Wrapping the contents in a CDATA section instructs the XML parser not to | ||
* transform the contents of the SCRIPT element before passing them to the | ||
* JavaScript engine. | ||
* | ||
* Example: | ||
* | ||
* <script>console.log('…');</script> | ||
* | ||
* In an HTML document this would print "…" to the console, | ||
* but in an XHTML document it would print "…" to the console. | ||
* | ||
* <script>console.log('An image is <img> in HTML');</script> | ||
* | ||
* In an HTML document this would print "An image is <img> in HTML", | ||
* but it's an invalid XHTML document because it interprets the `<img>` | ||
* as an empty tag missing its closing `/`. | ||
* | ||
* @see https://www.w3.org/TR/xhtml1/#h-4.8 | ||
*/ | ||
if ( | ||
! $is_html5 && | ||
( | ||
! isset( $attributes['type'] ) || | ||
'module' === $attributes['type'] || | ||
str_contains( $attributes['type'], 'javascript' ) || | ||
str_contains( $attributes['type'], 'ecmascript' ) || | ||
str_contains( $attributes['type'], 'jscript' ) || | ||
str_contains( $attributes['type'], 'livescript' ) | ||
) | ||
) { | ||
/* | ||
* If the string `]]>` exists within the JavaScript it would break | ||
* out of any wrapping CDATA section added here, so to start, it's | ||
* necessary to escape that sequence which requires splitting the | ||
* content into two CDATA sections wherever it's found. | ||
* | ||
* Note: it's only necessary to escape the closing `]]>` because | ||
* an additional `<![CDATA[` leaves the contents unchanged. | ||
*/ | ||
$data = str_replace( ']]>', ']]]]><![CDATA[>', $data ); | ||
|
||
// Wrap the entire escaped script inside a CDATA section. | ||
$data = sprintf( "/* <![CDATA[ */\n%s\n/* ]]> */", $data ); | ||
} | ||
|
||
$data = "\n" . trim( $data, "\n\r " ) . "\n"; | ||
|
||
/** | ||
* Filters attributes to be added to a script tag. | ||
* | ||
* Added for backward compatibility to support pre-5.7.0 WordPress versions. | ||
* | ||
* @since 5.7.0 | ||
* | ||
* @param array $attributes Key-value pairs representing `<script>` tag attributes. | ||
* Only the attribute name is added to the `<script>` tag for | ||
* entries with a boolean value, and that are true. | ||
* @param string $data Inline data. | ||
*/ | ||
$attributes = apply_filters( 'wp_inline_script_attributes', $attributes, $data ); | ||
|
||
return sprintf( "<script%s>%s</script>\n", wp_sanitize_script_attributes( $attributes ), $data ); | ||
} | ||
} | ||
|
||
if ( ! function_exists( 'wp_print_inline_script_tag' ) ) { | ||
/** | ||
* Prints an inline script tag. | ||
* | ||
* It is possible to inject attributes in the `<script>` tag via the {@see 'wp_inline_script_attributes'} filter. | ||
* Automatically injects type attribute if needed. | ||
* | ||
* @since 5.7.0 | ||
* | ||
* @param string $data Data for script tag: JavaScript, importmap, speculationrules, etc. | ||
* @param array $attributes Optional. Key-value pairs representing `<script>` tag attributes. | ||
*/ | ||
function wp_print_inline_script_tag( $data, $attributes = array() ) { | ||
echo wp_get_inline_script_tag( $data, $attributes ); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should add a breathing space between 2 doc annotations.
@param and @return
.