| 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.7.1 |
| 7 |
Author: Frank Goossens (futtta) |
| 8 |
Author URI: http://blog.futtta.be/ |
| 9 |
Released under the GNU General Public License (GPL) |
| 10 |
http://www.gnu.org/licenses/gpl.txt |
| 11 |
*/ |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 14 |
|
| 15 |
// Load config and cache class |
| 16 |
include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeConfig.php'); |
| 17 |
include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeCache.php'); |
| 18 |
|
| 19 |
// Plugin dir constants (plugin url's defined later to accomodate domain mapped sites) |
| 20 |
define('AUTOPTIMIZE_CACHE_DIR',WP_CONTENT_DIR.'/cache/autoptimize/'); |
| 21 |
define('AUTOPTIMIZE_CACHE_DELAY',true); |
| 22 |
define('WP_ROOT_DIR',str_replace('/wp-content','',WP_CONTENT_DIR)); |
| 23 |
|
| 24 |
// Initialize the cache at least once |
| 25 |
$conf = autoptimizeConfig::instance(); |
| 26 |
|
| 27 |
/* Check if we're updating, in which case we need to flush the cache |
| 28 |
to avoid old versions of aggregated files lingering around */ |
| 29 |
|
| 30 |
$autoptimize_version="1.7.1"; |
| 31 |
$autoptimize_db_version=get_option('autoptimize_version','none'); |
| 32 |
|
| 33 |
if ($autoptimize_db_version !== $autoptimize_version) { |
| 34 |
if ($autoptimize_db_version==="none") { |
| 35 |
add_action('admin_notices', 'autoptimize_install_config_notice'); |
| 36 |
} else if (strpos($autoptimize_db_version,"1.6.")!==false) { |
| 37 |
// if user was on version 1.6.x, force advanced options to be shown by default |
| 38 |
update_option('autoptimize_show_adv','1'); |
| 39 |
|
| 40 |
// and remove old options |
| 41 |
$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"); |
| 42 |
foreach ($delete_options as $del_opt) { |
| 43 |
delete_option( $del_opt ); |
| 44 |
} |
| 45 |
|
| 46 |
// and notify user to check result |
| 47 |
add_action('admin_notices', 'autoptimize_update_config_notice'); |
| 48 |
} |
| 49 |
|
| 50 |
autoptimizeCache::clearall(); |
| 51 |
update_option('autoptimize_version',$autoptimize_version); |
| 52 |
$autoptimize_db_version=$autoptimize_version; |
| 53 |
} |
| 54 |
|
| 55 |
// Do we gzip when caching? |
| 56 |
define('AUTOPTIMIZE_CACHE_NOGZIP',(bool) $conf->get('autoptimize_cache_nogzip')); |
| 57 |
|
| 58 |
// Load translations |
| 59 |
$plugin_dir = basename(dirname(__FILE__)); |
| 60 |
load_plugin_textdomain('autoptimize','wp-content/plugins/'.$plugin_dir.'/localization',$plugin_dir.'/localization'); |
| 61 |
|
| 62 |
function autoptimize_install_config_notice() { |
| 63 |
echo '<div class="updated"><p>'; |
| 64 |
_e('Thank you for installing and activating Autoptimize. Please configure it under "Settings" -> "Autoptimize" to start improving your site\'s performance.', 'autoptimize' ); |
| 65 |
echo '</p></div>'; |
| 66 |
} |
| 67 |
|
| 68 |
function autoptimize_update_config_notice() { |
| 69 |
echo '<div class="updated"><p>'; |
| 70 |
_e('Autoptimize has just been updated. Please <strong>test your site now</strong> and adapt Autoptimize config if needed.', 'autoptimize' ); |
| 71 |
echo '</p></div>'; |
| 72 |
} |
| 73 |
|
| 74 |
// Set up the buffering |
| 75 |
function autoptimize_start_buffering() |
| 76 |
{ |
| 77 |
if (!is_feed()) { |
| 78 |
|
| 79 |
// Config element |
| 80 |
$conf = autoptimizeConfig::instance(); |
| 81 |
|
| 82 |
// Load our base class |
| 83 |
include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeBase.php'); |
| 84 |
|
| 85 |
// Load extra classes and set some vars |
| 86 |
if($conf->get('autoptimize_html')) |
| 87 |
{ |
| 88 |
include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeHTML.php'); |
| 89 |
// BUG: new minify-html does not support keeping HTML comments, skipping for now |
| 90 |
// if (defined('AUTOPTIMIZE_LEGACY_MINIFIERS')) { |
| 91 |
@include(WP_PLUGIN_DIR.'/autoptimize/classes/external/php/minify-html.php'); |
| 92 |
// } else { |
| 93 |
// @include(WP_PLUGIN_DIR.'/autoptimize/classes/external/php/minify-2.1.7-html.php'); |
| 94 |
// } |
| 95 |
} |
| 96 |
|
| 97 |
if($conf->get('autoptimize_js')) |
| 98 |
{ |
| 99 |
include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeScripts.php'); |
| 100 |
if (defined('AUTOPTIMIZE_LEGACY_MINIFIERS')) { |
| 101 |
@include(WP_PLUGIN_DIR.'/autoptimize/classes/external/php/jsmin-1.1.1.php'); |
| 102 |
} else { |
| 103 |
@include(WP_PLUGIN_DIR.'/autoptimize/classes/external/php/minify-2.1.7-jsmin.php'); |
| 104 |
} |
| 105 |
define('CONCATENATE_SCRIPTS',false); |
| 106 |
define('COMPRESS_SCRIPTS',false); |
| 107 |
} |
| 108 |
|
| 109 |
if($conf->get('autoptimize_css')) |
| 110 |
{ |
| 111 |
include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeStyles.php'); |
| 112 |
if (defined('AUTOPTIMIZE_LEGACY_MINIFIERS')) { |
| 113 |
@include(WP_PLUGIN_DIR.'/autoptimize/classes/external/php/minify-css-compressor.php'); |
| 114 |
} else { |
| 115 |
@include(WP_PLUGIN_DIR.'/autoptimize/classes/external/php/yui-php-cssmin-2.4.8-1.php'); |
| 116 |
} |
| 117 |
define('COMPRESS_CSS',false); |
| 118 |
} |
| 119 |
|
| 120 |
// Now, start the real thing! |
| 121 |
ob_start('autoptimize_end_buffering'); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
//Action on end - |
| 126 |
function autoptimize_end_buffering($content) |
| 127 |
{ |
| 128 |
// load URL constants as late as possible to allow domain mapper to kick in |
| 129 |
if (function_exists(domain_mapping_siteurl)) { |
| 130 |
define('AUTOPTIMIZE_WP_SITE_URL',domain_mapping_siteurl()); |
| 131 |
define('AUTOPTIMIZE_WP_CONTENT_URL',str_replace(get_original_url(AUTOPTIMIZE_WP_SITE_URL),AUTOPTIMIZE_WP_SITE_URL,content_url())); |
| 132 |
} else { |
| 133 |
define('AUTOPTIMIZE_WP_SITE_URL',site_url()); |
| 134 |
define('AUTOPTIMIZE_WP_CONTENT_URL',content_url()); |
| 135 |
} |
| 136 |
define('AUTOPTIMIZE_CACHE_URL',AUTOPTIMIZE_WP_CONTENT_URL.'/cache/autoptimize/'); |
| 137 |
define('AUTOPTIMIZE_WP_ROOT_URL',str_replace('/wp-content','',AUTOPTIMIZE_WP_CONTENT_URL)); |
| 138 |
// Config element |
| 139 |
$conf = autoptimizeConfig::instance(); |
| 140 |
|
| 141 |
// Choose the classes |
| 142 |
$classes = array(); |
| 143 |
if($conf->get('autoptimize_js')) |
| 144 |
$classes[] = 'autoptimizeScripts'; |
| 145 |
if($conf->get('autoptimize_css')) |
| 146 |
$classes[] = 'autoptimizeStyles'; |
| 147 |
if($conf->get('autoptimize_html')) |
| 148 |
$classes[] = 'autoptimizeHTML'; |
| 149 |
|
| 150 |
// Set some options |
| 151 |
$classoptions = array( |
| 152 |
'autoptimizeScripts' => array( |
| 153 |
'justhead' => $conf->get('autoptimize_js_justhead'), |
| 154 |
'forcehead' => $conf->get('autoptimize_js_forcehead'), |
| 155 |
'trycatch' => $conf->get('autoptimize_js_trycatch'), |
| 156 |
'js_exclude' => $conf->get('autoptimize_js_exclude'), |
| 157 |
'cdn_url' => $conf->get('autoptimize_cdn_url') |
| 158 |
), |
| 159 |
'autoptimizeStyles' => array( |
| 160 |
'justhead' => $conf->get('autoptimize_css_justhead'), |
| 161 |
'datauris' => $conf->get('autoptimize_css_datauris'), |
| 162 |
'defer' => $conf->get('autoptimize_css_defer'), |
| 163 |
'css_exclude' => $conf->get('autoptimize_css_exclude'), |
| 164 |
'cdn_url' => $conf->get('autoptimize_cdn_url') |
| 165 |
), |
| 166 |
'autoptimizeHTML' => array( |
| 167 |
'keepcomments' => $conf->get('autoptimize_html_keepcomments') |
| 168 |
) |
| 169 |
); |
| 170 |
|
| 171 |
|
| 172 |
// Run the classes |
| 173 |
foreach($classes as $name) |
| 174 |
{ |
| 175 |
$instance = new $name($content); |
| 176 |
if($instance->read($classoptions[$name])) |
| 177 |
{ |
| 178 |
$instance->minify(); |
| 179 |
$instance->cache(); |
| 180 |
$content = $instance->getcontent(); |
| 181 |
} |
| 182 |
unset($instance); |
| 183 |
} |
| 184 |
return $content; |
| 185 |
} |
| 186 |
|
| 187 |
if(autoptimizeCache::cacheavail()) |
| 188 |
{ |
| 189 |
$conf = autoptimizeConfig::instance(); |
| 190 |
if( $conf->get('autoptimize_html') || $conf->get('autoptimize_js') || $conf->get('autoptimize_css') || $conf->get('autoptimize_cdn_js') || $conf->get('autoptimize_cdn_css')) |
| 191 |
{ |
| 192 |
// Hook to wordpress |
| 193 |
add_action('template_redirect','autoptimize_start_buffering',2); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
// Do not pollute other plugins |
| 198 |
unset($conf); |
| 199 |
|