functions.php
14.8 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
<?php
# Consider fallback to PHP Minify [2016.08.01] from https://github.com/matthiasmullie/minify (must be defined on the outer scope)
require_once ($plugindir . 'libs/matthiasmullie/minify/src/Minify.php');
require_once ($plugindir . 'libs/matthiasmullie/minify/src/CSS.php');
require_once ($plugindir . 'libs/matthiasmullie/minify/src/JS.php');
require_once ($plugindir . 'libs/matthiasmullie/minify/src/Exception.php');
require_once ($plugindir . 'libs/matthiasmullie/path-converter/src/Converter.php');
use MatthiasMullie\Minify;
# use HTML minification
require_once ($plugindir . 'libs/mrclay/HTML.php');
# get remove urls with curl
function fastvelocity_file_get_contents_curl($url, $uagent=NULL) {
$ch = curl_init();
if(isset($uagent) && !empty($uagent)) { curl_setopt($ch,CURLOPT_USERAGENT, $uagent); }
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
# functions, get hurl info
function fastvelocity_min_get_hurl($src, $wp_domain, $protocol, $wp_home) {
$hurl = trim($src); if(empty($hurl)) { return $hurl; } # preserve empty source handles
#make sure wp_home doesn't have a forward slash
if(substr($wp_home, -1) == '/') { $wp_home = trim($wp_home, '/'); }
# apply some filters
if (substr($hurl, 0, 2) === "//") { $hurl = $protocol.ltrim($hurl, "/"); } # protocol only
if (substr($hurl, 0, 4) === "http" && stripos($hurl, $wp_domain) === false) { return $hurl; } # return if external domain
if (substr($hurl, 0, 4) !== "http" && stripos($hurl, $wp_domain) !== false) { $hurl = $wp_home.'/'.ltrim($hurl, "/"); } # protocol + home
# prevent double forward slashes in the middle
$hurl = str_ireplace('###', '://', str_ireplace('//', '/', str_ireplace('://', '###', $hurl)));
# consider different wp-content directory
$proceed = 0; if(!empty($wp_home)) {
$alt_wp_content = basename($wp_home);
if(substr($hurl, 0, strlen($alt_wp_content)) === $alt_wp_content) { $proceed = 1; }
}
# protocol + home for relative paths
if (substr($hurl, 0, 12) === "/wp-includes" || substr($hurl, 0, 9) === "/wp-admin" || substr($hurl, 0, 11) === "/wp-content" || $proceed == 1) {
$hurl = $wp_home.'/'.ltrim($hurl, "/"); }
return $hurl;
}
# functions, minify html
function fastvelocity_min_minify_html($html) {
global $hide_google_analytics_pagespeed;
# minify html
$html = fastvelocity_min_Minify_HTML::minify($html);
return $html;
}
# case-insensitive in_array() wrapper
function fastvelocity_min_in_arrayi($needle, $haystack){
return in_array(strtolower($needle), array_map('strtolower', $haystack));
}
# process minification of javascript files
function fastvelocity_min_minify_js_process($jsfile) {
# default settings
global $use_google_closure, $use_php_minify, $tmpdir, $plugindir;
# default cache path location
$jstmp = $tmpdir.'/'.hash('adler32', $jsfile).'-'.basename($jsfile);
$jstmplog = $jstmp.'.log';
# return from cache if it's still valid
if(file_exists($jstmp) && file_exists($jstmplog) && filesize($jstmp) > 0 && filemtime($jstmp) >= filemtime($jsfile)) {
$output = file_get_contents($jstmp); # use cache
return array('js'=>$output, 'log'=>file_get_contents($jstmplog).' [cache]');
exit();
}
# skip if PHP Minify is selected
if(!$use_php_minify) {
# check for exec + a supported java version
if(function_exists('exec') && exec('command -v java >/dev/null && echo "yes" || echo "no"') == 'yes') {
# define jar paths
$yui = $plugindir.'libs/jar/yuicompressor-2.4.8.jar';
$gog = $plugindir.'libs/jar/google-closure.jar';
# choose between YUI or Google Closure, YUI by default (faster)
if($use_google_closure) {
$cmd = 'java -jar '.$gog.' --warning_level QUIET --js '.$jsfile.' --js_output_file '.$jstmp;
$savelog = 'GOOGLE CLOSURE';
} else {
$cmd = 'java -jar '.$yui.' --preserve-semi '.$jsfile.' -o '.$jstmp;
$savelog = 'YUI COMPRESSOR';
}
# save log
file_put_contents($jstmplog, $savelog, LOCK_EX);
# clear utf8 bom
if(is_file($jstmp)) { file_put_contents($jstmp, fastvelocity_min_remove_utf8_bom(file_get_contents($jstmp)), LOCK_EX); }
# run local compiler
exec($cmd . ' 2>&1', $output);
if(count($output) == 0 && file_exists($jstmp)) {
$output = file_get_contents($jstmp);
return array('js'=>$output, 'log'=>$savelog);
exit();
}
}
}
# Fallback to PHP Minify [2016.08.01] from https://github.com/matthiasmullie/minify
$fixbom = fastvelocity_min_remove_utf8_bom(file_get_contents($jsfile)); # clear utf8 bom
$minifier = new Minify\JS($fixbom);
$jsmin = $minifier->minify();
if(!empty($jsmin)) {
$savelog = 'PHP MINIFY';
return array('js'=>$jsmin, 'log'=>$savelog);
exit();
}
# this is our last resort fallback
$savelog = 'MERGED ONLY';
file_put_contents($jstmplog, $savelog, LOCK_EX);
return array('js'=>file_get_contents($jsfile, LOCK_EX), 'log'=>$savelog);
exit();
}
# minify js on demand (one file at one time, for compatibility)
function fastvelocity_min_minify_js($handle, $url, $path, $nocompress) {
global $plugindir;
# prevent double forward slashes in the middle
$path = str_ireplace('###', '://', str_ireplace('//', '/', str_ireplace('://', '###', $path)));
$url = str_ireplace('###', '://', str_ireplace('//', '/', str_ireplace('://', '###', $url)));
# try to get the unminified version if available, for better compatibility
$xtralog = '';
$ignorehandles = array('jquery-core', 'jquery', 'jquery-migrate');
if (!fastvelocity_min_in_arrayi($handle, $ignorehandles)) {
$use = str_ireplace(array('.min.js', '-min.js'), '.js', $path);
if($use != $path && file_exists($use)) { $path = $use; $xtralog = ' - [minified '.basename($use).' for compatibility]'; }
if (stripos(basename($url), 'min.js') && empty($xtralog)) { $xtralog = ' [already minified]'; }
} else { $xtralog = ' - [already minified]'; }
# basic cleaning and minification
$js = preg_replace("/^\xEF\xBB\xBF/", '', file_get_contents($path)); # remove BOM
$js = trim(join("\n", array_map("trim", explode("\n", preg_replace('/\v+/', "\n", preg_replace('/\h+/', " ", $js)))))); # BASIC MINIFICATION
# jQuery no conflict mode
if (stripos(basename($path), 'jquery.js') !== false) { $js = $js."jQuery.noConflict();"; }
# exclude minification on already minified files + jquery (because minification might break those)
$excl = array('jquery.js', '.min.js', '-min.js');
foreach($excl as $e) { if (stripos(basename($path), $e) !== false) { $nocompress = true; break; } }
# default log
$loginfo = 'MERGED';
# minification (if allowed)
if (!$nocompress) {
$newjsarr = fastvelocity_min_minify_js_process($path);
if (empty($newjsarr['js'])) {
$xtralog = ' [empty file]';
} else {
$js = $newjsarr['js'];
$loginfo = $newjsarr['log'];
}
}
# define log
$log = " - $loginfo".$xtralog." - $handle - $url \n";
# fix compatibility when mergin some scripts
if(substr($js, -1) != ';') { $js = $js.";\n"; } $js = $js."\n";
# return html
return array('js'=> $js, 'log' => $log);
}
# minify css string with PHP Minify
function fastvelocity_min_minify_css_string($css) {
$minifier = new Minify\CSS($css);
$cssmin = $minifier->minify();
if(!empty($cssmin)) { return $cssmin; }
return $css;
}
# minify css on demand (one file at one time, for compatibility)
function fastvelocity_min_minify_css($handle, $url, $path, $skip_clean_fonts, $disable_css_minification) {
# default settings
global $tmpdir;
$savelog = 'MERGED';
$xtralog = '';
# must have, or log error
if(!is_file($path)) { return array('css'=> '', 'log' => "NOT FOUND [$path] - Permission denied or the file does not exist"); }
# default cache path location
$csstmp = $tmpdir.'/'.hash('adler32', $path).'-'.basename($path);
$csstmplog = $csstmp.'.log';
# return from cache if it's still valid
if(file_exists($csstmp) && file_exists($csstmplog) && filesize($csstmp) > 0 && filemtime($csstmp) >= filemtime($path)) {
$css = file_get_contents($csstmp, LOCK_EX);
$log = ' - '.file_get_contents($csstmplog)." [cache] - $handle - $url \n";
return array('css'=>$css, 'log'=>$log);
exit();
}
# basic processing with PHP
$css = preg_replace("/^\xEF\xBB\xBF/", '', file_get_contents($path)); # remove BOM
$css = preg_replace("/url\(\s*['\"]?(?!data:)(?!http)(?![\/'\"])(.+?)['\"]?\s*\)/i", "url(".dirname($url)."/$1)", $css); # fix paths
# remove query strings from fonts (for better seo, but add a small cache buster based on most recent updates)
if(!$skip_clean_fonts) { $css = preg_replace('/(.eot|.svg|.woff2|.woff|.ttf)+[?+](.+?)(\)|\'|\")/', "$1"."#".filemtime($path)."$3", $css); }
# minify CSS
if(!$disable_css_minification) {
$mincss = fastvelocity_min_minify_css_string($css);
$savelog = 'MINIFIED';
$css = $mincss;
if (empty($mincss)) { $xtralog = ' [empty file]'; }
}
# save css + logs
file_put_contents($csstmp, $css, LOCK_EX);
file_put_contents($csstmplog, $savelog, LOCK_EX);
# generate log
$log = " - $savelog".$xtralog." - $handle - $url \n";
# return html
return array('css'=> $css, 'log' => $log);
}
# functions to minify HTML
function fastvelocity_min_html_compression_finish($html) { return fastvelocity_min_minify_html($html); }
function fastvelocity_min_html_compression_start() {
echo "\r\n"; # improve ttfb
ob_start('fastvelocity_min_html_compression_finish');
}
# remove all cache files
function rrmdir($dir) {
if(is_dir(rtrim($dir, '/'))) {
if ($handle = opendir($dir.'/')) {
while (false !== ($file = readdir($handle))) {
@unlink($dir.'/'.$file);
}
closedir($handle); }
}
}
# Concatenate Google Fonts tags (http://fonts.googleapis.com/css?...)
function fastvelocity_min_concatenate_google_fonts($array) {
global $protocol;
# extract unique font families
$families = array(); foreach ($array as $font) {
# get fonts name, type and subset, remove wp query strings
$font = explode('family=', htmlspecialchars_decode(rawurldecode(urldecode($font))));
$a = explode('&v', end($font)); $font = trim(trim(trim(current($a)), ','));
# reprocess if fonts are already concatenated in this url
if(stristr($font, '|') !== FALSE) {
$multiple = explode('|', $font); if (count($multiple) > 0) { foreach ($multiple as $f) { $families[] = trim($f); } }
} else { $families[] = $font; }
}
# process types, subsets, merge, etc
$fonts = array();
foreach ($families as $font) {
# if no type or subset
if(stristr($font, ':') === FALSE) {
$fonts[] = array('name'=>$font, 'type'=>'', 'sub'=>'');
} else {
# get type and subset
$name = stristr($font, ':', true); # font name, before :
$ftype = trim(stristr($font, ':'), ':'); # second part of the string, after :
# get font types and subset
if(stristr($ftype, '&subset=') === FALSE) {
$fonts[] = array('name'=>$name, 'type'=>$ftype, 'sub'=>'');
} else {
$newftype = stristr($ftype, '&', true); # font type, before &
$subset = trim(str_ireplace('&subset=', '', stristr($ftype, '&'))); # second part of the string, after &
$fonts[] = array('name'=>$name, 'type'=>$newftype, 'sub'=>$subset);
}
}
}
# make sure we have unique font names, types and subsets
$ufonts = array(); foreach ($fonts as $f) { $ufonts[$f['name']] = $f['name']; } # unique font names
$usubsets = array(); foreach ($fonts as $f) { if(!empty($f['sub'])) { $usubsets[$f['sub']] = $f['sub']; } } # unique subsets
# prepare
$fonts_and_types = $ufonts;
# get unique types and subsets for each unique font name
foreach ($ufonts as $uf) {
# types
$utypes = array();
foreach ($fonts as $f) {
if($f['name'] == $uf && !empty($f['type'])) { $utypes = array_merge($utypes, explode(',', $f['type'])); }
}
# filter types
$utypes = array_unique($utypes);
sort($utypes);
$ntype = ''; if(count($utypes) > 0) { $ntype = ':'.implode(',', $utypes); } # types to append to the font name
# generate font url queries
$fonts_and_types[$uf] = str_ireplace(' ', '+', $uf).$ntype;
}
# concat fonts, generate unique google fonts url
if(count($fonts_and_types) > 0) {
$msubsets = ''; if(count($usubsets) > 0 && implode(',', $usubsets) != 'latin') { $msubsets = "&subset=".implode(',', $usubsets); } # merge subsets
return $protocol.'fonts.googleapis.com/css?family='.implode('|', $fonts_and_types).$msubsets;
}
return false;
}
# readme parser
function fastvelocity_min_readme($url) {
# read file
$file = @file_get_contents( $url );
if (empty($file)) { return '<strong>Readme Parser: readme.txt not found!</strong>'; }
// line end to \n
$file = preg_replace("/(\n\r|\r\n|\r|\n)/", "\n", $file);
// headlines
$s = array('===','==','=' );
$r = array('h2' ,'h3','h4');
for ( $x = 0; $x < sizeof($s); $x++ ) {
$file = preg_replace('/(.*?)'.$s[$x].'(?!\")(.*?)'.$s[$x].'(.*?)/', '$1<'.$r[$x].'>$2</'.$r[$x].'>$3', $file);
}
// inline
$s = array('\*\*','\`');
$r = array('b' ,'code');
for ( $x = 0; $x < sizeof($s); $x++ ) {
$file = preg_replace('/(.*?)'.$s[$x].'(?!\s)(.*?)(?!\s)'.$s[$x].'(.*?)/', '$1<'.$r[$x].'>$2</'.$r[$x].'>$3', $file);
}
// ' _italic_ '
$file = preg_replace('/(\s)_(\S.*?\S)_(\s|$)/', ' <em>$2</em> ', $file);
// ul lists
$s = array('\*','\+','\-');
for ( $x = 0; $x < sizeof($s); $x++ )
$file = preg_replace('/^['.$s[$x].'](\s)(.*?)(\n|$)/m', '<li>$2</li>', $file);
$file = preg_replace('/\n<li>(.*?)/', '<ul><li>$1', $file);
$file = preg_replace('/(<\/li>)(?!<li>)/', '$1</ul>', $file);
// ol lists
$file = preg_replace('/(\d{1,2}\.)\s(.*?)(\n|$)/', '<li>$2</li>', $file);
$file = preg_replace('/\n<li>(.*?)/', '<ol><li>$1', $file);
$file = preg_replace('/(<\/li>)(?!(\<li\>|\<\/ul\>))/', '$1</ol>', $file);
// ol screenshots style
$file = preg_replace('/(?=Screenshots)(.*?)<ol>/', '$1<ol class="readme-parser-screenshots">', $file);
// line breaks
$file = preg_replace('/(.*?)(\n)/', "<p>$1</p>", $file);
$file = preg_replace('/(1|2|3|4)(><br\/>)/', '$1>', $file);
$file = str_ireplace('</ul><br/>', '</ul>', $file);
# cleanup
$file = str_ireplace('<p></p>', '', $file);
$file = str_ireplace('<p><h4>', '<h4>', $file);
$file = str_ireplace('</h4></p>', '</h4>', $file);
// urls
$file = str_replace('http://www.', 'www.', $file);
$file = str_replace('www.', 'http://www.', $file);
$file = preg_replace('#(^|[^\"=]{1})(http://|ftp://|mailto:|https://)([^\s<>]+)([\s\n<>]|$)#', '$1<a target="_blank" href="$2$3">$2$3</a>$4', $file);
# extract faqs
$prefix = "Frequently Asked Questions";
$faq = substr($file, strpos($file, $prefix) + strlen($prefix));
$faq = substr($faq, 0, strpos($faq, '<p><h3>'));
return trim($faq);
}
# remove emoji support
function fastvelocity_min_disable_wp_emojicons() {
remove_action('wp_head', 'print_emoji_detection_script', 7);
remove_action('wp_print_styles', 'print_emoji_styles');
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
remove_action( 'admin_print_styles', 'print_emoji_styles' );
}
# remove UTF8 BOM
function fastvelocity_min_remove_utf8_bom($text) {
$bom = pack('H*','EFBBBF');
$text = preg_replace("/^$bom/", '', $text);
return $text;
}