| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 3 |
|
| 4 |
class autoptimizeConfig |
| 5 |
{ |
| 6 |
private $config = null; |
| 7 |
static private $instance = null; |
| 8 |
|
| 9 |
//Singleton: private construct |
| 10 |
private function __construct() |
| 11 |
{ |
| 12 |
if( is_admin() ) |
| 13 |
{ |
| 14 |
//Add the admin page and settings |
| 15 |
add_action('admin_menu',array($this,'addmenu')); |
| 16 |
add_action('admin_init',array($this,'registersettings')); |
| 17 |
|
| 18 |
//Set meta info |
| 19 |
if(function_exists('plugin_row_meta')) |
| 20 |
{ |
| 21 |
//2.8+ |
| 22 |
add_filter('plugin_row_meta',array($this,'setmeta'),10,2); |
| 23 |
} elseif(function_exists('post_class')) { |
| 24 |
//2.7 |
| 25 |
$plugin = plugin_basename(WP_PLUGIN_DIR.'/autoptimize/autoptimize.php'); |
| 26 |
add_filter('plugin_action_links_'.$plugin,array($this,'setmeta')); |
| 27 |
} |
| 28 |
|
| 29 |
//Clean cache? |
| 30 |
if(get_option('autoptimize_cache_clean')) |
| 31 |
{ |
| 32 |
autoptimizeCache::clearall(); |
| 33 |
update_option('autoptimize_cache_clean',0); |
| 34 |
} |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
static public function instance() |
| 39 |
{ |
| 40 |
//Only one instance |
| 41 |
if (self::$instance == null) |
| 42 |
{ |
| 43 |
self::$instance = new autoptimizeConfig(); |
| 44 |
} |
| 45 |
|
| 46 |
return self::$instance; |
| 47 |
} |
| 48 |
|
| 49 |
public function show() |
| 50 |
{ |
| 51 |
?> |
| 52 |
<style>input[type=url]:invalid {color: red; border-color:red;} .form-table th{font-weight:100;}</style> |
| 53 |
|
| 54 |
<div class="wrap"> |
| 55 |
|
| 56 |
<h2><?php _e('Autoptimize Settings','autoptimize'); ?></h2> |
| 57 |
|
| 58 |
<div style="float:left;width:70%;"> |
| 59 |
<?php |
| 60 |
if (get_option('autoptimize_show_adv','0')=='1') { |
| 61 |
?> |
| 62 |
<a href="javascript:void(0);" id="ao_show_adv" class="button" style="display:none;"><?php _e("Show advanced settings","autoptimize") ?></a> |
| 63 |
<a href="javascript:void(0);" id="ao_hide_adv" class="button"><?php _e("Hide advanced settings","autoptimize") ?></a> |
| 64 |
<style>.ao_adv {display:table-row};</style> |
| 65 |
<?php |
| 66 |
} else { |
| 67 |
?> |
| 68 |
<a href="javascript:void(0);" id="ao_show_adv" class="button"><?php _e("Show advanced settings","autoptimize") ?></a> |
| 69 |
<a href="javascript:void(0);" id="ao_hide_adv" class="button" style="display:none;"><?php _e("Hide advanced settings","autoptimize") ?></a> |
| 70 |
<?php |
| 71 |
} |
| 72 |
?> |
| 73 |
|
| 74 |
<form method="post" action="options.php"> |
| 75 |
<?php settings_fields('autoptimize'); ?> |
| 76 |
|
| 77 |
<h3><?php _e('HTML Options','autoptimize'); ?></h3> |
| 78 |
<table class="form-table"> |
| 79 |
<tr valign="top"> |
| 80 |
<th scope="row"><?php _e('Optimize HTML Code?','autoptimize'); ?></th> |
| 81 |
<td><input type="checkbox" name="autoptimize_html" <?php echo get_option('autoptimize_html')?'checked="checked" ':''; ?>/></td> |
| 82 |
</tr> |
| 83 |
<tr valign="top"> |
| 84 |
<th scope="row"><?php _e('Keep HTML comments?','autoptimize'); ?></th> |
| 85 |
<td><label for="autoptimize_html_keepcomments"><input type="checkbox" name="autoptimize_html_keepcomments" <?php echo get_option('autoptimize_html_keepcomments')?'checked="checked" ':''; ?>/> |
| 86 |
<?php _e('Enable this if you want HTML comments to remain in the page, needed for e.g. AdSense to function properly.','autoptimize'); ?></label></td> |
| 87 |
</tr> |
| 88 |
</table> |
| 89 |
|
| 90 |
<h3><?php _e('JavaScript Options','autoptimize'); ?></h3> |
| 91 |
<table class="form-table"> |
| 92 |
<tr valign="top"> |
| 93 |
<th scope="row"><?php _e('Optimize JavaScript Code?','autoptimize'); ?></th> |
| 94 |
<td><input type="checkbox" name="autoptimize_js" <?php echo get_option('autoptimize_js')?'checked="checked" ':''; ?>/></td> |
| 95 |
</tr> |
| 96 |
<tr valign="top" class="hidden ao_adv"> |
| 97 |
<th scope="row"><?php _e('Force JavaScript in <head>?','autoptimize'); ?></th> |
| 98 |
<td><label for="autoptimize_js_forcehead"><input type="checkbox" name="autoptimize_js_forcehead" <?php echo get_option('autoptimize_js_forcehead')?'checked="checked" ':''; ?>/> |
| 99 |
<?php _e('For performance reasons it is better to include JavaScript at the bottom of HTML, but this sometimes breaks things. Especially useful for jQuery-based themes.','autoptimize'); ?></label></td> |
| 100 |
</tr> |
| 101 |
<tr valign="top" class="hidden ao_adv"> |
| 102 |
<th scope="row"><?php _e('Look for scripts only in <head>?','autoptimize'); ?></th> |
| 103 |
<td><label for="autoptimize_js_justhead"><input type="checkbox" name="autoptimize_js_justhead" <?php echo get_option('autoptimize_js_justhead')?'checked="checked" ':''; ?>/> |
| 104 |
<?php _e('Mostly usefull in combination with previous option when using jQuery-based templates, but might help keeping cache size under control.','autoptimize'); ?></label></td> |
| 105 |
</tr> |
| 106 |
<tr valign="top" class="hidden ao_adv"> |
| 107 |
<th scope="row"><?php _e('Exclude scripts from Autoptimize:','autoptimize'); ?></th> |
| 108 |
<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 /> |
| 109 |
<?php _e('A comma-seperated list of scripts you want to exclude from being optimized, for example \'whatever.js, another.js\' (without the quotes) to exclude those scripts from being aggregated and minimized by Autoptimize.','autoptimize'); ?></label></td> |
| 110 |
</tr> |
| 111 |
<tr valign="top" class="hidden ao_adv"> |
| 112 |
<th scope="row"><?php _e('Add try-catch wrapping?','autoptimize'); ?></th> |
| 113 |
<td><label for="autoptimize_js_trycatch"><input type="checkbox" name="autoptimize_js_trycatch" <?php echo get_option('autoptimize_js_trycatch')?'checked="checked" ':''; ?>/> |
| 114 |
<?php _e('If your scripts break because of an script error, you might want to try this.','autoptimize'); ?></label></td> |
| 115 |
</tr> |
| 116 |
</table> |
| 117 |
|
| 118 |
<h3><?php _e('CSS Options','autoptimize'); ?></h3> |
| 119 |
<table class="form-table"> |
| 120 |
<tr valign="top"> |
| 121 |
<th scope="row"><?php _e('Optimize CSS Code?','autoptimize'); ?></th> |
| 122 |
<td><input type="checkbox" name="autoptimize_css" <?php echo get_option('autoptimize_css')?'checked="checked" ':''; ?>/></td> |
| 123 |
</tr> |
| 124 |
<tr valign="top"> |
| 125 |
<th scope="row"><?php _e('Generate data: URIs for images?','autoptimize'); ?></th> |
| 126 |
<td><label for="autoptimize_css_datauris"><input type="checkbox" name="autoptimize_css_datauris" <?php echo get_option('autoptimize_css_datauris')?'checked="checked" ':''; ?>/> |
| 127 |
<?php _e('Enable this to include small background-images in the CSS itself instead of as seperate downloads.','autoptimize'); ?></label></td> |
| 128 |
</tr> |
| 129 |
<tr valign="top" class="hidden ao_adv"> |
| 130 |
<th scope="row"><?php _e('Look for styles only in <head>?','autoptimize'); ?></th> |
| 131 |
<td><label for="autoptimize_css_justhead"><input type="checkbox" name="autoptimize_css_justhead" <?php echo get_option('autoptimize_css_justhead')?'checked="checked" ':''; ?>/> |
| 132 |
<?php _e('Don\'t autoptimize CSS outside the head-section. If the cache gets big, you might want to enable this.','autoptimize'); ?></label></td> |
| 133 |
</tr> |
| 134 |
<tr valign="top" class="hidden ao_adv"> |
| 135 |
<th scope="row"><?php _e('Defer CSS loading?','autoptimize'); ?></th> |
| 136 |
<td><label for="autoptimize_css_defer"><input type="checkbox" name="autoptimize_css_defer" id="autoptimize_css_defer" <?php echo get_option('autoptimize_css_defer')?'checked="checked" ':''; ?>/> |
| 137 |
<?php _e('Load optimized CSS only after page load (disables CSS inlining). <strong>Warning</strong>: <a href="http://wordpress.org/plugins/autoptimize/faq/" target="_blank">check the FAQ</a> before activating this option!','autoptimize'); ?></label></td> |
| 138 |
</tr> |
| 139 |
<tr valign="top" class="hidden ao_adv"> |
| 140 |
<th scope="row"><?php _e('Inline all CSS?','autoptimize'); ?></th> |
| 141 |
<td><label for="autoptimize_css_inline"><input type="checkbox" id="autoptimize_css_inline" name="autoptimize_css_inline" <?php echo get_option('autoptimize_css_inline')?'checked="checked" ':''; ?>/> |
| 142 |
<?php _e('Inlining all CSS can improve performance for sites with a low pageviews/ visitor-rate, but may slow down performance otherwise. CSS inlining disables CSS deferring.','autoptimize'); ?></label></td> |
| 143 |
</tr> |
| 144 |
<tr valign="top" class="hidden ao_adv"> |
| 145 |
<th scope="row"><?php _e('Exclude CSS from Autoptimize:','autoptimize'); ?></th> |
| 146 |
<td><label for="autoptimize_css_exclude"><input type="text" style="width:100%;" name="autoptimize_css_exclude" value="<?php echo get_option('autoptimize_css_exclude','admin-bar.min.css, dashicons.min.css'); ?>"/><br /> |
| 147 |
<?php _e('A comma-seperated list of CSS you want to exclude from being optimized.','autoptimize'); ?></label></td> |
| 148 |
</tr> |
| 149 |
</table> |
| 150 |
|
| 151 |
<h3><?php _e('CDN Options','autoptimize'); ?></h3> |
| 152 |
<table class="form-table"> |
| 153 |
<tr valign="top"> |
| 154 |
<th scope="row"><?php _e('CDN Base URL','autoptimize'); ?></th> |
| 155 |
<td><label for="autoptimize_url"><input id="cdn_url" type="url" name="autoptimize_cdn_url" pattern="^(https?:)?\/\/([\da-z\.-]+)\.([\da-z\.]{2,6})([\/\w \.-]*)*\/?$" style="width:100%" value="<?php $it = get_option('autoptimize_cdn_url','');echo htmlentities($it); ?>" /><br /> |
| 156 |
<?php _e('Enter your CDN blog root directory URL if you want to enable CDN for images referenced in the CSS.','autoptimize'); ?></label></td> |
| 157 |
</tr> |
| 158 |
</table> |
| 159 |
|
| 160 |
<h3 class="hidden ao_adv"><?php _e('Cache Info','autoptimize'); ?></h3> |
| 161 |
<table class="form-table" > |
| 162 |
<tr valign="top" class="hidden ao_adv"> |
| 163 |
<th scope="row"><?php _e('Cache folder','autoptimize'); ?></th> |
| 164 |
<td><?php echo htmlentities(AUTOPTIMIZE_CACHE_DIR); ?></td> |
| 165 |
</tr> |
| 166 |
<tr valign="top" class="hidden ao_adv"> |
| 167 |
<th scope="row"><?php _e('Can we write?','autoptimize'); ?></th> |
| 168 |
<td><?php echo (autoptimizeCache::cacheavail() ? __('Yes','autoptimize') : __('No','autoptimize')); ?></td> |
| 169 |
</tr> |
| 170 |
<tr valign="top" class="hidden ao_adv"> |
| 171 |
<th scope="row"><?php _e('Cached styles and scripts','autoptimize'); ?></th> |
| 172 |
<td><?php echo autoptimizeCache::stats(); ?></td> |
| 173 |
</tr> |
| 174 |
<tr valign="top" class="hidden ao_adv"> |
| 175 |
<th scope="row"><?php _e('Save aggregated script/css as static files?','autoptimize'); ?></th> |
| 176 |
<td><label for="autoptimize_cache_nogzip"><input type="checkbox" name="autoptimize_cache_nogzip" <?php echo get_option('autoptimize_cache_nogzip','1')?'checked="checked" ':''; ?>/> |
| 177 |
<?php _e('By default files saved are static css/js, uncheck this option if your webserver doesn\'t properly handle the compression and expiry.','autoptimize'); ?></label></td> |
| 178 |
</tr> |
| 179 |
</table> |
| 180 |
<input type="hidden" id="autoptimize_show_adv" name="autoptimize_show_adv" value="<?php echo get_option('autoptimize_show_adv','0'); ?>"> |
| 181 |
|
| 182 |
<p class="submit"> |
| 183 |
<input type="submit" class="button-secondary" value="<?php _e('Save Changes','autoptimize') ?>" /> |
| 184 |
<input type="submit" class="button-primary" name="autoptimize_cache_clean" value="<?php _e('Save Changes and Empty Cache','autoptimize') ?>" /> |
| 185 |
</p> |
| 186 |
|
| 187 |
</form> |
| 188 |
</div> |
| 189 |
<div style="float:right;width:30%" id="autoptimize_admin_feed"> |
| 190 |
<div style="margin-left:10px;margin-top:-5px;"> |
| 191 |
<h3> |
| 192 |
<?php _e("futtta about","autoptimize") ?> |
| 193 |
<select id="feed_dropdown" > |
| 194 |
<option value="1"><?php _e("Autoptimize","autoptimize") ?></option> |
| 195 |
<option value="2"><?php _e("WordPress","autoptimize") ?></option> |
| 196 |
<option value="3"><?php _e("Web Technology","autoptimize") ?></option> |
| 197 |
</select> |
| 198 |
</h3> |
| 199 |
<div id="futtta_feed"></div> |
| 200 |
</div> |
| 201 |
<div style="float:right;margin:50px 15px;"><a href="http://blog.futtta.be/2013/10/21/do-not-donate-to-me/" target="_blank"><img width="100px" height="85px" src="<?php echo content_url(); ?>/plugins/autoptimize/classes/external/do_not_donate_smallest.png" title="<?php _e("Do not donate for this plugin!"); ?>"></a></div> |
| 202 |
</div> |
| 203 |
|
| 204 |
<script type="text/javascript"> |
| 205 |
var feed = new Array; |
| 206 |
feed[1]="http://feeds.feedburner.com/futtta_autoptimize"; |
| 207 |
feed[2]="http://feeds.feedburner.com/futtta_wordpress"; |
| 208 |
feed[3]="http://feeds.feedburner.com/futtta_webtech"; |
| 209 |
cookiename="autoptimize_feed"; |
| 210 |
|
| 211 |
jQuery(document).ready(function() { |
| 212 |
jQuery( "#ao_show_adv" ).click(function() { |
| 213 |
jQuery( "#ao_show_adv" ).hide(); |
| 214 |
jQuery( "#ao_hide_adv" ).show(); |
| 215 |
jQuery( ".ao_adv" ).show("slow"); |
| 216 |
jQuery( "input#autoptimize_show_adv" ).val("1"); |
| 217 |
}); |
| 218 |
|
| 219 |
jQuery( "#ao_hide_adv" ).click(function() { |
| 220 |
jQuery( "#ao_hide_adv" ).hide(); |
| 221 |
jQuery( "#ao_show_adv" ).show(); |
| 222 |
jQuery( ".ao_adv" ).hide("slow"); |
| 223 |
jQuery( "input#autoptimize_show_adv" ).val("0"); |
| 224 |
}); |
| 225 |
|
| 226 |
jQuery( "#autoptimize_css_inline" ).change(function() { |
| 227 |
if (this.checked) { |
| 228 |
jQuery("#autoptimize_css_defer").prop("checked",false); |
| 229 |
} |
| 230 |
}); |
| 231 |
|
| 232 |
jQuery( "#autoptimize_css_defer" ).change(function() { |
| 233 |
if (this.checked) { |
| 234 |
jQuery("#autoptimize_css_inline").prop("checked",false); |
| 235 |
} |
| 236 |
}); |
| 237 |
|
| 238 |
jQuery("#feed_dropdown").change(function() { show_feed(jQuery("#feed_dropdown").val()) }); |
| 239 |
feedid=jQuery.cookie(cookiename); |
| 240 |
if(typeof(feedid) !== "string") feedid=1; |
| 241 |
show_feed(feedid); |
| 242 |
}) |
| 243 |
|
| 244 |
function show_feed(id) { |
| 245 |
jQuery('#futtta_feed').rssfeed(feed[id], { |
| 246 |
<?php if ( is_ssl() ) echo "ssl: true,"; ?> |
| 247 |
limit: 4, |
| 248 |
date: true, |
| 249 |
header: false |
| 250 |
}); |
| 251 |
jQuery("#feed_dropdown").val(id); |
| 252 |
jQuery.cookie(cookiename,id,{ expires: 365 }); |
| 253 |
} |
| 254 |
</script> |
| 255 |
</div> |
| 256 |
|
| 257 |
<?php |
| 258 |
} |
| 259 |
|
| 260 |
public function addmenu() |
| 261 |
{ |
| 262 |
$hook=add_options_page(__('Autoptimize Options','autoptimize'),'Autoptimize','manage_options','autoptimize',array($this,'show')); |
| 263 |
add_action( 'admin_print_scripts-'.$hook,array($this,'autoptimize_admin_scripts')); |
| 264 |
add_action( 'admin_print_styles-'.$hook,array($this,'autoptimize_admin_styles')); |
| 265 |
} |
| 266 |
|
| 267 |
public function autoptimize_admin_scripts() { |
| 268 |
wp_enqueue_script('jqzrssfeed', plugins_url('/external/js/jquery.zrssfeed.min.js', __FILE__), array('jquery'),null,true); |
| 269 |
wp_enqueue_script('jqcookie', plugins_url('/external/js/jquery.cookie.min.js', __FILE__), array('jquery'),null,true); |
| 270 |
} |
| 271 |
|
| 272 |
public function autoptimize_admin_styles() { |
| 273 |
wp_enqueue_style('zrssfeed', plugins_url('/external/js/jquery.zrssfeed.css', __FILE__)); |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
public function registersettings() |
| 278 |
{ |
| 279 |
register_setting('autoptimize','autoptimize_html'); |
| 280 |
register_setting('autoptimize','autoptimize_html_keepcomments'); |
| 281 |
register_setting('autoptimize','autoptimize_js'); |
| 282 |
register_setting('autoptimize','autoptimize_js_exclude'); |
| 283 |
register_setting('autoptimize','autoptimize_js_trycatch'); |
| 284 |
register_setting('autoptimize','autoptimize_js_justhead'); |
| 285 |
register_setting('autoptimize','autoptimize_js_forcehead'); |
| 286 |
register_setting('autoptimize','autoptimize_css'); |
| 287 |
register_setting('autoptimize','autoptimize_css_exclude'); |
| 288 |
register_setting('autoptimize','autoptimize_css_justhead'); |
| 289 |
register_setting('autoptimize','autoptimize_css_datauris'); |
| 290 |
register_setting('autoptimize','autoptimize_css_defer'); |
| 291 |
register_setting('autoptimize','autoptimize_css_inline'); |
| 292 |
register_setting('autoptimize','autoptimize_cdn_url'); |
| 293 |
register_setting('autoptimize','autoptimize_cache_clean'); |
| 294 |
register_setting('autoptimize','autoptimize_cache_nogzip'); |
| 295 |
register_setting('autoptimize','autoptimize_show_adv'); |
| 296 |
} |
| 297 |
|
| 298 |
public function setmeta($links,$file=null) |
| 299 |
{ |
| 300 |
//Inspired on http://wpengineer.com/meta-links-for-wordpress-plugins/ |
| 301 |
//Do it only once - saves time |
| 302 |
static $plugin; |
| 303 |
if(empty($plugin)) |
| 304 |
$plugin = plugin_basename(WP_PLUGIN_DIR.'/autoptimize/autoptimize.php'); |
| 305 |
|
| 306 |
if($file===null) |
| 307 |
{ |
| 308 |
//2.7 |
| 309 |
$settings_link = sprintf('<a href="options-general.php?page=autoptimize">%s</a>', __('Settings')); |
| 310 |
array_unshift($links,$settings_link); |
| 311 |
}else{ |
| 312 |
//2.8 |
| 313 |
//If it's us, add the link |
| 314 |
if($file === $plugin) |
| 315 |
{ |
| 316 |
$newlink = array(sprintf('<a href="options-general.php?page=autoptimize">%s</a>',__('Settings'))); |
| 317 |
$links = array_merge($links,$newlink); |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
return $links; |
| 322 |
} |
| 323 |
|
| 324 |
public function get($key) |
| 325 |
{ |
| 326 |
if(!is_array($this->config)) |
| 327 |
{ |
| 328 |
//Default config |
| 329 |
$config = array('autoptimize_html' => 0, |
| 330 |
'autoptimize_html_keepcomments' => 0, |
| 331 |
'autoptimize_js' => 0, |
| 332 |
'autoptimize_js_exclude' => "s_sid, smowtion_size, sc_project, WAU_, wau_add, comment-form-quicktags, edToolbar, ch_client", |
| 333 |
'autoptimize_js_trycatch' => 0, |
| 334 |
'autoptimize_js_justhead' => 0, |
| 335 |
'autoptimize_js_forcehead' => 0, |
| 336 |
'autoptimize_css' => 0, |
| 337 |
'autoptimize_css_exclude' => "admin-bar.min.css, dashicons.min.css", |
| 338 |
'autoptimize_css_justhead' => 0, |
| 339 |
'autoptimize_css_defer' => 0, |
| 340 |
'autoptimize_css_inline' => 0, |
| 341 |
'autoptimize_css_datauris' => 0, |
| 342 |
'autoptimize_cdn_url' => "", |
| 343 |
'autoptimize_cache_nogzip' => 1, |
| 344 |
'autoptimize_show_adv' => 0 |
| 345 |
); |
| 346 |
|
| 347 |
//Override with user settings |
| 348 |
foreach(array_keys($config) as $name) |
| 349 |
{ |
| 350 |
$conf = get_option($name); |
| 351 |
if($conf!==false) |
| 352 |
{ |
| 353 |
//It was set before! |
| 354 |
$config[$name] = $conf; |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
//Save for next question |
| 359 |
$this->config = $config; |
| 360 |
} |
| 361 |
|
| 362 |
if(isset($this->config[$key])) |
| 363 |
return $this->config[$key]; |
| 364 |
|
| 365 |
return false; |
| 366 |
} |
| 367 |
} |
| 368 |
|