C:\xampp\htdocs\landing\wp-content\plugins\wp-asset-clean-up\classes\OptimiseAssets\OptimizeJs.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
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
<?php
namespace WpAssetCleanUp\OptimiseAssets;

use 
WpAssetCleanUp\FileSystem;
use 
WpAssetCleanUp\CleanUp;
use 
WpAssetCleanUp\Main;
use 
WpAssetCleanUp\MetaBoxes;
use 
WpAssetCleanUp\Misc;
use 
WpAssetCleanUp\ObjectCache;
use 
WpAssetCleanUp\Plugin;
use 
WpAssetCleanUp\Preloads;

/**
 * Class OptimizeJs
 * @package WpAssetCleanUp
 */
class OptimizeJs
{
    
/**
     *
     */
    
public function init()
    {
        
add_action'wp_print_footer_scripts', static function() {
            
/* [wpacu_timing] */ Misc::scriptExecTimer'prepare_optimize_files_js' ); /* [/wpacu_timing] */
            
self::prepareOptimizeList();
            
/* [wpacu_timing] */ Misc::scriptExecTimer'prepare_optimize_files_js''end' ); /* [/wpacu_timing] */
        
}, PHP_INT_MAX );
    }

    
/**
     *
     */
    
public static function prepareOptimizeList()
    {
        
// Are both Minify and Cache Dynamic JS disabled? No point in continuing and using extra resources as there is nothing to change
        
if ( ! self::isWorthCheckingForOptimization() || Plugin::preventAnyFrontendOptimization() ) {
            return;
        }

        global 
$wp_scripts;

        
$jsOptimizeList = array();

        
$wpScriptsDone  = isset($wp_scripts->done)  && is_array($wp_scripts->done)  ? $wp_scripts->done  : array();
        
$wpScriptsQueue = isset($wp_scripts->queue) && is_array($wp_scripts->queue) ? $wp_scripts->queue : array();

        
$wpScriptsList array_unique(array_merge($wpScriptsDone$wpScriptsQueue));

        
// Collect all enqueued clean (no query strings) HREFs to later compare them against any hardcoded JS
        
$allEnqueuedCleanScriptSrcs = array();

        if ( ! empty(
$wpScriptsList) ) {
            foreach ( 
$wpScriptsList as $index => $scriptHandle ) {
                if ( isset( 
Main::instance()->wpAllScripts['registered'][ $scriptHandle ]->src ) && ( $src Main::instance()->wpAllScripts['registered'][ $scriptHandle ]->src ) ) {
                    
$localAssetPath OptimizeCommon::getLocalAssetPath$src'js' );

                    if ( ! 
$localAssetPath || ! is_file$localAssetPath ) ) {
                        continue; 
// not a local file
                    
}

                    
ob_start();
                    
$wp_scripts->do_item$scriptHandle );
                    
$scriptSourceTag trimob_get_clean() );

                    
// Check if the JS has any 'data-wpacu-skip' attribute; if it does, do not alter it
                    
if ( preg_match'#data-wpacu-skip([=>/ ])#i'$scriptSourceTag ) ) {
                        unset( 
$wpScriptsList$index ] );
                        continue;
                    }

                    
$cleanScriptSrcFromTagArray OptimizeCommon::getLocalCleanSourceFromTag$scriptSourceTag'src' );

                    if ( isset( 
$cleanScriptSrcFromTagArray['source'] ) && $cleanScriptSrcFromTagArray['source'] ) {
                        
$allEnqueuedCleanScriptSrcs[] = $cleanScriptSrcFromTagArray['source'];
                    }
                }
            }
        }

        
// [Start] Collect for caching
        
if ( ! empty($wpScriptsList) ) {
            foreach ( 
$wpScriptsList as $handle ) {
                if ( ! isset( 
$wp_scripts->registered$handle ]->src ) ) {
                    continue;
                }

                
$value $wp_scripts->registered$handle ];

                
$localAssetPath OptimizeCommon::getLocalAssetPath$value->src'js' );
                if ( ! 
$localAssetPath || ! is_file$localAssetPath ) ) {
                    continue; 
// not a local file
                
}

                
$optimizeValues self::maybeOptimizeIt$value );
                
ObjectCache::wpacu_cache_set'wpacu_maybe_optimize_it_js_' $handle$optimizeValues );

                if ( ! empty( 
$optimizeValues ) ) {
                    
$jsOptimizeList[] = $optimizeValues;
                }
            }
        }

        
ObjectCache::wpacu_cache_add('wpacu_js_enqueued_srcs'$allEnqueuedCleanScriptSrcs);
        
ObjectCache::wpacu_cache_add('wpacu_js_optimize_list'$jsOptimizeList);
        
// [End] Collect for caching
    
}

    
/**
     * @param $value
     *
     * @return array
     */
    
public static function maybeOptimizeIt($value)
    {
        if (
$optimizeValues ObjectCache::wpacu_cache_get('wpacu_maybe_optimize_it_js_'.$value->handle)) {
            return 
$optimizeValues;
        }

        global 
$wp_version;

        
$src = isset($value->src) ? $value->src false;

        if (! 
$src) {
            return array();
        }

        
$doFileMinify true;

        if (! 
MinifyJs::isMinifyJsEnabled()) {
            
$doFileMinify false;
        } elseif (
MinifyJs::skipMinify($src$value->handle)) {
            
$doFileMinify false;
        }

        
// Default (it will be later replaced with the last time the file was modified, which is more accurate)
        
$dbVer = (isset($value->ver) && $value->ver) ? $value->ver $wp_version;

        
$isJsFile false;

        
$localAssetPath OptimizeCommon::getLocalAssetPath($src'js');
        if (
$localAssetPath && is_file($localAssetPath)) {
            if (
$fileMTime = @filemtime($localAssetPath)) {
                
$dbVer $fileMTime;
            }
            
$isJsFile true;
        }

        
$handleDbStr md5($value->handle);

        
$transientName 'wpacu_js_optimize_'.$handleDbStr;

        
$skipCache false;

        if (isset(
$_GET['wpacu_no_cache']) || (defined('WPACU_NO_CACHE') && WPACU_NO_CACHE === true)) {
            
$skipCache true;
        }

        if (! 
$skipCache) {
            if (
Main::instance()->settings['fetch_cached_files_details_from'] === 'db_disk') {
                if ( ! isset( 
$GLOBALS['wpacu_from_location_inc'] ) ) {
                    
$GLOBALS['wpacu_from_location_inc'] = 1;
                }
                
$fromLocation = ( $GLOBALS['wpacu_from_location_inc'] % ) ? 'db' 'disk';
            } else {
                
$fromLocation Main::instance()->settings['fetch_cached_files_details_from'];
            }

            
$savedValues OptimizeCommon::getTransient($transientName$fromLocation);

            if ( 
$savedValues ) {
                
$savedValuesArray json_decode($savedValuesARRAY_A);

                if ( 
$savedValuesArray['ver'] !== $dbVer ) {
                    
// New File Version? Delete transient as it will be re-added to the database with the new version
                    
OptimizeCommon::deleteTransient($transientName);
                } else {
                    
$localPathToJsOptimized str_replace'//''/'ABSPATH $savedValuesArray['optimize_uri'] );

                    
// Do not load any minified JS file (from the database transient cache) if it doesn't exist
                    // It will fallback to the original JS file
                    
if ( isset( $savedValuesArray['source_uri'] ) && is_file$localPathToJsOptimized ) ) {
                        if (
Main::instance()->settings['fetch_cached_files_details_from'] === 'db_disk') {
                            
$GLOBALS['wpacu_from_location_inc']++;
                        }

                        return array(
                            
$savedValuesArray['source_uri'],
                            
$savedValuesArray['optimize_uri'],
                            
$value->src,
                            
$value->handle
                        
);
                    }
                }
            }
        }

        
