Skip to content

Commit cff0d8f

Browse files
committed
Ensure ID attribute is added to scripts in WP<5.5
1 parent 15a2a0f commit cff0d8f

File tree

3 files changed

+58
-3
lines changed

3 files changed

+58
-3
lines changed

includes/amp-helper-functions.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ function amp_bootstrap_plugin() {
9292
// Ensure async and custom-element/custom-template attributes are present on script tags.
9393
add_filter( 'script_loader_tag', 'amp_filter_script_loader_tag', PHP_INT_MAX, 2 );
9494

95+
// Ensure ID attribute is present in WP<5.5.
96+
if ( version_compare( get_bloginfo( 'version' ), '5.5', '<' ) ) {
97+
add_filter( 'script_loader_tag', 'amp_ensure_id_attribute_script_loader_tag', 1000, 2 );
98+
}
99+
95100
// Ensure crossorigin=anonymous is added to font links.
96101
add_filter( 'style_loader_tag', 'amp_filter_font_style_loader_tag_with_crossorigin_anonymous', 10, 4 );
97102

@@ -1157,6 +1162,31 @@ function amp_filter_script_loader_tag( $tag, $handle ) {
11571162
return $tag;
11581163
}
11591164

1165+
/**
1166+
* Ensure ID attribute is added to printed scripts.
1167+
*
1168+
* Core started adding in WP 5.5. This is used both by validation logic for sourcing attribution as well as in the
1169+
* script and comments sanitizers.
1170+
*
1171+
* @link https://core.trac.wordpress.org/changeset/48295
1172+
* @since 2.2
1173+
* @internal
1174+
*
1175+
* @param string $tag The <script> tag for the enqueued script.
1176+
* @param string $handle The script's registered handle.
1177+
* @return string Filtered script.
1178+
*/
1179+
function amp_ensure_id_attribute_script_loader_tag( $tag, $handle ) {
1180+
if ( false === strpos( $tag, ' id="' ) ) {
1181+
$tag = str_replace(
1182+
'<script ',
1183+
sprintf( '<script id="%s" ', esc_attr( "$handle-js" ) ),
1184+
$tag
1185+
);
1186+
}
1187+
return $tag;
1188+
}
1189+
11601190
/**
11611191
* Explicitly opt-in to CORS mode by adding the crossorigin attribute to font stylesheet links.
11621192
*

includes/validation/class-amp-validation-manager.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -951,9 +951,16 @@ protected static function has_dependency( WP_Dependencies $dependencies, $curren
951951
* @return bool
952952
*/
953953
protected static function is_matching_script( DOMElement $element, $script_handle ) {
954+
955+
// Use the ID attribute which was added to printed scripts after WP ?.?.
956+
if ( $element->getAttribute( Attribute::ID ) === "{$script_handle}-js" ) {
957+
return true;
958+
}
959+
954960
if ( ! isset( wp_scripts()->registered[ $script_handle ] ) ) {
955961
return false;
956962
}
963+
957964
$script_dependency = wp_scripts()->registered[ $script_handle ];
958965
if ( empty( $script_dependency->src ) ) {
959966
return false;
@@ -1002,7 +1009,7 @@ public static function locate_sources( DOMNode $node ) {
10021009
&&
10031010
'link' === $node->nodeName
10041011
&&
1005-
preg_match( '/(?P<handle>.+)-css$/', (string) $node->getAttribute( 'id' ), $matches )
1012+
preg_match( '/(?P<handle>.+)-css$/', (string) $node->getAttribute( Attribute::ID ), $matches )
10061013
&&
10071014
wp_styles()->query( $matches['handle'] )
10081015
);
@@ -1047,9 +1054,9 @@ static function ( $enqueued_style_source ) use ( $style_handle ) {
10471054
&&
10481055
$node->firstChild instanceof DOMText
10491056
&&
1050-
$node->hasAttribute( 'id' )
1057+
$node->hasAttribute( Attribute::ID )
10511058
&&
1052-
preg_match( '/^(?P<handle>.+)-inline-css$/', $node->getAttribute( 'id' ), $matches )
1059+
preg_match( '/^(?P<handle>.+)-inline-css$/', $node->getAttribute( Attribute::ID ), $matches )
10531060
&&
10541061
wp_styles()->query( $matches['handle'] )
10551062
&&

tests/php/test-amp-helper-functions.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@ public function test_amp_bootstrap_plugin() {
128128
$this->assertEquals( 9, has_action( 'plugins_loaded', '_amp_bootstrap_customizer' ) );
129129

130130
$this->assertEquals( PHP_INT_MAX, has_filter( 'script_loader_tag', 'amp_filter_script_loader_tag' ) );
131+
if ( version_compare( get_bloginfo( 'version' ), '5.5', '<' ) ) {
132+
$this->assertEquals( 1000, has_filter( 'script_loader_tag', 'amp_ensure_id_attribute_script_loader_tag' ) );
133+
} else {
134+
$this->assertFalse( has_filter( 'script_loader_tag', 'amp_ensure_id_attribute_script_loader_tag' ) );
135+
}
131136
$this->assertEquals( 10, has_filter( 'style_loader_tag', 'amp_filter_font_style_loader_tag_with_crossorigin_anonymous' ) );
132137
$this->assertEquals( 10, has_filter( 'all_plugins', 'amp_modify_plugin_description' ) );
133138
}
@@ -145,6 +150,19 @@ public function test_amp_bootstrap_plugin_amp_disabled() {
145150
}
146151
}
147152

153+
/** @covers ::amp_ensure_id_attribute_script_loader_tag() */
154+
public function test_amp_ensure_id_attribute_script_loader_tag() {
155+
$this->assertEquals(
156+
'<script id="foo-js" src="foo.js"></script>',
157+
amp_ensure_id_attribute_script_loader_tag( '<script src="foo.js"></script>', 'foo' )
158+
);
159+
160+
$this->assertEquals(
161+
'<script src="foo.js" id="bar"></script>',
162+
amp_ensure_id_attribute_script_loader_tag( '<script src="foo.js" id="bar"></script>', 'foo' )
163+
);
164+
}
165+
148166
/** @covers ::amp_init() */
149167
public function test_amp_init_migration() {
150168
global $wp_actions;

0 commit comments

Comments
 (0)