Super Progressive Web Apps - Version 2.0.1

Version Description

  • Date: 15.January.2019
  • Enhancement: Added compatibility for setups where dynamic files are not supported.
Download this release

Release Info

Developer arunbasillal
Plugin Icon 128x128 Super Progressive Web Apps
Version 2.0.1
Comparing to
See all releases

Code changes from version 2.0 to 2.0.1

.gitignore ADDED
@@ -0,0 +1 @@
 
1
+ vendor
3rd-party/onesignal.php CHANGED
@@ -6,6 +6,7 @@
6
  *
7
  * @since 1.6
8
  *
 
9
  * @function superpwa_onesignal_add_gcm_sender_id() Add gcm_sender_id to SuperPWA manifest
10
  * @function superpwa_onesignal_sw_filename() Change Service Worker filename to OneSignalSDKWorker.js.php
11
  * @function superpwa_onesignal_sw() Import OneSignal service worker in SuperPWA
@@ -17,26 +18,40 @@
17
  // Exit if accessed directly
18
  if ( ! defined('ABSPATH') ) exit;
19
 
20
- // If OneSignal is installed and active
21
- if ( class_exists( 'OneSignal' ) ) {
22
-
23
- // Filter manifest and service worker for singe websites and not for multisites.
24
- if ( ! is_multisite() ) {
 
 
 
 
 
 
25
 
26
- // Add gcm_sender_id to SuperPWA manifest
27
- add_filter( 'superpwa_manifest', 'superpwa_onesignal_add_gcm_sender_id' );
 
 
 
28
 
29
- // Change service worker filename to match OneSignal's service worker
30
- add_filter( 'superpwa_sw_filename', 'superpwa_onesignal_sw_filename' );
 
 
 
 
 
 
 
31
 
32
- // Import OneSignal service worker in SuperPWA
33
- add_filter( 'superpwa_sw_template', 'superpwa_onesignal_sw' );
 
34
  }
35
-
36
- // Show admin notice.
37
- add_action( 'admin_notices', 'superpwa_onesignal_admin_notices', 9 );
38
- add_action( 'network_admin_notices', 'superpwa_onesignal_admin_notices', 9 );
39
  }
 
40
 
41
  /**
42
  * Add gcm_sender_id to SuperPWA manifest
@@ -86,10 +101,21 @@ function superpwa_onesignal_sw_filename( $sw_filename ) {
86
  *
87
  * @since 1.8
88
  * @since 2.0 Removed content-type header for compatibility with dynamic service workers.
 
89
  */
90
  function superpwa_onesignal_sw( $sw ) {
91
 
92
- $onesignal = 'importScripts( \'' . superpwa_httpsify( plugin_dir_url( 'onesignal-free-web-push-notifications/onesignal.php' ) ) . 'sdk_files/OneSignalSDKWorker.js.php\' );' . PHP_EOL;
 
 
 
 
 
 
 
 
 
 
93
 
94
  return $onesignal . $sw;
95
  }
@@ -101,6 +127,8 @@ function superpwa_onesignal_sw( $sw ) {
101
  * Delete current service worker.
102
  * Regenerate SuperPWA service worker with the new filename.
103
  *
 
 
104
  * @since 1.8
105
  * @since 1.8.1 Excluded multisites. No OneSignal compatibility on multisites yet. In 1.8 onesignal.php was not loaded for multisites.
106
  */
@@ -114,11 +142,20 @@ function superpwa_onesignal_activation() {
114
  // Filter in gcm_sender_id to SuperPWA manifest
115
  add_filter( 'superpwa_manifest', 'superpwa_onesignal_add_gcm_sender_id' );
116
 
 
 
 
 
 
 
117
  // Change service worker filename to match OneSignal's service worker
118
  add_filter( 'superpwa_sw_filename', 'superpwa_onesignal_sw_filename' );
119
 
120
  // Import OneSignal service worker in SuperPWA
121
  add_filter( 'superpwa_sw_template', 'superpwa_onesignal_sw' );
 
 
 
122
  }
123
  add_action( 'activate_onesignal-free-web-push-notifications/onesignal.php', 'superpwa_onesignal_activation', 11 );
124
 
@@ -129,6 +166,8 @@ add_action( 'activate_onesignal-free-web-push-notifications/onesignal.php', 'sup
129
  * Delete current service worker.
130
  * Regenerate SuperPWA service worker.
131
  *
 
 
132
  * @since 1.8
133
  * @since 1.8.1 Excluded multisites. No OneSignal compatibility on multisites yet. In 1.8 onesignal.php was not loaded for multisites.
134
  */
@@ -142,11 +181,20 @@ function superpwa_onesignal_deactivation() {
142
  // Remove gcm_sender_id from SuperPWA manifest
143
  remove_filter( 'superpwa_manifest', 'superpwa_onesignal_add_gcm_sender_id' );
144
 
 
 
 
 
 
 
145
  // Restore the default service worker of SuperPWA
146
  remove_filter( 'superpwa_sw_filename', 'superpwa_onesignal_sw_filename' );
147
 
148
  // Remove OneSignal service worker in SuperPWA
149
  remove_filter( 'superpwa_sw_template', 'superpwa_onesignal_sw' );
 
 
 
150
  }
151
  add_action( 'deactivate_onesignal-free-web-push-notifications/onesignal.php', 'superpwa_onesignal_deactivation', 11 );
152
 
6
  *
7
  * @since 1.6
8
  *
9
+ * @function superpwa_onesignal_todo() Compatibility with OneSignal
10
  * @function superpwa_onesignal_add_gcm_sender_id() Add gcm_sender_id to SuperPWA manifest
11
  * @function superpwa_onesignal_sw_filename() Change Service Worker filename to OneSignalSDKWorker.js.php
12
  * @function superpwa_onesignal_sw() Import OneSignal service worker in SuperPWA
18
  // Exit if accessed directly
19
  if ( ! defined('ABSPATH') ) exit;
20
 
21
+ /**
22
+ * Compatibility with OneSignal
23
+ *
24
+ * This was written without a function @since 1.6 but that caused issues in certain cases where
25
+ * SuperPWA was loaded before OneSignal. Hooked everything to plugins_loaded @since 2.0.1
26
+ *
27
+ * @author Arun Basil Lal
28
+ *
29
+ * @since 2.0.1
30
+ */
31
+ function superpwa_onesignal_todo() {
32
 
33
+ // If OneSignal is installed and active
34
+ if ( class_exists( 'OneSignal' ) ) {
35
+
36
+ // Filter manifest and service worker for singe websites and not for multisites.
37
+ if ( ! is_multisite() ) {
38
 
39
+ // Add gcm_sender_id to SuperPWA manifest
40
+ add_filter( 'superpwa_manifest', 'superpwa_onesignal_add_gcm_sender_id' );
41
+
42
+ // Change service worker filename to match OneSignal's service worker
43
+ add_filter( 'superpwa_sw_filename', 'superpwa_onesignal_sw_filename' );
44
+
45
+ // Import OneSignal service worker in SuperPWA
46
+ add_filter( 'superpwa_sw_template', 'superpwa_onesignal_sw' );
47
+ }
48
 
49
+ // Show admin notice.
50
+ add_action( 'admin_notices', 'superpwa_onesignal_admin_notices', 9 );
51
+ add_action( 'network_admin_notices', 'superpwa_onesignal_admin_notices', 9 );
52
  }
 
 
 
 
53
  }
54
+ add_action( 'plugins_loaded', 'superpwa_onesignal_todo' );
55
 
56
  /**
57
  * Add gcm_sender_id to SuperPWA manifest
101
  *
102
  * @since 1.8
103
  * @since 2.0 Removed content-type header for compatibility with dynamic service workers.
104
+ * @since 2.0.1 Added back compatibility with static service workers by sending content-type header.
105
  */
106
  function superpwa_onesignal_sw( $sw ) {
107
 
108
+ if ( ! empty ( preg_grep( '#Content-Type: text/javascript#i', headers_list() ) ) ) {
109
+
110
+ $onesignal = 'importScripts( \'' . superpwa_httpsify( plugin_dir_url( 'onesignal-free-web-push-notifications/onesignal.php' ) ) . 'sdk_files/OneSignalSDKWorker.js.php\' );' . PHP_EOL;
111
+
112
+ return $onesignal . $sw;
113
+ }
114
+
115
+ $onesignal = '<?php' . PHP_EOL;
116
+ $onesignal .= 'header( "Content-Type: application/javascript" );' . PHP_EOL;
117
+ $onesignal .= 'echo "importScripts( \'' . superpwa_httpsify( plugin_dir_url( 'onesignal-free-web-push-notifications/onesignal.php' ) ) . 'sdk_files/OneSignalSDKWorker.js.php\' );";' . PHP_EOL;
118
+ $onesignal .= '?>' . PHP_EOL . PHP_EOL;
119
 
120
  return $onesignal . $sw;
121
  }
127
  * Delete current service worker.
128
  * Regenerate SuperPWA service worker with the new filename.
129
  *
130
+ * @author Arun Basil Lal
131
+ *
132
  * @since 1.8
133
  * @since 1.8.1 Excluded multisites. No OneSignal compatibility on multisites yet. In 1.8 onesignal.php was not loaded for multisites.
134
  */
142
  // Filter in gcm_sender_id to SuperPWA manifest
143
  add_filter( 'superpwa_manifest', 'superpwa_onesignal_add_gcm_sender_id' );
144
 
145
+ // Regenerate SuperPWA manifest
146
+ superpwa_generate_manifest();
147
+
148
+ // Delete service worker if it exists
149
+ superpwa_delete_sw();
150
+
151
  // Change service worker filename to match OneSignal's service worker
152
  add_filter( 'superpwa_sw_filename', 'superpwa_onesignal_sw_filename' );
153
 
154
  // Import OneSignal service worker in SuperPWA
155
  add_filter( 'superpwa_sw_template', 'superpwa_onesignal_sw' );
156
+
157
+ // Regenerate SuperPWA service worker
158
+ superpwa_generate_sw();
159
  }
160
  add_action( 'activate_onesignal-free-web-push-notifications/onesignal.php', 'superpwa_onesignal_activation', 11 );
161
 
166
  * Delete current service worker.
167
  * Regenerate SuperPWA service worker.
168
  *
169
+ * @author Arun Basil Lal
170
+ *
171
  * @since 1.8
172
  * @since 1.8.1 Excluded multisites. No OneSignal compatibility on multisites yet. In 1.8 onesignal.php was not loaded for multisites.
173
  */
181
  // Remove gcm_sender_id from SuperPWA manifest
182
  remove_filter( 'superpwa_manifest', 'superpwa_onesignal_add_gcm_sender_id' );
183
 
184
+ // Regenerate SuperPWA manifest
185
+ superpwa_generate_manifest();
186
+
187
+ // Delete service worker if it exists
188
+ superpwa_delete_sw();
189
+
190
  // Restore the default service worker of SuperPWA
191
  remove_filter( 'superpwa_sw_filename', 'superpwa_onesignal_sw_filename' );
192
 
193
  // Remove OneSignal service worker in SuperPWA
194
  remove_filter( 'superpwa_sw_template', 'superpwa_onesignal_sw' );
195
+
196
+ // Regenerate SuperPWA service worker
197
+ superpwa_generate_sw();
198
  }
