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

Conditionally installing service workers on registered scripts or offline browsing enabled. #727

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
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
46 changes: 46 additions & 0 deletions tests/test-service-workers.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,50 @@ public function test_wp_get_service_worker_url_with_permalinks() {
wp_get_service_worker_url( WP_Service_Workers::SCOPE_ADMIN )
);
}

/**
* Test wp_get_actions_with_registered_service_worker_scripts() when no scripts are added.
*
* @covers ::wp_get_actions_with_registered_service_worker_scripts()
*/
public function test_wp_get_actions_with_registered_service_worker_scripts_with_no_scripts() {

remove_all_actions( 'wp_front_service_worker' );
remove_all_actions( 'wp_admin_service_worker' );

$this->assertEquals(
array(),
wp_get_actions_with_registered_service_worker_scripts()
);
}

/**
* Test wp_get_actions_with_registered_service_worker_scripts() when scripts are added.
*
* @covers ::wp_get_actions_with_registered_service_worker_scripts()
*/
public function test_wp_get_actions_with_registered_service_worker_scripts_with_registered_scripts() {
// Register for the admin service worker.
add_action( 'wp_front_service_worker', 'register_foo_sw' );

$this->assertEquals(
array( 'wp_front_service_worker' ),
wp_get_actions_with_registered_service_worker_scripts()
);
}

/**
* Register a service worker.
*
* @param WP_Service_Worker_Scripts $scripts Registry instance.
*/
public function register_foo_sw( $scripts ) {
$scripts->register(
'foo',
array(
'src' => array( $this, 'return_foo_sw' ),
'deps' => array(),
)
);
}
}
50 changes: 44 additions & 6 deletions wp-includes/default-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,52 @@
* @package PWA
*/

// Ensure service workers are printed on frontend, admin, Customizer, login, sign-up, and activate pages.
foreach ( array( 'wp_print_footer_scripts', 'admin_print_scripts', 'customize_controls_print_scripts', 'login_footer', 'after_signup_form', 'activate_wp_head' ) as $filter ) {
add_filter( $filter, 'wp_print_service_workers', 9 );
$service_worker_actions = wp_get_actions_with_registered_service_worker_scripts();

// Array of filter hooks that are used to trigger the service worker.
$hooks = array(
'front' => array(
'wp_print_footer_scripts',
),
'admin' => array(
'admin_print_scripts',
'customize_controls_print_scripts',
'login_footer',
'after_signup_form',
'activate_wp_head',
),
);

if ( get_option( 'offline_browsing' ) ) {
// Ensure service workers are printed on frontend, admin, Customizer, login, sign-up, and activate pages.
foreach ( array_merge( $hooks['front'], $hooks['admin'] ) as $filter ) {
add_filter( $filter, 'wp_print_service_workers', 9 );
}

add_action( 'parse_query', 'wp_service_worker_loaded' );
add_action( 'wp_ajax_wp_service_worker', 'wp_ajax_wp_service_worker' );
add_action( 'wp_ajax_nopriv_wp_service_worker', 'wp_ajax_wp_service_worker' );
} else if ( ! empty( $service_worker_actions ) ) {
if ( in_array( 'wp_front_service_worker', $service_worker_actions, true ) ) {
// Ensure service workers are printed on frontend.
foreach ( $hooks['front'] as $filter ) {
add_filter( $filter, 'wp_print_service_workers', 9 );
}

add_action( 'parse_query', 'wp_service_worker_loaded' );
}

if ( in_array( 'wp_admin_service_worker', $service_worker_actions, true ) ) {
// Ensure service workers are printed on admin, Customizer, login, sign-up, and activate pages.
foreach ( $hooks['admin'] as $filter ) {
add_filter( $filter, 'wp_print_service_workers', 9 );
}

add_action( 'wp_ajax_wp_service_worker', 'wp_ajax_wp_service_worker' );
add_action( 'wp_ajax_nopriv_wp_service_worker', 'wp_ajax_wp_service_worker' );
}
}

add_action( 'parse_query', 'wp_service_worker_loaded' );
add_action( 'wp_ajax_wp_service_worker', 'wp_ajax_wp_service_worker' );
add_action( 'wp_ajax_nopriv_wp_service_worker', 'wp_ajax_wp_service_worker' );
add_action( 'parse_query', 'wp_unauthenticate_error_template_requests' );

if ( version_compare( strtok( get_bloginfo( 'version' ), '-' ), '5.7', '>=' ) ) {
Expand Down
14 changes: 14 additions & 0 deletions wp-includes/service-workers.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,17 @@ function wp_service_worker_skip_waiting() {
*/
return (bool) apply_filters( 'wp_service_worker_skip_waiting', true );
}

/**
* Function to check if service worker scripts are registered on
* `wp_front_service_worker` or `wp_admin_service_worker` action hooks.
*
* @return array Array of hooks to which the actions are registered.
*/
function wp_get_actions_with_registered_service_worker_scripts() {
$actions = array();
has_action( 'wp_front_service_worker' ) ? $actions[] = 'wp_front_service_worker' : null;
has_action( 'wp_admin_service_worker' ) ? $actions[] = 'wp_admin_service_worker' : null;

return $actions;
}