-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #125 from xwp/tweak/integrations-flag
Separate service worker integrations from core and hide them behind feature flag
- Loading branch information
Showing
18 changed files
with
209 additions
and
143 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
/** | ||
* Service Worker functions related to integrations. | ||
* | ||
* These are experimental and therefore kept separate from the service worker core code. | ||
* | ||
* @since 0.2 | ||
* | ||
* @package PWA | ||
*/ | ||
|
||
/** | ||
* Registers all default service workers. | ||
* | ||
* @since 0.2 | ||
* | ||
* @param WP_Service_Worker_Scripts $scripts Instance to register service worker behavior with. | ||
*/ | ||
function wp_default_service_workers( $scripts ) { | ||
$scripts->base_url = site_url(); | ||
$scripts->content_url = defined( 'WP_CONTENT_URL' ) ? WP_CONTENT_URL : ''; | ||
$scripts->default_version = get_bloginfo( 'version' ); | ||
|
||
$integrations = array( | ||
'wp-site-icon' => new WP_Service_Worker_Site_Icon_Integration(), | ||
'wp-custom-logo' => new WP_Service_Worker_Custom_Logo_Integration(), | ||
'wp-custom-header' => new WP_Service_Worker_Custom_Header_Integration(), | ||
'wp-custom-background' => new WP_Service_Worker_Custom_Background_Integration(), | ||
'wp-scripts' => new WP_Service_Worker_Scripts_Integration(), | ||
'wp-styles' => new WP_Service_Worker_Styles_Integration(), | ||
'wp-fonts' => new WP_Service_Worker_Fonts_Integration(), | ||
); | ||
|
||
if ( ! SCRIPT_DEBUG ) { | ||
$integrations['wp-admin-assets'] = new WP_Service_Worker_Admin_Assets_Integration(); | ||
} | ||
|
||
/** | ||
* Filters the service worker integrations to initialize. | ||
* | ||
* @since 0.2 | ||
* | ||
* @param array $integrations Array of $slug => $integration pairs, where $integration is an instance | ||
* of a class that implements the WP_Service_Worker_Integration interface. | ||
*/ | ||
$integrations = apply_filters( 'wp_service_worker_integrations', $integrations ); | ||
|
||
foreach ( $integrations as $slug => $integration ) { | ||
if ( ! $integration instanceof WP_Service_Worker_Integration ) { | ||
_doing_it_wrong( | ||
__FUNCTION__, | ||
sprintf( | ||
/* translators: 1: integration slug, 2: interface name */ | ||
esc_html__( 'The integration with slug %1$s does not implement the %2$s interface.', 'pwa' ), | ||
esc_html( $slug ), | ||
'WP_Service_Worker_Integration' | ||
), | ||
'0.2' | ||
); | ||
continue; | ||
} | ||
|
||
$scope = $integration->get_scope(); | ||
$priority = $integration->get_priority(); | ||
switch ( $scope ) { | ||
case WP_Service_Workers::SCOPE_FRONT: | ||
add_action( 'wp_front_service_worker', array( $integration, 'register' ), $priority, 1 ); | ||
break; | ||
case WP_Service_Workers::SCOPE_ADMIN: | ||
add_action( 'wp_admin_service_worker', array( $integration, 'register' ), $priority, 1 ); | ||
break; | ||
case WP_Service_Workers::SCOPE_ALL: | ||
add_action( 'wp_front_service_worker', array( $integration, 'register' ), $priority, 1 ); | ||
add_action( 'wp_admin_service_worker', array( $integration, 'register' ), $priority, 1 ); | ||
break; | ||
default: | ||
$valid_scopes = array( WP_Service_Workers::SCOPE_FRONT, WP_Service_Workers::SCOPE_ADMIN, WP_Service_Workers::SCOPE_ALL ); | ||
_doing_it_wrong( | ||
__FUNCTION__, | ||
sprintf( | ||
/* translators: 1: integration slug, 2: a comma-separated list of valid scopes */ | ||
esc_html__( 'Scope for integration %1$s must be one out of %2$s.', 'pwa' ), | ||
esc_html( $slug ), | ||
esc_html( implode( ', ', $valid_scopes ) ) | ||
), | ||
'0.1' | ||
); | ||
} | ||
} | ||
} | ||
add_action( 'wp_default_service_workers', 'wp_default_service_workers' ); |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.