PluginProbe
Autoptimize / 1.7.0
Autoptimize v1.7.0
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.0, at autoptimize.php

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