forked from BoldGrid/w3-total-cache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinify_Plugin.php
1266 lines (1034 loc) · 34.1 KB
/
Minify_Plugin.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
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
namespace W3TC;
/**
* class Minify_Plugin
*/
class Minify_Plugin {
/**
* Minify reject reason
*
* @var string
*/
var $minify_reject_reason = '';
/**
* Error
*
* @var string
*/
var $error = '';
/**
* Array of replaced styles
*
* @var array
*/
var $replaced_styles = array();
/**
* Array of replaced scripts
*
* @var array
*/
var $replaced_scripts = array();
/**
* Helper object to use
*
* @var _W3_MinifyHelpers
*/
private $minify_helpers;
/**
* Config
*/
private $_config = null;
function __construct() {
$this->_config = Dispatcher::config();
}
/**
* Runs plugin
*/
function run() {
add_action( 'init', array( $this, 'init' ) );
add_filter( 'cron_schedules', array( $this, 'cron_schedules' ) );
add_filter( 'w3tc_admin_bar_menu',
array( $this, 'w3tc_admin_bar_menu' ) );
add_filter( 'w3tc_footer_comment', array(
$this, 'w3tc_footer_comment' ) );
if ( $this->_config->get_string( 'minify.engine' ) == 'file' ) {
add_action( 'w3_minify_cleanup', array(
$this,
'cleanup'
) );
}
add_filter( 'w3tc_pagecache_set_header',
array( $this, 'w3tc_pagecache_set_header' ), 20, 2 );
// usage statistics handling
add_action( 'w3tc_usage_statistics_of_request', array(
$this, 'w3tc_usage_statistics_of_request' ), 10, 1 );
add_filter( 'w3tc_usage_statistics_metrics', array(
$this, 'w3tc_usage_statistics_metrics' ) );
/**
* Start minify
*/
if ( $this->can_minify() ) {
Util_Bus::add_ob_callback( 'minify', array( $this, 'ob_callback' ) );
}
}
public function init() {
$url = Util_Environment::filename_to_url( W3TC_CACHE_MINIFY_DIR );
$parsed = parse_url( $url );
$prefix = '/' . trim( $parsed['path'], '/' ) . '/';
$request_uri = isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';
if ( substr( $request_uri, 0, strlen( $prefix ) ) == $prefix ) {
$w3_minify = Dispatcher::component( 'Minify_MinifiedFileRequestHandler' );
$filename = Util_Environment::remove_query_all( substr( $request_uri, strlen( $prefix ) ) );
$w3_minify->process( $filename );
exit();
}
if ( !empty( Util_Request::get_string( 'w3tc_minify' ) ) ) {
$w3_minify = Dispatcher::component( 'Minify_MinifiedFileRequestHandler' );
$w3_minify->process( Util_Request::get_string( 'w3tc_minify' ) );
exit();
}
}
/**
* Does disk cache cleanup
*
* @return void
*/
function cleanup() {
$a = Dispatcher::component( 'Minify_Plugin_Admin' );
$a->cleanup();
}
/**
* Cron schedules filter
*
* @param array $schedules
* @return array
*/
function cron_schedules( $schedules ) {
$gc = $this->_config->get_integer( 'minify.file.gc' );
return array_merge( $schedules, array(
'w3_minify_cleanup' => array(
'interval' => $gc,
'display' => sprintf( '[W3TC] Minify file GC (every %d seconds)', $gc )
)
) );
}
/**
* OB callback
*
* @param string $buffer
* @return string
*/
function ob_callback( $buffer ) {
$enable = Util_Content::is_html( $buffer ) &&
$this->can_minify2( $buffer );
$enable = apply_filters( 'w3tc_minify_enable', $enable );
if ( !$enable )
return $buffer;
$this->minify_helpers = new _W3_MinifyHelpers( $this->_config );
/**
* Replace script and style tags
*/
$js_enable = $this->_config->get_boolean( 'minify.js.enable' );
$css_enable = $this->_config->get_boolean( 'minify.css.enable' );
$html_enable = $this->_config->get_boolean( 'minify.html.enable' );
if ( function_exists( 'is_feed' ) && is_feed() ) {
$js_enable = false;
$css_enable = false;
}
$js_enable = apply_filters( 'w3tc_minify_js_enable', $js_enable );
$css_enable = apply_filters( 'w3tc_minify_css_enable', $css_enable );
$html_enable = apply_filters( 'w3tc_minify_html_enable', $html_enable );
$head_prepend = '';
$body_prepend = '';
$body_append = '';
$embed_extsrcjs = false;
$buffer = apply_filters( 'w3tc_minify_before', $buffer );
if ( $this->_config->get_boolean( 'minify.auto' ) ) {
if ( $js_enable ) {
$minifier = new Minify_AutoJs( $this->_config,
$buffer, $this->minify_helpers );
$buffer = $minifier->execute();
$this->replaced_scripts =
$minifier->get_debug_minified_urls();
}
if ( $css_enable ) {
$minifier = new Minify_AutoCss( $this->_config, $buffer,
$this->minify_helpers );
$buffer = $minifier->execute();
}
$buffer = apply_filters( 'w3tc_minify_processed', $buffer );
} else {
if ( $css_enable ) {
$style = $this->get_style_group( 'include' );
if ( $style['body'] ) {
if ( $this->_custom_location_does_not_exist( '/<!-- W3TC-include-css -->/', $buffer, $style['body'] ) )
$head_prepend .= $style['body'];
$this->remove_styles_group( $buffer, 'include' );
}
if ( $this->_config->getf_boolean( 'minify.css.http2push' ) ) {
$this->minify_helpers->http2_header_add( $style['url'],
'style' );
}
}
if ( $js_enable ) {
$embed_type = $this->_config->get_string( 'minify.js.header.embed_type' );
$http2push = $this->_config->getf_boolean( 'minify.js.http2push' );
$script = $this->get_script_group( 'include', $embed_type );
if ( $script['body'] ) {
$embed_extsrcjs = $embed_type == 'extsrc' || $embed_type == 'asyncsrc'?true:$embed_extsrcjs;
if ( $this->_custom_location_does_not_exist( '/<!-- W3TC-include-js-head -->/', $buffer, $script['body'] ) )
$head_prepend .= $script['body'];
$this->remove_scripts_group( $buffer, 'include' );
}
if ( $http2push ) {
$this->minify_helpers->http2_header_add( $script['url'],
'script' );
}
$embed_type = $this->_config->get_string( 'minify.js.body.embed_type' );
$script = $this->get_script_group( 'include-body', $embed_type );
if ( $script['body'] ) {
$embed_extsrcjs = $embed_type == 'extsrc' || $embed_type == 'asyncsrc'?true:$embed_extsrcjs;
if ( $this->_custom_location_does_not_exist( '/<!-- W3TC-include-js-body-start -->/', $buffer, $script['body'] ) )
$body_prepend .= $script['body'];
$this->remove_scripts_group( $buffer, 'include-body' );
}
if ( $http2push ) {
$this->minify_helpers->http2_header_add( $script['url'],
'script' );
}
$embed_type = $this->_config->get_string( 'minify.js.footer.embed_type' );
$script = $this->get_script_group( 'include-footer', $embed_type );
if ( $script['body'] ) {
$embed_extsrcjs = $embed_type == 'extsrc' || $embed_type == 'asyncsrc'?true:$embed_extsrcjs;
if ( $this->_custom_location_does_not_exist( '/<!-- W3TC-include-js-body-end -->/', $buffer, $script['body'] ) )
$body_append .= $script['body'];
$this->remove_scripts_group( $buffer, 'include-footer' );
}
if ( $http2push ) {
$this->minify_helpers->http2_header_add( $script['url'],
'script' );
}
}
}
if ( $head_prepend != '' ) {
$buffer = preg_replace( '~<head(\s+[^>]*)*>~Ui',
'\\0' . $head_prepend, $buffer, 1 );
}
if ( $body_prepend != '' ) {
$buffer = preg_replace( '~<body(\s+[^>]*)*>~Ui',
'\\0' . $body_prepend, $buffer, 1 );
}
if ( $body_append != '' ) {
$buffer = preg_replace( '~<\\/body>~',
$body_append . '\\0', $buffer, 1 );
}
if ( $embed_extsrcjs ) {
$script = "
<script>
" ."var extsrc=null;
".'(function(){function j(){if(b&&g){document.write=k;document.writeln=l;var f=document.createElement("span");f.innerHTML=b;g.appendChild(f);b=""}}function d(){j();for(var f=document.getElementsByTagName("script"),c=0;c<f.length;c++){var e=f[c],h=e.getAttribute("asyncsrc");if(h){e.setAttribute("asyncsrc","");var a=document.createElement("script");a.async=!0;a.src=h;document.getElementsByTagName("head")[0].appendChild(a)}if(h=e.getAttribute("extsrc")){e.setAttribute("extsrc","");g=document.createElement("span");e.parentNode.insertBefore(g,e);document.write=function(a){b+=a};document.writeln=function(a){b+=a;b+="\n"};a=document.createElement("script");a.async=!0;a.src=h;/msie/i.test(navigator.userAgent)&&!/opera/i.test(navigator.userAgent)?a.onreadystatechange=function(){("loaded"==this.readyState||"complete"==this.readyState)&&d()}:-1!=navigator.userAgent.indexOf("Firefox")||"onerror"in a?(a.onload=d,a.onerror=d):(a.onload=d,a.onreadystatechange=d);document.getElementsByTagName("head")[0].appendChild(a);return}}j();document.write=k;document.writeln=l;for(c=0;c<extsrc.complete.funcs.length;c++)extsrc.complete.funcs[c]()}function i(){arguments.callee.done||(arguments.callee.done=!0,d())}extsrc={complete:function(b){this.complete.funcs.push(b)}};extsrc.complete.funcs=[];var k=document.write,l=document.writeln,b="",g="";document.addEventListener&&document.addEventListener("DOMContentLoaded",i,!1);if(/WebKit/i.test(navigator.userAgent))var m=setInterval(function(){/loaded|complete/.test(document.readyState)&&(clearInterval(m),i())},10);window.onload=i})();' . "
</script>
";
$buffer = preg_replace( '~<head(\s+[^>]*)*>~Ui',
'\\0' . $script, $buffer, 1 );
}
/**
* Minify HTML/Feed
*/
if ( $html_enable ) {
try {
$buffer = $this->minify_html( $buffer );
} catch ( \Exception $exception ) {
$this->error = $exception->getMessage();
}
}
return $buffer;
}
public function w3tc_admin_bar_menu( $menu_items ) {
$menu_items['20210.minify'] = array(
'id' => 'w3tc_flush_minify',
'parent' => 'w3tc_flush',
'title' => __( 'Minify', 'w3-total-cache' ),
'href' => wp_nonce_url( admin_url(
'admin.php?page=w3tc_dashboard&w3tc_flush_minify' ),
'w3tc' )
);
return $menu_items;
}
function w3tc_footer_comment( $strings ) {
$strings[] = sprintf(
__( 'Minified using %s%s', 'w3-total-cache' ),
Cache::engine_name( $this->_config->get_string( 'minify.engine' ) ),
( $this->minify_reject_reason != ''
? sprintf( ' (%s)', $this->minify_reject_reason )
: '' ) );
if ( $this->_config->get_boolean( 'minify.debug' ) ) {
$strings[] = '';
$strings[] = 'Minify debug info:';
$strings[] = sprintf( "%s%s", str_pad( 'Theme: ', 20 ), $this->get_theme() );
$strings[] = sprintf( "%s%s", str_pad( 'Template: ', 20 ), $this->get_template() );
if ( $this->error ) {
$strings[] = sprintf( "%s%s", str_pad( 'Errors: ', 20 ), $this->error );
}
if ( count( $this->replaced_styles ) ) {
$strings[] = 'Replaced CSS files:';
foreach ( $this->replaced_styles as $index => $file ) {
$strings[] = sprintf( "%d. %s", $index + 1, Util_Content::escape_comment( $file ) );
}
}
if ( count( $this->replaced_scripts ) ) {
$strings[] = 'Replaced JavaScript files:';
foreach ( $this->replaced_scripts as $index => $file ) {
$strings[] = sprintf( "%d. %s\r\n", $index + 1, Util_Content::escape_comment( $file ) );
}
}
$strings[] = '';
}
return $strings;
}
/**
* Checks to see if pattern exists in source if so replaces it with the provided script
* and returns false. If pattern does not exists returns true.
*
* @param unknown $pattern
* @param unknown $source
* @param unknown $script
* @return bool
*/
function _custom_location_does_not_exist( $pattern, &$source, $script ) {
$count = 0;
$source = preg_replace( $pattern, $script, $source, 1, $count );
return $count==0;
}
/**
* Removes style tags from the source
*
* @param string $content
* @param array $files
* @return void
*/
function remove_styles( &$content, $files ) {
$regexps = array();
$home_url_regexp = Util_Environment::home_url_regexp();
$path = '';
if ( Util_Environment::is_wpmu() && !Util_Environment::is_wpmu_subdomain() )
$path = ltrim( Util_Environment::home_url_uri(), '/' );
foreach ( $files as $file ) {
if ( $path && strpos( $file, $path ) === 0 )
$file = substr( $file, strlen( $path ) );
$this->replaced_styles[] = $file;
if ( Util_Environment::is_url( $file ) && !preg_match( '~' . $home_url_regexp . '~i', $file ) ) {
// external CSS files
$regexps[] = Util_Environment::preg_quote( $file );
} else {
// local CSS files
$file = ltrim( $file, '/' );
if ( home_url() == site_url() && ltrim( Util_Environment::site_url_uri(), '/' ) && strpos( $file, ltrim( Util_Environment::site_url_uri(), '/' ) ) === 0 )
$file = str_replace( ltrim( Util_Environment::site_url_uri(), '/' ), '', $file );
$file = ltrim( preg_replace( '~' . $home_url_regexp . '~i', '', $file ), '/\\' );
$regexps[] = '(' . $home_url_regexp . ')?/?' . Util_Environment::preg_quote( $file );
}
}
foreach ( $regexps as $regexp ) {
$content = preg_replace( '~<link\s+[^<>]*href=["\']?' . $regexp . '["\']?[^<>]*/?>(.*</link>)?~Uis', '', $content );
$content = preg_replace( '~@import\s+(url\s*)?\(?["\']?\s*' . $regexp . '\s*["\']?\)?[^;]*;?~is', '', $content );
}
$content = preg_replace( '~<style[^<>]*>\s*</style>~', '', $content );
}
/**
* Remove script tags from the source
*
* @param string $content
* @param array $files
* @return void
*/
function remove_scripts( &$content, $files ) {
$regexps = array();
$home_url_regexp = Util_Environment::home_url_regexp();
$path = '';
if ( Util_Environment::is_wpmu() && !Util_Environment::is_wpmu_subdomain() ) {
$path = ltrim( Util_Environment::network_home_url_uri(), '/' );
}
foreach ( $files as $file ) {
if ( $path && strpos( $file, $path ) === 0 ) {
$file = substr( $file, strlen( $path ) );
}
$this->replaced_scripts[] = $file;
if ( Util_Environment::is_url( $file ) && !preg_match( '~' . $home_url_regexp . '~i', $file ) ) {
// external JS files
$regexps[] = Util_Environment::preg_quote( $file );
} else {
// local JS files
$file = ltrim( $file, '/' );
if ( home_url() == site_url() &&
ltrim( Util_Environment::site_url_uri(), '/' ) &&
strpos( $file, ltrim( Util_Environment::site_url_uri(), '/' ) ) === 0 ) {
$file = str_replace( ltrim( Util_Environment::site_url_uri(), '/' ), '', $file );
}
$file = ltrim( preg_replace( '~' . $home_url_regexp . '~i', '', $file ), '/\\' );
$regexps[] = '(' . $home_url_regexp . ')?/?' . Util_Environment::preg_quote( $file );
}
}
foreach ( $regexps as $regexp ) {
$content = preg_replace( '~<script\s+[^<>]*src=["\']?' . $regexp . '["\']?[^<>]*>\s*</script>~Uis', '', $content );
}
}
/**
* Removes style tag from the source for group
*
* @param string $content
* @param string $location
* @return void
*/
function remove_styles_group( &$content, $location ) {
$theme = $this->get_theme();
$template = $this->get_template();
$files = array();
$groups = $this->_config->get_array( 'minify.css.groups' );
if ( isset( $groups[$theme]['default'][$location]['files'] ) ) {
$files = (array) $groups[$theme]['default'][$location]['files'];
}
if ( $template != 'default' && isset( $groups[$theme][$template][$location]['files'] ) ) {
$files = array_merge( $files, (array) $groups[$theme][$template][$location]['files'] );
}
$this->remove_styles( $content, $files );
}
/**
* Removes script tags from the source for group
*
* @param string $content
* @param string $location
* @return void
*/
function remove_scripts_group( &$content, $location ) {
$theme = $this->get_theme();
$template = $this->get_template();
$files = array();
$groups = $this->_config->get_array( 'minify.js.groups' );
if ( isset( $groups[$theme]['default'][$location]['files'] ) ) {
$files = (array) $groups[$theme]['default'][$location]['files'];
}
if ( $template != 'default' && isset( $groups[$theme][$template][$location]['files'] ) ) {
$files = array_merge( $files, (array) $groups[$theme][$template][$location]['files'] );
}
$this->remove_scripts( $content, $files );
}
/**
* Minifies HTML
*
* @param string $html
* @return string
*/
function minify_html( $html ) {
$w3_minifier = Dispatcher::component( 'Minify_ContentMinifier' );
$ignored_comments = $this->_config->get_array( 'minify.html.comments.ignore' );
if ( count( $ignored_comments ) ) {
$ignored_comments_preserver = new \W3TCL\Minify\Minify_IgnoredCommentPreserver();
$ignored_comments_preserver->setIgnoredComments( $ignored_comments );
$html = $ignored_comments_preserver->search( $html );
}
if ( $this->_config->get_boolean( 'minify.html.inline.js' ) ) {
$js_engine = $this->_config->get_string( 'minify.js.engine' );
if ( !$w3_minifier->exists( $js_engine ) || !$w3_minifier->available( $js_engine ) ) {
$js_engine = 'js';
}
$js_minifier = $w3_minifier->get_minifier( $js_engine );
$js_options = $w3_minifier->get_options( $js_engine );
$w3_minifier->init( $js_engine );
$html = \W3TCL\Minify\Minify_Inline_JavaScript::minify( $html, $js_minifier, $js_options );
}
if ( $this->_config->get_boolean( 'minify.html.inline.css' ) ) {
$css_engine = $this->_config->get_string( 'minify.css.engine' );
if ( !$w3_minifier->exists( $css_engine ) || !$w3_minifier->available( $css_engine ) ) {
$css_engine = 'css';
}
$css_minifier = $w3_minifier->get_minifier( $css_engine );
$css_options = $w3_minifier->get_options( $css_engine );
$w3_minifier->init( $css_engine );
$html = \W3TCL\Minify\Minify_Inline_CSS::minify( $html, $css_minifier, $css_options );
}
$engine = $this->_config->get_string( 'minify.html.engine' );
if ( !$w3_minifier->exists( $engine ) || !$w3_minifier->available( $engine ) ) {
$engine = 'html';
}
if ( function_exists( 'is_feed' ) && is_feed() ) {
$engine .= 'xml';
}
$minifier = $w3_minifier->get_minifier( $engine );
$options = $w3_minifier->get_options( $engine );
$w3_minifier->init( $engine );
$html = call_user_func( $minifier, $html, $options );
if ( isset( $ignored_comments_preserver ) ) {
$html = $ignored_comments_preserver->replace( $html );
}
return $html;
}
/**
* Returns current theme
*
* @return string
*/
function get_theme() {
static $theme = null;
if ( $theme === null ) {
$theme = Util_Theme::get_theme_key( get_theme_root(), get_template(), get_stylesheet() );
}
return $theme;
}
/**
* Returns current template
*
* @return string
*/
function get_template() {
static $template = null;
if ( $template === null ) {
$template_file = 'index.php';
switch ( true ) {
case ( is_404() && ( $template_file = get_404_template() ) ):
case ( is_search() && ( $template_file = get_search_template() ) ):
case ( is_tax() && ( $template_file = get_taxonomy_template() ) ):
case ( is_front_page() && function_exists( 'get_front_page_template' ) && $template_file = get_front_page_template() ):
case ( is_home() && ( $template_file = get_home_template() ) ):
case ( is_attachment() && ( $template_file = get_attachment_template() ) ):
case ( is_single() && ( $template_file = get_single_template() ) ):
case ( is_page() && ( $template_file = get_page_template() ) ):
case ( is_category() && ( $template_file = get_category_template() ) ):
case ( is_tag() && ( $template_file = get_tag_template() ) ):
case ( is_author() && ( $template_file = get_author_template() ) ):
case ( is_date() && ( $template_file = get_date_template() ) ):
case ( is_archive() && ( $template_file = get_archive_template() ) ):
case ( is_paged() && ( $template_file = get_paged_template() ) ):
break;
default:
if ( function_exists( 'get_index_template' ) ) {
$template_file = get_index_template();
} else {
$template_file = 'index.php';
}
break;
}
$template = basename( $template_file, '.php' );
}
return $template;
}
/**
* Returns style tag
*
* @param string $url
* @param boolean $import
* @param boolean $use_style
* @return string
*/
function get_style( $url, $import = false, $use_style = true ) {
if ( $import && $use_style ) {
return "<style media=\"all\">@import url(\"" . $url . "\");</style>\r\n";
} elseif ( $import && !$use_style ) {
return "@import url(\"" . $url . "\");\r\n";
}else {
return "<link rel=\"stylesheet\" href=\"" . str_replace( '&', '&', $url ) . "\" media=\"all\" />\r\n";
}
}
/**
* Returns style tag for style group
*
* @param string $location
* @return array
*/
function get_style_group( $location ) {
$style = false;
$type = 'css';
$groups = $this->_config->get_array( 'minify.css.groups' );
$theme = $this->get_theme();
$template = $this->get_template();
if ( $template != 'default' && empty( $groups[$theme][$template][$location]['files'] ) ) {
$template = 'default';
}
$return = array(
'url' => null,
'body' => ''
);
if ( !empty( $groups[$theme][$template][$location]['files'] ) ) {
if ( $this->_config->get_boolean( 'minify.css.embed' ) ) {
$minify = Dispatcher::component( 'Minify_MinifiedFileRequestHandler' );
$minify_filename = $this->get_minify_manual_filename(
$theme, $template, $location, $type );
$m = $minify->process( $minify_filename, true );
if ( isset( $m['content'] ) )
$style = $m['content'];
else
$style = 'not set';
$return['body'] = "<style media=\"all\">$style</style>\r\n";
} else {
$return['url'] = $this->get_minify_manual_url( $theme, $template, $location, $type );
if ( $return['url'] ) {
$import = ( isset( $groups[$theme][$template][$location]['import'] ) ? (boolean) $groups[$theme][$template][$location]['import'] : false );
$return['body'] = $this->get_style( $return['url'], $import );
}
}
}
return $return;
}
/**
* Returns script tag for script group
*
* @param string $location
* @param string $embed_type
* @return array
*/
function get_script_group( $location, $embed_type = 'blocking' ) {
$script = false;
$fileType = 'js';
$theme = $this->get_theme();
$template = $this->get_template();
$groups = $this->_config->get_array( 'minify.js.groups' );
if ( $template != 'default' && empty( $groups[$theme][$template][$location]['files'] ) ) {
$template = 'default';
}
$return = array(
'url' => null,
'body' => ''
);
if ( !empty( $groups[$theme][$template][$location]['files'] ) ) {
$return['url'] = $this->get_minify_manual_url( $theme, $template, $location, $fileType );
if ( $return['url'] ) {
$return['body'] = $this->minify_helpers->generate_script_tag(
$return['url'], $embed_type );
}
}
return $return;
}
/**
* Returns style tag for custom files
*
* @return string
*/
function get_style_custom( $files, $embed_to_html = false ) {
return $this->minify_helpers->generate_css_style_tag(
$files, $embed_to_html );
}
/**
* Generates filename for minify manual resource
*/
function get_minify_manual_filename( $theme, $template, $location, $type ) {
$minify = Dispatcher::component( 'Minify_MinifiedFileRequestHandler' );
$id = $minify->get_id_group( $theme, $template, $location, $type );
if ( !$id )
return false;
return $theme . '.' . $template . '.' . $location . '.'. $id .
'.' . $type;
}
/**
* Generates URL for minify manual resource
*/
function get_minify_manual_url( $theme, $template, $location, $type ) {
return Minify_Core::minified_url( $this->get_minify_manual_filename(
$theme, $template, $location, $type ) );
}
/**
* Returns array of minify URLs
*
* @return array
*/
function get_urls() {
$files = array();
$js_groups = $this->_config->get_array( 'minify.js.groups' );
$css_groups = $this->_config->get_array( 'minify.css.groups' );
foreach ( $js_groups as $js_theme => $js_templates ) {
foreach ( $js_templates as $js_template => $js_locations ) {
foreach ( (array) $js_locations as $js_location => $js_config ) {
if ( !empty( $js_config['files'] ) ) {
$files[] = $this->get_minify_manual_url( $js_theme, $js_template, $js_location, 'js' );
}
}
}
}
foreach ( $css_groups as $css_theme => $css_templates ) {
foreach ( $css_templates as $css_template => $css_locations ) {
foreach ( (array) $css_locations as $css_location => $css_config ) {
if ( !empty( $css_config['files'] ) ) {
$files[] = $this->get_minify_manual_url( $css_theme, $css_template, $css_location, 'css' );
}
}
}
}
return $files;
}
/**
* Check if we can do minify logic
*
* @return boolean
*/
function can_minify() {
/**
* Skip if doint AJAX
*/
if ( defined( 'DOING_AJAX' ) ) {
$this->minify_reject_reason = 'Doing AJAX';
return false;
}
/**
* Skip if doing cron
*/
if ( defined( 'DOING_CRON' ) ) {
$this->minify_reject_reason = 'Doing cron';
return false;
}
/**
* Skip if APP request
*/
if ( defined( 'APP_REQUEST' ) ) {
$this->minify_reject_reason = 'Application request';
return false;
}
/**
* Skip if XMLRPC request
*/
if ( defined( 'XMLRPC_REQUEST' ) ) {
$this->minify_reject_reason = 'XMLRPC request';
return false;
}
/**
* Skip if Admin
*/
if ( defined( 'WP_ADMIN' ) ) {
$this->minify_reject_reason = 'wp-admin';
return false;
}
/**
* Check for WPMU's and WP's 3.0 short init
*/
if ( defined( 'SHORTINIT' ) && SHORTINIT ) {
$this->minify_reject_reason = 'Short init';
return false;
}
/**
* Check User agent
*/
if ( !$this->check_ua() ) {
$this->minify_reject_reason = 'User agent is rejected';
return false;
}
/**
* Check request URI
*/
if ( !$this->check_request_uri() ) {
$this->minify_reject_reason = 'Request URI is rejected';
return false;
}
/**
* Skip if user is logged in
*/
if ( $this->_config->get_boolean( 'minify.reject.logged' ) && !$this->check_logged_in() ) {
$this->minify_reject_reason = 'User is logged in';
return false;
}
return true;
}
/**
* Returns true if we can minify
*
* @param string $buffer
* @return string
*/
function can_minify2( $buffer ) {
/**
* Check for DONOTMINIFY constant
*/
if ( defined( 'DONOTMINIFY' ) && DONOTMINIFY ) {
$this->minify_reject_reason = 'DONOTMINIFY constant is defined';
return false;
}
/**
* Check feed minify
*/
if ( $this->_config->get_boolean( 'minify.html.reject.feed' ) && function_exists( 'is_feed' ) && is_feed() ) {
$this->minify_reject_reason = 'Feed is rejected';
return false;
}
return true;
}
/**
* Checks User Agent
*
* @return boolean
*/
function check_ua() {
$uas = array_merge( $this->_config->get_array( 'minify.reject.ua' ), array(
W3TC_POWERED_BY
) );
foreach ( $uas as $ua ) {
if ( !empty( $ua ) ) {
if ( stristr( isset( $_SERVER['HTTP_USER_AGENT'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ) : '', $ua ) !== false ) {
return false;
}
}
}
return true;
}
/**
* Check if user is logged in
*
* @return boolean
*/
function check_logged_in() {
foreach ( array_keys( $_COOKIE ) as $cookie_name ) {
if ( strpos( $cookie_name, 'wordpress_logged_in' ) === 0 )
return false;
}
return true;
}
/**
* Checks request URI
*
* @return boolean
*/
function check_request_uri() {
$auto_reject_uri = array(
'wp-login',
'wp-register'
);
foreach ( $auto_reject_uri as $uri ) {
if ( strstr( isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '', $uri ) !== false ) {
return false;
}
}
$reject_uri = $this->_config->get_array( 'minify.reject.uri' );
$reject_uri = array_map( array( '\W3TC\Util_Environment', 'parse_path' ), $reject_uri );
foreach ( $reject_uri as $expr ) {
$expr = trim( $expr );
$expr = str_replace( '~', '\~', $expr );
if ( '' !== $expr && preg_match( '~' . $expr . '~i', isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '' ) ) {
return false;
}
}
if ( Util_Request::get_string( 'wp_customize' ) )
return false;
return true;
}
public function w3tc_usage_statistics_of_request( $storage ) {
$o = Dispatcher::component( 'Minify_MinifiedFileRequestHandler' );
$o->w3tc_usage_statistics_of_request( $storage );
}
public function w3tc_usage_statistics_metrics( $metrics ) {
return array_merge( $metrics, array(
'minify_requests_total',
'minify_original_length_css', 'minify_output_length_css',