// Check if it starts without "/" or a protocol; e.g. "wp-content/theme/script.js"
        
if (strpos($src'/') !== &&
            
strpos($src'//') !== &&
            
stripos($src'http://') !== &&
            
stripos($src'https://') !== 0
        
) {
            
$src '/'.$src// append the forward slash to be processed as relative later on
        
}

        
// Starts with '/', but not with '//'
        
if (strpos($src'/') === && strpos($src'//') !== 0) {
            
$src site_url() . $src;
        }

        
/*
         * [START] JS Content Optimization
        */
        
if (Main::instance()->settings['cache_dynamic_loaded_js'] &&
            ((
strpos($src'/?') !== false) || strpos($src'.php?') !== false || Misc::endsWith($src'.php')) &&
            (
strpos($srcsite_url()) !== false)
        ) {
            
$pathToAssetDir '';
            
$sourceBeforeOptimization $value->src;

            if (! (
$jsContent DynamicLoadedAssets::getAssetContentFrom('dynamic'$value))) {
                return array();
            }
        } else {
            if (! 
$isJsFile) {
                return array();
            }

            
/*
             * This is a local .JS file
             */
            
$pathToAssetDir OptimizeCommon::getPathToAssetDir($value->src);
            
$sourceBeforeOptimization str_replace(ABSPATH'/'$localAssetPath);

            
$jsContent $jsContentBefore FileSystem::file_get_contents($localAssetPath);
        }

        
$hadToBeMinified false;

        
$jsContent trim($jsContent);

        
// If it stays like this, it means there is content there, even if only comments
        
$jsContentBecomesEmptyAfterMin false;

        if ( 
$doFileMinify && $jsContent ) { // only bother to minify it if it has any content, save resources
            // Minify this file?
            
$jsContentBeforeMin $jsContent;
            
$jsContentAfterMin  MinifyJs::applyMinification($jsContentBeforeMin);

            
$jsContent $jsContentAfterMin;

            if ( 
$jsContentBeforeMin && $jsContentAfterMin === '' ) {
                
// It had content, but became empty after minification, most likely it had only comments (e.g. a default child theme's style)
                
$jsContentBecomesEmptyAfterMin true;
            } else {
                
$jsContentCompare     md5(trim$jsContentBeforeMin'; ' ));
                
$jsContentCompareWith md5(trim$jsContentAfterMin'; ' ));

                if ( 
$jsContentCompare !== $jsContentCompareWith ) {
                    
$hadToBeMinified true;
                }
            }
        }

        if ( 
$jsContentBecomesEmptyAfterMin || $jsContent === '' ) {
            
$jsContent '/**/';
        } else {
            
$jsContentArray self::maybeAlterContentForJsFile$jsContentfalse );
            
$jsContent $jsContentArray['content']; // resulting content after alteration
            
$jsContentAfterAlterToCompare $jsContentArray['content_after_alter_to_compare'];

            if ( 
$isJsFile && ( ! $hadToBeMinified ) ) {
                
$jsContentCompare     md5(trim$jsContent'; ' ));
                
$jsContentCompareWith md5(trim$jsContentAfterAlterToCompare'; ' ));

                if ( 
$jsContentCompare === $jsContentCompareWith ) {
                    
// 1: The file was not minified
                    // 2: It doesn't need any alteration (e.g. no Google Fonts to strip from its content)
                    // No need to copy it in to the cache (save disk space)
                    
return array();
                }
            }

            
// Change the necessary relative paths before the file is copied to the caching directory (e.g. /wp-content/cache/asset-cleanup/)
            
$jsContent self::maybeDoJsFixes$jsContent$pathToAssetDir '/' );
        }
        
/*
         * [END] JS Content Optimization
        */

        // Relative path to the new file
        // Save it to /wp-content/cache/js/{OptimizeCommon::$optimizedSingleFilesDir}/
        
$fileVer sha1($jsContent);

        
$newFilePathUri  self::getRelPathJsCacheDir() . OptimizeCommon::$optimizedSingleFilesDir '/' sanitize_title($value->handle) . '-v' $fileVer;
        
$newFilePathUri .= '.js';

        if (
$jsContent === '') {
            
$jsContent '/**/';
        }

        if (
$jsContent === '/**/') {
            
// Leave a signature that the file is empty, thus it would be faster to take further actions upon it later on, saving resources)
            
$newFilePathUri str_replace('.js''-wpacu-empty-file.js'$newFilePathUri);
        }

        
$newLocalPath    WP_CONTENT_DIR $newFilePathUri// Ful Local path
        
$newLocalPathUrl WP_CONTENT_URL $newFilePathUri// Full URL path

        
if ($jsContent && $jsContent !== '/**/') {
            
$jsContent '/*!' $sourceBeforeOptimization '*/' "\n" $jsContent;
        }

        
$saveFile FileSystem::file_put_contents($newLocalPath$jsContent);

        if (! 
$saveFile || ! $jsContent) {
            
// Fallback to the original JS if the optimized version can't be created or updated
            
return array();
        }

        
$saveValues = array(
            
'source_uri'   => OptimizeCommon::getSourceRelPath($value->src),
            
'optimize_uri' => OptimizeCommon::getSourceRelPath($newLocalPathUrl),
            
'ver'          => $dbVer
        
);

        
// Add / Re-add (with new version) transient
        
OptimizeCommon::setTransient($transientNamejson_encode($saveValues));

        return array(
            
OptimizeCommon::getSourceRelPath($value->src), // Original SRC (Relative path)
            
OptimizeCommon::getSourceRelPath($newLocalPathUrl), // New SRC (Relative path)
            
$value->src// SRC (as it is)
            
$value->handle
        
);
    }

    
/**
     * This applies to both inline and static JS files contents
     *
     * @param $jsContent
     * @param bool $doJsMinify (false by default as it could be already minified or non-minify type)
     *
     * @return array
     */
    
