1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Plugin Name: PWA |
4
|
|
|
* Description: Add Progressive Web App support to your WordPress site. |
5
|
|
|
* Plugin URI: https://github.com/automattic/jetpack |
6
|
|
|
* Author: Automattic |
7
|
|
|
* Author URI: https://automattic.com |
8
|
|
|
* Version: 0.4.2 |
9
|
|
|
* Text Domain: pwa |
10
|
|
|
* Domain Path: /languages/ |
11
|
|
|
* License: GPLv2 or later |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
define( 'PWA_SW_QUERY_VAR', 'jetpack_service_worker' ); |
15
|
|
|
add_action( 'jetpack_activate_module_pwa', 'pwa_activate' ); |
16
|
|
|
add_action( 'jetpack_deactivate_module_pwa', 'pwa_deactivate' ); |
17
|
|
|
add_action( 'wp', 'pwa_render_service_worker' ); |
18
|
|
|
add_filter( 'query_vars', 'pwa_register_query_vars' ); |
19
|
|
|
add_action( 'wp_enqueue_scripts', 'pwa_enqueue_script' ); |
20
|
|
|
|
21
|
|
|
function pwa_activate() { |
22
|
|
|
if ( ! did_action( 'pwa_init' ) ) { |
23
|
|
|
pwa_init(); |
24
|
|
|
} |
25
|
|
|
flush_rewrite_rules(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
function pwa_deactivate() { |
29
|
|
|
flush_rewrite_rules(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
function pwa_register_query_vars( $vars ) { |
33
|
|
|
$vars[] = PWA_SW_QUERY_VAR; |
34
|
|
|
return $vars; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
add_action( 'init', 'pwa_init' ); |
38
|
|
|
function pwa_init() { |
39
|
|
|
if ( false === apply_filters( 'pwa_is_enabled', true ) ) { |
40
|
|
|
return; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
do_action( 'pwa_init' ); |
44
|
|
|
add_rewrite_rule('service-worker.js$', 'index.php?' . PWA_SW_QUERY_VAR . '=1', 'top'); |
45
|
|
|
wp_register_script( 'jetpack-register-service-worker', plugins_url( 'assets/register-service-worker.js', __FILE__ ), false, '1.5' ); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
function pwa_render_service_worker() { |
49
|
|
|
global $wp; |
50
|
|
|
if ( isset( $wp->query_vars[ PWA_SW_QUERY_VAR ] ) ) { |
51
|
|
|
header( 'Content-Type: application/javascript; charset=utf-8' ); |
52
|
|
|
echo file_get_contents( plugin_dir_path( __FILE__ ) . 'assets/service-worker.js' ); |
53
|
|
|
exit; |
|
|
|
|
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
function pwa_enqueue_script() { |
58
|
|
|
wp_enqueue_script( 'jetpack-register-service-worker' ); |
59
|
|
|
} |
An exit expression should only be used in rare cases. For example, if you write a short command line script.
In most cases however, using an
exit
expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.