PluginProbe
Autoptimize / 1.9.2
Autoptimize v1.9.2
2.2.2 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 All 107 releases
autoptimize / autoptimize.php

autoptimize.php in Autoptimize 1.9.2, at autoptimize.php

323 lines 12.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
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.9.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 */
14
15 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
16
17 // Load config class
18 include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeConfig.php');
19
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 // wp-content dir, dirname of AO cache dir and AO-prefix can be overridden in wp-config.php
27 if (!defined('AUTOPTIMIZE_CACHE_CHILD_DIR')) { define('AUTOPTIMIZE_CACHE_CHILD_DIR','/cache/autoptimize/'); }
28 if (!defined('AUTOPTIMIZE_WP_CONTENT_NAME')) { define('AUTOPTIMIZE_WP_CONTENT_NAME','/wp-content'); }
29 if (!defined('AUTOPTIMIZE_CACHEFILE_PREFIX')) { define('AUTOPTIMIZE_CACHEFILE_PREFIX', 'autoptimize_'); }
30
31 // Plugin dir constants (plugin url's defined later to accomodate domain mapped sites)
32 if (is_multisite()) {
33 $blog_id = get_current_blog_id();
34 define('AUTOPTIMIZE_CACHE_DIR' , WP_CONTENT_DIR.AUTOPTIMIZE_CACHE_CHILD_DIR.$blog_id.'/' );
35 } else {
36 define('AUTOPTIMIZE_CACHE_DIR',WP_CONTENT_DIR.AUTOPTIMIZE_CACHE_CHILD_DIR);
37 }
38 define('AUTOPTIMIZE_CACHE_DELAY',true);
39 define('WP_ROOT_DIR',str_replace(AUTOPTIMIZE_WP_CONTENT_NAME,'',WP_CONTENT_DIR));
40
41 // Initialize the cache at least once
42 $conf = autoptimizeConfig::instance();
43
44 /* Check if we're updating, in which case we might need to do stuff and flush the cache
45 to avoid old versions of aggregated files lingering around */
46
47 $autoptimize_version="1.9.2";
48 $autoptimize_db_version=get_option('autoptimize_version','none');
49
50 if ($autoptimize_db_version !== $autoptimize_version) {
51 if ($autoptimize_db_version==="none") {
52 add_action('admin_notices', 'autoptimize_install_config_notice');
53 } else {
54 $autoptimize_major_version=substr($autoptimize_db_version,0,3);
55 switch($autoptimize_major_version) {
56 case "1.6":
57 // from back in the days when I did not yet consider multisite
58 // if user was on version 1.6.x, force advanced options to be shown by default
59 update_option('autoptimize_show_adv','1');
60
61 // and remove old options
62 $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");
63 foreach ($to_delete_options as $del_opt) {
64 delete_option( $del_opt );
65 }
66
67 // and notify user to check result
68 add_action('admin_notices', 'autoptimize_update_config_notice');
69 case "1.7":
70 // force 3.8 dashicons in CSS exclude options when upgrading from 1.7 to 1.8
71 if ( !is_multisite() ) {
72 $css_exclude = get_option('autoptimize_css_exclude');
73 if (empty($css_exclude)) {
74 $css_exclude = "admin-bar.min.css, dashicons.min.css";
75 } else if (strpos($css_exclude,"dashicons.min.css")===false) {
76 $css_exclude .= ", dashicons.min.css";
77 }
78 update_option('autoptimize_css_exclude',$css_exclude);
79 } else {
80 global $wpdb;
81 $blog_ids = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" );
82 $original_blog_id = get_current_blog_id();
83 foreach ( $blog_ids as $blog_id ) {
84 switch_to_blog( $blog_id );
85 $css_exclude = get_option('autoptimize_css_exclude');
86 if (empty($css_exclude)) {
87 $css_exclude = "admin-bar.min.css, dashicons.min.css";
88 } else if (strpos($css_exclude,"dashicons.min.css")===false) {
89 $css_exclude .= ", dashicons.min.css";
90 }
91 update_option('autoptimize_css_exclude',$css_exclude);
92 }
93 switch_to_blog( $original_blog_id );
94 }
95 }
96 }
97
98 autoptimizeCache::clearall();
99 update_option('autoptimize_version',$autoptimize_version);
100 $autoptimize_db_version=$autoptimize_version;
101 }
102
103 // Load translations
104 $plugin_dir = basename(dirname(__FILE__));
105 load_plugin_textdomain('autoptimize','wp-content/plugins/'.$plugin_dir.'/localization',$plugin_dir.'/localization');
106
107 function autoptimize_uninstall(){
108 autoptimizeCache::clearall();
109
110 $delete_options=array("autoptimize_cache_clean", "autoptimize_cache_nogzip", "autoptimize_css", "autoptimize_css_datauris", "autoptimize_css_justhead", "autoptimize_css_defer", "autoptimize_css_defer_inline", "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");
111
112 if ( !is_multisite() ) {
113 foreach ($delete_options as $del_opt) { delete_option( $del_opt ); }
114 } else {
115 global $wpdb;
116 $blog_ids = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" );
117 $original_blog_id = get_current_blog_id();
118 foreach ( $blog_ids as $blog_id )
119 {
120 switch_to_blog( $blog_id );
121 foreach ($delete_options as $del_opt) { delete_option( $del_opt ); }
122 }
123 switch_to_blog( $original_blog_id );
124 }
125 }
126
127 function autoptimize_install_config_notice() {
128 echo '<div class="updated"><p>';
129 _e('Thank you for installing and activating Autoptimize. Please configure it under "Settings" -> "Autoptimize" to start improving your site\'s performance.', 'autoptimize' );
130 echo '</p></div>';
131 }
132
133 function autoptimize_update_config_notice() {
134 echo '<div class="updated"><p>';
135 _e('Autoptimize has just been updated. Please <strong>test your site now</strong> and adapt Autoptimize config if needed.', 'autoptimize' );
136 echo '</p></div>';
137 }
138
139 // Set up the buffering
140 function autoptimize_start_buffering() {
141 $ao_noptimize = false;
142
143 // filter you can use to block autoptimization on your own terms
144 $ao_noptimize = (bool) apply_filters( 'autoptimize_filter_noptimize', $ao_noptimize );
145
146 // noptimize in qs to get non-optimized page for debugging
147 if (array_key_exists("ao_noptimize",$_GET)) {
148 if ($_GET["ao_noptimize"]==="1") {
149 $ao_noptimize = true;
150 }
151 }
152
153 if (!is_feed() && !$ao_noptimize && !is_admin()) {
154
155 // Config element
156 $conf = autoptimizeConfig::instance();
157
158 // Load our base class
159 include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeBase.php');
160
161 // Load extra classes and set some vars
162 if($conf->get('autoptimize_html')) {
163 include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeHTML.php');
164 // BUG: new minify-html does not support keeping HTML comments, skipping for now
165 // if (defined('AUTOPTIMIZE_LEGACY_MINIFIERS')) {
166 @include(WP_PLUGIN_DIR.'/autoptimize/classes/external/php/minify-html.php');
167 // } else {
168 // @include(WP_PLUGIN_DIR.'/autoptimize/classes/external/php/minify-2.1.7-html.php');
169 // }
170 }
171
172 if($conf->get('autoptimize_js')) {
173 include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeScripts.php');
174 if (!class_exists('JSMin')) {
175 if (defined('AUTOPTIMIZE_LEGACY_MINIFIERS')) {
176 @include(WP_PLUGIN_DIR.'/autoptimize/classes/external/php/jsmin-1.1.1.php');
177 } else {
178 @include(WP_PLUGIN_DIR.'/autoptimize/classes/external/php/minify-2.1.7-jsmin.php');
179 }
180 }
181 define('CONCATENATE_SCRIPTS',false);
182 define('COMPRESS_SCRIPTS',false);
183 }
184
185 if($conf->get('autoptimize_css')) {
186 include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeStyles.php');
187 if (defined('AUTOPTIMIZE_LEGACY_MINIFIERS')) {
188 if (!class_exists('Minify_CSS_Compressor')) {
189 @include(WP_PLUGIN_DIR.'/autoptimize/classes/external/php/minify-css-compressor.php');
190 }
191 } else {
192 if (!class_exists('CSSmin')) {
193 @include(WP_PLUGIN_DIR.'/autoptimize/classes/external/php/yui-php-cssmin-2.4.8-3_fixes.php');
194 }
195 }
196 define('COMPRESS_CSS',false);
197 }
198
199 // Now, start the real thing!
200 ob_start('autoptimize_end_buffering');
201 }
202 }
203
204 // Action on end, this is where the magic happens
205 function autoptimize_end_buffering($content) {
206 if ( stripos($content,"<html") === false || stripos($content,"<xsl:stylesheet") !== false ) { return $content;}
207
208 // load URL constants as late as possible to allow domain mapper to kick in
209 if (function_exists("domain_mapping_siteurl")) {
210 define('AUTOPTIMIZE_WP_SITE_URL',domain_mapping_siteurl(get_current_blog_id()));
211 define('AUTOPTIMIZE_WP_CONTENT_URL',str_replace(get_original_url(AUTOPTIMIZE_WP_SITE_URL),AUTOPTIMIZE_WP_SITE_URL,content_url()));
212 } else {
213 define('AUTOPTIMIZE_WP_SITE_URL',site_url());
214 define('AUTOPTIMIZE_WP_CONTENT_URL',content_url());
215 }
216
217 if ( is_multisite() ) {
218 $blog_id = get_current_blog_id();
219 define('AUTOPTIMIZE_CACHE_URL',AUTOPTIMIZE_WP_CONTENT_URL.AUTOPTIMIZE_CACHE_CHILD_DIR.$blog_id.'/' );
220 } else {
221 define('AUTOPTIMIZE_CACHE_URL',AUTOPTIMIZE_WP_CONTENT_URL.AUTOPTIMIZE_CACHE_CHILD_DIR);
222 }
223 define('AUTOPTIMIZE_WP_ROOT_URL',str_replace(AUTOPTIMIZE_WP_CONTENT_NAME,'',AUTOPTIMIZE_WP_CONTENT_URL));
224
225 // Config element
226 $conf = autoptimizeConfig::instance();
227
228 // Choose the classes
229 $classes = array();
230 if($conf->get('autoptimize_js'))
231 $classes[] = 'autoptimizeScripts';
232 if($conf->get('autoptimize_css'))
233 $classes[] = 'autoptimizeStyles';
234 if($conf->get('autoptimize_html'))
235 $classes[] = 'autoptimizeHTML';
236
237 // Set some options
238 $classoptions = array(
239 'autoptimizeScripts' => array(
240 'justhead' => $conf->get('autoptimize_js_justhead'),
241 'forcehead' => $conf->get('autoptimize_js_forcehead'),
242 'trycatch' => $conf->get('autoptimize_js_trycatch'),
243 'js_exclude' => $conf->get('autoptimize_js_exclude'),
244 'cdn_url' => $conf->get('autoptimize_cdn_url')
245 ),
246 'autoptimizeStyles' => array(
247 'justhead' => $conf->get('autoptimize_css_justhead'),
248 'datauris' => $conf->get('autoptimize_css_datauris'),
249 'defer' => $conf->get('autoptimize_css_defer'),
250 'defer_inline' => $conf->get('autoptimize_css_defer_inline'),
251 'inline' => $conf->get('autoptimize_css_inline'),
252 'css_exclude' => $conf->get('autoptimize_css_exclude'),
253 'cdn_url' => $conf->get('autoptimize_cdn_url')
254 ),
255 'autoptimizeHTML' => array(
256 'keepcomments' => $conf->get('autoptimize_html_keepcomments')
257 )
258 );
259
260 // Run the classes
261 foreach($classes as $name) {
262 $instance = new $name($content);
263 if($instance->read($classoptions[$name]))
264 {
265 $instance->minify();
266 $instance->cache();
267 $content = $instance->getcontent();
268 }
269 unset($instance);
270 }
271 $content = apply_filters( 'autoptimize_html_after_minify', $content );
272 return $content;
273 }
274
275 function autoptimize_flush_pagecache($nothing) {
276 if(function_exists('wp_cache_clear_cache')) {
277 if (is_multisite()) {
278 $blog_id = get_current_blog_id();
279 wp_cache_clear_cache($blog_id);
280 } else {
281 wp_cache_clear_cache();
282 }
283 } else if ( has_action('cachify_flush_cache') ) {
284 do_action('cachify_flush_cache');
285 } else if ( function_exists('w3tc_pgcache_flush') ) {
286 w3tc_pgcache_flush(); // w3 total cache
287 } else if ( function_exists('hyper_cache_invalidate') ) {
288 hyper_cache_invalidate(); // hypercache
289 } else if ( function_exists('wp_fast_cache_bulk_delete_all') ) {
290 wp_fast_cache_bulk_delete_all(); // wp fast cache
291 } else if (class_exists("WpFastestCache")) {
292 $wpfc = new WpFastestCache(); // wp fastest cache
293 $wpfc -> deleteCache();
294 } else if ( class_exists("c_ws_plugin__qcache_purging_routines") ) {
295 c_ws_plugin__qcache_purging_routines::purge_cache_dir(); // quick cache
296 } else if(file_exists(WP_CONTENT_DIR.'/wp-cache-config.php') && function_exists('prune_super_cache')){
297 // fallback for WP-Super-Cache
298 global $cache_path;
299 if (is_multisite()) {
300 $blog_id = get_current_blog_id();
301 prune_super_cache( get_supercache_dir( $blog_id ), true );
302 prune_super_cache( $cache_path . 'blogs/', true );
303 } else {
304 prune_super_cache($cache_path.'supercache/',true);
305 prune_super_cache($cache_path,true);
306 }
307 }
308 }
309 add_action('ao_flush_pagecache','autoptimize_flush_pagecache',10,1);
310
311 if(autoptimizeCache::cacheavail()) {
312 $conf = autoptimizeConfig::instance();
313 if( $conf->get('autoptimize_html') || $conf->get('autoptimize_js') || $conf->get('autoptimize_css') || $conf->get('autoptimize_cdn_js') || $conf->get('autoptimize_cdn_css')) {
314 // Hook to wordpress
315 add_action('template_redirect','autoptimize_start_buffering',2);
316 }
317 }
318
319 register_uninstall_hook(__FILE__, "autoptimize_uninstall");
320
321 // Do not pollute other plugins
322 unset($conf);
323