| 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 |
} |
| 27 |
} |
| 28 |
|
| 29 |
static public function instance() |
| 30 |
{ |
| 31 |
//Only one instance |
| 32 |
if (self::$instance == null) |
| 33 |
{ |
| 34 |
self::$instance = new autoptimizeConfig(); |
| 35 |
} |
| 36 |
|
| 37 |
return self::$instance; |
| 38 |
} |
| 39 |
|
| 40 |
public function show() |
| 41 |
{ |
| 42 |
?> |
| 43 |
<div class="wrap"> |
| 44 |
<h2><?php _e('Autoptimize Settings','autoptimize'); ?></h2> |
| 45 |
|
| 46 |
<form method="post" action="options.php"> |
| 47 |
<?php settings_fields('autoptimize'); ?> |
| 48 |
|
| 49 |
<h3><?php _e('HTML Options','autoptimize'); ?></h3> |
| 50 |
<table class="form-table"> |
| 51 |
<tr valign="top"> |
| 52 |
<th scope="row"><?php _e('Optimize HTML Code?','autoptimize'); ?></th> |
| 53 |
<td><input type="checkbox" name="autoptimize_html" <?php echo get_option('autoptimize_html')?'checked="checked" ':''; ?>/></td> |
| 54 |
</tr> |
| 55 |
</table> |
| 56 |
|
| 57 |
<h3><?php _e('JavaScript Options','autoptimize'); ?></h3> |
| 58 |
<table class="form-table"> |
| 59 |
<tr valign="top"> |
| 60 |
<th scope="row"><?php _e('Optimize JavaScript Code?','autoptimize'); ?></th> |
| 61 |
<td><input type="checkbox" name="autoptimize_js" <?php echo get_option('autoptimize_js')?'checked="checked" ':''; ?>/></td> |
| 62 |
</tr> |
| 63 |
<tr valign="top"> |
| 64 |
<th scope="row"><?php _e('Look for scripts only in <head>?','autoptimize'); ?></th> |
| 65 |
<td><label for="autoptimize_js_justhead"><input type="checkbox" name="autoptimize_js_justhead" <?php echo get_option('autoptimize_js_justhead')?'checked="checked" ':''; ?>/> |
| 66 |
<?php _e('Disabled by default. If the cache gets big, you might want to enable this.','autoptimize'); ?></label></td> |
| 67 |
</tr> |
| 68 |
</table> |
| 69 |
|
| 70 |
<h3><?php _e('CSS Options','autoptimize'); ?></h3> |
| 71 |
<table class="form-table"> |
| 72 |
<tr valign="top"> |
| 73 |
<th scope="row"><?php _e('Optimize CSS Code?','autoptimize'); ?></th> |
| 74 |
<td><input type="checkbox" name="autoptimize_css" <?php echo get_option('autoptimize_css')?'checked="checked" ':''; ?>/></td> |
| 75 |
</tr> |
| 76 |
<tr valign="top"> |
| 77 |
<th scope="row"><?php _e('Look for styles on just <head>?','autoptimize'); ?></th> |
| 78 |
<td><label for="autoptimize_css_justhead"><input type="checkbox" name="autoptimize_css_justhead" <?php echo get_option('autoptimize_css_justhead')?'checked="checked" ':''; ?>/> |
| 79 |
<?php _e('Disabled by default. If the cache gets big, you might want to enable this.','autoptimize'); ?></label></td> |
| 80 |
</tr> |
| 81 |
</table> |
| 82 |
|
| 83 |
</table> |
| 84 |
|
| 85 |
<p class="submit"> |
| 86 |
<input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" /> |
| 87 |
</p> |
| 88 |
|
| 89 |
</form> |
| 90 |
</div> |
| 91 |
<?php |
| 92 |
} |
| 93 |
|
| 94 |
public function addmenu() |
| 95 |
{ |
| 96 |
add_options_page(__('Autoptimize Options','autoptimize'),'Autoptimize',8,'autoptimize',array($this,'show')); |
| 97 |
} |
| 98 |
|
| 99 |
public function registersettings() |
| 100 |
{ |
| 101 |
register_setting('autoptimize','autoptimize_html'); |
| 102 |
register_setting('autoptimize','autoptimize_js'); |
| 103 |
register_setting('autoptimize','autoptimize_js_justhead'); |
| 104 |
register_setting('autoptimize','autoptimize_css'); |
| 105 |
register_setting('autoptimize','autoptimize_css_justhead'); |
| 106 |
} |
| 107 |
|
| 108 |
public function setmeta($links,$file=null) |
| 109 |
{ |
| 110 |
//Inspired on http://wpengineer.com/meta-links-for-wordpress-plugins/ |
| 111 |
|
| 112 |
//Do it only once - saves time |
| 113 |
static $plugin; |
| 114 |
if(empty($plugin)) |
| 115 |
$plugin = plugin_basename(WP_PLUGIN_DIR.'/autoptimize/autoptimize.php'); |
| 116 |
|
| 117 |
if($file===null) |
| 118 |
{ |
| 119 |
//2.7 |
| 120 |
$settings_link = sprintf('<a href="options-general.php?page=autoptimize">%s</a>', __('Settings')); |
| 121 |
array_unshift($links,$settings_link); |
| 122 |
}else{ |
| 123 |
//2.8 |
| 124 |
//If it's us, add the link |
| 125 |
if($file === $plugin) |
| 126 |
{ |
| 127 |
$newlink = array(sprintf('<a href="options-general.php?page=autoptimize">%s</a>',__('Settings'))); |
| 128 |
$links = array_merge($links,$newlink); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
return $links; |
| 133 |
} |
| 134 |
|
| 135 |
public function get($key) |
| 136 |
{ |
| 137 |
if(!is_array($this->config)) |
| 138 |
{ |
| 139 |
//Default config |
| 140 |
$config = array('autoptimize_html' => 0, |
| 141 |
'autoptimize_js' => 0, |
| 142 |
'autoptimize_js_justhead' => 0, |
| 143 |
'autoptimize_css' => 0, |
| 144 |
'autoptimize_css_justhead' => 0); |
| 145 |
|
| 146 |
//Override with user settings |
| 147 |
foreach(array_keys($config) as $name) |
| 148 |
{ |
| 149 |
$conf = get_option($name); |
| 150 |
if($conf!==false) |
| 151 |
{ |
| 152 |
//It was set before! |
| 153 |
$config[$name] = $conf; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
//Save for next question |
| 158 |
$this->config = $config; |
| 159 |
} |
| 160 |
|
| 161 |
if(isset($this->config[$key])) |
| 162 |
return $this->config[$key]; |
| 163 |
|
| 164 |
return false; |
| 165 |
} |
| 166 |
} |
| 167 |
|