PluginProbe
CBX Map for Google Map & OpenStreetMap / 1.1.7
CBX Map for Google Map & OpenStreetMap v1.1.7
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.7, at includes/class-cbxgooglemap-settings.php

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