| 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.6 |
| 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 |
//Pre-2.6 compatibility |
| 14 |
if(!defined('WP_PLUGIN_URL')) |
| 15 |
define('WP_PLUGIN_URL',WP_CONTENT_URL.'/plugins'); |
| 16 |
if(!defined('WP_PLUGIN_DIR')) |
| 17 |
define('WP_PLUGIN_DIR',WP_CONTENT_DIR.'/plugins'); |
| 18 |
|
| 19 |
//Load config class |
| 20 |
@include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeConfig.php'); |
| 21 |
|
| 22 |
//Load translations |
| 23 |
$plugin_dir = basename(dirname(__FILE__)); |
| 24 |
load_plugin_textdomain('autoptimize','wp-content/plugins/'.$plugin_dir.'/localization',$plugin_dir.'/localization'); |
| 25 |
|
| 26 |
//Set up the buffering |
| 27 |
function autoptimize_start_buffering() |
| 28 |
{ |
| 29 |
//Config element |
| 30 |
$conf = autoptimizeConfig::instance(); |
| 31 |
|
| 32 |
//Load our always-on classes |
| 33 |
@include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeBase.php'); |
| 34 |
@include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeCache.php'); |
| 35 |
|
| 36 |
//Load extra classes and set some vars |
| 37 |
if($conf->get('autoptimize_html')) |
| 38 |
{ |
| 39 |
@include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeHTML.php'); |
| 40 |
@include(WP_PLUGIN_DIR.'/autoptimize/classes/minify-html.php'); |
| 41 |
} |
| 42 |
|
| 43 |
if($conf->get('autoptimize_js')) |
| 44 |
{ |
| 45 |
@include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeScripts.php'); |
| 46 |
@include(WP_PLUGIN_DIR.'/autoptimize/classes/jsmin-1.1.1.php'); |
| 47 |
define('CONCATENATE_SCRIPTS',false); |
| 48 |
define('COMPRESS_SCRIPTS',false); |
| 49 |
} |
| 50 |
|
| 51 |
if($conf->get('autoptimize_css')) |
| 52 |
{ |
| 53 |
@include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeStyles.php'); |
| 54 |
@include(WP_PLUGIN_DIR.'/autoptimize/classes/minify-css-compressor.php'); |
| 55 |
define('COMPRESS_CSS',false); |
| 56 |
} |
| 57 |
|
| 58 |
//Now, start the real thing! |
| 59 |
ob_start('autoptimize_end_buffering'); |
| 60 |
} |
| 61 |
|
| 62 |
//Action on end - |
| 63 |
function autoptimize_end_buffering($content) |
| 64 |
{ |
| 65 |
//Config element |
| 66 |
$conf = autoptimizeConfig::instance(); |
| 67 |
|
| 68 |
//Choose the classes |
| 69 |
$classes = array(); |
| 70 |
if($conf->get('autoptimize_js')) |
| 71 |
$classes[] = 'autoptimizeScripts'; |
| 72 |
if($conf->get('autoptimize_css')) |
| 73 |
$classes[] = 'autoptimizeStyles'; |
| 74 |
if($conf->get('autoptimize_html')) |
| 75 |
$classes[] = 'autoptimizeHTML'; |
| 76 |
|
| 77 |
//Set some options |
| 78 |
$classoptions = array( |
| 79 |
'autoptimizeScripts' => array( |
| 80 |
'justhead' => $conf->get('autoptimize_js_justhead') |
| 81 |
), |
| 82 |
'autoptimizeStyles' => array( |
| 83 |
'justhead' => $conf->get('autoptimize_css_justhead') |
| 84 |
), |
| 85 |
'autoptimizeHTML' => array( |
| 86 |
'justhead' => 0 |
| 87 |
) |
| 88 |
); |
| 89 |
|
| 90 |
|
| 91 |
//Run the classes |
| 92 |
foreach($classes as $name) |
| 93 |
{ |
| 94 |
$instance = new $name($content); |
| 95 |
if($instance->read($classoptions[$name]['justhead'])) |
| 96 |
{ |
| 97 |
$instance->minify(); |
| 98 |
$instance->cache(); |
| 99 |
$content = $instance->getcontent(); |
| 100 |
} |
| 101 |
unset($instance); |
| 102 |
} |
| 103 |
return $content; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
$conf = autoptimizeConfig::instance(); |
| 108 |
if($conf->get('autoptimize_html') || $conf->get('autoptimize_js') || $conf->get('autoptimize_css')) |
| 109 |
{ |
| 110 |
//Hook to wordpress |
| 111 |
add_action('template_redirect','autoptimize_start_buffering',2); |
| 112 |
} |
| 113 |
|