| 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 |
<form method="post" action="options.php"> |
| 53 |
<?php settings_fields('autoptimize'); ?> |
| 54 |
|
| 55 |
<h3><?php _e('HTML Options','autoptimize'); ?></h3> |
| 56 |
<table class="form-table"> |
| 57 |
<tr valign="top"> |
| 58 |
<th scope="row"><?php _e('Optimize HTML Code?','autoptimize'); ?></th> |
| 59 |
<td><input type="checkbox" name="autoptimize_html" <?php echo get_option('autoptimize_html')?'checked="checked" ':''; ?>/></td> |
| 60 |
</tr> |
| 61 |
</table> |
| 62 |
|
| 63 |
<h3><?php _e('JavaScript Options','autoptimize'); ?></h3> |
| 64 |
<table class="form-table"> |
| 65 |
<tr valign="top"> |
| 66 |
<th scope="row"><?php _e('Optimize JavaScript Code?','autoptimize'); ?></th> |
| 67 |
<td><input type="checkbox" name="autoptimize_js" <?php echo get_option('autoptimize_js')?'checked="checked" ':''; ?>/></td> |
| 68 |
</tr> |
| 69 |
<tr valign="top"> |
| 70 |
<th scope="row"><?php _e('Look for scripts only in <head>?','autoptimize'); ?></th> |
| 71 |
<td><label for="autoptimize_js_justhead"><input type="checkbox" name="autoptimize_js_justhead" <?php echo get_option('autoptimize_js_justhead')?'checked="checked" ':''; ?>/> |
| 72 |
<?php _e('Disabled by default. If the cache gets big, you might want to enable this.','autoptimize'); ?></label></td> |
| 73 |
</tr> |
| 74 |
<tr valign="top"> |
| 75 |
<th scope="row"><?php _e('Add try-catch wrapping?','autoptimize'); ?></th> |
| 76 |
<td><label for="autoptimize_js_trycatch"><input type="checkbox" name="autoptimize_js_trycatch" <?php echo get_option('autoptimize_js_trycatch')?'checked="checked" ':''; ?>/> |
| 77 |
<?php _e('Disabled by default. If your scripts break because of an script error, you might want to try this.','autoptimize'); ?></label></td> |
| 78 |
</tr> |
| 79 |
</table> |
| 80 |
|
| 81 |
<h3><?php _e('CSS Options','autoptimize'); ?></h3> |
| 82 |
<table class="form-table"> |
| 83 |
<tr valign="top"> |
| 84 |
<th scope="row"><?php _e('Optimize CSS Code?','autoptimize'); ?></th> |
| 85 |
<td><input type="checkbox" name="autoptimize_css" <?php echo get_option('autoptimize_css')?'checked="checked" ':''; ?>/></td> |
| 86 |
</tr> |
| 87 |
<tr valign="top"> |
| 88 |
<th scope="row"><?php _e('Look for styles on just <head>?','autoptimize'); ?></th> |
| 89 |
<td><label for="autoptimize_css_justhead"><input type="checkbox" name="autoptimize_css_justhead" <?php echo get_option('autoptimize_css_justhead')?'checked="checked" ':''; ?>/> |
| 90 |
<?php _e('Disabled by default. If the cache gets big, you might want to enable this.','autoptimize'); ?></label></td> |
| 91 |
</tr> |
| 92 |
</table> |
| 93 |
|
| 94 |
<h3><?php _e('Cache Info','autoptimize'); ?></h3> |
| 95 |
<table class="form-table"> |
| 96 |
<tr valign="top"> |
| 97 |
<th scope="row"><?php _e('Cache folder','autoptimize'); ?></th> |
| 98 |
<td><?php echo htmlentities(AUTOPTIMIZE_CACHE_DIR); ?></td> |
| 99 |
</tr> |
| 100 |
<tr valign="top"> |
| 101 |
<th scope="row"><?php _e('Can we write?','autoptimize'); ?></th> |
| 102 |
<td><?php echo (autoptimizeCache::cacheavail() ? __('Yes','autoptimize') : __('No','autoptimize')); ?></td> |
| 103 |
</tr> |
| 104 |
<tr valign="top"> |
| 105 |
<th scope="row"><?php _e('Cached styles and scripts','autoptimize'); ?></th> |
| 106 |
<td><?php echo autoptimizeCache::stats(); ?></td> |
| 107 |
</tr> |
| 108 |
</table> |
| 109 |
|
| 110 |
</table> |
| 111 |
|
| 112 |
<p class="submit"> |
| 113 |
<input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" /> |
| 114 |
<input type="submit" name="autoptimize_cache_clean" value="<?php _e('Save Changes and Empty Cache') ?>" /> |
| 115 |
</p> |
| 116 |
|
| 117 |
</form> |
| 118 |
</div> |
| 119 |
<?php |
| 120 |
} |
| 121 |
|
| 122 |
public function addmenu() |
| 123 |
{ |
| 124 |
add_options_page(__('Autoptimize Options','autoptimize'),'Autoptimize',8,'autoptimize',array($this,'show')); |
| 125 |
} |
| 126 |
|
| 127 |
public function registersettings() |
| 128 |
{ |
| 129 |
register_setting('autoptimize','autoptimize_html'); |
| 130 |
register_setting('autoptimize','autoptimize_js'); |
| 131 |
register_setting('autoptimize','autoptimize_js_trycatch'); |
| 132 |
register_setting('autoptimize','autoptimize_js_justhead'); |
| 133 |
register_setting('autoptimize','autoptimize_css'); |
| 134 |
register_setting('autoptimize','autoptimize_css_justhead'); |
| 135 |
register_setting('autoptimize','autoptimize_cache_clean'); |
| 136 |
} |
| 137 |
|
| 138 |
public function setmeta($links,$file=null) |
| 139 |
{ |
| 140 |
//Inspired on http://wpengineer.com/meta-links-for-wordpress-plugins/ |
| 141 |
|
| 142 |
//Do it only once - saves time |
| 143 |
static $plugin; |
| 144 |
if(empty($plugin)) |
| 145 |
$plugin = plugin_basename(WP_PLUGIN_DIR.'/autoptimize/autoptimize.php'); |
| 146 |
|
| 147 |
if($file===null) |
| 148 |
{ |
| 149 |
//2.7 |
| 150 |
$settings_link = sprintf('<a href="options-general.php?page=autoptimize">%s</a>', __('Settings')); |
| 151 |
array_unshift($links,$settings_link); |
| 152 |
}else{ |
| 153 |
//2.8 |
| 154 |
//If it's us, add the link |
| 155 |
if($file === $plugin) |
| 156 |
{ |
| 157 |
$newlink = array(sprintf('<a href="options-general.php?page=autoptimize">%s</a>',__('Settings'))); |
| 158 |
$links = array_merge($links,$newlink); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
return $links; |
| 163 |
} |
| 164 |
|
| 165 |
public function get($key) |
| 166 |
{ |
| 167 |
if(!is_array($this->config)) |
| 168 |
{ |
| 169 |
//Default config |
| 170 |
$config = array('autoptimize_html' => 0, |
| 171 |
'autoptimize_js' => 0, |
| 172 |
'autoptimize_js_trycatch' => 0, |
| 173 |
'autoptimize_js_justhead' => 0, |
| 174 |
'autoptimize_css' => 0, |
| 175 |
'autoptimize_css_justhead' => 0); |
| 176 |
|
| 177 |
//Override with user settings |
| 178 |
foreach(array_keys($config) as $name) |
| 179 |
{ |
| 180 |
$conf = get_option($name); |
| 181 |
if($conf!==false) |
| 182 |
{ |
| 183 |
//It was set before! |
| 184 |
$config[$name] = $conf; |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
//Save for next question |
| 189 |
$this->config = $config; |
| 190 |
} |
| 191 |
|
| 192 |
if(isset($this->config[$key])) |
| 193 |
return $this->config[$key]; |
| 194 |
|
| 195 |
return false; |
| 196 |
} |
| 197 |
} |
| 198 |
|