| @@ -1,96 +1,269 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * Plugin Name: Autoptimize | |
| 4 | - * Plugin URI: https://autoptimize.com/ | |
| 5 | - * Description: Makes your site faster by optimizing CSS, JS, Images, Google fonts and more. | |
| 6 | - * Version: 2.7.2 | |
| 7 | - * Author: Frank Goossens (futtta) | |
| 8 | - * Author URI: https://autoptimize.com/ | |
| 9 | - * Text Domain: autoptimize | |
| 10 | - * Released under the GNU General Public License (GPL) | |
| 11 | - * http://www.gnu.org/licenses/gpl.txt | |
| 12 | - */ | |
| 2 | +/* | |
| 3 | +Plugin Name: Autoptimize | |
| 4 | +Plugin URI: http://blog.futtta.be/autoptimize | |
| 5 | +Description: Optimizes your website, concatenating the CSS and JavaScript code, and compressing it. | |
| 6 | +Version: 1.8.2 | |
| 7 | +Author: Frank Goossens (futtta) | |
| 8 | +Author URI: http://blog.futtta.be/ | |
| 9 | +Domain Path: localization/ | |
| 10 | +Text Domain: autoptimize | |
| 11 | +Released under the GNU General Public License (GPL) | |
| 12 | +http://www.gnu.org/licenses/gpl.txt | |
| 13 | +*/ | |
| 13 | 14 | |
| 14 | -/** | |
| 15 | - * Autoptimize main plugin file. | |
| 16 | - */ | |
| 15 | +if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly | |
| 17 | 16 | |
| 17 | +// Load config class | |
| 18 | +include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeConfig.php'); | |
| 18 | 19 | |
| 19 | -if ( ! defined( 'ABSPATH' ) ) { | |
| 20 | - exit; | |
| 20 | +// Do we gzip when caching (needed early to load autoptimizeCache.php) | |
| 21 | +define('AUTOPTIMIZE_CACHE_NOGZIP',(bool) get_option('autoptimize_cache_nogzip')); | |
| 22 | + | |
| 23 | +// Load cache class | |
| 24 | +include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeCache.php'); | |
| 25 | + | |
| 26 | +// Plugin dir constants (plugin url's defined later to accomodate domain mapped sites) | |
| 27 | +define('AUTOPTIMIZE_CACHE_DIR',WP_CONTENT_DIR.'/cache/autoptimize/'); | |
| 28 | +define('AUTOPTIMIZE_CACHE_DELAY',true); | |
| 29 | +define('WP_ROOT_DIR',str_replace('/wp-content','',WP_CONTENT_DIR)); | |
| 30 | + | |
| 31 | +// Initialize the cache at least once | |
| 32 | +$conf = autoptimizeConfig::instance(); | |
| 33 | + | |
| 34 | +/* Check if we're updating, in which case we might need to do stuff and flush the cache | |
| 35 | +to avoid old versions of aggregated files lingering around */ | |
| 36 | + | |
| 37 | +$autoptimize_version="1.8.2"; | |
| 38 | +$autoptimize_db_version=get_option('autoptimize_version','none'); | |
| 39 | + | |
| 40 | +if ($autoptimize_db_version !== $autoptimize_version) { | |
| 41 | + if ($autoptimize_db_version==="none") { | |
| 42 | + add_action('admin_notices', 'autoptimize_install_config_notice'); | |
| 43 | + } else { | |
| 44 | + $autoptimize_major_version=substr($autoptimize_db_version,0,3); | |
| 45 | + switch($autoptimize_major_version) { | |
| 46 | + case "1.6": | |
| 47 | + // from back in the days when I did not yet consider multisite | |
| 48 | + // if user was on version 1.6.x, force advanced options to be shown by default | |
| 49 | + update_option('autoptimize_show_adv','1'); | |
| 50 | + | |
| 51 | + // and remove old options | |
| 52 | + $to_delete_options=array("autoptimize_cdn_css","autoptimize_cdn_css_url","autoptimize_cdn_js","autoptimize_cdn_js_url","autoptimize_cdn_img","autoptimize_cdn_img_url","autoptimize_css_yui","autoptimize_js_yui"); | |
| 53 | + foreach ($to_delete_options as $del_opt) { | |
| 54 | + delete_option( $del_opt ); | |
| 55 | + } | |
| 56 | + | |
| 57 | + // and notify user to check result | |
| 58 | + add_action('admin_notices', 'autoptimize_update_config_notice'); | |
| 59 | + case "1.7": | |
| 60 | + // force 3.8 dashicons in CSS exclude options when upgrading from 1.7 to 1.8 | |
| 61 | + if ( !is_multisite() ) { | |
| 62 | + $css_exclude = get_option('autoptimize_css_exclude'); | |
| 63 | + if (empty($css_exclude)) { | |
| 64 | + $css_exclude = "admin-bar.min.css, dashicons.min.css"; | |
| 65 | + } else if (strpos($css_exclude,"dashicons.min.css")===false) { | |
| 66 | + $css_exclude .= ", dashicons.min.css"; | |
| 67 | + } | |
| 68 | + update_option('autoptimize_css_exclude',$css_exclude); | |
| 69 | + } else { | |
| 70 | + global $wpdb; | |
| 71 | + $blog_ids = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" ); | |
| 72 | + $original_blog_id = get_current_blog_id(); | |
| 73 | + foreach ( $blog_ids as $blog_id ) { | |
| 74 | + switch_to_blog( $blog_id ); | |
| 75 | + $css_exclude = get_option('autoptimize_css_exclude'); | |
| 76 | + if (empty($css_exclude)) { | |
| 77 | + $css_exclude = "admin-bar.min.css, dashicons.min.css"; | |
| 78 | + } else if (strpos($css_exclude,"dashicons.min.css")===false) { | |
| 79 | + $css_exclude .= ", dashicons.min.css"; | |
| 80 | + } | |
| 81 | + update_option('autoptimize_css_exclude',$css_exclude); | |
| 82 | + } | |
| 83 | + switch_to_blog( $original_blog_id ); | |
| 84 | + } | |
| 85 | + } | |
| 86 | + } | |
| 87 | + | |
| 88 | + autoptimizeCache::clearall(); | |
| 89 | + update_option('autoptimize_version',$autoptimize_version); | |
| 90 | + $autoptimize_db_version=$autoptimize_version; | |
| 21 | 91 | } |
| 22 | 92 | |
| 23 | -define( 'AUTOPTIMIZE_PLUGIN_VERSION', '2.7.2' ); | |
| 93 | +// Load translations | |
| 94 | +$plugin_dir = basename(dirname(__FILE__)); | |
| 95 | +load_plugin_textdomain('autoptimize','wp-content/plugins/'.$plugin_dir.'/localization',$plugin_dir.'/localization'); | |
| 24 | 96 | |
| 25 | -// plugin_dir_path() returns the trailing slash! | |
| 26 | -define( 'AUTOPTIMIZE_PLUGIN_DIR', plugin_dir_path( __FILE__ ) ); | |
| 27 | -define( 'AUTOPTIMIZE_PLUGIN_FILE', __FILE__ ); | |
| 97 | +function autoptimize_uninstall(){ | |
| 98 | + autoptimizeCache::clearall(); | |
| 99 | + | |
| 100 | + $delete_options=array("autoptimize_cache_clean", "autoptimize_cache_nogzip", "autoptimize_css", "autoptimize_css_datauris", "autoptimize_css_justhead", "autoptimize_css_defer", "autoptimize_css_inline", "autoptimize_css_exclude", "autoptimize_html", "autoptimize_html_keepcomments", "autoptimize_js", "autoptimize_js_exclude", "autoptimize_js_forcehead", "autoptimize_js_justhead", "autoptimize_js_trycatch", "autoptimize_version", "autoptimize_show_adv", "autoptimize_cdn_url"); | |
| 101 | + | |
| 102 | + if ( !is_multisite() ) { | |
| 103 | + foreach ($delete_options as $del_opt) { delete_option( $del_opt ); } | |
| 104 | + } else { | |
| 105 | + global $wpdb; | |
| 106 | + $blog_ids = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" ); | |
| 107 | + $original_blog_id = get_current_blog_id(); | |
| 108 | + foreach ( $blog_ids as $blog_id ) | |
| 109 | + { | |
| 110 | + switch_to_blog( $blog_id ); | |
| 111 | + foreach ($delete_options as $del_opt) { delete_option( $del_opt ); } | |
| 112 | + } | |
| 113 | + switch_to_blog( $original_blog_id ); | |
| 114 | + } | |
| 115 | +} | |
| 28 | 116 | |
| 29 | -// Bail early if attempting to run on non-supported php versions. | |
| 30 | -if ( version_compare( PHP_VERSION, '5.6', '<' ) ) { | |
| 31 | - function autoptimize_incompatible_admin_notice() { | |
| 32 | - echo '<div class="error"><p>' . __( 'Autoptimize requires PHP 5.6 (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; | |
| 117 | +function autoptimize_install_config_notice() { | |
| 118 | + echo '<div class="updated"><p>'; | |
| 119 | + _e('Thank you for installing and activating Autoptimize. Please configure it under "Settings" -> "Autoptimize" to start improving your site\'s performance.', 'autoptimize' ); | |
| 120 | + echo '</p></div>'; | |
| 43 | 121 | } |
| 44 | 122 | |
| 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 | - } | |
| 123 | +function autoptimize_update_config_notice() { | |
| 124 | + echo '<div class="updated"><p>'; | |
| 125 | + _e('Autoptimize has just been updated. Please <strong>test your site now</strong> and adapt Autoptimize config if needed.', 'autoptimize' ); | |
| 126 | + echo '</p></div>'; | |
| 127 | + } | |
| 65 | 128 | |
| 66 | - // If we didn't match one of our rules, bail! | |
| 67 | - if ( ! isset( $filepath ) ) { | |
| 68 | - return; | |
| 69 | - } | |
| 129 | +// Set up the buffering | |
| 130 | +function autoptimize_start_buffering() { | |
| 131 | + $ao_noptimize = false; | |
| 70 | 132 | |
| 71 | - require $filepath; | |
| 72 | -} | |
| 133 | + // filter you can use to block autoptimization on your own terms | |
| 134 | + $ao_noptimize = (bool) apply_filters( 'autoptimize_filter_noptimize', $ao_noptimize ); | |
| 73 | 135 | |
| 74 | -spl_autoload_register( 'autoptimize_autoload' ); | |
| 136 | + // noptimize in qs to get non-optimized page for debugging | |
| 137 | + if (array_key_exists("ao_noptimize",$_GET)) { | |
| 138 | + if ($_GET["ao_noptimize"]==="1") { | |
| 139 | + $ao_noptimize = true; | |
| 140 | + } | |
| 141 | + } | |
| 75 | 142 | |
| 76 | -// Load WP CLI command(s) on demand. | |
| 77 | -if ( defined( 'WP_CLI' ) && WP_CLI ) { | |
| 78 | - require AUTOPTIMIZE_PLUGIN_DIR . 'classes/autoptimizeCLI.php'; | |
| 143 | + if (!is_feed() && !$ao_noptimize ) { | |
| 144 | + | |
| 145 | + // Config element | |
| 146 | + $conf = autoptimizeConfig::instance(); | |
| 147 | + | |
| 148 | + // Load our base class | |
| 149 | + include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeBase.php'); | |
| 150 | + | |
| 151 | + // Load extra classes and set some vars | |
| 152 | + if($conf->get('autoptimize_html')) { | |
| 153 | + include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeHTML.php'); | |
| 154 | + // BUG: new minify-html does not support keeping HTML comments, skipping for now | |
| 155 | + // if (defined('AUTOPTIMIZE_LEGACY_MINIFIERS')) { | |
| 156 | + @include(WP_PLUGIN_DIR.'/autoptimize/classes/external/php/minify-html.php'); | |
| 157 | + // } else { | |
| 158 | + // @include(WP_PLUGIN_DIR.'/autoptimize/classes/external/php/minify-2.1.7-html.php'); | |
| 159 | + // } | |
| 160 | + } | |
| 161 | + | |
| 162 | + if($conf->get('autoptimize_js')) { | |
| 163 | + include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeScripts.php'); | |
| 164 | + if (!class_exists('JSMin')) { | |
| 165 | + if (defined('AUTOPTIMIZE_LEGACY_MINIFIERS')) { | |
| 166 | + @include(WP_PLUGIN_DIR.'/autoptimize/classes/external/php/jsmin-1.1.1.php'); | |
| 167 | + } else { | |
| 168 | + @include(WP_PLUGIN_DIR.'/autoptimize/classes/external/php/minify-2.1.7-jsmin.php'); | |
| 169 | + } | |
| 170 | + } | |
| 171 | + define('CONCATENATE_SCRIPTS',false); | |
| 172 | + define('COMPRESS_SCRIPTS',false); | |
| 173 | + } | |
| 174 | + | |
| 175 | + if($conf->get('autoptimize_css')) { | |
| 176 | + include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeStyles.php'); | |
| 177 | + if (defined('AUTOPTIMIZE_LEGACY_MINIFIERS')) { | |
| 178 | + if (!class_exists('Minify_CSS_Compressor')) { | |
| 179 | + @include(WP_PLUGIN_DIR.'/autoptimize/classes/external/php/minify-css-compressor.php'); | |
| 180 | + } | |
| 181 | + } else { | |
| 182 | + if (!class_exists('CSSmin')) { | |
| 183 | + @include(WP_PLUGIN_DIR.'/autoptimize/classes/external/php/yui-php-cssmin-2.4.8-2_fgo.php'); | |
| 184 | + } | |
| 185 | + } | |
| 186 | + define('COMPRESS_CSS',false); | |
| 187 | + } | |
| 188 | + | |
| 189 | + // Now, start the real thing! | |
| 190 | + ob_start('autoptimize_end_buffering'); | |
| 191 | + } | |
| 79 | 192 | } |
| 80 | 193 | |
| 81 | -/** | |
| 82 | - * Retrieve the instance of the main plugin class. | |
| 83 | - * | |
| 84 | - * @return autoptimizeMain | |
| 85 | - */ | |
| 86 | -function autoptimize() { | |
| 87 | - static $plugin = null; | |
| 194 | +// Action on end, this is where the magic happens | |
| 195 | +function autoptimize_end_buffering($content) { | |
| 196 | + if ( stripos($content,"<html") === false || stripos($content,"<xsl:stylesheet") !== false ) { return $content;} | |
| 88 | 197 | |
| 89 | - if ( null === $plugin ) { | |
| 90 | - $plugin = new autoptimizeMain( AUTOPTIMIZE_PLUGIN_VERSION, AUTOPTIMIZE_PLUGIN_FILE ); | |
| 91 | - } | |
| 198 | + // load URL constants as late as possible to allow domain mapper to kick in | |
| 199 | + if (function_exists("domain_mapping_siteurl")) { | |
| 200 | + define('AUTOPTIMIZE_WP_SITE_URL',domain_mapping_siteurl(get_current_blog_id())); | |
| 201 | + define('AUTOPTIMIZE_WP_CONTENT_URL',str_replace(get_original_url(AUTOPTIMIZE_WP_SITE_URL),AUTOPTIMIZE_WP_SITE_URL,content_url())); | |
| 202 | + } else { | |
| 203 | + define('AUTOPTIMIZE_WP_SITE_URL',site_url()); | |
| 204 | + define('AUTOPTIMIZE_WP_CONTENT_URL',content_url()); | |
| 205 | + } | |
| 206 | + define('AUTOPTIMIZE_CACHE_URL',AUTOPTIMIZE_WP_CONTENT_URL.'/cache/autoptimize/'); | |
| 207 | + define('AUTOPTIMIZE_WP_ROOT_URL',str_replace('/wp-content','',AUTOPTIMIZE_WP_CONTENT_URL)); | |
| 92 | 208 | |
| 93 | - return $plugin; | |
| 209 | + // Config element | |
| 210 | + $conf = autoptimizeConfig::instance(); | |
| 211 | + | |
| 212 | + // Choose the classes | |
| 213 | + $classes = array(); | |
| 214 | + if($conf->get('autoptimize_js')) | |
| 215 | + $classes[] = 'autoptimizeScripts'; | |
| 216 | + if($conf->get('autoptimize_css')) | |
| 217 | + $classes[] = 'autoptimizeStyles'; | |
| 218 | + if($conf->get('autoptimize_html')) | |
| 219 | + $classes[] = 'autoptimizeHTML'; | |
| 220 | + | |
| 221 | + // Set some options | |
| 222 | + $classoptions = array( | |
| 223 | + 'autoptimizeScripts' => array( | |
| 224 | + 'justhead' => $conf->get('autoptimize_js_justhead'), | |
| 225 | + 'forcehead' => $conf->get('autoptimize_js_forcehead'), | |
| 226 | + 'trycatch' => $conf->get('autoptimize_js_trycatch'), | |
| 227 | + 'js_exclude' => $conf->get('autoptimize_js_exclude'), | |
| 228 | + 'cdn_url' => $conf->get('autoptimize_cdn_url') | |
| 229 | + ), | |
| 230 | + 'autoptimizeStyles' => array( | |
| 231 | + 'justhead' => $conf->get('autoptimize_css_justhead'), | |
| 232 | + 'datauris' => $conf->get('autoptimize_css_datauris'), | |
| 233 | + 'defer' => $conf->get('autoptimize_css_defer'), | |
| 234 | + 'inline' => $conf->get('autoptimize_css_inline'), | |
| 235 | + 'css_exclude' => $conf->get('autoptimize_css_exclude'), | |
| 236 | + 'cdn_url' => $conf->get('autoptimize_cdn_url') | |
| 237 | + ), | |
| 238 | + 'autoptimizeHTML' => array( | |
| 239 | + 'keepcomments' => $conf->get('autoptimize_html_keepcomments') | |
| 240 | + ) | |
| 241 | + ); | |
| 242 | + | |
| 243 | + | |
| 244 | + // Run the classes | |
| 245 | + foreach($classes as $name) { | |
| 246 | + $instance = new $name($content); | |
| 247 | + if($instance->read($classoptions[$name])) | |
| 248 | + { | |
| 249 | + $instance->minify(); | |
| 250 | + $instance->cache(); | |
| 251 | + $content = $instance->getcontent(); | |
| 252 | + } | |
| 253 | + unset($instance); | |
| 254 | + } | |
| 255 | + return $content; | |
| 94 | 256 | } |
| 95 | 257 | |
| 96 | -autoptimize()->run(); | |
| 258 | +if(autoptimizeCache::cacheavail()) { | |
| 259 | + $conf = autoptimizeConfig::instance(); | |
| 260 | + if( $conf->get('autoptimize_html') || $conf->get('autoptimize_js') || $conf->get('autoptimize_css') || $conf->get('autoptimize_cdn_js') || $conf->get('autoptimize_cdn_css')) { | |
| 261 | + // Hook to wordpress | |
| 262 | + add_action('template_redirect','autoptimize_start_buffering',2); | |
| 263 | + } | |
| 264 | +} | |
| 265 | + | |
| 266 | +register_uninstall_hook(__FILE__, "autoptimize_uninstall"); | |
| 267 | + | |
| 268 | +// Do not pollute other plugins | |
| 269 | +unset($conf); | |