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

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