AdminPage.Menu.class.php
4 years ago
AdminPage.Submenu.class.php
4 years ago
AdminPage.Themes.class.php
4 years ago
AdminPage.class.php
4 years ago
AdminPageSection.class.php
4 years ago
AdminPageSetting.Address.class.php
4 years ago
AdminPageSetting.Checkbox.class.php
4 years ago
AdminPageSetting.ColorPicker.class.php
4 years ago
AdminPageSetting.Count.class.php
4 years ago
AdminPageSetting.Editor.class.php
4 years ago
AdminPageSetting.FileUpload.class.php
4 years ago
AdminPageSetting.HTML.class.php
4 years ago
AdminPageSetting.Image.class.php
4 years ago
AdminPageSetting.InfiniteTable.class.php
4 years ago
AdminPageSetting.McApiKey.class.php
4 years ago
AdminPageSetting.McListMerge.class.php
4 years ago
AdminPageSetting.Number.class.php
4 years ago
AdminPageSetting.OpeningHours.class.php
4 years ago
AdminPageSetting.Ordering.class.php
4 years ago
AdminPageSetting.Radio.class.php
4 years ago
AdminPageSetting.Scheduler.class.php
4 years ago
AdminPageSetting.Select.class.php
4 years ago
AdminPageSetting.SelectMenu.class.php
4 years ago
AdminPageSetting.SelectPost.class.php
4 years ago
AdminPageSetting.SelectTaxonomy.class.php
4 years ago
AdminPageSetting.Text.class.php
4 years ago
AdminPageSetting.Textarea.class.php
4 years ago
AdminPageSetting.Time.class.php
4 years ago
AdminPageSetting.Toggle.class.php
4 years ago
AdminPageSetting.WarningTip.class.php
4 years ago
AdminPageSetting.class.php
4 years ago
Library.class.php
4 years ago
AdminPageSetting.class.php
412 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Register, display and save a setting on a custom admin menu |
| 5 | * |
| 6 | * All settings accept the following arguments in their constructor functions. |
| 7 | * |
| 8 | * $args = array( |
| 9 | * 'id' => 'setting_id', // Unique id |
| 10 | * 'title' => 'My Setting', // Title or label for the setting |
| 11 | * 'description' => 'Description' // Help text description |
| 12 | * 'args' => array(); // Arguments to pass to WordPress's add_settings_field() function |
| 13 | * ); |
| 14 | * |
| 15 | * @since 1.0 |
| 16 | * @package Simple Admin Pages |
| 17 | */ |
| 18 | |
| 19 | abstract class sapAdminPageSetting_2_6_1 { |
| 20 | |
| 21 | // Page defaults |
| 22 | public $id; // used in form fields and database to track and store setting |
| 23 | public $title; // setting label |
| 24 | public $description; // optional description of the setting |
| 25 | public $value; // value of the setting, if a value exists |
| 26 | public $disabled = false; // whether a setting should be disabled |
| 27 | public $small = false; // whether a text input should use the small styling |
| 28 | public $columns; // to be used for the number of columns for settings, like radio and checkbox, with lots of options/values |
| 29 | public $conditional_on; // optional setting that this one is dependent on to diplay (ex. payment enabled for payment settings) |
| 30 | public $conditional_on_value; // the required value of the dependent setting, if enabled |
| 31 | public $conditional_display = true; // whether this setting should be displayed based on its conditional settings |
| 32 | |
| 33 | |
| 34 | /** |
| 35 | * An array of arguments accepted by add_settings_field. |
| 36 | * See: https://codex.wordpress.org/Function_Reference/add_settings_field |
| 37 | */ |
| 38 | public $args = array(); |
| 39 | |
| 40 | // Array to store errors |
| 41 | public $errors = array(); |
| 42 | |
| 43 | /** |
| 44 | * Position in section |
| 45 | * |
| 46 | * An array with two elements describing where this setting should |
| 47 | * be placed in its section. The first element describes a position |
| 48 | * and the second (optional) element identifies the id of an |
| 49 | * existing setting. Examples: |
| 50 | * |
| 51 | * array( 'bottom' ) // Default. bottom of section |
| 52 | * array( 'top' ) // top of section |
| 53 | * array( 'before', 'my-setting' ) // before a specific setting |
| 54 | * array( 'after', 'my-setting' ) // after a specific setting |
| 55 | * |
| 56 | * This setting is intended for use when you have to hook in after |
| 57 | * the settings page has been defined, such as adding a new setting |
| 58 | * from a third-party plugin. |
| 59 | */ |
| 60 | public $position; |
| 61 | |
| 62 | /** |
| 63 | * Function to use when sanitizing the data |
| 64 | * |
| 65 | * We set this to a strict sanitization function as a default, but a |
| 66 | * setting should override this in an extended class when needed. |
| 67 | * |
| 68 | * @since 1.0 |
| 69 | */ |
| 70 | public $sanitize_callback = 'sanitize_text_field'; |
| 71 | |
| 72 | /** |
| 73 | * Scripts that must be loaded for this component |
| 74 | * @since 2.0.a.4 |
| 75 | */ |
| 76 | public $scripts = array( |
| 77 | /** |
| 78 | * Example |
| 79 | * See: http://codex.wordpress.org/Function_Reference/wp_enqueue_script |
| 80 | * |
| 81 | 'handle' => array( |
| 82 | 'path' => 'path/from/simple-admin-pages/file.js', |
| 83 | 'dependencies' => array( 'jquery' ), |
| 84 | 'version' => '3.5.0', |
| 85 | 'footer' => true, |
| 86 | ), |
| 87 | */ |
| 88 | ); |
| 89 | |
| 90 | /** |
| 91 | * Styles that must be loaded for this component |
| 92 | * @since 2.0.a.4 |
| 93 | */ |
| 94 | public $styles = array( |
| 95 | /** |
| 96 | * Example |
| 97 | * See: http://codex.wordpress.org/Function_Reference/wp_enqueue_style |
| 98 | * |
| 99 | 'handle' => array( |
| 100 | 'path' => 'path/from/simple-admin-pages/file.css', |
| 101 | 'dependencies' => 'array( 'another-handle')', // or empty string |
| 102 | 'version' => '3.5.0', |
| 103 | 'media' => null, |
| 104 | ), |
| 105 | */ |
| 106 | ); |
| 107 | |
| 108 | /** |
| 109 | * Translateable strings required for this component |
| 110 | * |
| 111 | * Settings classes which require translateable strings should be |
| 112 | * defined with string id's pointing to null values. The actual |
| 113 | * strings should be passed with the $sap->add_setting() call. |
| 114 | * |
| 115 | * @since 2.0.a.8 |
| 116 | */ |
| 117 | public $strings = array( |
| 118 | /** |
| 119 | * Example |
| 120 | * |
| 121 | 'string_id' => null |
| 122 | */ |
| 123 | ); |
| 124 | |
| 125 | /** |
| 126 | * Initialize the setting |
| 127 | * |
| 128 | * By default, every setting takes an id, title and description in the $args |
| 129 | * array. |
| 130 | * |
| 131 | * @since 1.0 |
| 132 | */ |
| 133 | public function __construct( $args ) { |
| 134 | |
| 135 | // Parse the values passed |
| 136 | $this->parse_args( $args ); |
| 137 | |
| 138 | // Get any existing value |
| 139 | $this->set_value(); |
| 140 | |
| 141 | // Get any existing value |
| 142 | $this->set_conditional_display(); |
| 143 | |
| 144 | // Check for missing data |
| 145 | $this->missing_data(); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Parse the arguments passed in the construction and assign them to |
| 150 | * internal variables. This function will be overwritten for most subclasses |
| 151 | * @since 1.0 |
| 152 | */ |
| 153 | private function parse_args( $args ) { |
| 154 | foreach ( $args as $key => $val ) { |
| 155 | switch ( $key ) { |
| 156 | |
| 157 | case 'id' : |
| 158 | $this->{$key} = esc_attr( $val ); |
| 159 | |
| 160 | default : |
| 161 | $this->{$key} = $val; |
| 162 | |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Check for missing data when setup. |
| 169 | * @since 1.0 |
| 170 | */ |
| 171 | private function missing_data() { |
| 172 | |
| 173 | $error_type = 'missing_data'; |
| 174 | |
| 175 | // Required fields |
| 176 | if ( empty( $this->id ) ) { |
| 177 | $this->set_error( |
| 178 | array( |
| 179 | 'type' => $error_type, |
| 180 | 'data' => 'id' |
| 181 | ) |
| 182 | ); |
| 183 | } |
| 184 | if ( empty( $this->title ) ) { |
| 185 | $this->set_error( |
| 186 | array( |
| 187 | 'type' => $error_type, |
| 188 | 'data' => 'title' |
| 189 | ) |
| 190 | ); |
| 191 | } |
| 192 | |
| 193 | // Check for strings |
| 194 | foreach ( $this->strings as $id => $string ) { |
| 195 | |
| 196 | if ( $string === null ) { |
| 197 | $this->set_error( |
| 198 | array( |
| 199 | 'type' => $error_type, |
| 200 | 'data' => 'string: ' . $id, |
| 201 | ) |
| 202 | ); |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Set a value |
| 209 | * @since 2.0 |
| 210 | */ |
| 211 | public function set_value( $val = null ) { |
| 212 | |
| 213 | if ( $val === null ) { |
| 214 | $option_group_value = get_option( $this->page ); |
| 215 | $val = isset( $option_group_value[ $this->id ] ) ? $option_group_value[ $this->id ] : ''; |
| 216 | } |
| 217 | |
| 218 | $this->value = $this->esc_value( $val ); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Escape the value to display it in text fields and other input fields |
| 223 | * |
| 224 | * We use esc_attr() here so that the default is quite strict, but other |
| 225 | * setting types should override this function with the appropriate escape |
| 226 | * function. See: http://codex.wordpress.org/Data_Validation |
| 227 | * |
| 228 | * @since 1.0 |
| 229 | */ |
| 230 | public function esc_value( $val ) { |
| 231 | |
| 232 | if ( is_array( $val ) ) { return array_map( 'esc_attr', $val );} |
| 233 | |
| 234 | return esc_attr( $val ); |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Determines whether this setting should be displayed, based on its |
| 239 | * conditional conditions, if any. |
| 240 | * |
| 241 | * @since 2.6 |
| 242 | */ |
| 243 | public function set_conditional_display() { |
| 244 | |
| 245 | if ( empty( $this->conditional_on ) ) { return; } |
| 246 | |
| 247 | $option_group_value = get_option( $this->page ); |
| 248 | |
| 249 | $option_group_value[ $this->conditional_on ] = isset( $option_group_value[ $this->conditional_on ] ) ? $option_group_value[ $this->conditional_on ] : false; |
| 250 | |
| 251 | if ( is_array( $option_group_value[ $this->conditional_on ] ) ) { |
| 252 | |
| 253 | $this->conditional_display = in_array( $this->conditional_on_value, $option_group_value[ $this->conditional_on ] ); |
| 254 | } |
| 255 | else { |
| 256 | |
| 257 | $this->conditional_display = $this->conditional_on_value == $option_group_value[ $this->conditional_on ] ? true : false; |
| 258 | } |
| 259 | |
| 260 | if ( ! empty( $this->conditional_display ) ) { return; } |
| 261 | |
| 262 | if ( ! empty( $this->args['class'] ) ) { |
| 263 | |
| 264 | $this->args['class'] .= ' sap-hidden'; |
| 265 | } |
| 266 | else { |
| 267 | |
| 268 | $this->args['class'] = 'sap-hidden'; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Prints conditional data tags within the input element if necessary |
| 274 | * |
| 275 | * @since 2.6 |
| 276 | */ |
| 277 | public function print_conditional_data() { |
| 278 | |
| 279 | if ( empty( $this->conditional_on ) ) { return; } |
| 280 | |
| 281 | echo 'data-conditional_on="' . esc_attr( $this->conditional_on ) . '"'; |
| 282 | echo 'data-conditional_on_value="' . esc_attr( $this->conditional_on_value ) . '"'; |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * Wrapper for the sanitization callback function. |
| 287 | * |
| 288 | * This just reduces code duplication for child classes that need a custom |
| 289 | * callback function. |
| 290 | * @since 1.0 |
| 291 | */ |
| 292 | public function sanitize_callback_wrapper( $value ) { |
| 293 | return call_user_func( $this->sanitize_callback, $value ); |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Display this setting |
| 298 | * @since 1.0 |
| 299 | */ |
| 300 | abstract public function display_setting(); |
| 301 | |
| 302 | /** |
| 303 | * Display a description for this setting |
| 304 | * @since 1.0 |
| 305 | */ |
| 306 | public function display_description() { |
| 307 | |
| 308 | if ( empty( $this->description ) ) { return; } |
| 309 | |
| 310 | ?> |
| 311 | |
| 312 | <p class="description<?php echo ( $this->disabled ? ' disabled' : ''); ?>"><?php echo $this->description; ?></p> |
| 313 | |
| 314 | <?php |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * Display a disabled image for this section and possibly a link to upgrade |
| 319 | * @since 2.0 |
| 320 | */ |
| 321 | public function display_disabled() { |
| 322 | |
| 323 | if ( $this->disabled and isset($this->disabled_image) ) { |
| 324 | |
| 325 | ?> |
| 326 | |
| 327 | <?php echo ( isset($this->purchase_link ) ? "<a href='" . $this->purchase_link . "'>" : '' ); ?> |
| 328 | <div class="disabled"><img src='<?php echo $this->disabled_image; ?>;' /></div> |
| 329 | <?php echo ( isset($this->purchase_link ) ? "</a>" : '' ); ?> |
| 330 | |
| 331 | <?php |
| 332 | |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * Generate an option input field name, using the grouped schema: |
| 338 | * "page[option_name]" |
| 339 | * @since 1.2 |
| 340 | */ |
| 341 | public function get_input_name() { |
| 342 | return esc_attr( $this->page ) . '[' . esc_attr( $this->id ) . ']'; |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * Get the default value for a setting if value is currently empty |
| 347 | * |
| 348 | * @since 2.4.1 |
| 349 | */ |
| 350 | public function get_default_setting() { |
| 351 | return ! empty( $this->default ) ? $this->default : $this->value; |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * Add and register this setting |
| 356 | * |
| 357 | * @since 1.0 |
| 358 | */ |
| 359 | public function add_settings_field( $section_id ) { |
| 360 | |
| 361 | // If no sanitization callback exists, don't register the setting. |
| 362 | if ( !$this->has_sanitize_callback() ) { |
| 363 | return; |
| 364 | } |
| 365 | |
| 366 | add_settings_field( |
| 367 | $this->id, |
| 368 | $this->title, |
| 369 | array( $this, 'display_setting' ), |
| 370 | $this->tab, |
| 371 | $section_id, |
| 372 | $this->args |
| 373 | ); |
| 374 | |
| 375 | } |
| 376 | |
| 377 | /** |
| 378 | * Check if this field has a sanitization callback set |
| 379 | * @since 1.2 |
| 380 | */ |
| 381 | public function has_sanitize_callback() { |
| 382 | if ( isset( $this->sanitize_callback ) && trim( $this->sanitize_callback ) ) { |
| 383 | return true; |
| 384 | } |
| 385 | |
| 386 | return false; |
| 387 | } |
| 388 | |
| 389 | /** |
| 390 | * Set an error |
| 391 | * @since 1.0 |
| 392 | */ |
| 393 | public function set_error( $error ) { |
| 394 | $this->errors[] = array_merge( |
| 395 | $error, |
| 396 | array( |
| 397 | 'class' => get_class( $this ), |
| 398 | 'id' => $this->id, |
| 399 | 'backtrace' => debug_backtrace() |
| 400 | ) |
| 401 | ); |
| 402 | } |
| 403 | |
| 404 | /** |
| 405 | * Check if a setting has a position |
| 406 | * @since 2.0.a.9 |
| 407 | */ |
| 408 | public function has_position() { |
| 409 | return !empty( $this->position ) && is_array( $this->position ) && !empty( $this->position[0] ); |
| 410 | } |
| 411 | } |
| 412 |