| 1 |
<?php |
| 2 |
|
| 3 |
class autoptimizeConfig |
| 4 |
{ |
| 5 |
private $config = null; |
| 6 |
static private $instance = null; |
| 7 |
|
| 8 |
//Singleton: private construct |
| 9 |
private function __construct() |
| 10 |
{ |
| 11 |
if( is_admin() ) |
| 12 |
{ |
| 13 |
//Add the admin page and settings |
| 14 |
add_action('admin_menu',array($this,'addmenu')); |
| 15 |
add_action('admin_init',array($this,'registersettings')); |
| 16 |
|
| 17 |
//Set meta info |
| 18 |
if(function_exists('plugin_row_meta')) |
| 19 |
{ |
| 20 |
//2.8+ |
| 21 |
add_filter('plugin_row_meta',array($this,'setmeta'),10,2); |
| 22 |
} elseif(function_exists('post_class')) { |
| 23 |
//2.7 |
| 24 |
$plugin = plugin_basename(WP_PLUGIN_DIR.'/autoptimize/autoptimize.php'); |
| 25 |
add_filter('plugin_action_links_'.$plugin,array($this,'setmeta')); |
| 26 |
} |
| 27 |
|
| 28 |
//Clean cache? |
| 29 |
if(get_option('autoptimize_cache_clean')) |
| 30 |
{ |
| 31 |
autoptimizeCache::clearall(); |
| 32 |
update_option('autoptimize_cache_clean',0); |
| 33 |
} |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
static public function instance() |
| 38 |
{ |
| 39 |
//Only one instance |
| 40 |
if (self::$instance == null) |
| 41 |
{ |
| 42 |
self::$instance = new autoptimizeConfig(); |
| 43 |
} |
| 44 |
|
| 45 |
return self::$instance; |
| 46 |
} |
| 47 |
|
| 48 |
public function show() |
| 49 |
{ |
| 50 |
?> |
| 51 |
<div class="wrap"> |
| 52 |
<h2><?php _e('Autoptimize Settings','autoptimize'); ?></h2> |
| 53 |
|
| 54 |
<div style="float:left;width:70%;"> |
| 55 |
<form method="post" action="options.php"> |
| 56 |
<?php settings_fields('autoptimize'); ?> |
| 57 |
|
| 58 |
<h3><?php _e('HTML Options','autoptimize'); ?></h3> |
| 59 |
<table class="form-table"> |
| 60 |
<tr valign="top"> |
| 61 |
<th scope="row"><?php _e('Optimize HTML Code?','autoptimize'); ?></th> |
| 62 |
<td><input type="checkbox" name="autoptimize_html" <?php echo get_option('autoptimize_html')?'checked="checked" ':''; ?>/></td> |
| 63 |
</tr> |
| 64 |
<tr valign="top"> |
| 65 |
<th scope="row"><?php _e('Keep HTML comments?','autoptimize'); ?></th> |
| 66 |
<td><label for="autoptimize_html_keepcomments"><input type="checkbox" name="autoptimize_html_keepcomments" <?php echo get_option('autoptimize_html_keepcomments')?'checked="checked" ':''; ?>/> |
| 67 |
<?php _e('Disabled by default. Enable this if you want HTML comments to remain in the page.','autoptimize'); ?></label></td> |
| 68 |
</tr> |
| 69 |
</table> |
| 70 |
|
| 71 |
<h3><?php _e('JavaScript Options','autoptimize'); ?></h3> |
| 72 |
<table class="form-table"> |
| 73 |
<tr valign="top"> |
| 74 |
<th scope="row"><?php _e('Optimize JavaScript Code?','autoptimize'); ?></th> |
| 75 |
<td><input type="checkbox" name="autoptimize_js" <?php echo get_option('autoptimize_js')?'checked="checked" ':''; ?>/></td> |
| 76 |
</tr> |
| 77 |
<tr valign="top"> |
| 78 |
<th scope="row"><?php _e('Exclude scripts from autoptimize:','autoptimize'); ?></th> |
| 79 |
<td><label for="autoptimize_js_exclude"><input type="text" style="width:100%;" name="autoptimize_js_exclude" value="<?php echo get_option('autoptimize_js_exclude',"s_sid,smowtion_size,sc_project,WAU_,wau_add,comment-form-quicktags,edToolbar,ch_client"); ?>"/><br /> |
| 80 |
<?php _e('A comma-seperated list of scripts you want to exclude from being Autoptimized, for example \'whatever.js, another.js\' (without the quotes) to exclude those scripts from being aggregated and minimized by Autoptimize.','autoptimize'); ?></label></td> |
| 81 |
</tr> |
| 82 |
<tr valign="top"> |
| 83 |
<th scope="row"><?php _e('Look for scripts only in <head>?','autoptimize'); ?></th> |
| 84 |
<td><label for="autoptimize_js_justhead"><input type="checkbox" name="autoptimize_js_justhead" <?php echo get_option('autoptimize_js_justhead')?'checked="checked" ':''; ?>/> |
| 85 |
<?php _e('Disabled by default. If the cache gets big, you might want to enable this.','autoptimize'); ?></label></td> |
| 86 |
</tr> |
| 87 |
<tr valign="top"> |
| 88 |
<th scope="row"><?php _e('Add try-catch wrapping?','autoptimize'); ?></th> |
| 89 |
<td><label for="autoptimize_js_trycatch"><input type="checkbox" name="autoptimize_js_trycatch" <?php echo get_option('autoptimize_js_trycatch')?'checked="checked" ':''; ?>/> |
| 90 |
<?php _e('Disabled by default. If your scripts break because of an script error, you might want to try this.','autoptimize'); ?></label></td> |
| 91 |
</tr> |
| 92 |
<tr valign="top"> |
| 93 |
<th scope="row"><?php _e('Use YUI compression?','autoptimize'); ?></th> |
| 94 |
<td><label for="autoptimize_js_yui"><input type="checkbox" name="autoptimize_js_yui" <?php echo get_option('autoptimize_js_yui')?'checked="checked" ':''; ?>/> |
| 95 |
<?php _e('Disabled by default. Read [autoptimize]/yui/README.txt for more information.','autoptimize'); ?></label></td> |
| 96 |
</tr> |
| 97 |
</table> |
| 98 |
|
| 99 |
<h3><?php _e('CSS Options','autoptimize'); ?></h3> |
| 100 |
<table class="form-table"> |
| 101 |
<tr valign="top"> |
| 102 |
<th scope="row"><?php _e('Optimize CSS Code?','autoptimize'); ?></th> |
| 103 |
<td><input type="checkbox" name="autoptimize_css" <?php echo get_option('autoptimize_css')?'checked="checked" ':''; ?>/></td> |
| 104 |
</tr> |
| 105 |
<tr valign="top"> |
| 106 |
<th scope="row"><?php _e('Look for styles on just <head>?','autoptimize'); ?></th> |
| 107 |
<td><label for="autoptimize_css_justhead"><input type="checkbox" name="autoptimize_css_justhead" <?php echo get_option('autoptimize_css_justhead')?'checked="checked" ':''; ?>/> |
| 108 |
<?php _e('Disabled by default. If the cache gets big, you might want to enable this.','autoptimize'); ?></label></td> |
| 109 |
</tr> |
| 110 |
<tr valign="top"> |
| 111 |
<th scope="row"><?php _e('Generate data: URIs for images?','autoptimize'); ?></th> |
| 112 |
<td><label for="autoptimize_css_datauris"><input type="checkbox" name="autoptimize_css_datauris" <?php echo get_option('autoptimize_css_datauris')?'checked="checked" ':''; ?>/> |
| 113 |
<?php _e('Disabled by default. Enable this to include images on the CSS itself.','autoptimize'); ?></label></td> |
| 114 |
</tr> |
| 115 |
<tr valign="top"> |
| 116 |
<th scope="row"><?php _e('Use YUI compression?','autoptimize'); ?></th> |
| 117 |
<td><label for="autoptimize_css_yui"><input type="checkbox" name="autoptimize_css_yui" <?php echo get_option('autoptimize_css_yui')?'checked="checked" ':''; ?>/> |
| 118 |
<?php _e('Disabled by default. Read [autoptimize]/yui/README.txt for more information.','autoptimize'); ?></label></td> |
| 119 |
</tr> |
| 120 |
</table> |
| 121 |
|
| 122 |
<h3><?php _e('CDN Options','autoptimize'); ?></h3> |
| 123 |
<table class="form-table"> |
| 124 |
<tr valign="top"> |
| 125 |
<th scope="row"><?php _e('Rewrite JavaScript URLs?','autoptimize'); ?></th> |
| 126 |
<td><label for="autoptimize_cdn_js"><input type="checkbox" name="autoptimize_cdn_js" <?php echo get_option('autoptimize_cdn_js')?'checked="checked" ':''; ?>/> |
| 127 |
<?php _e('Disabled by default. Do not enable this unless you know what you are doing.','autoptimize'); ?></label></td> |
| 128 |
</tr> |
| 129 |
<tr valign="top"> |
| 130 |
<th scope="row"><?php _e('JavaScript Base URL','autoptimize'); ?></th> |
| 131 |
<td><label for="autoptimize_cdn_js_url"><input type="text" name="autoptimize_cdn_js_url" value="<?php $it = get_option('autoptimize_cdn_js_url');echo htmlentities($it?$it:site_url()); ?>" /> |
| 132 |
<?php _e('This is the new base URL that will be used when rewriting. It should point to the blog root directory.','autoptimize'); ?></label></td> |
| 133 |
</tr> |
| 134 |
<tr valign="top"> |
| 135 |
<th scope="row"><?php _e('Rewrite CSS URLs?','autoptimize'); ?></th> |
| 136 |
<td><label for="autoptimize_cdn_css"><input type="checkbox" name="autoptimize_cdn_css" <?php echo get_option('autoptimize_cdn_css')?'checked="checked" ':''; ?>/> |
| 137 |
<?php _e('Disabled by default. Do not enable this unless you know what you are doing.','autoptimize'); ?></label></td> |
| 138 |
</tr> |
| 139 |
<tr valign="top"> |
| 140 |
<th scope="row"><?php _e('CSS Base URL','autoptimize'); ?></th> |
| 141 |
<td><label for="autoptimize_cdn_css_url"><input type="text" name="autoptimize_cdn_css_url" value="<?php $it = get_option('autoptimize_cdn_css_url');echo htmlentities($it?$it:site_url()); ?>" /> |
| 142 |
<?php _e('This is the new base URL that will be used when rewriting. It should point to the blog root directory.','autoptimize'); ?></label></td> |
| 143 |
</tr> |
| 144 |
<tr valign="top"> |
| 145 |
<th scope="row"><?php _e('Rewrite Image URLs?','autoptimize'); ?></th> |
| 146 |
<td><label for="autoptimize_cdn_img"><input type="checkbox" name="autoptimize_cdn_img" <?php echo get_option('autoptimize_cdn_img')?'checked="checked" ':''; ?>/> |
| 147 |
<?php _e('Disabled by default. Do not enable this unless you know what you are doing.','autoptimize'); ?></label></td> |
| 148 |
</tr> |
| 149 |
<tr valign="top"> |
| 150 |
<th scope="row"><?php _e('Image Base URL','autoptimize'); ?></th> |
| 151 |
<td><label for="autoptimize_cdn_img_url"><input type="text" name="autoptimize_cdn_img_url" value="<?php $it = get_option('autoptimize_cdn_img_url');echo htmlentities($it?$it:site_url()); ?>" /> |
| 152 |
<?php _e('This is the new base URL that will be used when rewriting. It should point to the blog root directory.','autoptimize'); ?></label></td> |
| 153 |
</tr> |
| 154 |
</table> |
| 155 |
|
| 156 |
<h3><?php _e('Cache Info','autoptimize'); ?></h3> |
| 157 |
<table class="form-table"> |
| 158 |
<tr valign="top"> |
| 159 |
<th scope="row"><?php _e('Cache folder','autoptimize'); ?></th> |
| 160 |
<td><?php echo htmlentities(AUTOPTIMIZE_CACHE_DIR); ?></td> |
| 161 |
</tr> |
| 162 |
<tr valign="top"> |
| 163 |
<th scope="row"><?php _e('Can we write?','autoptimize'); ?></th> |
| 164 |
<td><?php echo (autoptimizeCache::cacheavail() ? __('Yes','autoptimize') : __('No','autoptimize')); ?></td> |
| 165 |
</tr> |
| 166 |
<tr valign="top"> |
| 167 |
<th scope="row"><?php _e('Cached styles and scripts','autoptimize'); ?></th> |
| 168 |
<td><?php echo autoptimizeCache::stats(); ?></td> |
| 169 |
</tr> |
| 170 |
<tr valign="top"> |
| 171 |
<th scope="row"><?php _e('Do not compress cache files','autoptimize'); ?></th> |
| 172 |
<td><label for="autoptimize_cache_nogzip"><input type="checkbox" name="autoptimize_cache_nogzip" <?php echo get_option('autoptimize_cache_nogzip')?'checked="checked" ':''; ?>/> |
| 173 |
<?php _e('Disabled by default. Enable this if you want to compress the served files using your webserver.','autoptimize'); ?></label></td> |
| 174 |
</tr> |
| 175 |
</table> |
| 176 |
|
| 177 |
</table> |
| 178 |
|
| 179 |
<p class="submit"> |
| 180 |
<input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" /> |
| 181 |
<input type="submit" name="autoptimize_cache_clean" value="<?php _e('Save Changes and Empty Cache') ?>" /> |
| 182 |
</p> |
| 183 |
|
| 184 |
</form> |
| 185 |
</div> |
| 186 |
<div style="float:right;width:30%" id="autoptimize_admin_feed"> |
| 187 |
<div style="margin-left:10px;margin-top:-5px;"> |
| 188 |
<h3> |
| 189 |
<?php _e("futtta about","autoptimize") ?> |
| 190 |
<select id="feed_dropdown" > |
| 191 |
<option value="1"><?php _e("Autoptimize","autoptimize") ?></option> |
| 192 |
<option value="2"><?php _e("WordPress","autoptimize") ?></option> |
| 193 |
<option value="3"><?php _e("Web Technology","autoptimize") ?></option> |
| 194 |
</select> |
| 195 |
</h3> |
| 196 |
<div id="futtta_feed"></div> |
| 197 |
</div> |
| 198 |
</div> |
| 199 |
|
| 200 |
<script type="text/javascript"> |
| 201 |
var feed = new Array; |
| 202 |
feed[1]="http://feeds.feedburner.com/futtta_autoptimize"; |
| 203 |
feed[2]="http://feeds.feedburner.com/futtta_wordpress"; |
| 204 |
feed[3]="http://feeds.feedburner.com/futtta_webtech"; |
| 205 |
cookiename="autoptimize_feed"; |
| 206 |
|
| 207 |
jQuery(document).ready(function() { |
| 208 |
jQuery("#feed_dropdown").change(function() { show_feed(jQuery("#feed_dropdown").val()) }); |
| 209 |
|
| 210 |
feedid=jQuery.cookie(cookiename); |
| 211 |
if(typeof(feedid) !== "string") feedid=1; |
| 212 |
|
| 213 |
show_feed(feedid); |
| 214 |
}) |
| 215 |
|
| 216 |
function show_feed(id) { |
| 217 |
jQuery('#futtta_feed').rssfeed(feed[id], { |
| 218 |
<?php if ( is_ssl() ) echo "ssl: true,"; ?> |
| 219 |
limit: 4, |
| 220 |
date: true, |
| 221 |
header: false |
| 222 |
}); |
| 223 |
jQuery("#feed_dropdown").val(id); |
| 224 |
jQuery.cookie(cookiename,id,{ expires: 365 }); |
| 225 |
} |
| 226 |
</script> |
| 227 |
|
| 228 |
</div> |
| 229 |
<?php |
| 230 |
} |
| 231 |
|
| 232 |
public function addmenu() |
| 233 |
{ |
| 234 |
$hook=add_options_page(__('Autoptimize Options','autoptimize'),'Autoptimize','manage_options','autoptimize',array($this,'show')); |
| 235 |
add_action( 'admin_print_scripts-'.$hook,array($this,'autoptimize_admin_scripts')); |
| 236 |
add_action( 'admin_print_styles-'.$hook,array($this,'autoptimize_admin_styles')); |
| 237 |
} |
| 238 |
|
| 239 |
public function autoptimize_admin_scripts() { |
| 240 |
wp_enqueue_script('jqzrssfeed', plugins_url('/external/jquery.zrssfeed.min.js', __FILE__), array('jquery'),null,true); |
| 241 |
wp_enqueue_script('jqcookie', plugins_url('/external/jquery.cookie.min.js', __FILE__), array('jquery'),null,true); |
| 242 |
} |
| 243 |
|
| 244 |
public function autoptimize_admin_styles() { |
| 245 |
wp_enqueue_style('zrssfeed', plugins_url('/external/jquery.zrssfeed.css', __FILE__)); |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
public function registersettings() |
| 250 |
{ |
| 251 |
register_setting('autoptimize','autoptimize_html'); |
| 252 |
register_setting('autoptimize','autoptimize_html_keepcomments'); |
| 253 |
register_setting('autoptimize','autoptimize_js'); |
| 254 |
register_setting('autoptimize','autoptimize_js_exclude'); |
| 255 |
register_setting('autoptimize','autoptimize_js_trycatch'); |
| 256 |
register_setting('autoptimize','autoptimize_js_justhead'); |
| 257 |
register_setting('autoptimize','autoptimize_js_yui'); |
| 258 |
register_setting('autoptimize','autoptimize_css'); |
| 259 |
register_setting('autoptimize','autoptimize_css_justhead'); |
| 260 |
register_setting('autoptimize','autoptimize_css_datauris'); |
| 261 |
register_setting('autoptimize','autoptimize_css_yui'); |
| 262 |
register_setting('autoptimize','autoptimize_cdn_js'); |
| 263 |
register_setting('autoptimize','autoptimize_cdn_js_url'); |
| 264 |
register_setting('autoptimize','autoptimize_cdn_css'); |
| 265 |
register_setting('autoptimize','autoptimize_cdn_css_url'); |
| 266 |
register_setting('autoptimize','autoptimize_cdn_img'); |
| 267 |
register_setting('autoptimize','autoptimize_cdn_img_url'); |
| 268 |
register_setting('autoptimize','autoptimize_cache_clean'); |
| 269 |
register_setting('autoptimize','autoptimize_cache_nogzip'); |
| 270 |
} |
| 271 |
|
| 272 |
public function setmeta($links,$file=null) |
| 273 |
{ |
| 274 |
//Inspired on http://wpengineer.com/meta-links-for-wordpress-plugins/ |
| 275 |
|
| 276 |
//Do it only once - saves time |
| 277 |
static $plugin; |
| 278 |
if(empty($plugin)) |
| 279 |
$plugin = plugin_basename(WP_PLUGIN_DIR.'/autoptimize/autoptimize.php'); |
| 280 |
|
| 281 |
if($file===null) |
| 282 |
{ |
| 283 |
//2.7 |
| 284 |
$settings_link = sprintf('<a href="options-general.php?page=autoptimize">%s</a>', __('Settings')); |
| 285 |
array_unshift($links,$settings_link); |
| 286 |
}else{ |
| 287 |
//2.8 |
| 288 |
//If it's us, add the link |
| 289 |
if($file === $plugin) |
| 290 |
{ |
| 291 |
$newlink = array(sprintf('<a href="options-general.php?page=autoptimize">%s</a>',__('Settings'))); |
| 292 |
$links = array_merge($links,$newlink); |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
return $links; |
| 297 |
} |
| 298 |
|
| 299 |
public function get($key) |
| 300 |
{ |
| 301 |
if(!is_array($this->config)) |
| 302 |
{ |
| 303 |
//Default config |
| 304 |
$config = array('autoptimize_html' => 0, |
| 305 |
'autoptimize_html_keepcomments' => 0, |
| 306 |
'autoptimize_js' => 0, |
| 307 |
'autoptimize_js_exclude' => "s_sid, smowtion_size, sc_project, WAU_, wau_add, comment-form-quicktags, edToolbar, ch_client", |
| 308 |
'autoptimize_js_trycatch' => 0, |
| 309 |
'autoptimize_js_justhead' => 0, |
| 310 |
'autoptimize_js_yui' => 0, |
| 311 |
'autoptimize_css' => 0, |
| 312 |
'autoptimize_css_justhead' => 0, |
| 313 |
'autoptimize_css_datauris' => 0, |
| 314 |
'autoptimize_css_yui' => 0, |
| 315 |
'autoptimize_cdn_js' => 0, |
| 316 |
'autoptimize_cdn_js_url' => site_url(), |
| 317 |
'autoptimize_cdn_css' => 0, |
| 318 |
'autoptimize_cdn_css_url' => site_url(), |
| 319 |
'autoptimize_cdn_img' => 0, |
| 320 |
'autoptimize_cdn_img_url' => site_url(), |
| 321 |
'autoptimize_cache_nogzip' => 0, |
| 322 |
); |
| 323 |
|
| 324 |
//Override with user settings |
| 325 |
foreach(array_keys($config) as $name) |
| 326 |
{ |
| 327 |
$conf = get_option($name); |
| 328 |
if($conf!==false) |
| 329 |
{ |
| 330 |
//It was set before! |
| 331 |
$config[$name] = $conf; |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
//Save for next question |
| 336 |
$this->config = $config; |
| 337 |
} |
| 338 |
|
| 339 |
if(isset($this->config[$key])) |
| 340 |
return $this->config[$key]; |
| 341 |
|
| 342 |
return false; |
| 343 |
} |
| 344 |
} |
| 345 |
|