-
Notifications
You must be signed in to change notification settings - Fork 14
/
class-serviceworker.php
148 lines (122 loc) · 4.51 KB
/
class-serviceworker.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
namespace nicomartin\ProgressiveWordPress;
class Serviceworker {
public $capability = '';
public $sw_path = '';
public $sw_url = '';
public function __construct() {
$this->capability = pwp_get_instance()->Init->capability;
$this->sw_path = pwp_get_instance()->upload_dir . 'pwp-serviceworker.js';
$this->sw_url = pwp_get_instance()->upload_url . 'pwp-serviceworker.js';
if ( is_multisite() ) {
$this->sw_path = pwp_get_instance()->upload_dir . 'pwp-serviceworker-' . get_current_blog_id() . '.js';
$this->sw_url = pwp_get_instance()->upload_url . 'pwp-serviceworker-' . get_current_blog_id() . '.js';
}
}
public function run() {
add_action( 'admin_notices', [ $this, 'ssl_error_notice' ] );
add_action( 'pwp_after_save', [ $this, 'regenerate' ] );
add_action( 'pwp_on_update', [ $this, 'regenerate' ] );
add_action( 'pwp_on_deactivate', [ $this, 'delete_serviceworker' ] );
if ( file_exists( $this->sw_path ) ) {
add_action( 'plugins_loaded', [ $this, 'register_service_worker' ] );
}
}
/**
* Register service worker by using wp_register_service_worker();
*/
public function register_service_worker() {
if ( function_exists( 'wp_register_service_worker' ) ) {
/**
* Test: added a script where the dependency is registered afterwards
* Worls! the conts comes after the dependency
*/
wp_register_service_worker( 'progressive-wp-sw-test-deps', function () {
return "const pwpshouldbe = 'after progressive-wp-sw';";
}, [ 'progressive-wp-sw' ] );
/**
* Test: add a different scope
* Works!
*/
wp_register_service_worker( 'progressive-wp-sw-test-scope', function () {
return "const pwpanother = 'scope';";
}, [], [ '/mysite/' ] );
/**
* Progressive WP Serviceworker
*/
wp_register_service_worker( 'progressive-wp-sw', $this->sw_url );
}
}
public function ssl_error_notice() {
if ( is_ssl() ) {
return;
}
$screen = get_current_screen();
if ( PWP_SETTINGS_PARENT != $screen->parent_base ) {
return;
}
echo '<div class="notice notice-error">';
echo '<p>' . __( 'Your site has to be served over https to use progressive web app features.', 'pwp' ) . '</p>';
echo '</div>';
}
/**
* Helpers
*/
public function regenerate() {
$content_header = "'use strict';\n\n";
$content_header .= "/**\n";
$content_header .= " * generated by Progressive WordPress:\n";
$content_header .= " * https://wordpress.org/plugins/progressive-wordpress/\n";
$content_header .= " * by Nico Martin - https://nicomartin.ch\n";
$content_header .= "**/\n";
$content_header .= 'const version = \'{{time}}\';';
$content = apply_filters( 'pwp_sw_content', '' );
$path = plugin_dir_path( pwp_get_instance()->file ) . 'Classes/Libs';
require_once $path . '/minify/autoload.php';
require_once $path . '/path-converter/autoload.php';
$minifier = new \MatthiasMullie\Minify\JS( $content );
$content = $minifier->minify();
$content = $content_header . $content;
/**
* check if not the same:
* this provides a way that the version will only be updated if there are changes in the sw-file.
*/
$current_content = '';
if ( is_file( $this->sw_path ) ) {
$current_content = file_get_contents( $this->sw_path );
}
$new_content_check = str_replace( '{{time}}', get_option( 'pwp_sw_time' ), $content );
if ( $current_content == $new_content_check ) {
return;
}
/**
* set new content
*/
$time = time();
$content = str_replace( '{{time}}', $time, $content );
// Wrap content into IIFE.
$content = "( function() {\n" . $content . "} )();\n";
pwp_delete( $this->sw_path );
$save = pwp_put_contents( $this->sw_path, $content );
if ( ! $save ) {
add_action( 'admin_notices', function () {
echo '<div class="notice notice-error">';
// translators: There was a problem generating your serviceworker file. Please check your permissions for ABSPATH
echo '<p>' . sprintf( __( 'There was a problem generating your serviceworker file. Please check your permissions for %s', 'pwp' ), '<code>' . ABSPATH . '</code>' ) . '</p>';
echo '</div>';
} );
return;
} else {
add_action( 'admin_notices', function () {
echo '<div class="notice notice-success">';
// translators: There was a problem generating your serviceworker file. Please check your permissions for ABSPATH
echo '<p>' . __( 'ServiceWorker regenerated', 'pwp' ) . '</p>';
echo '</div>';
} );
}
update_option( 'pwp_sw_time', $time );
}
public function delete_serviceworker() {
pwp_delete( $this->sw_path );
}
}