Fast Velocity Minify - Version 3.1.8

Version Description

[2021.04.30] = * fixed missing dynamic css/js urls * fixed some relative static assets paths * added auto disabling of FVM on Web Stories (AMP)

Download this release

Release Info

Developer Alignak
Plugin Icon 128x128 Fast Velocity Minify
Version 3.1.8
Comparing to
See all releases

Code changes from version 3.1.7 to 3.1.8

Files changed (4) hide show
  1. fvm.php +1 -1
  2. inc/common.php +51 -30
  3. inc/frontend.php +51 -2
  4. readme.txt +6 -8
fvm.php CHANGED
@@ -6,7 +6,7 @@ Description: Improve your speed score on GTmetrix, Pingdom Tools and Google Page
6
  Author: Raul Peixoto
7
  Author URI: http://fastvelocity.com
8
  Text Domain: fast-velocity-minify
9
- Version: 3.1.7
10
  License: GPL2
11
 
12
  ------------------------------------------------------------------------
6
  Author: Raul Peixoto
7
  Author URI: http://fastvelocity.com
8
  Text Domain: fast-velocity-minify
9
+ Version: 3.1.8
10
  License: GPL2
11
 
12
  ------------------------------------------------------------------------
inc/common.php CHANGED
@@ -856,6 +856,11 @@ function fvm_replace_css_imports($css, $rq=null) {
856
  # download file, get contents, merge
857
  $ddl = array();
858
  $ddl = fvm_maybe_download($href);
 
 
 
 
 
859
 
860
  # if success
861
  if(isset($ddl['content'])) {
@@ -920,6 +925,9 @@ function fvm_extract_fonts($css_code) {
920
  $css_code_ff = str_replace('https://'.$fvm_urls['wp_domain'], '', $css_code_ff);
921
  $css_code_ff = str_replace('http://'.$fvm_urls['wp_domain'], '', $css_code_ff);
922
  $css_code_ff = str_replace('//'.$fvm_urls['wp_domain'], '', $css_code_ff);
 
 
 
923
 
924
  # return
925
  $result = array('code'=>$css_code, 'fonts'=>$css_code_ff);
@@ -1061,7 +1069,7 @@ function fvm_rewrite_cdn_url($url) {
1061
 
1062
  # get css font-face rules, original + simplified
1063
  function fvm_simplify_fontface($css_code) {
1064
-
1065
  $mff = array();
1066
  $before = array();
1067
  $after = array();
@@ -1078,22 +1086,22 @@ function fvm_simplify_fontface($css_code) {
1078
 
1079
  # woff
1080
  $fonts = array();
1081
- preg_match('/url\s*\(\s*[\'\"]*([^,\'\"]*)[\'\"]*\)\s*format\s*\([\'\"]*woff[\'\"]*\s*\)/Uui', $csr, $fonts);
1082
  if(isset($fonts[0])) { $cssrules[$k] = 'src:'.$fonts[0]; break; }
1083
 
1084
  # woff2
1085
  $fonts = array();
1086
- preg_match('/url\s*\(\s*[\'\"]*([^,\'\"]*)[\'\"]*\)\s*format\s*\([\'\"]*woff2[\'\"]*\s*\)/Uui', $csr, $fonts);
1087
  if(isset($fonts[0])) { $cssrules[$k] = 'src:'.$fonts[0]; break; }
1088
 
1089
  # svg
1090
  $fonts = array();
1091
- preg_match('/url\s*\(\s*[\'\"]*([^,\'\"]*)[\'\"]*\)\s*format\s*\([\'\"]*svg[\'\"]*\s*\)/Uui', $csr, $fonts);
1092
  if(isset($fonts[0])) { $cssrules[$k] = 'src:'.$fonts[0]; break; }
1093
 
1094
  # truetype
1095
  $fonts = array();
1096
- preg_match('/url\s*\(\s*[\'\"]*([^,\'\"]*)[\'\"]*\)\s*format\s*\([\'\"]*truetype[\'\"]*\s*\)/Uui', $csr, $fonts);
1097
  if(isset($fonts[0])) { $cssrules[$k] = 'src:'.$fonts[0]; break; }
1098
 
1099
  # delete other src:url rules
@@ -1145,6 +1153,11 @@ function fvm_get_css_from_file($tag) {
1145
  $ddl = array();
1146
  $ddl = fvm_maybe_download($href);
1147
 
 
 
 
 
 
1148
  # success
1149
  if(isset($ddl['content'])) {
1150
 
@@ -1163,7 +1176,7 @@ function fvm_get_css_from_file($tag) {
1163
  # handle import rules
1164
  $css = fvm_replace_css_imports($css, $href);
1165
  $meta = json_encode(array('href'=>$href));
1166
-
1167
  # save transient
1168
  $verify = fvm_set_transient(array('uid'=>$tkey, 'date'=>$tvers, 'type'=>'css', 'content'=>$css, 'meta'=>$meta));
1169
 
@@ -1176,10 +1189,7 @@ function fvm_get_css_from_file($tag) {
1176
  # success, from transient
1177
  return array('code'=>$css, 'tkey'=>$tkey, 'url'=> $href);
1178
  }
1179
-
1180
- # fallback
1181
- return false;
1182
-
1183
  }
1184
 
1185
 
@@ -1205,6 +1215,11 @@ function fvm_get_js_from_file($tag) {
1205
  $ddl = array();
1206
  $ddl = fvm_maybe_download($href);
1207
 
 
 
 
 
 
1208
  # success
1209
  if(isset($ddl['content'])) {
1210
 
@@ -1722,26 +1737,29 @@ function fvm_maybe_minify_css_file($css, $url, $min) {
1722
 
1723
  # adjust paths
1724
  $bgimgs = array();
1725
- preg_match_all ('/url\s*\((\s*[\'"]?(http:|https:|\/\/).+[\'"]?\s*)\)/Uui', $css, $bgimgs);
1726
  if(isset($bgimgs[1]) && is_array($bgimgs[1])) {
1727
  foreach($bgimgs[1] as $img) {
1728
-
1729
- # normalize
1730
- $newimg = fvm_normalize_url($img);
1731
- if($newimg != $img) { $css = str_replace($img, $newimg, $css); $img = $newimg; }
1732
-
1733
- # process
1734
- if(substr($img, 0, strlen($use_url)) == $use_url) {
1735
- $pos = strpos($img, $use_url);
1736
- if ($pos !== false) {
1737
-
1738
- # relative path image
1739
- $relimg = '/' . ltrim(substr_replace($img, '', $pos, strlen($use_url)), '/');
1740
-
1741
- # replace url
1742
- $css = str_replace($img, $relimg, $css);
1743
-
 
 
1744
  }
 
1745
  }
1746
  }
1747
  }
@@ -1754,6 +1772,9 @@ function fvm_maybe_minify_css_file($css, $url, $min) {
1754
  $css = str_replace('http://'.$fvm_urls['wp_domain'], '', $css);
1755
  $css = str_replace('//'.$fvm_urls['wp_domain'], '', $css);
1756
 
 
 
 
1757
  }
1758
 
1759
  # simplify font face
@@ -1927,9 +1948,9 @@ function fvm_maybe_download($url) {
1927
 
1928
  # file path + windows compatibility
1929
  $f = strtok(str_replace('/', DIRECTORY_SEPARATOR, str_replace(rtrim($fvm_urls['wp_site_url'], '/'), rtrim(ABSPATH, '/'), $url)), '?');
1930
-
1931
  # did it work?
1932
- if (file_exists($f)) {
1933
  return array('content'=>file_get_contents($f), 'src'=>'Disk');
1934
  }
1935
  }
@@ -1943,7 +1964,7 @@ function fvm_maybe_download($url) {
1943
  $response = wp_remote_get($url, array('user-agent'=>$uagent, 'timeout' => 7, 'httpversion' => '1.1', 'sslverify'=>false));
1944
  if ( is_wp_error( $response ) ) {
1945
  $error_message = $response->get_error_message();
1946
- return array('error'=>"Something went wrong: $error_message / $url");
1947
  } else {
1948
  return array('content'=>wp_remote_retrieve_body($response), 'src'=>'Web');
1949
  }
856
  # download file, get contents, merge
857
  $ddl = array();
858
  $ddl = fvm_maybe_download($href);
859
+
860
+ # error
861
+ if(isset($ddl['error'])) {
862
+ return trim($css);
863
+ }
864
 
865
  # if success
866
  if(isset($ddl['content'])) {
925
  $css_code_ff = str_replace('https://'.$fvm_urls['wp_domain'], '', $css_code_ff);
926
  $css_code_ff = str_replace('http://'.$fvm_urls['wp_domain'], '', $css_code_ff);
927
  $css_code_ff = str_replace('//'.$fvm_urls['wp_domain'], '', $css_code_ff);
928
+
929
+ # fixes
930
+ $css_code_ff = str_replace('/./', '/', $css_code_ff);
931
 
932
  # return
933
  $result = array('code'=>$css_code, 'fonts'=>$css_code_ff);
1069
 
1070
  # get css font-face rules, original + simplified
1071
  function fvm_simplify_fontface($css_code) {
1072
+
1073
  $mff = array();
1074
  $before = array();
1075
  $after = array();
1086
 
1087
  # woff
1088
  $fonts = array();
1089
+ preg_match('/url\s*\(\s*[\'\"]*([^\'\"]*)[\'\"]*\)\s*format\s*\([\'\"]*woff[\'\"]*\s*\)/Uui', $csr, $fonts);
1090
  if(isset($fonts[0])) { $cssrules[$k] = 'src:'.$fonts[0]; break; }
1091
 
1092
  # woff2
1093
  $fonts = array();
1094
+ preg_match('/url\s*\(\s*[\'\"]*([^\'\"]*)[\'\"]*\)\s*format\s*\([\'\"]*woff2[\'\"]*\s*\)/Uui', $csr, $fonts);
1095
  if(isset($fonts[0])) { $cssrules[$k] = 'src:'.$fonts[0]; break; }
1096
 
1097
  # svg
1098
  $fonts = array();
1099
+ preg_match('/url\s*\(\s*[\'\"]*([^\'\"]*)[\'\"]*\)\s*format\s*\([\'\"]*svg[\'\"]*\s*\)/Uui', $csr, $fonts);
1100
  if(isset($fonts[0])) { $cssrules[$k] = 'src:'.$fonts[0]; break; }
1101
 
1102
  # truetype
1103
  $fonts = array();
1104
+ preg_match('/url\s*\(\s*[\'\"]*([^\'\"]*)[\'\"]*\)\s*format\s*\([\'\"]*truetype[\'\"]*\s*\)/Uui', $csr, $fonts);
1105
  if(isset($fonts[0])) { $cssrules[$k] = 'src:'.$fonts[0]; break; }
1106
 
1107
  # delete other src:url rules
1153
  $ddl = array();
1154
  $ddl = fvm_maybe_download($href);
1155
 
1156
+ # error
1157
+ if(isset($ddl['error'])) {
1158
+ return array('error'=>$ddl['error'], 'tkey'=>$tkey, 'url'=> $href);
1159
+ }
1160
+
1161
  # success
1162
  if(isset($ddl['content'])) {
1163
 
1176
  # handle import rules
1177
  $css = fvm_replace_css_imports($css, $href);
1178
  $meta = json_encode(array('href'=>$href));
1179
+
1180
  # save transient
1181
  $verify = fvm_set_transient(array('uid'=>$tkey, 'date'=>$tvers, 'type'=>'css', 'content'=>$css, 'meta'=>$meta));
1182
 
1189
  # success, from transient
1190
  return array('code'=>$css, 'tkey'=>$tkey, 'url'=> $href);
1191
  }
1192
+
 
 
 
1193
  }
1194
 
1195
 
1215
  $ddl = array();
1216
  $ddl = fvm_maybe_download($href);
1217
 
1218
+ # error
1219
+ if(isset($ddl['error'])) {
1220
+ return array('error'=>$ddl['error'], 'tkey'=>$tkey, 'url'=> $href);
1221
+ }
1222
+
1223
  # success
1224
  if(isset($ddl['content'])) {
1225
 
1737
 
1738
  # adjust paths
1739
  $bgimgs = array();
1740
+ preg_match_all ('/url\s*\(\s*[\'\"]*([^\'\"]*)[\'\"]*\)/Uui', $css, $bgimgs);
1741
  if(isset($bgimgs[1]) && is_array($bgimgs[1])) {
1742
  foreach($bgimgs[1] as $img) {
1743
+ if(stripos($img, 'http') !== false || stripos($img, '//') !== false) {
1744
+
1745
+ # normalize
1746
+ $newimg = fvm_normalize_url($img);
1747
+ if($newimg != $img) { $css = str_replace($img, $newimg, $css); $img = $newimg; }
1748
+
1749
+ # process
1750
+ if(substr($img, 0, strlen($use_url)) == $use_url) {
1751
+ $pos = strpos($img, $use_url);
1752
+ if ($pos !== false) {
1753
+
1754
+ # relative path image
1755
+ $relimg = '/' . ltrim(substr_replace($img, '', $pos, strlen($use_url)), '/');
1756
+
1757
+ # replace url
1758
+ $css = str_replace($img, $relimg, $css);
1759
+
1760
+ }
1761
  }
1762
+
1763
  }
1764
  }
1765
  }
1772
  $css = str_replace('http://'.$fvm_urls['wp_domain'], '', $css);
1773
  $css = str_replace('//'.$fvm_urls['wp_domain'], '', $css);
1774
 
1775
+ # fixes
1776
+ $css = str_replace('/./', '/', $css);
1777
+
1778
  }
1779
 
1780
  # simplify font face
1948
 
1949
  # file path + windows compatibility
1950
  $f = strtok(str_replace('/', DIRECTORY_SEPARATOR, str_replace(rtrim($fvm_urls['wp_site_url'], '/'), rtrim(ABSPATH, '/'), $url)), '?');
1951
+
1952
  # did it work?
1953
+ if (file_exists($f) && is_file($f)) {
1954
  return array('content'=>file_get_contents($f), 'src'=>'Disk');
1955
  }
1956
  }
1964
  $response = wp_remote_get($url, array('user-agent'=>$uagent, 'timeout' => 7, 'httpversion' => '1.1', 'sslverify'=>false));
1965
  if ( is_wp_error( $response ) ) {
1966
  $error_message = $response->get_error_message();
1967
+ return array('error'=>"Something went wrong: $error_message");
1968
  } else {
1969
  return array('content'=>wp_remote_retrieve_body($response), 'src'=>'Web');
1970
  }
inc/frontend.php CHANGED
@@ -70,11 +70,18 @@ function fvm_process_page($html) {
70
 
71
  # collect all link preload headers, skip amp
72
  if(fvm_is_amp_page() !== true) {
 
 
 
 
 
 
73
  # add other preloads
74
  foreach($html->find('link[rel=preload]') as $tag) {
75
  $htmlpreloads[] = $tag->outertext;
76
  $tag->outertext = '';
77
  }
 
78
  }
79
 
80
 
@@ -132,8 +139,8 @@ function fvm_process_page($html) {
132
 
133
 
134
  # START CSS FILES
135
- if($tag->tag == 'link' && isset($tag->href)) {
136
-
137
  # Ignore css files
138
  $ignore_css_merging = false;
139
  if(isset($fvm_settings['css']['ignore']) && !empty($fvm_settings['css']['ignore'])) {
@@ -170,6 +177,13 @@ function fvm_process_page($html) {
170
 
171
  if($css !== false && is_array($css)) {
172
 
 
 
 
 
 
 
 
173
  # extract fonts and icons
174
  if(isset($fvm_settings['css']['fonts']) && $fvm_settings['css']['fonts'] == true) {
175
  $extract_fonts_arr = fvm_extract_fonts($css['code']);
@@ -234,6 +248,13 @@ function fvm_process_page($html) {
234
 
235
  if($css !== false && is_array($css)) {
236
 
 
 
 
 
 
 
 
237
  # extract fonts and icons
238
  if(isset($fvm_settings['css']['fonts']) && $fvm_settings['css']['fonts'] == true) {
239
  $extract_fonts_arr = fvm_extract_fonts($css['code']);
@@ -564,6 +585,13 @@ function fvm_process_page($html) {
564
  # download or fetch from transient, minified
565
  $js = fvm_get_js_from_file($tag);
566
  if($js !== false && is_array($js)) {
 
 
 
 
 
 
 
567
 
568
  # save js for merging
569
  $fvm_scripts_header[] = $js['code'];
@@ -594,6 +622,13 @@ function fvm_process_page($html) {
594
  # download or fetch from transient, minified
595
  $js = fvm_get_js_from_file($tag);
596
  if($js !== false && is_array($js)) {
 
 
 
 
 
 
 
597
 
598
  # save js for merging
599
  $fvm_scripts_defer[] = $js['code'];
@@ -657,6 +692,13 @@ function fvm_process_page($html) {
657
  # download or fetch from transient, minified
658
  $js = fvm_get_js_from_file($tag);
659
  if($js !== false && is_array($js)) {
 
 
 
 
 
 
 
660
 
661
  # generate url
662
  $ind_js_url = fvm_generate_min_url($tag->src, $js['tkey'], 'js', $js['code']);
@@ -702,6 +744,13 @@ function fvm_process_page($html) {
702
  # download or fetch from transient, minified
703
  $js = fvm_get_js_from_file($tag);
704
  if($js !== false && is_array($js)) {
 
 
 
 
 
 
 
705
 
706
  # generate url
707
  $ind_js_url = fvm_generate_min_url($tag->src, $js['tkey'], 'js', $js['code']);
70
 
71
  # collect all link preload headers, skip amp
72
  if(fvm_is_amp_page() !== true) {
73
+
74
+ # skip on web stories
75
+ if(count($html->find('script[src*=cdn.ampproject.org]')) > 0) {
76
+ return $html . '<!-- FVM does not support AMP -->';
77
+ }
78
+
79
  # add other preloads
80
  foreach($html->find('link[rel=preload]') as $tag) {
81
  $htmlpreloads[] = $tag->outertext;
82
  $tag->outertext = '';
83
  }
84
+
85
  }
86
 
87
 
139
 
140
 
141
  # START CSS FILES
142
+ if($tag->tag == 'link' && isset($tag->href)) {
143
+
144
  # Ignore css files
145
  $ignore_css_merging = false;
146
  if(isset($fvm_settings['css']['ignore']) && !empty($fvm_settings['css']['ignore'])) {
177
 
178
  if($css !== false && is_array($css)) {
179
 
180
+ # error
181
+ if(isset($css['error'])) {
182
+ $tag->outertext = '/* Error on '.$href.' : '.$css['error'].' */'. PHP_EOL . $tag->outertext;
183
+ unset($allcss[$k]);
184
+ continue;
185
+ }
186
+
187
  # extract fonts and icons
188
  if(isset($fvm_settings['css']['fonts']) && $fvm_settings['css']['fonts'] == true) {
189
  $extract_fonts_arr = fvm_extract_fonts($css['code']);
248
 
249
  if($css !== false && is_array($css)) {
250
 
251
+ # error
252
+ if(isset($css['error'])) {
253
+ $tag->outertext = '/* Error on '.$href.' : '.$css['error'].' */'. PHP_EOL . $tag->outertext;
254
+ unset($allcss[$k]);
255
+ continue;
256
+ }
257
+
258
  # extract fonts and icons
259
  if(isset($fvm_settings['css']['fonts']) && $fvm_settings['css']['fonts'] == true) {
260
  $extract_fonts_arr = fvm_extract_fonts($css['code']);
585
  # download or fetch from transient, minified
586
  $js = fvm_get_js_from_file($tag);
587
  if($js !== false && is_array($js)) {
588
+
589
+ # error
590
+ if(isset($js['error'])) {
591
+ $tag->outertext = '/* Error on '.$href.' : '.$js['error'].' */'. PHP_EOL . $tag->outertext;
592
+ unset($allscripts[$k]);
593
+ continue 2;
594
+ }
595
 
596
  # save js for merging
597
  $fvm_scripts_header[] = $js['code'];
622
  # download or fetch from transient, minified
623
  $js = fvm_get_js_from_file($tag);
624
  if($js !== false && is_array($js)) {
625
+
626
+ # error
627
+ if(isset($js['error'])) {
628
+ $tag->outertext = '/* Error on '.$href.' : '.$js['error'].' */'. PHP_EOL . $tag->outertext;
629
+ unset($allscripts[$k]);
630
+ continue 2;
631
+ }
632
 
633
  # save js for merging
634
  $fvm_scripts_defer[] = $js['code'];
692
  # download or fetch from transient, minified
693
  $js = fvm_get_js_from_file($tag);
694
  if($js !== false && is_array($js)) {
695
+
696
+ # error
697
+ if(isset($js['error'])) {
698
+ $tag->outertext = '/* Error on '.$href.' : '.$js['error'].' */'. PHP_EOL . $tag->outertext;
699
+ unset($allscripts[$k]);
700
+ continue 2;
701
+ }
702
 
703
  # generate url
704
  $ind_js_url = fvm_generate_min_url($tag->src, $js['tkey'], 'js', $js['code']);
744
  # download or fetch from transient, minified
745
  $js = fvm_get_js_from_file($tag);
746
  if($js !== false && is_array($js)) {
747
+
748
+ # error
749
+ if(isset($js['error'])) {
750
+ $tag->outertext = '/* Error on '.$href.' : '.$js['error'].' */'. PHP_EOL . $tag->outertext;
751
+ unset($allscripts[$k]);
752
+ continue 2;
753
+ }
754
 
755
  # generate url
756
  $ind_js_url = fvm_generate_min_url($tag->src, $js['tkey'], 'js', $js['code']);
readme.txt CHANGED
@@ -3,7 +3,7 @@ Contributors: Alignak
3
  Tags: PHP Minify, Lighthouse, GTmetrix, Pingdom, Pagespeed, Merging, Minification, Optimization, Speed, Performance, FVM
4
  Requires at least: 4.9
5
  Requires PHP: 5.6
6
- Stable tag: 3.1.7
7
  Tested up to: 5.7.1
8
  Text Domain: fast-velocity-minify
9
  License: GPLv3 or later
@@ -46,15 +46,13 @@ You can create a style tag, with an ID equal to "critical-path" ex: `<style id="
46
  1. The Settings page.
47
 
48
 
49
- == Upgrade Notice ==
50
-
51
- = 3.0.0 =
52
- Please backup your site before updating.
53
- Version 3.0 is a major code rewrite to improve JS and CSS merging, but it requires JS settings to be readjusted after the update.
54
-
55
-
56
  == Changelog ==
57
 
 
 
 
 
 
58
  = 3.1.7 [2021.04.26] =
59
  * more php notices fixes
60
 
3
  Tags: PHP Minify, Lighthouse, GTmetrix, Pingdom, Pagespeed, Merging, Minification, Optimization, Speed, Performance, FVM
4
  Requires at least: 4.9
5
  Requires PHP: 5.6
6
+ Stable tag: 3.1.8
7
  Tested up to: 5.7.1
8
  Text Domain: fast-velocity-minify
9
  License: GPLv3 or later
46
  1. The Settings page.
47
 
48
 
 
 
 
 
 
 
 
49
  == Changelog ==
50
 
51
+ = 3.1.8 [2021.04.30] =
52
+ * fixed missing dynamic css/js urls
53
+ * fixed some relative static assets paths
54
+ * added auto disabling of FVM on Web Stories (AMP)
55
+
56
  = 3.1.7 [2021.04.26] =
57
  * more php notices fixes
58