| 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 |
//Set meta info |
| 17 |
if(function_exists('plugin_row_meta')) |
| 18 |
{ |
| 19 |
//2.8+ |
| 20 |
add_filter('plugin_row_meta',array($this,'setmeta'),10,2); |
| 21 |
}elseif(function_exists('post_class')){ |
| 22 |
//2.7 |
| 23 |
$plugin = plugin_basename(WP_PLUGIN_DIR.'/autoptimize/autoptimize.php'); |
| 24 |
add_filter('plugin_action_links_'.$plugin,array($this,'setmeta')); |
| 25 |
} |
| 26 |
//Clean cache? |
| 27 |
if(get_option('autoptimize_cache_clean')) |
| 28 |
{ |
| 29 |
autoptimizeCache::clearall(); |
| 30 |
update_option('autoptimize_cache_clean',0); |
| 31 |
} |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
static public function instance() |
| 36 |
{ |
| 37 |
//Only one instance |
| 38 |
if (self::$instance == null) |
| 39 |
{ |
| 40 |
self::$instance = new autoptimizeConfig(); |
| 41 |
} |
| 42 |
|
| 43 |
return self::$instance; |
| 44 |
} |
| 45 |
|
| 46 |
public function show() |
| 47 |
{ |
| 48 |
?> |
| 49 |
<div class="wrap"> |
| 50 |
<h2><?php _e('Autoptimize Settings','autoptimize'); ?></h2> |
| 51 |
|
| 52 |
<div style="position:absolute;right:50px;background:#FFFFE0;border:1px solid #E6DB55;padding:5px;text-align:center;"> |
| 53 |
Like Autoptimize? |
| 54 |
<form action="https://www.paypal.com/cgi-bin/webscr" method="post"> |
| 55 |
<input type="hidden" name="cmd" value="_donations"> |
| 56 |
<input type="hidden" name="business" value="webmaster@turl.com.ar"> |
| 57 |
<input type="hidden" name="lc" value="AR"> |
| 58 |
<input type="hidden" name="item_name" value="Autoptimize"> |
| 59 |
<input type="hidden" name="item_number" value="autoptimize"> |
| 60 |
<input type="hidden" name="currency_code" value="USD"> |
| 61 |
<input type="hidden" name="bn" value="PP-DonationsBF:btn_donate_SM.gif:NonHostedGuest"> |
| 62 |
<input type="image" src="https://www.paypal.com/en_US/i/btn/btn_donate_SM.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!"> |
| 63 |
<img alt="" border="0" src="https://www.paypal.com/es_XC/i/scr/pixel.gif" width="1" height="1"> |
| 64 |
</form> |
| 65 |
Buy me a coffee :-) |
| 66 |
</div> |
| 67 |
|
| 68 |
<form method="post" action="options.php"> |
| 69 |
<?php settings_fields('autoptimize'); ?> |
| 70 |
|
| 71 |
<h3><?php _e('HTML Options','autoptimize'); ?></h3> |
| 72 |
<table class="form-table"> |
| 73 |
<tr valign="top"> |
| 74 |
<th scope="row"><?php _e('Optimize HTML Code?','autoptimize'); ?></th> |
| 75 |
<td><input type="checkbox" name="autoptimize_html" <?php echo get_option('autoptimize_html')?'checked="checked" ':''; ?>/></td> |
| 76 |
</tr> |
| 77 |
<tr valign="top"> |
| 78 |
<th scope="row"><?php _e('Keep HTML comments?','autoptimize'); ?></th> |
| 79 |
<td><label for="autoptimize_html_keepcomments"><input type="checkbox" name="autoptimize_html_keepcomments" <?php echo get_option('autoptimize_html_keepcomments')?'checked="checked" ':''; ?>/> |
| 80 |
<?php _e('Disabled by default. Enable this if you want HTML comments to remain in the page.','autoptimize'); ?></label></td> |
| 81 |
</tr> |
| 82 |
</table> |
| 83 |
|
| 84 |
<h3><?php _e('JavaScript Options','autoptimize'); ?></h3> |
| 85 |
<table class="form-table"> |
| 86 |
<tr valign="top"> |
| 87 |
<th scope="row"><?php _e('Optimize JavaScript Code?','autoptimize'); ?></th> |
| 88 |
<td><input type="checkbox" name="autoptimize_js" <?php echo get_option('autoptimize_js')?'checked="checked" ':''; ?>/></td> |
| 89 |
</tr> |
| 90 |
<tr valign="top"> |
| 91 |
<th scope="row"><?php _e('Look for scripts only in <head>?','autoptimize'); ?></th> |
| 92 |
<td><label for="autoptimize_js_justhead"><input type="checkbox" name="autoptimize_js_justhead" <?php echo get_option('autoptimize_js_justhead')?'checked="checked" ':''; ?>/> |
| 93 |
<?php _e('Disabled by default. If the cache gets big, you might want to enable this.','autoptimize'); ?></label></td> |
| 94 |
</tr> |
| 95 |
<tr valign="top"> |
| 96 |
<th scope="row"><?php _e('Add try-catch wrapping?','autoptimize'); ?></th> |
| 97 |
<td><label for="autoptimize_js_trycatch"><input type="checkbox" name="autoptimize_js_trycatch" <?php echo get_option('autoptimize_js_trycatch')?'checked="checked" ':''; ?>/> |
| 98 |
<?php _e('Disabled by default. If your scripts break because of an script error, you might want to try this.','autoptimize'); ?></label></td> |
| 99 |
</tr> |
| 100 |
<tr valign="top"> |
| 101 |
<th scope="row"><?php _e('Use YUI compression?','autoptimize'); ?></th> |
| 102 |
<td><label for="autoptimize_js_yui"><input type="checkbox" name="autoptimize_js_yui" <?php echo get_option('autoptimize_js_yui')?'checked="checked" ':''; ?>/> |
| 103 |
<?php _e('Disabled by default. Read [autoptimize]/yui/README.txt for more information.','autoptimize'); ?></label></td> |
| 104 |
</tr> |
| 105 |
</table> |
| 106 |
|
| 107 |
<h3><?php _e('CSS Options','autoptimize'); ?></h3> |
| 108 |
<table class="form-table"> |
| 109 |
<tr valign="top"> |
| 110 |
<th scope="row"><?php _e('Optimize CSS Code?','autoptimize'); ?></th> |
| 111 |
<td><input type="checkbox" name="autoptimize_css" <?php echo get_option('autoptimize_css')?'checked="checked" ':''; ?>/></td> |
| 112 |
</tr> |
| 113 |
<tr valign="top"> |
| 114 |
<th scope="row"><?php _e('Look for styles on just <head>?','autoptimize'); ?></th> |
| 115 |
<td><label for="autoptimize_css_justhead"><input type="checkbox" name="autoptimize_css_justhead" <?php echo get_option('autoptimize_css_justhead')?'checked="checked" ':''; ?>/> |
| 116 |
<?php _e('Disabled by default. If the cache gets big, you might want to enable this.','autoptimize'); ?></label></td> |
| 117 |
</tr> |
| 118 |
<tr valign="top"> |
| 119 |
<th scope="row"><?php _e('Generate data: URIs for images?','autoptimize'); ?></th> |
| 120 |
<td><label for="autoptimize_css_datauris"><input type="checkbox" name="autoptimize_css_datauris" <?php echo get_option('autoptimize_css_datauris')?'checked="checked" ':''; ?>/> |
| 121 |
<?php _e('Disabled by default. Enable this to include images on the CSS itself.','autoptimize'); ?></label></td> |
| 122 |
</tr> |
| 123 |
<tr valign="top"> |
| 124 |
<th scope="row"><?php _e('Use YUI compression?','autoptimize'); ?></th> |
| 125 |
<td><label for="autoptimize_css_yui"><input type="checkbox" name="autoptimize_css_yui" <?php echo get_option('autoptimize_css_yui')?'checked="checked" ':''; ?>/> |
| 126 |
<?php _e('Disabled by default. Read [autoptimize]/yui/README.txt for more information.','autoptimize'); ?></label></td> |
| 127 |
</tr> |
| 128 |
</table> |
| 129 |
|
| 130 |
<h3><?php _e('CDN Options','autoptimize'); ?></h3> |
| 131 |
<table class="form-table"> |
| 132 |
<tr valign="top"> |
| 133 |
<th scope="row"><?php _e('Rewrite JavaScript URLs?','autoptimize'); ?></th> |
| 134 |
<td><label for="autoptimize_cdn_js"><input type="checkbox" name="autoptimize_cdn_js" <?php echo get_option('autoptimize_cdn_js')?'checked="checked" ':''; ?>/> |
| 135 |
<?php _e('Disabled by default. Do not enable this unless you know what you are doing.','autoptimize'); ?></label></td> |
| 136 |
</tr> |
| 137 |
<tr valign="top"> |
| 138 |
<th scope="row"><?php _e('JavaScript Base URL','autoptimize'); ?></th> |
| 139 |
<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:get_bloginfo('siteurl')); ?>" /> |
| 140 |
<?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> |
| 141 |
</tr> |
| 142 |
<tr valign="top"> |
| 143 |
<th scope="row"><?php _e('Rewrite CSS URLs?','autoptimize'); ?></th> |
| 144 |
<td><label for="autoptimize_cdn_css"><input type="checkbox" name="autoptimize_cdn_css" <?php echo get_option('autoptimize_cdn_css')?'checked="checked" ':''; ?>/> |
| 145 |
<?php _e('Disabled by default. Do not enable this unless you know what you are doing.','autoptimize'); ?></label></td> |
| 146 |
</tr> |
| 147 |
<tr valign="top"> |
| 148 |
<th scope="row"><?php _e('CSS Base URL','autoptimize'); ?></th> |
| 149 |
<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:get_bloginfo('siteurl')); ?>" /> |
| 150 |
<?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> |
| 151 |
</tr> |
| 152 |
<tr valign="top"> |
| 153 |
<th scope="row"><?php _e('Rewrite Image URLs?','autoptimize'); ?></th> |
| 154 |
<td><label for="autoptimize_cdn_img"><input type="checkbox" name="autoptimize_cdn_img" <?php echo get_option('autoptimize_cdn_img')?'checked="checked" ':''; ?>/> |
| 155 |
<?php _e('Disabled by default. Do not enable this unless you know what you are doing.','autoptimize'); ?></label></td> |
| 156 |
</tr> |
| 157 |
<tr valign="top"> |
| 158 |
<th scope="row"><?php _e('Image Base URL','autoptimize'); ?></th> |
| 159 |
<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:get_bloginfo('siteurl')); ?>" /> |
| 160 |
<?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> |
| 161 |
</tr> |
| 162 |
</table> |
| 163 |
|
| 164 |
<h3><?php _e('Cache Info','autoptimize'); ?></h3> |
| 165 |
<table class="form-table"> |
| 166 |
<tr valign="top"> |
| 167 |
<th scope="row"><?php _e('Cache folder','autoptimize'); ?></th> |
| 168 |
<td><?php echo htmlentities(AUTOPTIMIZE_CACHE_DIR); ?></td> |
| 169 |
</tr> |
| 170 |
<tr valign="top"> |
| 171 |
<th scope="row"><?php _e('Can we write?','autoptimize'); ?></th> |
| 172 |
<td><?php echo (autoptimizeCache::cacheavail() ? __('Yes','autoptimize') : __('No','autoptimize')); ?></td> |
| 173 |
</tr> |
| 174 |
<tr valign="top"> |
| 175 |
<th scope="row"><?php _e('Cached styles and scripts','autoptimize'); ?></th> |
| 176 |
<td><?php echo autoptimizeCache::stats(); ?></td> |
| 177 |
</tr> |
| 178 |
<tr valign="top"> |
| 179 |
<th scope="row"><?php _e('Do not compress cache files','autoptimize'); ?></th> |
| 180 |
<td><label for="autoptimize_cache_nogzip"><input type="checkbox" name="autoptimize_cache_nogzip" <?php echo get_option('autoptimize_cache_nogzip')?'checked="checked" ':''; ?>/> |
| 181 |
<?php _e('Disabled by default. Enable this if you want to compress the served files using your webserver.','autoptimize'); ?></label></td> |
| 182 |
</tr> |
| 183 |
</table> |
| 184 |
|
| 185 |
</table> |
| 186 |
|
| 187 |
<p class="submit"> |
| 188 |
<input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" /> |
| 189 |
<input type="submit" name="autoptimize_cache_clean" value="<?php _e('Save Changes and Empty Cache') ?>" /> |
| 190 |
</p> |
| 191 |
|
| 192 |
</form> |
| 193 |
</div> |
| 194 |
<?php |
| 195 |
} |
| 196 |
|
| 197 |
public function addmenu() |
| 198 |
{ |
| 199 |
add_options_page(__('Autoptimize Options','autoptimize'),'Autoptimize',8,'autoptimize',array($this,'show')); |
| 200 |
} |
| 201 |
|
| 202 |
public function registersettings() |
| 203 |
{ |
| 204 |
register_setting('autoptimize','autoptimize_html'); |
| 205 |
register_setting('autoptimize','autoptimize_html_keepcomments'); |
| 206 |
register_setting('autoptimize','autoptimize_js'); |
| 207 |
register_setting('autoptimize','autoptimize_js_trycatch'); |
| 208 |
register_setting('autoptimize','autoptimize_js_justhead'); |
| 209 |
register_setting('autoptimize','autoptimize_js_yui'); |
| 210 |
register_setting('autoptimize','autoptimize_css'); |
| 211 |
register_setting('autoptimize','autoptimize_css_justhead'); |
| 212 |
register_setting('autoptimize','autoptimize_css_datauris'); |
| 213 |
register_setting('autoptimize','autoptimize_css_yui'); |
| 214 |
register_setting('autoptimize','autoptimize_cdn_js'); |
| 215 |
register_setting('autoptimize','autoptimize_cdn_js_url'); |
| 216 |
register_setting('autoptimize','autoptimize_cdn_css'); |
| 217 |
register_setting('autoptimize','autoptimize_cdn_css_url'); |
| 218 |
register_setting('autoptimize','autoptimize_cdn_img'); |
| 219 |
register_setting('autoptimize','autoptimize_cdn_img_url'); |
| 220 |
register_setting('autoptimize','autoptimize_cache_clean'); |
| 221 |
register_setting('autoptimize','autoptimize_cache_nogzip'); |
| 222 |
} |
| 223 |
|
| 224 |
public function setmeta($links,$file=null) |
| 225 |
{ |
| 226 |
//Inspired on http://wpengineer.com/meta-links-for-wordpress-plugins/ |
| 227 |
|
| 228 |
//Do it only once - saves time |
| 229 |
static $plugin; |
| 230 |
if(empty($plugin)) |
| 231 |
$plugin = plugin_basename(WP_PLUGIN_DIR.'/autoptimize/autoptimize.php'); |
| 232 |
|
| 233 |
if($file===null) |
| 234 |
{ |
| 235 |
//2.7 |
| 236 |
$settings_link = sprintf('<a href="options-general.php?page=autoptimize">%s</a>', __('Settings')); |
| 237 |
array_unshift($links,$settings_link); |
| 238 |
}else{ |
| 239 |
//2.8 |
| 240 |
//If it's us, add the link |
| 241 |
if($file === $plugin) |
| 242 |
{ |
| 243 |
$newlink = array(sprintf('<a href="options-general.php?page=autoptimize">%s</a>',__('Settings'))); |
| 244 |
$links = array_merge($links,$newlink); |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
return $links; |
| 249 |
} |
| 250 |
|
| 251 |
public function get($key) |
| 252 |
{ |
| 253 |
if(!is_array($this->config)) |
| 254 |
{ |
| 255 |
//Default config |
| 256 |
$config = array('autoptimize_html' => 0, |
| 257 |
'autoptimize_html_keepcomments' => 0, |
| 258 |
'autoptimize_js' => 0, |
| 259 |
'autoptimize_js_trycatch' => 0, |
| 260 |
'autoptimize_js_justhead' => 0, |
| 261 |
'autoptimize_js_yui' => 0, |
| 262 |
'autoptimize_css' => 0, |
| 263 |
'autoptimize_css_justhead' => 0, |
| 264 |
'autoptimize_css_datauris' => 0, |
| 265 |
'autoptimize_css_yui' => 0, |
| 266 |
'autoptimize_cdn_js' => 0, |
| 267 |
'autoptimize_cdn_js_url' => get_bloginfo('siteurl'), |
| 268 |
'autoptimize_cdn_css' => 0, |
| 269 |
'autoptimize_cdn_css_url' => get_bloginfo('siteurl'), |
| 270 |
'autoptimize_cdn_img' => 0, |
| 271 |
'autoptimize_cdn_img_url' => get_bloginfo('siteurl'), |
| 272 |
'autoptimize_cache_nogzip' => 0, |
| 273 |
); |
| 274 |
|
| 275 |
//Override with user settings |
| 276 |
foreach(array_keys($config) as $name) |
| 277 |
{ |
| 278 |
$conf = get_option($name); |
| 279 |
if($conf!==false) |
| 280 |
{ |
| 281 |
//It was set before! |
| 282 |
$config[$name] = $conf; |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
//Save for next question |
| 287 |
$this->config = $config; |
| 288 |
} |
| 289 |
|
| 290 |
if(isset($this->config[$key])) |
| 291 |
return $this->config[$key]; |
| 292 |
|
| 293 |
return false; |
| 294 |
} |
| 295 |
} |
| 296 |
|