| @@ -1,96 +1,153 @@ | ||
| 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.6.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/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 | +*/ | |
| 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',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)); | |
| 18 | 23 | |
| 19 | -if ( ! defined( 'ABSPATH' ) ) { | |
| 20 | - exit; | |
| 21 | -} | |
| 24 | +// Initialize the cache at least once | |
| 25 | +$conf = autoptimizeConfig::instance(); | |
| 22 | 26 | |
| 23 | -define( 'AUTOPTIMIZE_PLUGIN_VERSION', '2.6.2' ); | |
| 27 | +// Do we gzip when caching? | |
| 28 | +define('AUTOPTIMIZE_CACHE_NOGZIP',(bool) $conf->get('autoptimize_cache_nogzip')); | |
| 24 | 29 | |
| 25 | -// plugin_dir_path() returns the trailing slash! | |
| 26 | -define( 'AUTOPTIMIZE_PLUGIN_DIR', plugin_dir_path( __FILE__ ) ); | |
| 27 | -define( 'AUTOPTIMIZE_PLUGIN_FILE', __FILE__ ); | |
| 30 | +// Load translations | |
| 31 | +$plugin_dir = basename(dirname(__FILE__)); | |
| 32 | +load_plugin_textdomain('autoptimize','wp-content/plugins/'.$plugin_dir.'/localization',$plugin_dir.'/localization'); | |
| 28 | 33 | |
| 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; | |
| 43 | -} | |
| 34 | +// Set up the buffering | |
| 35 | +function autoptimize_start_buffering() | |
| 36 | +{ | |
| 37 | + // if (!is_user_logged_in()) { | |
| 44 | 38 | |
| 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 | - } | |
| 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'); | |
| 65 | 79 | |
| 66 | - // If we didn't match one of our rules, bail! | |
| 67 | - if ( ! isset( $filepath ) ) { | |
| 68 | - return; | |
| 69 | - } | |
| 70 | - | |
| 71 | - require $filepath; | |
| 80 | + // } | |
| 72 | 81 | } |
| 73 | 82 | |
| 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'; | |
| 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; | |
| 79 | 140 | } |
| 80 | 141 | |
| 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; | |
| 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 | + } | |
| 94 | 150 | } |
| 95 | 151 | |
| 96 | -autoptimize()->run(); | |
| 152 | +// Do not pollute other plugins | |
| 153 | +unset($conf); | |