fvm_1.php
67.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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
<?php
/*
Plugin Name: Fast Velocity Minify
Plugin URI: http://fastvelocity.com
Description: Improve your speed score on GTmetrix, Pingdom Tools and Google PageSpeed Insights by merging and minifying CSS and JavaScript files into groups, compressing HTML and other speed optimizations.
Author: Raul Peixoto
Author URI: http://fastvelocity.com
Version: 1.3.7
License: GPL2
------------------------------------------------------------------------
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
# get current protocol scheme
if (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1) || isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
$protocol = 'https://';
} else {
$protocol = 'http://';
}
# get the plugin directory and include functions
$plugindir = plugin_dir_path(__FILE__); # with trailing slash
include($plugindir . 'inc/functions.php');
# generate cache directory
$tmpdir = $plugindir . 'temp';
$cachedir = $plugindir . 'cache';
$cachedirurl = plugins_url('cache', __FILE__);
if (!is_dir($cachedir)) {
mkdir($cachedir);
}
if (!is_dir($tmpdir)) {
mkdir($tmpdir);
}
# get the current wordpress installation url and path
$wp_home = site_url(); # get the current wordpress installation url
$wp_domain = trim(str_ireplace($protocol, '', trim($wp_home, '/')));
$wp_home_path = ABSPATH;
# cleanup, delete any minification files older than 45 days (most probably unused files)
if ($handle = opendir($cachedir . '/')) {
while (false !== ($file = readdir($handle))) {
$file = $cachedir . '/' . $file;
if (is_file($file) && time() - filemtime($file) >= 86400 * 45) {
unlink($file);
}
}
closedir($handle);
}
# default globals
$fastvelocity_min_global_js_done = array();
###########################################
# build control panel pages ###############
###########################################
# default options
$ignore = array(); # urls to exclude for merging and minification
$disable_js_merge = false; # disable JS merging? Default: false (if true, minification is also disabled)
$disable_css_merge = false; # disable CSS merging? Default: false (if true, minification is also disabled)
$disable_js_minification = false; # disable JS minification? Default: false
$disable_css_minification = false; # disable CSS minification? Default: false
$remove_print_mediatypes = false; # remove CSS files of "print" mediatype
$skip_html_minification = false; # skip HTML minification? Default: false
$skip_clean_fonts = false; # skip removing query strings from fonts? Default: false (there's a cache buster with latest plugin/theme update time)
$skip_cssorder = false; # skip reordering CSS files by mediatype
$skip_google_fonts = false; # skip google fonts optimization? Default: false
$skip_fontawesome_fonts = false; # skip font awesome optimization? Default: false
$skip_emoji_removal = false; # skip removing emoji support? Default: false
$enable_defer_js = false; # Defer parsing of JavaScript? Default false
$exclude_defer_jquery = false; # Disable defer for jquery on the home
$use_google_closure = false; # Use Google closure (slower) instead of YUI processor? Default false
$use_php_minify = false; # Use PHP Minify instead of YUI or Closure? Default false
$force_inline_css = false; # Don't inline all css by default
$force_inline_googlefonts = false; # Don't inline all google fonts by default
$defer_for_pagespeed = false; # force defer for when we are being tested by pagespeed insights
# add admin page and rewrite defaults
if (is_admin()) {
add_action('admin_menu', 'fastvelocity_min_admin_menu');
add_action('admin_enqueue_scripts', 'fastvelocity_min_load_admin_jscss');
add_action('wp_ajax_fastvelocity_min_files', 'fastvelocity_min_files_callback');
add_action('admin_init', 'fastvelocity_min_register_settings');
register_deactivation_hook(__FILE__, 'fastvelocity_min_plugin_deactivate');
} else {
# overwrite options from the database, false if not set
$ignore = array_map('trim', explode(PHP_EOL, get_option('fastvelocity_min_ignore')));
$disable_js_merge = get_option('fastvelocity_min_disable_js_merge');
$disable_css_merge = get_option('fastvelocity_min_disable_css_merge');
$disable_js_minification = get_option('fastvelocity_min_disable_js_minification');
$disable_css_minification = get_option('fastvelocity_min_disable_css_minification');
$remove_print_mediatypes = get_option('fastvelocity_min_remove_print_mediatypes');
$skip_html_minification = get_option('fastvelocity_min_skip_html_minification');
$skip_cssorder = get_option('fastvelocity_min_skip_cssorder');
$skip_clean_fonts = get_option('fastvelocity_min_skip_clean_fonts');
$skip_google_fonts = get_option('fastvelocity_min_skip_google_fonts');
$skip_fontawesome_fonts = get_option('fastvelocity_min_skip_fontawesome_fonts');
$skip_emoji_removal = get_option('fastvelocity_min_skip_emoji_removal');
$enable_defer_js = get_option('fastvelocity_min_enable_defer_js');
$exclude_defer_jquery = get_option('fastvelocity_min_exclude_defer_jquery');
$use_google_closure = get_option('fastvelocity_min_use_google_closure');
$use_php_minify = get_option('fastvelocity_min_use_php_minify');
$force_inline_css = get_option('fastvelocity_min_force_inline_css');
$force_inline_googlefonts = get_option('fastvelocity_min_force_inline_googlefonts');
$defer_for_pagespeed = get_option('fastvelocity_min_defer_for_pagespeed');
# actions for frontend only
if (!$disable_js_merge) {
add_action('wp_print_scripts', 'fastvelocity_min_merge_header_scripts', PHP_INT_MAX);
add_action('wp_print_footer_scripts', 'fastvelocity_min_merge_footer_scripts', 9.999999);
}
if (!$disable_css_merge) {
add_action('wp_print_styles', 'fastvelocity_min_merge_header_css', PHP_INT_MAX);
add_action('wp_print_footer_scripts', 'fastvelocity_min_merge_footer_css', 9.999999);
}
if (!$skip_emoji_removal) {
add_action('init', 'fastvelocity_min_disable_wp_emojicons');
}
if (!$skip_fontawesome_fonts) {
add_action('wp_print_styles', 'fastvelocity_min_add_fontawesome');
}
}
# add fontawesome if needed
function fastvelocity_min_add_fontawesome() {
wp_enqueue_style('global-fvm-fa', plugins_url('libs/fa/css/fa.min.css', __FILE__), array(), null, 'all');
}
# jquery changes for a few versions (start at v1.3.4)
if ((get_option('fastvelocity_min_exclude_defer_jquery_home') || get_option('fastvelocity_min_exclude_defer_jquery_pages') || get_option('fastvelocity_min_exclude_defer_jquery_posts') || get_option('fastvelocity_min_exclude_defer_jquery_search') || get_option('fastvelocity_min_exclude_defer_jquery_404')) && !get_option('fastvelocity_min_exclude_defer_jquery')) {
update_option('fastvelocity_min_exclude_defer_jquery', true);
$exclude_defer_jquery = true;
delete_option('fastvelocity_min_exclude_defer_jquery_home');
delete_option('fastvelocity_min_exclude_defer_jquery_pages');
delete_option('fastvelocity_min_exclude_defer_jquery_posts');
delete_option('fastvelocity_min_exclude_defer_jquery_search');
delete_option('fastvelocity_min_exclude_defer_jquery_404');
}
# delete the cache when we deactivate the plugin
function fastvelocity_min_plugin_deactivate() {
global $cachedir;
if (is_dir($cachedir)) {
rrmdir($cachedir);
}
}
# function to list all cache files
function fastvelocity_min_files_callback() {
global $cachedir;
if (isset($_POST['purge']) && $_POST['purge'] == 'all') {
rrmdir($cachedir);
} else if (isset($_POST['purge'])) {
if ($handle = opendir($cachedir . '/')) {
while (false !== ($file = readdir($handle))) {
if (stripos($file, $_POST['purge']) !== false) {
@unlink($cachedir . '/' . $file);
}
}
closedir($handle);
}
}
# default
$return = array('js' => array(), 'css' => array(), 'stamp' => $_POST['stamp']);
# inspect directory with opendir, since glob might not be available in some systems
if ($handle = opendir($cachedir . '/')) {
while (false !== ($file = readdir($handle))) {
$file = $cachedir . '/' . $file;
$ext = pathinfo($file, PATHINFO_EXTENSION);
if (in_array($ext, array('js', 'css'))) {
$log = file_get_contents($file . '.txt');
$mincss = substr($file, 0, -4) . '.min.css';
$minjs = substr($file, 0, -3) . '.min.js';
$filename = basename($file);
if ($ext == 'css' && file_exists($mincss)) {
$filename = basename($mincss);
}
if ($ext == 'js' && file_exists($minjs)) {
$filename = basename($minjs);
}
# get location, hash, modified date
$info = explode('-', $filename);
$hash = $info['1'];
array_push($return[$ext], array('hash' => $hash, 'filename' => $filename, 'log' => $log));
}
}
closedir($handle);
}
header('Content-Type: application/json');
echo json_encode($return);
wp_die();
}
# load wp-admin css and js files
function fastvelocity_min_load_admin_jscss($hook) {
if ('settings_page_fastvelocity-min' != $hook) {
return;
}
wp_enqueue_script('postbox');
wp_enqueue_style('fastvelocity-min', plugins_url('admin.css', __FILE__), array(), filemtime(plugin_dir_path('admin.css', __FILE__)));
wp_enqueue_script('fastvelocity-min', plugins_url('admin.js', __FILE__), array('jquery'), filemtime(plugin_dir_path('admin.js', __FILE__)), true);
}
# create admin menu
function fastvelocity_min_admin_menu() {
add_options_page('Fast Velocity Minify Settings', 'Fast Velocity Minify', 'manage_options', 'fastvelocity-min', 'fastvelocity_min_settings');
}
# register plugin settings
function fastvelocity_min_register_settings() {
register_setting('fvm-group', 'fastvelocity_min_ignore');
register_setting('fvm-group', 'fastvelocity_min_disable_js_merge');
register_setting('fvm-group', 'fastvelocity_min_disable_css_merge');
register_setting('fvm-group', 'fastvelocity_min_disable_js_minification');
register_setting('fvm-group', 'fastvelocity_min_disable_css_minification');
register_setting('fvm-group', 'fastvelocity_min_remove_print_mediatypes');
register_setting('fvm-group', 'fastvelocity_min_skip_html_minification');
register_setting('fvm-group', 'fastvelocity_min_skip_cssorder');
register_setting('fvm-group', 'fastvelocity_min_skip_clean_fonts');
register_setting('fvm-group', 'fastvelocity_min_skip_google_fonts');
register_setting('fvm-group', 'fastvelocity_min_skip_fontawesome_fonts');
register_setting('fvm-group', 'fastvelocity_min_skip_emoji_removal');
register_setting('fvm-group', 'fastvelocity_min_enable_defer_js');
register_setting('fvm-group', 'fastvelocity_min_exclude_defer_jquery');
register_setting('fvm-group', 'fastvelocity_min_use_google_closure');
register_setting('fvm-group', 'fastvelocity_min_use_php_minify');
register_setting('fvm-group', 'fastvelocity_min_force_inline_css');
register_setting('fvm-group', 'fastvelocity_min_force_inline_googlefonts');
register_setting('fvm-group', 'fastvelocity_min_defer_for_pagespeed');
}
# add settings link on plugin page
function fastvelocity_min_settings_link($links) {
$settings_link = '<a href="options-general.php?page=fastvelocity-min">Settings</a>';
array_unshift($links, $settings_link);
return $links;
}
add_filter("plugin_action_links_" . plugin_basename(__FILE__), 'fastvelocity_min_settings_link');
# manage settings page
function fastvelocity_min_settings() {
if (!current_user_can('manage_options')) {
wp_die(__('You do not have sufficient permissions to access this page.'));
}
# tmp folder
global $tmpdir, $cachedir, $plugindir;
# get active tab, set default
$active_tab = isset($_GET['tab']) ? $_GET['tab'] : 'status';
?>
<div class="wrap">
<h1>Fast Velocity Minify</h1>
<?php
if (isset($_POST['purgealt']) && $_POST['purgealt'] == 1) {
rrmdir($tmpdir);
echo '<div class="notice notice-success is-dismissible"><p>The intermediate minification cache has been purged!</p></div>';
}
?>
<?php
if (isset($_POST['purgeall']) && $_POST['purgeall'] == 1) {
rrmdir($cachedir);
echo '<div class="notice notice-success is-dismissible"><p>The CSS and JS files have been purged!</p></div>';
}
?>
<h2 class="nav-tab-wrapper wp-clearfix">
<a href="?page=fastvelocity-min&tab=status" class="nav-tab <?php echo $active_tab == 'status' ? 'nav-tab-active' : ''; ?>">Status</a>
<a href="?page=fastvelocity-min&tab=settings" class="nav-tab <?php echo $active_tab == 'settings' ? 'nav-tab-active' : ''; ?>">Settings</a>
<a href="?page=fastvelocity-min&tab=help" class="nav-tab <?php echo $active_tab == 'help' ? 'nav-tab-active' : ''; ?>">Help</a>
</h2>
<?php if ($active_tab == 'status') { ?>
<div id="fastvelocity-min">
<div id="poststuff">
<div id="fastvelocity_min_processed" class="postbox-container">
<div class="meta-box-sortables ui-sortable">
<div class="postbox" id="tab-purge">
<h3 class="hndle"><span>Purge processed files</span></h3>
<div class="inside" id="fastvelocity_min_topbtns">
<ul class="processed">
<li id="purgeall-row">
<span class="filename">Purge processed CSS and JS files</span>
<span class="actions">
<form method="post" id="fastvelocity_min_clearall" action="<?php echo admin_url('options-general.php?page=fastvelocity-min&tab=status'); ?>">
<input type="hidden" name="purgeall" value="1" />
<?php submit_button('Delete', 'button-secondary', 'submit', false); ?>
</form>
</li>
<li id="purgealt-row">
<span class="filename">Purge intermediate minification cache</span>
<span class="actions">
<form method="post" id="fastvelocity_min_clearalt" action="<?php echo admin_url('options-general.php?page=fastvelocity-min&tab=status'); ?>">
<input type="hidden" name="purgealt" value="1" />
<?php submit_button('Delete', 'button-secondary', 'submit', false); ?>
</form>
</span>
</li>
<div class="clear"></div>
</ul>
</div>
</div>
<div class="postbox" id="tab-js">
<h3 class="hndle"><span>List of processed JS files</span></h3>
<div class="inside" id="fastvelocity_min_jsprocessed">
<ul class="processed"></ul>
</div>
</div>
<div class="postbox" id="tab-css">
<h3 class="hndle"><span>List of processed CSS files</span></h3>
<div class="inside" id="fastvelocity_min_cssprocessed">
<ul class="processed"></ul>
</div>
</div>
</div>
</div>
</div>
</div>
<?php } ?>
<?php if ($active_tab == 'settings') { ?>
<form method="post" action="options.php">
<?php settings_fields('fvm-group');
do_settings_sections('fvm-group');
?>
<table class="form-table" id="fvm-settings">
<tbody>
<tr>
<th scope="row">HTML Options</th>
<td><fieldset><legend class="screen-reader-text"><span>HTML Options</span></legend>
<label for="fastvelocity_min_skip_html_minification">
<input name="fastvelocity_min_skip_html_minification" type="checkbox" id="fastvelocity_min_skip_html_minification" value="1" <?php echo checked(1 == get_option('fastvelocity_min_skip_html_minification'), true, false); ?>>
Disable minification on HTML <span class="note-info">[ Normally, it's safe to leave HTML minification enabled ]</span></label>
<br />
</fieldset></td>
</tr>
<tr>
<th scope="row">Fonts Options</th>
<td><fieldset><legend class="screen-reader-text"><span>Fonts Options</span></legend>
<label for="fastvelocity_min_skip_fontawesome_fonts">
<input name="fastvelocity_min_skip_fontawesome_fonts" type="checkbox" id="fastvelocity_min_skip_fontawesome_fonts" value="1" <?php echo checked(1 == get_option('fastvelocity_min_skip_fontawesome_fonts'), true, false); ?> >
Disable Font Awesome optimization <span class="note-info">[ If selected, your Font Awesome files will be used... but duplicates won't be removed ]</span></label>
<br />
<label for="fastvelocity_min_skip_clean_fonts">
<input name="fastvelocity_min_skip_clean_fonts" type="checkbox" id="fastvelocity_min_skip_clean_fonts" value="1" <?php echo checked(1 == get_option('fastvelocity_min_skip_clean_fonts'), true, false); ?> >
Disable the removal of query strings for Web Fonts <span class="note-info">[ If selected, query strings on CSS code will be preserved for local web fonts ]</span></label>
<br />
<label for="fastvelocity_min_skip_emoji_removal">
<input name="fastvelocity_min_skip_emoji_removal" type="checkbox" id="fastvelocity_min_skip_emoji_removal" class="jsprocessor" value="1" <?php echo checked(1 == get_option('fastvelocity_min_skip_emoji_removal'), true, false); ?> >
Stop removing Emojis and smileys <span class="note-info">[ If selected, Emojis will be left alone and won't be removed from wordpress ]</span></label>
<br />
<label for="fastvelocity_min_skip_google_fonts">
<input name="fastvelocity_min_skip_google_fonts" type="checkbox" id="fastvelocity_min_skip_google_fonts" value="1" <?php echo checked(1 == get_option('fastvelocity_min_skip_google_fonts'), true, false); ?> >
Disable Google Fonts merging <span class="note-info">[ If selected, Google Fonts will no longer be merged into one request ]</span></label>
<br />
<label for="fastvelocity_min_force_inline_googlefonts">
<input name="fastvelocity_min_force_inline_googlefonts" type="checkbox" id="fastvelocity_min_force_inline_googlefonts" value="1" <?php echo checked(1 == get_option('fastvelocity_min_force_inline_googlefonts'), true, false); ?> >
Inline Google Fonts CSS <span class="note-info">[ If selected, Google Fonts CSS code will be inlined using "*.woof" format - NOTE: IE9+ and <a target="_blank" href="http://caniuse.com/#feat=woff">modern browsers</a> only]</span></label>
<br />
</fieldset></td>
</tr>
<tr>
<th scope="row">CSS Options</th>
<td><fieldset><legend class="screen-reader-text"><span>CSS Options</span></legend>
<label for="fastvelocity_min_disable_css_merge">
<input name="fastvelocity_min_disable_css_merge" type="checkbox" id="fastvelocity_min_disable_css_merge" value="1" <?php echo checked(1 == get_option('fastvelocity_min_disable_css_merge'), true, false); ?>>
Disable CSS processing<span class="note-info">[ If selected, this plugin will ignore CSS files completely ]</span></label>
<br />
<label for="fastvelocity_min_disable_css_minification">
<input name="fastvelocity_min_disable_css_minification" type="checkbox" id="fastvelocity_min_disable_css_minification" value="1" <?php echo checked(1 == get_option('fastvelocity_min_disable_css_minification'), true, false); ?>>
Disable minification on CSS files <span class="note-info">[ If selected, CSS files will be merged but not minified ]</span></label>
<br />
<label for="fastvelocity_min_skip_cssorder">
<input name="fastvelocity_min_skip_cssorder" type="checkbox" id="fastvelocity_min_skip_cssorder" value="1" <?php echo checked(1 == get_option('fastvelocity_min_skip_cssorder'), true, false); ?> >
Disable reordering of CSS files <span class="note-info">[ If selected, you will have better CSS compatibility but possibly more CSS files]</span></label>
<br />
<label for="fastvelocity_min_remove_print_mediatypes">
<input name="fastvelocity_min_remove_print_mediatypes" type="checkbox" id="fastvelocity_min_remove_print_mediatypes" value="1" <?php echo checked(1 == get_option('fastvelocity_min_remove_print_mediatypes'), true, false); ?> >
Remove the "Print" related stylesheets <span class="note-info">[ If selected, CSS files of mediatype "print" will be removed from the site ]</span></label>
<br />
<label for="fastvelocity_min_force_inline_css">
<input name="fastvelocity_min_force_inline_css" type="checkbox" id="fastvelocity_min_force_inline_css" value="1" <?php echo checked(1 == get_option('fastvelocity_min_force_inline_css'), true, false); ?>>
Inline all enqueued CSS<span class="note-info">[ If selected, CSS will be inlined to avoid the "render blocking" on pagespeed insights ]</span></label>
<br />
</fieldset></td>
</tr>
<tr>
<th scope="row">JavaScript Options</th>
<td><fieldset><legend class="screen-reader-text"><span>JavaScript Options</span></legend>
<label for="fastvelocity_min_disable_js_merge">
<input name="fastvelocity_min_disable_js_merge" type="checkbox" id="fastvelocity_min_disable_js_merge" value="1" <?php echo checked(1 == get_option('fastvelocity_min_disable_js_merge'), true, false); ?> >
Disable JavaScript processing <span class="note-info">[ If selected, this plugin will ignore JS files completely ]</span></label>
<br />
<label for="fastvelocity_min_disable_js_minification">
<input name="fastvelocity_min_disable_js_minification" type="checkbox" id="fastvelocity_min_disable_js_minification" value="1" <?php echo checked(1 == get_option('fastvelocity_min_disable_js_minification'), true, false); ?> >
Disable minification on JS files <span class="note-info">[ If selected, JS files will be merged but not minified ]</span></label>
<br />
<label for="fastvelocity_min_use_google_closure">
<input name="fastvelocity_min_use_google_closure" type="checkbox" id="fastvelocity_min_use_google_closure" class="jsprocessor" value="1" <?php echo checked(1 == get_option('fastvelocity_min_use_google_closure'), true, false); ?> >
Minify with Google Closure <span class="note-info">[ Google Closure is more recent but YUI works by default and it's faster ]</span></label>
<br />
<label for="fastvelocity_min_use_php_minify">
<input name="fastvelocity_min_use_php_minify" type="checkbox" id="fastvelocity_min_use_php_minify" class="jsprocessor" value="1" <?php echo checked(1 == get_option('fastvelocity_min_use_php_minify'), true, false); ?> >
Minify with PHP Minify <span class="note-info">[ If selected, JS minification will be done by PHP Minify only ]</span></label>
<br />
</fieldset></td>
</tr>
<tr>
<th scope="row">Render-blocking JavaScript</th>
<td><fieldset><legend class="screen-reader-text"><span>Render-blocking JavaScript</span></legend>
<label for="fastvelocity_min_enable_defer_js">
<input name="fastvelocity_min_enable_defer_js" type="checkbox" id="fastvelocity_min_enable_defer_js" value="1" <?php echo checked(1 == get_option('fastvelocity_min_enable_defer_js'), true, false); ?> >
Enable defer parsing of JS files <span class="note-info">[ Not all browsers, themes or plugins support this. Beware of broken functionality and design ]</span></label>
<br />
<label for="fastvelocity_min_defer_for_pagespeed">
<input name="fastvelocity_min_defer_for_pagespeed" type="checkbox" id="fastvelocity_min_defer_for_pagespeed" value="1" <?php echo checked(1 == get_option('fastvelocity_min_defer_for_pagespeed'), true, false); ?> >
Enable defer of JS files for Pagespeed Insights <span class="note-info">[ This will defer all js files (even if they are on the ignore list) for Pagespeed Insights only ]</span></label>
<br />
<label for="fastvelocity_min_exclude_defer_jquery">
<input name="fastvelocity_min_exclude_defer_jquery" type="checkbox" id="fastvelocity_min_exclude_defer_jquery" value="1" <?php echo checked(1 == get_option('fastvelocity_min_exclude_defer_jquery'), true, false); ?> >
Add jQuery to the ignore list <span class="note-info">[ Will fix "undefined jQuery" errors on the Google Chrome console log ]</span></label>
<br />
</fieldset></td>
</tr>
<tr>
<th scope="row">Ignore List</th>
<td><fieldset><legend class="screen-reader-text"><span>Ignore List</span></legend>
<p><label for="blacklist_keys">Ignore the following CSS and JS full urls below:</label></p>
<p>
<textarea name="fastvelocity_min_ignore" rows="10" cols="50" id="fastvelocity_min_ignore" class="large-text code" placeholder="Example: http://yourdomain.com/wp-includes/js/jquery/jquery.js"><?php echo get_option('fastvelocity_min_ignore'); ?></textarea>
</p>
<p class="description">[ View the logs for a list of urls and add one url per line here to be ignored. No wildcards support yet. ]</p>
</fieldset></td>
</tr>
</tbody></table>
<p class="submit"><input type="submit" name="fastvelocity_min_save_options" id="fastvelocity_min_save_options" class="button button-primary" value="Save Changes"></p>
</form>
<?php } ?>
<?php if ($active_tab == 'help') { ?>
<div class="wrap" id="fastvelocity-min">
<div id="poststuff">
<div id="fastvelocity_min_processed" class="postbox-container">
<div class="meta-box-sortables ui-sortable">
<div class="postbox" id="tab-info">
<h3 class="hndle"><span>Frequently Asked Questions</span></h3>
<div class="inside"><? echo fastvelocity_min_readme($plugindir.'readme.txt'); ?></div>
</div>
</div>
</div>
</div>
</div>
<?php } ?>
</div>
<div class="clear"></div>
<?php
}
###########################################
# process header javascript ###############
###########################################
function fastvelocity_min_merge_header_scripts() {
global $wp_scripts, $wp_domain, $protocol, $wp_home, $wp_home_path, $cachedir, $cachedirurl, $ignore, $disable_js_merge, $disable_js_minification, $enable_defer_js, $exclude_defer_jquery;
if (!is_object($wp_scripts)) {
return false;
}
$scripts = wp_clone($wp_scripts);
$scripts->all_deps($scripts->queue);
$header = array();
# mark as done (as we go)
$done = $scripts->done;
# try to find dynamically generated js files and add those to the ignore list
foreach ($scripts->to_do as $handle) :
$hurl = fastvelocity_min_get_hurl($wp_scripts->registered[$handle]->src, $wp_domain, $protocol, $wp_home);
if (stripos($hurl, '.php?') !== false && !fastvelocity_min_in_arrayi($hurl, $ignore)) {
$ignore[] = $hurl;
}
endforeach;
# get groups of handles & latest modified date
foreach ($scripts->to_do as $handle) :
# is it a footer script?
$is_footer = 0;
if (isset($wp_scripts->registered[$handle]->extra["group"]) || isset($wp_scripts->registered[$handle]->args)) {
$is_footer = 1;
}
# skip footer scripts for now
if ($is_footer != 1) {
# get full url
$hurl = fastvelocity_min_get_hurl($wp_scripts->registered[$handle]->src, $wp_domain, $protocol, $wp_home);
# if defer is enable, add jQuery to the ignore list
if ($enable_defer_js == '1' && $handle == 'jquery-core' && $exclude_defer_jquery == '1') {
$ignore[] = $hurl;
}
# skip ignore list, scripts with conditionals, external scripts
if ((!fastvelocity_min_in_arrayi($hurl, $ignore) && !isset($wp_scripts->registered[$handle]->extra["conditional"]) && substr($hurl, 0, strlen($wp_home)) === $wp_home) || empty($hurl)) {
# process
if (isset($header[count($header) - 1]['handle']) || count($header) == 0) {
array_push($header, array('modified' => 0, 'handles' => array()));
}
# get path and last modified date
$handlepath = str_ireplace($wp_home, $wp_home_path, $hurl);
$modified = 0;
if (is_file($handlepath)) {
$modified = filemtime($handlepath);
}
# push it to the array get latest modified time
array_push($header[count($header) - 1]['handles'], $handle);
if ($modified > $header[count($header) - 1]['modified']) {
$header[count($header) - 1]['modified'] = $modified;
}
# external and ignored scripts
} else {
array_push($header, array('handle' => $handle));
}
# make sure that the scripts skipped here, show up in the footer
} else {
$hurl = fastvelocity_min_get_hurl($wp_scripts->registered[$handle]->src, $wp_domain, $protocol, $wp_home);
wp_enqueue_script($handle, $hurl, array(), null, true);
}
endforeach;
# loop through header scripts and merge
for ($i = 0, $l = count($header); $i < $l; $i++) {
if (!isset($header[$i]['handle'])) {
# static cache file info + done
$done = array_merge($done, $header[$i]['handles']);
$hash = 'header-' . hash('adler32', implode('', $header[$i]['handles']));
$modified = $header[$i]['modified'];
$cache_path = $cachedir . '/' . $hash . '-' . $modified . '.min.js';
$cache_lock = $cache_path . '.lock';
$cache_url = $cachedirurl . '/' . $hash . '-' . $modified . '.min.js';
# generate a new cache file
if (!file_exists($cache_path)) {
# create a lock file to prevent incomplete views, log start
file_put_contents($cache_lock, '', LOCK_EX);
file_put_contents($cache_path . '.txt', date('r') . " - PROCESSED:\n");
# minify and write to file
$js = '';
$log = '';
foreach ($header[$i]['handles'] as $handle) :
if (!empty($wp_scripts->registered[$handle]->src)) {
# get full url and path
$hurl = fastvelocity_min_get_hurl($wp_scripts->registered[$handle]->src, $wp_domain, $protocol, $wp_home);
$handlepath = str_ireplace($wp_home, $wp_home_path, $hurl);
# process
$jsarr = array();
$jsarr = fastvelocity_min_minify_js($handle, $hurl, $handlepath, $disable_js_minification);
$js .= $jsarr['js'];
$log .= $jsarr['log'];
# consider dependencies on handles with an empty src
} else {
wp_dequeue_script($handle);
wp_enqueue_script($handle);
}
endforeach;
# generate cache, write log
file_put_contents($cache_path, $js, LOCK_EX);
file_put_contents($cache_path . '.txt', $log . date('r') . " - ALL DONE\n", FILE_APPEND);
file_put_contents($cache_path . '.gz', gzencode(file_get_contents($cache_path), 9)); # useful for gzip_static on nginx
@unlink($cache_lock);
clearstatcache();
}
# register minified file
wp_register_script("fvm-header-$i", $cache_url, array(), null, false);
# add all extra data from wp_localize_script
$data = array();
foreach ($header[$i]['handles'] as $handle) {
if (isset($wp_scripts->registered[$handle]->extra['data'])) {
$data[] = $wp_scripts->registered[$handle]->extra['data'];
}
}
if (count($data) > 0) {
$wp_scripts->registered["fvm-header-$i"]->extra['data'] = implode("\n", $data);
}
# enqueue file
wp_enqueue_script("fvm-header-$i");
# other scripts need to be requeued for the order of files to be kept
} else {
wp_dequeue_script($header[$i]['handle']);
wp_enqueue_script($header[$i]['handle']);
}
}
# remove from queue
$wp_scripts->done = $done;
}
###########################################
# process header css ######################
###########################################
function fastvelocity_min_merge_header_css() {
global $wp_styles, $wp_domain, $protocol, $wp_home, $wp_home_path, $cachedir, $cachedirurl, $ignore, $disable_css_merge, $disable_css_minification, $skip_clean_fonts, $skip_google_fonts, $skip_fontawesome_fonts, $skip_cssorder, $remove_print_mediatypes, $force_inline_css, $force_inline_googlefonts;
if (!is_object($wp_styles)) {
return false;
}
$styles = wp_clone($wp_styles);
$styles->all_deps($styles->queue);
$done = $styles->done;
$header = array();
$google_fonts = array();
$colect_css_after = array();
$process = array();
# try to find dynamically generated css files and add those to the ignore list
foreach ($styles->to_do as $handle) :
$hurl = fastvelocity_min_get_hurl($wp_styles->registered[$handle]->src, $wp_domain, $protocol, $wp_home);
if (stripos($hurl, '.php?') !== false && !fastvelocity_min_in_arrayi($hurl, $ignore)) {
$ignore[] = $hurl;
}
endforeach;
# get list of handles to process, dequeue duplicate css urls and keep empty source handles (for dependencies)
$uniq = array();
$gfonts = array();
foreach ($styles->to_do as $handle):
$conditional = NULL;
if (isset($wp_styles->registered[$handle]->extra["conditional"])) {
$conditional = $wp_styles->registered[$handle]->extra["conditional"]; # such as ie7, ie8, ie9, etc
}
$mediatype = isset($wp_styles->registered[$handle]->args) ? $wp_styles->registered[$handle]->args : 'all'; # such as all, print, mobile, etc
# mediatype screen and all are the same, standardize
if ($mediatype == 'screen' || $mediatype == 'screen, print') {
$mediatype = 'all';
}
$hurl = fastvelocity_min_get_hurl($wp_styles->registered[$handle]->src, $wp_domain, $protocol, $wp_home); # full url or empty
# colect inline css for this handle
if (isset($wp_styles->registered[$handle]->extra['after']) && is_array($wp_styles->registered[$handle]->extra['after'])) {
$colect_css_after[$handle] = fastvelocity_min_minify_css_string(implode('', $wp_styles->registered[$handle]->extra['after'])); # save
$wp_styles->registered[$handle]->extra['after'] = null; # dequeue
}
# get path and last modified date
$handlepath = '';
if (!empty($hurl)) {
$handlepath = str_ireplace($wp_home, $wp_home_path, $hurl);
}
$modified = 0;
if (!empty($handlepath) && is_file($handlepath)) {
$modified = filemtime($handlepath);
}
# mark duplicates as done and remove from the queue
if (!empty($hurl)) {
$key = hash('adler32', $hurl);
if (isset($uniq[$key])) {
$done = array_merge($done, array($handle));
continue;
} else {
$uniq[$key] = $handle;
}
}
# array of info to save
$arr = array('handle' => $handle, 'url' => $hurl, 'path' => $handlepath, 'modified' => $modified, 'conditional' => $conditional, 'mediatype' => $mediatype);
# google fonts to the top
if (stripos($hurl, 'fonts.googleapis.com') !== false) {
if (!$skip_google_fonts) {
$google_fonts[$handle] = $hurl;
} else {
wp_enqueue_style($handle);
}
continue;
}
# add font awesome to the top (if used), delete duplicates
if (stripos($hurl, 'font-awesome.min.css') !== false || stripos($hurl, 'font-awesome.css') !== false) {
if (!$skip_fontawesome_fonts) {
$done = array_merge($done, array($handle)); # remove from queue
} else {
wp_enqueue_style($handle);
}
}
# all else
$process[$handle] = $arr;
endforeach;
# concat google fonts, if enabled
if (!$skip_google_fonts && count($google_fonts) > 0) {
$concat_google_fonts = fastvelocity_min_concatenate_google_fonts($google_fonts);
foreach ($google_fonts as $h => $a) {
$done = array_merge($done, array($h));
} # mark as done
if ($force_inline_googlefonts == false) {
wp_enqueue_style('header-fvm-fonts', $concat_google_fonts, array(), null, 'all');
} else {
# don't re-download the fonts for the next 4 hours
$cache_key = 'fvm-cache-gfonts-header';
$gfonts = get_transient($cache_key);
# save into cache if transient info not available
if (false === $gfonts) {
$gfonts = fastvelocity_file_get_contents_curl($concat_google_fonts, 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko'); # woff format only
$gfonts = fastvelocity_min_minify_css_string($gfonts);
set_transient($cache_key, $gfonts, 4 * HOUR_IN_SECONDS);
}
# inline google fonts
echo '<style type="text/css">' . $gfonts . '</style>';
}
}
# get groups of handles & latest modified date
foreach ($styles->to_do as $handle) :
# skip already processed google fonts and empty dependencies
if (isset($google_fonts[$handle])) {
continue;
} # skip google fonts
if (empty($wp_styles->registered[$handle]->src)) {
continue;
} # skip empty src
if (fastvelocity_min_in_arrayi($handle, $done)) {
continue;
} # skip if marked as done before
if (!isset($process[$handle])) {
continue;
} # skip if not on our unique process list
# get full url
$hurl = $process[$handle]['url'];
$conditional = $process[$handle]['conditional'];
$mediatype = $process[$handle]['mediatype'];
$handlepath = $process[$handle]['path'];
$modified = $process[$handle]['modified'];
# skip ignore list, conditional css, external css
if ((!fastvelocity_min_in_arrayi($hurl, $ignore) && !isset($conditional) && substr($hurl, 0, strlen($wp_home)) === $wp_home) || empty($hurl)) {
# process
if (isset($header[count($header) - 1]['handle']) || count($header) == 0 || $header[count($header) - 1]['media'] != $mediatype) {
array_push($header, array('modified' => 0, 'handles' => array(), 'media' => $mediatype));
}
# push it to the array get latest modified time
array_push($header[count($header) - 1]['handles'], $handle);
if ($modified > $header[count($header) - 1]['modified']) {
$header[count($header) - 1]['modified'] = $modified;
}
# external and ignored css
} else {
array_push($header, array('handle' => $handle));
}
endforeach;
# reorder CSS by mediatypes
if (!$skip_cssorder) {
if (count($header) > 0) {
# get unique mediatypes and most recent modified time
$allmedia = array();
foreach ($header as $key => $array) {
if (isset($array['media']) && isset($allmedia[$array['media']]) && isset($array['modified'])) {
if ($allmedia[$array['media']] <= $array['modified']) {
$allmedia[$array['media']] = $array['modified'];
unset($header[$key]);
} else {
$allmedia[$array['media']] = $array['modified'];
unset($header[$key]);
}
}
}
# extract handles by mediatype
$grouphandles = array();
foreach ($allmedia as $md => $dt) {
foreach ($header as $array) {
if (isset($array['media']) && $array['media'] == $md) {
foreach ($array['handles'] as $h) {
$grouphandles[$md][] = $h;
}
}
}
}
# reset and reorder header by mediatypes
foreach ($allmedia as $md => $dt) {
$header[] = array('modified' => $dt, 'handles' => $grouphandles[$md], 'media' => $md);
}
}
}
# loop through header css and merge
for ($i = 0, $l = count($header); $i < $l; $i++) {
if (!isset($header[$i]['handle'])) {
# static cache file info + done
$done = array_merge($done, $header[$i]['handles']);
$hash = 'header-' . hash('adler32', implode('', $header[$i]['handles']));
$modified = $header[$i]['modified'];
$cache_path = $cachedir . '/' . $hash . '-' . $modified . '.min.css';
$cache_lock = $cache_path . '.lock';
$cache_url = $cachedirurl . '/' . $hash . '-' . $modified . '.min.css';
# generate a new cache file
if (!file_exists($cache_path)) {
# create a lock file to prevent incomplete views, log start
file_put_contents($cache_lock, '', LOCK_EX);
file_put_contents($cache_path . '.txt', date('r') . " - PROCESSED:\n");
# minify and write to file
$css = '';
$log = '';
foreach ($header[$i]['handles'] as $handle) :
if (!empty($wp_styles->registered[$handle]->src)) {
# get full url and path
$hurl = $process[$handle]['url'];
$handlepath = $process[$handle]['path'];
# process
$cssarr = array();
$cssarr = fastvelocity_min_minify_css($handle, $hurl, $handlepath, $skip_clean_fonts, $disable_css_minification);
$css .= $cssarr['css'];
$log .= $cssarr['log'];
# consider dependencies on handles with an empty src
} else {
wp_dequeue_style($handle);
wp_enqueue_style($handle);
}
endforeach;
# generate cache, write log
file_put_contents($cache_path, $css, LOCK_EX);
file_put_contents($cache_path . '.txt', $log . date('r') . " - ALL DONE\n", FILE_APPEND);
file_put_contents($cache_path . '.gz', gzencode(file_get_contents($cache_path), 9)); # useful for gzip_static on nginx
@unlink($cache_lock);
clearstatcache();
}
# register and enqueue minified file, consider excluding of mediatype "print" and inline css
if ($remove_print_mediatypes != 1 || ($remove_print_mediatypes == 1 && $header[$i]['media'] != 'print')) {
if ($force_inline_css != false) {
echo '<style type="text/css" media="' . $header[$i]['media'] . '">' . file_get_contents($cache_path) . '</style>';
if (count($colect_css_after) > 0) {
echo '<style type="text/css" media="' . $header[$i]['media'] . '">' . implode('', $colect_css_after) . '</style>';
}
} else {
wp_register_style("fvm-header-$i", $cache_url, array(), null, $header[$i]['media']);
wp_enqueue_style("fvm-header-$i");
if (count($colect_css_after) > 0) {
wp_add_inline_style("fvm-header-$i", implode('', $colect_css_after));
} # add extra, inline css
}
}
# other css need to be requeued for the order of files to be kept
} else {
wp_dequeue_style($header[$i]['handle']);
wp_enqueue_style($header[$i]['handle']);
}
}
# remove from queue
$wp_styles->done = $done;
}
###########################################
# process js in the footer ################
###########################################
function fastvelocity_min_merge_footer_scripts() {
global $wp_scripts, $protocol, $wp_domain, $wp_home, $wp_home_path, $cachedir, $cachedirurl, $ignore, $disable_js_merge, $disable_js_minification, $enable_defer_js, $exclude_defer_jquery;
if (!is_object($wp_scripts)) {
return false;
}
# process JS in the footer
$scripts = wp_clone($wp_scripts);
$scripts->all_deps($scripts->queue);
$footer = array();
# mark as done (as we go)
$done = $scripts->done;
# try to find dynamically generated js files and add those to the ignore list
foreach ($scripts->to_do as $handle) :
$hurl = fastvelocity_min_get_hurl($wp_scripts->registered[$handle]->src, $wp_domain, $protocol, $wp_home);
if (stripos($hurl, '.php?') !== false && !fastvelocity_min_in_arrayi($hurl, $ignore)) {
$ignore[] = $hurl;
}
endforeach;
# get groups of handles & latest modified date
foreach ($scripts->to_do as $handle) :
# get full url
$hurl = fastvelocity_min_get_hurl($wp_scripts->registered[$handle]->src, $wp_domain, $protocol, $wp_home);
# if defer is enable, add jQuery to the ignore list for home, pages, posts, search or 404 page (if required)
if ($enable_defer_js == '1' && $handle == 'jquery-core' && $exclude_defer_jquery_pages == '1') {
$ignore[] = $hurl;
}
# skip ignore list, scripts with conditionals, external scripts
if ((!fastvelocity_min_in_arrayi($hurl, $ignore) && !isset($wp_scripts->registered[$handle]->extra["conditional"]) && substr($hurl, 0, strlen($wp_home)) === $wp_home) || empty($hurl)) {
# process
if (isset($footer[count($footer) - 1]['handle']) || count($footer) == 0) {
array_push($footer, array('modified' => 0, 'handles' => array()));
}
# get path and last modified date
$handlepath = str_ireplace($wp_home, $wp_home_path, $hurl);
$modified = 0;
if (is_file($handlepath)) {
$modified = filemtime($handlepath);
}
# push it to the array get latest modified time
array_push($footer[count($footer) - 1]['handles'], $handle);
if ($modified > $footer[count($footer) - 1]['modified']) {
$footer[count($footer) - 1]['modified'] = $modified;
}
# external and ignored scripts
} else {
array_push($footer, array('handle' => $handle));
}
endforeach;
# loop through footer scripts and merge
for ($i = 0, $l = count($footer); $i < $l; $i++) {
if (!isset($footer[$i]['handle'])) {
# static cache file info + done
$done = array_merge($done, $footer[$i]['handles']);
$hash = 'footer-' . hash('adler32', implode('', $footer[$i]['handles']));
$modified = $footer[$i]['modified'];
$cache_path = $cachedir . '/' . $hash . '-' . $modified . '.min.js';
$cache_lock = $cache_path . '.lock';
$cache_url = $cachedirurl . '/' . $hash . '-' . $modified . '.min.js';
# generate a new cache file
if (!file_exists($cache_path)) {
# create a lock file to prevent incomplete views, log start
file_put_contents($cache_lock, '', LOCK_EX);
file_put_contents($cache_path . '.txt', date('r') . " - PROCESSED:\n");
# minify and write to file
$js = '';
$log = '';
foreach ($footer[$i]['handles'] as $handle) :
if (!empty($wp_scripts->registered[$handle]->src)) {
# get full url and path
$hurl = fastvelocity_min_get_hurl($wp_scripts->registered[$handle]->src, $wp_domain, $protocol, $wp_home);
$handlepath = str_ireplace($wp_home, $wp_home_path, $hurl);
# process
$jsarr = array();
$jsarr = fastvelocity_min_minify_js($handle, $hurl, $handlepath, $disable_js_minification);
$js .= $jsarr['js'];
$log .= $jsarr['log'];
# consider dependencies on handles with an empty src
} else {
wp_dequeue_script($handle);
wp_enqueue_script($handle);
}
endforeach;
# generate cache, write log
file_put_contents($cache_path, $js, LOCK_EX);
file_put_contents($cache_path . '.txt', $log . date('r') . " - ALL DONE\n", FILE_APPEND);
file_put_contents($cache_path . '.gz', gzencode(file_get_contents($cache_path), 9)); # useful for gzip_static on nginx
@unlink($cache_lock);
clearstatcache();
}
# register minified file
wp_register_script("fvm-footer-$i", $cache_url, array(), null, true);
# add all extra data from wp_localize_script
$data = array();
foreach ($footer[$i]['handles'] as $handle) {
if (isset($wp_scripts->registered[$handle]->extra['data'])) {
$data[] = $wp_scripts->registered[$handle]->extra['data'];
}
}
if (count($data) > 0) {
$wp_scripts->registered["fvm-footer-$i"]->extra['data'] = implode("\n", $data);
}
# enqueue file
wp_enqueue_script("fvm-footer-$i");
# other scripts need to be requeued for the order of files to be kept
} else {
wp_dequeue_script($footer[$i]['handle']);
wp_enqueue_script($footer[$i]['handle']);
}
}
# remove from queue
$wp_scripts->done = $done;
}
###########################################
# process css in the footer ###############
###########################################
function fastvelocity_min_merge_footer_css() {
global $wp_styles, $protocol, $wp_domain, $wp_home, $wp_home_path, $cachedir, $cachedirurl, $ignore, $disable_css_merge, $disable_css_minification, $skip_clean_fonts, $skip_google_fonts, $skip_fontawesome_fonts, $skip_cssorder, $remove_print_mediatypes, $force_inline_css, $force_inline_googlefonts;
if (!is_object($wp_styles)) {
return false;
}
# process CSS in the footer
$styles = wp_clone($wp_styles);
$styles->all_deps($styles->queue);
$footer = array();
$google_fonts = array();
$colect_css_after = array();
# mark as done (as we go)
$done = $styles->done;
# try to find dynamically generated css files and add those to the ignore list
foreach ($styles->to_do as $handle) :
$hurl = fastvelocity_min_get_hurl($wp_styles->registered[$handle]->src, $wp_domain, $protocol, $wp_home);
if (stripos($hurl, '.php?') !== false && !fastvelocity_min_in_arrayi($hurl, $ignore)) {
$ignore[] = $hurl;
}
endforeach;
# google fonts to the top
foreach ($styles->to_do as $handle) :
# dequeue and get a list of google fonts, or requeue external
$hurl = fastvelocity_min_get_hurl($wp_styles->registered[$handle]->src, $wp_domain, $protocol, $wp_home);
if (stripos($hurl, 'fonts.googleapis.com') !== false) {
wp_dequeue_style($handle);
if (!$skip_google_fonts) {
$google_fonts[$handle] = $hurl;
} else {
wp_enqueue_style($handle);
} # skip google fonts optimization?
}
# add font awesome to the top (if used), delete duplicates
if (stripos($hurl, 'font-awesome.min.css') !== false || stripos($hurl, 'font-awesome.css') !== false) {
if (!$skip_fontawesome_fonts) {
$done = array_merge($done, array($handle)); # remove from queue
} else {
wp_enqueue_style($handle);
}
}
# failsafe
else {
wp_dequeue_style($handle);
wp_enqueue_style($handle);
}
endforeach;
# concat google fonts, if enabled
if (!$skip_google_fonts && count($google_fonts) > 0) {
$concat_google_fonts = fastvelocity_min_concatenate_google_fonts($google_fonts);
foreach ($google_fonts as $h => $a) {
$done = array_merge($done, array($h));
} # mark as done
if ($force_inline_googlefonts == false) {
wp_enqueue_style('footer-fvm-fonts', $concat_google_fonts, array(), null, 'all');
} else {
# don't re-download the fonts for the next 4 hours
$cache_key = 'fvm-cache-gfonts-footer';
$gfonts = get_transient($cache_key);
# save into cache if transient info not available
if (false === $cache_key) {
$gfonts = fastvelocity_file_get_contents_curl($concat_google_fonts, 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko'); # woff format only
$gfonts = fastvelocity_min_minify_css_string($gfonts);
set_transient($cache_key, $gfonts, 4 * HOUR_IN_SECONDS);
}
# inline google fonts
echo '<style type="text/css" media="all">' . $gfonts . '</style>';
}
}
# get groups of handles & latest modified date
foreach ($styles->to_do as $handle) :
# skip already processed google fonts
if (isset($google_fonts[$handle])) {
continue;
}
# get full url
$hurl = fastvelocity_min_get_hurl($wp_styles->registered[$handle]->src, $wp_domain, $protocol, $wp_home);
# skip ignore list, conditional css, external css
if ((!fastvelocity_min_in_arrayi($hurl, $ignore) && !isset($wp_scripts->registered[$handle]->extra["conditional"]) && substr($hurl, 0, strlen($wp_home)) === $wp_home) || empty($hurl)) {
# colect inline css for this handle
if (isset($wp_styles->registered[$handle]->extra['after']) && is_array($wp_styles->registered[$handle]->extra['after'])) {
$colect_css_after[$handle] = fastvelocity_min_minify_css_string(implode('', $wp_styles->registered[$handle]->extra['after'])); # save
$wp_styles->registered[$handle]->extra['after'] = null; # dequeue
}
# process
if (isset($footer[count($footer) - 1]['handle']) || count($footer) == 0 || $footer[count($footer) - 1]['media'] != $wp_styles->registered[$handle]->args) {
$media = isset($wp_styles->registered[$handle]->args) ? $wp_styles->registered[$handle]->args : 'all';
array_push($footer, array('modified' => 0, 'handles' => array(), 'media' => $media));
}
# get path and last modified date
$handlepath = str_ireplace($wp_home, $wp_home_path, $hurl);
$modified = 0;
if (is_file($handlepath)) {
$modified = filemtime($handlepath);
}
# push it to the array get latest modified time
array_push($footer[count($footer) - 1]['handles'], $handle);
if ($modified > $footer[count($footer) - 1]['modified']) {
$footer[count($footer) - 1]['modified'] = $modified;
}
# external and ignored css
} else {
array_push($footer, array('handle' => $handle));
}
endforeach;
# reorder CSS by mediatypes
if (!$skip_cssorder) {
if (count($footer) > 0) {
# get unique mediatypes and most recent modified time
$allmedia = array();
foreach ($footer as $key => $array) {
if (isset($array['media']) && isset($allmedia[$array['media']]) && isset($array['modified'])) {
if ($allmedia[$array['media']] <= $array['modified']) {
$allmedia[$array['media']] = $array['modified'];
unset($footer[$key]);
} else {
$allmedia[$array['media']] = $array['modified'];
unset($footer[$key]);
}
}
}
# extract handles by mediatype
$grouphandles = array();
foreach ($allmedia as $md => $dt) {
foreach ($footer as $array) {
if (isset($array['media']) && $array['media'] == $md) {
foreach ($array['handles'] as $h) {
$grouphandles[$md][] = $h;
}
}
}
}
# reset and reorder footer by mediatypes
foreach ($allmedia as $md => $dt) {
$footer[] = array('modified' => $dt, 'handles' => $grouphandles[$md], 'media' => $md);
}
}
}
# loop through footer css and merge
for ($i = 0, $l = count($footer); $i < $l; $i++) {
if (!isset($footer[$i]['handle'])) {
# static cache file info + done
$done = array_merge($done, $footer[$i]['handles']);
$hash = 'footer-' . hash('adler32', implode('', $footer[$i]['handles']));
$modified = $footer[$i]['modified'];
$cache_path = $cachedir . '/' . $hash . '-' . $modified . '.min.css';
$cache_lock = $cache_path . '.lock';
$cache_url = $cachedirurl . '/' . $hash . '-' . $modified . '.min.css';
# generate a new cache file
if (!file_exists($cache_path)) {
# create a lock file to prevent incomplete views, log start
file_put_contents($cache_lock, '', LOCK_EX);
file_put_contents($cache_path . '.txt', date('r') . " - PROCESSED:\n");
# minify and write to file
$css = '';
$log = '';
foreach ($footer[$i]['handles'] as $handle) :
if (!empty($wp_styles->registered[$handle]->src)) {
# get full url and path
$hurl = fastvelocity_min_get_hurl($wp_styles->registered[$handle]->src, $wp_domain, $protocol, $wp_home);
$handlepath = str_ireplace($wp_home, $wp_home_path, $hurl);
# process
$cssarr = array();
$cssarr = fastvelocity_min_minify_css($handle, $hurl, $handlepath, $skip_clean_fonts, $disable_css_minification);
$css .= $cssarr['css'];
$log .= $cssarr['log'];
# consider dependencies on handles with an empty src
} else {
wp_dequeue_style($handle);
wp_enqueue_style($handle);
}
endforeach;
# generate cache, write log
file_put_contents($cache_path, $css, LOCK_EX);
file_put_contents($cache_path . '.txt', $log . date('r') . " - ALL DONE\n", FILE_APPEND);
file_put_contents($cache_path . '.gz', gzencode(file_get_contents($cache_path), 9)); # useful for gzip_static on nginx
@unlink($cache_lock);
clearstatcache();
}
# register and enqueue minified file, consider excluding of mediatype "print" and inline css
if ($remove_print_mediatypes != 1 || ($remove_print_mediatypes == 1 && $footer[$i]['media'] != 'print')) {
if ($force_inline_css != false) {
echo '<style type="text/css" media="' . $footer[$i]['media'] . '">' . file_get_contents($cache_path) . '</style>';
if (count($colect_css_after) > 0) {
echo '<style type="text/css">' . implode('', $colect_css_after) . '</style>';
}
} else {
wp_register_style("fvm-footer-$i", $cache_url, array(), null, $footer[$i]['media']);
wp_enqueue_style("fvm-footer-$i");
if (count($colect_css_after) > 0) {
wp_add_inline_style("fvm-footer-$i", implode('', $colect_css_after));
} # add extra, inline css
}
}
# other css need to be requeued for the order of files to be kept
} else {
wp_dequeue_style($footer[$i]['handle']);
wp_enqueue_style($footer[$i]['handle']);
}
}
# remove from queue
$wp_styles->done = $done;
}
# enable defer for JavaScript (WP 4.1 and above) and remove query strings for ignored files
function fastvelocity_min_defer_js($tag, $handle, $src) {
global $ignore, $exclude_defer_jquery, $enable_defer_js, $defer_for_pagespeed;
if (stripos($src, '?v') !== false) {
$src = stristr($src, '?v', true);
} # no query strings
# if defer is enable, add jQuery to the ignore list (if required)
if ($exclude_defer_jquery == '1' && $enable_defer_js == '1' && (stripos($src, '/jquery.js') !== false || stripos($src, '/jquery.min.js') !== false)) {
if (!fastvelocity_min_in_arrayi($src, $ignore)) {
$ignore[] = $src;
}
}
# when to defer, order matters
$defer = 0;
if ($enable_defer_js == 1) {
$defer = 1;
}
if (fastvelocity_min_in_arrayi($src, $ignore)) {
$defer = 0;
}
if (fastvelocity_min_in_arrayi($src, $ignore) && $exclude_defer_jquery == 1) {
$defer = 0;
}
# skip the ignore list by default, defer the rest
if ($defer == 0) {
# remove defer and async tags
$tag = str_ireplace(array('async="async"', 'defer="defer"', ' async ', ' defer '), '', $tag);
# no defer
if ($defer_for_pagespeed != 1) {
return $tag;
} else {
# defer for pagespeed insights only
$deferinsights = <<<EOF
<script type="text/javascript">
if(navigator.userAgent.match(/Speed/i)) {
(function (d, defer, id) {var js, fjs = d.getElementsByTagName(s)[0];if (d.getElementById(id)){return;}js = d.createElement(s);js.defer = defer;js.src = "$src";fjs.parentNode.insertBefore(js, fjs);}(document, 'script', 'defer'));
} else { (function (d, s, id) {var js, fjs = d.getElementsByTagName(s)[0];if (d.getElementById(id)){return;}js = d.createElement(s);js.id = id;js.src = "$src";fjs.parentNode.insertBefore(js, fjs);}(document, 'script', '')); }
</script>
EOF;
# return code
if (!strpos($src, 'player.js'))
return preg_replace('#<script(.*?)>(.*?)</script>#is', $deferinsights, $tag);
}
# normal defer enabled
} else {
return str_ireplace(' src=', ' defer="defer" src=', $tag);
}
}
# process defering
if (!is_admin()) {
add_filter('script_loader_tag', 'fastvelocity_min_defer_js', 10, 3);
}
# enable html minification
if (!$skip_html_minification) {
add_action('get_header', 'fastvelocity_min_html_compression_start');
}