public static function maybeAlterContentForJsFile($jsContent$doJsMinify false)
    {
        if (! 
trim($jsContent)) { // No Content! Return it as it, no point in doing extra checks
            
return array('content' => $jsContent);
        }

        
$jsContentBefore $jsContent;

        
/* [START] Change JS Content */
        
if ($doJsMinify) {
            
$jsContent MinifyJs::applyMinification($jsContent);
        }

        if (
Main::instance()->settings['google_fonts_remove']) {
            
$jsContent FontsGoogleRemove::stripReferencesFromJsCode($jsContent);
        } elseif (
Main::instance()->settings['google_fonts_display']) {
            
// Perhaps "display" parameter has to be applied to Google Font Links if they are active
            
$jsContent FontsGoogle::alterGoogleFontUrlFromJsContent($jsContent);
        }
        
/* [END] Change JS Content */

        // Does it have a source map? Strip it only if any optimization was already applied
        // As, otherwise, there's no point in creating a caching file, since there are no changes worth made to the file
        
if (($jsContentBefore !== $jsContent) && (strpos($jsContent'// #sourceMappingURL') !== false) && Misc::endsWith(trim($jsContent), '.map')) {
            
$jsContent OptimizeCommon::stripSourceMap($jsContent'js');
        }

        
$jsContentAfterAlterToCompare $jsContent// new possible values
        
return array('content' => $jsContent 'content_after_alter_to_compare' => $jsContentAfterAlterToCompare);
    }

    
/**
     * @param $jsContent
     * @param $doJsMinify
     *
     * @return false|mixed|string|string[]|null
     */
    
public static function maybeAlterContentForInlineScriptTag($jsContent$doJsMinify)
    {
        if (! 
trim($jsContent)) { // No Content! Return it as it, no point in doing extra checks
            
return $jsContent;
        }

        if (
mb_strlen($jsContent) > 500000) { // Bigger then ~500KB? Skip alteration for this inline SCRIPT
            
return $jsContent;
        }

        
$useCacheForInlineScript true;

        if (
mb_strlen($jsContent) < 40000) { // Smaller than ~40KB? Do not cache it
            
$useCacheForInlineScript false;
        }

        
// For debugging purposes
        
if (isset($_GET['wpacu_no_cache']) || (defined('WPACU_NO_CACHE') && WPACU_NO_CACHE === true)) { $useCacheForInlineScript false; }

        if (
$useCacheForInlineScript) {
            
// Anything in the cache? Take it from there and don't spend resources with the minification
            // (which in some environments uses the CPU, depending on the complexity of the JavaScript code) and any other alteration
            
$jsContentBeforeHash sha1$jsContent );

            
$pathToInlineJsOptimizedItem WP_CONTENT_DIR self::getRelPathJsCacheDir() . '/item/inline/' $jsContentBeforeHash '.js';

            
// Check if the file exists before moving forward
            
if ( is_file$pathToInlineJsOptimizedItem ) ) {
                
$cachedJsFileExpiresIn OptimizeCommon::$cachedAssetFileExpiresIn;

                if ( 
filemtime$pathToInlineJsOptimizedItem ) < ( time() - $cachedJsFileExpiresIn ) ) {
                    
// Has the caching period expired? Remove the file as a new one has to be generated
                    
@unlink$pathToInlineJsOptimizedItem );
                } else {
                    
// Not expired / Return its content from the cache in a faster way
                    
$inlineJsStorageItemJsonContent FileSystem::file_get_contents$pathToInlineJsOptimizedItem );

                    if ( 
$inlineJsStorageItemJsonContent !== '' ) {
                        return 
$inlineJsStorageItemJsonContent;
                    }
                }
            }
        }

        
/* [START] Change JS Content */
        
if ($doJsMinify) {
            
$jsContent MinifyJs::applyMinification($jsContent);
        }

        if (
Main::instance()->settings['google_fonts_remove']) {
            
$jsContent FontsGoogleRemove::stripReferencesFromJsCode($jsContent);
        } elseif (
Main::instance()->settings['google_fonts_display']) {
            
// Perhaps "display" parameter has to be applied to Google Font Links if they are active
            
$jsContent FontsGoogle::alterGoogleFontUrlFromJsContent($jsContent);
        }
        
/* [END] Change JS Content */

        
if ( $useCacheForInlineScript && isset($pathToInlineJsOptimizedItem) ) {
            
// Store the optimized content to the cached JS file which would be read quicker
            
FileSystem::file_put_contents$pathToInlineJsOptimizedItem$jsContent );
        }

        return 
$jsContent;
    }

    
/**
     * @param $htmlSource
     *
     * @return mixed
     */
    
