Skip to content

Commit 60ebaeb

Browse files
committed
Prevent wp_print_service_workers() from happening at wp_print_footer_scripts in legacy Reader mode (#5270)
1 parent 682fcf1 commit 60ebaeb

File tree

2 files changed

+12
-18
lines changed

2 files changed

+12
-18
lines changed

includes/class-amp-service-worker.php

+10-17
Original file line numberDiff line numberDiff line change
@@ -243,27 +243,20 @@ public static function get_precached_script_cdn_urls() {
243243
* Add hooks to install the service worker from AMP page.
244244
*/
245245
public static function add_install_hooks() {
246-
if ( ! amp_is_legacy() && amp_is_request() ) {
247-
add_action( 'wp_footer', [ __CLASS__, 'install_service_worker' ] );
248-
249-
// Prevent validation error due to the script that installs the service worker on non-AMP pages.
250-
foreach ( [ 'wp_print_scripts', 'wp_print_footer_scripts' ] as $action ) {
251-
$priority = has_action( $action, 'wp_print_service_workers' );
252-
if ( false !== $priority ) {
253-
remove_action( $action, 'wp_print_service_workers', $priority );
254-
}
246+
if ( ! amp_is_request() ) {
247+
return;
248+
}
249+
250+
// Prevent validation error due to the script that installs the service worker on non-AMP pages.
251+
foreach ( [ 'wp_print_scripts', 'wp_print_footer_scripts' ] as $action ) {
252+
$priority = has_action( $action, 'wp_print_service_workers' );
253+
if ( false !== $priority ) {
254+
remove_action( $action, 'wp_print_service_workers', $priority );
255255
}
256256
}
257257

258-
// Reader mode integration.
258+
add_action( 'wp_footer', [ __CLASS__, 'install_service_worker' ] );
259259
add_action( 'amp_post_template_footer', [ __CLASS__, 'install_service_worker' ] );
260-
add_filter(
261-
'amp_post_template_data',
262-
static function ( $data ) {
263-
$data['amp_component_scripts']['amp-install-serviceworker'] = true;
264-
return $data;
265-
}
266-
);
267260
}
268261

269262
/**

tests/php/test-class-amp-service-worker.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,9 @@ public function test_add_install_hooks() {
197197
$post_id = self::factory()->post->create();
198198
$this->go_to( get_permalink( $post_id ) );
199199

200+
$this->assertFalse( amp_is_request() );
200201
AMP_Service_Worker::add_install_hooks();
201-
$this->assertSame( 10, has_action( 'amp_post_template_footer', [ 'AMP_Service_Worker', 'install_service_worker' ] ) );
202+
$this->assertFalse( has_action( 'amp_post_template_footer', [ 'AMP_Service_Worker', 'install_service_worker' ] ) );
202203
$this->assertFalse( has_action( 'wp_footer', [ 'AMP_Service_Worker', 'install_service_worker' ] ) );
203204

204205
AMP_Options_Manager::update_option( Option::THEME_SUPPORT, AMP_Theme_Support::STANDARD_MODE_SLUG );

0 commit comments

Comments
 (0)