PluginProbe
Autoptimize / 1.8.5
Autoptimize v1.8.5
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.8.5, at autoptimize.php

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