Skip to content
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

Allow the amp-carousel script to be used on page when there is just amp-lightbox-gallery #6509

Merged
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
10 changes: 10 additions & 0 deletions includes/class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -1503,6 +1503,16 @@ public static function ensure_required_markup( Document $dom, $script_handles =
array_keys( $amp_scripts ),
array_merge( $script_handles, [ Amp::RUNTIME ] )
);

// Allow the amp-carousel script as a special case to be on the page when there is no <amp-carousel> since the
// amp-lightbox-gallery component will lazy-load the amp-carousel script when a lightbox is opened, and since
// amp-carousel v0.1 is still the 'latest' version, this can mean that fixes needed with the 0.2 version won't
// be present on the page. Adding the amp-carousel v0.2 script is a stated workaround suggested in an AMP core
// issue: <https://github.com/ampproject/amphtml/issues/35402#issuecomment-887837815>.
if ( in_array( 'amp-lightbox-gallery', $script_handles, true ) ) {
$superfluous_script_handles = array_diff( $superfluous_script_handles, [ 'amp-carousel' ] );
}

foreach ( $superfluous_script_handles as $superfluous_script_handle ) {
if ( ! empty( $extension_specs[ $superfluous_script_handle ]['requires_usage'] ) ) {
unset( $amp_scripts[ $superfluous_script_handle ] );
Expand Down
14 changes: 11 additions & 3 deletions tests/php/test-class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -1354,7 +1354,7 @@ public function test_unneeded_scripts_get_removed() {
AMP_Theme_Support::finish_init();

// These should all get removed, unless used.
$required_usage_grandfathered = [
$required_usage_exempted = [
'amp-anim',
'amp-ad',
'amp-mustache',
Expand All @@ -1364,6 +1364,10 @@ public function test_unneeded_scripts_get_removed() {
'amp-live-list',
];

$conditionally_allowed_usage = [
'amp-carousel',
];

// These also should get removed, unless used.
$required_usage_error = [
'amp-facebook-like',
Expand All @@ -1384,7 +1388,11 @@ public function test_unneeded_scripts_get_removed() {
<html>
<head></head>
<body>
<?php wp_print_scripts( array_merge( $required_usage_grandfathered, $required_usage_error, $required_usage_none ) ); ?>
<amp-img src="https://example.com/cat.jpg" width="100" height="100" lightbox></amp-img>
<amp-img src="https://example.com/dog.jpg" width="100" height="100" lightbox></amp-img>
<amp-img src="https://example.com/bird.jpg" width="100" height="100" lightbox></amp-img>

<?php wp_print_scripts( array_merge( $required_usage_exempted, $conditionally_allowed_usage, $required_usage_error, $required_usage_none ) ); ?>
<?php wp_footer(); ?>
</body>
</html>
Expand All @@ -1403,7 +1411,7 @@ public function test_unneeded_scripts_get_removed() {
$expected_script_srcs = [
wp_scripts()->registered['amp-runtime']->src,
];
foreach ( $required_usage_none as $handle ) {
foreach ( array_merge( $required_usage_none, $conditionally_allowed_usage ) as $handle ) {
$expected_script_srcs[] = wp_scripts()->registered[ $handle ]->src;
}

Expand Down