PluginProbe
Ever Compare – Products Compare Plugin for WooCommerce / 1.3.2
Ever Compare – Products Compare Plugin for WooCommerce v1.3.2
1.3.7 1.1.7 1.1.8 1.1.9 1.2.0 1.2.1 1.2.2 1.2.3 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 1.3.4 1.3.5 1.3.6 trunk 1.0.0 1.0.1 1.0.2 All 31 releases
ever-compare / includes / classes / Admin / Settings_APi.php

Settings_APi.php in Ever Compare – Products Compare Plugin for WooCommerce 1.3.2, at includes/classes/Admin/Settings_APi.php

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