PluginProbe
Ever Compare – Products Compare Plugin for WooCommerce / 1.3.5
Ever Compare – Products Compare Plugin for WooCommerce v1.3.5
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.5, at includes/classes/Admin/Settings_APi.php

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