Skip to content

Commit fe6434d

Browse files
committed
Use the tag processor to correctly extract script tag contents
1 parent 1639b2b commit fe6434d

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

src/wp-includes/functions.wp-scripts.php

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -130,18 +130,24 @@ function wp_print_scripts( $handles = false ) {
130130
function wp_add_inline_script( $handle, $data, $position = 'after' ) {
131131
_wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle );
132132

133-
if ( false !== stripos( $data, '</script>' ) ) {
134-
_doing_it_wrong(
135-
__FUNCTION__,
136-
sprintf(
137-
/* translators: 1: <script>, 2: wp_add_inline_script() */
138-
__( 'Do not pass %1$s tags to %2$s.' ),
139-
'<code>&lt;script&gt;</code>',
140-
'<code>wp_add_inline_script()</code>'
141-
),
142-
'4.5.0'
143-
);
144-
$data = trim( preg_replace( '#<script[^>]*>(.*)</script>#is', '$1', $data ) );
133+
if ( false !== stripos( $data, '<script>' ) ) {
134+
135+
// The script tag should be the only token, otherwise it's not a <script> tag.
136+
$processor = new WP_HTML_Tag_Processor( $data );
137+
$processor->next_token();
138+
if ( $processor->get_tag() === 'SCRIPT' ) {
139+
_doing_it_wrong(
140+
__FUNCTION__,
141+
sprintf(
142+
/* translators: 1: <script>, 2: wp_add_inline_script() */
143+
__( 'Do not pass %1$s tags to %2$s.' ),
144+
'<code>&lt;script&gt;</code>',
145+
'<code>wp_add_inline_script()</code>'
146+
),
147+
'4.5.0'
148+
);
149+
$data = $processor->get_modifiable_text();
150+
}
145151
}
146152

147153
return wp_scripts()->add_inline_script( $handle, $data, $position );

0 commit comments

Comments
 (0)