3.4.php
17.6 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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
<?php
defined( 'ABSPATH' ) || exit;
/**
* Get Zones linked to a Cloudflare account
*
* @since 2.9
* @deprecated 3.4.1.2
* @author Remy Perona
*
* @return Array List of zones or default no domain
*/
function get_rocket_cloudflare_zones() {
_deprecated_function( __FUNCTION__ . '()', '3.4.1.2' );
$cf_api_instance = get_rocket_cloudflare_api_instance();
$domains = array(
'' => __( 'Choose a domain from the list', 'rocket' ),
);
if ( is_wp_error( $cf_api_instance ) ) {
return $domains;
}
try {
$cf_zone_instance = new Cloudflare\Zone( $cf_api_instance );
$cf_zones = $cf_zone_instance->zones( null, 'active', null, 50 );
$cf_zones_list = $cf_zones->result;
if ( ! (bool) $cf_zones_list ) {
$domains[] = __( 'No domain available in your Cloudflare account', 'rocket' );
return $domains;
}
foreach ( $cf_zones_list as $cf_zone ) {
$domains[ $cf_zone->name ] = $cf_zone->name;
}
return $domains;
} catch ( Exception $e ) {
return $domains;
}
}
/**
* Get CNAMES hosts
*
* @since 2.3
* @deprecated 3.4
*
* @param string $zones CNAMES zones.
* @return array $hosts CNAMES hosts
*/
function get_rocket_cnames_host( $zones = array( 'all' ) ) {
_deprecated_function( __FUNCTION__ . '()', '3.4', '\WP_Rocket\Subscriber\CDN\CDNSubscriber::get_cdn_hosts()' );
$hosts = array();
$cnames = get_rocket_cdn_cnames( $zones );
if ( $cnames ) {
foreach ( $cnames as $cname ) {
$cname = rocket_add_url_protocol( $cname );
$hosts[] = rocket_extract_url_component( $cname, PHP_URL_HOST );
}
}
return $hosts;
}
/**
* Apply CDN on CSS properties (background, background-image, @import, src:url (fonts))
*
* @since 2.6
* @since 3.4
*
* @param string $buffer file content.
* @return string modified file content
*/
function rocket_cdn_css_properties( $buffer ) {
_deprecated_function( __FUNCTION__ . '()', '3.4', '\WP_Rocket\Subscriber\CDN\CDN::rewrite_css_properties()' );
$zone = array(
'all',
'images',
'css_and_js',
'css',
);
$cnames = get_rocket_cdn_cnames( $zone );
/**
* Filters the application of the CDN on CSS properties
*
* @since 2.6
*
* @param bool true to apply CDN to properties, false otherwise
*/
$do_rocket_cdn_css_properties = apply_filters( 'do_rocket_cdn_css_properties', true );
if ( ! get_rocket_option( 'cdn' ) || ! $cnames || ! $do_rocket_cdn_css_properties ) {
return $buffer;
}
preg_match_all( '/url\((?![\'"]?data)([^\)]+)\)/i', $buffer, $matches );
if ( is_array( $matches ) ) {
$i = 0;
foreach ( $matches[1] as $url ) {
$url = trim( $url, " \t\n\r\0\x0B\"'" );
/**
* Filters the URL of the CSS property
*
* @since 2.8
*
* @param string $url URL of the CSS property
*/
$url = get_rocket_cdn_url( apply_filters( 'rocket_cdn_css_properties_url', $url ), $zone );
$property = str_replace( $matches[1][ $i ], $url, $matches[0][ $i ] );
$buffer = str_replace( $matches[0][ $i ], $property, $buffer );
$i++;
}
}
return $buffer;
}
/**
* Apply CDN on custom data attributes.
*
* @since 2.5.5
* @deprecated 3.4
*
* @param string $html Original Output.
* @return string $html Output that will be printed
*/
function rocket_add_cdn_on_custom_attr( $html ) {
_deprecated_function( __FUNCTION__ . '()', '3.4' );
if ( preg_match( '/(data-lazy-src|data-lazyload|data-src|data-retina)=[\'"]?([^\'"\s>]+)[\'"]/i', $html, $matches ) ) {
$html = str_replace( $matches[2], get_rocket_cdn_url( $matches[2], array( 'all', 'images' ) ), $html );
}
return $html;
}
/**
* Replace URL by CDN of all thumbnails and smilies.
*
* @since 2.1
* @deprecated 3.4
*
* @param string $url URL of the file to replace the domain with the CDN.
* @return string modified URL
*/
function rocket_cdn_file( $url ) {
_deprecated_function( __FUNCTION__ . '()', '3.4' );
if ( defined( 'DONOTROCKETOPTIMIZE' ) && DONOTROCKETOPTIMIZE ) {
return $url;
}
if ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) {
return $url;
}
$ext = pathinfo( $url, PATHINFO_EXTENSION );
if ( is_admin() || 'php' === $ext ) {
return $url;
}
$filter = current_filter();
$rejected_files = get_rocket_cdn_reject_files();
if ( 'template_directory_uri' === $filter && ! empty( $rejected_files ) ) {
return $url;
}
switch ( $filter ) {
case 'wp_get_attachment_url':
case 'wp_calculate_image_srcset':
$zone = array( 'all', 'images' );
break;
case 'smilies_src':
$zone = array( 'all', 'images' );
break;
case 'stylesheet_uri':
case 'wp_minify_css_url':
case 'wp_minify_js_url':
case 'bwp_get_minify_src':
$zone = array( 'all', 'css_and_js', $ext );
break;
default:
$zone = array( 'all', $ext );
break;
}
$cnames = get_rocket_cdn_cnames( $zone );
if ( $cnames ) {
$url = get_rocket_cdn_url( $url, $zone );
}
return $url;
}
/**
* Replace URL by CDN of images displayed using wp_get_attachment_image_src
*
* @since 2.9.2
* @deprecated 3.4
* @author Remy Perona
* @source https://github.com/wp-media/wp-rocket/issues/271#issuecomment-269849927
*
* @param array $image An array containing the src, width and height of the image.
* @return array Array with updated src URL
*/
function rocket_cdn_attachment_image_src( $image ) {
_deprecated_function( __FUNCTION__ . '()', '3.4' );
if ( defined( 'DONOTROCKETOPTIMIZE' ) && DONOTROCKETOPTIMIZE ) {
return $image;
}
if ( ! (bool) $image ) {
return $image;
}
if ( is_admin() || is_preview() || is_feed() ) {
return $image;
}
$zones = array( 'all', 'images' );
if ( ! (bool) get_rocket_cdn_cnames( $zones ) ) {
return $image;
}
$image[0] = get_rocket_cdn_url( $image[0], $zones );
return $image;
}
/**
* Replace srcset URLs by CDN URLs for WP responsive images
*
* @since WP 4.4
* @since 2.6.14
* @deprecated 3.4
* @author Remy Perona
*
* @param array $sources multidimensional array containing srcset images urls.
* @return array $sources
*/
function rocket_add_cdn_on_srcset( $sources ) {
_deprecated_function( __FUNCTION__ . '()', '3.4' );
if ( defined( 'DONOTROCKETOPTIMIZE' ) && DONOTROCKETOPTIMIZE ) {
return $sources;
}
if ( (bool) $sources ) {
foreach ( $sources as $width => $data ) {
$sources[ $width ]['url'] = rocket_cdn_file( $data['url'] );
}
}
return $sources;
}
/**
* Replace URL by CDN of all images display in a post content or a widget text.
*
* @since 2.1
* @deprecated 3.4
*
* @param string $html HTML content to parse.
* @return string modified HTML content
*/
function rocket_cdn_images( $html ) {
_deprecated_function( __FUNCTION__ . '()', '3.4' );
// Don't use CDN if the image is in admin, a feed or in a post preview.
if ( is_admin() || is_feed() || is_preview() || empty( $html ) || defined( 'DONOTROCKETOPTIMIZE' ) && DONOTROCKETOPTIMIZE ) {
return $html;
}
$zone = array( 'all', 'images' );
$cnames = get_rocket_cdn_cnames( $zone );
if ( $cnames ) {
$cnames = array_flip( $cnames );
$wp_content_dirname = wp_parse_url( content_url(), PHP_URL_PATH );
$custom_media_uploads_dirname = '';
$uploads_info = wp_upload_dir();
if ( ! empty( $uploads_info['baseurl'] ) ) {
$custom_media_uploads_dirname = '|' . trailingslashit( wp_parse_url( $uploads_info['baseurl'], PHP_URL_PATH ) );
}
// Get all images of the content.
preg_match_all( '#<img([^>]+?)src=([\'"\\\]*)([^\'"\s\\\>]+)([\'"\\\]*)([^>]*)>#i', $html, $images_match );
foreach ( $images_match[3] as $k => $image_url ) {
$parse_url = get_rocket_parse_url( $image_url );
$path = trim( $parse_url['path'] );
$host = $parse_url['host'];
if ( empty( $path ) || ! preg_match( '#(' . $wp_content_dirname . $custom_media_uploads_dirname . '|wp-includes)#', $path ) ) {
continue;
}
if ( isset( $cnames[ $host ] ) ) {
continue;
}
// Image path is relative, apply the host to it.
if ( empty( $host ) ) {
$image_url = home_url( '/' ) . ltrim( $image_url, '/' );
$host = rocket_extract_url_component( $image_url, PHP_URL_HOST );
}
// Check if the link isn't external.
if ( rocket_extract_url_component( home_url(), PHP_URL_HOST ) !== $host ) {
continue;
}
// Check if the URL isn't a DATA-URI.
if ( false !== strpos( $image_url, 'data:image' ) ) {
continue;
}
$html = str_replace(
$images_match[0][ $k ],
/**
* Filter the image HTML output with the CDN link
*
* @since 2.5.5
*
* @param array $html Output that will be printed.
*/
apply_filters(
'rocket_cdn_images_html',
sprintf(
'<img %1$s %2$s %3$s>',
trim( $images_match[1][ $k ] ),
'src=' . $images_match[2][ $k ] . get_rocket_cdn_url( $image_url, $zone ) . $images_match[4][ $k ],
trim( $images_match[5][ $k ] )
)
),
$html
);
}
}
return $html;
}
/**
* Replace URL by CDN of all inline styles containing url()
*
* @since 2.9
* @deprecated 3.4
* @author Remy Perona
*
* @param string $html HTML content of the page.
* @return string modified HTML content
*/
function rocket_cdn_inline_styles( $html ) {
_deprecated_function( __FUNCTION__ . '()', '3.4' );
if ( is_preview() || empty( $html ) || defined( 'DONOTROCKETOPTIMIZE' ) && DONOTROCKETOPTIMIZE ) {
return $html;
}
$zone = array(
'all',
'images',
);
$cnames = get_rocket_cdn_cnames( $zone );
if ( $cnames ) {
preg_match_all( '/url\((?![\'\"]?data)[\"\']?([^\)\"\']+)[\"\']?\)/i', $html, $matches );
if ( (bool) $matches ) {
foreach ( $matches[1] as $k => $url ) {
$url = str_replace( array( ' ', '\t', '\n', '\r', '\0', '\x0B', '"', "'", '"', ''' ), '', $url );
if ( '#' === substr( $url, 0, 1 ) ) {
continue;
}
$url = get_rocket_cdn_url( $url, $zone );
$property = str_replace( $matches[1][ $k ], $url, $matches[0][ $k ] );
$html = str_replace( $matches[0][ $k ], $property, $html );
}
}
}
return $html;
}
/**
* Replace URL by CDN for custom files
*
* @since 2.9
* @deprecated 3.4
* @author Remy Perona
*
* @param string $html HTML content of the page.
* @return string modified HTML content
*/
function rocket_cdn_custom_files( $html ) {
_deprecated_function( __FUNCTION__ . '()', '3.4' );
if ( is_preview() || empty( $html ) || defined( 'DONOTROCKETOPTIMIZE' ) && DONOTROCKETOPTIMIZE ) {
return $html;
}
$image_types = [
'jpg',
'jpeg',
'jpe',
'png',
'gif',
'webp',
'bmp',
'tiff',
];
$other_types = [
'mp3',
'ogg',
'mp4',
'm4v',
'avi',
'mov',
'flv',
'swf',
'webm',
'pdf',
'doc',
'docx',
'txt',
'zip',
'tar',
'bz2',
'tgz',
'rar',
];
$zones = array_filter( array_unique( get_rocket_option( 'cdn_zone', [] ) ) );
if ( empty( $zones ) ) {
return $html;
}
if ( ! in_array( 'all', $zones, true ) && ! in_array( 'images', $zones, true ) ) {
return $html;
}
$cdn_zones = [];
$file_types = [];
if ( in_array( 'images', $zones, true ) ) {
$cdn_zones[] = 'images';
$file_types = array_merge( $file_types, $image_types );
}
if ( in_array( 'all', $zones, true ) ) {
$cdn_zones[] = 'all';
$file_types = array_merge( $file_types, $image_types, $other_types );
}
$cnames = get_rocket_cdn_cnames( $cdn_zones );
if ( empty( $cnames ) ) {
return $html;
}
/**
* Filters the filetypes allowed for the CDN
*
* @since 2.9
* @author Remy Perona
*
* @param array $filetypes Array of file types.
*/
$file_types = apply_filters( 'rocket_cdn_custom_filetypes', $file_types );
$file_types = implode( '|', $file_types );
preg_match_all( '#<a[^>]+?href=[\'"]?([^"\'>]+\.(?:' . $file_types . '))[\'"]?[^>]*>#i', $html, $matches );
if ( ! (bool) $matches ) {
return $html;
}
foreach ( $matches[1] as $key => $url ) {
$url = trim( $url, " \t\n\r\0\x0B\"'" );
$url = get_rocket_cdn_url( $url, $cdn_zones );
$src = str_replace( $matches[1][ $key ], $url, $matches[0][ $key ] );
$html = str_replace( $matches[0][ $key ], $src, $html );
}
return $html;
}
/**
* Replace URL by CDN of all scripts and styles enqueues with WordPress functions
*
* @since 2.9 Only add protocol if $src is an absolute url
* @since 2.1
* @deprecated 3.4
*
* @param string $src URL of the file.
* @return string modified URL
*/
function rocket_cdn_enqueue( $src ) {
_deprecated_function( __FUNCTION__ . '()', '3.4' );
// Don't use CDN if in admin, in login page, in register page or in a post preview.
if ( is_admin() || is_preview() || in_array( $GLOBALS['pagenow'], array( 'wp-login.php', 'wp-register.php' ), true ) || defined( 'DONOTROCKETOPTIMIZE' ) && DONOTROCKETOPTIMIZE ) {
return $src;
}
if ( rocket_extract_url_component( $src, PHP_URL_HOST ) !== '' ) {
$src = rocket_add_url_protocol( $src );
}
$zone = array( 'all', 'css_and_js' );
// Add only CSS zone.
if ( 'style_loader_src' === current_filter() ) {
$zone[] = 'css';
}
// Add only JS zone.
if ( 'script_loader_src' === current_filter() ) {
$zone[] = 'js';
}
$cnames = get_rocket_cdn_cnames( $zone );
if ( $cnames ) {
// Check if the path isn't empty.
if ( trim( rocket_extract_url_component( $src, PHP_URL_PATH ), '/' ) !== '' ) {
$src = get_rocket_cdn_url( $src, $zone );
}
}
return $src;
}
/**
* Get all files we don't allow to get in CDN.
*
* @since 2.5
* @deprecated 3.4
*
* @return string A pipe-separated list of rejected files.
*/
function get_rocket_cdn_reject_files() {
_deprecated_function( __FUNCTION__ . '()', '3.4', '\WP_Rocket\Subscriber\CDN\CDN::get_excluded_files()' );
$files = get_rocket_option( 'cdn_reject_files', [] );
/**
* Filter the rejected files.
*
* @since 2.5
*
* @param array $files List of rejected files.
*/
$files = (array) apply_filters( 'rocket_cdn_reject_files', $files );
$files = array_filter( $files );
$files = array_flip( array_flip( $files ) );
return implode( '|', $files );
}
/**
* Conflict with Envira Gallery: changes the URL argument if using WP Rocket CDN and Envira
*
* @since 2.6.5
* @since 3.4
*
* @param array $args An array of arguments.
* @return array Updated array of arguments
*/
function rocket_cdn_resize_image_args_on_envira_gallery( $args ) {
_deprecated_function( __FUNCTION__ . '()', '3.4' );
if ( ! isset( $args['url'] ) || (int) get_rocket_option( 'cdn' ) === 0 ) {
return $args;
}
$cnames_host = array_flip( get_rocket_cnames_host() );
$url_host = rocket_extract_url_component( $args['url'], PHP_URL_HOST );
$home_host = rocket_extract_url_component( home_url(), PHP_URL_HOST );
if ( isset( $cnames_host[ $url_host ] ) ) {
$args['url'] = str_replace( $url_host, $home_host , $args['url'] );
}
return $args;
}
/**
* Conflict with Envira Gallery: changes the resized URL if using WP Rocket CDN and Envira
*
* @since 2.6.5
* @since 3.4
*
* @param string $url Resized image URL.
* @return string Resized image URL using the CDN URL
*/
function rocket_cdn_resized_url_on_envira_gallery( $url ) {
_deprecated_function( __FUNCTION__ . '()', '3.4' );
if ( (int) get_rocket_option( 'cdn' ) === 0 ) {
return $url;
}
$url = get_rocket_cdn_url( $url, array( 'all', 'images' ) );
return $url;
}
/**
* Apply CDN settings to Beaver Builder parallax.
*
* @since 3.2.1
* @deprecated 3.4
* @author Grégory Viguier
*
* @param array $attrs HTML attributes.
* @return array
*/
function rocket_beaver_builder_add_cdn_to_parallax( $attrs ) {
_deprecated_function( __FUNCTION__ . '()', '3.4' );
if ( ! empty( $attrs['data-parallax-image'] ) ) {
$attrs['data-parallax-image'] = get_rocket_cdn_url( $attrs['data-parallax-image'], [ 'all', 'images' ] );
}
return $attrs;
}
if ( class_exists( 'WR2X_Admin' ) ) :
/**
* Conflict with WP Retina x2: Apply CDN on srcset attribute.
*
* @since 2.9.1 Use global $wr2x_admin
* @since 2.5.5
* @deprecated 3.4
*
* @param string $url URL of the image.
* @return string Updated URL with CDN
*/
function rocket_cdn_on_images_from_wp_retina_x2( $url ) {
_deprecated_function( __FUNCTION__ . '()', '3.4' );
global $wr2x_admin;
if ( ! method_exists( $wr2x_admin, 'is_pro' ) || ! $wr2x_admin->is_pro() ) {
return $url;
}
$cdn_domain = get_option( 'wr2x_cdn_domain' );
if ( ! empty( $cdn_domain ) ) {
return $url;
}
return get_rocket_cdn_url( $url, array( 'all', 'images' ) );
}
endif;
/**
* Conflict with Avada theme and WP Rocket CDN
*
* @since 2.6.1
* @deprecated 3.4
*
* @param array $vars An array of variables.
* @param string $handle Name of the avada resource.
* @return array updated array of variables
*/
function rocket_fix_cdn_for_avada_theme( $vars, $handle ) {
_deprecated_function( __FUNCTION__ . '()', '3.4' );
if ( 'avada-dynamic' === $handle && get_rocket_option( 'cdn' ) ) {
$src = get_rocket_cdn_url( get_template_directory_uri() . '/assets/less/theme/dynamic.less' );
$vars['template-directory'] = sprintf( '~"%s"', dirname( dirname( dirname( dirname( $src ) ) ) ) );
$vars['lessurl'] = sprintf( '~"%s"', dirname( $src ) );
}
return $vars;
}
/**
* Conflict with Aqua Resizer & IrishMiss Framework: Apply CDN without blank src!!
*
* @since 2.5.8 Add compatibility with IrishMiss Framework
* @since 2.5.5
* @deprecated 3.4
*/
function rocket_cdn_on_aqua_resizer() {
_deprecated_function( __FUNCTION__ . '()', '3.4' );
if ( function_exists( 'aq_resize' ) || function_exists( 'miss_display_image' ) ) {
remove_filter( 'wp_get_attachment_url' , 'rocket_cdn_file', PHP_INT_MAX );
add_filter( 'rocket_lazyload_html', 'rocket_add_cdn_on_custom_attr' );
}
}
/**
* Conflict with Revolution Slider & Master Slider: Apply CDN on data-lazyload|data-src attribute.
*
* @since 2.5.5
* @deprecated 3.4
*/
function rocket_cdn_on_sliders_with_lazyload() {
_deprecated_function( __FUNCTION__ . '()', '3.4' );
if ( class_exists( 'RevSliderFront' ) || class_exists( 'Master_Slider' ) ) {
add_filter( 'rocket_cdn_images_html', 'rocket_add_cdn_on_custom_attr' );
}
}