diff --git a/pwa.php b/pwa.php index 71f319652..851872dbd 100644 --- a/pwa.php +++ b/pwa.php @@ -197,12 +197,6 @@ function _pwa_check_disabled_navigation_preload() { /** WP_HTTPS_UI Class */ require_once PWA_PLUGIN_DIR . '/wp-includes/class-wp-https-ui.php'; -/** WP_Service_Worker_Registry Interface */ -require_once PWA_PLUGIN_DIR . '/wp-includes/interface-wp-service-worker-registry.php'; - -/** WP_Service_Worker_Registry_Aware Interface */ -require_once PWA_PLUGIN_DIR . '/wp-includes/interface-wp-service-worker-registry-aware.php'; - /** WP_Service_Workers Class */ require_once PWA_PLUGIN_DIR . '/wp-includes/class-wp-service-workers.php'; diff --git a/wp-includes/class-wp-service-worker-scripts.php b/wp-includes/class-wp-service-worker-scripts.php index efc5ef510..c4f3a86f1 100644 --- a/wp-includes/class-wp-service-worker-scripts.php +++ b/wp-includes/class-wp-service-worker-scripts.php @@ -12,11 +12,8 @@ * @since 0.1 * * @see WP_Dependencies - * - * @method WP_Service_Worker_Precaching_Routes precaching_routes() - * @method WP_Service_Worker_Caching_Routes caching_routes() */ -class WP_Service_Worker_Scripts extends WP_Scripts implements WP_Service_Worker_Registry { +class WP_Service_Worker_Scripts extends WP_Scripts { /** * Service worker components. @@ -27,30 +24,36 @@ class WP_Service_Worker_Scripts extends WP_Scripts implements WP_Service_Worker_ protected $components = array(); /** - * Service worker component registries. + * Caching routes. * - * @since 0.2 - * @var array + * @since 0.6 + * @var WP_Service_Worker_Caching_Routes */ - protected $registries = array(); + protected $caching_routes; + + /** + * Precaching routes. + * + * @since 0.6 + * @var WP_Service_Worker_Precaching_Routes + */ + protected $precaching_routes; /** * Constructor. * * @since 0.2 * - * @param array $components Optional. Service worker components as $slug => $instance pairs. - * Each component must implement `WP_Service_Worker_Component`. - * Default empty array. + * @param WP_Service_Worker_Caching_Routes $caching_routes Caching routes. + * @param WP_Service_Worker_Precaching_Routes $precaching_routes Precaching routes. + * @param array $components Optional. Service worker components as $slug => $instance pairs. + * Each component must implement `WP_Service_Worker_Component`. + * Default empty array. */ - public function __construct( $components = array() ) { - $this->components = $components; - foreach ( $this->components as $slug => $instance ) { - if ( $instance instanceof WP_Service_Worker_Registry_Aware ) { - $this->registries[ $slug ] = $instance->get_registry(); - } - } - + public function __construct( $caching_routes, $precaching_routes, $components = array() ) { + $this->caching_routes = $caching_routes; + $this->precaching_routes = $precaching_routes; + $this->components = $components; parent::__construct(); } @@ -77,20 +80,25 @@ public function init() { } /** - * Magic call method. Allows accessing component registries. + * Get caching routes registry. * - * @since 0.2 + * @since 0.6 * - * @param string $method Method name. Should be the identifier for a component registry. - * @param array $args Method arguments. - * @return WP_Service_Worker_Registry|null Registry instance if valid method name, otherwise null. + * @return WP_Service_Worker_Caching_Routes Registry. */ - public function __call( $method, $args ) { - if ( isset( $this->registries[ $method ] ) ) { - return $this->registries[ $method ]; - } + public function caching_routes() { + return $this->caching_routes; + } - return null; + /** + * Get precaching routes registry. + * + * @since 0.6 + * + * @return WP_Service_Worker_Precaching_Routes Registry. + */ + public function precaching_routes() { + return $this->precaching_routes; } /** diff --git a/wp-includes/class-wp-service-workers.php b/wp-includes/class-wp-service-workers.php index 4e2263604..da9afe83d 100644 --- a/wp-includes/class-wp-service-workers.php +++ b/wp-includes/class-wp-service-workers.php @@ -13,7 +13,7 @@ * * @see WP_Dependencies */ -class WP_Service_Workers implements WP_Service_Worker_Registry_Aware { +class WP_Service_Workers { /** * Param for service workers. @@ -50,12 +50,31 @@ class WP_Service_Workers implements WP_Service_Worker_Registry_Aware { */ protected $scripts; + /** + * Caching routes. + * + * @since 0.6 + * @var WP_Service_Worker_Caching_Routes + */ + protected $caching_routes; + + /** + * Precaching routes. + * + * @since 0.6 + * @var WP_Service_Worker_Precaching_Routes + */ + protected $precaching_routes; + /** * Constructor. * * Instantiates the service worker scripts registry. */ public function __construct() { + $this->precaching_routes = new WP_Service_Worker_Precaching_Routes(); + $this->caching_routes = new WP_Service_Worker_Caching_Routes(); + $components = array( 'configuration' => new WP_Service_Worker_Configuration_Component(), 'navigation_routing' => new WP_Service_Worker_Navigation_Routing_Component(), @@ -63,11 +82,11 @@ public function __construct() { 'theme_asset_caching' => new WP_Service_Worker_Theme_Asset_Caching_Component(), 'plugin_asset_caching' => new WP_Service_Worker_Plugin_Asset_Caching_Component(), 'uploaded_image_caching' => new WP_Service_Worker_Uploaded_Image_Caching_Component(), - 'precaching_routes' => new WP_Service_Worker_Precaching_Routes_Component(), - 'caching_routes' => new WP_Service_Worker_Caching_Routes_Component(), + 'precaching_routes' => new WP_Service_Worker_Precaching_Routes_Component( $this->precaching_routes ), + 'caching_routes' => new WP_Service_Worker_Caching_Routes_Component( $this->caching_routes ), ); - $this->scripts = new WP_Service_Worker_Scripts( $components ); + $this->scripts = new WP_Service_Worker_Scripts( $this->caching_routes, $this->precaching_routes, $components ); } /** diff --git a/wp-includes/components/class-wp-service-worker-caching-routes-component.php b/wp-includes/components/class-wp-service-worker-caching-routes-component.php index 60ee2df3b..cf1713333 100644 --- a/wp-includes/components/class-wp-service-worker-caching-routes-component.php +++ b/wp-includes/components/class-wp-service-worker-caching-routes-component.php @@ -10,7 +10,7 @@ * * @since 0.2 */ -class WP_Service_Worker_Caching_Routes_Component implements WP_Service_Worker_Component, WP_Service_Worker_Registry_Aware { +class WP_Service_Worker_Caching_Routes_Component implements WP_Service_Worker_Component { /** * Caching routes registry. @@ -26,9 +26,11 @@ class WP_Service_Worker_Caching_Routes_Component implements WP_Service_Worker_Co * Instantiates the registry. * * @since 0.2 + * + * @param WP_Service_Worker_Caching_Routes $registry Registry. */ - public function __construct() { - $this->registry = new WP_Service_Worker_Caching_Routes(); + public function __construct( WP_Service_Worker_Caching_Routes $registry ) { + $this->registry = $registry; } /** @@ -59,15 +61,6 @@ public function get_priority() { return 999999; } - /** - * Gets the registry. - * - * @return WP_Service_Worker_Caching_Routes Caching routes registry instance. - */ - public function get_registry() { - return $this->registry; - } - /** * Gets the script that registers the caching routes. * diff --git a/wp-includes/components/class-wp-service-worker-caching-routes.php b/wp-includes/components/class-wp-service-worker-caching-routes.php index 0554deebb..116f9a59a 100644 --- a/wp-includes/components/class-wp-service-worker-caching-routes.php +++ b/wp-includes/components/class-wp-service-worker-caching-routes.php @@ -10,7 +10,7 @@ * * @since 0.2 */ -class WP_Service_Worker_Caching_Routes implements WP_Service_Worker_Registry { +class WP_Service_Worker_Caching_Routes { /** * Stale while revalidate caching strategy. @@ -79,36 +79,43 @@ class WP_Service_Worker_Caching_Routes implements WP_Service_Worker_Registry { * * @since 0.2 * - * @param string $route Route regular expression, without delimiters. - * @param array|string $args { - * Additional route arguments, or else just the strategy name as a string. + * @param string $route Route regular expression, without delimiters. + * @param string|array $strategy Strategy, can be WP_Service_Worker_Caching_Routes::STRATEGY_STALE_WHILE_REVALIDATE, + * WP_Service_Worker_Caching_Routes::STRATEGY_CACHE_FIRST, WP_Service_Worker_Caching_Routes::STRATEGY_NETWORK_FIRST, + * WP_Service_Worker_Caching_Routes::STRATEGY_CACHE_ONLY, WP_Service_Worker_Caching_Routes::STRATEGY_NETWORK_ONLY. + * Deprecated usage: supplying strategy args as an array. + * @param array $args { + * Additional caching strategy route arguments. * - * @type string $strategy Strategy, can be WP_Service_Worker_Caching_Routes::STRATEGY_STALE_WHILE_REVALIDATE, - * WP_Service_Worker_Caching_Routes::STRATEGY_CACHE_FIRST, WP_Service_Worker_Caching_Routes::STRATEGY_NETWORK_FIRST, - * WP_Service_Worker_Caching_Routes::STRATEGY_CACHE_ONLY, WP_Service_Worker_Caching_Routes::STRATEGY_NETWORK_ONLY. - * Required. - * @type string $cache_name Name to use for the cache. - * @type array|null $expiration Expiration plugin configuration. See . - * @type array|null $broadcast_update Broadcast update plugin configuration. See . - * @type array|null $cacheable_response Cacheable response plugin configuration. See . - * @type array|null $background_sync Background sync plugin configuration. See . - * @type array|null $plugins Deprecated. Array of plugins with configuration. The key of each plugin in the array must match the plugin's name. - * This is deprecated in favor of defining the plugins in the top-level. - * See . + * @type string $cache_name Name to use for the cache. + * @type array $expiration Expiration plugin configuration. See . + * @type array $broadcast_update Broadcast update plugin configuration. See . + * @type array $cacheable_response Cacheable response plugin configuration. See . + * @type array $background_sync Background sync plugin configuration. See . + * @type array $plugins Deprecated. Array of plugins with configuration. The key of each plugin in the array must match the plugin's name. + * This is deprecated in favor of defining the plugins in the top-level. + * See . * @return bool Whether the registration was successful. * } */ - public function register( $route, $args = array() ) { - if ( is_string( $args ) ) { - $args = array( 'strategy' => $args ); - } elseif ( ! is_array( $args ) ) { + public function register( $route, $strategy, $args = array() ) { + + // In versions prior to 0.6, this argument could either be a strategy string or an array of strategy args containing the strategy name. + if ( is_array( $strategy ) ) { + $args = $strategy; + $strategy = isset( $args['strategy'] ) ? $args['strategy'] : null; + unset( $args['strategy'] ); + } + + if ( empty( $strategy ) || ! is_string( $strategy ) ) { _doing_it_wrong( __METHOD__, - esc_html__( 'Args must either be an array or a strategy name as a string.', 'pwa' ), + esc_html__( 'Strategy must be supplied.', 'pwa' ), '0.6' ); return false; } + $args['strategy'] = $strategy; if ( empty( $route ) || ! is_string( $route ) ) { _doing_it_wrong( diff --git a/wp-includes/components/class-wp-service-worker-core-asset-caching-component.php b/wp-includes/components/class-wp-service-worker-core-asset-caching-component.php index f26114521..bb10086fb 100644 --- a/wp-includes/components/class-wp-service-worker-core-asset-caching-component.php +++ b/wp-includes/components/class-wp-service-worker-core-asset-caching-component.php @@ -76,7 +76,10 @@ public function serve( WP_Service_Worker_Scripts $scripts ) { $route = $config['route']; unset( $config['route'] ); - $scripts->caching_routes()->register( $route, $config ); + $strategy = $config['strategy']; + unset( $config['strategy'] ); + + $scripts->caching_routes()->register( $route, $strategy, $config ); } /** diff --git a/wp-includes/components/class-wp-service-worker-plugin-asset-caching-component.php b/wp-includes/components/class-wp-service-worker-plugin-asset-caching-component.php index d5e3e7f99..61de4ae1b 100644 --- a/wp-includes/components/class-wp-service-worker-plugin-asset-caching-component.php +++ b/wp-includes/components/class-wp-service-worker-plugin-asset-caching-component.php @@ -76,7 +76,10 @@ public function serve( WP_Service_Worker_Scripts $scripts ) { $route = $config['route']; unset( $config['route'] ); - $scripts->caching_routes()->register( $route, $config ); + $strategy = $config['strategy']; + unset( $config['strategy'] ); + + $scripts->caching_routes()->register( $route, $strategy, $config ); } /** diff --git a/wp-includes/components/class-wp-service-worker-precaching-routes-component.php b/wp-includes/components/class-wp-service-worker-precaching-routes-component.php index 805a5cefb..f41f6a674 100644 --- a/wp-includes/components/class-wp-service-worker-precaching-routes-component.php +++ b/wp-includes/components/class-wp-service-worker-precaching-routes-component.php @@ -10,7 +10,7 @@ * * @since 0.2 */ -class WP_Service_Worker_Precaching_Routes_Component implements WP_Service_Worker_Component, WP_Service_Worker_Registry_Aware { +class WP_Service_Worker_Precaching_Routes_Component implements WP_Service_Worker_Component { /** * Precaching routes registry. @@ -23,12 +23,12 @@ class WP_Service_Worker_Precaching_Routes_Component implements WP_Service_Worker /** * Constructor. * - * Instantiates the registry. - * * @since 0.2 + * + * @param WP_Service_Worker_Precaching_Routes $registry Registry. */ - public function __construct() { - $this->registry = new WP_Service_Worker_Precaching_Routes(); + public function __construct( WP_Service_Worker_Precaching_Routes $registry ) { + $this->registry = $registry; } /** @@ -64,15 +64,6 @@ public function get_priority() { return -99999; } - /** - * Gets the registry. - * - * @return WP_Service_Worker_Precaching_Routes Precaching routes registry instance. - */ - public function get_registry() { - return $this->registry; - } - /** * Gets the script that registers the precaching routes. * diff --git a/wp-includes/components/class-wp-service-worker-precaching-routes.php b/wp-includes/components/class-wp-service-worker-precaching-routes.php index 451cafcda..7c3aa11fc 100644 --- a/wp-includes/components/class-wp-service-worker-precaching-routes.php +++ b/wp-includes/components/class-wp-service-worker-precaching-routes.php @@ -10,7 +10,7 @@ * * @since 0.2 */ -class WP_Service_Worker_Precaching_Routes implements WP_Service_Worker_Registry { +class WP_Service_Worker_Precaching_Routes { /** * Registered caching routes. diff --git a/wp-includes/components/class-wp-service-worker-theme-asset-caching-component.php b/wp-includes/components/class-wp-service-worker-theme-asset-caching-component.php index ab2ef0185..de56ee5ab 100644 --- a/wp-includes/components/class-wp-service-worker-theme-asset-caching-component.php +++ b/wp-includes/components/class-wp-service-worker-theme-asset-caching-component.php @@ -43,7 +43,7 @@ public function serve( WP_Service_Worker_Scripts $scripts ) { 'strategy' => WP_Service_Worker_Caching_Routes::STRATEGY_NETWORK_FIRST, 'cache_name' => self::CACHE_NAME, 'expiration' => array( - 'maxEntries' => 34, + 'max_entries' => 34, ), ); @@ -83,7 +83,10 @@ public function serve( WP_Service_Worker_Scripts $scripts ) { $route = $config['route']; unset( $config['route'] ); - $scripts->caching_routes()->register( $route, $config ); + $strategy = $config['strategy']; + unset( $config['strategy'] ); + + $scripts->caching_routes()->register( $route, $strategy, $config ); } /** diff --git a/wp-includes/components/class-wp-service-worker-uploaded-image-caching-component.php b/wp-includes/components/class-wp-service-worker-uploaded-image-caching-component.php index 56cc5d969..f203fd88c 100644 --- a/wp-includes/components/class-wp-service-worker-uploaded-image-caching-component.php +++ b/wp-includes/components/class-wp-service-worker-uploaded-image-caching-component.php @@ -102,7 +102,10 @@ static function ( $image_extension ) { $route = $config['route']; unset( $config['route'] ); - $scripts->caching_routes()->register( $route, $config ); + $strategy = $config['strategy']; + unset( $config['strategy'] ); + + $scripts->caching_routes()->register( $route, $strategy, $config ); } /** diff --git a/wp-includes/interface-wp-service-worker-registry-aware.php b/wp-includes/interface-wp-service-worker-registry-aware.php deleted file mode 100644 index a472e8a13..000000000 --- a/wp-includes/interface-wp-service-worker-registry-aware.php +++ /dev/null @@ -1,21 +0,0 @@ -