| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: Autoptimize |
| 4 |
Plugin URI: http://blog.futtta.be/category/autoptimize/ |
| 5 |
Description: Optimizes your website, concatenating the CSS and JavaScript code, and compressing it. |
| 6 |
Version: 1.6.4 |
| 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 |
// Load config and cache class |
| 14 |
include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeConfig.php'); |
| 15 |
include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeCache.php'); |
| 16 |
|
| 17 |
// Plugin constants |
| 18 |
define('AUTOPTIMIZE_CACHE_DIR',WP_CONTENT_DIR.'/cache/autoptimize/'); |
| 19 |
define('AUTOPTIMIZE_CACHE_URL',content_url().'/cache/autoptimize/'); |
| 20 |
define('AUTOPTIMIZE_CACHE_DELAY',true); |
| 21 |
define('WP_ROOT_URL',str_replace('/wp-content','',content_url())); |
| 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.6.4"; |
| 31 |
$autoptimize_db_version=get_option('autoptimize_version','none'); |
| 32 |
|
| 33 |
if ($autoptimize_db_version !== $autoptimize_version) { |
| 34 |
autoptimizeCache::clearall(); |
| 35 |
update_option('autoptimize_version',$autoptimize_version); |
| 36 |
$autoptimize_db_version=$autoptimize_version; |
| 37 |
} |
| 38 |
|
| 39 |
// Do we gzip when caching? |
| 40 |
define('AUTOPTIMIZE_CACHE_NOGZIP',(bool) $conf->get('autoptimize_cache_nogzip')); |
| 41 |
|
| 42 |
// Load translations |
| 43 |
$plugin_dir = basename(dirname(__FILE__)); |
| 44 |
load_plugin_textdomain('autoptimize','wp-content/plugins/'.$plugin_dir.'/localization',$plugin_dir.'/localization'); |
| 45 |
|
| 46 |
// Set up the buffering |
| 47 |
function autoptimize_start_buffering() |
| 48 |
{ |
| 49 |
if (!is_feed()) { |
| 50 |
|
| 51 |
// Config element |
| 52 |
$conf = autoptimizeConfig::instance(); |
| 53 |
|
| 54 |
// Load our base class |
| 55 |
include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeBase.php'); |
| 56 |
|
| 57 |
// Load extra classes and set some vars |
| 58 |
if($conf->get('autoptimize_html')) |
| 59 |
{ |
| 60 |
include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeHTML.php'); |
| 61 |
@include(WP_PLUGIN_DIR.'/autoptimize/classes/minify-html.php'); |
| 62 |
} |
| 63 |
|
| 64 |
if($conf->get('autoptimize_js')) |
| 65 |
{ |
| 66 |
include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeScripts.php'); |
| 67 |
@include(WP_PLUGIN_DIR.'/autoptimize/classes/jsmin-1.1.1.php'); |
| 68 |
define('CONCATENATE_SCRIPTS',false); |
| 69 |
define('COMPRESS_SCRIPTS',false); |
| 70 |
} |
| 71 |
|
| 72 |
if($conf->get('autoptimize_css')) |
| 73 |
{ |
| 74 |
include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeStyles.php'); |
| 75 |
@include(WP_PLUGIN_DIR.'/autoptimize/classes/minify-css-compressor.php'); |
| 76 |
define('COMPRESS_CSS',false); |
| 77 |
} |
| 78 |
|
| 79 |
if($conf->get('autoptimize_js_yui') || $conf->get('autoptimize_css_yui')) |
| 80 |
{ |
| 81 |
include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeYUI.php'); |
| 82 |
} |
| 83 |
|
| 84 |
if($conf->get('autoptimize_cdn_js') || $conf->get('autoptimize_cdn_css')) |
| 85 |
{ |
| 86 |
include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeCDN.php'); |
| 87 |
} |
| 88 |
|
| 89 |
// Now, start the real thing! |
| 90 |
ob_start('autoptimize_end_buffering'); |
| 91 |
|
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
//Action on end - |
| 96 |
function autoptimize_end_buffering($content) |
| 97 |
{ |
| 98 |
// Config element |
| 99 |
$conf = autoptimizeConfig::instance(); |
| 100 |
|
| 101 |
// Choose the classes |
| 102 |
$classes = array(); |
| 103 |
if($conf->get('autoptimize_js')) |
| 104 |
$classes[] = 'autoptimizeScripts'; |
| 105 |
if($conf->get('autoptimize_css')) |
| 106 |
$classes[] = 'autoptimizeStyles'; |
| 107 |
if($conf->get('autoptimize_cdn_js') || $conf->get('autoptimize_cdn_css')) |
| 108 |
$classes[] = 'autoptimizeCDN'; |
| 109 |
if($conf->get('autoptimize_html')) |
| 110 |
$classes[] = 'autoptimizeHTML'; |
| 111 |
|
| 112 |
// Set some options |
| 113 |
$classoptions = array( |
| 114 |
'autoptimizeScripts' => array( |
| 115 |
'justhead' => $conf->get('autoptimize_js_justhead'), |
| 116 |
'trycatch' => $conf->get('autoptimize_js_trycatch'), |
| 117 |
'yui' => $conf->get('autoptimize_js_yui'), |
| 118 |
'exclude' => $conf->get('autoptimize_js_exclude') |
| 119 |
), |
| 120 |
'autoptimizeStyles' => array( |
| 121 |
'justhead' => $conf->get('autoptimize_css_justhead'), |
| 122 |
'datauris' => $conf->get('autoptimize_css_datauris'), |
| 123 |
'yui' => $conf->get('autoptimize_css_yui'), |
| 124 |
), |
| 125 |
'autoptimizeCDN' => array( |
| 126 |
'js' => $conf->get('autoptimize_cdn_js'), |
| 127 |
'jsurl' => $conf->get('autoptimize_cdn_js_url'), |
| 128 |
'css' => $conf->get('autoptimize_cdn_css'), |
| 129 |
'cssurl' => $conf->get('autoptimize_cdn_css_url'), |
| 130 |
'img' => $conf->get('autoptimize_cdn_img'), |
| 131 |
'imgurl' => $conf->get('autoptimize_cdn_img_url') |
| 132 |
), |
| 133 |
'autoptimizeHTML' => array( |
| 134 |
'keepcomments' => $conf->get('autoptimize_html_keepcomments') |
| 135 |
) |
| 136 |
); |
| 137 |
|
| 138 |
|
| 139 |
// Run the classes |
| 140 |
foreach($classes as $name) |
| 141 |
{ |
| 142 |
$instance = new $name($content); |
| 143 |
if($instance->read($classoptions[$name])) |
| 144 |
{ |
| 145 |
$instance->minify(); |
| 146 |
$instance->cache(); |
| 147 |
$content = $instance->getcontent(); |
| 148 |
} |
| 149 |
unset($instance); |
| 150 |
} |
| 151 |
return $content; |
| 152 |
} |
| 153 |
|
| 154 |
if(autoptimizeCache::cacheavail()) |
| 155 |
{ |
| 156 |
$conf = autoptimizeConfig::instance(); |
| 157 |
if( $conf->get('autoptimize_html') || $conf->get('autoptimize_js') || $conf->get('autoptimize_css') || $conf->get('autoptimize_cdn_js') || $conf->get('autoptimize_cdn_css')) |
| 158 |
{ |
| 159 |
// Hook to wordpress |
| 160 |
add_action('template_redirect','autoptimize_start_buffering',2); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
// Do not pollute other plugins |
| 165 |
unset($conf); |
| 166 |
|