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

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