public static function updateHtmlSourceOriginalToOptimizedJs($htmlSource)
    {
        
$jsOptimizeList ObjectCache::wpacu_cache_get('wpacu_js_optimize_list') ?: array();
        
$allEnqueuedCleanScriptSrcs ObjectCache::wpacu_cache_get('wpacu_js_enqueued_srcs') ?: array();

        
$cdnUrls OptimizeCommon::getAnyCdnUrls();
        
$cdnUrlForJs = isset($cdnUrls['js']) ? $cdnUrls['js'] : false;

        
preg_match_all('#(<script[^>]*src(|\s+)=(|\s+)[^>]*>)|(<link[^>]*(as(\s+|)=(\s+|)(|"|\')script(|"|\'))[^>]*>)#Umi'$htmlSource$matchesSourcesFromTagsPREG_SET_ORDER);

        
$scriptTagsToUpdate = array();

        foreach (
$matchesSourcesFromTags as $matches) {
            
$scriptSourceTag $matches[0];

            if (
strip_tags($scriptSourceTag) !== '') {
                
// Hmm? Not a valid tag... Skip it...
                
continue;
            }

            
// Check if the CSS has any 'data-wpacu-skip' attribute; if it does, do not alter it
            
if (preg_match('#data-wpacu-skip([=>/ ])#i'$scriptSourceTag)) {
                continue;
            }

            
$forAttr 'src';

            
// Any preloads for the optimized script?
            // e.g. <link rel='preload' as='script' href='...' />
            
if (strpos($scriptSourceTag'<link') !== false) {
                
$forAttr 'href';
            }

            
$cleanScriptSrcFromTagArray OptimizeCommon::getLocalCleanSourceFromTag($scriptSourceTag$forAttr);

            
// Skip external links, no point in carrying on
            
if (! $cleanScriptSrcFromTagArray || ! is_array($cleanScriptSrcFromTagArray)) {
                continue;
            }

            
// Is it a local JS? Check if it's hardcoded (not enqueued the WordPress way)
            
$cleanScriptSrcFromTag      $cleanScriptSrcFromTagArray['source'];
            
$afterQuestionMark          $cleanScriptSrcFromTagArray['after_question_mark'];

            if (! 
in_array($cleanScriptSrcFromTag$allEnqueuedCleanScriptSrcs)) {
                
// Not in the final enqueued list? Most likely hardcoded (not added via wp_enqueue_scripts())
                // Emulate the object value (as the enqueued styles)
                
$generatedHandle md5($cleanScriptSrcFromTag);

                
$value = (object)array(
                    
'handle' => $generatedHandle,
                    
'src'    => $cleanScriptSrcFromTag,
                    
'ver'    => md5($afterQuestionMark)
                );

                
$optimizeValues self::maybeOptimizeIt($value);
                
ObjectCache::wpacu_cache_set('wpacu_maybe_optimize_it_js_'.$generatedHandle$optimizeValues);

                if (! empty(
$optimizeValues)) {
                    
$jsOptimizeList[] = $optimizeValues;
                }
            }

            if (empty(
$jsOptimizeList)) {
                continue;
            }

            foreach (
$jsOptimizeList as $jsItemIndex => $listValues) {
                
// Index 0: Source URL (relative)
                // Index 1: New Optimized URL (relative)
                // Index 2: Source URL (as it is)

                // If the minified files are deleted (e.g. /wp-content/cache/ is cleared)
                // do not replace the JS file path to avoid breaking the website
                
$localPathOptimizedFile rtrim(ABSPATH'/') . $listValues[1];

                if (! 
is_file($localPathOptimizedFile)) {
                    continue;
                }

                
// Make sure the source URL gets updated even if it starts with // (some plugins/theme strip the protocol when enqueuing JavaScript files)
                
$siteUrlNoProtocol str_replace(array('http://''https://'), '//'site_url());

                
$sourceUrlList = array(
                    
site_url() . $listValues[0],
                    
$siteUrlNoProtocol $listValues[0]
                ); 
// array

                
if ($cdnUrlForJs) {
                    
// Does it have a CDN?
                    
$sourceUrlList[] = OptimizeCommon::cdnToUrlFormat($cdnUrlForJs'rel') . $listValues[0];
                }

                
// Any rel tag? You never know
                // e.g. <script src="/wp-content/themes/my-theme/script.js"></script>
                
if ( (strpos($listValues[2], '/') === && strpos($listValues[2], '//') !== 0)
                     || (
strpos($listValues[2], '/') !== &&
                         
strpos($listValues[2], '//') !== &&
                         
stripos($listValues[2], 'http://') !== &&
                         
stripos($listValues[2], 'https://') !== 0) ) {
                    
$sourceUrlList[] = $listValues[2];
                }

                
// If no CDN is set, it will return site_url() as a prefix
                
$optimizeUrl OptimizeCommon::cdnToUrlFormat($cdnUrlForJs'raw') . $listValues[1]; // string

                
if ($scriptSourceTag !== str_ireplace($sourceUrlList$optimizeUrl$scriptSourceTag)) {
                    
// Extra measure: Check the file size which should be 4 bytes, but add some margin error in case some environments will report less
                    
$isEmptyOptimizedFile = (strpos($localPathOptimizedFile'-wpacu-empty-file.js') !== false && filesize($localPathOptimizedFile) < 10);

                    if (
$isEmptyOptimizedFile) {
                        
// Strip it as its content (after optimization, for instance) is empty; no point in having extra HTTP requests
                        
$scriptTagsToUpdate[$scriptSourceTag.'</script>'] = '';

                        
// Note: As for September 3, 2020, the inline JS associated with the handle is no longer removed if the main JS file is empty
                        // There could be cases when the main JS file is empty, but the inline JS tag associated with it has code that is needed

                        
} else {
                        
$newScriptSourceTag self::updateOriginalToOptimizedTag$scriptSourceTag$sourceUrlList$optimizeUrl );
                        
$scriptTagsToUpdate[$scriptSourceTag] = $newScriptSourceTag;
                    }

                    unset(
$jsOptimizeList[$jsItemIndex]); // item from the array is not needed anymore
                    
break;
                }
            }
        }

        
$htmlSource strtr($htmlSource$scriptTagsToUpdate);

        return 
$htmlSource;
    }

    
/**
     * @param $scriptSourceTag string
     * @param $sourceUrl array
     * @param $optimizeUrl string
     *
     * @return mixed
     */
    
public static function updateOriginalToOptimizedTag($scriptSourceTag$sourceUrl$optimizeUrl)
    {
        
$newScriptSourceTag str_replace($sourceUrl$optimizeUrl$scriptSourceTag);
        
$sourceUrlRel is_array($sourceUrl) ? OptimizeCommon::getSourceRelPath($sourceUrl[0]) : OptimizeCommon::getSourceRelPath($sourceUrl);
        
$newScriptSourceTag str_ireplace('<script ''<script data-wpacu-script-rel-src-before="'.$sourceUrlRel.'" '$newScriptSourceTag);

        
preg_match_all'#\ssrc=(["\'])(.*?)(["\'])#'$scriptSourceTag$outputMatchesSrc );

        
// No space from the matching and ? should be there
        
if (isset( $outputMatchesSrc[2][0] ) && ( strpos$outputMatchesSrc[2][0], ' ' ) === false )) {
            if ( 
strpos$outputMatchesSrc[2][0], '?' ) !== false ) {
                
// Strip things like ?ver=
                
list( , $toStrip ) = explode'?'$outputMatchesSrc[2][0] );
                
$toStrip            '?' trim$toStrip );
                
$newScriptSourceTag str_replace$toStrip''$newScriptSourceTag );
            }

            if ( 
strpos$outputMatchesSrc[2][0], '&#038;ver' ) !== false ) {
                
// Replace any .js&#038;ver with .js
                
$toStrip strrchr($outputMatchesSrc[2][0], '&#038;ver');
                
$newScriptSourceTag str_replace$toStrip''$newScriptSourceTag );
            }
        }

        global 
$wp_version;

        
$newScriptSourceTag str_replace('.js&#038;ver='.$wp_version'.js'$newScriptSourceTag);
        
$newScriptSourceTag str_replace('.js&#038;ver=''.js'$newScriptSourceTag);

        
$newScriptSourceTag preg_replace('!\s+!'' '$newScriptSourceTag); // replace multiple spaces with only one space

        
return $newScriptSourceTag;
    }

    
/**
     * @param $htmlSource
     *
     * @return mixed|void
     */
    
public static function alterHtmlSource($htmlSource)
    {
        
// There has to be at least one "<script", otherwise, it could be a feed request or something similar (not page, post, homepage etc.)
        
if (stripos($htmlSource'<script') === false || array_key_exists('wpacu_no_optimize_js'$_GET)) {
            return 
$htmlSource;
        }

        
/* [wpacu_timing] */ Misc::scriptExecTimer'alter_html_source_for_optimize_js' ); /* [/wpacu_timing] */

        /* [wpacu_pro] */
