Skip to content

Commit e045771

Browse files
committed
Script Loader: Deprecate wp_sanitize_script_attributes().
The function is no longer used by WordPress and better alternatives are available: `wp_get_script_tag()` and `wp_get_inline_script_tag()`. Developed in WordPress#10742. Follow-up to [61415], [61485]. Props jonsurrell, westonruter. Fixes #64511. See #64442. git-svn-id: https://develop.svn.wordpress.org/trunk@61518 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 9e848a3 commit e045771

File tree

3 files changed

+36
-31
lines changed

3 files changed

+36
-31
lines changed

src/wp-includes/deprecated.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6479,3 +6479,33 @@ function wp_print_auto_sizes_contain_css_fix() {
64796479
<style>img:is([sizes="auto" i], [sizes^="auto," i]) { contain-intrinsic-size: 3000px 1500px }</style>
64806480
<?php
64816481
}
6482+
6483+
/**
6484+
* Sanitizes an attributes array into an attributes string to be placed inside a `<script>` tag.
6485+
*
6486+
* This function is deprecated, use {@see wp_get_script_tag()} or {@see wp_get_inline_script_tag()} instead.
6487+
*
6488+
* @since 5.7.0
6489+
* @deprecated 7.0.0 Use wp_get_script_tag() or wp_get_inline_script_tag().
6490+
* @see wp_get_script_tag()
6491+
* @see wp_get_inline_script_tag()
6492+
*
6493+
* @param array<string, string|bool> $attributes Key-value pairs representing `<script>` tag attributes.
6494+
* @return string String made of sanitized `<script>` tag attributes.
6495+
*/
6496+
function wp_sanitize_script_attributes( $attributes ) {
6497+
_deprecated_function( __FUNCTION__, '7.0.0', 'wp_get_script_tag() or wp_get_inline_script_tag()' );
6498+
6499+
$attributes_string = '';
6500+
foreach ( $attributes as $attribute_name => $attribute_value ) {
6501+
if ( is_bool( $attribute_value ) ) {
6502+
if ( $attribute_value ) {
6503+
$attributes_string .= ' ' . esc_attr( $attribute_name );
6504+
}
6505+
} else {
6506+
$attributes_string .= sprintf( ' %1$s="%2$s"', esc_attr( $attribute_name ), esc_attr( $attribute_value ) );
6507+
}
6508+
}
6509+
return $attributes_string;
6510+
}
6511+

src/wp-includes/script-loader.php

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2882,37 +2882,6 @@ function wp_enqueue_editor_format_library_assets() {
28822882
wp_enqueue_style( 'wp-format-library' );
28832883
}
28842884

2885-
/**
2886-
* Sanitizes an attributes array into an attributes string to be placed inside a `<script>` tag.
2887-
*
2888-
* Automatically injects type attribute if needed.
2889-
* Used by {@see wp_get_script_tag()} and {@see wp_get_inline_script_tag()}.
2890-
*
2891-
* @since 5.7.0
2892-
*
2893-
* @param array<string, string|bool> $attributes Key-value pairs representing `<script>` tag attributes.
2894-
* @return string String made of sanitized `<script>` tag attributes.
2895-
*/
2896-
function wp_sanitize_script_attributes( $attributes ) {
2897-
$attributes_string = '';
2898-
2899-
/*
2900-
* If HTML5 script tag is supported, only the attribute name is added
2901-
* to $attributes_string for entries with a boolean value, and that are true.
2902-
*/
2903-
foreach ( $attributes as $attribute_name => $attribute_value ) {
2904-
if ( is_bool( $attribute_value ) ) {
2905-
if ( $attribute_value ) {
2906-
$attributes_string .= ' ' . esc_attr( $attribute_name );
2907-
}
2908-
} else {
2909-
$attributes_string .= sprintf( ' %1$s="%2$s"', esc_attr( $attribute_name ), esc_attr( $attribute_value ) );
2910-
}
2911-
}
2912-
2913-
return $attributes_string;
2914-
}
2915-
29162885
/**
29172886
* Formats `<script>` loader tags.
29182887
*

tests/phpunit/tests/dependencies/wpSanitizeScriptAttributes.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
class Tests_Dependencies_wpSanitizeScriptAttributes extends WP_UnitTestCase {
1111

1212
public function test_sanitize_script_attributes_type_set() {
13+
$this->setExpectedDeprecated( 'wp_sanitize_script_attributes' );
1314
$this->assertSame(
1415
' type="application/javascript" src="https://DOMAIN.TLD/PATH/FILE.js" nomodule',
1516
wp_sanitize_script_attributes(
@@ -24,6 +25,7 @@ public function test_sanitize_script_attributes_type_set() {
2425
}
2526

2627
public function test_sanitize_script_attributes_type_not_set() {
28+
$this->setExpectedDeprecated( 'wp_sanitize_script_attributes' );
2729
$this->assertSame(
2830
' src="https://DOMAIN.TLD/PATH/FILE.js" nomodule',
2931
wp_sanitize_script_attributes(
@@ -38,13 +40,15 @@ public function test_sanitize_script_attributes_type_not_set() {
3840

3941

4042
public function test_sanitize_script_attributes_no_attributes() {
43+
$this->setExpectedDeprecated( 'wp_sanitize_script_attributes' );
4144
$this->assertSame(
4245
'',
4346
wp_sanitize_script_attributes( array() )
4447
);
4548
}
4649

4750
public function test_sanitize_script_attributes_relative_src() {
51+
$this->setExpectedDeprecated( 'wp_sanitize_script_attributes' );
4852
$this->assertSame(
4953
' src="PATH/FILE.js" nomodule',
5054
wp_sanitize_script_attributes(
@@ -59,6 +63,7 @@ public function test_sanitize_script_attributes_relative_src() {
5963

6064

6165
public function test_sanitize_script_attributes_only_false_boolean_attributes() {
66+
$this->setExpectedDeprecated( 'wp_sanitize_script_attributes' );
6267
$this->assertSame(
6368
'',
6469
wp_sanitize_script_attributes(
@@ -71,6 +76,7 @@ public function test_sanitize_script_attributes_only_false_boolean_attributes()
7176
}
7277

7378
public function test_sanitize_script_attributes_only_true_boolean_attributes() {
79+
$this->setExpectedDeprecated( 'wp_sanitize_script_attributes' );
7480
$this->assertSame(
7581
' async nomodule',
7682
wp_sanitize_script_attributes(

0 commit comments

Comments
 (0)