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

229 lines 8.0 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.7.3
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 */
12
13 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
14
15 // Load config and cache class
16 include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeConfig.php');
17 include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeCache.php');
18
19 // Plugin dir constants (plugin url's defined later to accomodate domain mapped sites)
20 define('AUTOPTIMIZE_CACHE_DIR',WP_CONTENT_DIR.'/cache/autoptimize/');
21 define('AUTOPTIMIZE_CACHE_DELAY',true);
22 define('WP_ROOT_DIR',str_replace('/wp-content','',WP_CONTENT_DIR));
23
24 // Initialize the cache at least once
25 $conf = autoptimizeConfig::instance();
26
27 /* Check if we're updating, in which case we need to flush the cache
28 to avoid old versions of aggregated files lingering around */
29
30 $autoptimize_version="1.7.3";
31 $autoptimize_db_version=get_option('autoptimize_version','none');
32
33 if ($autoptimize_db_version !== $autoptimize_version) {
34 if ($autoptimize_db_version==="none") {
35 add_action('admin_notices', 'autoptimize_install_config_notice');
36 } else if (strpos($autoptimize_db_version,"1.6.")!==false) {
37 // if user was on version 1.6.x, force advanced options to be shown by default
38 update_option('autoptimize_show_adv','1');
39
40 // and remove old options
41 $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");
42 foreach ($delete_options as $del_opt) {
43 delete_option( $del_opt );
44 }
45
46 // and notify user to check result
47 add_action('admin_notices', 'autoptimize_update_config_notice');
48 }
49
50 autoptimizeCache::clearall();
51 update_option('autoptimize_version',$autoptimize_version);
52 $autoptimize_db_version=$autoptimize_version;
53 }
54
55 // Do we gzip when caching?
56 define('AUTOPTIMIZE_CACHE_NOGZIP',(bool) $conf->get('autoptimize_cache_nogzip'));
57
58 // Load translations
59 $plugin_dir = basename(dirname(__FILE__));
60 load_plugin_textdomain('autoptimize','wp-content/plugins/'.$plugin_dir.'/localization',$plugin_dir.'/localization');
61
62 function autoptimize_uninstall(){
63 autoptimizeCache::clearall();
64
65 $delete_options=array("autoptimize_cache_clean", "autoptimize_cache_nogzip", "autoptimize_css", "autoptimize_css_datauris", "autoptimize_css_justhead", "autoptimize_css_defer", "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");
66
67 if ( !is_multisite() ) {
68 foreach ($delete_options as $del_opt) { delete_option( $del_opt ); }
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 {
75 switch_to_blog( $blog_id );
76 foreach ($delete_options as $del_opt) { delete_option( $del_opt ); }
77 }
78 switch_to_blog( $original_blog_id );
79 }
80 }
81
82 function autoptimize_install_config_notice() {
83 echo '<div class="updated"><p>';
84 _e('Thank you for installing and activating Autoptimize. Please configure it under "Settings" -> "Autoptimize" to start improving your site\'s performance.', 'autoptimize' );
85 echo '</p></div>';
86 }
87
88 function autoptimize_update_config_notice() {
89 echo '<div class="updated"><p>';
90 _e('Autoptimize has just been updated. Please <strong>test your site now</strong> and adapt Autoptimize config if needed.', 'autoptimize' );
91 echo '</p></div>';
92 }
93
94 // Set up the buffering
95 function autoptimize_start_buffering()
96 {
97 if (!is_feed()) {
98
99 // Config element
100 $conf = autoptimizeConfig::instance();
101
102 // Load our base class
103 include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeBase.php');
104
105 // Load extra classes and set some vars
106 if($conf->get('autoptimize_html'))
107 {
108 include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeHTML.php');
109 // BUG: new minify-html does not support keeping HTML comments, skipping for now
110 // if (defined('AUTOPTIMIZE_LEGACY_MINIFIERS')) {
111 @include(WP_PLUGIN_DIR.'/autoptimize/classes/external/php/minify-html.php');
112 // } else {
113 // @include(WP_PLUGIN_DIR.'/autoptimize/classes/external/php/minify-2.1.7-html.php');
114 // }
115 }
116
117 if($conf->get('autoptimize_js'))
118 {
119 include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeScripts.php');
120 if (!class_exists('JSMin')) {
121 if (defined('AUTOPTIMIZE_LEGACY_MINIFIERS')) {
122 @include(WP_PLUGIN_DIR.'/autoptimize/classes/external/php/jsmin-1.1.1.php');
123 } else {
124 @include(WP_PLUGIN_DIR.'/autoptimize/classes/external/php/minify-2.1.7-jsmin.php');
125 }
126 }
127 define('CONCATENATE_SCRIPTS',false);
128 define('COMPRESS_SCRIPTS',false);
129 }
130
131 if($conf->get('autoptimize_css'))
132 {
133 include(WP_PLUGIN_DIR.'/autoptimize/classes/autoptimizeStyles.php');
134 if (defined('AUTOPTIMIZE_LEGACY_MINIFIERS')) {
135 if (!class_exists('Minify_CSS_Compressor')) {
136 @include(WP_PLUGIN_DIR.'/autoptimize/classes/external/php/minify-css-compressor.php');
137 }
138 } else {
139 if (!class_exists('CSSmin')) {
140 @include(WP_PLUGIN_DIR.'/autoptimize/classes/external/php/yui-php-cssmin-2.4.8-1.php');
141 }
142 }
143 define('COMPRESS_CSS',false);
144 }
145
146 // Now, start the real thing!
147 ob_start('autoptimize_end_buffering');
148 }
149 }
150
151 //Action on end -
152 function autoptimize_end_buffering($content)
153 {
154 if ( stripos($content,"<html") === false || stripos($content,"<xsl:stylesheet") !== false ) { return $content;}
155
156 // load URL constants as late as possible to allow domain mapper to kick in
157 if (function_exists(domain_mapping_siteurl)) {
158 define('AUTOPTIMIZE_WP_SITE_URL',domain_mapping_siteurl());
159 define('AUTOPTIMIZE_WP_CONTENT_URL',str_replace(get_original_url(AUTOPTIMIZE_WP_SITE_URL),AUTOPTIMIZE_WP_SITE_URL,content_url()));
160 } else {
161 define('AUTOPTIMIZE_WP_SITE_URL',site_url());
162 define('AUTOPTIMIZE_WP_CONTENT_URL',content_url());
163 }
164 define('AUTOPTIMIZE_CACHE_URL',AUTOPTIMIZE_WP_CONTENT_URL.'/cache/autoptimize/');
165 define('AUTOPTIMIZE_WP_ROOT_URL',str_replace('/wp-content','',AUTOPTIMIZE_WP_CONTENT_URL));
166 // Config element
167 $conf = autoptimizeConfig::instance();
168
169 // Choose the classes
170 $classes = array();
171 if($conf->get('autoptimize_js'))
172 $classes[] = 'autoptimizeScripts';
173 if($conf->get('autoptimize_css'))
174 $classes[] = 'autoptimizeStyles';
175 if($conf->get('autoptimize_html'))
176 $classes[] = 'autoptimizeHTML';
177
178 // Set some options
179 $classoptions = array(
180 'autoptimizeScripts' => array(
181 'justhead' => $conf->get('autoptimize_js_justhead'),
182 'forcehead' => $conf->get('autoptimize_js_forcehead'),
183 'trycatch' => $conf->get('autoptimize_js_trycatch'),
184 'js_exclude' => $conf->get('autoptimize_js_exclude'),
185 'cdn_url' => $conf->get('autoptimize_cdn_url')
186 ),
187 'autoptimizeStyles' => array(
188 'justhead' => $conf->get('autoptimize_css_justhead'),
189 'datauris' => $conf->get('autoptimize_css_datauris'),
190 'defer' => $conf->get('autoptimize_css_defer'),
191 'css_exclude' => $conf->get('autoptimize_css_exclude'),
192 'cdn_url' => $conf->get('autoptimize_cdn_url')
193 ),
194 'autoptimizeHTML' => array(
195 'keepcomments' => $conf->get('autoptimize_html_keepcomments')
196 )
197 );
198
199
200 // Run the classes
201 foreach($classes as $name)
202 {
203 $instance = new $name($content);
204 if($instance->read($classoptions[$name]))
205 {
206 $instance->minify();
207 $instance->cache();
208 $content = $instance->getcontent();
209 }
210 unset($instance);
211 }
212 return $content;
213 }
214
215 if(autoptimizeCache::cacheavail())
216 {
217 $conf = autoptimizeConfig::instance();
218 if( $conf->get('autoptimize_html') || $conf->get('autoptimize_js') || $conf->get('autoptimize_css') || $conf->get('autoptimize_cdn_js') || $conf->get('autoptimize_cdn_css'))
219 {
220 // Hook to wordpress
221 add_action('template_redirect','autoptimize_start_buffering',2);
222 }
223 }
224
225 register_uninstall_hook(__FILE__, "autoptimize_uninstall");
226
227 // Do not pollute other plugins
228 unset($conf);
229