$htmlSource apply_filters('wpacu_pro_maybe_move_jquery_after_body_tag'$htmlSource);/* [/wpacu_pro] */

        
if (! Main::instance()->preventAssetsSettings()) {
            
/* [wpacu_timing] */ $wpacuTimingName 'alter_html_source_unload_ignore_deps_js'Misc::scriptExecTimer($wpacuTimingName); /* [/wpacu_timing] */
            // Are there any assets unloaded where their "children" are ignored?
            // Since they weren't dequeued the WP way (to avoid unloading the "children"), they will be stripped here
            
$htmlSource self::ignoreDependencyRuleAndKeepChildrenLoaded($htmlSource);
            
/* [wpacu_timing] */ Misc::scriptExecTimer($wpacuTimingName'end'); /* [/wpacu_timing] */

            // Move any jQuery inline SCRIPT that is triggered before jQuery library is called through "jquery-core" handle
            
if (Main::instance()->settings['move_inline_jquery_after_src_tag']) {
                
/* [wpacu_timing] */ $wpacuTimingName 'alter_html_source_move_inline_jquery_after_src_tag'Misc::scriptExecTimer($wpacuTimingName); /* [/wpacu_timing] */
                
$htmlSource self::moveInlinejQueryAfterjQuerySrc($htmlSource);
                
/* [wpacu_timing] */ Misc::scriptExecTimer($wpacuTimingName'end'); /* [/wpacu_timing] */
            
}
        }

        
/*
         * The JavaScript files only get cached if they are minified or are loaded like /?custom-js=version - /script.php?ver=1 etc.
         * #optimizing
         * STEP 2: Load optimize-able caching list and replace the original source URLs with the new cached ones
         */

        // At least minify or cache dynamic loaded JS has to be enabled to proceed
        
if (self::isWorthCheckingForOptimization()) {
            
/* [wpacu_timing] */ $wpacuTimingName 'alter_html_source_original_to_optimized_js'Misc::scriptExecTimer($wpacuTimingName); /* [/wpacu_timing] */
            // 'wpacu_js_optimize_list' caching list is also checked; if it's empty, no optimization is made
            
$htmlSource self::updateHtmlSourceOriginalToOptimizedJs($htmlSource);
            
/* [wpacu_timing] */ Misc::scriptExecTimer($wpacuTimingName'end'); /* [/wpacu_timing] */
        
}

        if (! 
Main::instance()->preventAssetsSettings()) {
            
/* [wpacu_timing] */ $wpacuTimingName 'alter_html_source_for_preload_js'Misc::scriptExecTimer($wpacuTimingName); /* [/wpacu_timing] */
            
$preloads Preloads::instance()->getPreloads();

            if (isset(
$preloads['scripts']) && ! empty($preloads['scripts'])) {
                
$htmlSource Preloads::appendPreloadsForScriptsToHead($htmlSource);
            }

            
$htmlSource str_replace(Preloads::DEL_SCRIPTS_PRELOADS''$htmlSource);
            
/* [wpacu_timing] */ Misc::scriptExecTimer($wpacuTimingName'end'); /* [/wpacu_timing] */
        
}

        
/* [wpacu_timing] */ $wpacuTimingName 'alter_html_source_for_combine_js';

        
Misc::scriptExecTimer($wpacuTimingName); /* [/wpacu_timing] */
        
$proceedWithCombineOnThisPage true;

        
// If "Do not combine JS on this page" is checked in "Asset CleanUp Options" side meta box
        // Works for posts, pages and custom post types
        
if (defined('WPACU_CURRENT_PAGE_ID') && WPACU_CURRENT_PAGE_ID 0) {
            
$pageOptions MetaBoxes::getPageOptionsWPACU_CURRENT_PAGE_ID );

            
// 'no_js_optimize' refers to avoid the combination of JS files
            
if ( isset( $pageOptions['no_js_optimize'] ) && $pageOptions['no_js_optimize'] ) {
                
$proceedWithCombineOnThisPage false;
            }
        }

        if (
$proceedWithCombineOnThisPage) {
            
/* [wpacu_timing] */ // Note: Load timing is checked within the method /* [/wpacu_timing] */
            
$htmlSource CombineJs::doCombine($htmlSource);
        }
        
/* [wpacu_timing] */ Misc::scriptExecTimer($wpacuTimingName'end'); /* [/wpacu_timing] */

        
if (self::isWorthCheckingForOptimization() && ! Main::instance()->preventAssetsSettings() && (MinifyJs::isMinifyJsEnabled() && Main::instance()->settings['minify_loaded_js_inline'])) {
            
/* [wpacu_timing] */ $wpacuTimingName 'alter_html_source_for_minify_inline_script_tags'Misc::scriptExecTimer($wpacuTimingName); /* [/wpacu_timing] */
            
$htmlSource MinifyJs::minifyInlineScriptTags($htmlSource);
            
/* [wpacu_timing] */ Misc::scriptExecTimer($wpacuTimingName'end'); /* [/wpacu_timing] */
        
}

        
// Final cleanups
        
$htmlSource preg_replace('#(\s+|)(data-wpacu-jquery-core-handle=1|data-wpacu-jquery-migrate-handle=1)(\s+|)#Umi'' '$htmlSource);

        
$htmlSource preg_replace('#(\s+|)data-wpacu-script-rel-src-before=(["\'])' '(.*)' '(\1)(\s+|)#Usmi'' '$htmlSource);
        
$htmlSource preg_replace('#<script(.*)data-wpacu-script-handle=\'(.*)\'#Umi''<script \\1'$htmlSource);
        
$htmlSource preg_replace('#<script(\s+)src=\'#Umi''<script src=\''$htmlSource);

        
// Clear possible empty SCRIPT tags (e.g. left from associated 'before' and 'after' tags after their content was stripped)
        
$htmlSource preg_replace('#<script(\s+| )(type=\'text/javascript\'|)(\s+|)></script>#Umi'''$htmlSource);

        
/* [wpacu_timing] */ Misc::scriptExecTimer('alter_html_source_for_optimize_js''end'); /* [/wpacu_timing] */
        
return $htmlSource;
    }

    
/**
     * @return string
     */
    
public static function getRelPathJsCacheDir()
    {
        return 
OptimizeCommon::getRelPathPluginCacheDir().'js/'// keep trailing slash at the end
    
}

    
/**
     * @param $scriptSrcs
     * @param $htmlSource
     *
     * @return array
     */
    
public static function getScriptTagsFromSrcs($scriptSrcs$htmlSource)
    {
        
$scriptTags = array();

        foreach (
$scriptSrcs as $scriptSrc) {
            
$scriptSrc str_replace('{site_url}'''$scriptSrc);

            
// Clean it up for the preg_quote() call
            
if (strpos($scriptSrc'.js?') !== false) {
                list(
$scriptSrc,) = explode('.js?'$scriptSrc);
                
$scriptSrc .= '.js';
            }

            
preg_match_all('#<script[^>]*src(|\s+)=(|\s+)[^>]*'preg_quote($scriptSrc'/'). '.*(>)(.*|)</script>#Usmi'$htmlSource$matchesFromSrcPREG_SET_ORDER);

            if (isset(
$matchesFromSrc[0][0]) && strip_tags($matchesFromSrc[0][0]) === '') {
                
$scriptTags[] = trim($matchesFromSrc[0][0]);
            }
        }

        return 
$scriptTags;
    }

    
