Skip to content

Commit

Permalink
v3.2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
peixotorms committed May 9, 2021
1 parent 4c09d0b commit c267c0b
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 75 deletions.
2 changes: 1 addition & 1 deletion fvm.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
Author: Raul Peixoto
Author URI: http://fastvelocity.com
Text Domain: fast-velocity-minify
Version: 3.2.0
Version: 3.2.2
License: GPL2
------------------------------------------------------------------------
Expand Down
21 changes: 6 additions & 15 deletions inc/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,13 @@ function fvm_check_minimum_requirements() {
$error = __( 'FVM requires WP 4.9 or higher. You’re still on', 'fast-velocity-minify' ) .' '. $GLOBALS['wp_version'];
}

# set cache on the uploads directory
$upload_dir = wp_upload_dir();
if(isset($upload_dir['basedir']) && isset($upload_dir['baseurl']) && !empty($upload_dir['basedir'])) {

# define and create directory
$cache_dir = $upload_dir['basedir'] . DIRECTORY_SEPARATOR . 'fvm-cache'. DIRECTORY_SEPARATOR . 'min';
$cache_dir_url = $upload_dir['baseurl'] . '/fvm-cache/min';
if(!is_dir($cache_dir) && function_exists('wp_mkdir_p')) { wp_mkdir_p($cache_dir); }

# check
if(is_dir($cache_dir) && !is_writable($cache_dir)) {
$error = __( 'FVM needs writing permissions on ', 'fast-velocity-minify' ). ' ['.$cache_dir.']';
# check cache directory
$ch_info = fvm_get_cache_location();
if(isset($ch_info['ch_url']) && !empty($ch_info['ch_url']) && isset($ch_info['ch_dir']) && !empty($ch_info['ch_dir'])) {
if(is_dir($ch_info['ch_dir']) && !is_writable($ch_info['ch_dir'])) {
$error = __( 'FVM needs writing permissions on ', 'fast-velocity-minify' ). ' ['.$ch_info['ch_dir'].']';
}

}

}

# deactivate plugin forcefully
global $fvm_var_basename;
Expand Down
140 changes: 92 additions & 48 deletions inc/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,60 @@ function fvm_admintoolbar() {
}


# get cache directory
function fvm_get_cache_location() {

# custom path
if (defined('FVM_DIR') && defined('FVM_URL')){

# define paths and url
$sep = DIRECTORY_SEPARATOR;
$dir = trim(rtrim(FVM_DIR, '/\\')). $sep . 'cache' . $sep . 'fvm'. $sep . 'min';
$durl = trim(rtrim(FVM_URL, '/')). '/cache/fvm/min';

# create and return
if(!is_dir($dir) && function_exists('wp_mkdir_p')) { wp_mkdir_p($dir); }
return array('ch_dir'=>$dir,'ch_url'=>$durl);

}


# /wp-content/cache
if (defined('WP_CONTENT_DIR') && defined('WP_CONTENT_URL')){

# define paths and url
$sep = DIRECTORY_SEPARATOR;
$dir = trim(rtrim(WP_CONTENT_DIR, '/\\')). $sep . 'cache' . $sep . 'fvm'. $sep . 'min';
$durl = trim(rtrim(WP_CONTENT_URL, '/')). '/cache/fvm/min';

# create and return
if(!is_dir($dir) && function_exists('wp_mkdir_p')) { wp_mkdir_p($dir); }
return array('ch_dir'=>$dir,'ch_url'=>$durl);

}

# uploads directory
$ch_info = wp_upload_dir();
if(isset($ch_info['basedir']) && isset($ch_info['baseurl']) && !empty($ch_info['basedir'])) {

# define and create directory
$sep = DIRECTORY_SEPARATOR;
$dir = $ch_info['basedir'] . $sep . 'cache' . $sep . 'fvm'. $sep . 'min';
$durl = $ch_info['baseurl'] . '/cache/fvm/min';

# create and return
if(!is_dir($dir) && function_exists('wp_mkdir_p')) { wp_mkdir_p($dir); }
return array('ch_dir'=>$dir,'ch_url'=>$durl);

}

# error
return false;

}



# purge all caches when clicking the button on the admin bar
function fvm_process_cache_purge_request(){

Expand Down Expand Up @@ -205,8 +259,8 @@ function fvm_purge_others(){
# hosting companies

# Purge SG Optimizer (Siteground)
if (function_exists('sg_cachepress_purge_cache')) {
sg_cachepress_purge_cache();
if (function_exists('sg_cachepress_purge_everything')) {
sg_cachepress_purge_everything();
return __( 'All caches on <strong>SG Optimizer</strong> have been purged.', 'fast-velocity-minify' );
}

Expand Down Expand Up @@ -272,9 +326,9 @@ function fvm_purge_others(){


# Purge Godaddy Managed WordPress Hosting (Varnish)
function fvm_godaddy_request( $method, $url = null ) {
$url = empty( $url ) ? home_url() : $url;
$host = parse_url( $url, PHP_URL_HOST );
function fvm_godaddy_request( $method) {
$url = home_url();
$host = wpraiser_get_domain();
$url = set_url_scheme( str_replace( $host, WPaas\Plugin::vip(), $url ), 'http' );
update_option( 'gd_system_last_cache_flush', time(), 'no'); # purge apc
wp_remote_request( esc_url_raw( $url ), array('method' => $method, 'blocking' => false, 'headers' => array('Host' => $host)) );
Expand Down Expand Up @@ -390,45 +444,37 @@ function fvm_can_minify_css() {

# save minified code, if not yet available
function fvm_generate_min_url($url, $tkey, $type, $code) {

# files first, but only for js/css types
if(function_exists('wp_upload_dir')) {

# cache date
$tvers = get_option('fvm_last_cache_update', '0');
# cache date
$tvers = get_option('fvm_last_cache_update', '0');

# parse uripath and check if it matches against our rewrite format
$filename = $tvers.'-'.$tkey .'.'. $type;

# set cache on the uploads directory
$upload_dir = wp_upload_dir();
if(isset($upload_dir['basedir']) && isset($upload_dir['baseurl']) && !empty($upload_dir['basedir'])) {
# parse uripath and check if it matches against our rewrite format
$filename = $tvers.'-'.$tkey .'.'. $type;

# check cache directory
$ch_info = fvm_get_cache_location();
if(isset($ch_info['ch_url']) && !empty($ch_info['ch_url']) && isset($ch_info['ch_dir']) && !empty($ch_info['ch_dir'])) {
if(is_dir($ch_info['ch_dir']) && is_writable($ch_info['ch_dir'])) {

# define and create directory
$cache_dir = $upload_dir['basedir'] . DIRECTORY_SEPARATOR . 'fvm-cache'. DIRECTORY_SEPARATOR . 'min';
$cache_dir_url = $upload_dir['baseurl'] . '/fvm-cache/min';
if(!is_dir($cache_dir) && function_exists('wp_mkdir_p')) { wp_mkdir_p($cache_dir); }

# filename
$file = $cache_dir . DIRECTORY_SEPARATOR . $filename;
$public = $cache_dir_url . '/' .$filename;

# cache date
$tvers = get_option('fvm_last_cache_update', '0');
$file = $ch_info['ch_dir'] . DIRECTORY_SEPARATOR . $filename;
$public = $ch_info['ch_url'] . '/' .$filename;

# wordpress functions
require_once (ABSPATH . DIRECTORY_SEPARATOR . 'wp-admin'. DIRECTORY_SEPARATOR .'includes'. DIRECTORY_SEPARATOR .'class-wp-filesystem-base.php');
require_once (ABSPATH . DIRECTORY_SEPARATOR .'wp-admin'. DIRECTORY_SEPARATOR .'includes'. DIRECTORY_SEPARATOR .'class-wp-filesystem-direct.php');

# create if doesn't exist
# initialize
$fileSystemDirect = new WP_Filesystem_Direct(false);

# create if doesn't exist
if(!$fileSystemDirect->exists($file) || ($fileSystemDirect->exists($file) && $fileSystemDirect->mtime($file) < $tvers)) {
$fileSystemDirect->put_contents($file, $code);
}

# return url
return $public;

}
}

Expand Down Expand Up @@ -498,44 +544,42 @@ function fvm_purge_static_files() {
# increment
update_option('fvm_last_cache_update', time());

# process
if( function_exists('wp_upload_dir') ) {

# current timestamp
$tvers = get_option('fvm_last_cache_update', '0');

$upload_dir = wp_upload_dir();
if(isset($upload_dir['basedir']) && isset($upload_dir['baseurl']) && !empty($upload_dir['basedir'])) {
# check cache directory
$ch_info = fvm_get_cache_location();
if(isset($ch_info['ch_url']) && !empty($ch_info['ch_url']) && isset($ch_info['ch_dir']) && !empty($ch_info['ch_dir'])) {
if(is_dir($ch_info['ch_dir']) && is_writable($ch_info['ch_dir'])) {

# wordpress functions
require_once (ABSPATH . DIRECTORY_SEPARATOR . 'wp-admin'. DIRECTORY_SEPARATOR .'includes'. DIRECTORY_SEPARATOR .'class-wp-filesystem-base.php');
require_once (ABSPATH . DIRECTORY_SEPARATOR .'wp-admin'. DIRECTORY_SEPARATOR .'includes'. DIRECTORY_SEPARATOR .'class-wp-filesystem-direct.php');


# start
$fileSystemDirect = new WP_Filesystem_Direct(false);

# instant purge
global $fvm_settings;
if(isset($fvm_settings['cache']['min_instant_purge']) && $fvm_settings['cache']['min_instant_purge'] == true) {
$cache_dir = $upload_dir['basedir'] . DIRECTORY_SEPARATOR . 'fvm-cache';
$fileSystemDirect = new WP_Filesystem_Direct(false);
$fileSystemDirect->rmdir($cache_dir, true);
$fileSystemDirect->rmdir($ch_info['ch_dir'], true);
return true;
} else {
$cache_dir = $upload_dir['basedir'] . DIRECTORY_SEPARATOR . 'fvm-cache'. DIRECTORY_SEPARATOR . 'min';
$fileSystemDirect = new WP_Filesystem_Direct(false);
} else {

# older than 24h and not matching current timestamp
$list = $fileSystemDirect->dirlist($cache_dir, false, true);
$list = $fileSystemDirect->dirlist($ch_info['ch_dir'], false, true);
if(is_array($list) && count($list) > 0) {
foreach($list as $k=>$arr) {
if(isset($arr['lastmodunix']) && $arr['type'] == 'f' && intval($arr['lastmodunix']) <= time()-86400) {
if(substr($arr['name'], 0, 10) !== $tvers) {
$fileSystemDirect->delete($cache_dir . DIRECTORY_SEPARATOR . $arr['name'], false, 'f');
if(substr($arr['name'], 0, 10) !== time()) {
$fileSystemDirect->delete($ch_info['ch_dir'] . DIRECTORY_SEPARATOR . $arr['name'], false, 'f');
}
}
}
}

}

}
}
}

}


Expand Down
15 changes: 11 additions & 4 deletions inc/frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,9 @@ function fvm_process_page($html) {
# START JS FILES
if(isset($tag->src)) {

# filter url
$href = fvm_normalize_url($tag->src);

# ignore js files
if(isset($fvm_settings['js']['ignore']) && !empty($fvm_settings['js']['ignore'])) {
$arr = fvm_string_toarray($fvm_settings['js']['ignore']);
Expand Down Expand Up @@ -649,9 +652,6 @@ function fvm_process_page($html) {
# jquery needs to load earlier, if not being merged (while merging is active)
if(stripos($tag->src, '/jquery.js') !== false || stripos($tag->src, '/jquery.min.js') !== false || stripos($tag->src, '/jquery-migrate') !== false) {

# filter url
$href = fvm_normalize_url($tag->src);

# http and html preload for render blocking js
if(!isset($fvm_settings['js']['nopreload']) || (isset($fvm_settings['js']['nopreload']) && $fvm_settings['js']['nopreload'] != true)) {
$htmlpreloads[] = '<link rel="preload" href="'.$href.'" as="script" />';
Expand All @@ -661,7 +661,7 @@ function fvm_process_page($html) {
if(stripos($tag->src, '/jquery-migrate') !== false) {
$htmljsheader[1] = "<script data-cfasync='false' src='".$href."'></script>"; # jquery migrate
} else {
$htmljsheader[0] = "<script data-cfasync='false' src='".$tag->src."'></script>"; # jquery
$htmljsheader[0] = "<script data-cfasync='false' src='".$href."'></script>"; # jquery
}

# content
Expand All @@ -678,6 +678,13 @@ function fvm_process_page($html) {
# minify individually, if enabled
if(!isset($fvm_settings['js']['min_disable']) || (isset($fvm_settings['js']['min_disable'])&& $fvm_settings['js']['min_disable'] != true)) {

# skip third party scripts, unless allowed
$allowed = array($fvm_urls['wp_domain'], '/ajax.aspnetcdn.com/ajax/', '/ajax.googleapis.com/ajax/libs/', '/cdnjs.cloudflare.com/ajax/libs/');
if(str_replace($allowed, '', $href) == $href) {
unset($allscripts[$k]);
continue;
}

# force render blocking
if(isset($fvm_settings['js']['merge_header']) && !empty($fvm_settings['js']['merge_header'])) {
$arr = fvm_string_toarray($fvm_settings['js']['merge_header']);
Expand Down
23 changes: 16 additions & 7 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Contributors: Alignak
Tags: PHP Minify, Lighthouse, GTmetrix, Pingdom, Pagespeed, Merging, Minification, Optimization, Speed, Performance, FVM
Requires at least: 4.9
Requires PHP: 5.6
Stable tag: 3.2.0
Stable tag: 3.2.2
Tested up to: 5.7.1
Text Domain: fast-velocity-minify
License: GPLv3 or later
Expand All @@ -13,25 +13,26 @@ Improve your speed score on GTmetrix, Pingdom Tools and Google PageSpeed Insight


== Description ==
Speed optimization plugin for developers and advanced users.
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.

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.
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.

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).

The plugin includes options for developers and advanced users, however the default settings should work just fine for most sites.
Kindly read the HELP section after installing the plugin, about possible issues and how to solve them.

= Additional Optimization =

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.
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.


= WP-CLI Commands =
* Purge all caches: `wp fvm purge`
* Purge all caches on a network site: `wp --url=blog.example.com fvm purge`
* Purge all caches on the entire network (linux): `wp site list --field=url | xargs -n1 -I % wp --url=% fvm purge`

= How to add your own critical path ? =
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.
= How to customize the cache path ? =
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');` .


== Installation ==
Expand All @@ -48,6 +49,14 @@ You can create a style tag, with an ID equal to "critical-path" ex: `<style id="

== Changelog ==

= 3.2.2 [2021.05.09] =
* added auto varnish cache purge for Cloudways
* fixed some JS files not being minified

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

= 3.2.0 [2021.05.06] =
* fixed an issue where some files were not being minified
* better sourceMappingURL removal during minification
Expand Down

0 comments on commit c267c0b

Please sign in to comment.