PluginProbe
StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more / 2.0
StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more v2.0
3.7.2 3.7.1 3.7.0 3.6.0 3.6.1 3.5.2 trunk 2.0 2.1 2.2 2.3 2.4 2.4.1 2.4.2 2.4.3 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.6.0 2.6.1 All 73 releases
stockpack / vendor / tareq1988 / wordpress-settings-api-class / src / class.settings-api.php

class.settings-api.php in StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more 2.0, at vendor/tareq1988/wordpress-settings-api-class/src/class.settings-api.php

659 lines 22.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * weDevs Settings API wrapper class
5 *
6 * @version 1.3 (27-Sep-2016)
7 *
8 * @author Tareq Hasan <[email protected]>
9 * @link https://tareq.co Tareq Hasan
10 * @example example/oop-example.php How to use the class
11 */
12 if ( !class_exists( 'WeDevs_Settings_API' ) ):
13 class WeDevs_Settings_API {
14
15 /**
16 * settings sections array
17 *
18 * @var array
19 */
20 protected $settings_sections = array();
21
22 /**
23 * Settings fields array
24 *
25 * @var array
26 */
27 protected $settings_fields = array();
28
29 public function __construct() {
30 add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
31 }
32
33 /**
34 * Enqueue scripts and styles
35 */
36 function admin_enqueue_scripts() {
37 wp_enqueue_style( 'wp-color-picker' );
38
39 wp_enqueue_media();
40 wp_enqueue_script( 'wp-color-picker' );
41 wp_enqueue_script( 'jquery' );
42 }
43
44 /**
45 * Set settings sections
46 *
47 * @param array $sections setting sections array
48 */
49 function set_sections( $sections ) {
50 $this->settings_sections = $sections;
51
52 return $this;
53 }
54
55 /**
56 * Add a single section
57 *
58 * @param array $section
59 */
60 function add_section( $section ) {
61 $this->settings_sections[] = $section;
62
63 return $this;
64 }
65
66 /**
67 * Set settings fields
68 *
69 * @param array $fields settings fields array
70 */
71 function set_fields( $fields ) {
72 $this->settings_fields = $fields;
73
74 return $this;
75 }
76
77 function add_field( $section, $field ) {
78 $defaults = array(
79 'name' => '',
80 'label' => '',
81 'desc' => '',
82 'type' => 'text'
83 );
84
85 $arg = wp_parse_args( $field, $defaults );
86 $this->settings_fields[$section][] = $arg;
87
88 return $this;
89 }
90
91 /**
92 * Initialize and registers the settings sections and fileds to WordPress
93 *
94 * Usually this should be called at `admin_init` hook.
95 *
96 * This function gets the initiated settings sections and fields. Then
97 * registers them to WordPress and ready for use.
98 */
99 function admin_init() {
100 //register settings sections
101 foreach ( $this->settings_sections as $section ) {
102 if ( false == get_option( $section['id'] ) ) {
103 add_option( $section['id'] );
104 }
105
106 if ( isset($section['desc']) && !empty($section['desc']) ) {
107 $section['desc'] = '<div class="inside">' . $section['desc'] . '</div>';
108 $callback = function() use ( $section ) {
109 echo str_replace( '"', '\"', $section['desc'] );
110 };
111 } else if ( isset( $section['callback'] ) ) {
112 $callback = $section['callback'];
113 } else {
114 $callback = null;
115 }
116
117 add_settings_section( $section['id'], $section['title'], $callback, $section['id'] );
118 }
119
120 //register settings fields
121 foreach ( $this->settings_fields as $section => $field ) {
122 foreach ( $field as $option ) {
123
124 $name = $option['name'];
125 $type = isset( $option['type'] ) ? $option['type'] : 'text';
126 $label = isset( $option['label'] ) ? $option['label'] : '';
127 $callback = isset( $option['callback'] ) ? $option['callback'] : array( $this, 'callback_' . $type );
128
129 $args = array(
130 'id' => $name,
131 'class' => isset( $option['class'] ) ? $option['class'] : $name,
132 'label_for' => "{$section}[{$name}]",
133 'desc' => isset( $option['desc'] ) ? $option['desc'] : '',
134 'name' => $label,
135 'section' => $section,
136 'size' => isset( $option['size'] ) ? $option['size'] : null,
137 'options' => isset( $option['options'] ) ? $option['options'] : '',
138 'std' => isset( $option['default'] ) ? $option['default'] : '',
139 'sanitize_callback' => isset( $option['sanitize_callback'] ) ? $option['sanitize_callback'] : '',
140 'type' => $type,
141 'placeholder' => isset( $option['placeholder'] ) ? $option['placeholder'] : '',
142 'min' => isset( $option['min'] ) ? $option['min'] : '',
143 'max' => isset( $option['max'] ) ? $option['max'] : '',
144 'step' => isset( $option['step'] ) ? $option['step'] : '',
145 );
146
147 add_settings_field( "{$section}[{$name}]", $label, $callback, $section, $section, $args );
148 }
149 }
150
151 // creates our settings in the options table
152 foreach ( $this->settings_sections as $section ) {
153 register_setting( $section['id'], $section['id'], array( $this, 'sanitize_options' ) );
154 }
155 }
156
157 /**
158 * Get field description for display
159 *
160 * @param array $args settings field args
161 */
162 public function get_field_description( $args ) {
163 if ( ! empty( $args['desc'] ) ) {
164 $desc = sprintf( '<p class="description">%s</p>', $args['desc'] );
165 } else {
166 $desc = '';
167 }
168
169 return $desc;
170 }
171
172 /**
173 * Displays a text field for a settings field
174 *
175 * @param array $args settings field args
176 */
177 function callback_text( $args ) {
178
179 $value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) );
180 $size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular';
181 $type = isset( $args['type'] ) ? $args['type'] : 'text';
182 $placeholder = empty( $args['placeholder'] ) ? '' : ' placeholder="' . $args['placeholder'] . '"';
183
184 $html = sprintf( '<input type="%1$s" class="%2$s-text" id="%3$s[%4$s]" name="%3$s[%4$s]" value="%5$s"%6$s/>', $type, $size, $args['section'], $args['id'], $value, $placeholder );
185 $html .= $this->get_field_description( $args );
186
187 echo $html;
188 }
189
190 /**
191 * Displays a url field for a settings field
192 *
193 * @param array $args settings field args
194 */
195 function callback_url( $args ) {
196 $this->callback_text( $args );
197 }
198
199 /**
200 * Displays a number field for a settings field
201 *
202 * @param array $args settings field args
203 */
204 function callback_number( $args ) {
205 $value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) );
206 $size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular';
207 $type = isset( $args['type'] ) ? $args['type'] : 'number';
208 $placeholder = empty( $args['placeholder'] ) ? '' : ' placeholder="' . $args['placeholder'] . '"';
209 $min = ( $args['min'] == '' ) ? '' : ' min="' . $args['min'] . '"';
210 $max = ( $args['max'] == '' ) ? '' : ' max="' . $args['max'] . '"';
211 $step = ( $args['step'] == '' ) ? '' : ' step="' . $args['step'] . '"';
212
213 $html = sprintf( '<input type="%1$s" class="%2$s-number" id="%3$s[%4$s]" name="%3$s[%4$s]" value="%5$s"%6$s%7$s%8$s%9$s/>', $type, $size, $args['section'], $args['id'], $value, $placeholder, $min, $max, $step );
214 $html .= $this->get_field_description( $args );
215
216 echo $html;
217 }
218
219 /**
220 * Displays a checkbox for a settings field
221 *
222 * @param array $args settings field args
223 */
224 function callback_checkbox( $args ) {
225
226 $value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) );
227
228 $html = '<fieldset>';
229 $html .= sprintf( '<label for="wpuf-%1$s[%2$s]">', $args['section'], $args['id'] );
230 $html .= sprintf( '<input type="hidden" name="%1$s[%2$s]" value="off" />', $args['section'], $args['id'] );
231 $html .= sprintf( '<input type="checkbox" class="checkbox" id="wpuf-%1$s[%2$s]" name="%1$s[%2$s]" value="on" %3$s />', $args['section'], $args['id'], checked( $value, 'on', false ) );
232 $html .= sprintf( '%1$s</label>', $args['desc'] );
233 $html .= '</fieldset>';
234
235 echo $html;
236 }
237
238 /**
239 * Displays a multicheckbox for a settings field
240 *
241 * @param array $args settings field args
242 */
243 function callback_multicheck( $args ) {
244
245 $value = $this->get_option( $args['id'], $args['section'], $args['std'] );
246 $html = '<fieldset>';
247 $html .= sprintf( '<input type="hidden" name="%1$s[%2$s]" value="" />', $args['section'], $args['id'] );
248 foreach ( $args['options'] as $key => $label ) {
249 $checked = isset( $value[$key] ) ? $value[$key] : '0';
250 $html .= sprintf( '<label for="wpuf-%1$s[%2$s][%3$s]">', $args['section'], $args['id'], $key );
251 $html .= sprintf( '<input type="checkbox" class="checkbox" id="wpuf-%1$s[%2$s][%3$s]" name="%1$s[%2$s][%3$s]" value="%3$s" %4$s />', $args['section'], $args['id'], $key, checked( $checked, $key, false ) );
252 $html .= sprintf( '%1$s</label><br>', $label );
253 }
254
255 $html .= $this->get_field_description( $args );
256 $html .= '</fieldset>';
257
258 echo $html;
259 }
260
261 /**
262 * Displays a radio button for a settings field
263 *
264 * @param array $args settings field args
265 */
266 function callback_radio( $args ) {
267
268 $value = $this->get_option( $args['id'], $args['section'], $args['std'] );
269 $html = '<fieldset>';
270
271 foreach ( $args['options'] as $key => $label ) {
272 $html .= sprintf( '<label for="wpuf-%1$s[%2$s][%3$s]">', $args['section'], $args['id'], $key );
273 $html .= sprintf( '<input type="radio" class="radio" id="wpuf-%1$s[%2$s][%3$s]" name="%1$s[%2$s]" value="%3$s" %4$s />', $args['section'], $args['id'], $key, checked( $value, $key, false ) );
274 $html .= sprintf( '%1$s</label><br>', $label );
275 }
276
277 $html .= $this->get_field_description( $args );
278 $html .= '</fieldset>';
279
280 echo $html;
281 }
282
283 /**
284 * Displays a selectbox for a settings field
285 *
286 * @param array $args settings field args
287 */
288 function callback_select( $args ) {
289
290 $value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) );
291 $size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular';
292 $html = sprintf( '<select class="%1$s" name="%2$s[%3$s]" id="%2$s[%3$s]">', $size, $args['section'], $args['id'] );
293
294 foreach ( $args['options'] as $key => $label ) {
295 $html .= sprintf( '<option value="%s"%s>%s</option>', $key, selected( $value, $key, false ), $label );
296 }
297
298 $html .= sprintf( '</select>' );
299 $html .= $this->get_field_description( $args );
300
301 echo $html;
302 }
303
304 /**
305 * Displays a textarea for a settings field
306 *
307 * @param array $args settings field args
308 */
309 function callback_textarea( $args ) {
310
311 $value = esc_textarea( $this->get_option( $args['id'], $args['section'], $args['std'] ) );
312 $size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular';
313 $placeholder = empty( $args['placeholder'] ) ? '' : ' placeholder="'.$args['placeholder'].'"';
314
315 $html = sprintf( '<textarea rows="5" cols="55" class="%1$s-text" id="%2$s[%3$s]" name="%2$s[%3$s]"%4$s>%5$s</textarea>', $size, $args['section'], $args['id'], $placeholder, $value );
316 $html .= $this->get_field_description( $args );
317
318 echo $html;
319 }
320
321 /**
322 * Displays the html for a settings field
323 *
324 * @param array $args settings field args
325 * @return string
326 */
327 function callback_html( $args ) {
328 echo $this->get_field_description( $args );
329 }
330
331 /**
332 * Displays a rich text textarea for a settings field
333 *
334 * @param array $args settings field args
335 */
336 function callback_wysiwyg( $args ) {
337
338 $value = $this->get_option( $args['id'], $args['section'], $args['std'] );
339 $size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : '500px';
340
341 echo '<div style="max-width: ' . $size . ';">';
342
343 $editor_settings = array(
344 'teeny' => true,
345 'textarea_name' => $args['section'] . '[' . $args['id'] . ']',
346 'textarea_rows' => 10
347 );
348
349 if ( isset( $args['options'] ) && is_array( $args['options'] ) ) {
350 $editor_settings = array_merge( $editor_settings, $args['options'] );
351 }
352
353 wp_editor( $value, $args['section'] . '-' . $args['id'], $editor_settings );
354
355 echo '</div>';
356
357 echo $this->get_field_description( $args );
358 }
359
360 /**
361 * Displays a file upload field for a settings field
362 *
363 * @param array $args settings field args
364 */
365 function callback_file( $args ) {
366
367 $value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) );
368 $size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular';
369 $id = $args['section'] . '[' . $args['id'] . ']';
370 $label = isset( $args['options']['button_label'] ) ? $args['options']['button_label'] : __( 'Choose File' );
371
372 $html = sprintf( '<input type="text" class="%1$s-text wpsa-url" id="%2$s[%3$s]" name="%2$s[%3$s]" value="%4$s"/>', $size, $args['section'], $args['id'], $value );
373 $html .= '<input type="button" class="button wpsa-browse" value="' . $label . '" />';
374 $html .= $this->get_field_description( $args );
375
376 echo $html;
377 }
378
379 /**
380 * Displays a password field for a settings field
381 *
382 * @param array $args settings field args
383 */
384 function callback_password( $args ) {
385
386 $value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) );
387 $size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular';
388
389 $html = sprintf( '<input type="password" class="%1$s-text" id="%2$s[%3$s]" name="%2$s[%3$s]" value="%4$s"/>', $size, $args['section'], $args['id'], $value );
390 $html .= $this->get_field_description( $args );
391
392 echo $html;
393 }
394
395 /**
396 * Displays a color picker field for a settings field
397 *
398 * @param array $args settings field args
399 */
400 function callback_color( $args ) {
401
402 $value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) );
403 $size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular';
404
405 $html = sprintf( '<input type="text" class="%1$s-text wp-color-picker-field" id="%2$s[%3$s]" name="%2$s[%3$s]" value="%4$s" data-default-color="%5$s" />', $size, $args['section'], $args['id'], $value, $args['std'] );
406 $html .= $this->get_field_description( $args );
407
408 echo $html;
409 }
410
411
412 /**
413 * Displays a select box for creating the pages select box
414 *
415 * @param array $args settings field args
416 */
417 function callback_pages( $args ) {
418
419 $dropdown_args = array(
420 'selected' => esc_attr($this->get_option($args['id'], $args['section'], $args['std'] ) ),
421 'name' => $args['section'] . '[' . $args['id'] . ']',
422 'id' => $args['section'] . '[' . $args['id'] . ']',
423 'echo' => 0
424 );
425 $html = wp_dropdown_pages( $dropdown_args );
426 echo $html;
427 }
428
429 /**
430 * Sanitize callback for Settings API
431 *
432 * @return mixed
433 */
434 function sanitize_options( $options ) {
435
436 if ( !$options ) {
437 return $options;
438 }
439
440 foreach( $options as $option_slug => $option_value ) {
441 $sanitize_callback = $this->get_sanitize_callback( $option_slug );
442
443 // If callback is set, call it
444 if ( $sanitize_callback ) {
445 $options[ $option_slug ] = call_user_func( $sanitize_callback, $option_value );
446 continue;
447 }
448 }
449
450 return $options;
451 }
452
453 /**
454 * Get sanitization callback for given option slug
455 *
456 * @param string $slug option slug
457 *
458 * @return mixed string or bool false
459 */
460 function get_sanitize_callback( $slug = '' ) {
461 if ( empty( $slug ) ) {
462 return false;
463 }
464
465 // Iterate over registered fields and see if we can find proper callback
466 foreach( $this->settings_fields as $section => $options ) {
467 foreach ( $options as $option ) {
468 if ( $option['name'] != $slug ) {
469 continue;
470 }
471
472 // Return the callback name
473 return isset( $option['sanitize_callback'] ) && is_callable( $option['sanitize_callback'] ) ? $option['sanitize_callback'] : false;
474 }
475 }
476
477 return false;
478 }
479
480 /**
481 * Get the value of a settings field
482 *
483 * @param string $option settings field name
484 * @param string $section the section name this field belongs to
485 * @param string $default default text if it's not found
486 * @return string
487 */
488 function get_option( $option, $section, $default = '' ) {
489
490 $options = get_option( $section );
491
492 if ( isset( $options[$option] ) ) {
493 return $options[$option];
494 }
495
496 return $default;
497 }
498
499 /**
500 * Show navigations as tab
501 *
502 * Shows all the settings section labels as tab
503 */
504 function show_navigation() {
505 $html = '<h2 class="nav-tab-wrapper">';
506
507 $count = count( $this->settings_sections );
508
509 // don't show the navigation if only one section exists
510 if ( $count === 1 ) {
511 return;
512 }
513
514 foreach ( $this->settings_sections as $tab ) {
515 $html .= sprintf( '<a href="#%1$s" class="nav-tab" id="%1$s-tab">%2$s</a>', $tab['id'], $tab['title'] );
516 }
517
518 $html .= '</h2>';
519
520 echo $html;
521 }
522
523 /**
524 * Show the section settings forms
525 *
526 * This function displays every sections in a different form
527 */
528 function show_forms() {
529 ?>
530 <div class="metabox-holder">
531 <?php foreach ( $this->settings_sections as $form ) { ?>
532 <div id="<?php echo $form['id']; ?>" class="group" style="display: none;">
533 <form method="post" action="options.php">
534 <?php
535 do_action( 'wsa_form_top_' . $form['id'], $form );
536 settings_fields( $form['id'] );
537 do_settings_sections( $form['id'] );
538 do_action( 'wsa_form_bottom_' . $form['id'], $form );
539 if ( isset( $this->settings_fields[ $form['id'] ] ) ):
540 ?>
541 <div style="padding-left: 10px">
542 <?php submit_button(); ?>
543 </div>
544 <?php endif; ?>
545 </form>
546 </div>
547 <?php } ?>
548 </div>
549 <?php
550 $this->script();
551 }
552
553 /**
554 * Tabbable JavaScript codes & Initiate Color Picker
555 *
556 * This code uses localstorage for displaying active tabs
557 */
558 function script() {
559 ?>
560 <script>
561 jQuery(document).ready(function($) {
562 //Initiate Color Picker
563 $('.wp-color-picker-field').wpColorPicker();
564
565 // Switches option sections
566 $('.group').hide();
567 var activetab = '';
568 if (typeof(localStorage) != 'undefined' ) {
569 activetab = localStorage.getItem("activetab");
570 }
571
572 //if url has section id as hash then set it as active or override the current local storage value
573 if(window.location.hash){
574 activetab = window.location.hash;
575 if (typeof(localStorage) != 'undefined' ) {
576 localStorage.setItem("activetab", activetab);
577 }
578 }
579
580 if (activetab != '' && $(activetab).length ) {
581 $(activetab).fadeIn();
582 } else {
583 $('.group:first').fadeIn();
584 }
585 $('.group .collapsed').each(function(){
586 $(this).find('input:checked').parent().parent().parent().nextAll().each(
587 function(){
588 if ($(this).hasClass('last')) {
589 $(this).removeClass('hidden');
590 return false;
591 }
592 $(this).filter('.hidden').removeClass('hidden');
593 });
594 });
595
596 if (activetab != '' && $(activetab + '-tab').length ) {
597 $(activetab + '-tab').addClass('nav-tab-active');
598 }
599 else {
600 $('.nav-tab-wrapper a:first').addClass('nav-tab-active');
601 }
602 $('.nav-tab-wrapper a').click(function(evt) {
603 $('.nav-tab-wrapper a').removeClass('nav-tab-active');
604 $(this).addClass('nav-tab-active').blur();
605 var clicked_group = $(this).attr('href');
606 if (typeof(localStorage) != 'undefined' ) {
607 localStorage.setItem("activetab", $(this).attr('href'));
608 }
609 $('.group').hide();
610 $(clicked_group).fadeIn();
611 evt.preventDefault();
612 });
613
614 $('.wpsa-browse').on('click', function (event) {
615 event.preventDefault();
616
617 var self = $(this);
618
619 // Create the media frame.
620 var file_frame = wp.media.frames.file_frame = wp.media({
621 title: self.data('uploader_title'),
622 button: {
623 text: self.data('uploader_button_text'),
624 },
625 multiple: false
626 });
627
628 file_frame.on('select', function () {
629 attachment = file_frame.state().get('selection').first().toJSON();
630 self.prev('.wpsa-url').val(attachment.url).change();
631 });
632
633 // Finally, open the modal
634 file_frame.open();
635 });
636 });
637 </script>
638 <?php
639 $this->_style_fix();
640 }
641
642 function _style_fix() {
643 global $wp_version;
644
645 if (version_compare($wp_version, '3.8', '<=')):
646 ?>
647 <style type="text/css">
648 /** WordPress 3.8 Fix **/
649 .form-table th { padding: 20px 10px; }
650 #wpbody-content .metabox-holder { padding-top: 5px; }
651 </style>
652 <?php
653 endif;
654 }
655
656 }
657
658 endif;
659