PluginProbe
HT Builder – WordPress Theme Builder for Elementor / 1.2.4
HT Builder – WordPress Theme Builder for Elementor v1.2.4
1.3.5 1.3.4 trunk 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.8 1.0.9 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3
ht-builder / includes / admin / classes / class.settings-api.php

class.settings-api.php in HT Builder – WordPress Theme Builder for Elementor 1.2.4, at includes/admin/classes/class.settings-api.php

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