| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: Autoptimize |
| 4 |
Plugin URI: http://blog.futtta.be/autoptimize |
| 5 |
Description: Optimizes your website, concatenating the CSS and JavaScript code, and compressing it. |
| 6 |
Version: 1.8.1 |
| 7 |
Author: Frank Goossens (futtta) |
| 8 |
Author URI: http://blog.futtta.be/ |
| 9 |
Domain Path: localization/ |
| 10 |
Text Domain: autoptimize |
| 11 |
Released under the GNU General Public License (GPL) |
| 12 |
http://www.gnu.org/licenses/gpl.txt |
| 13 |
*/ |
| 14 |
|
| 15 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 16 |
|
| 17 |
// Load config class |
| 18 |
include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeConfig.php'); |
| 19 |
|
| 20 |
// Do we gzip when caching (needed early to load autoptimizeCache.php) |
| 21 |
define('AUTOPTIMIZE_CACHE_NOGZIP',(bool) get_option('autoptimize_cache_nogzip')); |
| 22 |
|
| 23 |
// Load cache class |
| 24 |
include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeCache.php'); |
| 25 |
|
| 26 |
// Plugin dir constants (plugin url's defined later to accomodate domain mapped sites) |
| 27 |
define('AUTOPTIMIZE_CACHE_DIR',WP_CONTENT_DIR.'/cache/autoptimize/'); |
| 28 |
define('AUTOPTIMIZE_CACHE_DELAY',true); |
| 29 |
define('WP_ROOT_DIR',str_replace('/wp-content','',WP_CONTENT_DIR)); |
| 30 |
|
| 31 |
// Initialize the cache at least once |
| 32 |
$conf = autoptimizeConfig::instance(); |
| 33 |
|
| 34 |
/* Check if we're updating, in which case we might need to do stuff and flush the cache |
| 35 |
to avoid old versions of aggregated files lingering around */ |
| 36 |
|
| 37 |
$autoptimize_version="1.8.1"; |
| 38 |
$autoptimize_db_version=get_option('autoptimize_version','none'); |
| 39 |
|
| 40 |
if ($autoptimize_db_version !== $autoptimize_version) { |
| 41 |
if ($autoptimize_db_version==="none") { |
| 42 |
add_action('admin_notices', 'autoptimize_install_config_notice'); |
| 43 |
} else { |
| 44 |
$autoptimize_major_version=substr($autoptimize_db_version,0,3); |
| 45 |
switch($autoptimize_major_version) { |
| 46 |
case "1.6": |
| 47 |
// from back in the days when I did not yet consider multisite |
| 48 |
// if user was on version 1.6.x, force advanced options to be shown by default |
| 49 |
update_option('autoptimize_show_adv','1'); |
| 50 |
|
| 51 |
// and remove old options |
| 52 |
$to_delete_options=array("autoptimize_cdn_css","autoptimize_cdn_css_url","autoptimize_cdn_js","autoptimize_cdn_js_url","autoptimize_cdn_img","autoptimize_cdn_img_url","autoptimize_css_yui","autoptimize_js_yui"); |
| 53 |
foreach ($to_delete_options as $del_opt) { |
| 54 |
delete_option( $del_opt ); |
| 55 |
} |
| 56 |
|
| 57 |
// and notify user to check result |
| 58 |
add_action('admin_notices', 'autoptimize_update_config_notice'); |
| 59 |
case "1.7": |
| 60 |
// force 3.8 dashicons in CSS exclude options when upgrading from 1.7 to 1.8 |
| 61 |
if ( !is_multisite() ) { |
| 62 |
$css_exclude = get_option('autoptimize_css_exclude'); |
| 63 |
if (empty($css_exclude)) { |
| 64 |
$css_exclude = "admin-bar.min.css, dashicons.min.css"; |
| 65 |
} else if (strpos($css_exclude,"dashicons.min.css")===false) { |
| 66 |
$css_exclude .= ", dashicons.min.css"; |
| 67 |
} |
| 68 |
update_option('autoptimize_css_exclude',$css_exclude); |
| 69 |
} else { |
| 70 |
global $wpdb; |
| 71 |
$blog_ids = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" ); |
| 72 |
$original_blog_id = get_current_blog_id(); |
| 73 |
foreach ( $blog_ids as $blog_id ) { |
| 74 |
switch_to_blog( $blog_id ); |
| 75 |
$css_exclude = get_option('autoptimize_css_exclude'); |
| 76 |
if (empty($css_exclude)) { |
| 77 |
$css_exclude = "admin-bar.min.css, dashicons.min.css"; |
| 78 |
} else if (strpos($css_exclude,"dashicons.min.css")===false) { |
| 79 |
$css_exclude .= ", dashicons.min.css"; |
| 80 |
} |
| 81 |
update_option('autoptimize_css_exclude',$css_exclude); |
| 82 |
} |
| 83 |
switch_to_blog( $original_blog_id ); |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
autoptimizeCache::clearall(); |
| 89 |
update_option('autoptimize_version',$autoptimize_version); |
| 90 |
$autoptimize_db_version=$autoptimize_version; |
| 91 |
} |
| 92 |
|
| 93 |
// Load translations |
| 94 |
$plugin_dir = basename(dirname(__FILE__)); |
| 95 |
load_plugin_textdomain('autoptimize','wp-content/plugins/'.$plugin_dir.'/localization',$plugin_dir.'/localization'); |
| 96 |
|
| 97 |
function autoptimize_uninstall(){ |
| 98 |
autoptimizeCache::clearall(); |
| 99 |
|
| 100 |
$delete_options=array("autoptimize_cache_clean", "autoptimize_cache_nogzip", "autoptimize_css", "autoptimize_css_datauris", "autoptimize_css_justhead", "autoptimize_css_defer", "autoptimize_css_inline", "autoptimize_css_exclude", "autoptimize_html", "autoptimize_html_keepcomments", "autoptimize_js", "autoptimize_js_exclude", "autoptimize_js_forcehead", "autoptimize_js_justhead", "autoptimize_js_trycatch", "autoptimize_version", "autoptimize_show_adv", "autoptimize_cdn_url"); |
| 101 |
|
| 102 |
if ( !is_multisite() ) { |
| 103 |
foreach ($delete_options as $del_opt) { delete_option( $del_opt ); } |
| 104 |
} else { |
| 105 |
global $wpdb; |
| 106 |
$blog_ids = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" ); |
| 107 |
$original_blog_id = get_current_blog_id(); |
| 108 |
foreach ( $blog_ids as $blog_id ) |
| 109 |
{ |
| 110 |
switch_to_blog( $blog_id ); |
| 111 |
foreach ($delete_options as $del_opt) { delete_option( $del_opt ); } |
| 112 |
} |
| 113 |
switch_to_blog( $original_blog_id ); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
function autoptimize_install_config_notice() { |
| 118 |
echo '<div class="updated"><p>'; |
| 119 |
_e('Thank you for installing and activating Autoptimize. Please configure it under "Settings" -> "Autoptimize" to start improving your site\'s performance.', 'autoptimize' ); |
| 120 |
echo '</p></div>'; |
| 121 |
} |
| 122 |
|
| 123 |
function autoptimize_update_config_notice() { |
| 124 |
echo '<div class="updated"><p>'; |
| 125 |
_e('Autoptimize has just been updated. Please <strong>test your site now</strong> and adapt Autoptimize config if needed.', 'autoptimize' ); |
| 126 |
echo '</p></div>'; |
| 127 |
} |
| 128 |
|
| 129 |
// Set up the buffering |
| 130 |
function autoptimize_start_buffering() { |
| 131 |
$ao_noptimize = false; |
| 132 |
|
| 133 |
// filter you can use to block autoptimization on your own terms |
| 134 |
$ao_noptimize = (bool) apply_filters( 'autoptimize_filter_noptimize', $ao_noptimize ); |
| 135 |
|
| 136 |
if (!is_feed() && !$ao_noptimize ) { |
| 137 |
|
| 138 |
// Config element |
| 139 |
$conf = autoptimizeConfig::instance(); |
| 140 |
|
| 141 |
// Load our base class |
| 142 |
include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeBase.php'); |
| 143 |
|
| 144 |
// Load extra classes and set some vars |
| 145 |
if($conf->get('autoptimize_html')) { |
| 146 |
include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeHTML.php'); |
| 147 |
// BUG: new minify-html does not support keeping HTML comments, skipping for now |
| 148 |
// if (defined('AUTOPTIMIZE_LEGACY_MINIFIERS')) { |
| 149 |
@include(WP_PLUGIN_DIR.'/autoptimize/classes/external/php/minify-html.php'); |
| 150 |
// } else { |
| 151 |
// @include(WP_PLUGIN_DIR.'/autoptimize/classes/external/php/minify-2.1.7-html.php'); |
| 152 |
// } |
| 153 |
} |
| 154 |
|
| 155 |
if($conf->get('autoptimize_js')) { |
| 156 |
include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeScripts.php'); |
| 157 |
if (!class_exists('JSMin')) { |
| 158 |
if (defined('AUTOPTIMIZE_LEGACY_MINIFIERS')) { |
| 159 |
@include(WP_PLUGIN_DIR.'/autoptimize/classes/external/php/jsmin-1.1.1.php'); |
| 160 |
} else { |
| 161 |
@include(WP_PLUGIN_DIR.'/autoptimize/classes/external/php/minify-2.1.7-jsmin.php'); |
| 162 |
} |
| 163 |
} |
| 164 |
define('CONCATENATE_SCRIPTS',false); |
| 165 |
define('COMPRESS_SCRIPTS',false); |
| 166 |
} |
| 167 |
|
| 168 |
if($conf->get('autoptimize_css')) { |
| 169 |
include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeStyles.php'); |
| 170 |
if (defined('AUTOPTIMIZE_LEGACY_MINIFIERS')) { |
| 171 |
if (!class_exists('Minify_CSS_Compressor')) { |
| 172 |
@include(WP_PLUGIN_DIR.'/autoptimize/classes/external/php/minify-css-compressor.php'); |
| 173 |
} |
| 174 |
} else { |
| 175 |
if (!class_exists('CSSmin')) { |
| 176 |
@include(WP_PLUGIN_DIR.'/autoptimize/classes/external/php/yui-php-cssmin-2.4.8-2_fgo.php'); |
| 177 |
} |
| 178 |
} |
| 179 |
define('COMPRESS_CSS',false); |
| 180 |
} |
| 181 |
|
| 182 |
// Now, start the real thing! |
| 183 |
ob_start('autoptimize_end_buffering'); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
//Action on end - |
| 188 |
function autoptimize_end_buffering($content) { |
| 189 |
if ( stripos($content,"<html") === false || stripos($content,"<xsl:stylesheet") !== false ) { return $content;} |
| 190 |
|
| 191 |
// load URL constants as late as possible to allow domain mapper to kick in |
| 192 |
if (function_exists("domain_mapping_siteurl")) { |
| 193 |
define('AUTOPTIMIZE_WP_SITE_URL',domain_mapping_siteurl(get_current_blog_id())); |
| 194 |
define('AUTOPTIMIZE_WP_CONTENT_URL',str_replace(get_original_url(AUTOPTIMIZE_WP_SITE_URL),AUTOPTIMIZE_WP_SITE_URL,content_url())); |
| 195 |
} else { |
| 196 |
define('AUTOPTIMIZE_WP_SITE_URL',site_url()); |
| 197 |
define('AUTOPTIMIZE_WP_CONTENT_URL',content_url()); |
| 198 |
} |
| 199 |
define('AUTOPTIMIZE_CACHE_URL',AUTOPTIMIZE_WP_CONTENT_URL.'/cache/autoptimize/'); |
| 200 |
define('AUTOPTIMIZE_WP_ROOT_URL',str_replace('/wp-content','',AUTOPTIMIZE_WP_CONTENT_URL)); |
| 201 |
|
| 202 |
// Config element |
| 203 |
$conf = autoptimizeConfig::instance(); |
| 204 |
|
| 205 |
// Choose the classes |
| 206 |
$classes = array(); |
| 207 |
if($conf->get('autoptimize_js')) |
| 208 |
$classes[] = 'autoptimizeScripts'; |
| 209 |
if($conf->get('autoptimize_css')) |
| 210 |
$classes[] = 'autoptimizeStyles'; |
| 211 |
if($conf->get('autoptimize_html')) |
| 212 |
$classes[] = 'autoptimizeHTML'; |
| 213 |
|
| 214 |
// Set some options |
| 215 |
$classoptions = array( |
| 216 |
'autoptimizeScripts' => array( |
| 217 |
'justhead' => $conf->get('autoptimize_js_justhead'), |
| 218 |
'forcehead' => $conf->get('autoptimize_js_forcehead'), |
| 219 |
'trycatch' => $conf->get('autoptimize_js_trycatch'), |
| 220 |
'js_exclude' => $conf->get('autoptimize_js_exclude'), |
| 221 |
'cdn_url' => $conf->get('autoptimize_cdn_url') |
| 222 |
), |
| 223 |
'autoptimizeStyles' => array( |
| 224 |
'justhead' => $conf->get('autoptimize_css_justhead'), |
| 225 |
'datauris' => $conf->get('autoptimize_css_datauris'), |
| 226 |
'defer' => $conf->get('autoptimize_css_defer'), |
| 227 |
'inline' => $conf->get('autoptimize_css_inline'), |
| 228 |
'css_exclude' => $conf->get('autoptimize_css_exclude'), |
| 229 |
'cdn_url' => $conf->get('autoptimize_cdn_url') |
| 230 |
), |
| 231 |
'autoptimizeHTML' => array( |
| 232 |
'keepcomments' => $conf->get('autoptimize_html_keepcomments') |
| 233 |
) |
| 234 |
); |
| 235 |
|
| 236 |
|
| 237 |
// Run the classes |
| 238 |
foreach($classes as $name) { |
| 239 |
$instance = new $name($content); |
| 240 |
if($instance->read($classoptions[$name])) |
| 241 |
{ |
| 242 |
$instance->minify(); |
| 243 |
$instance->cache(); |
| 244 |
$content = $instance->getcontent(); |
| 245 |
} |
| 246 |
unset($instance); |
| 247 |
} |
| 248 |
return $content; |
| 249 |
} |
| 250 |
|
| 251 |
if(autoptimizeCache::cacheavail()) { |
| 252 |
$conf = autoptimizeConfig::instance(); |
| 253 |
if( $conf->get('autoptimize_html') || $conf->get('autoptimize_js') || $conf->get('autoptimize_css') || $conf->get('autoptimize_cdn_js') || $conf->get('autoptimize_cdn_css')) { |
| 254 |
// Hook to wordpress |
| 255 |
add_action('template_redirect','autoptimize_start_buffering',2); |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
register_uninstall_hook(__FILE__, "autoptimize_uninstall"); |
| 260 |
|
| 261 |
// Do not pollute other plugins |
| 262 |
unset($conf); |
| 263 |
|