/**
     * @param $strFind
     * @param $strReplaceWith
     * @param $string
     *
     * @return mixed
     */
    
public static function strReplaceOnce($strFind$strReplaceWith$string)
    {
        if ( 
strpos($string$strFind) === false ) {
            return 
$string;
        }

        
$occurrence strpos($string$strFind);
        return 
substr_replace($string$strReplaceWith$occurrencestrlen($strFind));
    }

    
/**
     * @param $jsContent
     * @param $appendBefore
     *
     * @return mixed
     */
    
public static function maybeDoJsFixes($jsContent$appendBefore)
    {
        
// Relative URIs for CSS Paths
        // For code such as:
        // $(this).css("background", "url('../images/image-1.jpg')");

        
$jsContentPathReps = array(
            
'url("../' => 'url("'.$appendBefore.'../',
            
"url('../" => "url('".$appendBefore.'../',
            
'url(../'  => 'url('.$appendBefore.'../',

            
'url("./'  => 'url("'.$appendBefore.'./',
            
"url('./"  => "url('".$appendBefore.'./',
            
'url(./'   => 'url('.$appendBefore.'./'
        
);

        
$jsContent str_replace(array_keys($jsContentPathReps), array_values($jsContentPathReps), $jsContent);

        
$jsContent trim($jsContent);

        if (
substr($jsContent, -1) !== ';') {
            
$jsContent .= "\n" ';'// add semicolon as the last character
        
}

        return 
$jsContent;
    }

    
/**
     * @param $htmlSource
     *
     * @return false|mixed|string|void
     */
    
public static function moveInlinejQueryAfterjQuerySrc($htmlSource)
    {
        if (
stripos($htmlSource'<script') === false) {
            return 
$htmlSource// no SCRIPT tags, hmm
        
}

        if (! (
function_exists('libxml_use_internal_errors') && function_exists('libxml_clear_errors') && class_exists('\DOMDocument')) && class_exists('\DOMXpath')) {
            return 
$htmlSource// DOMDocument has to be enabled
        
}

        
$domTag OptimizeCommon::getDomLoadedTag($htmlSource'moveInlinejQueryAfterjQuerySrc');

        
$scriptTagsObj $domTag->getElementsByTagName'script' );

        if (
$scriptTagsObj === null) {
            return 
$htmlSource;
        }

        
// Does it have the "src" attribute? Skip it as it's not an inline SCRIPT tag
        
$jQueryPatternsToMatch = array(
            
'jQuery',
            
'\$(\s+|)\((\s+|)document(\s+|)\)(\s+|).(\s+|)ready(\s+|)\('
        
);

        
$jQueryRegExp '#' implode('|'$jQueryPatternsToMatch) . '#si';

        
$jQueryCoreDel    'data-wpacu-jquery-core-handle=';
        
$jQueryMigrateDel 'data-wpacu-jquery-migrate-handle=';

        if (
strpos($htmlSource$jQueryMigrateDel) !== false) {
            
$collectUntil $jQueryMigrateDel;
        } elseif (
strpos($htmlSource$jQueryCoreDel) !== false) {
            
$collectUntil $jQueryCoreDel;
        } else {
            return 
$htmlSource// No jQuery or jQuery Migrate? Just return the HTML source
        
}

        
$inlineBeforejQuerySrc = array();

        foreach (
$scriptTagsObj as $scriptTagObj) {
            
$tagContents $scriptTagObj->nodeValue;

            if (
strpos(CleanUp::getOuterHTML($scriptTagObj), $collectUntil) !== false) {
                break;
            }

            if (
$tagContents !== '' && preg_match($jQueryRegExp$tagContents)) {
                
preg_match('#<script[^>]*>'.preg_quote($tagContents'/').'</script>#si'$htmlSource$matchesExact);
                
$exactMatchTag = isset($matchesExact[0]) ? $matchesExact[0] : '';

                
// Replace the first match only in rare cases there are multiple SCRIPT tags with the same code
                
if ($exactMatchTag && ($pos strpos($htmlSource$exactMatchTag)) !== false) {
                    
$inlineBeforejQuerySrc[] = $exactMatchTag;
                    
$htmlSource substr_replace($htmlSource''$posstrlen($exactMatchTag));
                }
            }
        }

        
preg_match('#<script* '.$collectUntil.'*[^>]*>(.*?)</script>#si'$htmlSource$matches);

        if (! empty(
$inlineBeforejQuerySrc) && $collectUntil && isset($matches[0])) {
            
$htmlSource preg_replace('#<script* '.$collectUntil.'*[^>]*>(.*?)</script>#si'$matches[0]."\n".implode("\n"$inlineBeforejQuerySrc), $htmlSource);
        }

        return 
$htmlSource;
    }

    
/**
     * @param string $returnType
     * 'list' - will return the list of plugins that have JS optimization enabled
     * 'if_enabled' - will stop when it finds the first one (any order) and return true
     * @return array|bool
     */
    
