Completed
Push — add/amp-pwa-experiment ( 77dd49...8ada21 )
by
unknown
10:44
created

pwa.php ➔ pwa_render_amp_serviceworker_element()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 5
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
define( 'PWA_SW_QUERY_VAR', 'jetpack_service_worker' );
15
define( 'PWA_MANIFEST_QUERY_VAR', 'jetpack_app_manifest' );
16
add_action( 'jetpack_activate_module_pwa', 'pwa_activate' );
17
add_action( 'jetpack_deactivate_module_pwa', 'pwa_deactivate' );
18
add_filter( 'query_vars', 'pwa_register_query_vars' );
19
20
// manifest
21
add_action( 'wp_head', 'pwa_render_manifest_link' );
22
add_action( 'amp_post_template_head', 'pwa_render_manifest_link' );
23
add_action( 'admin_head', 'pwa_render_manifest_link' );
24
25
// service worker
26
add_action( 'template_redirect', 'pwa_force_https', 1 );
27
add_action( 'wp_enqueue_scripts', 'pwa_enqueue_script' );
28
add_action( 'template_redirect', 'pwa_render_custom_assets', 2 );
29
add_action( 'amp_post_template_head', 'pwa_render_amp_serviceworker_script' );
30
add_action( 'amp_post_template_footer', 'pwa_render_amp_serviceworker_element' );
31
32
function pwa_activate() {
33
	if ( ! did_action( 'pwa_init' ) ) {
34
		pwa_init();
35
	}
36
	flush_rewrite_rules();
37
}
38
39
function pwa_deactivate() {
40
	flush_rewrite_rules();
41
}
42
43
function pwa_register_query_vars( $vars ) {
44
    $vars[] = PWA_SW_QUERY_VAR;
45
    $vars[] = PWA_MANIFEST_QUERY_VAR;
46
    return $vars;
47
}
48
49
add_action( 'init', 'pwa_init' );
50
function pwa_init() {
51
	if ( false === apply_filters( 'pwa_is_enabled', true ) ) {
52
		return;
53
	}
54
55
	do_action( 'pwa_init' );
56
    add_rewrite_rule('service-worker.js$', 'index.php?' . PWA_SW_QUERY_VAR . '=1', 'top');
57
    wp_register_script( 'jetpack-register-service-worker', plugins_url( 'assets/register-service-worker.js', __FILE__ ), false, '1.5' );
58
}
59
60
function pwa_render_custom_assets() {
61
    global $wp_query;
62
63
    if ( $wp_query->get( PWA_SW_QUERY_VAR ) ) {
64
        header( 'Content-Type: application/javascript; charset=utf-8' );
65
        echo file_get_contents( plugin_dir_path( __FILE__ ) . 'assets/service-worker.js' );
66
        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...
67
    }
68
69
    if ( $wp_query->get( PWA_MANIFEST_QUERY_VAR ) ) {
70
        $theme_color = pwa_get_theme_color();
71
72
        $manifest = array(
73
            'start_url'  => get_bloginfo( 'url' ),
74
            'short_name' => get_bloginfo( 'name' ),
75
            'name'       => get_bloginfo( 'name' ),
76
            'display'    => 'standalone',
77
            'background_color' => $theme_color,
78
            'theme_color' => $theme_color,
79
        );
80
81
        $icon_48 = pwa_site_icon_url( 48 );
82
83
        if ( $icon_48 ) {
84
            $manifest[ 'icons' ] = array(
85
                array(
86
                    'src' => $icon_48,
87
                    'sizes' => '48x48' 
88
                ),
89
                array(
90
                    'src' => pwa_site_icon_url( 192 ),
91
                    'sizes' => '192x192' 
92
                ),
93
                array(
94
                    'src' => pwa_site_icon_url( 512 ),
95
                    'sizes' => '512x512' 
96
                )
97
            );
98
        }
99
100
        wp_send_json( $manifest );
101
    }
102
}
103
104
function pwa_site_icon_url( $size ) {
105
    $url = get_site_icon_url( $size );
106
107
    if ( ! $url ) {
108
        if ( ! function_exists( 'jetpack_site_icon_url' ) ) {
109
            require_once( JETPACK__PLUGIN_DIR . 'modules/site-icon/site-icon-functions.php' );
110
        }
111
        $url = jetpack_site_icon_url( null, $size );
112
    }
113
114
    return $url;
115
}
116
117
function pwa_enqueue_script() {
118
    wp_enqueue_script( 'jetpack-register-service-worker' );
119
}
120
121
function pwa_get_theme_color() {
122
     // if we have AMP enabled, use those colors?
123
    if ( class_exists( 'AMP_Customizer_Settings' ) ) {
124
        $amp_settings = apply_filters( 'amp_post_template_customizer_settings', AMP_Customizer_Settings::get_settings(), null );
125
        $theme_color = $amp_settings['header_background_color'];
126
    } elseif ( current_theme_supports( 'custom-background' ) ) {
127
        $theme_color = get_theme_support( 'custom-background' )->{'default-color'};
128
    } else {
129
        $theme_color = '#FFF';
130
    }
131
132
    return apply_filters( 'jetpack_pwa_background_color', $theme_color );
133
}
134
135
function pwa_render_manifest_link() {
136
    ?>
137
        <link rel="manifest" href="/index.php?<?php echo PWA_MANIFEST_QUERY_VAR; ?>=1">
138
        <meta name="theme-color" content="<?php echo pwa_get_theme_color(); ?>">
139
    <?php
140
}
141
142
function pwa_render_amp_serviceworker_script() {
143
    ?>
144
        <script async custom-element="amp-install-serviceworker" src="https://cdn.ampproject.org/v0/amp-install-serviceworker-0.1.js"></script>
145
    <?php
146
}
147
148
function pwa_render_amp_serviceworker_element() {
149
    ?>
150
        <amp-install-serviceworker src="/service-worker.js" layout="nodisplay"></amp-install-serviceworker>
151
    <?php
152
}
153
154
function pwa_force_https () {
155
	if ( !is_ssl() ) {
156
		wp_redirect('https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], 301 );
157
		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...
158
	}
159
}
160