PluginProbe
StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more / 3.7.2
StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more v3.7.2
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 3.7.2, at vendor/tareq1988/wordpress-settings-api-class/src/class.settings-api.php

584 lines 19.2 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.2 (18-Oct-2015)
7 *
8 * @author Tareq Hasan <[email protected]>
9 * @link http://tareq.weDevs.com Tareq's Planet
10 * @example src/settings-api.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) { echo str_replace('"', '\"', $section['desc']).'";'; };
109 } else if ( isset( $section['callback'] ) ) {
110 $callback = $section['callback'];
111 } else {
112 $callback = null;
113 }
114
115 add_settings_section( $section['id'], $section['title'], $callback, $section['id'] );
116 }
117
118 //register settings fields
119 foreach ( $this->settings_fields as $section => $field ) {
120 foreach ( $field as $option ) {
121
122 $type = isset( $option['type'] ) ? $option['type'] : 'text';
123
124 $args = array(
125 'id' => $option['name'],
126 'label_for' => $args['label_for'] = "{$section}[{$option['name']}]",
127 'desc' => isset( $option['desc'] ) ? $option['desc'] : '',
128 'name' => $option['label'],
129 'section' => $section,
130 'size' => isset( $option['size'] ) ? $option['size'] : null,
131 'options' => isset( $option['options'] ) ? $option['options'] : '',
132 'std' => isset( $option['default'] ) ? $option['default'] : '',
133 'sanitize_callback' => isset( $option['sanitize_callback'] ) ? $option['sanitize_callback'] : '',
134 'type' => $type,
135 );
136
137 add_settings_field( $section . '[' . $option['name'] . ']', $option['label'], array( $this, 'callback_' . $type ), $section, $section, $args );
138 }
139 }
140
141 // creates our settings in the options table
142 foreach ( $this->settings_sections as $section ) {
143 register_setting( $section['id'], $section['id'], array( $this, 'sanitize_options' ) );
144 }
145 }
146
147 /**
148 * Get field description for display
149 *
150 * @param array $args settings field args
151 */
152 public function get_field_description( $args ) {
153 if ( ! empty( $args['desc'] ) ) {
154 $desc = sprintf( '<p class="description">%s</p>', $args['desc'] );
155 } else {
156 $desc = '';
157 }
158
159 return $desc;
160 }
161
162 /**
163 * Displays a text field for a settings field
164 *
165 * @param array $args settings field args
166 */
167 function callback_text( $args ) {
168
169 $value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) );
170 $size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular';
171 $type = isset( $args['type'] ) ? $args['type'] : 'text';
172
173 $html = sprintf( '<input type="%1$s" class="%2$s-text" id="%3$s[%4$s]" name="%3$s[%4$s]" value="%5$s"/>', $type, $size, $args['section'], $args['id'], $value );
174 $html .= $this->get_field_description( $args );
175
176 echo $html;
177 }
178
179 /**
180 * Displays a url field for a settings field
181 *
182 * @param array $args settings field args
183 */
184 function callback_url( $args ) {
185 $this->callback_text( $args );
186 }
187
188 /**
189 * Displays a number field for a settings field
190 *
191 * @param array $args settings field args
192 */
193 function callback_number( $args ) {
194 $this->callback_text( $args );
195 }
196
197 /**
198 * Displays a checkbox for a settings field
199 *
200 * @param array $args settings field args
201 */
202 function callback_checkbox( $args ) {
203
204 $value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) );
205
206 $html = '<fieldset>';
207 $html .= sprintf( '<label for="wpuf-%1$s[%2$s]">', $args['section'], $args['id'] );
208 $html .= sprintf( '<input type="hidden" name="%1$s[%2$s]" value="off" />', $args['section'], $args['id'] );
209 $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 ) );
210 $html .= sprintf( '%1$s</label>', $args['desc'] );
211 $html .= '</fieldset>';
212
213 echo $html;
214 }
215
216 /**
217 * Displays a multicheckbox a settings field
218 *
219 * @param array $args settings field args
220 */
221 function callback_multicheck( $args ) {
222
223 $value = $this->get_option( $args['id'], $args['section'], $args['std'] );
224 $html = '<fieldset>';
225
226 foreach ( $args['options'] as $key => $label ) {
227 $checked = isset( $value[$key] ) ? $value[$key] : '0';
228 $html .= sprintf( '<label for="wpuf-%1$s[%2$s][%3$s]">', $args['section'], $args['id'], $key );
229 $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 ) );
230 $html .= sprintf( '%1$s</label><br>', $label );
231 }
232
233 $html .= $this->get_field_description( $args );
234 $html .= '</fieldset>';
235
236 echo $html;
237 }
238
239 /**
240 * Displays a multicheckbox a settings field
241 *
242 * @param array $args settings field args
243 */
244 function callback_radio( $args ) {
245
246 $value = $this->get_option( $args['id'], $args['section'], $args['std'] );
247 $html = '<fieldset>';
248
249 foreach ( $args['options'] as $key => $label ) {
250 $html .= sprintf( '<label for="wpuf-%1$s[%2$s][%3$s]">', $args['section'], $args['id'], $key );
251 $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 ) );
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 selectbox for a settings field
263 *
264 * @param array $args settings field args
265 */
266 function callback_select( $args ) {
267
268 $value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) );
269 $size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular';
270 $html = sprintf( '<select class="%1$s" name="%2$s[%3$s]" id="%2$s[%3$s]">', $size, $args['section'], $args['id'] );
271
272 foreach ( $args['options'] as $key => $label ) {
273 $html .= sprintf( '<option value="%s"%s>%s</option>', $key, selected( $value, $key, false ), $label );
274 }
275
276 $html .= sprintf( '</select>' );
277 $html .= $this->get_field_description( $args );
278
279 echo $html;
280 }
281
282 /**
283 * Displays a textarea for a settings field
284 *
285 * @param array $args settings field args
286 */
287 function callback_textarea( $args ) {
288
289 $value = esc_textarea( $this->get_option( $args['id'], $args['section'], $args['std'] ) );
290 $size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular';
291
292 $html = sprintf( '<textarea rows="5" cols="55" class="%1$s-text" id="%2$s[%3$s]" name="%2$s[%3$s]">%4$s</textarea>', $size, $args['section'], $args['id'], $value );
293 $html .= $this->get_field_description( $args );
294
295 echo $html;
296 }
297
298 /**
299 * Displays a textarea for a settings field
300 *
301 * @param array $args settings field args
302 * @return string
303 */
304 function callback_html( $args ) {
305 echo $this->get_field_description( $args );
306 }
307
308 /**
309 * Displays a rich text textarea for a settings field
310 *
311 * @param array $args settings field args
312 */
313 function callback_wysiwyg( $args ) {
314
315 $value = $this->get_option( $args['id'], $args['section'], $args['std'] );
316 $size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : '500px';
317
318 echo '<div style="max-width: ' . $size . ';">';
319
320 $editor_settings = array(
321 'teeny' => true,
322 'textarea_name' => $args['section'] . '[' . $args['id'] . ']',
323 'textarea_rows' => 10
324 );
325
326 if ( isset( $args['options'] ) && is_array( $args['options'] ) ) {
327 $editor_settings = array_merge( $editor_settings, $args['options'] );
328 }
329
330 wp_editor( $value, $args['section'] . '-' . $args['id'], $editor_settings );
331
332 echo '</div>';
333
334 echo $this->get_field_description( $args );
335 }
336
337 /**
338 * Displays a file upload field for a settings field
339 *
340 * @param array $args settings field args
341 */
342 function callback_file( $args ) {
343
344 $value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) );
345 $size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular';
346 $id = $args['section'] . '[' . $args['id'] . ']';
347 $label = isset( $args['options']['button_label'] ) ? $args['options']['button_label'] : __( 'Choose File' );
348
349 $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 );
350 $html .= '<input type="button" class="button wpsa-browse" value="' . $label . '" />';
351 $html .= $this->get_field_description( $args );
352
353 echo $html;
354 }
355
356 /**
357 * Displays a password field for a settings field
358 *
359 * @param array $args settings field args
360 */
361 function callback_password( $args ) {
362
363 $value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) );
364 $size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular';
365
366 $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 );
367 $html .= $this->get_field_description( $args );
368
369 echo $html;
370 }
371
372 /**
373 * Displays a color picker field for a settings field
374 *
375 * @param array $args settings field args
376 */
377 function callback_color( $args ) {
378
379 $value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) );
380 $size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular';
381
382 $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'] );
383 $html .= $this->get_field_description( $args );
384
385 echo $html;
386 }
387
388 /**
389 * Sanitize callback for Settings API
390 */
391 function sanitize_options( $options ) {
392 foreach( $options as $option_slug => $option_value ) {
393 $sanitize_callback = $this->get_sanitize_callback( $option_slug );
394
395 // If callback is set, call it
396 if ( $sanitize_callback ) {
397 $options[ $option_slug ] = call_user_func( $sanitize_callback, $option_value );
398 continue;
399 }
400 }
401
402 return $options;
403 }
404
405 /**
406 * Get sanitization callback for given option slug
407 *
408 * @param string $slug option slug
409 *
410 * @return mixed string or bool false
411 */
412 function get_sanitize_callback( $slug = '' ) {
413 if ( empty( $slug ) ) {
414 return false;
415 }
416
417 // Iterate over registered fields and see if we can find proper callback
418 foreach( $this->settings_fields as $section => $options ) {
419 foreach ( $options as $option ) {
420 if ( $option['name'] != $slug ) {
421 continue;
422 }
423
424 // Return the callback name
425 return isset( $option['sanitize_callback'] ) && is_callable( $option['sanitize_callback'] ) ? $option['sanitize_callback'] : false;
426 }
427 }
428
429 return false;
430 }
431
432 /**
433 * Get the value of a settings field
434 *
435 * @param string $option settings field name
436 * @param string $section the section name this field belongs to
437 * @param string $default default text if it's not found
438 * @return string
439 */
440 function get_option( $option, $section, $default = '' ) {
441
442 $options = get_option( $section );
443
444 if ( isset( $options[$option] ) ) {
445 return $options[$option];
446 }
447
448 return $default;
449 }
450
451 /**
452 * Show navigations as tab
453 *
454 * Shows all the settings section labels as tab
455 */
456 function show_navigation() {
457 $html = '<h2 class="nav-tab-wrapper">';
458
459 foreach ( $this->settings_sections as $tab ) {
460 $html .= sprintf( '<a href="#%1$s" class="nav-tab" id="%1$s-tab">%2$s</a>', $tab['id'], $tab['title'] );
461 }
462
463 $html .= '</h2>';
464
465 echo $html;
466 }
467
468 /**
469 * Show the section settings forms
470 *
471 * This function displays every sections in a different form
472 */
473 function show_forms() {
474 ?>
475 <div class="metabox-holder">
476 <?php foreach ( $this->settings_sections as $form ) { ?>
477 <div id="<?php echo $form['id']; ?>" class="group" style="display: none;">
478 <form method="post" action="options.php">
479 <?php
480 do_action( 'wsa_form_top_' . $form['id'], $form );
481 settings_fields( $form['id'] );
482 do_settings_sections( $form['id'] );
483 do_action( 'wsa_form_bottom_' . $form['id'], $form );
484 ?>
485 <div style="padding-left: 10px">
486 <?php submit_button(); ?>
487 </div>
488 </form>
489 </div>
490 <?php } ?>
491 </div>
492 <?php
493 $this->script();
494 }
495
496 /**
497 * Tabbable JavaScript codes & Initiate Color Picker
498 *
499 * This code uses localstorage for displaying active tabs
500 */
501 function script() {
502 ?>
503 <script>
504 jQuery(document).ready(function($) {
505 //Initiate Color Picker
506 $('.wp-color-picker-field').wpColorPicker();
507
508 // Switches option sections
509 $('.group').hide();
510 var activetab = '';
511 if (typeof(localStorage) != 'undefined' ) {
512 activetab = localStorage.getItem("activetab");
513 }
514 if (activetab != '' && $(activetab).length ) {
515 $(activetab).fadeIn();
516 } else {
517 $('.group:first').fadeIn();
518 }
519 $('.group .collapsed').each(function(){
520 $(this).find('input:checked').parent().parent().parent().nextAll().each(
521 function(){
522 if ($(this).hasClass('last')) {
523 $(this).removeClass('hidden');
524 return false;
525 }
526 $(this).filter('.hidden').removeClass('hidden');
527 });
528 });
529
530 if (activetab != '' && $(activetab + '-tab').length ) {
531 $(activetab + '-tab').addClass('nav-tab-active');
532 }
533 else {
534 $('.nav-tab-wrapper a:first').addClass('nav-tab-active');
535 }
536 $('.nav-tab-wrapper a').click(function(evt) {
537 $('.nav-tab-wrapper a').removeClass('nav-tab-active');
538 $(this).addClass('nav-tab-active').blur();
539 var clicked_group = $(this).attr('href');
540 if (typeof(localStorage) != 'undefined' ) {
541 localStorage.setItem("activetab", $(this).attr('href'));
542 }
543 $('.group').hide();
544 $(clicked_group).fadeIn();
545 evt.preventDefault();
546 });
547
548 $('.wpsa-browse').on('click', function (event) {
549 event.preventDefault();
550
551 var self = $(this);
552
553 // Create the media frame.
554 var file_frame = wp.media.frames.file_frame = wp.media({
555 title: self.data('uploader_title'),
556 button: {
557 text: self.data('uploader_button_text'),
558 },
559 multiple: false
560 });
561
562 file_frame.on('select', function () {
563 attachment = file_frame.state().get('selection').first().toJSON();
564
565 self.prev('.wpsa-url').val(attachment.url);
566 });
567
568 // Finally, open the modal
569 file_frame.open();
570 });
571 });
572 </script>
573
574 <style type="text/css">
575 /** WordPress 3.8 Fix **/
576 .form-table th { padding: 20px 10px; }
577 #wpbody-content .metabox-holder { padding-top: 5px; }
578 </style>
579 <?php
580 }
581
582 }
583 endif;
584