These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /* |
||
3 | Plugin Name: Autoptimize |
||
4 | Plugin URI: https://autoptimize.com/ |
||
5 | Description: Optimizes your website, concatenating the CSS and JavaScript code, and compressing it. |
||
6 | Version: 2.3.2 |
||
7 | Author: Frank Goossens (futtta) |
||
8 | Author URI: https://autoptimize.com/ |
||
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 | define('AUTOPTIMIZE_PLUGIN_DIR',plugin_dir_path(__FILE__)); |
||
18 | |||
19 | // Load config class |
||
20 | include(AUTOPTIMIZE_PLUGIN_DIR.'classes/autoptimizeConfig.php'); |
||
21 | |||
22 | // Load toolbar class |
||
23 | include( AUTOPTIMIZE_PLUGIN_DIR.'classes/autoptimizeToolbar.php' ); |
||
24 | |||
25 | // Load partners tab if admin |
||
26 | if (is_admin()) { |
||
27 | include AUTOPTIMIZE_PLUGIN_DIR.'classlesses/autoptimizePartners.php'; |
||
28 | } |
||
29 | |||
30 | // Do we gzip when caching (needed early to load autoptimizeCache.php) |
||
31 | define('AUTOPTIMIZE_CACHE_NOGZIP',(bool) get_option('autoptimize_cache_nogzip')); |
||
32 | |||
33 | // Load cache class |
||
34 | include(AUTOPTIMIZE_PLUGIN_DIR.'classes/autoptimizeCache.php'); |
||
35 | |||
36 | // wp-content dir name (automagically set, should not be needed), dirname of AO cache dir and AO-prefix can be overridden in wp-config.php |
||
37 | if (!defined('AUTOPTIMIZE_WP_CONTENT_NAME')) { define('AUTOPTIMIZE_WP_CONTENT_NAME','/'.wp_basename( WP_CONTENT_DIR )); } |
||
38 | if (!defined('AUTOPTIMIZE_CACHE_CHILD_DIR')) { define('AUTOPTIMIZE_CACHE_CHILD_DIR','/cache/autoptimize/'); } |
||
39 | if (!defined('AUTOPTIMIZE_CACHEFILE_PREFIX')) { define('AUTOPTIMIZE_CACHEFILE_PREFIX', 'autoptimize_'); } |
||
40 | |||
41 | // Plugin dir constants (plugin url's defined later to accomodate domain mapped sites) |
||
42 | View Code Duplication | if (is_multisite() && apply_filters( 'autoptimize_separate_blog_caches' , true )) { |
|
0 ignored issues
–
show
|
|||
43 | $blog_id = get_current_blog_id(); |
||
44 | define('AUTOPTIMIZE_CACHE_DIR', WP_CONTENT_DIR.AUTOPTIMIZE_CACHE_CHILD_DIR.$blog_id.'/' ); |
||
45 | } else { |
||
46 | define('AUTOPTIMIZE_CACHE_DIR', WP_CONTENT_DIR.AUTOPTIMIZE_CACHE_CHILD_DIR); |
||
47 | } |
||
48 | define('AUTOPTIMIZE_CACHE_DELAY',true); |
||
49 | define('WP_ROOT_DIR',substr(WP_CONTENT_DIR, 0, strlen(WP_CONTENT_DIR)-strlen(AUTOPTIMIZE_WP_CONTENT_NAME))); |
||
50 | |||
51 | // WP CLI |
||
52 | if ( defined( 'WP_CLI' ) && WP_CLI ) { |
||
53 | require_once AUTOPTIMIZE_PLUGIN_DIR . 'classes/autoptimizeCLI.php'; |
||
54 | } |
||
55 | |||
56 | // Initialize the cache at least once |
||
57 | $conf = autoptimizeConfig::instance(); |
||
58 | |||
59 | /* Check if we're updating, in which case we might need to do stuff and flush the cache |
||
60 | to avoid old versions of aggregated files lingering around */ |
||
61 | |||
62 | $autoptimize_version="2.3.0"; |
||
63 | $autoptimize_db_version=get_option('autoptimize_version','none'); |
||
64 | |||
65 | if ($autoptimize_db_version !== $autoptimize_version) { |
||
66 | if ($autoptimize_db_version==="none") { |
||
67 | add_action('admin_notices', 'autoptimize_install_config_notice'); |
||
68 | } else { |
||
69 | // updating, include the update-code |
||
70 | include(AUTOPTIMIZE_PLUGIN_DIR.'classlesses/autoptimizeUpdateCode.php'); |
||
71 | } |
||
72 | |||
73 | update_option('autoptimize_version',$autoptimize_version); |
||
74 | $autoptimize_db_version=$autoptimize_version; |
||
75 | } |
||
76 | |||
77 | // Load translations |
||
78 | function autoptimize_load_plugin_textdomain() { |
||
79 | load_plugin_textdomain('autoptimize',false,plugin_basename(dirname( __FILE__ )).'/localization'); |
||
80 | } |
||
81 | add_action( 'init', 'autoptimize_load_plugin_textdomain' ); |
||
82 | |||
83 | function autoptimize_uninstall(){ |
||
84 | autoptimizeCache::clearall(); |
||
85 | |||
86 | $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", "autoptimize_cachesize_notice","autoptimize_css_include_inline","autoptimize_js_include_inline","autoptimize_optimize_logged","autoptimize_optimize_checkout","autoptimize_extra_settings"); |
||
87 | |||
88 | if ( !is_multisite() ) { |
||
89 | foreach ($delete_options as $del_opt) { delete_option( $del_opt ); } |
||
90 | } else { |
||
91 | global $wpdb; |
||
92 | $blog_ids = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" ); |
||
93 | $original_blog_id = get_current_blog_id(); |
||
94 | foreach ( $blog_ids as $blog_id ) { |
||
95 | switch_to_blog( $blog_id ); |
||
96 | foreach ($delete_options as $del_opt) { delete_option( $del_opt ); } |
||
97 | } |
||
98 | switch_to_blog( $original_blog_id ); |
||
99 | } |
||
100 | |||
101 | if ( wp_get_schedule( 'ao_cachechecker' ) ) { |
||
102 | wp_clear_scheduled_hook( 'ao_cachechecker' ); |
||
103 | } |
||
104 | } |
||
105 | |||
106 | function autoptimize_install_config_notice() { |
||
107 | echo '<div class="updated"><p>'; |
||
108 | _e('Thank you for installing and activating Autoptimize. Please configure it under "Settings" -> "Autoptimize" to start improving your site\'s performance.', 'autoptimize' ); |
||
109 | echo '</p></div>'; |
||
110 | } |
||
111 | |||
112 | function autoptimize_update_config_notice() { |
||
113 | echo '<div class="updated"><p>'; |
||
114 | _e('Autoptimize has just been updated. Please <strong>test your site now</strong> and adapt Autoptimize config if needed.', 'autoptimize' ); |
||
115 | echo '</p></div>'; |
||
116 | } |
||
117 | |||
118 | function autoptimize_cache_unavailable_notice() { |
||
119 | echo '<div class="error"><p>'; |
||
120 | printf( __( 'Autoptimize cannot write to the cache directory (%s), please fix to enable CSS/ JS optimization!', 'autoptimize' ), AUTOPTIMIZE_CACHE_DIR ); |
||
121 | echo '</p></div>'; |
||
122 | } |
||
123 | |||
124 | // Set up the buffering |
||
125 | function autoptimize_start_buffering() { |
||
126 | $ao_noptimize = false; |
||
127 | |||
128 | // noptimize in qs to get non-optimized page for debugging |
||
129 | if (array_key_exists("ao_noptimize",$_GET) || array_key_exists("ao_noptirocket",$_GET)) { |
||
130 | if ( ( ($_GET["ao_noptimize"]==="1") || ($_GET["ao_noptirocket"]==="1") ) && (apply_filters('autoptimize_filter_honor_qs_noptimize',true)) ) { |
||
131 | $ao_noptimize = true; |
||
132 | } |
||
133 | } |
||
134 | |||
135 | // check for DONOTMINIFY constant as used by e.g. WooCommerce POS |
||
136 | if (defined('DONOTMINIFY') && (constant('DONOTMINIFY')===true || constant('DONOTMINIFY')==="true")) { |
||
137 | $ao_noptimize = true; |
||
138 | } |
||
139 | |||
140 | // if setting says not to optimize logged in user and user is logged in |
||
141 | if ( get_option('autoptimize_optimize_logged','on') !== 'on' && is_user_logged_in() && current_user_can('edit_posts') ) { |
||
142 | $ao_noptimize = true; |
||
143 | } |
||
144 | |||
145 | // if setting says not to optimize cart/ checkout |
||
146 | if ( get_option('autoptimize_optimize_checkout','on') !== 'on' ) { |
||
147 | // checking for woocommerce, easy digital downloads and wp ecommerce |
||
148 | foreach ( array("is_checkout","is_cart","edd_is_checkout","wpsc_is_cart","wpsc_is_checkout") as $shopCond ) { |
||
149 | if ( function_exists($shopCond) && $shopCond() ) { |
||
150 | $ao_noptimize = true; |
||
151 | break; |
||
152 | } |
||
153 | } |
||
154 | } |
||
155 | |||
156 | // filter you can use to block autoptimization on your own terms |
||
157 | $ao_noptimize = (bool) apply_filters( 'autoptimize_filter_noptimize', $ao_noptimize ); |
||
158 | |||
159 | if (!is_feed() && !$ao_noptimize && !is_admin() && ( !function_exists('is_customize_preview') || !is_customize_preview() ) ) { |
||
160 | // load speedupper conditionally (true by default?) |
||
161 | if ( apply_filters('autoptimize_filter_speedupper', true) ) { |
||
162 | include(AUTOPTIMIZE_PLUGIN_DIR.'classlesses/autoptimizeSpeedupper.php'); |
||
163 | } |
||
164 | |||
165 | // Config element |
||
166 | $conf = autoptimizeConfig::instance(); |
||
167 | |||
168 | // Load our base class |
||
169 | include(AUTOPTIMIZE_PLUGIN_DIR.'classes/autoptimizeBase.php'); |
||
170 | |||
171 | // Load extra classes and set some vars |
||
172 | if($conf->get('autoptimize_html')) { |
||
173 | include(AUTOPTIMIZE_PLUGIN_DIR.'classes/autoptimizeHTML.php'); |
||
174 | // BUG: new minify-html does not support keeping HTML comments, skipping for now |
||
175 | // if (defined('AUTOPTIMIZE_LEGACY_MINIFIERS')) { |
||
176 | @include(AUTOPTIMIZE_PLUGIN_DIR.'classes/external/php/minify-html.php'); |
||
177 | // } else { |
||
178 | // @include(AUTOPTIMIZE_PLUGIN_DIR.'classes/external/php/minify-2.1.7-html.php'); |
||
179 | // } |
||
180 | } |
||
181 | |||
182 | View Code Duplication | if($conf->get('autoptimize_js')) { |
|
183 | include(AUTOPTIMIZE_PLUGIN_DIR.'classes/autoptimizeScripts.php'); |
||
184 | if (!class_exists('JSMin')) { |
||
185 | if (defined('AUTOPTIMIZE_LEGACY_MINIFIERS')) { |
||
186 | @include(AUTOPTIMIZE_PLUGIN_DIR.'classes/external/php/jsmin-1.1.1.php'); |
||
187 | } else { |
||
188 | @include(AUTOPTIMIZE_PLUGIN_DIR.'classes/external/php/minify-2.3.1-jsmin.php'); |
||
189 | } |
||
190 | } |
||
191 | if ( ! defined( 'CONCATENATE_SCRIPTS' )) { |
||
192 | define('CONCATENATE_SCRIPTS',false); |
||
193 | } |
||
194 | if ( ! defined( 'COMPRESS_SCRIPTS' )) { |
||
195 | define('COMPRESS_SCRIPTS',false); |
||
196 | } |
||
197 | } |
||
198 | |||
199 | View Code Duplication | if($conf->get('autoptimize_css')) { |
|
200 | include(AUTOPTIMIZE_PLUGIN_DIR.'classes/autoptimizeStyles.php'); |
||
201 | if (defined('AUTOPTIMIZE_LEGACY_MINIFIERS')) { |
||
202 | if (!class_exists('Minify_CSS_Compressor')) { |
||
203 | @include(AUTOPTIMIZE_PLUGIN_DIR.'classes/external/php/minify-css-compressor.php'); |
||
204 | } |
||
205 | } else { |
||
206 | if (!class_exists('CSSmin')) { |
||
207 | @include(AUTOPTIMIZE_PLUGIN_DIR.'classes/external/php/yui-php-cssmin-2.4.8-4_fgo.php'); |
||
208 | } |
||
209 | } |
||
210 | if ( ! defined( 'COMPRESS_CSS' )) { |
||
211 | define('COMPRESS_CSS',false); |
||
212 | } |
||
213 | } |
||
214 | |||
215 | // filter to be used with care, kills all output buffers when true. use with extreme caution. you have been warned! |
||
216 | if (apply_filters('autoptimize_filter_obkiller',false)) { |
||
217 | while (ob_get_level() > 0) { |
||
218 | ob_end_clean(); |
||
219 | } |
||
220 | } |
||
221 | // Now, start the real thing! |
||
222 | ob_start('autoptimize_end_buffering'); |
||
223 | } |
||
224 | } |
||
225 | |||
226 | // Action on end, this is where the magic happens |
||
227 | function autoptimize_end_buffering($content) { |
||
228 | if ( ((stripos($content,"<html") === false) && (stripos($content,"<!DOCTYPE html") === false)) || preg_match('/<html[^>]*(?:amp|⚡)/',$content) === 1 || stripos($content,"<xsl:stylesheet") !== false ) { return $content; } |
||
229 | |||
230 | // load URL constants as late as possible to allow domain mapper to kick in |
||
231 | if (function_exists("domain_mapping_siteurl")) { |
||
232 | define('AUTOPTIMIZE_WP_SITE_URL',domain_mapping_siteurl(get_current_blog_id())); |
||
233 | define('AUTOPTIMIZE_WP_CONTENT_URL',str_replace(get_original_url(AUTOPTIMIZE_WP_SITE_URL),AUTOPTIMIZE_WP_SITE_URL,content_url())); |
||
234 | } else { |
||
235 | define('AUTOPTIMIZE_WP_SITE_URL',site_url()); |
||
236 | define('AUTOPTIMIZE_WP_CONTENT_URL',content_url()); |
||
237 | } |
||
238 | |||
239 | View Code Duplication | if ( is_multisite() && apply_filters( 'autoptimize_separate_blog_caches' , true ) ) { |
|
240 | $blog_id = get_current_blog_id(); |
||
241 | define('AUTOPTIMIZE_CACHE_URL',AUTOPTIMIZE_WP_CONTENT_URL.AUTOPTIMIZE_CACHE_CHILD_DIR.$blog_id.'/' ); |
||
242 | } else { |
||
243 | define('AUTOPTIMIZE_CACHE_URL',AUTOPTIMIZE_WP_CONTENT_URL.AUTOPTIMIZE_CACHE_CHILD_DIR); |
||
244 | } |
||
245 | define('AUTOPTIMIZE_WP_ROOT_URL',str_replace(AUTOPTIMIZE_WP_CONTENT_NAME,'',AUTOPTIMIZE_WP_CONTENT_URL)); |
||
246 | define('AUTOPTIMIZE_HASH',wp_hash(AUTOPTIMIZE_CACHE_DIR)); |
||
247 | |||
248 | // Config element |
||
249 | $conf = autoptimizeConfig::instance(); |
||
250 | |||
251 | // Choose the classes |
||
252 | $classes = array(); |
||
253 | if($conf->get('autoptimize_js')) |
||
254 | $classes[] = 'autoptimizeScripts'; |
||
255 | if($conf->get('autoptimize_css')) |
||
256 | $classes[] = 'autoptimizeStyles'; |
||
257 | if($conf->get('autoptimize_html')) |
||
258 | $classes[] = 'autoptimizeHTML'; |
||
259 | |||
260 | // Set some options |
||
261 | $classoptions = array( |
||
262 | 'autoptimizeScripts' => array( |
||
263 | 'justhead' => $conf->get('autoptimize_js_justhead'), |
||
264 | 'forcehead' => $conf->get('autoptimize_js_forcehead'), |
||
265 | 'trycatch' => $conf->get('autoptimize_js_trycatch'), |
||
266 | 'js_exclude' => $conf->get('autoptimize_js_exclude'), |
||
267 | 'cdn_url' => $conf->get('autoptimize_cdn_url'), |
||
268 | 'include_inline' => $conf->get('autoptimize_js_include_inline') |
||
269 | ), |
||
270 | 'autoptimizeStyles' => array( |
||
271 | 'justhead' => $conf->get('autoptimize_css_justhead'), |
||
272 | 'datauris' => $conf->get('autoptimize_css_datauris'), |
||
273 | 'defer' => $conf->get('autoptimize_css_defer'), |
||
274 | 'defer_inline' => $conf->get('autoptimize_css_defer_inline'), |
||
275 | 'inline' => $conf->get('autoptimize_css_inline'), |
||
276 | 'css_exclude' => $conf->get('autoptimize_css_exclude'), |
||
277 | 'cdn_url' => $conf->get('autoptimize_cdn_url'), |
||
278 | 'include_inline' => $conf->get('autoptimize_css_include_inline'), |
||
279 | 'nogooglefont' => $conf->get('autoptimize_css_nogooglefont') |
||
280 | ), |
||
281 | 'autoptimizeHTML' => array( |
||
282 | 'keepcomments' => $conf->get('autoptimize_html_keepcomments') |
||
283 | ) |
||
284 | ); |
||
285 | |||
286 | $content = apply_filters( 'autoptimize_filter_html_before_minify', $content ); |
||
287 | |||
288 | // Run the classes |
||
289 | foreach($classes as $name) { |
||
290 | $instance = new $name($content); |
||
291 | if($instance->read($classoptions[$name])) { |
||
292 | $instance->minify(); |
||
293 | $instance->cache(); |
||
294 | $content = $instance->getcontent(); |
||
295 | } |
||
296 | unset($instance); |
||
297 | } |
||
298 | |||
299 | $content = apply_filters( 'autoptimize_html_after_minify', $content ); |
||
300 | return $content; |
||
301 | } |
||
302 | |||
303 | if ( autoptimizeCache::cacheavail() ) { |
||
304 | $conf = autoptimizeConfig::instance(); |
||
305 | if( $conf->get('autoptimize_html') || $conf->get('autoptimize_js') || $conf->get('autoptimize_css') ) { |
||
306 | // Hook to wordpress |
||
307 | if (defined('AUTOPTIMIZE_INIT_EARLIER')) { |
||
308 | add_action('init','autoptimize_start_buffering',-1); |
||
309 | } else { |
||
310 | if (!defined('AUTOPTIMIZE_HOOK_INTO')) { define('AUTOPTIMIZE_HOOK_INTO', 'template_redirect'); } |
||
311 | add_action(constant("AUTOPTIMIZE_HOOK_INTO"),'autoptimize_start_buffering',2); |
||
312 | } |
||
313 | } |
||
314 | } else { |
||
315 | add_action('admin_notices', 'autoptimize_cache_unavailable_notice'); |
||
316 | } |
||
317 | |||
318 | function autoptimize_activate() { |
||
319 | register_uninstall_hook( __FILE__, 'autoptimize_uninstall' ); |
||
320 | } |
||
321 | register_activation_hook( __FILE__, 'autoptimize_activate' ); |
||
322 | |||
323 | include_once('classlesses/autoptimizeCacheChecker.php'); |
||
324 | |||
325 | add_action('plugins_loaded','includeAutoptimizeExtra'); |
||
326 | function includeAutoptimizeExtra() { |
||
327 | if ( apply_filters('autoptimize_filter_extra_activate',true) ) { |
||
328 | include_once('classlesses/autoptimizeExtra.php'); |
||
329 | } |
||
330 | } |
||
331 | |||
332 | // Do not pollute other plugins |
||
333 | unset($conf); |
||
334 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.