| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: Autoptimize |
| 4 |
Plugin URI: http://www.turleando.com.ar/autoptimize/ |
| 5 |
Description: Optimizes your website, concatenating the CSS and JavaScript code, and compressing it. |
| 6 |
Version: 0.9 |
| 7 |
Author: Emilio López |
| 8 |
Author URI: http://www.turleando.com.ar/ |
| 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',WP_CONTENT_URL.'/cache/autoptimize/'); |
| 20 |
|
| 21 |
//Load translations |
| 22 |
$plugin_dir = basename(dirname(__FILE__)); |
| 23 |
load_plugin_textdomain('autoptimize','wp-content/plugins/'.$plugin_dir.'/localization',$plugin_dir.'/localization'); |
| 24 |
|
| 25 |
//Set up the buffering |
| 26 |
function autoptimize_start_buffering() |
| 27 |
{ |
| 28 |
//Config element |
| 29 |
$conf = autoptimizeConfig::instance(); |
| 30 |
|
| 31 |
//Load our base class |
| 32 |
include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeBase.php'); |
| 33 |
|
| 34 |
//Load extra classes and set some vars |
| 35 |
if($conf->get('autoptimize_html')) |
| 36 |
{ |
| 37 |
include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeHTML.php'); |
| 38 |
@include(WP_PLUGIN_DIR.'/autoptimize/classes/minify-html.php'); |
| 39 |
} |
| 40 |
|
| 41 |
if($conf->get('autoptimize_js')) |
| 42 |
{ |
| 43 |
include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeScripts.php'); |
| 44 |
@include(WP_PLUGIN_DIR.'/autoptimize/classes/jsmin-1.1.1.php'); |
| 45 |
define('CONCATENATE_SCRIPTS',false); |
| 46 |
define('COMPRESS_SCRIPTS',false); |
| 47 |
} |
| 48 |
|
| 49 |
if($conf->get('autoptimize_css')) |
| 50 |
{ |
| 51 |
include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeStyles.php'); |
| 52 |
@include(WP_PLUGIN_DIR.'/autoptimize/classes/minify-css-compressor.php'); |
| 53 |
define('COMPRESS_CSS',false); |
| 54 |
} |
| 55 |
|
| 56 |
//Now, start the real thing! |
| 57 |
ob_start('autoptimize_end_buffering'); |
| 58 |
} |
| 59 |
|
| 60 |
//Action on end - |
| 61 |
function autoptimize_end_buffering($content) |
| 62 |
{ |
| 63 |
//Config element |
| 64 |
$conf = autoptimizeConfig::instance(); |
| 65 |
|
| 66 |
//Choose the classes |
| 67 |
$classes = array(); |
| 68 |
if($conf->get('autoptimize_js')) |
| 69 |
$classes[] = 'autoptimizeScripts'; |
| 70 |
if($conf->get('autoptimize_css')) |
| 71 |
$classes[] = 'autoptimizeStyles'; |
| 72 |
if($conf->get('autoptimize_html')) |
| 73 |
$classes[] = 'autoptimizeHTML'; |
| 74 |
|
| 75 |
//Set some options |
| 76 |
$classoptions = array( |
| 77 |
'autoptimizeScripts' => array( |
| 78 |
'justhead' => $conf->get('autoptimize_js_justhead'), |
| 79 |
'trycatch' => $conf->get('autoptimize_js_trycatch') |
| 80 |
), |
| 81 |
'autoptimizeStyles' => array( |
| 82 |
'justhead' => $conf->get('autoptimize_css_justhead') |
| 83 |
), |
| 84 |
'autoptimizeHTML' => array( |
| 85 |
) |
| 86 |
); |
| 87 |
|
| 88 |
|
| 89 |
//Run the classes |
| 90 |
foreach($classes as $name) |
| 91 |
{ |
| 92 |
$instance = new $name($content); |
| 93 |
if($instance->read($classoptions[$name])) |
| 94 |
{ |
| 95 |
$instance->minify(); |
| 96 |
$instance->cache(); |
| 97 |
$content = $instance->getcontent(); |
| 98 |
} |
| 99 |
unset($instance); |
| 100 |
} |
| 101 |
return $content; |
| 102 |
} |
| 103 |
|
| 104 |
if(autoptimizeCache::cacheavail()) |
| 105 |
{ |
| 106 |
$conf = autoptimizeConfig::instance(); |
| 107 |
if($conf->get('autoptimize_html') || $conf->get('autoptimize_js') || $conf->get('autoptimize_css')) |
| 108 |
{ |
| 109 |
//Hook to wordpress |
| 110 |
add_action('template_redirect','autoptimize_start_buffering',2); |
| 111 |
} |
| 112 |
} |
| 113 |
|