| 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.0 |
| 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_PATH',str_replace('/wp-content','',WP_CONTENT_DIR)); |
| 23 |
|
| 24 |
// Initialize the cache at least once |
| 25 |
$conf = autoptimizeConfig::instance(); |
| 26 |
|
| 27 |
// Do we gzip when caching? |
| 28 |
define('AUTOPTIMIZE_CACHE_NOGZIP',(bool) $conf->get('autoptimize_cache_nogzip')); |
| 29 |
|
| 30 |
// Load translations |
| 31 |
$plugin_dir = basename(dirname(__FILE__)); |
| 32 |
load_plugin_textdomain('autoptimize','wp-content/plugins/'.$plugin_dir.'/localization',$plugin_dir.'/localization'); |
| 33 |
|
| 34 |
// Set up the buffering |
| 35 |
function autoptimize_start_buffering() |
| 36 |
{ |
| 37 |
// if (!is_user_logged_in()) { |
| 38 |
|
| 39 |
// Config element |
| 40 |
$conf = autoptimizeConfig::instance(); |
| 41 |
|
| 42 |
// Load our base class |
| 43 |
include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeBase.php'); |
| 44 |
|
| 45 |
// Load extra classes and set some vars |
| 46 |
if($conf->get('autoptimize_html')) |
| 47 |
{ |
| 48 |
include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeHTML.php'); |
| 49 |
@include(WP_PLUGIN_DIR.'/autoptimize/classes/minify-html.php'); |
| 50 |
} |
| 51 |
|
| 52 |
if($conf->get('autoptimize_js')) |
| 53 |
{ |
| 54 |
include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeScripts.php'); |
| 55 |
@include(WP_PLUGIN_DIR.'/autoptimize/classes/jsmin-1.1.1.php'); |
| 56 |
define('CONCATENATE_SCRIPTS',false); |
| 57 |
define('COMPRESS_SCRIPTS',false); |
| 58 |
} |
| 59 |
|
| 60 |
if($conf->get('autoptimize_css')) |
| 61 |
{ |
| 62 |
include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeStyles.php'); |
| 63 |
@include(WP_PLUGIN_DIR.'/autoptimize/classes/minify-css-compressor.php'); |
| 64 |
define('COMPRESS_CSS',false); |
| 65 |
} |
| 66 |
|
| 67 |
if($conf->get('autoptimize_js_yui') || $conf->get('autoptimize_css_yui')) |
| 68 |
{ |
| 69 |
include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeYUI.php'); |
| 70 |
} |
| 71 |
|
| 72 |
if($conf->get('autoptimize_cdn_js') || $conf->get('autoptimize_cdn_css')) |
| 73 |
{ |
| 74 |
include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeCDN.php'); |
| 75 |
} |
| 76 |
|
| 77 |
// Now, start the real thing! |
| 78 |
ob_start('autoptimize_end_buffering'); |
| 79 |
|
| 80 |
// } |
| 81 |
} |
| 82 |
|
| 83 |
//Action on end - |
| 84 |
function autoptimize_end_buffering($content) |
| 85 |
{ |
| 86 |
// Config element |
| 87 |
$conf = autoptimizeConfig::instance(); |
| 88 |
|
| 89 |
// Choose the classes |
| 90 |
$classes = array(); |
| 91 |
if($conf->get('autoptimize_js')) |
| 92 |
$classes[] = 'autoptimizeScripts'; |
| 93 |
if($conf->get('autoptimize_css')) |
| 94 |
$classes[] = 'autoptimizeStyles'; |
| 95 |
if($conf->get('autoptimize_cdn_js') || $conf->get('autoptimize_cdn_css')) |
| 96 |
$classes[] = 'autoptimizeCDN'; |
| 97 |
if($conf->get('autoptimize_html')) |
| 98 |
$classes[] = 'autoptimizeHTML'; |
| 99 |
|
| 100 |
// Set some options |
| 101 |
$classoptions = array( |
| 102 |
'autoptimizeScripts' => array( |
| 103 |
'justhead' => $conf->get('autoptimize_js_justhead'), |
| 104 |
'trycatch' => $conf->get('autoptimize_js_trycatch'), |
| 105 |
'yui' => $conf->get('autoptimize_js_yui'), |
| 106 |
'exclude' => $conf->get('autoptimize_js_exclude') |
| 107 |
), |
| 108 |
'autoptimizeStyles' => array( |
| 109 |
'justhead' => $conf->get('autoptimize_css_justhead'), |
| 110 |
'datauris' => $conf->get('autoptimize_css_datauris'), |
| 111 |
'yui' => $conf->get('autoptimize_css_yui'), |
| 112 |
), |
| 113 |
'autoptimizeCDN' => array( |
| 114 |
'js' => $conf->get('autoptimize_cdn_js'), |
| 115 |
'jsurl' => $conf->get('autoptimize_cdn_js_url'), |
| 116 |
'css' => $conf->get('autoptimize_cdn_css'), |
| 117 |
'cssurl' => $conf->get('autoptimize_cdn_css_url'), |
| 118 |
'img' => $conf->get('autoptimize_cdn_img'), |
| 119 |
'imgurl' => $conf->get('autoptimize_cdn_img_url') |
| 120 |
), |
| 121 |
'autoptimizeHTML' => array( |
| 122 |
'keepcomments' => $conf->get('autoptimize_html_keepcomments') |
| 123 |
) |
| 124 |
); |
| 125 |
|
| 126 |
|
| 127 |
// Run the classes |
| 128 |
foreach($classes as $name) |
| 129 |
{ |
| 130 |
$instance = new $name($content); |
| 131 |
if($instance->read($classoptions[$name])) |
| 132 |
{ |
| 133 |
$instance->minify(); |
| 134 |
$instance->cache(); |
| 135 |
$content = $instance->getcontent(); |
| 136 |
} |
| 137 |
unset($instance); |
| 138 |
} |
| 139 |
return $content; |
| 140 |
} |
| 141 |
|
| 142 |
if(autoptimizeCache::cacheavail()) |
| 143 |
{ |
| 144 |
$conf = autoptimizeConfig::instance(); |
| 145 |
if( $conf->get('autoptimize_html') || $conf->get('autoptimize_js') || $conf->get('autoptimize_css') || $conf->get('autoptimize_cdn_js') || $conf->get('autoptimize_cdn_css')) |
| 146 |
{ |
| 147 |
// Hook to wordpress |
| 148 |
add_action('template_redirect','autoptimize_start_buffering',2); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
// Do not pollute other plugins |
| 153 |
unset($conf); |
| 154 |
|