Fast Velocity Minify - Version 3.2.1

Version Description

[2021.05.07] = * added support for custom cache location via wp-config.php constants * changed the default cache directory to wp-content/cache

Download this release

Release Info

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

Code changes from version 3.2.0 to 3.2.1

Files changed (5) hide show
  1. fvm.php +1 -1
  2. inc/admin.php +6 -15
  3. inc/common.php +86 -41
  4. inc/frontend.php +7 -0
  5. readme.txt +12 -7
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.2.0
10
  License: GPL2
11
 
12
  ------------------------------------------------------------------------
6
  Author: Raul Peixoto
7
  Author URI: http://fastvelocity.com
8
  Text Domain: fast-velocity-minify
9
+ Version: 3.2.1
10
  License: GPL2
11
 
12
  ------------------------------------------------------------------------
inc/admin.php CHANGED
@@ -25,22 +25,13 @@ function fvm_check_minimum_requirements() {
25
  $error = __( 'FVM requires WP 4.9 or higher. You’re still on', 'fast-velocity-minify' ) .' '. $GLOBALS['wp_version'];
26
  }
27
 
28
- # set cache on the uploads directory
29
- $upload_dir = wp_upload_dir();
30
- if(isset($upload_dir['basedir']) && isset($upload_dir['baseurl']) && !empty($upload_dir['basedir'])) {
31
-
32
- # define and create directory
33
- $cache_dir = $upload_dir['basedir'] . DIRECTORY_SEPARATOR . 'fvm-cache'. DIRECTORY_SEPARATOR . 'min';
34
- $cache_dir_url = $upload_dir['baseurl'] . '/fvm-cache/min';
35
- if(!is_dir($cache_dir) && function_exists('wp_mkdir_p')) { wp_mkdir_p($cache_dir); }
36
-
37
- # check
38
- if(is_dir($cache_dir) && !is_writable($cache_dir)) {
39
- $error = __( 'FVM needs writing permissions on ', 'fast-velocity-minify' ). ' ['.$cache_dir.']';
40
  }
41
-
42
- }
43
-
44
 
45
  # deactivate plugin forcefully
46
  global $fvm_var_basename;
25
  $error = __( 'FVM requires WP 4.9 or higher. You’re still on', 'fast-velocity-minify' ) .' '. $GLOBALS['wp_version'];
26
  }
27
 
28
+ # check cache directory
29
+ $ch_info = fvm_get_cache_location();
30
+ if(isset($ch_info['ch_url']) && !empty($ch_info['ch_url']) && isset($ch_info['ch_dir']) && !empty($ch_info['ch_dir'])) {
31
+ if(is_dir($ch_info['ch_dir']) && !is_writable($ch_info['ch_dir'])) {
32
+ $error = __( 'FVM needs writing permissions on ', 'fast-velocity-minify' ). ' ['.$ch_info['ch_dir'].']';
 
 
 
 
 
 
 
33
  }
34
+ }
 
 
35
 
36
  # deactivate plugin forcefully
37
  global $fvm_var_basename;
inc/common.php CHANGED
@@ -55,6 +55,60 @@ function fvm_admintoolbar() {
55
  }
56
 
57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
  # purge all caches when clicking the button on the admin bar
