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