Completed
Push — add/amp-pwa-experiment ( 88c8e7...20183a )
by
unknown
23:11 queued 13:43
created

pwa.php ➔ pwa_render_manifest_link()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 6
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
add_action( 'wp_enqueue_scripts', 'pwa_enqueue_script' );
20
add_action( 'wp_head', 'pwa_render_manifest_link' );
21
add_action( 'admin_head', 'pwa_render_manifest_link' );
22
add_action( 'template_redirect', 'pwa_render_custom_assets', 1 );
23
24
function pwa_activate() {
25
	if ( ! did_action( 'pwa_init' ) ) {
26
		pwa_init();
27
	}
28
	flush_rewrite_rules();
29
}
30
31
function pwa_deactivate() {
32
	flush_rewrite_rules();
33
}
34
35
function pwa_register_query_vars( $vars ) {
36
    $vars[] = PWA_SW_QUERY_VAR;
37
    $vars[] = PWA_MANIFEST_QUERY_VAR;
38
    return $vars;
39
}
40
41
add_action( 'init', 'pwa_init' );
42
function pwa_init() {
43
	if ( false === apply_filters( 'pwa_is_enabled', true ) ) {
44
		return;
45
	}
46
47
	do_action( 'pwa_init' );
48
    add_rewrite_rule('service-worker.js$', 'index.php?' . PWA_SW_QUERY_VAR . '=1', 'top');
49
    wp_register_script( 'jetpack-register-service-worker', plugins_url( 'assets/register-service-worker.js', __FILE__ ), false, '1.5' );
50
}
51
52
function pwa_render_custom_assets() {
53
    global $wp_query;
54
55
    if ( $wp_query->get( PWA_SW_QUERY_VAR ) ) {
56
        header( 'Content-Type: application/javascript; charset=utf-8' );
57
        echo file_get_contents( plugin_dir_path( __FILE__ ) . 'assets/service-worker.js' );
58
        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...
59
    }
60
61
    if ( $wp_query->get( PWA_MANIFEST_QUERY_VAR ) ) {
62
        $theme_color = pwa_get_theme_color();
63
64
        $manifest = array(
65
            'start_url'  => get_bloginfo( 'url' ),
66
            'short_name' => get_bloginfo( 'name' ),
67
            'name'       => get_bloginfo( 'name' ),
68
            'display'    => 'standalone',
69
            'background_color' => $theme_color,
70
            'theme_color' => $theme_color,
71
        );
72
73
        $icon_48 = pwa_site_icon_url( 48 );
74
75
        if ( $icon_48 ) {
76
            $manifest[ 'icons' ] = array(
77
                array(
78
                    'src' => $icon_48,
79
                    'sizes' => '48x48' 
80
                ),
81
                array(
82
                    'src' => pwa_site_icon_url( 192 ),
83
                    'sizes' => '192x192' 
84
                ),
85
                array(
86
                    'src' => pwa_site_icon_url( 512 ),
87
                    'sizes' => '512x512' 
88
                )
89
            );
90
        }
91
92
        wp_send_json( $manifest );
93
    }
94
}
95
96
function pwa_site_icon_url( $size ) {
97
    $url = get_site_icon_url( $size );
98
99
    if ( ! $url ) {
100
        if ( ! function_exists( 'jetpack_site_icon_url' ) ) {
101
            require_once( JETPACK__PLUGIN_DIR . 'modules/site-icon/site-icon-functions.php' );
102
        }
103
        $url = jetpack_site_icon_url( null, $size );
104
    }
105
106
    return $url;
107
}
108
109
function pwa_enqueue_script() {
110
    wp_enqueue_script( 'jetpack-register-service-worker' );
111
}
112
113
function pwa_get_theme_color() {
114
     // if we have AMP enabled, use those colors?
115
    if ( class_exists( 'AMP_Customizer_Settings' ) ) {
116
        $amp_settings = apply_filters( 'amp_post_template_customizer_settings', AMP_Customizer_Settings::get_settings(), null );
117
        $theme_color = $amp_settings['header_background_color'];
118
    } elseif ( current_theme_supports( 'custom-background' ) ) {
119
        $theme_color = get_theme_support( 'custom-background' )->{'default-color'};
120
    } else {
121
        $theme_color = '#FFF';
122
    }
123
124
    return apply_filters( 'jetpack_pwa_background_color', $theme_color );
125
}
126
127
function pwa_render_manifest_link() {
128
    ?>
129
        <link rel="manifest" href="/index.php?<?php echo PWA_MANIFEST_QUERY_VAR; ?>=1">
130
        <meta name="theme-color" content="<?php echo pwa_get_theme_color(); ?>">
131
    <?php
132
}