PluginProbe
CBX Map for Google Map & OpenStreetMap / 1.1.10
CBX Map for Google Map & OpenStreetMap v1.1.10
trunk 1.1.10 1.1.11 1.1.12 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5
cbxgooglemap / includes / class-cbxgooglemap-settings.php

class-cbxgooglemap-settings.php in CBX Map for Google Map & OpenStreetMap 1.1.10, at includes/class-cbxgooglemap-settings.php

693 lines 27.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // If this file is called directly, abort.
3 if (!defined('WPINC')) {
4 die;
5 }
6
7 /**
8 * Wordpress Settings API wrapper class
9 *
10 * @version 1.2
11 * Last update: 27.09.2016
12 *
13 * Initial version Forked from https://github.com/tareq1988/wordpress-settings-api-class
14 *
15 */
16 if (!class_exists('CBXGooglemapSettings')):
17
18 class CBXGooglemapSettings {
19
20 /**
21 * settings sections array
22 *
23 * @var array
24 */
25 protected $settings_sections = array();
26
27 /**
28 * Settings fields array
29 *
30 * @var array
31 */
32 protected $settings_fields = array();
33
34 public function __construct() {
35
36 }
37
38 /**
39 * Set settings sections
40 *
41 * @param array $sections setting sections array
42 */
43 function set_sections( $sections ) {
44 $this->settings_sections = $sections;
45
46 return $this;
47 }
48
49 /**
50 * Add a single section
51 *
52 * @param array $section
53 */
54 function add_section( $section ) {
55 $this->settings_sections[] = $section;
56
57 return $this;
58 }
59
60 /**
61 * Set settings fields
62 *
63 * @param array $fields settings fields array
64 */
65 function set_fields( $fields ) {
66 $this->settings_fields = $fields;
67
68 return $this;
69 }
70
71 function add_field( $section, $field ) {
72 $defaults = array(
73 'name' => '',
74 'label' => '',
75 'desc' => '',
76 'type' => 'text'
77 );
78
79 $arg = wp_parse_args( $field, $defaults );
80 $this->settings_fields[$section][] = $arg;
81
82 return $this;
83 }
84
85 /**
86 * Initialize and registers the settings sections and fileds to WordPress
87 *
88 * Usually this should be called at `admin_init` hook.
89 *
90 * This function gets the initiated settings sections and fields. Then
91 * registers them to WordPress and ready for use.
92 */
93 function admin_init() {
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 add_settings_section( $section['id'], $section['title'], $callback, $section['id'] );
110 }
111
112 //register settings fields
113 foreach ( $this->settings_fields as $section => $field ) {
114 foreach ( $field as $option ) {
115
116 $name = $option['name'];
117 $type = isset( $option['type'] ) ? $option['type'] : 'text';
118
119 $args = array(
120 'id' => $option['name'],
121 'class' => isset( $option['class'] ) ? $option['class'] : $name,
122 'label_for' => $args['label_for'] = "{$section}[{$option['name']}]",
123 'desc' => isset( $option['desc'] ) ? $option['desc'] : '',
124 'name' => $option['label'],
125 'section' => $section,
126 'size' => isset( $option['size'] ) ? $option['size'] : null,
127 'options' => isset( $option['options'] ) ? $option['options'] : '',
128 'default' => isset( $option['default'] ) ? $option['default'] : '',
129 'sanitize_callback' => isset( $option['sanitize_callback'] ) ? $option['sanitize_callback'] : '',
130 'type' => $type,
131 'placeholder' => isset( $option['placeholder'] ) ? $option['placeholder'] : '',
132 'optgroup' => isset( $option['optgroup'] ) ? intval($option['optgroup']) : 0
133 );
134
135 add_settings_field( $section . '[' . $option['name'] . ']', $option['label'], array( $this, 'callback_' . $type ), $section, $section, $args );
136 }
137 }
138
139 // creates our settings in the options table
140 foreach ( $this->settings_sections as $section ) {
141 register_setting( $section['id'], $section['id'], array( $this, 'sanitize_options' ) );
142 }
143 }
144
145 /**
146 * Get field description for display
147 *
148 * @param array $args settings field args
149 */
150 public function get_field_description( $args ) {
151 if ( ! empty( $args['desc'] ) ) {
152 $desc = sprintf( '<p class="description">%s</p>', $args['desc'] );
153 } else {
154 $desc = '';
155 }
156
157 return $desc;
158 }
159
160 /**
161 * Displays a text field for a settings field
162 *
163 * @param array $args settings field args
164 */
165 function callback_text( $args ) {
166
167
168 $value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['default'] ) );
169
170
171
172 $size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular';
173 $type = isset( $args['type'] ) ? $args['type'] : 'text';
174
175
176 $html = sprintf( '<input type="%1$s" class="%2$s-text" id="%3$s[%4$s]" name="%3$s[%4$s]" value="%5$s" placeholder="%6$s" />', $type, $size, $args['section'], $args['id'], $value, $args['placeholder'] );
177 $html .= $this->get_field_description( $args );
178
179 echo $html;
180 }
181
182 /**
183 * Displays a text field for a settings field
184 *
185 * @param array $args settings field args
186 */
187 function callback_location( $args ) {
188
189
190 $value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['default'] ) );
191
192
193
194 $size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular';
195 $type = isset( $args['type'] ) ? $args['type'] : 'text';
196
197
198 $html = sprintf( '<input type="%1$s" class="%2$s-text" id="%3$s[%4$s]" name="%3$s[%4$s]" value="%5$s" placeholder="%6$s" />', $type, $size, $args['section'], $args['id'], $value, $args['placeholder'] );
199 $html .= $this->get_field_description( $args );
200
201 echo $html;
202 }
203
204 /**
205 * Displays a url field for a settings field
206 *
207 * @param array $args settings field args
208 */
209 function callback_url( $args ) {
210 $this->callback_text( $args );
211 }
212
213 /**
214 * Displays a number field for a settings field
215 *
216 * @param array $args settings field args
217 */
218 function callback_number( $args ) {
219 $this->callback_text( $args );
220 }
221
222 /**
223 * Displays a checkbox for a settings field
224 *
225 * @param array $args settings field args
226 */
227 function callback_checkbox( $args ) {
228
229 $value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['default'] ) );
230
231 $html = '<fieldset>';
232 $html .= sprintf( '<label for="wpuf-%1$s[%2$s]">', $args['section'], $args['id'] );
233 $html .= sprintf( '<input type="hidden" name="%1$s[%2$s]" value="off" />', $args['section'], $args['id'] );
234 $html .= sprintf( '<input type="checkbox" class="checkbox" id="wpuf-%1$s[%2$s]" name="%1$s[%2$s]" value="on" %3$s />', $args['section'], $args['id'], checked( $value, 'on', false ) );
235 $html .= sprintf( '%1$s</label>', $args['desc'] );
236 $html .= '</fieldset>';
237
238 echo $html;
239 }
240
241
242
243 /**
244 * Displays a multicheckbox a settings field
245 *
246 * @param array $args settings field args
247 */
248 function callback_radio( $args ) {
249
250 $value = $this->get_option( $args['id'], $args['section'], $args['default'] );
251
252 if(!isset($args['options'][$value])) $value = $args['default'];
253
254
255
256 $html = '<fieldset>';
257
258 foreach ( $args['options'] as $key => $label ) {
259 $html .= sprintf( '<label for="wpuf-%1$s[%2$s][%3$s]">', $args['section'], $args['id'], $key );
260 $html .= sprintf( '<input type="radio" class="radio" id="wpuf-%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 ) );
261 $html .= sprintf( '%1$s</label><br>', $label );
262 }
263
264 $html .= $this->get_field_description( $args );
265 $html .= '</fieldset>';
266
267 echo $html;
268 }
269
270 /**
271 * Displays a selectbox for a settings field
272 *
273 * @param array $args settings field args
274 */
275 function callback_select( $args ) {
276
277 $value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['default'] ) );
278 $size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular selecttwo-select';
279 $html = sprintf( '<select class="%1$s" name="%2$s[%3$s]" id="%2$s[%3$s]">', $size, $args['section'], $args['id'] );
280
281 foreach ( $args['options'] as $key => $label ) {
282 $html .= sprintf( '<option value="%s"%s>%s</option>', $key, selected( $value, $key, false ), $label );
283 }
284
285 $html .= sprintf( '</select>' );
286 $html .= $this->get_field_description( $args );
287
288 echo $html;
289 }
290
291 /**
292 * Displays a multi-selectbox for a settings field
293 *
294 * @param array $args settings field args
295 */
296 function callback_multiselect($args) {
297
298
299 $value = $this->get_option($args['id'], $args['section'], $args['default']);
300
301 if(!is_array($value)) $value = array();
302
303 $size = isset($args['size']) && !is_null($args['size']) ? $args['size'] : 'regular selecttwo-select';
304
305 $html = sprintf('<input type="hidden" name="%1$s[%2$s][]" value="" />', $args['section'], $args['id']);
306 $html .= sprintf('<select multiple class="%1$s" name="%2$s[%3$s][]" id="%2$s[%3$s]" style="min-width: 150px !important;" placeholder="%4$s" data-placeholder="%4$s">', $size, $args['section'], $args['id'], $args['placeholder']);
307
308
309 if(isset($args['optgroup']) && $args['optgroup']){
310 foreach ($args['options'] as $opt_grouplabel => $dataOpt) {
311
312
313 if(!is_array($dataOpt)) $dataOpt = array();
314 $data = isset($dataOpt['data']) ? $dataOpt['data']: array();
315 $opt_label = isset($dataOpt['label']) ? esc_attr($dataOpt['label']) : ucfirst($opt_grouplabel);
316
317 if(!is_array($data)) $data = array();
318
319 $html .= '<optgroup label="' . $opt_label . '">';
320
321
322
323 foreach ($data as $key => $val) {
324 $selected = in_array($key, $value)? ' selected="selected" ': '';
325 $html .= sprintf('<option value="%s" '.$selected.'>%s</option>', $key, $val);
326 }
327 $html .= '<optgroup>';
328 }
329 }
330 else{
331 foreach ($args['options'] as $key => $val) {
332 $selected = in_array($key, $value)? ' selected="selected" ': '';
333 $html .= sprintf('<option value="%s" '.$selected.'>%s</option>', $key, $val);
334 }
335 }
336
337 $html .= sprintf('</select>');
338 $html .= $this->get_field_description($args);
339
340 echo $html;
341 }
342
343 /**
344 * Displays a multicheckbox a settings field
345 *
346 * @param array $args settings field args
347 */
348 /* function callback_multicheck( $args ) {
349
350 $value = $this->get_option( $args['id'], $args['section'], $args['default'] );
351 $html = '<fieldset>';
352
353 foreach ( $args['options'] as $key => $label ) {
354 $checked = isset( $value[$key] ) ? $value[$key] : '0';
355 $html .= sprintf( '<label for="wpuf-%1$s[%2$s][%3$s]">', $args['section'], $args['id'], $key );
356 $html .= sprintf('<input type="hidden" name="%1$s[%2$s][]" value="" />', $args['section'], $args['id']);
357 $html .= sprintf( '<input type="checkbox" class="checkbox" id="wpuf-%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 ) );
358 $html .= sprintf( '%1$s</label><br>', $label );
359 }
360
361 $html .= $this->get_field_description( $args );
362 $html .= '</fieldset>';
363
364 echo $html;
365 }*/
366
367 /**
368 * Displays a multicheckbox settings field
369 *
370 * @param array $args settings field args
371 */
372 function callback_multicheck($args)
373 {
374
375 $value = $this->get_option($args['id'], $args['section'], $args['default']);
376 if(!is_array($value)) $value = array();
377
378 $html = '<fieldset class="multicheck_fields">';
379 foreach ($args['options'] as $key => $label) {
380
381 //$checked = isset($value[$key]) ? $value[$key] : '0';
382 $checked = in_array($key, $value)? ' checked="checked" ' : '';
383
384 $html .= sprintf('<p class="multicheck_field"><!--<span class="multicheck_field_handle"><i class="dashicons dashicons-move"></i></span>--><label for="wpuf-%1$s[%2$s][%3$s]">', $args['section'], $args['id'], $key);
385 $html .= sprintf('<input type="hidden" name="%1$s[%2$s][]" value="" />', $args['section'], $args['id']);
386 $html .= sprintf('<input type="checkbox" class="checkbox" id="wpuf-%1$s[%2$s][%3$s]" name="%1$s[%2$s][%3$s]" value="%3$s" %4$s />', $args['section'], $args['id'], $key, $checked);
387 $html .= sprintf('%1$s</label></p>', $label);
388 }
389 $html .= $this->get_field_description($args);
390 $html .= '</fieldset>';
391
392 echo $html;
393 }
394
395
396 /**
397 * Displays a info field
398 *
399 * @param array $args settings field args
400 */
401 function callback_info($args) {
402 $html = $args['desc'];
403 echo $html;
404 }
405
406 /**
407 * Displays a textarea for a settings field
408 *
409 * @param array $args settings field args
410 */
411 function callback_textarea( $args ) {
412
413 $value = esc_textarea( $this->get_option( $args['id'], $args['section'], $args['default'] ) );
414 $size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular';
415
416 $html = sprintf( '<textarea rows="5" cols="55" class="%1$s-text" id="%2$s[%3$s]" name="%2$s[%3$s]">%4$s</textarea>', $size, $args['section'], $args['id'], $value );
417 $html .= $this->get_field_description( $args );
418
419 echo $html;
420 }
421
422 /**
423 * Displays a textarea for a settings field
424 *
425 * @param array $args settings field args
426 * @return string
427 */
428 function callback_html( $args ) {
429 echo $this->get_field_description( $args );
430 }
431
432 /**
433 * Displays a rich text textarea for a settings field
434 *
435 * @param array $args settings field args
436 */
437 function callback_wysiwyg( $args ) {
438
439 $value = $this->get_option( $args['id'], $args['section'], $args['default'] );
440 $size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : '500px';
441
442 echo '<div style="max-width: ' . $size . ';">';
443
444 $editor_settings = array(
445 'teeny' => true,
446 'textarea_name' => $args['section'] . '[' . $args['id'] . ']',
447 'textarea_rows' => 10
448 );
449
450 if ( isset( $args['options'] ) && is_array( $args['options'] ) ) {
451 $editor_settings = array_merge( $editor_settings, $args['options'] );
452 }
453
454 wp_editor( $value, $args['section'] . '-' . $args['id'], $editor_settings );
455
456 echo '</div>';
457
458 echo $this->get_field_description( $args );
459 }
460
461 /**
462 * Displays a file upload field for a settings field
463 *
464 * @param array $args settings field args
465 */
466 function callback_file( $args ) {
467
468 $value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['default'] ) );
469 $size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular';
470 $id = $args['section'] . '[' . $args['id'] . ']';
471 $label = isset( $args['options']['button_label'] ) ? $args['options']['button_label'] : esc_html__( 'Choose File', 'cbxgooglemap' );
472
473 $html = sprintf( '<input type="text" class="%1$s-text wpsa-url" id="%2$s[%3$s]" name="%2$s[%3$s]" value="%4$s"/>', $size, $args['section'], $args['id'], $value );
474 $html .= '<input type="button" class="button wpsa-browse" value="' . $label . '" />';
475 $html .= $this->get_field_description( $args );
476
477 echo $html;
478 }
479
480 /**
481 * Displays a password field for a settings field
482 *
483 * @param array $args settings field args
484 */
485 function callback_password( $args ) {
486
487 $value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['default'] ) );
488 $size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular';
489
490 $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 );
491 $html .= $this->get_field_description( $args );
492
493 echo $html;
494 }
495
496 /**
497 * Displays a color picker field for a settings field
498 *
499 * @param array $args settings field args
500 */
501 function callback_color( $args ) {
502
503 $value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['default'] ) );
504 $size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular';
505
506 $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['default'] );
507 $html .= $this->get_field_description( $args );
508
509 echo $html;
510 }
511
512 /**
513 * Displays a textarea for a settings field
514 *
515 * @param array $args settings field args
516 */
517 function callback_shortcode( $args ) {
518 $value = $args['default'];
519 $value_esc = esc_textarea( $value );
520 $size = isset( $args['size'] ) && ! is_null( $args['size'] ) ? $args['size'] : 'regular';
521 $class = isset( $args['class'] ) && ! is_null( $args['class'] ) ? $args['class'] : '';
522
523 $required = isset( $args['required'] ) ? 'required' : '';
524
525
526 $html = sprintf( '<textarea readonly rows="5" cols="55" class="%1$s-text %6$s" id="%2$s[%3$s]" name="%2$s[%3$s]" %5$s>%4$s</textarea>',
527 $size,
528 $args['section'],
529 $args['id'],
530 $value_esc,
531 $required, $class );
532
533 $html .= '<a data-target-cp="#' . $args['section'] . '\\[' . $args['id'] . '\\]' . '" class="shortcode_demo_btn" href="#">Click to copy shortcode</a>';
534 $html .= $this->get_field_description( $args );
535
536
537 $html .= '<div class="shortcode_demo_wrap">' . do_shortcode( $value ) . '</div>';
538
539 echo $html;
540 }
541
542 /**
543 * Sanitize callback for Settings API
544 */
545 function sanitize_options( $options ) {
546 if(!is_array($options)) $options = array();
547
548 foreach( $options as $option_slug => $option_value ) {
549 $sanitize_callback = $this->get_sanitize_callback( $option_slug );
550
551 // If callback is set, call it
552 if ( $sanitize_callback ) {
553 $options[ $option_slug ] = call_user_func( $sanitize_callback, $option_value );
554 continue;
555 }
556 }
557
558 return $options;
559 }
560
561 /**
562 * Get sanitization callback for given option slug
563 *
564 * @param string $slug option slug
565 *
566 * @return mixed string or bool false
567 */
568 function get_sanitize_callback( $slug = '' ) {
569 if ( empty( $slug ) ) {
570 return false;
571 }
572
573 // Iterate over registered fields and see if we can find proper callback
574 foreach( $this->settings_fields as $section => $options ) {
575 foreach ( $options as $option ) {
576 if ( $option['name'] != $slug ) {
577 continue;
578 }
579
580 if($option['type'] == 'multiselect' || $option['type'] == 'multicheck'){
581 $option['sanitize_callback'] = array($this, 'sanitize_multi_select_check');
582 }
583
584 // Return the callback name
585 return isset( $option['sanitize_callback'] ) && is_callable( $option['sanitize_callback'] ) ? $option['sanitize_callback'] : false;
586 }
587 }
588
589 return false;
590 }
591
592 /**
593 * Remove empty values from multi select fields (multi select and multi checkbox)
594 *
595 * @param $option_value
596 *
597 * @return array
598 */
599 public function sanitize_multi_select_check($option_value){
600 if(is_array($option_value)){
601 return array_filter($option_value);
602 }
603 return $option_value;
604 }
605
606 /**
607 * Get the value of a settings field
608 *
609 * @param string $option settings field name
610 * @param string $section the section name this field belongs to
611 * @param string $default default text if it's not found
612 * @return string
613 */
614 function get_option( $option, $section, $default = '' ) {
615
616 $options = get_option( $section );
617
618 if ( isset( $options[$option] ) ) {
619 return $options[$option];
620 }
621
622 return $default;
623 }
624
625 /**
626 * Get value from option data if field set, returns default if not set
627 *
628 * @param $option
629 * @param array $section
630 * @param string $default
631 *
632 * @return mixed|string
633 */
634 function get_field( $option, $section = array(), $default = '' ) {
635 if ( isset( $section[$option] ) ) {
636 return $section[$option];
637 }
638
639 return $default;
640 }//end get_field
641
642 /**
643 * Show navigations as tab
644 *
645 * Shows all the settings section labels as tab
646 */
647 function show_navigation() {
648 $html = '<h2 class="nav-tab-wrapper">';
649
650 $i = 0;
651 foreach ( $this->settings_sections as $tab ) {
652 $extra_tab_class = ( $i === 0 ) ? 'nav-tab-active' : '';
653 $html .= sprintf( '<a data-tabid="' . $tab['id'] . '" href="#%1$s" class="nav-tab %3$s" id="%1$s-tab">%2$s</a>', $tab['id'], $tab['title'], $extra_tab_class );
654 $i ++;
655 }
656
657 $html .= '</h2>';
658
659 echo $html;
660 }
661
662 /**
663 * Show the section settings forms
664 *
665 * This function displays every sections in a different form
666 */
667 function show_forms() {
668 ?>
669 <div class="metabox-holder">
670 <?php
671 $i = 0;
672 foreach ( $this->settings_sections as $form ) {
673 $display_style = ( $i === 0 ) ? '' : 'display: none;';
674 ?>
675 <div id="<?php echo $form['id']; ?>" class="cbxgooglemap_group" style="<?php echo $display_style; ?>">
676 <form method="post" action="options.php">
677 <?php
678 do_action( 'cbxgooglemap_form_top_' . $form['id'], $form );
679 settings_fields( $form['id'] );
680 do_settings_sections( $form['id'] );
681 do_action( 'cbxgooglemap_form_bottom_' . $form['id'], $form );
682 ?>
683 <div style="padding-left: 10px">
684 <?php submit_button(esc_html__('Save Settings', 'cbxgooglemap'), 'primary submit_cbxgooglemap', 'submit', true, array('id' => 'submit_'.esc_attr($form['id']))); ?>
685 </div>
686 </form>
687 </div>
688 <?php } ?>
689 </div>
690 <?php
691 }
692 }//end class CBXGooglemapSettings
693 endif;