public static function isOptimizeJsEnabledByOtherParty($returnType 'list')
    {
        
$pluginsToCheck = array(
            
'autoptimize/autoptimize.php'            => 'Autoptimize',
            
'wp-rocket/wp-rocket.php'                => 'WP Rocket',
            
'wp-fastest-cache/wpFastestCache.php'    => 'WP Fastest Cache',
            
'w3-total-cache/w3-total-cache.php'      => 'W3 Total Cache',
            
'sg-cachepress/sg-cachepress.php'        => 'SG Optimizer',
            
'fast-velocity-minify/fvm.php'           => 'Fast Velocity Minify',
            
'litespeed-cache/litespeed-cache.php'    => 'LiteSpeed Cache',
            
'swift-performance-lite/performance.php' => 'Swift Performance Lite',
            
'breeze/breeze.php'                      => 'Breeze – WordPress Cache Plugin'
        
);

        
$jsOptimizeEnabledIn = array();

        foreach (
$pluginsToCheck as $plugin => $pluginTitle) {
            
// "Autoptimize" check
            
if ($plugin === 'autoptimize/autoptimize.php' && Misc::isPluginActive($plugin) && get_option('autoptimize_js')) {
                
$jsOptimizeEnabledIn[] = $pluginTitle;

                if (
$returnType === 'if_enabled') { return true; }
            }

            
// "WP Rocket" check
            
if ($plugin === 'wp-rocket/wp-rocket.php' && Misc::isPluginActive($plugin)) {
                if (
function_exists('get_rocket_option')) {
                    
$wpRocketMinifyJs get_rocket_option('minify_js');
                    
$wpRocketMinifyConcatenateJs get_rocket_option('minify_concatenate_js');
                } else {
                    
$wpRocketSettings  get_option('wp_rocket_settings');
                    
$wpRocketMinifyJs = isset($wpRocketSettings['minify_js']) ? $wpRocketSettings['minify_js'] : false;
                    
$wpRocketMinifyConcatenateJs = isset($wpRocketSettings['minify_concatenate_js']) ? $wpRocketSettings['minify_concatenate_js'] : false;
                }

                if (
$wpRocketMinifyJs || $wpRocketMinifyConcatenateJs) {
                    
$jsOptimizeEnabledIn[] = $pluginTitle;

                    if (
$returnType === 'if_enabled') { return true; }
                }
            }

            
// "WP Fastest Cache" check
            
if ($plugin === 'wp-fastest-cache/wpFastestCache.php' && Misc::isPluginActive($plugin)) {
                
$wpfcOptionsJson get_option('WpFastestCache');
                
$wpfcOptions = @json_decode($wpfcOptionsJsonARRAY_A);

                if (isset(
$wpfcOptions['wpFastestCacheMinifyJs']) || isset($wpfcOptions['wpFastestCacheCombineJs'])) {
                    
$jsOptimizeEnabledIn[] = $pluginTitle;

                    if (
$returnType === 'if_enabled') { return true; }
                }
            }

            
// "W3 Total Cache" check
            
if ($plugin === 'w3-total-cache/w3-total-cache.php' && Misc::isPluginActive($plugin)) {
                
$w3tcConfigMaster Misc::getW3tcMasterConfig();
                
$w3tcEnableJs = (int)trim(Misc::extractBetween($w3tcConfigMaster'"minify.js.enable":'','), '" ');

                if (
$w3tcEnableJs === 1) {
                    
$jsOptimizeEnabledIn[] = $pluginTitle;

                    if (
$returnType === 'if_enabled') { return true; }
                }
            }

            
// "SG Optimizer" check
            
if ($plugin === 'sg-cachepress/sg-cachepress.php' && Misc::isPluginActive($plugin)) {
                if (
class_exists('\SiteGround_Optimizer\Options\Options') && method_exists('\SiteGround_Optimizer\Options\Options''is_enabled')) {
                    if (@\
SiteGround_Optimizer\Options\Options::is_enabled'siteground_optimizer_optimize_javascript')) {
                        
$jsOptimizeEnabledIn[] = $pluginTitle;

                        if (
$returnType === 'if_enabled') { return true; }
                    }
                }
            }

            
// "Fast Velocity Minify" check
            
if ($plugin === 'fast-velocity-minify/fvm.php' && Misc::isPluginActive($plugin)) {
                
// It's enough if it's active due to its configuration
                
$jsOptimizeEnabledIn[] = $pluginTitle;

                if (
$returnType === 'if_enabled') { return true; }
            }

            
// "LiteSpeed Cache" check
            
if ($plugin === 'litespeed-cache/litespeed-cache.php' && Misc::isPluginActive($plugin) && ($liteSpeedCacheConf apply_filters('litespeed_cache_get_options'get_option('litespeed-cache-conf')))) {
                if ( (isset(
$liteSpeedCacheConf['js_minify']) && $liteSpeedCacheConf['js_minify'])
                     || (isset(
$liteSpeedCacheConf['js_combine']) && $liteSpeedCacheConf['js_combine']) ) {
                    
$jsOptimizeEnabledIn[] = $pluginTitle;

                    if (
$returnType === 'if_enabled') { return true; }
                }
            }

            
// "Swift Performance Lite" check
            
if ($plugin === 'swift-performance-lite/performance.php' && Misc::isPluginActive($plugin)
                && 
class_exists('Swift_Performance_Lite') && method_exists('Swift_Performance_Lite''check_option')) {
                if ( @\
Swift_Performance_Lite::check_option('merge-scripts'1) ) {
                    
$jsOptimizeEnabledIn[] = $pluginTitle;
                }

                if (
$returnType === 'if_enabled') { return true; }
            }

            
// "Breeze – WordPress Cache Plugin"
            
if ($plugin === 'breeze/breeze.php' && Misc::isPluginActive($plugin)) {
                
$breezeBasicSettings    get_option('breeze_basic_settings');
                
$breezeAdvancedSettings get_option('breeze_advanced_settings');

                if (isset(
$breezeBasicSettings['breeze-minify-js'], $breezeAdvancedSettings['breeze-group-js'])
                    && 
$breezeBasicSettings['breeze-minify-js'] && $breezeAdvancedSettings['breeze-group-js']) {
                    
$jsOptimizeEnabledIn[] = $pluginTitle;

                    if (
$returnType === 'if_enabled') { return true; }
                }
            }
        }

        if (
$returnType === 'if_enabled') { return false; }

        return 
$jsOptimizeEnabledIn;
    }

    
/**
     * @return bool
     */
    
public static function isWorthCheckingForOptimization()
    {
        
// At least one of these options have to be enabled
        // Otherwise, we will not perform specific useless actions and save resources
        
return MinifyJs::isMinifyJsEnabled() ||
               
Main::instance()->settings['cache_dynamic_loaded_js'] ||
               
Main::instance()->settings['google_fonts_display'] ||
               
Main::instance()->settings['google_fonts_remove'];
    }

    
/**
     * @param $htmlSource
     *
     * @return mixed
     */
    
public static function ignoreDependencyRuleAndKeepChildrenLoaded($htmlSource)
    {
        
$ignoreChild Main::instance()->getIgnoreChildren();

        if (isset(
$ignoreChild['scripts']) && ! empty($ignoreChild['scripts'])) {
            foreach (
array_keys($ignoreChild['scripts']) as $scriptHandle) {
                if (isset(
Main::instance()->wpAllScripts['registered'][$scriptHandle]->srcMain::instance()->ignoreChildren['scripts'][$scriptHandle.'_has_unload_rule']) && ($scriptSrc Main::instance()->wpAllScripts['registered'][$scriptHandle]->src) && Main::instance()->ignoreChildren['scripts'][$scriptHandle.'_has_unload_rule']) {
                    
$toReplaceTagList = array();

                    
// If the handle has any inline JavaScript associated with it (before or after the tag), make sure it's stripped as well
                    
if ($translationsContent self::generateInlineAssocHtmlForHandle($scriptHandle'translations')) {
                        
$toReplaceTagList[] = $translationsContent;
                    }

                    if (
$cDataContent self::generateInlineAssocHtmlForHandle($scriptHandle'data')) {
                        
$toReplaceTagList[] = $cDataContent;
                    }

                    if (
$beforeContent self::generateInlineAssocHtmlForHandle($scriptHandle'before')) {
                        
$toReplaceTagList[] = $beforeContent;
                    }

                    
$toReplaceTagList[] = self::getScriptTagFromHandle(array('data-wpacu-script-handle=[\'"]' $scriptHandle '[\'"]'), $htmlSource);

                    if (
$afterContent self::generateInlineAssocHtmlForHandle($scriptHandle'after')) {
                        
$toReplaceTagList[] = $afterContent;
                    }

                    
$htmlSource str_replace($toReplaceTagList''$htmlSource);

                    
// Extra, in case the previous replace didn't go through
                    
$listWithMatches   = array();
                    
$listWithMatches[] = 'data-wpacu-script-handle=[\'"]'.$scriptHandle.'[\'"]';
                    
$listWithMatches[] = preg_quote(OptimizeCommon::getSourceRelPath($scriptSrc), '/');

                    
$htmlSource CleanUp::cleanScriptTagFromHtmlSource($listWithMatches$htmlSource);
                }
            }
        }

        return 
$htmlSource;
    }


    
