2016-02-11 19:33:59 +03:00
|
|
|
<?php
|
|
|
|
|
2016-04-06 14:42:02 +03:00
|
|
|
namespace Mozilla;
|
|
|
|
|
2016-02-11 22:34:44 +03:00
|
|
|
if (!class_exists('WP_SW_Manager')) {
|
2016-02-11 19:33:59 +03:00
|
|
|
/**
|
2016-02-11 22:34:44 +03:00
|
|
|
* Holds the shared manager for composing the service workers.
|
2016-02-11 19:33:59 +03:00
|
|
|
*
|
2016-02-11 22:34:44 +03:00
|
|
|
* Service Workers enable web applications to send push notifications, work
|
|
|
|
* offline or perform background tasks periodically. Currently the standard only
|
|
|
|
* allows **one service worker per scope** making it hard for plugin developers
|
|
|
|
* to combine different focused and isolated functionality.
|
2016-02-11 19:33:59 +03:00
|
|
|
*
|
2016-02-11 22:34:44 +03:00
|
|
|
* The WP_SW_Manager library provides a collaborative way to generate service
|
|
|
|
* workers. It is as simple as registering a callback for writing your service
|
|
|
|
* worker functionality:
|
2016-02-11 19:33:59 +03:00
|
|
|
*
|
2016-02-11 22:34:44 +03:00
|
|
|
* ```php
|
|
|
|
* WP_SW_Manager::get_manager()->sw()->add_contents(write_sw);
|
2016-02-11 19:33:59 +03:00
|
|
|
*
|
2016-02-11 22:34:44 +03:00
|
|
|
* function write_sw() {
|
|
|
|
* echo 'console.log("Here is my plugin!")';
|
|
|
|
* }
|
|
|
|
* ```
|
2016-02-11 19:33:59 +03:00
|
|
|
*
|
2016-02-18 19:50:35 +03:00
|
|
|
* @version 0.2.0
|
2016-02-11 22:34:44 +03:00
|
|
|
* @license GPL
|
|
|
|
* @author Salvador de la Puente González <salva@unoyunodiez.com>
|
2016-02-11 19:33:59 +03:00
|
|
|
*/
|
2016-02-11 22:34:44 +03:00
|
|
|
class WP_SW_Manager {
|
|
|
|
/**
|
|
|
|
* The name of the enqueued script in charge of registering the
|
|
|
|
* service workers.
|
|
|
|
*
|
|
|
|
* In case you have client code depending on the registration of
|
|
|
|
* a service worker, use this const as dependency to ensure your
|
|
|
|
* script is added **after** the registration script:
|
|
|
|
*
|
|
|
|
* ```php
|
|
|
|
* wp_register_script('my-plugin-script', 'url/to/my/script.js', array(WP_SW_Manager::SW_REGISTRAR_SCRIPT));
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* From your JavaScript code, you can access these registrations
|
|
|
|
* via `$swRegistrations` variable.
|
|
|
|
*
|
|
|
|
* @see WP_SW_Manager::get_js_id()
|
|
|
|
* @api
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
const SW_REGISTRAR_SCRIPT = 'wp-sw-manager-registrar';
|
2016-02-12 11:56:36 +03:00
|
|
|
|
2016-04-04 16:48:38 +03:00
|
|
|
const SW_REGISTRAR_SCRIPT_URL = 'wpswmanager_sw-registrar.js';
|
2016-02-12 11:56:36 +03:00
|
|
|
|
2016-02-11 22:34:44 +03:00
|
|
|
private static $instance;
|
2016-02-12 11:56:36 +03:00
|
|
|
|
2016-02-11 22:34:44 +03:00
|
|
|
/**
|
|
|
|
* Obtains the shared manager.
|
|
|
|
*
|
|
|
|
* @api
|
|
|
|
* @returns WP_SW_Manager The shared manager instance.
|
|
|
|
*/
|
|
|
|
public static function get_manager() {
|
|
|
|
if (!self::$instance) {
|
|
|
|
self::$instance = new self();
|
|
|
|
}
|
|
|
|
return self::$instance;
|
2016-02-11 19:33:59 +03:00
|
|
|
}
|
2016-02-12 11:56:36 +03:00
|
|
|
|
2016-04-04 16:48:38 +03:00
|
|
|
private $dynamic_server;
|
|
|
|
|
2016-02-11 22:34:44 +03:00
|
|
|
private $router;
|
2016-02-12 11:56:36 +03:00
|
|
|
|
2016-02-11 22:34:44 +03:00
|
|
|
private $service_workers;
|
2016-02-12 11:56:36 +03:00
|
|
|
|
2016-02-11 22:34:44 +03:00
|
|
|
private function __construct() {
|
2016-04-06 15:28:46 +03:00
|
|
|
$this->dynamic_server = WP_Serve_File::getInstance();
|
2016-02-11 22:34:44 +03:00
|
|
|
$this->router = WP_SW_Manager_Router::get_router();
|
|
|
|
$this->service_workers = array();
|
|
|
|
$this->setup_sw_registrar_script();
|
2016-02-11 19:33:59 +03:00
|
|
|
}
|
2016-02-12 11:56:36 +03:00
|
|
|
|
2016-02-11 22:34:44 +03:00
|
|
|
/**
|
|
|
|
* Selects the combinator representing the service worker to write into.
|
|
|
|
*
|
|
|
|
* As only one service worker per scope is currently allowed by the
|
|
|
|
* ServiceWorker API, the manager identify the proper service worker
|
|
|
|
* based on the scope only.
|
|
|
|
*
|
|
|
|
* @api
|
|
|
|
* @param string $scope The scope used to select the service worker.
|
2016-02-12 11:56:36 +03:00
|
|
|
* If omitted, it defaults to the URL where WordPress site is installed.
|
2016-02-11 22:34:44 +03:00
|
|
|
* @return WP_SW_Combinator The combinator instance to generate the
|
|
|
|
* content of the service worker.
|
|
|
|
*/
|
|
|
|
public function sw($scope='') {
|
|
|
|
if (!$scope) { $scope = $this->default_scope(); }
|
|
|
|
if (!array_key_exists($scope, $this->service_workers)) {
|
|
|
|
$this->add_new_sw($scope);
|
|
|
|
}
|
|
|
|
return $this->service_workers[$scope];
|
|
|
|
}
|
2016-02-12 11:56:36 +03:00
|
|
|
|
2016-02-11 22:34:44 +03:00
|
|
|
/**
|
|
|
|
* Obtains the JavaScript ID to select the registration promise in
|
|
|
|
* JavaScript client code.
|
|
|
|
*
|
|
|
|
* When attaching content to a service worker combinator, the worker
|
|
|
|
* is automatically [registered](https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/register)
|
|
|
|
* with a client JavaScript script. If you need to access this registration,
|
|
|
|
* you can use the `$swRegistrations` object in your client scripts. This
|
|
|
|
* object is a map between service workers and registration promises. To index
|
|
|
|
* the proper service worker you need a unique key that can be retrieved
|
|
|
|
* with this function.
|
|
|
|
*
|
|
|
|
* @api
|
|
|
|
* @param string $scope The scope identifying the service worker.
|
2016-02-12 11:56:36 +03:00
|
|
|
* If omitted, it defaults to the URL where WordPress site is installed.
|
2016-02-11 22:34:44 +03:00
|
|
|
* @return string A string representing the unique identifier for the
|
|
|
|
* service worker registration promise.
|
|
|
|
*/
|
|
|
|
public function sw_js_id($scope='') {
|
|
|
|
if (!$scope) { $scope = $this->default_scope(); }
|
|
|
|
return $scope;
|
|
|
|
}
|
2016-02-12 11:56:36 +03:00
|
|
|
|
2016-02-11 22:34:44 +03:00
|
|
|
private function default_scope() {
|
2016-02-12 11:56:36 +03:00
|
|
|
return site_url('/', 'relative');
|
2016-02-11 22:34:44 +03:00
|
|
|
}
|
2016-02-12 11:56:36 +03:00
|
|
|
|
2016-02-11 22:34:44 +03:00
|
|
|
private function setup_sw_registrar_script() {
|
2016-04-04 16:48:38 +03:00
|
|
|
$this->dynamic_server->add_file(self::SW_REGISTRAR_SCRIPT_URL, array($this, 'sw_registrar'));
|
|
|
|
add_action('init', array($this, 'check_registrations'), 999);
|
2016-02-11 22:34:44 +03:00
|
|
|
add_action('wp_enqueue_scripts', array($this, 'enqueue_registrar'));
|
|
|
|
}
|
2016-02-12 11:56:36 +03:00
|
|
|
|
2016-04-04 16:48:38 +03:00
|
|
|
public function check_registrations() {
|
|
|
|
$last_registrations = get_option('wpswmanager_registrations', array());
|
|
|
|
$current_registrations = array_keys($this->service_workers);
|
|
|
|
$areEqual = array_count_values($last_registrations) == array_count_values($current_registrations);
|
|
|
|
if (!$areEqual) {
|
|
|
|
$this->dynamic_server->invalidate_files(array(self::SW_REGISTRAR_SCRIPT_URL));
|
|
|
|
update_option('wpswmanager_registrations', $current_registrations);
|
2016-03-12 22:55:17 +03:00
|
|
|
}
|
2016-04-04 16:48:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function enqueue_registrar() {
|
|
|
|
$url = WP_Serve_File::get_relative_to_wp_root_url(self::SW_REGISTRAR_SCRIPT_URL);
|
|
|
|
wp_enqueue_script(self::SW_REGISTRAR_SCRIPT, $url);
|
2016-02-11 22:34:44 +03:00
|
|
|
}
|
2016-02-12 11:56:36 +03:00
|
|
|
|
2016-02-11 22:34:44 +03:00
|
|
|
private function add_new_sw($scope) {
|
|
|
|
$virtual_url = "wpswmanager/sw/sw@$scope";
|
|
|
|
$real_url = $this->router->add_route($virtual_url, array($this, 'write_sw'), $scope);
|
|
|
|
$service_worker = new WP_SW_Manager_Combinator($real_url);
|
|
|
|
$this->service_workers[$scope] = $service_worker;
|
|
|
|
}
|
2016-02-12 11:56:36 +03:00
|
|
|
|
2016-02-11 22:34:44 +03:00
|
|
|
public function sw_registrar() {
|
|
|
|
$contents = file_get_contents(__DIR__ . '/lib/js/sw-registrar.js');
|
|
|
|
$contents = str_replace('$enabledSw', $this->json_for_sw_registrations(), $contents);
|
2016-04-04 16:48:38 +03:00
|
|
|
return array(
|
|
|
|
'content' => $contents,
|
2016-04-05 19:51:56 +03:00
|
|
|
'contentType' => 'application/javascript'
|
2016-04-04 16:48:38 +03:00
|
|
|
);
|
2016-02-11 22:34:44 +03:00
|
|
|
}
|
2016-02-12 11:56:36 +03:00
|
|
|
|
2016-02-11 22:34:44 +03:00
|
|
|
public function write_sw($scope) {
|
|
|
|
$service_worker = $this->service_workers[$scope];
|
|
|
|
header('Content-Type: application/javascript');
|
|
|
|
header('Service-Worker-Allowed: ' . $scope);
|
2016-02-25 13:49:58 +03:00
|
|
|
include(__DIR__ . '/lib/js/localforage.nopromises.min.js');
|
2016-04-04 21:32:27 +03:00
|
|
|
include(__DIR__ . '/lib/js/sw-base.js');
|
|
|
|
include(__DIR__ . '/lib/js/sw-start.js');
|
2016-02-11 22:34:44 +03:00
|
|
|
$service_worker->write_content();
|
|
|
|
$this->end();
|
|
|
|
}
|
2016-02-12 11:56:36 +03:00
|
|
|
|
2016-02-11 22:34:44 +03:00
|
|
|
private function end() {
|
2016-03-15 20:00:44 +03:00
|
|
|
wp_die();
|
2016-02-11 22:34:44 +03:00
|
|
|
}
|
2016-02-12 11:56:36 +03:00
|
|
|
|
2016-02-11 22:34:44 +03:00
|
|
|
private function json_for_sw_registrations() {
|
|
|
|
$registrations = array();
|
|
|
|
foreach ($this->service_workers as $scope => $service_worker) {
|
|
|
|
if ($service_worker->has_content()) {
|
|
|
|
$registrations[] = array(
|
|
|
|
'scope' => $scope,
|
|
|
|
'url' => $service_worker->get_url()
|
|
|
|
);
|
|
|
|
}
|
2016-02-11 19:33:59 +03:00
|
|
|
}
|
2016-02-11 22:34:44 +03:00
|
|
|
return json_encode($registrations);
|
2016-02-11 19:33:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-11 22:34:44 +03:00
|
|
|
?>
|