-
Notifications
You must be signed in to change notification settings - Fork 0
/
namespace.php
493 lines (425 loc) · 15.2 KB
/
namespace.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
<?php
/**
* Figuren_Theater Performance PWA.
*
* @package figuren-theater/ft-performance
*/
namespace Figuren_Theater\Performance\PWA;
use Figuren_Theater;
use Figuren_Theater\Options;
use Figuren_Theater\Performance;
use Figuren_Theater\Theming\Themed_Login;
use FT_VENDOR_DIR;
use function add_action;
use function add_filter;
use function esc_url;
use function get_option;
use function get_permalink;
use function get_post;
use function get_term;
use function get_term_link;
use function get_the_excerpt;
use function get_the_title;
use function home_url;
use WPMU_PLUGIN_URL;
use WP_DEBUG;
use WP_Post;
use WP_Term;
use WP_Web_App_Manifest;
const BASENAME = 'pwa/pwa.php';
const PLUGINPATH = '/wpackagist-plugin/' . BASENAME;
/**
* Bootstrap module, when enabled.
*
* @return void
*/
function bootstrap() :void {
add_action( 'Figuren_Theater\loaded', __NAMESPACE__ . '\\filter_options', 11 );
add_action( 'plugins_loaded', __NAMESPACE__ . '\\load_plugin' );
}
/**
* Conditionally load the plugin itself and its modifications.
*
* @return void
*/
function load_plugin() :void {
$config = Figuren_Theater\get_config()['modules']['performance'];
if ( ! $config['pwa'] ) {
return;
}
// Enable verbose Workbox.js logging in the browser console.
define( 'WP_SERVICE_WORKER_DEBUG_LOG', WP_DEBUG );
require_once FT_VENDOR_DIR . PLUGINPATH; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingCustomConstant
add_filter( 'web_app_manifest', __NAMESPACE__ . '\\modify_manifest' );
add_filter( 'wp_resource_hints', __NAMESPACE__ . '\\prefetch_manifest', 10, 2 );
// Filters service worker caching configuration for theme asset requests.
add_filter( 'wp_service_worker_theme_asset_caching', __NAMESPACE__ . '\\theme_asset_caching' );
add_filter( 'wp_service_worker_plugin_asset_caching', __NAMESPACE__ . '\\theme_asset_caching' );
add_filter( 'wp_service_worker_core_asset_caching', __NAMESPACE__ . '\\theme_asset_caching' );
/**
* Enable offline media.
*
* UNUSED, at the moment
*
* @todo #25 Re-Enable offline media.
*
* @see docblock of wp_front_service_worker__offline_media()
*
* add_filter( 'wp_front_service_worker', __NAMESPACE__ . '\\wp_front_service_worker__offline_media' );
*/
}
/**
* Handle options
*
* @return void
*/
function filter_options() :void {
$_options = [
'offline_browsing' => 1,
];
/*
* Gets added to the 'OptionsCollection'
* from within itself on creation.
*/
new Options\Factory(
$_options,
'Figuren_Theater\Options\Option',
BASENAME,
);
}
/**
* Add the web app manifest url to the list of 'prefetch'ed ressources.
*
* The used filter: Filters domains and URLs for resource hints of relation type.
*
* @param array<int, string|array<string, string>> $urls {
* Array of resources and their attributes, or URLs to print for resource hints.
*
* @type array|string ...$0 {
* Array of resource attributes, or a URL string.
*
* @type string $href URL to include in resource hints. Required.
* @type string $as How the browser should treat the resource
* (`script`, `style`, `image`, `document`, etc).
* @type string $crossorigin Indicates the CORS policy of the specified resource.
* @type float $pr Expected probability that the resource hint will be used.
* @type string $type Type of the resource (`text/html`, `text/css`, etc).
* }
* }
* @param string $relation_type The relation type the URLs are printed for,
* e.g. 'preconnect' or 'prerender'.
*
* @return array<int, string|array<string, string>>
*/
function prefetch_manifest( array $urls, string $relation_type ) : array {
if ( ! class_exists( 'WP_Web_App_Manifest' ) ) {
return $urls;
}
if ( 'prefetch' === $relation_type ) {
$urls[] = WP_Web_App_Manifest::get_url();
}
return $urls;
}
/**
* Overriding the (default) manifest json.
*
* There are more possible values for this, including 'orientation' and 'scope.'
* See the documentation: https://developers.google.com/web/fundamentals/web-app-manifest/
*
* @param array<string, mixed> $manifest Data of the manifest, to send in the REST API response.
*
* @return array<string, mixed> $manifest Data of the manifest, to send in the REST API response.
*/
function modify_manifest( array $manifest ) : array {
$manifest = set_shortcuts( $manifest );
$manifest = set_colors( $manifest );
$manifest = set_defaults( $manifest );
$manifest = set_screenshots( $manifest );
return $manifest;
}
/**
* Provide an app shortcut to the latest news via either the defined blog_posts page or the default_category.
*
* Overriding the (default) manifest json.
*
* @param array<string, mixed> $manifest Data of the manifest, to send in the REST API response.
*
* @return array<string, mixed> $manifest Data of the manifest, to send in the REST API response.
*/
function set_shortcuts( array $manifest ) : array {
$page_for_posts_id = get_option( 'page_for_posts' );
if ( \is_int( $page_for_posts_id ) && ! empty( $page_for_posts_id ) ) {
$page_for_posts = get_post( $page_for_posts_id );
if ( $page_for_posts instanceof WP_Post ) {
$name = get_the_title( $page_for_posts );
$url = get_permalink( $page_for_posts );
$excerpt = get_the_excerpt( $page_for_posts );
}
} else {
// Try to use the default category for posts
// as the base url for any kind of news.
$default_category = get_term( (int) get_option( 'default_category' ), 'category' );
if ( $default_category instanceof WP_Term ) {
$name = $default_category->name;
$url = get_term_link( $default_category );
$excerpt = $default_category->description;
}
}
if ( ! isset( $name ) || ! isset( $url ) ) {
return $manifest;
}
if ( \is_string( $url ) && esc_url( $url ) ) {
$excerpt = ( ! empty( $excerpt ) ) ? $excerpt : __( 'News', 'figurentheater' );
$manifest['shortcuts'][] = [
'name' => $name,
'url' => $url,
'description' => $excerpt,
/**
* Icons 2 SVG 2 data-uri
*
* @see https://icon-sets.iconify.design/dashicons/admin-post/
*/
'icons' => [
[
'src' => WPMU_PLUGIN_URL . Performance\ASSETS_URL . 'svg/admin-post.svg',
'type' => 'image/svg+xml',
'purpose' => 'any monochrome',
],
],
];
}
return $manifest;
}
/**
* Provide app colors for background and theme.
*
* Overriding the (default) manifest json.
*
* @param array<string, mixed> $manifest Data of the manifest, to send in the REST API response.
*
* @return array<string, mixed> $manifest Data of the manifest, to send in the REST API response.
*/
function set_colors( array $manifest ) : array {
$relevant_colors = Themed_Login\ft_get_relevant_colors();
$manifest['background_color'] = $relevant_colors['ft_accent'];
$manifest['theme_color'] = $relevant_colors['ft_accent'];
return $manifest;
}
/**
* Provide app defaults for OS integration and UI.
*
* Overriding the (default) manifest json.
*
* @param array<string, mixed> $manifest Data of the manifest, to send in the REST API response.
*
* @return array<string, mixed> $manifest Data of the manifest, to send in the REST API response.
*/
function set_defaults( array $manifest ) : array {
$manifest['orientation'] = 'any';
$manifest['display'] = 'standalone';
$manifest['url_handler'] = [
'origin' => home_url( '/', 'https' ),
];
return $manifest;
}
/**
* Provide an app screenshot of the home-page to be shown in the install dialog.
*
* Overriding the (default) manifest json.
*
* @param array<string, mixed> $manifest Data of the manifest, to send in the REST API response.
*
* @return array<string, mixed> $manifest Data of the manifest, to send in the REST API response.
*/
function set_screenshots( array $manifest ) : array {
if ( ! isset( $manifest['screenshots'] ) ) {
$manifest['screenshots'] = [];
}
$manifest['screenshots'][] = [
'src' => get_shot( home_url(), 900, 2000, 'home' ),
'sizes' => '900x2000',
'type' => 'image/jpeg',
];
return $manifest;
}
/**
* Get Browser Screenshot
*
* Get a screenshot of a website using Automatics mShots service.
*
* @see https://github.com/BinaryMoon/browser-shots/blob/master/browser-shots.php#L176-L202
* @see https://github.com/Automattic/mShots/blob/master/public_html/class-mshots.php
*
* @since 1.3 Removed call $this->save_screenshot( $remote_url, $new_name )
*
* @param string $url Url to screenshot.
* @param int $width Width of screenshot.
* @param int $height Height of screenshot.
* @param string $new_name Filename to save.
*
* @return string
*/
function get_shot( string $url = '', int $width = 600, int $height = 450, string $new_name = '' ) : string {
// $new_name = 'pwa-'.$new_name.'.jpeg';
// $dir = $this->set_screenshots_dir_path(); // !
// $new_file = $dir.'/'.$new_name;
// already cached ?
// DISABLED,
// because it's nothing saved
// to pick up here
// if (
// file_exists($new_file)
// &&
// file not older than 7 days
// ! ( time() - filemtime($new_file) > 7 * 24 * 3600 )
// file not older than 60 seconds // DEBUG
// ! ( time() - filemtime($new_file) > 60 )
// )
// return $this->set_screenshots_dir_url() . $new_name; //!
// Image found.
if ( '' !== $url ) {
/*
* Possible indexes are: 'vpw', 'vph' and 'scale'.
*/
$args = [
'vpw' => intval( $width ),
'vph' => intval( $height ),
];
$remote_url = 'https://s0.wp.com/mshots/v1/' . rawurlencode( esc_url( $url ) );
/**
* Values of add_query_arg() are expected to be encoded
* appropriately with urlencode() or rawurlencode().
*
* Using rawurlencode on any variable used as part of the query string,
* either by using add_query_arg() or directly by string concatenation,
* will prevent parameter hijacking.
*
* @see https://docs.wpvip.com/technical-references/code-quality-and-best-practices/encode-values-passed-to-add_query_arg/
*/
$args = array_map( 'rawurlencode', $args );
$remote_url = \add_query_arg( $args, $remote_url );
return $remote_url;
}
return '';
}
/**
* Filters service worker caching configuration for theme asset requests.
*
* @since PWA 0.6
*
* @param array<string, string|null|array<mixed>> $config {
* Theme asset caching configuration. If array filtered to be empty, then caching is disabled.
*
* @type string $route Route. Regular expression pattern to match. See <https://developers.google.com/web/tools/workbox/reference-docs/latest/module-workbox-routing#.registerRoute>.
* @type string $strategy Strategy. Defaults to NetworkFirst.
* Even though assets should have far-future expiration, network-first is still preferred for development purposes.
* See <https://developers.google.com/web/tools/workbox/reference-docs/latest/module-workbox-strategies>.
* @type string $cache_name Cache name. Defaults to 'uploaded-images'. This will get a site-specific prefix to prevent subdirectory multisite conflicts.
* @type array|null $expiration {
* Expiration plugin configuration. See <https://developers.google.com/web/tools/workbox/reference-docs/latest/module-workbox-expiration.ExpirationPlugin>.
*
* @type int|null $max_entries Max entries to cache. Defaults to 34.
* This limit the cached entries to the number of files loaded over network, e.g. JS, CSS, and PNG.
* The number 34 is derived from the 75th percentile of theme assets used on pages served from
* WordPress sites, as indexed by HTTP Archive.
* See https://github.com/GoogleChromeLabs/pwa-wp/issues/265#issuecomment-706612536.
* @type int|null $max_age_seconds Max age seconds. Defaults to null.
* }
* @type array|null $broadcast_update Broadcast update plugin configuration. Not included by default. See <https://developers.google.com/web/tools/workbox/reference-docs/latest/module-workbox-broadcast-update.BroadcastUpdatePlugin>.
* @type array|null $cacheable_response Cacheable response plugin configuration. Not included by default. See <https://developers.google.com/web/tools/workbox/reference-docs/latest/module-workbox-cacheable-response.CacheableResponsePlugin>.
* }
*
* @return array<string, string|null|array<mixed>>
*/
function theme_asset_caching( array $config ) : array {
// 'NetworkFirst' is the default.
$config['strategy'] = \WP_Service_Worker_Caching_Routes::STRATEGY_STALE_WHILE_REVALIDATE;
return $config;
}
/**
// TEMP DISABLED ::
protected function save_screenshot( string $file_url, string $new_file_name ) : string
{
// If the function it's not available, require it.
if ( ! function_exists( 'download_url' ) ) {
require_once \ABSPATH . 'wp-admin/includes/file.php';
}
$dir = $this->set_screenshots_dir_path();
// check is directory exists
if ( !file_exists( $dir ) && !is_dir( $dir) )
mkdir($dir);
// Sets file final destination.
$filepath = $dir . '/' . $new_file_name;
// Now you can use it!
// $file_url = 'https://example.com/myfile.ext';
$tmp_file = \download_url( $file_url );
// Copies the file to the final destination and deletes temporary file.
copy( $tmp_file, $filepath );
*
@unlink( $tmp_file );
$public_url = $this->set_screenshots_dir_url() . $new_file_name;
return $public_url;
}
protected function set_screenshots_dir_path() : string
{
// VARIANT 1
// save to cache folder
#$current_domain = substr( \get_site_url(), 8);
#$dir = \ABSPATH . 'content/cache/'.$current_domain;
#$public_url = \WP_CONTENT_URL .'/cache/'.$current_domain;
// VARIANT 2
// save to __media
// $this->set_screenshots_dir_url( '/__media/' );
$blog_id = \get_current_blog_id();
return $this->screenshots_dir_path = \WP_CONTENT_DIR . '/uploads/sites/'.$blog_id;
}
protected function set_screenshots_dir_url() : string
{
return $this->screenshots_dir_url = \home_url( '/__media/', 'https' );
}
__// TEMP DISABLED :: END
*/
/**
* UNUSED at the moment
* because, it can't be that easy to have offline media
* as this repo and its readme descripe deeply:
* https://github.com/daffinm/audio-cache-test
*
* [wp_front_service_worker__offline_media description]
*
* @package figurentheater
* @version 2022.09.14
* @author Carsten Bach
*
* @param \WP_Service_Worker_Scripts $scripts [description]
* @return void
function wp_front_service_worker__offline_media( \WP_Service_Worker_Scripts $scripts )
{
$scripts->caching_routes()->register(
'/__media/.*\.(?:png|gif|jpg|jpeg|svg|webp)(\?.*)?$',
array(
'strategy' => \WP_Service_Worker_Caching_Routes::STRATEGY_NETWORK_FIRST,
'cacheName' => 'ft_nw_images',
'plugins' => array(
'expiration' => array(
'maxEntries' => 60,
'maxAgeSeconds' => \MONTH_IN_SECONDS,
),
),
)
);
$scripts->caching_routes()->register(
'/__media/.*\.(?:mp3)(\?.*)?$',
array(
'strategy' => \WP_Service_Worker_Caching_Routes::STRATEGY_NETWORK_FIRST,
'cacheName' => 'ft_music',
'plugins' => array(
'expiration' => array(
'maxEntries' => 60,
'maxAgeSeconds' => \YEAR_IN_SECONDS,
),
),
)
);
}
*/