/**
     * This would fetch the content of the SCRIPT tag (data, before & after)
     *
     * @param $scriptTagOrHandle
     * @param $wpacuRegisteredScripts
     * @param string $from
     * @param string $return ("value": JS Inline Content / "html": JS Inline Content surrounded by tags)
     *
     * @return array
     */
    
public static function getInlineAssociatedWithScriptHandle($scriptTagOrHandle$wpacuRegisteredScripts$from 'tag'$return 'value')
    {
        if (
$from === 'tag') {
            
preg_match_all('#data-wpacu-script-handle=([\'])' '(.*)' '(\1)#Usmi'$scriptTagOrHandle$outputMatches);
            
$scriptHandle = (isset($outputMatches[2][0]) && $outputMatches[2][0]) ? trim($outputMatches[2][0], '"\'') : '';
        } else { 
// 'handle'
            
$scriptHandle $scriptTagOrHandle;
        }

        if ( 
$return === 'value' && $scriptHandle ) {
            
$scriptExtraCdata $scriptExtraBefore $scriptExtraAfter '';

            if (isset(
$wpacuRegisteredScripts[$scriptHandle]->extra)) {
                
$scriptExtraArray $wpacuRegisteredScripts$scriptHandle ]->extra;

                if ( isset( 
$scriptExtraArray['data'] ) && $scriptExtraArray['data'] ) {
                    
$scriptExtraCdata .= $scriptExtraArray['data'] . "\n";
                }

                if ( isset( 
$scriptExtraArray['before'] ) && ! empty( $scriptExtraArray['before'] ) ) {
                    foreach ( 
$scriptExtraArray['before'] as $beforeData ) {
                        if ( ! 
is_bool$beforeData ) ) {
                            
$scriptExtraBefore .= $beforeData "\n";
                        }
                    }
                }

                if ( isset( 
$scriptExtraArray['after'] ) && ! empty( $scriptExtraArray['after'] ) ) {
                    foreach ( 
$scriptExtraArray['after'] as $afterData ) {
                        if ( ! 
is_bool$afterData ) ) {
                            
$scriptExtraAfter .= $afterData "\n";
                        }
                    }
                }
            }

            
$scriptTranslations '';

            if (
method_exists('wp_scripts''print_translations')) {
                
$scriptTranslations wp_scripts()->print_translations$scriptHandlefalse );
            }

            return array(
                
'translations' => trim($scriptTranslations),
                
'data'   => trim($scriptExtraCdata),
                
'before' => trim($scriptExtraBefore),
                
'after'  => trim($scriptExtraAfter)
            );
        }

        if ( 
$return === 'html' && $scriptHandle ) {
            return array(
                
'translations' => self::generateInlineAssocHtmlForHandle($scriptHandle'translations'),
                
'data'         => self::generateInlineAssocHtmlForHandle($scriptHandle'data'),
                
'before'       => self::generateInlineAssocHtmlForHandle($scriptHandle'before'),
                
'after'        => self::generateInlineAssocHtmlForHandle($scriptHandle'after')
            );
        }

        return array(
'translations' => '''data' => '''before' => '''after' => '');
    }

    
/**
     * @param $handle
     * @param $position
     * @param $inlineScriptContent
     *
     * @return string
     */
    
public static function generateInlineAssocHtmlForHandle($handle$position$inlineScriptContent '')
    {
        global 
$wp_scripts;

        
$typeAttr '';

        if ( 
function_exists'is_admin' ) && ! is_admin() &&
             
function_exists'current_theme_supports' ) && ! current_theme_supports'html5''script' )
        ) {
            
$typeAttr " type='text/javascript'";
        }

        
$output '';

        if (
$position === 'translations') {
            
$translations false;

            if (
method_exists('wp_scripts''print_translations')) {
                
$translations $wp_scripts->print_translations$handlefalse );
            }

            if ( 
$translations ) {
                
$output sprintf"<script%s id='%s-js-translations'>\n%s\n</script>\n"$typeAttresc_attr$handle ), $translations );
            }
        }

        if ( 
$position === 'data' ) {
            if ( 
$inlineScriptContent === '' ) {
                
$inlineScriptContent $wp_scripts->get_data$handle'data' );

                if (! 
$inlineScriptContent) {
                    return 
'';
                }
            }

            
$output .= sprintf("<script%s id='%s-js-extra'>\n"$typeAttresc_attr($handle));

            
// CDATA is not needed for HTML 5.
            
if ( $typeAttr ) {
                
$output .= "/* <![CDATA[ */\n";
            }

            
$output .= $inlineScriptContent."\n";

            if ( 
$typeAttr ) {
                
$output .= "/* ]]> */\n";
            }

            
$output .= '</script>';
        }

        if ( 
$position === 'before' || $position === 'after' ) {
            if ( 
$inlineScriptContent === '' ) {
                
$inlineScriptContent $wp_scripts->print_inline_script$handle$positionfalse );

                if (! 
$inlineScriptContent) {
                    return 
'';
                }
            }

            if ( 
$inlineScriptContent ) {
                
$output sprintf"<script%s id='%s-js-%s'>\n%s\n</script>\n"$typeAttr$handle$position$inlineScriptContent );
            }
        }

        return 
$output;
    }

    
/**
     * @param $listWithPatterns
     * @param $htmlSource
     *
     * @return string
     */
    
public static function getScriptTagFromHandle($listWithPatterns$htmlSource)
    {
        if (empty(
$listWithPatterns)) {
            return 
'';
        }

        if (! 
is_array($listWithPatterns)) {
            
$listWithPatterns = array($listWithPatterns);
        }

        
preg_match_all(
            
'#<script[^>]*('.implode('|'$listWithPatterns).')[^>].*(>)#Usmi',
            
$htmlSource,
            
$matchesSourcesFromTags
        
);

        if (empty(
$matchesSourcesFromTags)) {
            return 
'';
        }

        if (isset(
$matchesSourcesFromTags[0]) && ! empty($matchesSourcesFromTags[0])) {
            foreach (
$matchesSourcesFromTags[0] as $matchesFromTag) {
                if (
stripos($matchesFromTag' src=') !== false && strip_tags($matchesFromTag) === '') {
                    return 
$matchesFromTag.'</script>';
                }
            }
        }

        return 
'';
    }

    }
x

Windows NT KPTV 6.2 build 9200 (Windows Server 2012 Datacenter Edition) i586