-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass-imagecdn.php
More file actions
642 lines (562 loc) · 17.7 KB
/
class-imagecdn.php
File metadata and controls
642 lines (562 loc) · 17.7 KB
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
<?php
/**
* This file contains the ImageCDN class.
*
* @package ImageCDN
*/
namespace ImageEngine;
/**
* The ImageCDN class manages plugin initialization (adding actions and filters, etc).
*/
class ImageCDN {
/**
* Static constructor.
*/
public static function instance() {
new self();
}
/**
* Client hints.
*
* @var []string
*/
private static $client_hints = array(
'sec-ch-dpr',
'sec-ch-width',
'sec-ch-viewport-width',
'ect', // kept in for legacy reasons
'sec-ch-ect',
'sec-ch-ua-full-version',
'sec-ch-ua-full-version-list',
'sec-ch-ua-platform-version',
'sec-ch-ua-arch',
'sec-ch-ua-wow64',
'sec-ch-ua-bitness',
'sec-ch-ua-model',
/**
* Disabled for CORS compatibility:
* 'ECT',
* 'Device-Memory',
* 'RTT',
* 'Downlink',
*/
);
/**
* Client hints that do not require CORS preflight confirmation.
*
* @var []string
*/
private static $safe_client_hints = array(
'sec-ch-dpr',
'sec-ch-width',
'sec-ch-viewport-width',
);
/**
* Singleton Rewriter instance.
*
* @var Rewriter
*/
private static $rewriter;
/**
* If true, some functionality will be augmented to facilitate testing.
*
* @internal
* @var bool
*/
public static $tests_running = false;
/**
* Captures headers written during unit testing.
*
* @internal
* @var []string
*/
public static $test_headers_written = array();
/**
* Options that will be used during unit testing.
*
* @internal
* @var []string
*/
public static $test_options = array();
/**
* Constructor.
*/
public function __construct() {
// Only enable the hooks if the configuration is valid and the plugin is enabled.
if ( self::should_rewrite() ) {
// Rewriter hook.
add_action( 'template_redirect', array( self::class, 'handle_rewrite_hook' ) );
// Rewrite rendered content in REST API.
add_filter( 'the_content', array( self::class, 'rewrite_html' ), 100 );
/**
* Resource hints. Note that the 'wp_head' is disabled for the time being due to CORS incompatibility.
* add_action( 'wp_head', array( self::class, 'add_head_tags' ), 0 );
*/
add_action( 'send_headers', array( self::class, 'add_headers' ), 0 );
// REST API hooks.
add_filter( 'rest_post_dispatch', array( self::class, 'rewrite_rest_api' ), 10, 3 );
// Custom filters that can be used in themes to update bare URLs and URLs in HTML.
add_filter( 'image_cdn_url', array( self::class, 'rewrite_url' ) );
add_filter( 'image_cdn_html', array( self::class, 'rewrite_html' ) );
// Thrive custom filters
if ( class_exists( 'TCB_Landing_Page' ) ) {
add_filter( 'tve_landing_page_content', array( self::class, 'rewrite_html' ), 100 );
add_filter( 'thrive_css_file_content', array( self::class, 'rewrite_html' ), 100 );
add_filter( 'get_post_metadata', array( self::class, 'tve_custom_css_post_metadata' ), 100, 4 );
}
}
// Hooks.
add_action( 'admin_init', array( self::class, 'register_textdomain' ) );
add_action( 'admin_init', array( Settings::class, 'register_settings' ) );
add_action( 'admin_menu', array( Settings::class, 'add_settings_page' ) );
add_action( 'admin_footer', array( Settings::class, 'register_test_config' ) );
add_action( 'wp_ajax_image_cdn_test_config', array( Settings::class, 'test_config' ) );
add_filter( 'plugin_action_links_' . IMAGE_CDN_BASE, array( self::class, 'add_action_link' ) );
add_action( 'admin_post_register', array( Settings::class, 'register' ) );
add_action( 'admin_post_login', array( Settings::class, 'login' ) );
add_action( 'admin_post_logout', array( Settings::class, 'logout' ) );
add_action( 'admin_footer', array( Settings::class, 'register_analytics' ) );
add_action( 'wp_ajax_image_cdn_analytics', array( Settings::class, 'analytics' ) );
}
/**
* Outputs an HTTP header.
*
* @param string $key HTTP header key.
* @param string $value HTTP header value.
*/
private static function header( $key, $value ) {
$val = "$key: $value";
if ( self::$tests_running ) {
self::$test_headers_written[] = $val;
return;
}
header( $val );
}
/**
* Add http headers for Client Hints, Feature Policy and Preconnect Resource Hint.
*/
public static function add_headers() {
self::header( 'Accept-CH', strtolower( implode( ', ', self::$client_hints ) ) );
// Add resource hints and feature policy.
$options = self::get_options();
$host = wp_parse_url( $options['url'], PHP_URL_HOST );
if ( empty( $host ) ) {
return;
}
$protocol = is_ssl() ? 'https' : 'http';
// Add Preconnect header.
self::header( 'Link', "<{$protocol}://{$host}>; rel=preconnect" );
// Add Feature-Policy header.
// @deprecated in favor of Permissions-Policy and will be removed once adaquate market
// adoption has been reached (90-95%).
$features = array();
foreach ( self::$client_hints as $hint ) {
$features[] = strtolower( "ch-{$hint} {$protocol}://{$host}" );
}
$permissions = array();
foreach ( self::$client_hints as $hint ) {
$get_hint = str_replace( 'sec-', '', $hint );
if($get_hint ==='ect') continue;
$permissions[] = strtolower( "{$get_hint}=(\"{$protocol}://{$host}\")" );
}
// Add Permissions-Policy header.
// This header replaced Feature-Policy in Chrome 88, released in January 2021.
// @see https://github.com/w3c/webappsec-permissions-policy/blob/main/permissions-policy-explainer.md#appendix-big-changes-since-this-was-called-feature-policy .
self::header( 'Permissions-Policy', strtolower( implode( ', ', $permissions ) ) );
}
/**
* Add meta tags for Client Hints and Preconnect Resource Hint.
*/
public static function add_head_tags() {
// Add client hints.
echo ' <meta http-equiv="Accept-CH" content="' . esc_attr( implode( ', ', self::$client_hints ) ) . '">' . "\n";
// Add resource hints.
$options = self::get_options();
$host = wp_parse_url( $options['url'], PHP_URL_HOST );
if ( ! empty( $host ) ) {
echo ' <link rel="preconnect" href="//' . esc_attr( $host ) . '">' . "\n";
}
}
/**
* Add action links.
*
* @param array $data already existing links.
* @return array $data extended array with links.
*/
public static function add_action_link( $data ) {
// check permission.
if ( ! current_user_can( 'manage_options' ) ) {
return $data;
}
return array_merge(
$data,
array(
sprintf(
'<a href="%s">%s</a>',
add_query_arg( array( 'page' => 'image_cdn' ), admin_url( 'admin.php' ) ),
__( 'Settings' )
),
)
);
}
/**
* Gets the list of active client hints.
*
* @return array client hints.
*/
public static function get_client_hints() {
return self::$client_hints;
}
/**
* Gets the list of safe client hints.
*
* @return array client hints.
*/
public static function get_safe_client_hints() {
return self::$safe_client_hints;
}
/**
* Gets the list of active unsafe client hints.
*
* @return array client hints.
*/
public static function get_unsafe_client_hints() {
return array_diff( self::$client_hints, self::$safe_client_hints );
}
/**
* Rewrite image URLs in REST API responses
*
* @param mixed $result response to replace the requested version with. Can be anything a normal endpoint can return, or null to not hijack the request.
* @param \WP_REST_Server $server REST API server instance.
* @param \WP_REST_Request $request request used to generate the response.
* @return array $data extended array with links.
*/
public static function rewrite_rest_api( $result, $server, $request ) {
if ( ! ( $result instanceof \WP_REST_Response && is_array( $result->data ) ) ) {
return $result;
}
$rewriter = self::get_rewriter();
$url_matcher = $rewriter->generate_regex_for_url();
$url = array_key_exists( 'REQUEST_URI', $_SERVER ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';
$is_woocommerce = strpos( $url, '/wp-json/wc/' ) !== false;
foreach ( $result->data as &$item ) {
if ( ! is_array( $item ) ) {
continue;
}
// Rewrite image URLs for Advanced Custom Fields REST API.
if ( array_key_exists( 'acf', $item ) ) {
foreach ( $item['acf'] as &$field ) {
if ( ! is_array( $field ) ) {
continue;
}
if ( array_key_exists( 'type', $field ) && 'image' === $field['type'] ) {
// Main image.
if ( preg_match( $url_matcher, $field['url'] ) ) {
$field['url'] = $rewriter->rewrite_url( $field['url'] );
}
// Icon.
if ( preg_match( $url_matcher, $field['icon'] ) ) {
$field['icon'] = $rewriter->rewrite_url( $field['icon'] );
}
// Image variants.
foreach ( $field['sizes'] as &$variant ) {
if ( is_string( $variant ) && preg_match( $url_matcher, $variant ) ) {
$variant = $rewriter->rewrite_url( $variant );
}
}
}
}
}
// Rewrite image URLs for WooCommerce REST API.
if ( $is_woocommerce ) {
// Product gallery images.
if ( array_key_exists( 'images', $item ) && is_array( $item['images'] ) ) {
foreach ( $item['images'] as &$image ) {
if ( ! is_array( $image ) ) {
continue;
}
if ( array_key_exists( 'src', $image ) && preg_match( $url_matcher, $image['src'] ) ) {
$image['src'] = $rewriter->rewrite_url( $image['src'] );
}
}
}
// HTML fragments.
foreach ( array( 'description', 'short_description', 'price_html' ) as $fragment ) {
if ( array_key_exists( $fragment, $item ) && is_string( $item[ $fragment ] ) ) {
$item[ $fragment ] = $rewriter->rewrite( $item[ $fragment ] );
}
}
}
}
return $result;
}
/**
* Run uninstall hook.
*/
public static function handle_uninstall_hook() {
delete_option( 'image_cdn' );
}
/**
* Returns the default options for this plugin.
*
* @return array Default options.
*/
public static function default_options() {
$content_path = trim( wp_parse_url( content_url(), PHP_URL_PATH ), '/' );
$includes_path = trim( wp_parse_url( includes_url(), PHP_URL_PATH ), '/' );
return array(
'url' => '',
'dirs' => implode( ',', array( $content_path, $includes_path ) ),
'excludes' => '.php',
'relative' => true,
'https' => true,
'directives' => '',
'enabled' => false,
);
}
/**
* Run activation hook.
*/
public static function handle_activation_hook() {
add_option( 'image_cdn', self::default_options() );
}
/**
* Redirect the user to the settings page after activation.
*
* @param string $plugin name of the activated plugin.
*/
public static function settings_redirect( $plugin ) {
if ( ! defined( 'IMAGE_CDN_BASE' ) || IMAGE_CDN_BASE !== $plugin ) {
return;
}
wp_safe_redirect( add_query_arg( array( 'page' => 'image_cdn' ), admin_url( 'admin.php' ) ) );
exit;
}
/**
* Check plugin requirements.
*/
public static function image_cdn_requirements_check() {
// WordPress version check.
if ( version_compare( $GLOBALS['wp_version'], IMAGE_CDN_MIN_WP . 'alpha', '<' ) ) {
show_message(
sprintf(
'<div class="error"><p>%s</p></div>',
sprintf(
// translators: %s: WordPress version.
__( 'The Image CDN plugin is optimized for WordPress %s. Please disable the plugin or upgrade your WordPress installation (recommended).', 'image-cdn' ),
IMAGE_CDN_MIN_WP
)
)
);
}
}
/**
* Register textdomain.
*/
public static function register_textdomain() {
load_plugin_textdomain( 'image-cdn', false, 'image-cdn/lang' );
}
/**
* Return plugin options.
*
* @return array $diff data pairs.
*/
public static function get_options() {
if ( self::$tests_running ) {
return self::$test_options;
}
return wp_parse_args( get_option( 'image_cdn' ), self::default_options() );
}
/**
* Update Delivery Address
*
* @return string
*
* @throws Exception
*/
public static function update_delivery_address( $delivery_addresses ) {
if ( is_string( $delivery_addresses ) ) {
$delivery_address = $delivery_addresses;
$options = self::get_options();
$options['url'] = (is_ssl() ? "https://" : "http://") . $delivery_address . ".imgeng.in";
// $options['enabled'] = true;
$options['https'] = is_ssl();
self::update_options( $options );
return __( "Delivery address saved!", "image-cdn" );
} else {
$found = false;
$options = self::get_options();
foreach ( $delivery_addresses as &$address ) {
$address = $address . ".imgeng.in";
$url = (is_ssl() ? "https://" : "http://") . $address;
if ( $url == $options['url'] ) {
$found = true;
break;
}
}
if ( $found ) {
return __("Delivery address found! You can choose a different delivery address bellow.", "image-cdn");
} else {
$options['url'] = '';
$options['enabled'] = false;
self::update_options( $options );
return __("Please choose a delivery address bellow!", "image-cdn");
}
}
}
/**
* Update plugin options.
*
* @return bool
*/
public static function update_options( $options ) {
return update_option( 'image_cdn', $options );
}
/**
* Return new rewriter.
*/
public static function get_rewriter() {
if ( ! ( self::$rewriter instanceof Rewriter ) ) {
$options = self::get_options();
$excludes = array_map( 'trim', explode( ',', $options['excludes'] ) );
self::$rewriter = new Rewriter(
get_option( 'home' ),
$options['url'],
$options['dirs'],
$excludes,
$options['relative'],
$options['https'],
$options['directives']
);
}
return self::$rewriter;
}
/**
* Run rewrite hook.
*/
public static function handle_rewrite_hook() {
$rewriter = self::get_rewriter();
ob_start( array( $rewriter, 'rewrite' ) );
}
/**
* Rewrite html content.
*
* @param string $html The HTML content.
*/
public static function rewrite_html( $html ) {
$rewriter = self::get_rewriter();
return $rewriter->rewrite( $html );
}
/**
* Returns true if the content should be rewritten.
*/
public static function should_rewrite() {
$options = self::get_options();
if ( ! $options['enabled'] ) {
return false;
}
if ( '' === $options['url'] ) {
return false;
}
return true;
}
/**
* Rewrite URL.
*
* @param string $url The URL.
*/
public static function rewrite_url( $url ) {
$rewriter = self::get_rewriter();
return $rewriter->rewrite_url( $url );
}
/**
* Notifies the user in the settings page if they are not using ImageEngine.
*
* @return void
*/
public static function ie_admin_notice() {
$options = self::get_options();
$img_url = $options['url'] . wp_parse_url( plugin_dir_url( IMAGE_CDN_FILE ), PHP_URL_PATH ) . 'assets/logo.png';
global $pagenow;
// check if user is on the settings page.
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( 'admin.php' === $pagenow && isset( $_GET['page'] ) && 'image_cdn' === $_GET['page'] ) {
if ( ! $options['enabled'] || false === self::is_server_imageengine( $img_url ) ) {
?>
<div class="notice notice-warning">
<p>
<?php
printf(
// translators: %s is a link to the ImageEngine site.
esc_html__( 'This plugin will enable the %s CDN on %s.', 'image-cdn' ),
'<a href="https://imageengine.io/?utm_source=WP-plugin-settigns&utm_medium=page&utm_term=wp-imageengine&utm_campaign=wp-imageengine" target="_blank">ImageEngine</a>',
esc_html( get_site_url() )
);
?>
</p>
<ol>
<li><?php esc_html_e( 'Claim your delivery address by signing up, or log in if you already have an account.', 'image-cdn') ?></li>
<li><?php esc_html_e( 'Delivery address appears in the Setup tab below.', 'image-cdn') ?></li>
<?php
if ( ! $options['enabled'] ) {
?>
<li>Enable ImageEngine and hit "Test Configuration" before saving the changes.</li>
<?php
}
?>
</ol>
<p>See <a href="https://support.imageengine.io/hc/en-us/articles/360059238371-Quick-Start/?utm_source=WP-plugin-settigns&utm_medium=page&utm_term=wp-imageengine&utm_campaign=wp-imageengine" target="_blank">full documentation.</a></p>
</div>
<?php
} else {
?>
<div class="notice notice-success">
<p>
<?php
printf(
// translators: %s is a the ImageEngine delivery address .
esc_html__( '%s is a valid ImageEngine delivery address.', 'image-cdn' ),
'<code>' . esc_html( $options['url'] ) . '</code>'
);
?>
</p>
<ul>
<li><a href="https://control.imageengine.io/?utm_source=WP-plugin-settigns&utm_medium=page&utm_term=wp-imageengine&utm_campaign=wp-imageengine" target="_blank">ImageEngine Control Panel</a></li>
<li><a href="https://support.imageengine.io/?utm_source=WP-plugin-settigns&utm_medium=page&utm_term=wp-imageengine&utm_campaign=wp-imageengine" target="_blank">ImageEngine Documentation</a></li>
</ul>
</div>
<?php
}
}
}
/**
* Returns true if the URL is served by ImageEngine.
*
* @param string $url URL to be checked.
* @return bool whether or not the server is ImageEngine.
*/
public static function is_server_imageengine( $url ) {
$cdn_res = wp_remote_get( $url, array( 'sslverify' => true ) );
if ( is_wp_error( $cdn_res ) ) {
return false;
}
if ( 'ScientiaMobile ImageEngine' === $cdn_res['headers']['server'] ) {
return true;
} else {
return false;
}
}
/**
* Rewrite tve_custom_css.
*/
public static function tve_custom_css_post_metadata( $value, $object_id, $meta_key, $single ) {
if ( isset( $meta_key ) && strpos( $meta_key, 'tve_custom_css' ) !== false ) {
remove_filter( 'get_post_metadata', array( self::class, 'tve_custom_css_post_metadata' ), 100 );
$current_meta = get_post_meta( $object_id, $meta_key, $single );
add_filter( 'get_post_metadata', array( self::class, 'tve_custom_css_post_metadata' ), 100, 4 );
$rewriter = self::get_rewriter();
return $rewriter->rewrite( $current_meta );
}
return $value;
}
}