Completed
Push — add/amp-pwa-experiment ( 8ada21...45b0b1 )
by
unknown
10:01
created

pwa.php ➔ pwa_init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 0
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
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
 /**
15
  * Include the following PWA capabilities:
16
  * - cache the home page and posts/pages
17
  * - cache all CSS and JS
18
  * - show offline/online status using body class "jetpack__offline"
19
  * TODO:
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
20
  * - push updates
21
  * - how to cache within wp-admin?
22
  * - hook WP's native cache functions to expire and push updates to sites
23
  */
24
25
define( 'PWA_SW_QUERY_VAR', 'jetpack_service_worker' );
26
define( 'PWA_MANIFEST_QUERY_VAR', 'jetpack_app_manifest' );
27
add_action( 'jetpack_activate_module_pwa', 'pwa_activate' );
28
add_action( 'jetpack_deactivate_module_pwa', 'pwa_deactivate' );
29
add_filter( 'query_vars', 'pwa_register_query_vars' );
30
31
// manifest
32
add_action( 'wp_head', 'pwa_render_manifest_link' );
33
add_action( 'amp_post_template_head', 'pwa_render_manifest_link' );
34
add_action( 'admin_head', 'pwa_render_manifest_link' );
35
36
// service worker
37
add_action( 'template_redirect', 'pwa_force_https', 1 );
38
add_action( 'wp_enqueue_scripts', 'pwa_enqueue_script' );
39
add_action( 'template_redirect', 'pwa_render_custom_assets', 2 );
40
add_action( 'amp_post_template_head', 'pwa_render_amp_serviceworker_script' );
41
add_action( 'amp_post_template_footer', 'pwa_render_amp_serviceworker_element' );
42
43
function pwa_activate() {
44
	if ( ! did_action( 'pwa_init' ) ) {
45
		pwa_init();
46
	}
47
	flush_rewrite_rules();
48
}
49
50
function pwa_deactivate() {
51
	flush_rewrite_rules();
52
}
53
54
function pwa_register_query_vars( $vars ) {
55
    $vars[] = PWA_SW_QUERY_VAR;
56
    $vars[] = PWA_MANIFEST_QUERY_VAR;
57
    return $vars;
58
}
59
60
add_action( 'init', 'pwa_init' );
61
function pwa_init() {
62
	if ( false === apply_filters( 'pwa_is_enabled', true ) ) {
63
		return;
64
	}
65
66
	do_action( 'pwa_init' );
67
    add_rewrite_rule('service-worker.js$', 'index.php?' . PWA_SW_QUERY_VAR . '=1', 'top');
68
    wp_register_script( 'jetpack-register-service-worker', plugins_url( 'assets/js/register-service-worker.js', __FILE__ ), false, '1.5' );
69
    wp_register_script( 'jetpack-show-network-status', plugins_url( 'assets/js/show-network-status.js', __FILE__ ), false, '1.5' );
70
    wp_register_style( 'jetpack-show-network-status', plugins_url( 'assets/css/show-network-status.css', __FILE__ ) );
71
}
72
73
function pwa_render_custom_assets() {
74
    global $wp_query;
75
76
    if ( $wp_query->get( PWA_SW_QUERY_VAR ) ) {
77
        header( 'Content-Type: application/javascript; charset=utf-8' );
78
        echo file_get_contents( plugin_dir_path( __FILE__ ) . 'assets/js/service-worker.js' );
79
        exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The function pwa_render_custom_assets() contains an exit expression.

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.

Loading history...
80
    }
81
82
    if ( $wp_query->get( PWA_MANIFEST_QUERY_VAR ) ) {
83
        $theme_color = pwa_get_theme_color();
84
85
        $manifest = array(
86
            'start_url'  => get_bloginfo( 'url' ),
87
            'short_name' => get_bloginfo( 'name' ),
88
            'name'       => get_bloginfo( 'name' ),
89
            'display'    => 'standalone',
90
            'background_color' => $theme_color,
91
            'theme_color' => $theme_color,
92
        );
93
94
        $icon_48 = pwa_site_icon_url( 48 );
95
96
        if ( $icon_48 ) {
97
            $manifest[ 'icons' ] = array(
98
                array(
99
                    'src' => $icon_48,
100
                    'sizes' => '48x48' 
101
                ),
102
                array(
103
                    'src' => pwa_site_icon_url( 192 ),
104
                    'sizes' => '192x192' 
105
                ),
106
                array(
107
                    'src' => pwa_site_icon_url( 512 ),
108
                    'sizes' => '512x512' 
109
                )
110
            );
111
        }
112
113
        wp_send_json( $manifest );
114
    }
115
}
116
117
function pwa_site_icon_url( $size ) {
118
    $url = get_site_icon_url( $size );
119
120
    if ( ! $url ) {
121
        if ( ! function_exists( 'jetpack_site_icon_url' ) ) {
122
            require_once( JETPACK__PLUGIN_DIR . 'modules/site-icon/site-icon-functions.php' );
123
        }
124
        $url = jetpack_site_icon_url( null, $size );
125
    }
126
127
    return $url;
128
}
129
130
function pwa_enqueue_script() {
131
    wp_enqueue_script( 'jetpack-register-service-worker' );
132
    wp_enqueue_script( 'jetpack-show-network-status' );
133
    wp_enqueue_style( 'jetpack-show-network-status' );
134
}
135
136
function pwa_get_theme_color() {
137
     // if we have AMP enabled, use those colors?
138
    if ( class_exists( 'AMP_Customizer_Settings' ) ) {
139
        $amp_settings = apply_filters( 'amp_post_template_customizer_settings', AMP_Customizer_Settings::get_settings(), null );
140
        $theme_color = $amp_settings['header_background_color'];
141
    } elseif ( current_theme_supports( 'custom-background' ) ) {
142
        $theme_color = get_theme_support( 'custom-background' )->{'default-color'};
143
    } else {
144
        $theme_color = '#FFF';
145
    }
146
147
    return apply_filters( 'jetpack_pwa_background_color', $theme_color );
148
}
149
150
function pwa_render_manifest_link() {
151
    ?>
152
        <link rel="manifest" href="/index.php?<?php echo PWA_MANIFEST_QUERY_VAR; ?>=1">
153
        <meta name="theme-color" content="<?php echo pwa_get_theme_color(); ?>">
154
    <?php
155
}
156
157
function pwa_render_amp_serviceworker_script() {
158
    ?>
159
        <script async custom-element="amp-install-serviceworker" src="https://cdn.ampproject.org/v0/amp-install-serviceworker-0.1.js"></script>
160
    <?php
161
}
162
163
function pwa_render_amp_serviceworker_element() {
164
    ?>
165
        <amp-install-serviceworker src="/service-worker.js" layout="nodisplay"></amp-install-serviceworker>
166
    <?php
167
}
168
169
function pwa_force_https () {
170
	if ( !is_ssl() ) {
171
		wp_redirect('https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], 301 );
172
		exit();
0 ignored issues
show
Coding Style Compatibility introduced by
The function pwa_force_https() contains an exit expression.

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.

Loading history...
173
	}
174
}
175