| | 1 | <?php |
| | 2 | /** |
| | 3 | * Settings Page, Group, and Field API |
| | 4 | */ |
| | 5 | |
| | 6 | class WP_Settings { |
| | 7 | private static $instance; |
| | 8 | |
| | 9 | private $settings_pages; |
| | 10 | |
| | 11 | /** |
| | 12 | * Returns singleton instance of api |
| | 13 | * |
| | 14 | * @return WP_Settings |
| | 15 | */ |
| | 16 | public static function GetInstance() { |
| | 17 | if(!isset(self::$instance)) { |
| | 18 | self::$instance = new WP_Settings(); |
| | 19 | } |
| | 20 | return self::$instance; |
| | 21 | } |
| | 22 | |
| | 23 | private function __construct() { |
| | 24 | $this->settings_pages = array(); |
| | 25 | } |
| | 26 | |
| | 27 | public function get_setting($setting_key, $group_key, $default = null) { |
| | 28 | if(is_array($setting_group = get_option($group_key)) && isset($setting_group[$setting_key])) { |
| | 29 | return $setting_group[$setting_key]; |
| | 30 | } elseif(($group = $this->get_group($group_key)) && isset($group->settings[$setting_key]) && !empty($group->settings[$setting_key]->default_value)) { |
| | 31 | return $group->settings[$setting_key]->default_value; |
| | 32 | } |
| | 33 | return $default; |
| | 34 | } |
| | 35 | |
| | 36 | private function get_group($group_key) { |
| | 37 | foreach($this->settings_pages as $settings_page) { |
| | 38 | if(isset($settings_page->groups[$group_key])) { |
| | 39 | return $settings_page->groups[$group_key]; |
| | 40 | } |
| | 41 | } |
| | 42 | return null; |
| | 43 | } |
| | 44 | |
| | 45 | /** |
| | 46 | * Adds a new settings page if one doesn't already exist |
| | 47 | * |
| | 48 | * @param string $page_key |
| | 49 | * @param string $page_title |
| | 50 | * @param string $menu_title |
| | 51 | * @param string $capability |
| | 52 | * @param string $description |
| | 53 | * @param string $parent_page slug for parent page, leave empty to create new menu |
| | 54 | * @return WP_Settings_Page |
| | 55 | */ |
| | 56 | public function add_page($page_title, $menu_title, $page_key, $capability = 'manage_options', $description = '', $parent_page = '') { |
| | 57 | if(!$capability) $capability = 'manage_options'; |
| | 58 | |
| | 59 | if ( ! $page_key ) { |
| | 60 | $page_key = 'wp_'.sanitize_key($this->title); |
| | 61 | } |
| | 62 | if(!isset($this->settings_pages[$page_key])) { |
| | 63 | $page = new WP_Settings_Page($page_title, $menu_title, $page_key, $capability, $description, $parent_page); |
| | 64 | $this->settings_pages[$page_key] = $page; |
| | 65 | } |
| | 66 | return $this->settings_pages[$page_key]; |
| | 67 | } |
| | 68 | } |
| | 69 | |
| | 70 | class WP_Settings_Page { |
| | 71 | |
| | 72 | public $title; |
| | 73 | public $menu_title; |
| | 74 | public $page_key; |
| | 75 | public $capability; |
| | 76 | public $description; |
| | 77 | |
| | 78 | /** |
| | 79 | * Key of this page's parent page if it is a submenu item |
| | 80 | * |
| | 81 | * @var string |
| | 82 | */ |
| | 83 | public $parent_page; |
| | 84 | public $groups; |
| | 85 | |
| | 86 | public function __construct($title, $menu_title, $page_key, $capability = 'manage_options', $description = '', $parent_page = '') { |
| | 87 | $this->title = $title; |
| | 88 | $this->page_key = $page_key; |
| | 89 | $this->menu_title = $menu_title; |
| | 90 | $this->capability = $capability; |
| | 91 | $this->description = $description; |
| | 92 | $this->parent_page = $parent_page; |
| | 93 | $this->groups = array(); |
| | 94 | |
| | 95 | global $pagenow; |
| | 96 | // Quick hack to avoid adding the menu for core files. |
| | 97 | if ( 0 !== strpos( 'options-', $pagenow) ) |
| | 98 | add_action('admin_menu', array($this, 'admin_menu')); |
| | 99 | if (current_user_can($this->capability)) { |
| | 100 | add_action('admin_head', array($this, 'admin_head')); |
| | 101 | } |
| | 102 | } |
| | 103 | |
| | 104 | public function add_error($code, $message, $type = 'error') { |
| | 105 | add_settings_error($this->page_key, $code, $message, $type); |
| | 106 | } |
| | 107 | |
| | 108 | /** |
| | 109 | * Adds a new group to the page |
| | 110 | * |
| | 111 | * @param string $group_key |
| | 112 | * @param string $title |
| | 113 | * @param string $capability |
| | 114 | * @param string $description |
| | 115 | * @return WP_Settings_Group |
| | 116 | */ |
| | 117 | public function add_group($title, $group_key, $capability = '', $description = '') { |
| | 118 | if(!isset($this->groups[$group_key])) { |
| | 119 | $group = new WP_Settings_Group($this, $title, $group_key, $capability, $description); |
| | 120 | $this->groups[$group_key] = $group; |
| | 121 | } |
| | 122 | return $this->groups[$group_key]; |
| | 123 | } |
| | 124 | |
| | 125 | public function admin_menu() { |
| | 126 | if(current_user_can($this->capability)) { |
| | 127 | //only add the page if groups exist |
| | 128 | if($this->parent_page) { |
| | 129 | add_submenu_page($this->parent_page, $this->title, $this->menu_title, $this->capability, $this->page_key, array($this, 'display')); |
| | 130 | } else { |
| | 131 | add_menu_page($this->title, $this->menu_title, $this->capability, $this->page_key, array($this, 'display')); |
| | 132 | } |
| | 133 | } |
| | 134 | } |
| | 135 | |
| | 136 | public function admin_head() { |
| | 137 | register_setting($this->page_key, $this->page_key, array($this, 'sanitize_callback')); |
| | 138 | } |
| | 139 | |
| | 140 | public function sanitize_callback($new_values) { |
| | 141 | if(current_user_can($this->capability)) { |
| | 142 | $this->add_error('all', 'Your changes have been saved.', 'updated'); |
| | 143 | |
| | 144 | foreach($this->groups as $group) { |
| | 145 | $new_value = isset($new_values[$group->group_key]) ? $new_values[$group->group_key] : array(); |
| | 146 | $group->sanitize_callback($new_value); |
| | 147 | } |
| | 148 | } |
| | 149 | /** |
| | 150 | * return false so a new option doesn't get added for this. |
| | 151 | */ |
| | 152 | return false; |
| | 153 | } |
| | 154 | |
| | 155 | public function display() { |
| | 156 | if(current_user_can($this->capability)) { |
| | 157 | ?> |
| | 158 | <div class="wrap"> |
| | 159 | <?php screen_icon(); ?> |
| | 160 | <h2><?php echo $this->title ?></h2> |
| | 161 | <?php settings_errors($this->page_key); ?> |
| | 162 | <?php if($this->description) echo "<p>{$this->description}</p>"; ?> |
| | 163 | <form action="options.php" method="POST"> |
| | 164 | <?php settings_fields($this->page_key); ?> |
| | 165 | <?php do_settings_sections($this->page_key); ?> |
| | 166 | <p class="submit"> |
| | 167 | <input type="submit" value="Save Changes" class="button-primary" name="Submit"> |
| | 168 | </p> |
| | 169 | </form> |
| | 170 | </div> |
| | 171 | <?php |
| | 172 | } |
| | 173 | } |
| | 174 | } |
| | 175 | |
| | 176 | class WP_Settings_Group { |
| | 177 | /** |
| | 178 | * Pointer to this Section's Page |
| | 179 | * |
| | 180 | * @var WP_Settings_Page |
| | 181 | */ |
| | 182 | public $page; |
| | 183 | |
| | 184 | public $title; |
| | 185 | public $capability; |
| | 186 | public $group_key; |
| | 187 | public $description; |
| | 188 | public $settings; |
| | 189 | |
| | 190 | public function __construct($page, $title, $group_key, $capability = '', $description = '') { |
| | 191 | $this->page = $page; |
| | 192 | $this->group_key = $group_key; |
| | 193 | $this->title = $title; |
| | 194 | $this->capability = $capability ? $capability : $this->page->capability; |
| | 195 | $this->description = $description; |
| | 196 | if(current_user_can($this->capability)) { |
| | 197 | add_action('admin_head', array($this, 'admin_head')); |
| | 198 | } |
| | 199 | } |
| | 200 | |
| | 201 | public function add_error($code, $message, $type = 'error') { |
| | 202 | $this->page->add_error($code, $message, $type); |
| | 203 | } |
| | 204 | |
| | 205 | |
| | 206 | public function admin_head() { |
| | 207 | add_settings_section($this->group_key, $this->title, array($this, 'display'), $this->page->page_key); |
| | 208 | } |
| | 209 | |
| | 210 | /** |
| | 211 | * Adds a group to the group |
| | 212 | * |
| | 213 | * @param string $title |
| | 214 | * @param string $group_key |
| | 215 | * @param string $capability |
| | 216 | * @param string $description |
| | 217 | * @return WP_Setting |
| | 218 | */ |
| | 219 | public function add_setting($title, $setting_key, $args = array()) { |
| | 220 | if(!isset($this->settings[$setting_key])) { |
| | 221 | $setting = new WP_Setting($this, $title, $setting_key, $args); |
| | 222 | $this->settings[$setting_key] = $setting; |
| | 223 | } |
| | 224 | return $this->settings[$setting_key]; |
| | 225 | } |
| | 226 | |
| | 227 | public function display() { |
| | 228 | if(current_user_can($this->capability)) { |
| | 229 | if($this->description) echo "<p>{$this->description}</p>"; |
| | 230 | } |
| | 231 | } |
| | 232 | |
| | 233 | public function sanitize_callback($new_values) { |
| | 234 | $old_values = get_option($this->group_key, array()); |
| | 235 | if(current_user_can($this->capability)) { |
| | 236 | foreach($this->settings as $setting) { |
| | 237 | $new_value = isset($new_values[$setting->setting_key]) ? $new_values[$setting->setting_key] : null; |
| | 238 | $old_value = isset($old_values[$setting->setting_key]) ? $old_values[$setting->setting_key] : null; |
| | 239 | $old_values[$setting->setting_key] = $setting->sanitize($new_value, $old_value); |
| | 240 | } |
| | 241 | } |
| | 242 | update_option($this->group_key, $old_values); |
| | 243 | } |
| | 244 | } |
| | 245 | |
| | 246 | class WP_Setting { |
| | 247 | |
| | 248 | /** |
| | 249 | * Pointer to this settings group |
| | 250 | * |
| | 251 | * @var WP_Settings_Group |
| | 252 | */ |
| | 253 | public $group; |
| | 254 | public $title; |
| | 255 | public $setting_key; |
| | 256 | public $capability; |
| | 257 | public $default_value; |
| | 258 | public $args; |
| | 259 | |
| | 260 | /** |
| | 261 | * Constructor for WP Setting |
| | 262 | * |
| | 263 | * @param WP_Settings_Group $group |
| | 264 | * @param string $title |
| | 265 | * @param string $setting_key |
| | 266 | * @param array $args |
| | 267 | */ |
| | 268 | public function __construct($group, $title, $setting_key, $args = array()) { |
| | 269 | $this->group = $group; |
| | 270 | $this->title = $title; |
| | 271 | $this->setting_key = $setting_key; |
| | 272 | |
| | 273 | $args = wp_parse_args($args, $defaults = array( |
| | 274 | 'capability' => $this->group->capability, |
| | 275 | 'default_value' => '', |
| | 276 | 'display_callback' => '', |
| | 277 | 'sanitize_callbacks' => array(), |
| | 278 | 'description' => '' |
| | 279 | )); |
| | 280 | |
| | 281 | $this->default_value = $args['default_value']; |
| | 282 | $this->capability = $args['capability']; |
| | 283 | $this->args = $args; |
| | 284 | if(current_user_can($this->capability)) { |
| | 285 | add_action('admin_head', array($this, 'admin_head')); |
| | 286 | } |
| | 287 | } |
| | 288 | |
| | 289 | public function add_error($message, $type = 'error') { |
| | 290 | $this->group->add_error($this->setting_key, $message, $type); |
| | 291 | } |
| | 292 | |
| | 293 | public function admin_head() { |
| | 294 | add_settings_field($this->setting_key, $this->title, array($this, 'display'), $this->group->page->page_key, $this->group->group_key); |
| | 295 | } |
| | 296 | |
| | 297 | public function get_field_name() { |
| | 298 | return $this->group->page->page_key . '[' . $this->group->group_key . '][' . $this->setting_key . ']'; |
| | 299 | } |
| | 300 | |
| | 301 | public function get_field_id() { |
| | 302 | return $this->group->page->page_key . '-' . $this->group->group_key . '-' . $this->setting_key; |
| | 303 | } |
| | 304 | |
| | 305 | public function display() { |
| | 306 | $value = WP_Settings::GetInstance()->get_setting($this->setting_key, $this->group->group_key, $this->default_value); |
| | 307 | if(!empty($this->args['display_callback'])) { |
| | 308 | call_user_func_array($this->args['display_callback'], array($value, $this, $this->args)); |
| | 309 | } else { |
| | 310 | ?> |
| | 311 | <input name="<?php echo $this->get_field_name() ?>" id="<?php echo $this->get_field_id() ?>" value="<?php echo esc_attr($value) ?>" class="regular-text" type="text"> |
| | 312 | <?php if(!empty($this->args['description'])) : ?> |
| | 313 | <span class="description"><?php echo $this->args['description'] ?></span> |
| | 314 | <?php endif; ?> |
| | 315 | <?php |
| | 316 | } |
| | 317 | } |
| | 318 | |
| | 319 | public function sanitize($new_value, $old_value) { |
| | 320 | if(current_user_can($this->capability)) { |
| | 321 | $old_value = $new_value; |
| | 322 | foreach($this->args['sanitize_callbacks'] as $callback) { |
| | 323 | $old_value = call_user_func_array($callback, array($old_value, $this, $this->args)); |
| | 324 | } |
| | 325 | } |
| | 326 | return $old_value; |
| | 327 | } |
| | 328 | } |