-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathoptimize.cls.php
More file actions
1298 lines (1110 loc) · 38.6 KB
/
optimize.cls.php
File metadata and controls
1298 lines (1110 loc) · 38.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
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// phpcs:ignoreFile
/**
* The optimize class.
*
* @since 1.2.2
*/
namespace LiteSpeed;
defined('WPINC') || exit();
class Optimize extends Base {
const LOG_TAG = '🎢';
const LIB_FILE_CSS_ASYNC = 'assets/js/css_async.min.js';
const LIB_FILE_WEBFONTLOADER = 'assets/js/webfontloader.min.js';
const LIB_FILE_JS_DELAY = 'assets/js/js_delay.min.js';
const ITEM_TIMESTAMP_PURGE_CSS = 'timestamp_purge_css';
const DUMMY_CSS_REGEX = "#<link [ \w='\"/]*id=['\"]litespeed-cache-dummy-css['\"] href=['\"].+assets/css/litespeed-dummy\.css[?\w.=-]*['\"][ \w='\"/]*>#isU";
private $content;
private $content_ori;
private $cfg_css_min;
private $cfg_css_comb;
private $cfg_js_min;
private $cfg_js_comb;
private $cfg_css_async;
private $cfg_js_delay_inc = array();
private $cfg_js_defer;
private $cfg_js_defer_exc = false;
private $cfg_ggfonts_async;
private $_conf_css_font_display;
private $cfg_ggfonts_rm;
private $dns_prefetch;
private $dns_preconnect;
private $_ggfonts_urls = array();
private $_ccss;
private $_ucss = false;
private $__optimizer;
private $html_foot = ''; // The html info append to <body>
private $html_head = ''; // The html info append to <head>
private $html_head_early = ''; // The html info prepend to top of head
private static $_var_i = 0;
private $_var_preserve_js = array();
private $_request_url;
/**
* Constructor
*
* @since 4.0
*/
public function __construct() {
self::debug('init');
$this->__optimizer = $this->cls('Optimizer');
}
/**
* Init optimizer
*
* @since 3.0
* @access protected
*/
public function init() {
$this->cfg_css_async = defined('LITESPEED_GUEST_OPTM') || $this->conf(self::O_OPTM_CSS_ASYNC);
if ($this->cfg_css_async) {
if (!$this->cls('Cloud')->activated()) {
self::debug('❌ CCSS set to OFF due to QC not activated');
$this->cfg_css_async = false;
}
if ((defined('LITESPEED_GUEST_OPTM') || ($this->conf(self::O_OPTM_UCSS) && $this->conf(self::O_OPTM_CSS_COMB))) && $this->conf(self::O_OPTM_UCSS_INLINE)) {
self::debug('⚠️ CCSS set to OFF due to UCSS Inline');
$this->cfg_css_async = false;
}
}
$this->cfg_js_defer = $this->conf(self::O_OPTM_JS_DEFER);
if (defined('LITESPEED_GUEST_OPTM')) {
$this->cfg_js_defer = 2;
}
if ($this->cfg_js_defer == 2) {
add_filter(
'litespeed_optm_cssjs',
function ( $con, $file_type ) {
if ($file_type == 'js') {
$con = str_replace('DOMContentLoaded', 'DOMContentLiteSpeedLoaded', $con);
// $con = str_replace( 'addEventListener("load"', 'addEventListener("litespeedLoad"', $con );
}
return $con;
},
20,
2
);
}
// To remove emoji from WP
if ($this->conf(self::O_OPTM_EMOJI_RM)) {
$this->_emoji_rm();
}
if ($this->conf(self::O_OPTM_QS_RM)) {
add_filter('style_loader_src', array( $this, 'remove_query_strings' ), 999);
add_filter('script_loader_src', array( $this, 'remove_query_strings' ), 999);
}
// GM JS exclude @since 4.1
if (defined('LITESPEED_GUEST_OPTM')) {
$this->cfg_js_defer_exc = apply_filters('litespeed_optm_gm_js_exc', $this->conf(self::O_OPTM_GM_JS_EXC));
} else {
/**
* Exclude js from deferred setting
*
* @since 1.5
*/
if ($this->cfg_js_defer) {
add_filter('litespeed_optm_js_defer_exc', array( $this->cls('Data'), 'load_js_defer_exc' ));
$this->cfg_js_defer_exc = apply_filters('litespeed_optm_js_defer_exc', $this->conf(self::O_OPTM_JS_DEFER_EXC));
$this->cfg_js_delay_inc = apply_filters('litespeed_optm_js_delay_inc', $this->conf(self::O_OPTM_JS_DELAY_INC));
}
}
// Add vary filter for Role Excludes @since 1.6
add_filter('litespeed_vary', array( $this, 'vary_add_role_exclude' ));
// DNS optm (Prefetch/Preconnect) @since 7.3
$this->_dns_optm_init();
add_filter('litespeed_buffer_finalize', array( $this, 'finalize' ), 20);
// Inject a dummy CSS file to control final optimized data location in <head>
wp_enqueue_style(Core::PLUGIN_NAME . '-dummy', LSWCP_PLUGIN_URL . 'assets/css/litespeed-dummy.css');
}
/**
* Exclude role from optimization filter
*
* @since 1.6
* @access public
*/
public function vary_add_role_exclude( $vary ) {
if ($this->cls('Conf')->in_optm_exc_roles()) {
$vary['role_exclude_optm'] = 1;
}
return $vary;
}
/**
* Remove emoji from WP
*
* @since 1.4
* @since 2.9.8 Changed to private
* @access private
*/
private function _emoji_rm() {
remove_action('wp_head', 'print_emoji_detection_script', 7);
remove_action('admin_print_scripts', 'print_emoji_detection_script');
remove_filter('the_content_feed', 'wp_staticize_emoji');
remove_filter('comment_text_rss', 'wp_staticize_emoji');
/**
* Added for better result
*
* @since 1.6.2.1
*/
remove_action('wp_print_styles', 'print_emoji_styles');
remove_action('admin_print_styles', 'print_emoji_styles');
remove_filter('wp_mail', 'wp_staticize_emoji_for_email');
}
/**
* Delete file-based cache folder
*
* @since 2.1
* @access public
*/
public function rm_cache_folder( $subsite_id = false ) {
if ($subsite_id) {
file_exists(LITESPEED_STATIC_DIR . '/css/' . $subsite_id) && File::rrmdir(LITESPEED_STATIC_DIR . '/css/' . $subsite_id);
file_exists(LITESPEED_STATIC_DIR . '/js/' . $subsite_id) && File::rrmdir(LITESPEED_STATIC_DIR . '/js/' . $subsite_id);
return;
}
file_exists(LITESPEED_STATIC_DIR . '/css') && File::rrmdir(LITESPEED_STATIC_DIR . '/css');
file_exists(LITESPEED_STATIC_DIR . '/js') && File::rrmdir(LITESPEED_STATIC_DIR . '/js');
}
/**
* Remove QS
*
* @since 1.3
* @access public
*/
public function remove_query_strings( $src ) {
if (strpos($src, '_litespeed_rm_qs=0') || strpos($src, '/recaptcha')) {
return $src;
}
if (!Utility::is_internal_file($src)) {
return $src;
}
if (strpos($src, '.js?') !== false || strpos($src, '.css?') !== false) {
$src = preg_replace('/\?.*/', '', $src);
}
return $src;
}
/**
* Run optimize process
* NOTE: As this is after cache finalized, can NOT set any cache control anymore
*
* @since 1.2.2
* @access public
* @return string The content that is after optimization
*/
public function finalize( $content ) {
$content = $this->_finalize($content);
// Fallback to replace dummy css placeholder
if (false !== preg_match(self::DUMMY_CSS_REGEX, $content)) {
self::debug('Fallback to drop dummy CSS');
$content = preg_replace( self::DUMMY_CSS_REGEX, '', $content );
}
return $content;
}
private function _finalize( $content ) {
if (defined('LITESPEED_NO_PAGEOPTM')) {
self::debug2('bypass: NO_PAGEOPTM const');
return $content;
}
if (!defined('LITESPEED_IS_HTML')) {
self::debug('bypass: Not frontend HTML type');
return $content;
}
if (!defined('LITESPEED_GUEST_OPTM')) {
if (!Control::is_cacheable()) {
self::debug('bypass: Not cacheable');
return $content;
}
// Check if hit URI excludes
add_filter('litespeed_optm_uri_exc', array( $this->cls('Data'), 'load_optm_uri_exc' ));
$excludes = apply_filters('litespeed_optm_uri_exc', $this->conf(self::O_OPTM_EXC));
$result = Utility::str_hit_array($_SERVER['REQUEST_URI'], $excludes);
if ($result) {
self::debug('bypass: hit URI Excludes setting: ' . $result);
return $content;
}
}
self::debug('start');
$this->content_ori = $this->content = $content;
$this->_optimize();
return $this->content;
}
/**
* Optimize css src
*
* @since 1.2.2
* @access private
*/
private function _optimize() {
global $wp;
// get current request url
$permalink_structure = get_option( 'permalink_structure' );
if ( ! empty( $permalink_structure ) ) {
$this->_request_url = trailingslashit( home_url( $wp->request ) );
} else {
$qs_add = $wp->query_string ? '?' . (string) $wp->query_string : '' ;
$this->_request_url = home_url( $wp->request ) . $qs_add;
}
$this->cfg_css_min = defined('LITESPEED_GUEST_OPTM') || $this->conf(self::O_OPTM_CSS_MIN);
$this->cfg_css_comb = defined('LITESPEED_GUEST_OPTM') || $this->conf(self::O_OPTM_CSS_COMB);
$this->cfg_js_min = defined('LITESPEED_GUEST_OPTM') || $this->conf(self::O_OPTM_JS_MIN);
$this->cfg_js_comb = defined('LITESPEED_GUEST_OPTM') || $this->conf(self::O_OPTM_JS_COMB);
$this->cfg_ggfonts_rm = defined('LITESPEED_GUEST_OPTM') || $this->conf(self::O_OPTM_GGFONTS_RM);
$this->cfg_ggfonts_async = !defined('LITESPEED_GUEST_OPTM') && $this->conf(self::O_OPTM_GGFONTS_ASYNC); // forced rm already
$this->_conf_css_font_display = !defined('LITESPEED_GUEST_OPTM') && $this->conf(self::O_OPTM_CSS_FONT_DISPLAY);
if (!$this->cls('Router')->can_optm()) {
self::debug('bypass: admin/feed/preview');
return;
}
if ($this->cfg_css_async) {
$this->_ccss = $this->cls('CSS')->prepare_ccss();
if (!$this->_ccss) {
self::debug('❌ CCSS set to OFF due to CCSS not generated yet');
$this->cfg_css_async = false;
} elseif (strpos($this->_ccss, '<style id="litespeed-ccss" data-error') === 0) {
self::debug('❌ CCSS set to OFF due to CCSS failed to generate');
$this->cfg_css_async = false;
}
}
do_action('litespeed_optm');
// Parse css from content
$src_list = false;
if ($this->cfg_css_min || $this->cfg_css_comb || $this->cfg_ggfonts_rm || $this->cfg_css_async || $this->cfg_ggfonts_async || $this->_conf_css_font_display) {
add_filter('litespeed_optimize_css_excludes', array( $this->cls('Data'), 'load_css_exc' ));
list($src_list, $html_list) = $this->_parse_css();
}
// css optimizer
if ($this->cfg_css_min || $this->cfg_css_comb) {
if ($src_list) {
// IF combine
if ($this->cfg_css_comb) {
// Check if has inline UCSS enabled or not
if ((defined('LITESPEED_GUEST_OPTM') || $this->conf(self::O_OPTM_UCSS)) && $this->conf(self::O_OPTM_UCSS_INLINE)) {
$filename = $this->cls('UCSS')->load($this->_request_url, true);
if ($filename) {
$filepath_prefix = $this->_build_filepath_prefix('ucss');
$this->_ucss = File::read(LITESPEED_STATIC_DIR . $filepath_prefix . $filename);
// Drop all css
$this->content = str_replace($html_list, '', $this->content);
}
}
if (!$this->_ucss) {
$url = $this->_build_hash_url($src_list);
if ($url) {
// Handle css async load
if ($this->cfg_css_async) {
$this->html_head .=
'<link rel="preload" data-asynced="1" data-optimized="2" as="style" onload="this.onload=null;this.rel=\'stylesheet\'" href="' .
Str::trim_quotes($url) .
'" />'; // todo: How to use " in attr wrapper "
} else {
$this->html_head .= '<link data-optimized="2" rel="stylesheet" href="' . Str::trim_quotes($url) . '" />'; // use 2 as combined
}
// Move all css to top
$this->content = str_replace($html_list, '', $this->content);
}
}
}
// Only minify
elseif ($this->cfg_css_min) {
// will handle async css load inside
$this->_src_queue_handler($src_list, $html_list);
}
// Only HTTP2 push
else {
foreach ($src_list as $src_info) {
if (!empty($src_info['inl'])) {
continue;
}
}
}
}
}
// Handle css lazy load if not handled async loaded yet
if ($this->cfg_css_async && !$this->cfg_css_min && !$this->cfg_css_comb) {
// async html
$html_list_async = $this->_async_css_list($html_list, $src_list);
// Replace async css
$this->content = str_replace($html_list, $html_list_async, $this->content);
}
// Parse js from buffer as needed
$src_list = false;
if ($this->cfg_js_min || $this->cfg_js_comb || $this->cfg_js_defer || $this->cfg_js_delay_inc) {
add_filter('litespeed_optimize_js_excludes', array( $this->cls('Data'), 'load_js_exc' ));
list($src_list, $html_list) = $this->_parse_js();
}
// js optimizer
if ($src_list) {
// IF combine
if ($this->cfg_js_comb) {
$url = $this->_build_hash_url($src_list, 'js');
if ($url) {
$this->html_foot .= $this->_build_js_tag($url);
// Will move all JS to bottom combined one
$this->content = str_replace($html_list, '', $this->content);
}
}
// Only minify
elseif ($this->cfg_js_min) {
// Will handle js defer inside
$this->_src_queue_handler($src_list, $html_list, 'js');
}
// Only HTTP2 push and Defer
else {
foreach ($src_list as $k => $src_info) {
// Inline JS
if (!empty($src_info['inl'])) {
if ($this->cfg_js_defer) {
$attrs = !empty($src_info['attrs']) ? $src_info['attrs'] : '';
$deferred = $this->_js_inline_defer($src_info['src'], $attrs);
if ($deferred) {
$this->content = str_replace($html_list[$k], $deferred, $this->content);
}
}
}
// JS files
elseif ($this->cfg_js_defer) {
$deferred = $this->_js_defer($html_list[$k], $src_info['src']);
if ($deferred) {
$this->content = str_replace($html_list[$k], $deferred, $this->content);
}
} elseif ($this->cfg_js_delay_inc) {
$deferred = $this->_js_delay($html_list[$k], $src_info['src']);
if ($deferred) {
$this->content = str_replace($html_list[$k], $deferred, $this->content);
}
}
}
}
}
// Append JS inline var for preserved ESI
// Shouldn't give any optm (defer/delay) @since 4.4
if ($this->_var_preserve_js) {
$this->html_head .= '<script>var ' . implode(',', $this->_var_preserve_js) . ';</script>';
self::debug2('Inline JS defer vars', $this->_var_preserve_js);
}
// Append async compatibility lib to head
if ($this->cfg_css_async) {
// Inline css async lib
if ($this->conf(self::O_OPTM_CSS_ASYNC_INLINE)) {
$this->html_head .= $this->_build_js_inline(File::read(LSCWP_DIR . self::LIB_FILE_CSS_ASYNC), true);
} else {
$css_async_lib_url = LSWCP_PLUGIN_URL . self::LIB_FILE_CSS_ASYNC;
$this->html_head .= $this->_build_js_tag($css_async_lib_url); // Don't exclude it from defer for now
}
}
/**
* Handle google fonts async
* This will result in a JS snippet in head, so need to put it in the end to avoid being replaced by JS parser
*/
$this->_async_ggfonts();
/**
* Font display optm
*
* @since 3.0
*/
$this->_font_optm();
// Inject JS Delay lib
$this->_maybe_js_delay();
/**
* HTML Lazyload
*/
if ($this->conf(self::O_OPTM_HTML_LAZY)) {
$this->html_head = $this->cls('CSS')->prepare_html_lazy() . $this->html_head;
}
// Maybe prepend inline UCSS
if ($this->_ucss) {
$this->html_head = '<style id="litespeed-ucss">' . $this->_ucss . '</style>' . $this->html_head;
}
// Check if there is any critical css rules setting
if ($this->cfg_css_async && $this->_ccss) {
$this->html_head = $this->_ccss . $this->html_head;
}
// Replace html head part
$this->html_head_early = apply_filters('litespeed_optm_html_head_early', $this->html_head_early);
if ($this->html_head_early) {
// Put header content to be after charset
if (false !== strpos($this->content, '<meta charset')) {
self::debug('Put early optm data to be after <meta charset>');
$this->content = preg_replace('#<meta charset([^>]*)>#isU', '<meta charset$1>' . $this->html_head_early, $this->content, 1);
} else {
self::debug('Put early optm data to be right after <head>');
$this->content = preg_replace('#<head([^>]*)>#isU', '<head$1>' . $this->html_head_early, $this->content, 1);
}
}
$this->html_head = apply_filters('litespeed_optm_html_head', $this->html_head);
if ($this->html_head) {
if (apply_filters('litespeed_optm_html_after_head', false)) {
$this->content = str_replace('</head>', $this->html_head . '</head>', $this->content);
} else {
// Put header content to dummy css position
if (false !== preg_match(self::DUMMY_CSS_REGEX, $this->content)) {
self::debug('Put optm data to dummy css location');
$this->content = preg_replace( self::DUMMY_CSS_REGEX, $this->html_head, $this->content );
}
// Fallback: try to be after charset
elseif (strpos($this->content, '<meta charset') !== false) {
self::debug('Put optm data to be after <meta charset>');
$this->content = preg_replace('#<meta charset([^>]*)>#isU', '<meta charset$1>' . $this->html_head, $this->content, 1);
} else {
self::debug('Put optm data to be after <head>');
$this->content = preg_replace('#<head([^>]*)>#isU', '<head$1>' . $this->html_head, $this->content, 1);
}
}
}
// Replace html foot part
$this->html_foot = apply_filters('litespeed_optm_html_foot', $this->html_foot);
if ($this->html_foot) {
$this->content = str_replace('</body>', $this->html_foot . '</body>', $this->content);
}
// Drop noscript if enabled
if ($this->conf(self::O_OPTM_NOSCRIPT_RM)) {
// $this->content = preg_replace( '#<noscript>.*</noscript>#isU', '', $this->content );
}
// Inline font-face optimize
$this->content = $this->__optimizer->optm_font_face( $this->content );
// HTML minify
if (defined('LITESPEED_GUEST_OPTM') || $this->conf(self::O_OPTM_HTML_MIN)) {
$this->content = $this->__optimizer->html_min($this->content);
}
}
/**
* Build a full JS tag
*
* @since 4.0
*/
private function _build_js_tag( $src ) {
if ($this->cfg_js_defer === 2 || Utility::str_hit_array($src, $this->cfg_js_delay_inc)) {
return '<script data-optimized="1" type="litespeed/javascript" data-src="' . Str::trim_quotes($src) . '"></script>';
}
if ($this->cfg_js_defer) {
return '<script data-optimized="1" src="' . Str::trim_quotes($src) . '" defer></script>';
}
return '<script data-optimized="1" src="' . Str::trim_quotes($src) . '"></script>';
}
/**
* Build a full inline JS snippet
*
* @since 4.0
*/
private function _build_js_inline( $script, $minified = false ) {
if ($this->cfg_js_defer) {
$deferred = $this->_js_inline_defer($script, false, $minified);
if ($deferred) {
return $deferred;
}
}
return '<script>' . $script . '</script>';
}
/**
* Load JS delay lib
*
* @since 4.0
*/
private function _maybe_js_delay() {
if ($this->cfg_js_defer !== 2 && !$this->cfg_js_delay_inc) {
return;
}
if (!defined('LITESPEED_JS_DELAY_LIB_LOADED')) {
define('LITESPEED_JS_DELAY_LIB_LOADED', true);
$this->html_foot .= '<script>' . File::read(LSCWP_DIR . self::LIB_FILE_JS_DELAY) . '</script>';
}
}
/**
* Google font async
*
* @since 2.7.3
* @access private
*/
private function _async_ggfonts() {
if (!$this->cfg_ggfonts_async || !$this->_ggfonts_urls) {
return;
}
self::debug2('google fonts async found: ', $this->_ggfonts_urls);
$this->html_head_early .= '<link rel="preconnect" href="https://fonts.gstatic.com/" crossorigin />';
/**
* Append fonts
*
* Could be multiple fonts
*
* <link rel='stylesheet' href='//fonts.googleapis.com/css?family=Open+Sans%3A400%2C600%2C700%2C800%2C300&ver=4.9.8' type='text/css' media='all' />
* <link rel='stylesheet' href='//fonts.googleapis.com/css?family=PT+Sans%3A400%2C700%7CPT+Sans+Narrow%3A400%7CMontserrat%3A600&subset=latin&ver=4.9.8' type='text/css' media='all' />
* -> family: PT Sans:400,700|PT Sans Narrow:400|Montserrat:600
* <link rel='stylesheet' href='https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,300,300italic,400italic,600,700,900&subset=latin%2Clatin-ext' />
*/
$script = 'WebFontConfig={google:{families:[';
$families = array();
foreach ($this->_ggfonts_urls as $v) {
$qs = wp_specialchars_decode($v);
$qs = urldecode($qs);
$qs = parse_url($qs, PHP_URL_QUERY);
parse_str($qs, $qs);
if (empty($qs['family'])) {
self::debug('ERR ggfonts failed to find family: ' . $v);
continue;
}
$subset = empty($qs['subset']) ? '' : ':' . $qs['subset'];
foreach (array_filter(explode('|', $qs['family'])) as $v2) {
$families[] = Str::trim_quotes($v2 . $subset);
}
}
$script .= '"' . implode('","', $families) . ($this->_conf_css_font_display ? '&display=swap' : '') . '"';
$script .= ']}};';
// if webfontloader lib was loaded before WebFontConfig variable, call WebFont.load
$script .= 'if ( typeof WebFont === "object" && typeof WebFont.load === "function" ) { WebFont.load( WebFontConfig ); }';
$html = $this->_build_js_inline($script);
// https://cdnjs.cloudflare.com/ajax/libs/webfont/1.6.28/webfontloader.js
$webfont_lib_url = LSWCP_PLUGIN_URL . self::LIB_FILE_WEBFONTLOADER;
// default async, if js defer set use defer
$html .= $this->_build_js_tag($webfont_lib_url);
// Put this in the very beginning for preconnect
$this->html_head = $html . $this->html_head;
}
/**
* Font optm
*
* @since 3.0
* @access private
*/
private function _font_optm() {
if (!$this->_conf_css_font_display || !$this->_ggfonts_urls) {
return;
}
self::debug2('google fonts optm ', $this->_ggfonts_urls);
foreach ($this->_ggfonts_urls as $v) {
if (strpos($v, 'display=')) {
continue;
}
$this->html_head = str_replace($v, $v . '&display=swap', $this->html_head);
$this->html_foot = str_replace($v, $v . '&display=swap', $this->html_foot);
$this->content = str_replace($v, $v . '&display=swap', $this->content);
}
}
/**
* Prefetch DNS
*
* @since 1.7.1 DNS prefetch
* @since 5.6.1 DNS preconnect
* @access private
*/
private function _dns_optm_init() {
// Widely enable link DNS prefetch
if (defined('LITESPEED_GUEST_OPTM') || $this->conf(self::O_OPTM_DNS_PREFETCH_CTRL)) {
@header('X-DNS-Prefetch-Control: on');
}
$this->dns_prefetch = $this->conf(self::O_OPTM_DNS_PREFETCH);
$this->dns_preconnect = $this->conf(self::O_OPTM_DNS_PRECONNECT);
if (!$this->dns_prefetch && !$this->dns_preconnect) {
return;
}
if (function_exists('wp_resource_hints')) {
add_filter('wp_resource_hints', array( $this, 'dns_optm_filter' ), 10, 2);
} else {
add_action('litespeed_optm', array( $this, 'dns_optm_output' ));
}
}
/**
* DNS optm hook for WP
*
* @since 1.7.1
* @access public
*/
public function dns_optm_filter( $urls, $relation_type ) {
if ('dns-prefetch' === $relation_type) {
foreach ($this->dns_prefetch as $v) {
if ($v) {
$urls[] = $v;
}
}
}
if ('preconnect' === $relation_type) {
foreach ($this->dns_preconnect as $v) {
if ($v) {
$urls[] = $v;
}
}
}
return $urls;
}
/**
* DNS optm output directly
*
* @since 1.7.1 DNS prefetch
* @since 5.6.1 DNS preconnect
* @access public
*/
public function dns_optm_output() {
foreach ($this->dns_prefetch as $v) {
if ($v) {
$this->html_head_early .= '<link rel="dns-prefetch" href="' . Str::trim_quotes($v) . '" />';
}
}
foreach ($this->dns_preconnect as $v) {
if ($v) {
$this->html_head_early .= '<link rel="preconnect" href="' . Str::trim_quotes($v) . '" crossorigin />';
}
}
}
/**
* Run minify with src queue list
*
* @since 1.2.2
* @access private
*/
private function _src_queue_handler( $src_list, $html_list, $file_type = 'css' ) {
$html_list_ori = $html_list;
$can_webp = $this->cls('Media')->webp_support();
$tag = $file_type == 'css' ? 'link' : 'script';
foreach ($src_list as $key => $src_info) {
// Minify inline CSS/JS
if (!empty($src_info['inl'])) {
if ($file_type == 'css') {
$code = Optimizer::minify_css($src_info['src']);
$can_webp && ($code = $this->cls('Media')->replace_background_webp($code));
$snippet = str_replace($src_info['src'], $code, $html_list[$key]);
} else {
// Inline defer JS
if ($this->cfg_js_defer) {
$attrs = !empty($src_info['attrs']) ? $src_info['attrs'] : '';
$snippet = $this->_js_inline_defer($src_info['src'], $attrs) ?: $html_list[$key];
} else {
$code = Optimizer::minify_js($src_info['src']);
$snippet = str_replace($src_info['src'], $code, $html_list[$key]);
}
}
}
// CSS/JS files
else {
$url = $this->_build_single_hash_url($src_info['src'], $file_type);
if ($url) {
$snippet = str_replace($src_info['src'], $url, $html_list[$key]);
}
// Handle css async load
if ($file_type == 'css' && $this->cfg_css_async) {
$snippet = $this->_async_css($snippet);
}
// Handle js defer
if ($file_type === 'js' && $this->cfg_js_defer) {
$snippet = $this->_js_defer($snippet, $src_info['src']) ?: $snippet;
}
}
$snippet = str_replace("<$tag ", '<' . $tag . ' data-optimized="1" ', $snippet);
$html_list[$key] = $snippet;
}
$this->content = str_replace($html_list_ori, $html_list, $this->content);
}
/**
* Build a single URL mapped filename (This will not save in DB)
*
* @since 4.0
*/
private function _build_single_hash_url( $src, $file_type = 'css' ) {
$content = $this->__optimizer->load_file($src, $file_type);
$is_min = $this->__optimizer->is_min($src);
$content = $this->__optimizer->optm_snippet($content, $file_type, !$is_min, $src);
$filepath_prefix = $this->_build_filepath_prefix($file_type);
// Save to file
$filename = $filepath_prefix . md5($this->remove_query_strings($src)) . '.' . $file_type;
$static_file = LITESPEED_STATIC_DIR . $filename;
File::save($static_file, $content, true);
// QS is required as $src may contains version info
$qs_hash = substr(md5($src), -5);
return LITESPEED_STATIC_URL . "$filename?ver=$qs_hash";
}
/**
* Generate full URL path with hash for a list of src
*
* @since 1.2.2
* @access private
*/
private function _build_hash_url( $src_list, $file_type = 'css' ) {
// $url_sensitive = $this->conf( self::O_OPTM_CSS_UNIQUE ) && $file_type == 'css'; // If need to keep unique CSS per URI
// Replace preserved ESI (before generating hash)
if ($file_type == 'js') {
foreach ($src_list as $k => $v) {
if (empty($v['inl'])) {
continue;
}
$src_list[$k]['src'] = $this->_preserve_esi($v['src']);
}
}
$minify = $file_type === 'css' ? $this->cfg_css_min : $this->cfg_js_min;
$filename_info = $this->__optimizer->serve($this->_request_url, $file_type, $minify, $src_list);
if (!$filename_info) {
return false; // Failed to generate
}
list($filename, $type) = $filename_info;
// Add cache tag in case later file deleted to avoid lscache served stale non-existed files @since 4.4.1
Tag::add(Tag::TYPE_MIN . '.' . $filename);
$qs_hash = substr(md5(self::get_option(self::ITEM_TIMESTAMP_PURGE_CSS)), -5);
// As filename is already related to filecon md5, no need QS anymore
$filepath_prefix = $this->_build_filepath_prefix($type);
return LITESPEED_STATIC_URL . $filepath_prefix . $filename . '?ver=' . $qs_hash;
}
/**
* Parse js src
*
* @since 1.2.2
* @access private
*/
private function _parse_js() {
$excludes = apply_filters('litespeed_optimize_js_excludes', $this->conf(self::O_OPTM_JS_EXC));
$combine_ext_inl = $this->conf(self::O_OPTM_JS_COMB_EXT_INL);
if (!apply_filters('litespeed_optm_js_comb_ext_inl', true)) {
self::debug2('js_comb_ext_inl bypassed via litespeed_optm_js_comb_ext_inl filter');
$combine_ext_inl = false;
}
$src_list = array();
$html_list = array();
// V7 added: (?:\r\n?|\n?) to fix replacement leaving empty new line
$content = preg_replace('#<!--.*-->(?:\r\n?|\n?)#sU', '', $this->content);
preg_match_all('#<script([^>]*)>(.*)</script>(?:\r\n?|\n?)#isU', $content, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
$attrs = empty($match[1]) ? array() : Utility::parse_attr($match[1]);
if (isset($attrs['data-optimized'])) {
continue;
}
if (!empty($attrs['data-no-optimize'])) {
continue;
}
if (!empty($attrs['data-cfasync']) && $attrs['data-cfasync'] === 'false') {
continue;
}
if (!empty($attrs['type']) && $attrs['type'] != 'text/javascript') {
continue;
}
// to avoid multiple replacement
if (in_array($match[0], $html_list)) {
continue;
}
$this_src_arr = array();
// JS files
if (!empty($attrs['src'])) {
// Exclude check
$js_excluded = Utility::str_hit_array($attrs['src'], $excludes);
$is_internal = Utility::is_internal_file($attrs['src']);
$is_file = substr($attrs['src'], 0, 5) != 'data:';
$ext_excluded = !$combine_ext_inl && !$is_internal;
if ($js_excluded || $ext_excluded || !$is_file) {
// Maybe defer
if ($this->cfg_js_defer) {
$deferred = $this->_js_defer($match[0], $attrs['src']);
if ($deferred) {
$this->content = str_replace($match[0], $deferred, $this->content);
}
}
self::debug2('_parse_js bypassed due to ' . ($js_excluded ? 'js files excluded [hit] ' . $js_excluded : 'external js'));
continue;
}
if (strpos($attrs['src'], '/localres/') !== false) {
continue;
}
if (strpos($attrs['src'], 'instant_click') !== false) {
continue;
}
$this_src_arr['src'] = $attrs['src'];
}
// Inline JS
elseif (!empty($match[2])) {
// self::debug( '🌹🌹🌹 ' . $match[2] . '🌹' );
// Exclude check
$js_excluded = Utility::str_hit_array($match[2], $excludes);
if ($js_excluded || !$combine_ext_inl) {
// Maybe defer
if ($this->cfg_js_defer) {
$deferred = $this->_js_inline_defer($match[2], $match[1]);
if ($deferred) {
$this->content = str_replace($match[0], $deferred, $this->content);
}
}
self::debug2('_parse_js bypassed due to ' . ($js_excluded ? 'js excluded [hit] ' . $js_excluded : 'inline js'));
continue;
}
$this_src_arr['inl'] = true;
$this_src_arr['src'] = $match[2];
if ($match[1]) {
$this_src_arr['attrs'] = $match[1];
}
} else {
// Compatibility to those who changed src to data-src already
self::debug2('No JS src or inline JS content');
continue;
}
$src_list[] = $this_src_arr;
$html_list[] = $match[0];
}
return array( $src_list, $html_list );
}
/**
* Inline JS defer
*
* @since 3.0
* @access private
*/
private function _js_inline_defer( $con, $attrs = false, $minified = false ) {
if (strpos($attrs, 'data-no-defer') !== false) {
self::debug2('bypass: attr api data-no-defer');
return false;
}
$hit = Utility::str_hit_array($con, $this->cfg_js_defer_exc);
if ($hit) {
self::debug2('inline js defer excluded [setting] ' . $hit);
return false;
}
$con = trim($con);
// Minify JS first
if (!$minified) {
// && $this->cfg_js_defer !== 2
$con = Optimizer::minify_js($con);
}
if (!$con) {
return false;
}
// Check if the content contains ESI nonce or not
$con = $this->_preserve_esi($con);