| @@ -1,96 +1,146 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /* |
| 3 | 3 | Plugin Name: Autoptimize |
| 4 | -Plugin URI: https://autoptimize.com/ | |
| 5 | -Description: Optimize your website's performance: JS, CSS, HTML, images, Google Fonts and more! | |
| 6 | -Version: 2.4.2 | |
| 7 | -Author: Frank Goossens (futtta) | |
| 8 | -Author URI: https://autoptimize.com/ | |
| 9 | -Text Domain: 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: 1.4 | |
| 7 | +Author: Emilio López | |
| 8 | +Author URI: http://www.turleando.com.ar/ | |
| 10 | 9 | Released under the GNU General Public License (GPL) |
| 11 | 10 | http://www.gnu.org/licenses/gpl.txt |
| 12 | 11 | */ |
| 13 | 12 | |
| 14 | -/** | |
| 15 | - * Autoptimize main plugin file. | |
| 16 | - */ | |
| 13 | +//Load config and cache class | |
| 14 | +include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeConfig.php'); | |
| 15 | +include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeCache.php'); | |
| 17 | 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 | +define('AUTOPTIMIZE_CACHE_DELAY',true); | |
| 18 | 21 | |
| 19 | -if ( ! defined( 'ABSPATH' ) ) { | |
| 20 | - exit; | |
| 21 | -} | |
| 22 | +//Initialize the cache at least once | |
| 23 | +$conf = autoptimizeConfig::instance(); | |
| 22 | 24 | |
| 23 | -define( 'AUTOPTIMIZE_PLUGIN_VERSION', '2.4.2' ); | |
| 25 | +//Do we gzip when caching? | |
| 26 | +define('AUTOPTIMIZE_CACHE_NOGZIP',(bool) $conf->get('autoptimize_cache_nogzip')); | |
| 24 | 27 | |
| 25 | -// plugin_dir_path() returns the trailing slash! | |
| 26 | -define( 'AUTOPTIMIZE_PLUGIN_DIR', plugin_dir_path( __FILE__ ) ); | |
| 27 | -define( 'AUTOPTIMIZE_PLUGIN_FILE', __FILE__ ); | |
| 28 | +//Load translations | |
| 29 | +$plugin_dir = basename(dirname(__FILE__)); | |
| 30 | +load_plugin_textdomain('autoptimize','wp-content/plugins/'.$plugin_dir.'/localization',$plugin_dir.'/localization'); | |
| 28 | 31 | |
| 29 | -// Bail early if attempting to run on non-supported php versions. | |
| 30 | -if ( version_compare( PHP_VERSION, '5.3', '<' ) ) { | |
| 31 | - function autoptimize_incompatible_admin_notice() { | |
| 32 | - echo '<div class="error"><p>' . __( 'Autoptimize requires PHP 5.3 (or higher) to function properly. Please upgrade PHP. The Plugin has been auto-deactivated.', 'autoptimize' ) . '</p></div>'; | |
| 33 | - if ( isset( $_GET['activate'] ) ) { | |
| 34 | - unset( $_GET['activate'] ); | |
| 35 | - } | |
| 36 | - } | |
| 37 | - function autoptimize_deactivate_self() { | |
| 38 | - deactivate_plugins( plugin_basename( AUTOPTIMIZE_PLUGIN_FILE ) ); | |
| 39 | - } | |
| 40 | - add_action( 'admin_notices', 'autoptimize_incompatible_admin_notice' ); | |
| 41 | - add_action( 'admin_init', 'autoptimize_deactivate_self' ); | |
| 42 | - return; | |
| 32 | +//Set up the buffering | |
| 33 | +function autoptimize_start_buffering() | |
| 34 | +{ | |
| 35 | + //Config element | |
| 36 | + $conf = autoptimizeConfig::instance(); | |
| 37 | + | |
| 38 | + //Load our base class | |
| 39 | + include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeBase.php'); | |
| 40 | + | |
| 41 | + //Load extra classes and set some vars | |
| 42 | + if($conf->get('autoptimize_html')) | |
| 43 | + { | |
| 44 | + include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeHTML.php'); | |
| 45 | + @include(WP_PLUGIN_DIR.'/autoptimize/classes/minify-html.php'); | |
| 46 | + } | |
| 47 | + | |
| 48 | + if($conf->get('autoptimize_js')) | |
| 49 | + { | |
| 50 | + include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeScripts.php'); | |
| 51 | + @include(WP_PLUGIN_DIR.'/autoptimize/classes/jsmin-1.1.1.php'); | |
| 52 | + define('CONCATENATE_SCRIPTS',false); | |
| 53 | + define('COMPRESS_SCRIPTS',false); | |
| 54 | + } | |
| 55 | + | |
| 56 | + if($conf->get('autoptimize_css')) | |
| 57 | + { | |
| 58 | + include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeStyles.php'); | |
| 59 | + @include(WP_PLUGIN_DIR.'/autoptimize/classes/minify-css-compressor.php'); | |
| 60 | + define('COMPRESS_CSS',false); | |
| 61 | + } | |
| 62 | + | |
| 63 | + if($conf->get('autoptimize_js_yui') || $conf->get('autoptimize_css_yui')) | |
| 64 | + { | |
| 65 | + include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeYUI.php'); | |
| 66 | + } | |
| 67 | + | |
| 68 | + if($conf->get('autoptimize_cdn_js') || $conf->get('autoptimize_cdn_css')) | |
| 69 | + { | |
| 70 | + include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeCDN.php'); | |
| 71 | + } | |
| 72 | + | |
| 73 | + //Now, start the real thing! | |
| 74 | + ob_start('autoptimize_end_buffering'); | |
| 43 | 75 | } |
| 44 | 76 | |
| 45 | -function autoptimize_autoload( $class_name ) { | |
| 46 | - if ( in_array( $class_name, array( 'Minify_HTML', 'JSMin' ) ) ) { | |
| 47 | - $file = strtolower( $class_name ); | |
| 48 | - $file = str_replace( '_', '-', $file ); | |
| 49 | - $path = dirname( __FILE__ ) . '/classes/external/php/'; | |
| 50 | - $filepath = $path . $file . '.php'; | |
| 51 | - } elseif ( false !== strpos( $class_name, 'Autoptimize\\tubalmartin\\CssMin' ) ) { | |
| 52 | - $file = str_replace( 'Autoptimize\\tubalmartin\\CssMin\\', '', $class_name ); | |
| 53 | - $path = dirname( __FILE__ ) . '/classes/external/php/yui-php-cssmin-bundled/'; | |
| 54 | - $filepath = $path . $file . '.php'; | |
| 55 | - } elseif ( 'autoptimize' === substr( $class_name, 0, 11 ) ) { | |
| 56 | - // One of our "old" classes. | |
| 57 | - $file = $class_name; | |
| 58 | - $path = dirname( __FILE__ ) . '/classes/'; | |
| 59 | - $filepath = $path . $file . '.php'; | |
| 60 | - } elseif ( 'PAnD' === $class_name ) { | |
| 61 | - $file = 'persist-admin-notices-dismissal'; | |
| 62 | - $path = dirname( __FILE__ ) . '/classes/external/php/persist-admin-notices-dismissal/'; | |
| 63 | - $filepath = $path . $file . '.php'; | |
| 64 | - } | |
| 65 | - | |
| 66 | - // If we didn't match one of our rules, bail! | |
| 67 | - if ( ! isset( $filepath ) ) { | |
| 68 | - return; | |
| 69 | - } | |
| 70 | - | |
| 71 | - require $filepath; | |
| 77 | +//Action on end - | |
| 78 | +function autoptimize_end_buffering($content) | |
| 79 | +{ | |
| 80 | + //Config element | |
| 81 | + $conf = autoptimizeConfig::instance(); | |
| 82 | + | |
| 83 | + //Choose the classes | |
| 84 | + $classes = array(); | |
| 85 | + if($conf->get('autoptimize_js')) | |
| 86 | + $classes[] = 'autoptimizeScripts'; | |
| 87 | + if($conf->get('autoptimize_css')) | |
| 88 | + $classes[] = 'autoptimizeStyles'; | |
| 89 | + if($conf->get('autoptimize_cdn_js') || $conf->get('autoptimize_cdn_css')) | |
| 90 | + $classes[] = 'autoptimizeCDN'; | |
| 91 | + if($conf->get('autoptimize_html')) | |
| 92 | + $classes[] = 'autoptimizeHTML'; | |
| 93 | + | |
| 94 | + //Set some options | |
| 95 | + $classoptions = array( | |
| 96 | + 'autoptimizeScripts' => array( | |
| 97 | + 'justhead' => $conf->get('autoptimize_js_justhead'), | |
| 98 | + 'trycatch' => $conf->get('autoptimize_js_trycatch'), | |
| 99 | + 'yui' => $conf->get('autoptimize_js_yui'), | |
| 100 | + ), | |
| 101 | + 'autoptimizeStyles' => array( | |
| 102 | + 'justhead' => $conf->get('autoptimize_css_justhead'), | |
| 103 | + 'datauris' => $conf->get('autoptimize_css_datauris'), | |
| 104 | + 'yui' => $conf->get('autoptimize_css_yui'), | |
| 105 | + ), | |
| 106 | + 'autoptimizeCDN' => array( | |
| 107 | + 'js' => $conf->get('autoptimize_cdn_js'), | |
| 108 | + 'jsurl' => $conf->get('autoptimize_cdn_js_url'), | |
| 109 | + 'css' => $conf->get('autoptimize_cdn_css'), | |
| 110 | + 'cssurl' => $conf->get('autoptimize_cdn_css_url'), | |
| 111 | + 'img' => $conf->get('autoptimize_cdn_img'), | |
| 112 | + 'imgurl' => $conf->get('autoptimize_cdn_img_url') | |
| 113 | + ), | |
| 114 | + 'autoptimizeHTML' => array( | |
| 115 | + 'keepcomments' => $conf->get('autoptimize_html_keepcomments') | |
| 116 | + ) | |
| 117 | + ); | |
| 118 | + | |
| 119 | + | |
| 120 | + //Run the classes | |
| 121 | + foreach($classes as $name) | |
| 122 | + { | |
| 123 | + $instance = new $name($content); | |
| 124 | + if($instance->read($classoptions[$name])) | |
| 125 | + { | |
| 126 | + $instance->minify(); | |
| 127 | + $instance->cache(); | |
| 128 | + $content = $instance->getcontent(); | |
| 129 | + } | |
| 130 | + unset($instance); | |
| 131 | + } | |
| 132 | + return $content; | |
| 72 | 133 | } |
| 73 | 134 | |
| 74 | -spl_autoload_register( 'autoptimize_autoload' ); | |
| 75 | - | |
| 76 | -// Load WP CLI command(s) on demand. | |
| 77 | -if ( defined( 'WP_CLI' ) && WP_CLI ) { | |
| 78 | - require AUTOPTIMIZE_PLUGIN_DIR . 'classes/autoptimizeCLI.php'; | |
| 135 | +if(autoptimizeCache::cacheavail()) | |
| 136 | +{ | |
| 137 | + $conf = autoptimizeConfig::instance(); | |
| 138 | + if($conf->get('autoptimize_html') || $conf->get('autoptimize_js') || $conf->get('autoptimize_css') || $conf->get('autoptimize_cdn_js') || $conf->get('autoptimize_cdn_css')) | |
| 139 | + { | |
| 140 | + //Hook to wordpress | |
| 141 | + add_action('template_redirect','autoptimize_start_buffering',2); | |
| 142 | + } | |
| 79 | 143 | } |
| 80 | 144 | |
| 81 | -/** | |
| 82 | - * Retrieve the instance of the main plugin class. | |
| 83 | - * | |
| 84 | - * @return autoptimizeMain | |
| 85 | - */ | |
| 86 | -function autoptimize() { | |
| 87 | - static $plugin = null; | |
| 88 | - | |
| 89 | - if ( null === $plugin ) { | |
| 90 | - $plugin = new autoptimizeMain( AUTOPTIMIZE_PLUGIN_VERSION, AUTOPTIMIZE_PLUGIN_FILE ); | |
| 91 | - } | |
| 92 | - | |
| 93 | - return $plugin; | |
| 94 | -} | |
| 95 | - | |
| 96 | -autoptimize()->run(); | |
| 145 | +//Do not pollute other plugins | |
| 146 | +unset($conf); | |