59
  function fvm_process_cache_purge_request(){
60
 
@@ -390,28 +444,21 @@ function fvm_can_minify_css() {
390
 
391
  # save minified code, if not yet available
392
  function fvm_generate_min_url($url, $tkey, $type, $code) {
393
-
394
- # files first, but only for js/css types
395
- if(function_exists('wp_upload_dir')) {
396
 
397
- # cache date
398
- $tvers = get_option('fvm_last_cache_update', '0');
399
 
400
- # parse uripath and check if it matches against our rewrite format
401
- $filename = $tvers.'-'.$tkey .'.'. $type;
402
-
403
- # set cache on the uploads directory
404
- $upload_dir = wp_upload_dir();
405
- if(isset($upload_dir['basedir']) && isset($upload_dir['baseurl']) && !empty($upload_dir['basedir'])) {
 
406
 
407
- # define and create directory
408
- $cache_dir = $upload_dir['basedir'] . DIRECTORY_SEPARATOR . 'fvm-cache'. DIRECTORY_SEPARATOR . 'min';
409
- $cache_dir_url = $upload_dir['baseurl'] . '/fvm-cache/min';
410
- if(!is_dir($cache_dir) && function_exists('wp_mkdir_p')) { wp_mkdir_p($cache_dir); }
411
-
412
  # filename
413
- $file = $cache_dir . DIRECTORY_SEPARATOR . $filename;
414
- $public = $cache_dir_url . '/' .$filename;
415
 
416
  # cache date
417
  $tvers = get_option('fvm_last_cache_update', '0');
@@ -419,7 +466,7 @@ function fvm_generate_min_url($url, $tkey, $type, $code) {
419
  # wordpress functions
420
  require_once (ABSPATH . DIRECTORY_SEPARATOR . 'wp-admin'. DIRECTORY_SEPARATOR .'includes'. DIRECTORY_SEPARATOR .'class-wp-filesystem-base.php');
421
  require_once (ABSPATH . DIRECTORY_SEPARATOR .'wp-admin'. DIRECTORY_SEPARATOR .'includes'. DIRECTORY_SEPARATOR .'class-wp-filesystem-direct.php');
422
-
423
  # create if doesn't exist
424
  $fileSystemDirect = new WP_Filesystem_Direct(false);
425
  if(!$fileSystemDirect->exists($file) || ($fileSystemDirect->exists($file) && $fileSystemDirect->mtime($file) < $tvers)) {
@@ -428,8 +475,8 @@ function fvm_generate_min_url($url, $tkey, $type, $code) {
428
 
429
  # return url
430
  return $public;
431
-
432
- }
433
  }
434
 
435
  # default
@@ -498,44 +545,42 @@ function fvm_purge_static_files() {
498
  # increment
499
  update_option('fvm_last_cache_update', time());
500
 
501
- # process
502
- if( function_exists('wp_upload_dir') ) {
503
-
504
- # current timestamp
505
- $tvers = get_option('fvm_last_cache_update', '0');
506
-
507
- $upload_dir = wp_upload_dir();
508
- if(isset($upload_dir['basedir']) && isset($upload_dir['baseurl']) && !empty($upload_dir['basedir'])) {
509
  require_once (ABSPATH . DIRECTORY_SEPARATOR . 'wp-admin'. DIRECTORY_SEPARATOR .'includes'. DIRECTORY_SEPARATOR .'class-wp-filesystem-base.php');
510
  require_once (ABSPATH . DIRECTORY_SEPARATOR .'wp-admin'. DIRECTORY_SEPARATOR .'includes'. DIRECTORY_SEPARATOR .'class-wp-filesystem-direct.php');
511
-
 
 
 
512
  # instant purge
513
  global $fvm_settings;
514
  if(isset($fvm_settings['cache']['min_instant_purge']) && $fvm_settings['cache']['min_instant_purge'] == true) {
515
- $cache_dir = $upload_dir['basedir'] . DIRECTORY_SEPARATOR . 'fvm-cache';
516
- $fileSystemDirect = new WP_Filesystem_Direct(false);
517
- $fileSystemDirect->rmdir($cache_dir, true);
518
  return true;
519
- } else {
520
- $cache_dir = $upload_dir['basedir'] . DIRECTORY_SEPARATOR . 'fvm-cache'. DIRECTORY_SEPARATOR . 'min';
521
- $fileSystemDirect = new WP_Filesystem_Direct(false);
522
 
523
  # older than 24h and not matching current timestamp
524
- $list = $fileSystemDirect->dirlist($cache_dir, false, true);
525
  if(is_array($list) && count($list) > 0) {
526
  foreach($list as $k=>$arr) {
527
  if(isset($arr['lastmodunix']) && $arr['type'] == 'f' && intval($arr['lastmodunix']) <= time()-86400) {
528
- if(substr($arr['name'], 0, 10) !== $tvers) {
529
- $fileSystemDirect->delete($cache_dir . DIRECTORY_SEPARATOR . $arr['name'], false, 'f');
530
  }
531
  }
532
  }
533
  }
534
 
535
  }
536
-
537
- }
538
  }
 
539
  }
540
 
541
 
55
  }
56
 
57
 
58
+ # get cache directory
59
+ function fvm_get_cache_location() {
60
+
61
+ # custom path
62
+ if (defined('FVM_DIR') && defined('FVM_URL')){
63
+
64
+ # define paths and url
65
+ $sep = DIRECTORY_SEPARATOR;
66
+ $dir = trim(rtrim(FVM_DIR, '/\\')). $sep . 'cache' . $sep . 'fvm'. $sep . 'min';
67
+ $durl = trim(rtrim(FVM_URL, '/')). '/cache/fvm/min';
68
+
69
+ # create and return
70
+ if(!is_dir($dir) && function_exists('wp_mkdir_p')) { wp_mkdir_p($dir); }
71
+ return array('ch_dir'=>$dir,'ch_url'=>$durl);
72
+
73
+ }
74
+
75
+
76
+ # /wp-content/cache
77
+ if (defined('WP_CONTENT_DIR') && defined('WP_CONTENT_URL')){
78
+
79
+ # define paths and url
80
+ $sep = DIRECTORY_SEPARATOR;
81
+ $dir = trim(rtrim(WP_CONTENT_DIR, '/\\')). $sep . 'cache' . $sep . 'fvm'. $sep . 'min';
82
+ $durl = trim(rtrim(WP_CONTENT_URL, '/')). '/cache/fvm/min';
83
+
84
+ # create and return
85
+ if(!is_dir($dir) && function_exists('wp_mkdir_p')) { wp_mkdir_p($dir); }
86
+ return array('ch_dir'=>$dir,'ch_url'=>$durl);
87
+
88
+ }
89
+
90
+ # uploads directory
91
+ $ch_info = wp_upload_dir();
92
+ if(isset($ch_info['basedir']) && isset($ch_info['baseurl']) && !empty($ch_info['basedir'])) {
93
+
94
+ # define and create directory
95
+ $sep = DIRECTORY_SEPARATOR;
96
+ $dir = $ch_info['basedir'] . $sep . 'cache' . $sep . 'fvm'. $sep . 'min';
97
+ $durl = $ch_info['baseurl'] . '/cache/fvm/min';
98
+
99
+ # create and return
100
+ if(!is_dir($dir) && function_exists('wp_mkdir_p')) { wp_mkdir_p($dir); }
101
+ return array('ch_dir'=>$dir,'ch_url'=>$durl);
102
+
103
+ }
104
+
105
+ # error
106
+ return false;
107
+
108
+ }
109
+
110
+
111
+
112
  # purge all caches when clicking the button on the admin bar
113
  function fvm_process_cache_purge_request(){
114
 
444
 
445
  # save minified code, if not yet available
446
  function fvm_generate_min_url($url, $tkey, $type, $code) {
 
 
 
447
 
448
+ # cache date
449
+ $tvers = get_option('fvm_last_cache_update', '0');
450
 
451
+ # parse uripath and check if it matches against our rewrite format
452
+ $filename = $tvers.'-'.$tkey .'.'. $type;
453
+
454
+ # check cache directory
455
+ $ch_info = fvm_get_cache_location();
456
+ if(isset($ch_info['ch_url']) && !empty($ch_info['ch_url']) && isset($ch_info['ch_dir']) && !empty($ch_info['ch_dir'])) {
457
+ if(is_dir($ch_info['ch_dir']) && is_writable($ch_info['ch_dir'])) {
458
 
 
 
 
 
 
459
  # filename
460
+ $file = $ch_info['ch_dir'] . DIRECTORY_SEPARATOR . $filename;
461
+ $public = $ch_info['ch_url'] . '/' .$filename;
462
 
463
  # cache date
464
  $tvers = get_option('fvm_last_cache_update', '0');
466
  # wordpress functions
467
  require_once (ABSPATH . DIRECTORY_SEPARATOR . 'wp-admin'. DIRECTORY_SEPARATOR .'includes'. DIRECTORY_SEPARATOR .'class-wp-filesystem-base.php');
468
  require_once (ABSPATH . DIRECTORY_SEPARATOR .'wp-admin'. DIRECTORY_SEPARATOR .'includes'. DIRECTORY_SEPARATOR .'class-wp-filesystem-direct.php');
469
+
470
  # create if doesn't exist
471
  $fileSystemDirect = new WP_Filesystem_Direct(false);
472
  if(!$fileSystemDirect->exists($file) || ($fileSystemDirect->exists($file) && $fileSystemDirect->mtime($file) < $tvers)) {
475
 
476
  # return url
477
  return $public;
478
+
479
+ }
480
  }
481
 
482
  # default
545
  # increment
546
  update_option('fvm_last_cache_update', time());
547
 
548
+ # check cache directory
549
+ $ch_info = fvm_get_cache_location();
550
+ if(isset($ch_info['ch_url']) && !empty($ch_info['ch_url']) && isset($ch_info['ch_dir']) && !empty($ch_info['ch_dir'])) {
551
+ if(is_dir($ch_info['ch_dir']) && is_writable($ch_info['ch_dir'])) {
552
+
553
+ # wordpress functions
 
 
554
  require_once (ABSPATH . DIRECTORY_SEPARATOR . 'wp-admin'. DIRECTORY_SEPARATOR .'includes'. DIRECTORY_SEPARATOR .'class-wp-filesystem-base.php');
555
  require_once (ABSPATH . DIRECTORY_SEPARATOR .'wp-admin'. DIRECTORY_SEPARATOR .'includes'. DIRECTORY_SEPARATOR .'class-wp-filesystem-direct.php');
556
+
557
+ # start
558
+ $fileSystemDirect = new WP_Filesystem_Direct(false);
559
+
560
  # instant purge
561
  global $fvm_settings;
562
  if(isset($fvm_settings['cache']['min_instant_purge']) && $fvm_settings['cache']['min_instant_purge'] == true) {
563
+ $fileSystemDirect->rmdir($ch_info['ch_dir'], true);
 
 
564
  return true;
565
+ } else {
 
 
566
 
567
  # older than 24h and not matching current timestamp
568
+ $list = $fileSystemDirect->dirlist($ch_info['ch_dir'], false, true);
569
  if(is_array($list) && count($list) > 0) {
570
  foreach($list as $k=>$arr) {
571
  if(isset($arr['lastmodunix']) && $arr['type'] == 'f' && intval($arr['lastmodunix']) <= time()-86400) {
572
+ if(substr($arr['name'], 0, 10) !== time()) {
573
+ $fileSystemDirect->delete($ch_info['ch_dir'] . DIRECTORY_SEPARATOR . $arr['name'], false, 'f');
574
  }
575
  }
576
  }
577
  }
578
 
579
  }
580
+
581
+ }
582
  }
583
+
584
  }
585
 
586
 
inc/frontend.php CHANGED
@@ -678,6 +678,13 @@ function fvm_process_page($html) {
678
  # minify individually, if enabled
679
  if(!isset($fvm_settings['js']['min_disable']) || (isset($fvm_settings['js']['min_disable'])&& $fvm_settings['js']['min_disable'] != true)) {
680
 
 
 
 
 
 
 
 
681
  # force render blocking
682
  if(isset($fvm_settings['js']['merge_header']) && !empty($fvm_settings['js']['merge_header'])) {
683
  $arr = fvm_string_toarray($fvm_settings['js']['merge_header']);
678
  # minify individually, if enabled
679
  if(!isset($fvm_settings['js']['min_disable']) || (isset($fvm_settings['js']['min_disable'])&& $fvm_settings['js']['min_disable'] != true)) {
680
 
681
+ # skip third party scripts, unless allowed
682
+ $allowed = array($fvm_urls['wp_domain'], '/ajax.aspnetcdn.com/ajax/', '/ajax.googleapis.com/ajax/libs/', '/cdnjs.cloudflare.com/ajax/libs/');
683
+ if(str_replace($allowed, '', $href) == $href) {
684
+ unset($allscripts[$k]);
685
+ continue;
686
+ }
687
+
688
  # force render blocking
689
  if(isset($fvm_settings['js']['merge_header']) && !empty($fvm_settings['js']['merge_header'])) {
690
  $arr = fvm_string_toarray($fvm_settings['js']['merge_header']);
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.2.0
7
  Tested up to: 5.7.1
8
  Text Domain: fast-velocity-minify
9
  License: GPLv3 or later
@@ -13,16 +13,17 @@ Improve your speed score on GTmetrix, Pingdom Tools and Google PageSpeed Insight
13
 
14
 
15
  == Description ==
16
- Speed optimization plugin for developers and advanced users.
17
 
18
- Minification is done on the frontend during the first uncached request. Once the first request is processed, any other pages that require the same set of CSS and JS files, will be able to reuse the same generated file.
 
 
19
 
20
- The plugin includes options for developers and advanced users, however the default settings should work just fine for most sites.
21
  Kindly read the HELP section after installing the plugin, about possible issues and how to solve them.
22
 
23
  = Additional Optimization =
24
 
25
- I can offer you aditional `custom made` optimization on top of this plugin. If you would like to hire me, please visit my profile links for further information.
26
 
27
 
28
  = WP-CLI Commands =
@@ -30,8 +31,8 @@ I can offer you aditional `custom made` optimization on top of this plugin. If y
30
  * Purge all caches on a network site: `wp --url=blog.example.com fvm purge`
31
  * Purge all caches on the entire network (linux): `wp site list --field=url | xargs -n1 -I % wp --url=% fvm purge`
32
 
33
- = How to add your own critical path ? =
34
- You can create a style tag, with an ID equal to "critical-path" ex: `<style id="critical-path"> your code </style>` anywhere on the header and FVM will move it to before the CSS merged files.
35
 
36
 
37
  == Installation ==
@@ -48,6 +49,10 @@ You can create a style tag, with an ID equal to "critical-path" ex: `<style id="
48
 
49
  == Changelog ==
50
 
 
 
 
 
51
  = 3.2.0 [2021.05.06] =
52
  * fixed an issue where some files were not being minified
53
  * better sourceMappingURL removal during minification
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.2.1
7
  Tested up to: 5.7.1
8
  Text Domain: fast-velocity-minify
9
  License: GPLv3 or later
13
 
14
 
15
  == Description ==
16
+ HTML, CSS & JS optimization plugin for developers and advanced users. Note you need to look into the HELP tab after installing the plugin and manually configure it for your site. Each site is different, so the default recommendations may or may not work for you and you will need to test and find out how to adjust your settings.
17
 
18
+ Minification is done on the frontend during the first uncached request. Once the first request is processed, any other pages that require the same set of CSS and JS files will be able to reuse the same generated static CSS or JS file.
19
+
20
+ If your cache is growing significantly, this could mean one of your CSS or JS files is dynamic and changes on every pageview. In that case, you would need to add the file to the ignore list, else the cache would grow indefinitely (because obviously the original files themselves are dynamic and when you minify, the plugin sees a different file).
21
 
 
22
  Kindly read the HELP section after installing the plugin, about possible issues and how to solve them.
23
 
24
  = Additional Optimization =
25
 
26
+ I can offer you additional `custom made` optimization on top of this plugin. If you would like to hire me, please visit my profile links for further information.
27
 
28
 
29
  = WP-CLI Commands =
31
  * Purge all caches on a network site: `wp --url=blog.example.com fvm purge`
32
  * Purge all caches on the entire network (linux): `wp site list --field=url | xargs -n1 -I % wp --url=% fvm purge`
33
 
34
+ = How to customize the cache path ? =
35
+ You need a public directory to store and serve minified cache files. If you need to customize the path and url, you need to edit your `wp-config.php` and add both `define('FVM_DIR', '/path/to/example.com/your/public/directory');` and `define('FVM_URL', 'https://example.com/your/public/directory');` .
36
 
37
 
38
  == Installation ==
49
 
50
  == Changelog ==
51
 
52
+ = 3.2.1 [2021.05.07] =
53
+ * added support for custom cache location via wp-config.php constants
54
+ * changed the default cache directory to wp-content/cache
55
+
56
  = 3.2.0 [2021.05.06] =
57
  * fixed an issue where some files were not being minified
58
  * better sourceMappingURL removal during minification