199
  add_action( 'deactivate_onesignal-free-web-push-notifications/onesignal.php', 'superpwa_onesignal_deactivation', 11 );
200
 
admin/admin-ui-render-settings.php CHANGED
@@ -260,7 +260,7 @@ function superpwa_offline_page_cb() {
260
  </label>
261
 
262
  <p class="description">
263
- <?php printf( __( 'Offline page is displayed when the device is offline and the requested page is not already cached. Current offline page is <code>%s</code>', 'super-progressive-web-apps' ), get_permalink($settings['offline_page']) ? get_permalink( $settings['offline_page'] ) : get_bloginfo( 'url' ) ); ?>
264
  </p>
265
 
266
  <?php
@@ -343,15 +343,19 @@ function superpwa_display_cb() {
343
  * @since 1.2
344
  * @since 1.8 Attempt to generate manifest again if the manifest doesn't exist.
345
  * @since 2.0 Remove logic to check if manifest exists in favour of dynamic manifest.
 
346
  */
347
  function superpwa_manifest_status_cb() {
348
 
349
- // Dynamic files need a custom permalink structure.
350
- if ( get_option( 'permalink_structure' ) !== '' ) {
351
- // Since Manifest is dynamically generated, it should always be present.
352
- printf( '<p><span class="dashicons dashicons-yes" style="color: #46b450;"></span> ' . __( 'Manifest generated successfully. You can <a href="%s" target="_blank">see it here &rarr;</a>', 'super-progressive-web-apps' ) . '</p>', superpwa_manifest( 'src' ) );
 
 
353
  } else {
354
- printf( '<p><span class="dashicons dashicons-no-alt" style="color: #dc3232;"></span> ' . __( 'SuperPWA requires a custom permalink structure. Go to <a href="%s" target="_blank">WordPress Settings > Permalinks</a> and choose anything other than "Plain".', 'super-progressive-web-apps' ) . '</p>', admin_url( 'options-permalink.php' ) );
 
355
  }
356
  }
357
 
@@ -359,20 +363,23 @@ function superpwa_manifest_status_cb() {
359
  * Service Worker Status
360
  *
361
  * @author Arun Basil Lal
362
- * @author Maria Daniel Deepak <daniel@danieldeepak.com>
363
  *
364
- * @since 1.2
365
- * @since 1.8 Attempt to generate service worker again if it doesn't exist.
366
- * @since 2.0 Modify logic to check if Service worker exists.
 
367
  */
368
  function superpwa_sw_status_cb() {
369
 
370
- // Dynamic files need a custom permalink structure.
371
- if ( get_option( 'permalink_structure' ) !== '' ) {
372
- // Since Service worker is dynamically generated, it should always be present.
373
- printf( '<p><span class="dashicons dashicons-yes" style="color: #46b450;"></span> ' . __( 'Service worker generated successfully. <a href="%s" target="_blank">see it here &rarr;</a>', 'super-progressive-web-apps' ) . '</p>', superpwa_sw( 'src' ) );
 
 
374
  } else {
375
- printf( '<p><span class="dashicons dashicons-no-alt" style="color: #dc3232;"></span> ' . __( 'SuperPWA requires a custom permalink structure. Go to <a href="%s" target="_blank">WordPress Settings > Permalinks</a> and choose anything other than "Plain".', 'super-progressive-web-apps' ) . '</p>', admin_url( 'options-permalink.php' ) );
 
376
  }
377
  }
378
 
260
  </label>
261
 
262
  <p class="description">
263
+ <?php printf( __( 'Offline page is displayed when the device is offline and the requested page is not already cached. Current offline page is <code>%s</code>', 'super-progressive-web-apps' ), superpwa_get_offline_page() ); ?>
264
  </p>
265
 
266
  <?php
343
  * @since 1.2
344
  * @since 1.8 Attempt to generate manifest again if the manifest doesn't exist.
345
  * @since 2.0 Remove logic to check if manifest exists in favour of dynamic manifest.
346
+ * @since 2.0.1 Added checks to see if dynamic file is valid. If not, generates a physical file.
347
  */
348
  function superpwa_manifest_status_cb() {
349
 
350
+ /**
351
+ * Check to see if the file exists, If not attempts to generate a new one.
352
+ */
353
+ if ( superpwa_file_exists( superpwa_manifest( 'src' ) ) || superpwa_generate_manifest() ) {
354
+
355
+ printf( '<p><span class="dashicons dashicons-yes" style="color: #46b450;"></span> ' . __( 'Manifest generated successfully. You can <a href="%s" target="_blank">See it here &rarr;</a>', 'super-progressive-web-apps' ) . '</p>', superpwa_manifest( 'src' ) );
356
  } else {
357
+
358
+ printf( '<p><span class="dashicons dashicons-no-alt" style="color: #dc3232;"></span> ' . __( 'Manifest generation failed. <a href="%s" target="_blank">Fix it &rarr;</a>', 'super-progressive-web-apps' ) . '</p>', 'https://superpwa.com/doc/fixing-manifest-service-worker-generation-failed-error/?utm_source=superpwa-plugin&utm_medium=settings-status-no-manifest' );
359
  }
360
  }
361
 
363
  * Service Worker Status
364
  *
365
  * @author Arun Basil Lal
 
366
  *
367
+ * @since 1.2
368
+ * @since 1.8 Attempt to generate service worker again if it doesn't exist.
369
+ * @since 2.0 Modify logic to check if Service worker exists.
370
+ * @since 2.0.1 Added checks to see if dynamic file is valid. If not, generates a physical file.
371
  */
372
  function superpwa_sw_status_cb() {
373
 
374
+ /**
375
+ * Check to see if the file exists, If not attempts to generate a new one.
376
+ */
377
+ if ( superpwa_file_exists( superpwa_sw( 'src' ) ) || superpwa_generate_sw() ) {
378
+
379
+ printf( '<p><span class="dashicons dashicons-yes" style="color: #46b450;"></span> ' . __( 'Service worker generated successfully. <a href="%s" target="_blank">See it here &rarr;</a>', 'super-progressive-web-apps' ) . '</p>', superpwa_sw( 'src' ) );
380
  } else {
381
+
382
+ printf( '<p><span class="dashicons dashicons-no-alt" style="color: #dc3232;"></span> ' . __( 'Service worker generation failed. <a href="%s" target="_blank">Fix it &rarr;</a>', 'super-progressive-web-apps' ) . '</p>', 'https://superpwa.com/doc/fixing-manifest-service-worker-generation-failed-error/?utm_source=superpwa-plugin&utm_medium=settings-status-no-sw' );
383
  }
384
  }
385
 
admin/admin-ui-setup.php CHANGED
@@ -196,9 +196,15 @@ add_action( 'admin_init', 'superpwa_register_settings' );
196
  /**
197
  * Validate and sanitize user input before its saved to database
198
  *
 
 
 
 
199
  * @since 1.0
200
  * @since 1.3 Added splash_icon
201
  * @since 1.6 Added description
 
 
202
  */
203
  function superpwa_validater_and_sanitizer( $settings ) {
204
 
@@ -223,6 +229,23 @@ function superpwa_validater_and_sanitizer( $settings ) {
223
  // Sanitize splash screen icon
224
  $settings['splash_icon'] = sanitize_text_field( superpwa_httpsify( $settings['splash_icon'] ) );
225
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
226
  return $settings;
227
  }
228
 
@@ -234,6 +257,9 @@ function superpwa_validater_and_sanitizer( $settings ) {
234
  * @author Arun Basil Lal
235
  *
236
  * @since 1.0
 
 
 
237
  */
238
  function superpwa_get_settings() {
239
 
@@ -250,6 +276,8 @@ function superpwa_get_settings() {
250
  'offline_page' => 0,
251
  'orientation' => 1,
252
  'display' => 1,
 
 
253
  );
254
 
255
  $settings = get_option( 'superpwa_settings', $defaults );
@@ -257,6 +285,27 @@ function superpwa_get_settings() {
257
  return $settings;
258
  }
259
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
260
  /**
261
  * Enqueue CSS and JS
262
  *
196
  /**
197
  * Validate and sanitize user input before its saved to database
198
  *
199
+ * @author Arun Basil Lal
200
+ *
201
+ * @param (array) $settings Values passed from the Settings API from SuperPWA > Settings
202
+ *
203
  * @since 1.0
204
  * @since 1.3 Added splash_icon
205
  * @since 1.6 Added description
206
+ * @since 2.0 Limit app_short_name to 12 characters
207
+ * @since 2.0.1 Added is_static_sw and is_static_manifest
208
  */
209
  function superpwa_validater_and_sanitizer( $settings ) {
210
 
229
  // Sanitize splash screen icon
230
  $settings['splash_icon'] = sanitize_text_field( superpwa_httpsify( $settings['splash_icon'] ) );
231
 
232
+ /**
233
+ * Get current settings already saved in the database.
234
+ *
235
+ * When the SuperPWA > Settings page is saved, the form does not have the values for
236
+ * is_static_sw or is_static_manifest. So this is added here to match the already saved
237
+ * values in the database.
238
+ */
239
+ $current_settings = superpwa_get_settings();
240
+
241
+ if ( ! isset( $settings['is_static_sw'] ) ) {
242
+ $settings['is_static_sw'] = $current_settings['is_static_sw'];
243
+ }
244
+
245
+ if ( ! isset( $settings['is_static_manifest'] ) ) {
246
+ $settings['is_static_manifest'] = $current_settings['is_static_manifest'];
247
+ }
248
+
249
  return $settings;
250
  }
251
 
257
  * @author Arun Basil Lal
258
  *
259
  * @since 1.0
260
+ * @since 2.0 Added display
261
+ * @since 2.0.1 Added is_static_manifest. 1 for static files, 0 for dynamic files.
262
+ * @since 2.0.1 Added is_static_sw. 1 for static files, 0 for dynamic files.
263
  */
264
  function superpwa_get_settings() {
265
 
276
  'offline_page' => 0,
277
  'orientation' => 1,
278
  'display' => 1,
279
+ 'is_static_manifest'=> 0,
280
+ 'is_static_sw' => 0,
281
  );
282
 
283
  $settings = get_option( 'superpwa_settings', $defaults );
285
  return $settings;
286
  }
287
 
288
+ /**
289
+ * Todo list after saving admin options
290
+ *
291
+ * Regenerate manifest
292
+ * Regenerate service worker
293
+ *
294
+ * @author Arun Basil Lal
295
+ *
296
+ * @since 1.0
297
+ */
298
+ function superpwa_after_save_settings_todo() {
299
+
300
+ // Regenerate manifest
301
+ superpwa_generate_manifest();
302
+
303
+ // Regenerate service worker
304
+ superpwa_generate_sw();
305
+ }
306
+ add_action( 'add_option_superpwa_settings', 'superpwa_after_save_settings_todo' );
307
+ add_action( 'update_option_superpwa_settings', 'superpwa_after_save_settings_todo' );
308
+
309
  /**
310
  * Enqueue CSS and JS
311
  *
admin/basic-setup.php CHANGED
@@ -26,6 +26,9 @@ if ( ! defined('ABSPATH') ) exit;
26
  * For the rest of the sites, superpwa_upgrader() handles generation of manifest and service worker.
27
  *
28
  * @param $network_active (Boolean) True if the plugin is network activated, false otherwise.
 
 
 
29
  * @link https://www.alexgeorgiou.gr/network-activated-wordpress-plugins/ (Thanks Alex!)
30
  *
31
  * @since 1.0
@@ -33,6 +36,7 @@ if ( ! defined('ABSPATH') ) exit;
33
  * @since 1.6 Added checks for multisite compatibility.
34
  */
35
  function superpwa_activate_plugin( $network_active ) {
 
36
  // Not network active i.e. plugin is activated on a single install (normal WordPress install) or a single site on a multisite network
37
  if ( ! $network_active ) {
38
 
@@ -177,7 +181,14 @@ function superpwa_upgrader() {
177
  */
178
  if ( $current_ver === false ) {
179
 
 
 
 
 
 
 
180
  if ( is_multisite() ) {
 
181
  // For multisites, save the activation status of current blog.
182
  superpwa_multisite_activation_status( true );
183
  }
@@ -227,9 +238,8 @@ function superpwa_upgrader() {
227
  // Restore the default service worker filename of SuperPWA.
228
  remove_filter( 'superpwa_sw_filename', 'superpwa_onesignal_sw_filename' );
229
 
230
- // TODO: Commenting the following line for now. Delete it later.
231
- // Delete service worker if it exists.
232
- // superpwa_delete_sw();
233
 
234
  // Change service worker filename to match OneSignal's service worker.
235
  add_filter( 'superpwa_sw_filename', 'superpwa_onesignal_sw_filename' );
@@ -242,7 +252,7 @@ function superpwa_upgrader() {
242
  * Until 2.0, there was no UI for display.
243
  * In the manifest, display was hard coded as 'standalone'.
244
  *
245
- * Starting with 2.0, manifest and servcei worker files are dynamic and no longer static.
246
  *
247
  * @since 2.0
248
  */
@@ -256,13 +266,31 @@ function superpwa_upgrader() {
256
 
257
  // Write settings back to database
258
  update_option( 'superpwa_settings', $settings );
 
 
 
 
 
 
 
 
259
 
260
- // Delete manifest
261
- superpwa_delete_manifest();
262
 
263
- // Delete service worker
264
- superpwa_delete_sw();
 
 
 
 
265
  }
 
 
 
 
 
 
266
 
267
  // Add current version to database
268
  update_option( 'superpwa_version', SUPERPWA_VERSION );
@@ -288,13 +316,12 @@ add_action( 'admin_init', 'superpwa_upgrader' );
288
  * @since 1.6 register_deactivation_hook() moved to this file (basic-setup.php) from main plugin file (superpwa.php)
289
  */
290
  function superpwa_deactivate_plugin( $network_active ) {
291
- // TODO: Commenting the following line for now. Delete it later.
292
  // Delete manifest
293
- // superpwa_delete_manifest();
294
 
295
- // TODO: Commenting the following line for now. Delete it later.
296
  // Delete service worker
297
- // superpwa_delete_sw();
298
 
299
  // For multisites, save the de-activation status of current blog.
300
  superpwa_multisite_activation_status( false );
@@ -396,7 +423,7 @@ function superpwa_generate_sw_and_manifest_on_fly( $query ) {
396
  exit();
397
  }
398
  if ( strpos( $query_vars_as_string, $sw_filename ) !== false ) {
399
- header( 'Content-type: text/javascript' );
400
  echo superpwa_sw_template();
401
  exit();
402
  }
@@ -416,5 +443,4 @@ function superpwa_setup_hooks() {
416
  add_action( 'init', 'superpwa_add_rewrite_rules' );
417
  add_action( 'parse_request', 'superpwa_generate_sw_and_manifest_on_fly' );
418
  }
419
-
420
  add_action( 'plugins_loaded', 'superpwa_setup_hooks' );
26
  * For the rest of the sites, superpwa_upgrader() handles generation of manifest and service worker.
27
  *
28
  * @param $network_active (Boolean) True if the plugin is network activated, false otherwise.
29
+ *
30
+ * @author Arun Basil Lal
31
+ *
32
  * @link https://www.alexgeorgiou.gr/network-activated-wordpress-plugins/ (Thanks Alex!)
33
  *
34
  * @since 1.0
36
  * @since 1.6 Added checks for multisite compatibility.
37
  */
38
  function superpwa_activate_plugin( $network_active ) {
39
+
40
  // Not network active i.e. plugin is activated on a single install (normal WordPress install) or a single site on a multisite network
41
  if ( ! $network_active ) {
42
 
181
  */
182
  if ( $current_ver === false ) {
183
 
184
+ // Generate manifest
185
+ superpwa_generate_manifest();
186
+
187
+ // Generate service worker
188
+ superpwa_generate_sw();
189
+
190
  if ( is_multisite() ) {
191
+
192
  // For multisites, save the activation status of current blog.
193
  superpwa_multisite_activation_status( true );
194
  }
238
  // Restore the default service worker filename of SuperPWA.
239
  remove_filter( 'superpwa_sw_filename', 'superpwa_onesignal_sw_filename' );
240
 
241
+ // Delete service worker
242
+ superpwa_delete_sw();
 
243
 
244
  // Change service worker filename to match OneSignal's service worker.
245
  add_filter( 'superpwa_sw_filename', 'superpwa_onesignal_sw_filename' );
252
  * Until 2.0, there was no UI for display.
253
  * In the manifest, display was hard coded as 'standalone'.
254
  *
255
+ * Starting with 2.0, manifest and service worker files are dynamic and no longer static.
256
  *
257
  * @since 2.0
258
  */
266
 
267
  // Write settings back to database
268
  update_option( 'superpwa_settings', $settings );
269
+ }
270
+
271
+ /**
272
+ * Add file state variables to database
273
+ *
274
+ * @since 2.0.1
275
+ */
276
+ if ( version_compare( $current_ver, '2.0', '<=' ) ) {
277
 
278
+ // Get settings
279
+ $settings = superpwa_get_settings();
280
 
281
+ // 1 for static files, 0 for dynamic files (default).
282
+ $settings['is_static_manifest'] = 0;
283
+ $settings['is_static_sw'] = 0;
284
+
285
+ // Write settings back to database
286
+ update_option( 'superpwa_settings', $settings );
287
  }
288
+
289
+ // Re-generate manifest
290
+ superpwa_generate_manifest();
291
+
292
+ // Re-generate service worker
293
+ superpwa_generate_sw();
294
 
295
  // Add current version to database
296
  update_option( 'superpwa_version', SUPERPWA_VERSION );
316
  * @since 1.6 register_deactivation_hook() moved to this file (basic-setup.php) from main plugin file (superpwa.php)
317
  */
318
  function superpwa_deactivate_plugin( $network_active ) {
319
+
320
  // Delete manifest
321
+ superpwa_delete_manifest();
322
 
 
323
  // Delete service worker
324
+ superpwa_delete_sw();
325
 
326
  // For multisites, save the de-activation status of current blog.
327
  superpwa_multisite_activation_status( false );
423
  exit();
424
  }
425
  if ( strpos( $query_vars_as_string, $sw_filename ) !== false ) {
426
+ header( 'Content-Type: text/javascript' );
427
  echo superpwa_sw_template();
428
  exit();
429
  }
443
  add_action( 'init', 'superpwa_add_rewrite_rules' );
444
  add_action( 'parse_request', 'superpwa_generate_sw_and_manifest_on_fly' );
445
  }
 
446
  add_action( 'plugins_loaded', 'superpwa_setup_hooks' );
functions/common.php CHANGED
@@ -8,6 +8,9 @@
8
  * @function superpwa_get_start_url() Return Start Page URL
9
  * @function superpwa_httpsify() Convert http URL to https
10
  * @function superpwa_is_pwa_ready() Check if PWA is ready
 
 
 
11
  */
12
 
13
  // Exit if accessed directly
@@ -83,7 +86,7 @@ function superpwa_get_start_url( $rel = false ) {
83
  $settings = superpwa_get_settings();
84
 
85
  // Start Page
86
- $start_url = get_permalink( $settings['start_url'] ) ? get_permalink( $settings['start_url'] ) : get_bloginfo( 'url' );
87
 
88
  // Force HTTPS
89
  $start_url = superpwa_httpsify( $start_url );
@@ -127,17 +130,114 @@ function superpwa_httpsify( $url ) {
127
  *
128
  * @return (bool) True if PWA is ready. False otherwise
129
  *
 
 
130
  * @since 1.8.1
 
131
  */
132
  function superpwa_is_pwa_ready() {
133
 
134
  if (
135
  is_ssl() &&
136
- superpwa_get_contents( superpwa_manifest( 'abs' ) ) &&
137
- superpwa_get_contents( superpwa_sw( 'abs' ) )
138
  ) {
139
  return apply_filters( 'superpwa_is_pwa_ready', true );
140
  }
141
 
142
  return false;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
143
  }
8
  * @function superpwa_get_start_url() Return Start Page URL
9
  * @function superpwa_httpsify() Convert http URL to https
10
  * @function superpwa_is_pwa_ready() Check if PWA is ready
11
+ * @function superpwa_file_exists() Check if file exists
12
+ * @function superpwa_is_static() Check if service worker or manifest is static or dynamic
13
+ * @function superpwa_get_bloginfo() Returns WordPress URL v/s Site URL depending on the status of the file.
14
  */
15
 
16
  // Exit if accessed directly
86
  $settings = superpwa_get_settings();
87
 
88
  // Start Page
89
+ $start_url = get_permalink( $settings['start_url'] ) ? get_permalink( $settings['start_url'] ) : superpwa_get_bloginfo( 'sw' );
90
 
91
  // Force HTTPS
92
  $start_url = superpwa_httpsify( $start_url );
130
  *
131
  * @return (bool) True if PWA is ready. False otherwise
132
  *
133
+ * @author Arun Basil Lal
134
+ *
135
  * @since 1.8.1
136
+ * @since 2.0.1 replaced superpwa_get_contents() with superpwa_file_exists() to accommodate dynamic files.
137
  */
138
  function superpwa_is_pwa_ready() {
139
 
140
  if (
141
  is_ssl() &&
142
+ superpwa_file_exists( superpwa_manifest( 'src' ) ) &&
143
+ superpwa_file_exists( superpwa_sw( 'src' ) )
144
  ) {
145
  return apply_filters( 'superpwa_is_pwa_ready', true );
146
  }
147
 
148
  return false;
149
+ }
150
+
151
+ /**
152
+ * Check if file exists
153
+ *
154
+ * Not to be confused with file_exists PHP function.
155
+ * In SuperPWA context, file exists if the response code is 200.
156
+ *
157
+ * @param $file (string) URL to check
158
+ *
159
+ * @return (bool) True, if file exists. False otherwise.
160
+ *
161
+ * @author Arun Basil Lal
162
+ * @author Maria Daniel Deepak <daniel@danieldeepak.com>
163
+ *
164
+ * @since 2.0.1
165
+ */
166
+ function superpwa_file_exists( $file ) {
167
+
168
+ $response = wp_remote_head( $file, array( 'sslverify' => false ) );
169
+ $response_code = wp_remote_retrieve_response_code( $response );
170
+
171
+ if ( 200 === $response_code ) {
172
+ return true;
173
+ }
174
+
175
+ return false;
176
+ }
177
+
178
+ /**
179
+ * Check if service worker or manifest is static or dynamic
180
+ *
181
+ * @param (string) $file keyword 'manifest' to test manifest and 'sw' to test service worker.
182
+ *
183
+ * @return (bool) True if the file is static. False otherwise.
184
+ *
185
+ * @author Arun Basil Lal
186
+ *
187
+ * @since 2.0.1
188
+ */
189
+ function superpwa_is_static( $file = 'manifest' ) {
190
+
191
+ // Get Settings
192
+ $settings = superpwa_get_settings();
193
+
194
+ switch ( $file ) {
195
+
196
+ case 'sw':
197
+
198
+ if ( $settings['is_static_sw'] === 1 ) {
199
+ return true;
200
+ }
201
+
202
+ return false;
203
+ break;
204
+
205
+ case 'manifest':
206
+ default:
207
+
208
+ if ( $settings['is_static_manifest'] === 1 ) {
209
+ return true;
210
+ }
211
+
212
+ return false;
213
+ break;
214
+ }
215
+ }
216
+
217
+ /**
218
+ * Returns WordPress URL v/s Site URL depending on the status of the file.
219
+ *
220
+ * Static files are generated in the root directory of WordPress. So if static
221
+ * files are used, the WordPress URL will be needed for many use cases, like
222
+ * offline page, start_url etc.
223
+ *
224
+ * The status of the service worker is mostly relevant since the service worker
225
+ * can work on the folder it is located and its sub folders. Not the folders above
226
+ * its own directory.
227
+ *
228
+ * @param (string) $file keyword 'manifest' to test manifest and 'sw' to test service worker.
229
+ *
230
+ * @return (string) get_bloginfo( 'wpurl' ) if file is static. get_bloginfo( 'url' ) otherwise.
231
+ *
232
+ * @author Arun Basil Lal
233
+ *
234
+ * @since 2.0.1
235
+ */
236
+ function superpwa_get_bloginfo( $file = 'sw' ) {
237
+
238
+ if ( superpwa_is_static( $file ) ) {
239
+ return get_bloginfo( 'wpurl' );
240
+ }
241
+
242
+ return get_bloginfo( 'url' );
243
  }
functions/multisite.php CHANGED
@@ -83,13 +83,11 @@ function superpwa_multisite_network_deactivator() {
83
  // Switch to each blog
84
  switch_to_blog( $blog_id );
85
 
86
- // TODO: Commenting the following line for now. Delete it later.
87
  // Delete manifest
88
- // superpwa_delete_manifest();
89
 
90
- // TODO: Commenting the following line for now. Delete it later.
91
  // Delete service worker
92
- // superpwa_delete_sw();
93
 
94
  /**
95
  * Delete SuperPWA version info for current blog.
83
  // Switch to each blog
84
  switch_to_blog( $blog_id );
85
 
 
86
  // Delete manifest
87
+ superpwa_delete_manifest();
88
 
 
89
  // Delete service worker
90
+ superpwa_delete_sw();
91
 
92
  /**
93
  * Delete SuperPWA version info for current blog.
languages/super-progressive-web-apps.pot CHANGED
@@ -1,15 +1,15 @@
1
- # Copyright (C) 2018 SuperPWA
2
  # This file is distributed under the same license as the Super Progressive Web Apps plugin.
3
  msgid ""
4
  msgstr ""
5
- "Project-Id-Version: Super Progressive Web Apps 2.0\n"
6
  "Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/super-progressive-web-apps\n"
7
  "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
8
  "Language-Team: LANGUAGE <LL@li.org>\n"
9
  "MIME-Version: 1.0\n"
10
  "Content-Type: text/plain; charset=UTF-8\n"
11
  "Content-Transfer-Encoding: 8bit\n"
12
- "POT-Creation-Date: 2018-12-27T20:31:33+00:00\n"
13
  "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
14
  "X-Generator: WP-CLI 2.1.0\n"
15
  "X-Domain: super-progressive-web-apps\n"
@@ -39,11 +39,11 @@ msgstr ""
39
  msgid "https://superpwa.com/?utm_source=superpwa-plugin&utm_medium=author-uri"
40
  msgstr ""
41
 
42
- #: 3rd-party/onesignal.php:172
43
  msgid "<strong>SuperPWA</strong> is not compatible with OneSignal on multisites yet. Disable one of these plugins until the compatibility is available.<br>Please refer to the <a href=\"%s\" target=\"_blank\">OneSignal integration documentation</a> for more info. "
44
  msgstr ""
45
 
46
- #: 3rd-party/onesignal.php:194
47
  msgid "<strong>Action Required to integrate SuperPWA with OneSignal:</strong><br>1. Go to <a href=\"%s\" target=\"_blank\">OneSignal Configuration > Scroll down to Advanced Settings &rarr;</a><br>2. Enable <strong>Use my own manifest.json</strong><br>3. Set <code>%s</code>as <strong>Custom manifest.json URL</strong> and Save Settings.<br>Please refer the <a href=\"%s\" target=\"_blank\">OneSignal integration documentation</a> for more info. "
48
  msgstr ""
49
 
@@ -85,7 +85,7 @@ msgid "Campaign Source is mandatory and defaults to <code>superpwa</code>. The r
85
  msgstr ""
86
 
87
  #: addons/utm-tracking.php:362
88
- #: admin/admin-ui-render-settings.php:412
89
  msgid "Settings saved."
90
  msgstr ""
91
 
@@ -94,7 +94,7 @@ msgid "UTM Tracking for"
94
  msgstr ""
95
 
96
  #: addons/utm-tracking.php:382
97
- #: admin/admin-ui-render-settings.php:435
98
  msgid "Save Settings"
99
  msgstr ""
100
 
@@ -272,29 +272,32 @@ msgstr ""
272
  msgid "Display mode decides what browser UI is shown when your app is launched. <code>Standalone</code> is default. <a href=\"%s\" target=\"_blank\">What's the difference? &rarr;</a>"
273
  msgstr ""
274
 
275
- #: admin/admin-ui-render-settings.php:352
276
- msgid "Manifest generated successfully. You can <a href=\"%s\" target=\"_blank\">see it here &rarr;</a>"
277
  msgstr ""
278
 
279
- #: admin/admin-ui-render-settings.php:354
280
- #: admin/admin-ui-render-settings.php:375
281
- msgid "SuperPWA requires a custom permalink structure. Go to <a href=\"%s\" target=\"_blank\">WordPress Settings > Permalinks</a> and choose anything other than \"Plain\"."
282
  msgstr ""
283
 
284
- #: admin/admin-ui-render-settings.php:373
285
- msgid "Service worker generated successfully. <a href=\"%s\" target=\"_blank\">see it here &rarr;</a>"
286
  msgstr ""
287
 
288
- #: admin/admin-ui-render-settings.php:388
 
 
 
 
289
  msgid "Your website is served over HTTPS."
290
  msgstr ""
291
 
292
- #: admin/admin-ui-render-settings.php:391
293
  msgid "Progressive Web Apps require that your website is served over HTTPS. Please contact your host to add a SSL certificate to your domain."
294
  msgstr ""
295
 
296
  #: admin/admin-ui-setup.php:31
297
- #: admin/basic-setup.php:328
298
  msgid "Settings"
299
  msgstr ""
300
 
@@ -362,28 +365,28 @@ msgstr ""
362
  msgid "HTTPS"
363
  msgstr ""
364
 
365
- #: admin/admin-ui-setup.php:299
366
  msgid "If you like SuperPWA, please <a href=\"%s\" target=\"_blank\">make a donation</a> or leave a <a href=\"%s\" target=\"_blank\">&#9733;&#9733;&#9733;&#9733;&#9733;</a> rating to support continued development. Thanks a bunch!"
367
  msgstr ""
368
 
369
- #: admin/basic-setup.php:99
370
  msgid "Your app is ready with the default settings. "
371
  msgstr ""
372
 
373
- #: admin/basic-setup.php:103
374
  msgid "<a href=\"%s\">Customize your app &rarr;</a>"
375
  msgstr ""
376
 
377
- #: admin/basic-setup.php:105
378
- #: admin/basic-setup.php:139
379
  msgid "Thank you for installing <strong>Super Progressive Web Apps!</strong> "
380
  msgstr ""
381
 
382
- #: admin/basic-setup.php:114
383
- #: admin/basic-setup.php:148
384
  msgid "<strong>SuperPWA</strong>: Successfully updated to version %s. Thank you! <a href=\"%s\" target=\"_blank\">Discover new features and read the story &rarr;</a>"
385
  msgstr ""
386
 
387
- #: admin/basic-setup.php:344
388
  msgid "Demo"
389
  msgstr ""
1
+ # Copyright (C) 2019 SuperPWA
2
  # This file is distributed under the same license as the Super Progressive Web Apps plugin.
3
  msgid ""
4
  msgstr ""
5
+ "Project-Id-Version: Super Progressive Web Apps 2.0.1\n"
6
  "Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/super-progressive-web-apps\n"
7
  "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
8
  "Language-Team: LANGUAGE <LL@li.org>\n"
9
  "MIME-Version: 1.0\n"
10
  "Content-Type: text/plain; charset=UTF-8\n"
11
  "Content-Transfer-Encoding: 8bit\n"
12
+ "POT-Creation-Date: 2019-01-15T05:23:48+00:00\n"
13
  "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
14
  "X-Generator: WP-CLI 2.1.0\n"
15
  "X-Domain: super-progressive-web-apps\n"
39
  msgid "https://superpwa.com/?utm_source=superpwa-plugin&utm_medium=author-uri"
40
  msgstr ""
41
 
42
+ #: 3rd-party/onesignal.php:220
43
  msgid "<strong>SuperPWA</strong> is not compatible with OneSignal on multisites yet. Disable one of these plugins until the compatibility is available.<br>Please refer to the <a href=\"%s\" target=\"_blank\">OneSignal integration documentation</a> for more info. "
44
  msgstr ""
45
 
46
+ #: 3rd-party/onesignal.php:242
47
  msgid "<strong>Action Required to integrate SuperPWA with OneSignal:</strong><br>1. Go to <a href=\"%s\" target=\"_blank\">OneSignal Configuration > Scroll down to Advanced Settings &rarr;</a><br>2. Enable <strong>Use my own manifest.json</strong><br>3. Set <code>%s</code>as <strong>Custom manifest.json URL</strong> and Save Settings.<br>Please refer the <a href=\"%s\" target=\"_blank\">OneSignal integration documentation</a> for more info. "
48
  msgstr ""
49
 
85
  msgstr ""
86
 
87
  #: addons/utm-tracking.php:362
88
+ #: admin/admin-ui-render-settings.php:419
89
  msgid "Settings saved."
90
  msgstr ""
91
 
94
  msgstr ""
95
 
96
  #: addons/utm-tracking.php:382
97
+ #: admin/admin-ui-render-settings.php:442
98
  msgid "Save Settings"
99
  msgstr ""
100
 
272
  msgid "Display mode decides what browser UI is shown when your app is launched. <code>Standalone</code> is default. <a href=\"%s\" target=\"_blank\">What's the difference? &rarr;</a>"
273
  msgstr ""
274
 
275
+ #: admin/admin-ui-render-settings.php:355
276
+ msgid "Manifest generated successfully. You can <a href=\"%s\" target=\"_blank\">See it here &rarr;</a>"
277
  msgstr ""
278
 
279
+ #: admin/admin-ui-render-settings.php:358
280
+ msgid "Manifest generation failed. <a href=\"%s\" target=\"_blank\">Fix it &rarr;</a>"
 
281
  msgstr ""
282
 
283
+ #: admin/admin-ui-render-settings.php:379
284
+ msgid "Service worker generated successfully. <a href=\"%s\" target=\"_blank\">See it here &rarr;</a>"
285
  msgstr ""
286
 
287
+ #: admin/admin-ui-render-settings.php:382
288
+ msgid "Service worker generation failed. <a href=\"%s\" target=\"_blank\">Fix it &rarr;</a>"
289
+ msgstr ""
290
+
291
+ #: admin/admin-ui-render-settings.php:395
292
  msgid "Your website is served over HTTPS."
293
  msgstr ""
294
 
295
+ #: admin/admin-ui-render-settings.php:398
296
  msgid "Progressive Web Apps require that your website is served over HTTPS. Please contact your host to add a SSL certificate to your domain."
297
  msgstr ""
298
 
299
  #: admin/admin-ui-setup.php:31
300
+ #: admin/basic-setup.php:337
301
  msgid "Settings"
302
  msgstr ""
303
 
365
  msgid "HTTPS"
366
  msgstr ""
367
 
368
+ #: admin/admin-ui-setup.php:325
369
  msgid "If you like SuperPWA, please <a href=\"%s\" target=\"_blank\">make a donation</a> or leave a <a href=\"%s\" target=\"_blank\">&#9733;&#9733;&#9733;&#9733;&#9733;</a> rating to support continued development. Thanks a bunch!"
370
  msgstr ""
371
 
372
+ #: admin/basic-setup.php:103
373
  msgid "Your app is ready with the default settings. "
374
  msgstr ""
375
 
376
+ #: admin/basic-setup.php:107
377
  msgid "<a href=\"%s\">Customize your app &rarr;</a>"
378
  msgstr ""
379
 
380
+ #: admin/basic-setup.php:109
381
+ #: admin/basic-setup.php:143
382
  msgid "Thank you for installing <strong>Super Progressive Web Apps!</strong> "
383
  msgstr ""
384
 
385
+ #: admin/basic-setup.php:118
386
+ #: admin/basic-setup.php:152
387
  msgid "<strong>SuperPWA</strong>: Successfully updated to version %s. Thank you! <a href=\"%s\" target=\"_blank\">Discover new features and read the story &rarr;</a>"
388
  msgstr ""
389
 
390
+ #: admin/basic-setup.php:353
391
  msgid "Demo"
392
  msgstr ""
public/manifest.php CHANGED
@@ -60,9 +60,12 @@ function superpwa_manifest( $arg = 'src' ) {
60
  /**
61
  * Absolute path to manifest.
62
  *
63
- * @since 2.0 manifest is no longer a physical file and absolute path doesn't make sense.
64
  * Also using home_url instead of network_site_url in "src" in 2.0 changes the apparent location of the file.
65
  * However, absolute path is preserved at the "old" location, so that phyiscal files can be deleted when upgrading from pre-2.0 versions.
 
 
 
66
  */
67
  case 'abs':
68
  return trailingslashit( ABSPATH ) . $manifest_filename;
@@ -71,7 +74,24 @@ function superpwa_manifest( $arg = 'src' ) {
71
  // Link to manifest
72
  case 'src':
73
  default:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  return home_url( '/' ) . $manifest_filename;
 
75
  break;
76
  }
77
  }
@@ -120,27 +140,58 @@ function superpwa_manifest_template() {
120
 
121
  /**
122
  * Generate and write manifest into WordPress root folder
 
 
 
 
123
  *
124
- * @return (boolean) true on success, false on failure.
125
- *
126
- * @since 1.0
127
- * @since 1.3 Added support for 512x512 icon.
128
- * @since 1.4 Added orientation and scope.
129
- * @since 1.5 Added gcm_sender_id
130
- * @since 1.6 Added description
131
- * @since 1.8 Removed gcm_sender_id and introduced filter superpwa_manifest. gcm_sender_id is added in
132
- * /3rd-party/onesignal.php
133
- * @since 2.0 Deprecated since Manifest is generated on the fly
134
- * {@see superpwa_generate_sw_and_manifest_on_fly()}.
135
- *
136
- * @author Arun Basil Lal
137
- * @author Maria Daniel Deepak <daniel@danieldeepak.com>
138
  *
139
- * @deprecated 2.0 No longer used by internal code.
 
 
 
 
 
 
 
140
  */
141
  function superpwa_generate_manifest() {
142
- // Returns TRUE for backward compatibility.
143
- return true;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
144
  }
145
 
146
  /**
@@ -174,11 +225,11 @@ add_action( 'wp_head', 'superpwa_add_manifest_to_wp_head', 0 );
174
  /**
175
  * Delete manifest
176
  *
177
- * @return (boolean) true on success, false on failure
178
- *
179
- * @since 1.0
180
  *
181
- * @deprecated 2.0 No longer used by internal code.
182
  */
183
  function superpwa_delete_manifest() {
184
  return superpwa_delete( superpwa_manifest( 'abs' ) );
@@ -222,7 +273,7 @@ function superpwa_get_pwa_icons() {
222
  * @since 1.4
223
  */
224
  function superpwa_get_scope() {
225
- return parse_url( trailingslashit( get_bloginfo( 'url' ) ), PHP_URL_PATH );
226
  }
227
 
228
  /**
60
  /**
61
  * Absolute path to manifest.
62
  *
63
+ * Note: @since 2.0 manifest is no longer a physical file and absolute path doesn't make sense.
64
  * Also using home_url instead of network_site_url in "src" in 2.0 changes the apparent location of the file.
65
  * However, absolute path is preserved at the "old" location, so that phyiscal files can be deleted when upgrading from pre-2.0 versions.
66
+ *
67
+ * Since static files are being used in conditions where dynamic files are not possible, this path
68
+ * pointing to the root folder of WordPress is still useful.
69
  */
70
  case 'abs':
71
  return trailingslashit( ABSPATH ) . $manifest_filename;
74
  // Link to manifest
75
  case 'src':
76
  default:
77
+
78
+ // Get Settings
79
+ $settings = superpwa_get_settings();
80
+
81
+ /**
82
+ * For static file, return site_url and network_site_url
83
+ *
84
+ * Static files are generated in the root directory.
85
+ * The site_url template tag retrieves the site url for the
86
+ * current site (where the WordPress core files reside).
87
+ */
88
+ if ( $settings['is_static_manifest'] === 1 ) {
89
+ return trailingslashit( network_site_url() ) . $manifest_filename;
90
+ }
91
+
92
+ // For dynamic files, return the home_url
93
  return home_url( '/' ) . $manifest_filename;
94
+
95
  break;
96
  }
97
  }
140
 
141
  /**
142
  * Generate and write manifest into WordPress root folder
143
+ *
144
+ * Starting with 2.0, files are only generated if dynamic files are not possible.
145
+ * Some webserver configurations does not load WordPress and attempts to server files directly
146
+ * from the server. This returns 404 when files do not exist physically.
147
  *
148
+ * @return (boolean) true on success, false on failure.
149
+ *
150
+ * @author Arun Basil Lal
151
+ * @author Maria Daniel Deepak <daniel@danieldeepak.com>
 
 
 
 
 
 
 
 
 
 
152
  *
153
+ * @since 1.0
154
+ * @since 1.3 Added support for 512x512 icon.
155
+ * @since 1.4 Added orientation and scope.
156
+ * @since 1.5 Added gcm_sender_id
157
+ * @since 1.6 Added description
158
+ * @since 1.8 Removed gcm_sender_id and introduced filter superpwa_manifest. gcm_sender_id is added in /3rd-party/onesignal.php
159
+ * @since 2.0 Deprecated since Manifest is generated on the fly {@see superpwa_generate_sw_and_manifest_on_fly()}.
160
+ * @since 2.0.1 No longer deprecated since physical files are now generated in certain cases. See funtion description.
161
  */
162
  function superpwa_generate_manifest() {
163
+
164
+ // Delete manifest if it exists.
165
+ superpwa_delete_manifest();
166
+
167
+ // Get Settings
168
+ $settings = superpwa_get_settings();
169
+
170
+ // Return true if dynamic file returns a 200 response.
171
+ if ( superpwa_file_exists( home_url( '/' ) . superpwa_get_manifest_filename() ) && defined( 'WP_CACHE' ) && ! WP_CACHE ) {
172
+
173
+ // set file status as dynamic file in database.
174
+ $settings['is_static_manifest'] = 0;
175
+
176
+ // Write settings back to database.
177
+ update_option( 'superpwa_settings', $settings );
178
+
179
+ return true;
180
+ }
181
+
182
+ // Write the manfiest to disk.
183
+ if ( superpwa_put_contents( superpwa_manifest( 'abs' ), json_encode( superpwa_manifest_template() ) ) ) {
184
+
185
+ // set file status as satic file in database.
186
+ $settings['is_static_manifest'] = 1;
187
+
188
+ // Write settings back to database.
189
+ update_option( 'superpwa_settings', $settings );
190
+
191
+ return true;
192
+ }
193
+
194
+ return false;
195
  }
196
 
197
  /**
225
  /**
226
  * Delete manifest
227
  *
228
+ * @return (boolean) true on success, false on failure
229
+ *
230
+ * @author Arun Basil Lal
231
  *
232
+ * @since 1.0
233
  */
234
  function superpwa_delete_manifest() {
235
  return superpwa_delete( superpwa_manifest( 'abs' ) );
273
  * @since 1.4
274
  */
275
  function superpwa_get_scope() {
276
+ return parse_url( trailingslashit( superpwa_get_bloginfo( 'sw' ) ), PHP_URL_PATH );
277
  }
278
 
279
  /**
public/sw.php CHANGED
@@ -69,7 +69,24 @@ function superpwa_sw( $arg = 'src' ) {
69
  // Link to service worker
70
  case 'src':
71
  default:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
  return home_url( '/' ) . $sw_filename;
 
73
  break;
74
  }
75
  }
@@ -77,17 +94,51 @@ function superpwa_sw( $arg = 'src' ) {
77
  /**
78
  * Generate and write service worker into superpwa-sw.js
79
  *
80
- * @return (boolean) true on success, false on failure.
 
 
 
 
 
 
81
  *
82
- * @since 1.0
83
- * @since 2.0 Deprecated since Service worker is generated on the fly
84
- * {@see superpwa_generate_sw_and_manifest_on_fly()}.
85
  *
86
- * @deprecated 2.0 No longer used by internal code.
87
  */
88
  function superpwa_generate_sw() {
89
- // Returns TRUE for backward compatibility.
90
- return true;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
  }
92
 
93
  /**
@@ -115,7 +166,7 @@ function superpwa_sw_template() {
115
 
116
  const cacheName = '<?php echo parse_url( get_bloginfo( 'url' ), PHP_URL_HOST ) . '-superpwa-' . SUPERPWA_VERSION; ?>';
117
  const startPage = '<?php echo superpwa_get_start_url(); ?>';
118
- const offlinePage = '<?php echo get_permalink( $settings['offline_page'] ) ? superpwa_httpsify( get_permalink( $settings['offline_page'] ) ) : superpwa_httpsify( get_bloginfo( 'url' ) ); ?>';
119
  const filesToCache = [<?php echo apply_filters( 'superpwa_sw_files_to_cache', 'startPage, offlinePage' ); ?>];
120
  const neverCacheUrls = [<?php echo apply_filters( 'superpwa_sw_never_cache_urls', '/\/wp-admin/,/\/wp-login/,/preview=true/' ); ?>];
121
 
@@ -236,9 +287,9 @@ add_action( 'wp_enqueue_scripts', 'superpwa_register_sw' );
236
  *
237
  * @return true on success, false on failure
238
  *
 
 
239
  * @since 1.0
240
- *
241
- * @deprecated 2.0 No longer used by internal code.
242
  */
243
  function superpwa_delete_sw() {
244
  return superpwa_delete( superpwa_sw( 'abs' ) );
@@ -280,4 +331,21 @@ function superpwa_offline_page_images( $files_to_cache ) {
280
 
281
  return $files_to_cache;
282
  }
283
- add_filter( 'superpwa_sw_files_to_cache', 'superpwa_offline_page_images' );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  // Link to service worker
70
  case 'src':
71
  default:
72
+
73
+ // Get Settings
74
+ $settings = superpwa_get_settings();
75
+
76
+ /**
77
+ * For static file, return site_url and network_site_url
78
+ *
79
+ * Static files are generated in the root directory.
80
+ * The site_url template tag retrieves the site url for the
81
+ * current site (where the WordPress core files reside).
82
+ */
83
+ if ( $settings['is_static_sw'] === 1 ) {
84
+ return trailingslashit( network_site_url() ) . $sw_filename;
85
+ }
86
+
87
+ // For dynamic files, return the home_url
88
  return home_url( '/' ) . $sw_filename;
89
+
90
  break;
91
  }
92
  }
94
  /**
95
  * Generate and write service worker into superpwa-sw.js
96
  *
97
+ * Starting with 2.0, files are only generated if dynamic files are not possible.
98
+ * Some webserver configurations does not load WordPress and attempts to server files directly
99
+ * from the server. This returns 404 when files do not exist physically.
100
+ *
101
+ * @return (boolean) true on success, false on failure.
102
+ *
103
+ * @author Arun Basil Lal
104
  *
105
+ * @since 1.0
106
+ * @since 2.0 Deprecated since Service worker is generated on the fly {@see superpwa_generate_sw_and_manifest_on_fly()}.
107
+ * @since 2.0.1 No longer deprecated since physical files are now generated in certain cases. See funtion description.
108
  *
 
109
  */
110
  function superpwa_generate_sw() {
111
+
112
+ // Delete service worker if it exists
113
+ superpwa_delete_sw();
114
+
115
+ // Get Settings
116
+ $settings = superpwa_get_settings();
117
+
118
+ // Return true if dynamic file returns a 200 response.
119
+ if ( superpwa_file_exists( home_url( '/' ) . superpwa_get_sw_filename() ) && defined( 'WP_CACHE' ) && ! WP_CACHE ) {
120
+
121
+ // set file status as dynamic file in database.
122
+ $settings['is_static_sw'] = 0;
123
+
124
+ // Write settings back to database.
125
+ update_option( 'superpwa_settings', $settings );
126
+
127
+ return true;
128
+ }
129
+
130
+ if ( superpwa_put_contents( superpwa_sw( 'abs' ), superpwa_sw_template() ) ) {
131
+
132
+ // set file status as satic file in database.
133
+ $settings['is_static_sw'] = 1;
134
+
135
+ // Write settings back to database.
136
+ update_option( 'superpwa_settings', $settings );
137
+
138
+ return true;
139
+ }
140
+
141
+ return false;
142
  }
143
 
144
  /**
166
 
167
  const cacheName = '<?php echo parse_url( get_bloginfo( 'url' ), PHP_URL_HOST ) . '-superpwa-' . SUPERPWA_VERSION; ?>';
168
  const startPage = '<?php echo superpwa_get_start_url(); ?>';
169
+ const offlinePage = '<?php echo superpwa_get_offline_page(); ?>';
170
  const filesToCache = [<?php echo apply_filters( 'superpwa_sw_files_to_cache', 'startPage, offlinePage' ); ?>];
171
  const neverCacheUrls = [<?php echo apply_filters( 'superpwa_sw_never_cache_urls', '/\/wp-admin/,/\/wp-login/,/preview=true/' ); ?>];
172
 
287
  *
288
  * @return true on success, false on failure
289
  *
290
+ * @author Arun Basil Lal
291
+ *
292
  * @since 1.0
 
 
293
  */
294
  function superpwa_delete_sw() {
295
  return superpwa_delete( superpwa_sw( 'abs' ) );
331
 
332
  return $files_to_cache;
333
  }
334
+ add_filter( 'superpwa_sw_files_to_cache', 'superpwa_offline_page_images' );
335
+
336
+ /**
337
+ * Get offline page
338
+ *
339
+ * @return (string) the URL of the offline page.
340
+ *
341
+ * @author Arun Basil Lal
342
+ *
343
+ * @since 2.0.1
344
+ */
345
+ function superpwa_get_offline_page() {
346
+
347
+ // Get Settings
348
+ $settings = superpwa_get_settings();
349
+
350
+ return get_permalink( $settings['offline_page'] ) ? superpwa_httpsify( get_permalink( $settings['offline_page'] ) ) : superpwa_httpsify( superpwa_get_bloginfo( 'sw' ) );
351
+ }
readme.txt CHANGED
@@ -3,7 +3,7 @@ Contributors: superpwa, arunbasillal, josevarghese, mariadanieldeepak
3
  Donate link: http://millionclues.com/donate/
4
  Tags: pwa, progressive web apps, manifest, web manifest, android app, chrome app, add to homescreen, mobile web
5
  Requires at least: 3.6.0
6
- Tested up to: 5.0.2
7
  Requires PHP: 5.3
8
  Stable tag: trunk
9
  License: GPLv2 or later
@@ -178,8 +178,12 @@ Feel free to get in touch if you have any questions.
178
 
179
  == Changelog ==
180
 
 
 
 
 
181
  = 2.0 =
182
- * Date: [28.December.2018](https://superpwa.com/superpwa-2-0/)
183
  * Tested with WordPress 5.0.2.
184
  * Enhancement: Dynamic service worker and manifest.
185
  * Enhancement: SuperPWA is now compatible with WordPress in a sub-folder.
@@ -293,6 +297,9 @@ Feel free to get in touch if you have any questions.
293
 
294
  == Upgrade Notice ==
295
 
 
 
 
296
  = 2.0 =
297
  * Tested with WordPress 5.0.2.
298
  * Enhancement: Dynamic service worker and manifest.
3
  Donate link: http://millionclues.com/donate/
4
  Tags: pwa, progressive web apps, manifest, web manifest, android app, chrome app, add to homescreen, mobile web
5
  Requires at least: 3.6.0
6
+ Tested up to: 5.0.3
7
  Requires PHP: 5.3
8
  Stable tag: trunk
9
  License: GPLv2 or later
178
 
179
  == Changelog ==
180
 
181
+ = 2.0.1 =
182
+ * Date: [15.January.2019](https://superpwa.com/superpwa-2-0/?utm_source=wordpress.org&utm_medium=changelog#2.0.1)
183
+ * Enhancement: Added compatibility for setups where dynamic files are not supported.
184
+
185
  = 2.0 =
186
+ * Date: [28.December.2018](https://superpwa.com/superpwa-2-0/?utm_source=wordpress.org&utm_medium=changelog)
187
  * Tested with WordPress 5.0.2.
188
  * Enhancement: Dynamic service worker and manifest.
189
  * Enhancement: SuperPWA is now compatible with WordPress in a sub-folder.
297
 
298
  == Upgrade Notice ==
299
 
300
+ = 2.0.1 =
301
+ * Enhancement: Added compatibility for setups where dynamic files are not supported.
302
+
303
  = 2.0 =
304
  * Tested with WordPress 5.0.2.
305
  * Enhancement: Dynamic service worker and manifest.
superpwa.php CHANGED
@@ -6,7 +6,7 @@
6
  * Author: SuperPWA
7
  * Author URI: https://superpwa.com/?utm_source=superpwa-plugin&utm_medium=author-uri
8
  * Contributors: Arun Basil Lal, Jose Varghese
9
- * Version: 2.0
10
  * Text Domain: super-progressive-web-apps
11
  * Domain Path: /languages
12
  * License: GPL v2 - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
@@ -43,7 +43,7 @@ if ( ! defined('ABSPATH') ) exit;
43
  * @since 1.0
44
  */
45
  if ( ! defined( 'SUPERPWA_VERSION' ) ) {
46
- define( 'SUPERPWA_VERSION' , '2.0' );
47
  }
48
 
49
  /**
6
  * Author: SuperPWA
7
  * Author URI: https://superpwa.com/?utm_source=superpwa-plugin&utm_medium=author-uri
8
  * Contributors: Arun Basil Lal, Jose Varghese
9
+ * Version: 2.0.1
10
  * Text Domain: super-progressive-web-apps
11
  * Domain Path: /languages
12
  * License: GPL v2 - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
43
  * @since 1.0
44
  */
45
  if ( ! defined( 'SUPERPWA_VERSION' ) ) {
46
+ define( 'SUPERPWA_VERSION' , '2.0.1' );
47
  }
48
 
49
  /**