|
| 1 | +<?php |
| 2 | + |
| 3 | +abstract class scbAdminPage extends scbForms |
| 4 | +{ |
| 5 | + /** Page args |
| 6 | + * string $parent (default: options-general.php) |
| 7 | + * string $page_title (mandatory) |
| 8 | + * string $menu_title |
| 9 | + * string $page_slug |
| 10 | + * array $action_link (default: Settings) |
| 11 | + * string $nonce |
| 12 | + */ |
| 13 | + protected $args; |
| 14 | + |
| 15 | + // URL to the current plugin directory. |
| 16 | + // Useful for adding css and js files |
| 17 | + protected $plugin_url; |
| 18 | + |
| 19 | + // Created at page init |
| 20 | + protected $pagehook; |
| 21 | + |
| 22 | + // scbOptions object holder |
| 23 | + // Normally, it's used for storing formdata |
| 24 | + protected $options; |
| 25 | + |
| 26 | + // Formdata used for filling the form elements |
| 27 | + protected $formdata = array(); |
| 28 | + |
| 29 | + |
| 30 | +//_____MAIN METHODS_____ |
| 31 | + |
| 32 | + |
| 33 | + // Constructor |
| 34 | + function __construct($file, $options = NULL) |
| 35 | + { |
| 36 | + $this->setup(); |
| 37 | + |
| 38 | + $this->_check_args(); |
| 39 | + $this->_set_url($file); |
| 40 | + |
| 41 | + if ( $options !== NULL ) |
| 42 | + { |
| 43 | + $this->options = $options; |
| 44 | + $this->formdata = $this->options->get(); |
| 45 | + } |
| 46 | + |
| 47 | + add_action('admin_menu', array($this, 'page_init')); |
| 48 | + |
| 49 | + if ( $this->args['action_link'] ) |
| 50 | + add_filter('plugin_action_links_' . plugin_basename($file), array($this, '_action_link')); |
| 51 | + } |
| 52 | + |
| 53 | + // This is where all the page args are set (DEPRECATED) |
| 54 | + function setup(){} |
| 55 | + |
| 56 | + // This is where the css and js go |
| 57 | + // Both wp_enqueue_*() and inline code can be added |
| 58 | + function page_head(){} |
| 59 | + |
| 60 | + // A generic page header |
| 61 | + function page_header() |
| 62 | + { |
| 63 | + $this->form_handler(); |
| 64 | + |
| 65 | + echo "<div class='wrap'>\n"; |
| 66 | + echo "<h2>" . $this->args['page_title'] . "</h2>\n"; |
| 67 | + } |
| 68 | + |
| 69 | + |
| 70 | + // This is where the page content goes |
| 71 | + abstract function page_content(); |
| 72 | + |
| 73 | + |
| 74 | + // A generic page footer |
| 75 | + function page_footer() |
| 76 | + { |
| 77 | + echo "</div>\n"; |
| 78 | + } |
| 79 | + |
| 80 | + |
| 81 | + // This is where the form data is validated |
| 82 | + function validate($formdata) |
| 83 | + { |
| 84 | + return $formdata; |
| 85 | + } |
| 86 | + |
| 87 | + // A generic form handler |
| 88 | + function form_handler() |
| 89 | + { |
| 90 | + if ( empty($_POST['action']) ) |
| 91 | + return false; |
| 92 | + |
| 93 | + check_admin_referer($this->nonce); |
| 94 | + |
| 95 | + foreach ( $this->formdata as $name => $value ) |
| 96 | + $new_data[$name] = $_POST[$name]; |
| 97 | + |
| 98 | + $this->formdata = $this->validate($new_data); |
| 99 | + |
| 100 | + if ( isset($this->options) ) |
| 101 | + $this->options->update($this->formdata); |
| 102 | + |
| 103 | + $this->admin_msg(__('Settings <strong>saved</strong>.')); |
| 104 | + } |
| 105 | + |
| 106 | + |
| 107 | +//_____HELPER METHODS_____ |
| 108 | + |
| 109 | + |
| 110 | + // See scbForms::input() |
| 111 | + function input($args, $options = NULL) |
| 112 | + { |
| 113 | + if ( $options === NULL ) |
| 114 | + $options = $this->formdata; |
| 115 | + |
| 116 | + return parent::input($args, $options); |
| 117 | + } |
| 118 | + |
| 119 | + // See scbForms::form() |
| 120 | + function form($rows, $options = NULL, $nonce = NULL) |
| 121 | + { |
| 122 | + if ( $nonce === NULL ) |
| 123 | + $nonce = $this->nonce; |
| 124 | + |
| 125 | + if ( $options === NULL ) |
| 126 | + $options = $this->formdata; |
| 127 | + |
| 128 | + return parent::form($rows, $options, $nonce); |
| 129 | + } |
| 130 | + |
| 131 | + // See scbForms::table() |
| 132 | + function table($rows, $options = NULL) |
| 133 | + { |
| 134 | + if ( $options === NULL ) |
| 135 | + $options = $this->formdata; |
| 136 | + |
| 137 | + return parent::table($rows, $options); |
| 138 | + } |
| 139 | + |
| 140 | + // See scbForms::table_row() |
| 141 | + function table_row($row, $options = NULL) |
| 142 | + { |
| 143 | + if ( $options === NULL ) |
| 144 | + $options = $this->formdata; |
| 145 | + |
| 146 | + return parent::table_row($row, $options); |
| 147 | + } |
| 148 | + |
| 149 | + // See scbForms::table_wrap() |
| 150 | + function form_wrap($content, $submit_button = true) |
| 151 | + { |
| 152 | + if ( $submit_button === true ) |
| 153 | + $submit_button = $this->submit_button(); |
| 154 | + |
| 155 | + $content .= $submit_button; |
| 156 | + |
| 157 | + return parent::form_wrap($content, $this->nonce); |
| 158 | + } |
| 159 | + |
| 160 | + // See scbForms::form_table() |
| 161 | + function form_table($rows, $options = NULL, $submit_button = true) |
| 162 | + { |
| 163 | + $output = $this->table($rows, $options); |
| 164 | + |
| 165 | + return $this->form_wrap($output, $submit_button); |
| 166 | + } |
| 167 | + |
| 168 | + // Generates a form submit button |
| 169 | + function submit_button($action = 'action', $value = 'Save Changes', $class = "button") |
| 170 | + { |
| 171 | + if ( in_array($action, (array) $this->_actions) ) |
| 172 | + trigger_error("Duplicate action for submit button: {$action}", E_USER_WARNING); |
| 173 | + |
| 174 | + $this->_actions[] = $action; |
| 175 | + |
| 176 | + $args = array( |
| 177 | + 'type' => 'submit', |
| 178 | + 'names' => $action, |
| 179 | + 'values' => $value, |
| 180 | + 'extra' => '', |
| 181 | + 'desc' => false |
| 182 | + ); |
| 183 | + |
| 184 | + if ( ! empty($class) ) |
| 185 | + $args['extra'] = "class='{$class}'"; |
| 186 | + |
| 187 | + $output = "<p class='submit'>\n" . parent::input($args) . "</p>\n"; |
| 188 | + |
| 189 | + return $output; |
| 190 | + } |
| 191 | + |
| 192 | + // To be used in page_head() |
| 193 | + function admin_msg($msg, $class = "updated") |
| 194 | + { |
| 195 | + echo "<div class='$class fade'><p>$msg</p></div>\n"; |
| 196 | + } |
| 197 | + |
| 198 | + // Wraps a string in a <script> tag |
| 199 | + function js_wrap($string) |
| 200 | + { |
| 201 | + return "\n<script language='javascript' type='text/javascript'>\n" . $string . "\n</script>\n"; |
| 202 | + } |
| 203 | + |
| 204 | + // Wraps a string in a <style> tag |
| 205 | + function css_wrap($string) |
| 206 | + { |
| 207 | + return "\n<style type='text/css'>\n" . $string . "\n</style>\n"; |
| 208 | + } |
| 209 | + |
| 210 | + |
| 211 | +//_____INTERNAL METHODS (DON'T WORRY ABOUT THESE)_____ |
| 212 | + |
| 213 | + |
| 214 | + // Registers a page |
| 215 | + function page_init() |
| 216 | + { |
| 217 | + extract($this->args); |
| 218 | + $this->pagehook = add_submenu_page($parent, $page_title, $menu_title, $capability, $page_slug, array($this, '_page_content_hook')); |
| 219 | + |
| 220 | + add_action('admin_print_styles-' . $this->pagehook, array($this, 'page_head')); |
| 221 | + } |
| 222 | + |
| 223 | + function _page_content_hook() |
| 224 | + { |
| 225 | + $this->page_header(); |
| 226 | + $this->page_content(); |
| 227 | + $this->page_footer(); |
| 228 | + } |
| 229 | + |
| 230 | + function _action_link($links) |
| 231 | + { |
| 232 | + $url = add_query_arg('page', $this->args['page_slug'], admin_url($this->args['parent'])); |
| 233 | + $links[] = "<a href='$url'>" . $this->args['action_link'] . "</a>"; |
| 234 | + |
| 235 | + return $links; |
| 236 | + } |
| 237 | + |
| 238 | + function _check_args() |
| 239 | + { |
| 240 | + if ( empty($this->args['page_title']) ) |
| 241 | + trigger_error('Page title cannot be empty', E_USER_ERROR); |
| 242 | + |
| 243 | + $this->args = wp_parse_args($this->args, array( |
| 244 | + 'menu_title' => $this->args['page_title'], |
| 245 | + 'page_slug' => '', |
| 246 | + 'action_link' => __('Settings'), |
| 247 | + 'parent' => 'options-general.php', |
| 248 | + 'capability' => 'manage_options', |
| 249 | + 'nonce' => '' |
| 250 | + )); |
| 251 | + |
| 252 | + if ( empty($this->args['page_slug']) ) |
| 253 | + $this->args['page_slug'] = sanitize_title_with_dashes($this->args['menu_title']); |
| 254 | + |
| 255 | + if ( empty($this->args['nonce']) ) |
| 256 | + $this->nonce = $this->args['page_slug']; |
| 257 | + } |
| 258 | + |
| 259 | + // Set plugin_dir |
| 260 | + function _set_url($file) |
| 261 | + { |
| 262 | + if ( function_exists('plugins_url') ) |
| 263 | + $this->plugin_url = plugins_url(plugin_basename(dirname($file))); |
| 264 | + else |
| 265 | + // WP < 2.6 |
| 266 | + $this->plugin_url = get_option('siteurl') . '/wp-content/plugins/' . plugin_basename(dirname($file)); |
| 267 | + } |
| 268 | +} |
| 269 | + |
0 commit comments