PluginProbe
Booking Calendar / 10.1.3
Booking Calendar v10.1.3
11.8.3 11.8.2 11.8.1 11.8 11.7 11.6.1 11.6 11.5 11.4.3 11.4.2 11.4.1 11.4 11.3 11.2.1 11.2 11.1 11.0 10.15.7 10.15.6 10.1.3 10.10 10.10.1 10.10.2 10.11 10.11.2 All 203 releases
booking / core / any / class-admin-settings-api.php

class-admin-settings-api.php in Booking Calendar 10.1.3, at core/any/class-admin-settings-api.php

1,871 lines 73.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @version 1.0
4 * @package Settings
5 * @category Abstract Class
6 * @author wpdevelop
7 *
8 * @web-site https://wpbookingcalendar.com/
9 * @email info@wpbookingcalendar.com
10 * @modified 2015-10-06
11 */
12
13 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
14
15
16 abstract class WPBC_Settings_API {
17
18 /**
19 * ID of specific Settings page
20 * basically its prefix near each generated Input field
21 *
22 * @var String
23 */
24 public $id;
25
26 /**
27 * Define different options for CLASS - check more in constructor.
28 *
29 * @var array
30 */
31 public $options;
32
33 /**
34 * List of all settings rows
35 * @var array
36 */
37 public $fields = array();
38
39 /**
40 * Values of all settings fields
41 * @var array
42 */
43 public $fields_values = array();
44
45 /**
46 * Validated fields after $_POST request - for example before saving to DB
47 *
48 * @var array
49 */
50 public $validated_fields = array();
51
52
53 /**
54 * If setted, then we can validate all form fields, BEFORE post.
55 * Each form field that we need to validate have to have parameter: "validate_as" which ia array, like array('required')
56 * HTML fields elements will have additional CSS classes, like "validate-required"
57 * If this $validated_form_id empty so then no validation procedure.
58 *
59 * @var string
60 */
61 public $validated_form_id = '';
62 /**
63 * Constructor for Settings API
64 *
65 * @param string $id - unique ID of this Settings
66 * @param array $options - Optional. Array of parameters.
67 * array(
68 * 'db_prefix_option' => 'wpbc_'
69 * , 'db_saving_type' => 'togather' ['togather'|'separate'|'separate_prefix']
70 * )
71 * @param array $fields_values - optional array of values. If skipped, then case system try to get values from DB.
72 */
73 public function __construct( $id, $options = array(), $fields_values = array() ) {
74
75 $this->id = $id; // Define name of this settings page
76
77 $default_options = array(
78 'db_prefix_option' => 'wpbc_' // Prefix for adding to option name before saving to DB
79 , 'db_saving_type' => 'separate' // Type of DB saving: 'togather' | 'separate' | 'separate_prefix'
80 /* 'togather' - saving all parameters from settings page to one DB option
81 'separate' - saving each settings parameters as separate DB options
82 'separate_prefix' - saving each settings parameters as separate DB options
83 with prefix $id */
84 );
85
86 $this->options = wp_parse_args( $options, $default_options );
87
88 // Define what fields we are having
89 $this->init_settings_fields(); // Init all Fields Rows for settings page
90
91
92 // Get Values for the fields
93 if ( empty( $fields_values ) ) {
94 $this->define_fields_values_from_db(); // Define Fields Values from DB
95 } else {
96 $this->define_fields_values( $fields_values ); // Define Fields by transmited values
97 }
98
99 // Set Values for the fields
100 $this->set_values_to_fields(); // Assign $this->fields_values['some_field'] to $this->fields[ 'some_field' ]['value']
101
102 add_action( 'wpbc_after_settings_content', array($this, 'enqueue_validate_js'), 10, 3 ); // Add JavaScript if you need
103 }
104
105
106 /**
107 * Validate Form Fields before $_POST request
108 *
109 * @param string $page_tag
110 */
111 public function enqueue_validate_js( $page_tag, $active_page_tab, $active_page_subtab ) {
112
113 if ( empty( $this->validated_form_id ) )
114 return;
115
116 // Get Fields to validate
117 $fields_to_validate = array();
118 foreach ( $this->fields as $field_name => $field_values ) {
119
120 if ( ( isset( $field_values['validate_as'] ) ) && ( ! empty( $field_values['validate_as'] ) ) ) {
121 $field_html_id = $this->id . '_' . $field_name;
122 $fields_to_validate[ $field_html_id ] = $field_values['validate_as'];
123 }
124 }
125 // JavaScript //////////////////////////////////////////////////////////////
126
127 $js_script = '';
128
129 $js_script .= " jQuery('#". $this->validated_form_id ."').on( 'submit', function(){ "; // Catch Submit event
130
131 foreach ( $fields_to_validate as $field_html_id => $field_validate_array ) {
132
133 // Validate Required.
134 $js_script .= " if ( jQuery('#". $field_html_id ."').val() == '' ) { " . "\n" ;
135
136 $js_script .= " wpbc_field_highlight( '#". $field_html_id ."' );" . "\n" ;
137
138 //$js_script .= " wpbc_scroll_to( '#". $field_html_id ."' );" . "\n" ;
139 //$js_script .= " showWarningUnderElement( '#". $field_html_id ."' );" . "\n" ;
140
141 $js_script .= " return false; " . "\n" ; // - cancel event - Submit
142
143 $js_script .= " }" . "\n" ;
144 }
145
146 $js_script .= " } ); " . "\n";
147
148 // Eneque JS to the footer of the page
149 wpbc_enqueue_js( $js_script );
150 }
151
152 ////////////////////////////////////////////////////////////////////////////
153 // Abstract methods
154 ////////////////////////////////////////////////////////////////////////////
155
156 /**
157 * Init all fields rows for settings page
158 */
159 abstract public function init_settings_fields();
160
161
162
163
164 ////////////////////////////////////////////////////////////////////////////
165 // Functions
166 ////////////////////////////////////////////////////////////////////////////
167
168 /**
169 * Get array of Default fields Names and Values.
170 * Example:
171 * $default_values = array(
172 * 'my_date_format' => get_option('date_format')
173 * , 'my_time_format' => get_option('time_format')
174 * );
175 *
176 * @return array
177 */
178 public function get_default_values() {
179
180 $default_values = array();
181
182 foreach ( $this->fields as $field_name => $field_data ) {
183 if ( isset( $field_data['default'] ) ) {
184 $default_values[ $field_name ] = $field_data['default'];
185 } else {
186 // Action here if no default value in field.
187 }
188 }
189
190 return $default_values;
191 }
192
193
194 /**
195 * Define "Default Values" for all exist settings Fields.
196 *
197 * @param array $fields_values - Optional. Field values
198 */
199 public function define_fields_values( $fields_values = array(), $is_check_exist_values_from_fields = true ) {
200
201 // Default
202 if ( $is_check_exist_values_from_fields )
203 $default_values = $this->get_default_values();
204 else
205 $default_values = array();
206
207 // Parse
208 $this->fields_values = wp_parse_args( $fields_values, $default_values );
209
210 if ( $is_check_exist_values_from_fields ) { // We no need to check it after saving to DB
211 $defined_values_in_fields = array();
212 foreach ( $this->fields as $field_name => $field_data) {
213 if (isset($field_data['value'])) {
214 $defined_values_in_fields[$field_name] = $field_data['value'];
215 }
216 }
217 $this->fields_values = wp_parse_args( $defined_values_in_fields, $this->fields_values );
218 }
219 }
220
221
222
223 /**
224 * Set Values to Fiels
225 * Assign $this->fields_values to $fields[ 'some_field' ]['value']
226 */
227 public function set_values_to_fields() {
228
229 foreach ( $this->fields_values as $field_name => $field_value ) {
230
231 if ( isset( $this->fields[ $field_name ] ) ) {
232 $this->fields[ $field_name ][ 'value' ] = $field_value;
233 }
234 }
235 }
236
237
238 /**
239 * Get ID of this settings page
240 *
241 * @return string
242 */
243 public function get_id() {
244 return $this->id;
245 }
246
247
248 /**
249 * Get Value of specfic Field
250 * @param string $field_name
251 * @return mixed or false, if field does not exist
252 *
253 */
254 public function get_field_value( $field_name ) {
255
256 if ( empty( $this->fields_values ) )
257 $this->define_fields_values(); // If empty, then define by Default values
258
259 return isset( $this->fields_values[ $field_name ] ) ? $this->fields_values[ $field_name ] : false;
260 }
261
262
263
264 /**
265 * Set Value to specfic Field
266 * @param string $field_name
267 * @return mixed or false, if field does not exist
268 *
269 */
270 public function set_field_value( $field_name , $field_value ) {
271
272 if ( empty( $this->fields_values ) )
273 $this->define_fields_values(); // If empty, then define by Default values
274
275 $this->fields_values[ $field_name ] = $field_value;
276 }
277
278
279 /**
280 * Get all exist form fields
281 * and Init fields, if fields are empty.
282 *
283 * @return array
284 */
285 public function get_form_fields() {
286
287 if ( empty( $this->fields ) ) {
288 $this->init_settings_fields();
289 }
290
291 return $this->fields;
292 }
293
294
295 /**
296 * Generate Settings Table
297 */
298 public function show( $group = false ) {
299 ?>
300 <table class="form-table">
301 <?php $this->generate_settings_rows( $group ); ?>
302 </table><?php
303 }
304
305
306 /**
307 * Loop through the fields array and show settings.
308 */
309 public function generate_settings_rows( $group = false, $form_fields = false ) {
310
311 if ( ! $form_fields ) {
312 $form_fields = $this->get_form_fields();
313 }
314
315 $html = '';
316 foreach ( $form_fields as $k => $v ) {
317
318 $k = $this->id . '_' . $k;
319
320 if ( ! isset( $v['type'] ) || ( $v['type'] == '' ) ) {
321 $v['type'] = 'text';
322 }
323
324 if ( ( $group === false )
325 || ( ( ! isset( $v['group'] ) ) && ( $group == 'general' ) )
326 || ( ( isset( $v['group'] ) ) && ( $group == $v['group'] ) )
327 ) {
328 if ( method_exists( $this, 'field_' . $v['type'] . '_row' ) ) {
329 $html .= $this->{'field_' . $v['type'] . '_row'}( $k, $v );
330 } else {
331 $html .= $this->{'field_text_row'}( $k, $v );
332 }
333 }
334 }
335
336 echo $html;
337 }
338
339
340 // <editor-fold defaultstate="collapsed" desc=" G e n e r a t e F i e l d s " >
341
342 ////////////////////////////////////////////////////////////////////////////
343 // Input Fields
344 ////////////////////////////////////////////////////////////////////////////
345
346 /**
347 * Text Input Row
348 *
349 * @param string $field_name - name of field
350 * @param array $field - parameters
351 * @param boolean $echo - show or return reaults {default true}
352 * @return string - html || nothing
353 */
354 public function field_text_row( $field_name, $field, $echo = true ) {
355 if ( ! $echo )
356 return self::field_text_row_static( $field_name, $field, $echo );
357 else
358 self::field_text_row_static( $field_name, $field, $echo );
359 }
360
361
362 /**
363 * Text Color Row
364 *
365 * @param string $field_name - name of field
366 * @param array $field - parameters
367 * @param boolean $echo - show or return reaults {default true}
368 * @return string - html || nothing
369 */
370 public function field_color_row( $field_name, $field, $echo = true ) {
371 if ( ! $echo )
372 return self::field_color_row_static( $field_name, $field, $echo );
373 else
374 self::field_color_row_static( $field_name, $field, $echo );
375 }
376
377
378 /**
379 * Textarea Row
380 *
381 * @param string $field_name - name of field
382 * @param array $field - parameters
383 * @param boolean $echo - show or return reaults {default true}
384 * @return string - html || nothing
385 */
386 public function field_textarea_row( $field_name, $field, $echo = true ) {
387 if ( ! $echo )
388 return self::field_textarea_row_static( $field_name, $field, $echo );
389 else
390 self::field_textarea_row_static( $field_name, $field, $echo );
391 }
392
393
394 /**
395 * WP Rich Content Edit Textarea Row
396 *
397 * @param string $field_name - name of field
398 * @param array $field - parameters
399 * @param boolean $echo - show or return reaults {default true}
400 * @return string - html || nothing
401 */
402 public function field_wp_textarea_row( $field_name, $field, $echo = true ) {
403 if ( ! $echo )
404 return self::field_wp_textarea_row_static( $field_name, $field, $echo );
405 else
406 self::field_wp_textarea_row_static( $field_name, $field, $echo );
407 }
408
409
410 /**
411 * Radio buttons field row
412 *
413 * @param string $field_name - name of field
414 * @param array $field - parameters
415 * @param boolean $echo - show or return reaults {default true}
416 * @return string - html || nothing
417 */
418 public function field_radio_row( $field_name, $field, $echo = true ) {
419 if ( ! $echo )
420 return self::field_radio_row_static( $field_name, $field, $echo);
421 else
422 self::field_radio_row_static( $field_name, $field, $echo);
423 }
424
425
426 /**
427 * Selectbox field row
428 *
429 * @param string $field_name - name of field
430 * @param array $field - parameters
431 * @param boolean $echo - show or return reaults {default true}
432 * @return string - html || nothing
433 */
434 public function field_select_row( $field_name, $field, $echo = true ) {
435 if ( ! $echo )
436 return self::field_select_row_static( $field_name, $field, $echo );
437 else
438 self::field_select_row_static( $field_name, $field, $echo );
439 }
440
441
442 /**
443 * Checkbox field row
444 *
445 * @param string $field_name - name of field
446 * @param array $field - parameters
447 * @param boolean $echo - show or return reaults {default true}
448 * @return string - html || nothing
449 */
450 public function field_checkbox_row( $field_name, $field, $echo = true ) {
451 if ( ! $echo )
452 return self::field_checkbox_row_static( $field_name, $field, $echo );
453 else
454 self::field_checkbox_row_static( $field_name, $field, $echo );
455 }
456
457 ////////////////////////////////////////////////////////////////////////////
458 // Static Methods
459 ////////////////////////////////////////////////////////////////////////////
460
461 /**
462 * Static Text Input Row
463 *
464 * @param string $field_name - name of field
465 * @param array $field - parameters
466 * @param boolean $echo - show or return reaults {default true}
467 * @return string - html || nothing
468 */
469 public static function field_text_row_static( $field_name, $field, $echo = true ) {
470
471 $defaults = array(
472 'title' => '',
473 'disabled' => false,
474 'class' => '',
475 'css' => '',
476 'placeholder' => '',
477 'type' => 'text',
478 'description' => '',
479 'attr' => array(),
480 'group' => 'general',
481 'tr_class' => '',
482 'only_field' => false,
483 'description_tag' => 'p'
484 , 'validate_as' => array()
485 );
486
487 $field = wp_parse_args( $field, $defaults );
488
489 if ( ! $echo ) {
490 ob_start();
491 }
492
493 if ( ! $field['only_field'] ) {
494 ?>
495 <tr valign="top" class="wpbc_tr_<?php echo esc_attr( $field_name ), ' ', esc_attr( $field['tr_class'] ); ?>">
496 <th scope="row">
497 <?php echo self::label_static( $field_name, $field ); ?>
498 </th>
499 <td><fieldset><?php
500 }
501 ?>
502 <legend class="screen-reader-text"><span><?php echo wp_kses_post( $field['title'] ); ?></span></legend>
503 <input type="<?php echo esc_attr( $field['type'] ); ?>"
504 id="<?php echo esc_attr( $field_name ); ?>"
505 name="<?php echo esc_attr( $field_name ); ?>"
506 value="<?php echo esc_attr($field['value']); ?>"
507 class="regular-text <?php echo esc_attr( $field['class'] );
508 echo ( ! empty($field['validate_as']) ) ? ' validate-' . implode( ' validate-', $field['validate_as'] ) : ''; ?>"
509 style="<?php echo esc_attr( $field['css'] ); ?>"
510 placeholder="<?php echo esc_attr( $field['placeholder'] ); ?>"
511 <?php echo self::get_custom_attr_static( $field ); ?>
512 <?php disabled( $field['disabled'], true ); ?>
513 autocomplete="off"
514 />
515 <?php echo self::description_static( $field, $field['description_tag'] ); ?>
516 <?php
517 if ( ! $field['only_field'] ) {
518 ?>
519 </fieldset></td>
520 </tr>
521 <?php
522 }
523
524 if ( ! $echo ) {
525 return ob_get_clean();
526 }
527 }
528
529
530 /**
531 * Static Color Input Row
532 *
533 * @param string $field_name - name of field
534 * @param array $field - parameters
535 * @param boolean $echo - show or return reaults {default true}
536 * @return string - html || nothing
537 */
538 public static function field_color_row_static( $field_name, $field, $echo = true ) {
539
540 $defaults = array(
541 'title' => '',
542 'disabled' => false,
543 'class' => '',
544 'css' => '',
545 'placeholder' => '',
546 'type' => 'text',
547 'description' => '',
548 'attr' => array(),
549 'group' => 'general',
550 'tr_class' => '',
551 'only_field' => false,
552 'description_tag' => 'p'
553 );
554
555 $field = wp_parse_args( $field, $defaults );
556
557 if ( ! $echo ) {
558 ob_start();
559 }
560 if ( ! $field['only_field'] ) {
561 ?>
562 <tr valign="top" class="wpbc_tr_<?php echo esc_attr( $field_name ), ' ', esc_attr( $field['tr_class'] ); ?>">
563 <th scope="row">
564 <?php echo self::label_static( $field_name, $field ); ?>
565 </th>
566 <td><fieldset><?php
567 }
568 ?>
569 <legend class="screen-reader-text"><span><?php echo wp_kses_post( $field['title'] ); ?></span></legend>
570 <input type="text"
571 id="<?php echo esc_attr( $field_name ); ?>"
572 name="<?php echo esc_attr( $field_name ); ?>"
573 value="<?php echo esc_attr($field['value']); ?>"
574 class="regular-text wpbc_colorpick <?php echo esc_attr( $field['class'] ); ?>"
575 style="width: 6em; background-color: <?php echo $field['value']; ?>;<?php echo esc_attr( $field['css'] ); ?>"
576 placeholder="<?php echo esc_attr( $field['placeholder'] ); ?>"
577 <?php echo self::get_custom_attr_static( $field ); ?>
578 <?php disabled( $field['disabled'], true ); ?>
579 autocomplete="off"
580 />
581 <?php echo self::description_static( $field, $field['description_tag'] ); ?>
582 <?php
583 if ( ! $field['only_field'] ) {
584 ?>
585 </fieldset></td>
586 </tr>
587 <?php
588 }
589
590 if ( ! $echo ) {
591 return ob_get_clean();
592 }
593 }
594
595
596 /**
597 * Static Textarea Row
598 *
599 * @param string $field_name - name of field
600 * @param array $field - parameters
601 * @param boolean $echo - show or return reaults {default true}
602 * @return string - html || nothing
603 */
604 public static function field_textarea_row_static( $field_name, $field, $echo = true ) {
605
606 $defaults = array(
607 'title' => '',
608 'disabled' => false,
609 'class' => '',
610 'css' => '',
611 'placeholder' => '',
612 'type' => 'text',
613 'description' => '',
614 'attr' => array(),
615 'rows' => 3,
616 'cols' => 20,
617 'show_in_2_cols' => false,
618 'group' => 'general',
619 'tr_class' => '',
620 'only_field' => false,
621 'description_tag' => 'p'
622 , 'validate_as' => array()
623 );
624
625 $field = wp_parse_args( $field, $defaults );
626
627 if ( ! $echo ) {
628 ob_start();
629 }
630
631 if ( ! $field['only_field'] ) {
632 ?>
633 <tr valign="top" class="wpbc_tr_<?php echo esc_attr( $field_name ), ' ', esc_attr( $field['tr_class'] ); ?>">
634 <th scope="row">
635 <?php echo self::label_static( $field_name, $field ); ?>
636 <?php if ( $field['show_in_2_cols'] ) { ?>
637 </th>
638 <td></td>
639 </tr>
640 <tr valign="top" class="wpbc_tr_<?php echo esc_attr( $field_name ), ' ', esc_attr( $field['tr_class'] ); ?>">
641 <td colspan="2"><fieldset>
642 <?php } else {
643 echo ' </th>'; //FixIn: 9.6.3.3
644 echo ' <td><fieldset>';
645 } ?><?php
646 }
647 ?>
648 <legend class="screen-reader-text"><span><?php echo wp_kses_post( $field['title'] ); ?></span></legend>
649 <textarea
650 rows="<?php echo esc_attr( $field['rows'] ); ?>"
651 cols="<?php echo esc_attr( $field['cols'] ); ?>"
652 <?php /* type="<?php echo esc_attr( $field['type'] ); ?>" */ ?>
653 id="<?php echo esc_attr( $field_name ); ?>"
654 name="<?php echo esc_attr( $field_name ); ?>"
655 class="input-text wide-input <?php echo esc_attr( $field['class'] );
656 echo ( ! empty($field['validate_as']) ) ? ' validate-' . implode( ' validate-', $field['validate_as'] ) : ''; ?>"
657 style="<?php echo esc_attr( $field['css'] ); ?>"
658 placeholder="<?php echo esc_attr( $field['placeholder'] ); ?>"
659 <?php echo self::get_custom_attr_static( $field ); ?>
660 <?php disabled( $field['disabled'], true ); ?>
661 autocomplete="off"
662 ><?php
663 echo esc_textarea( $field['value'] ); ?></textarea>
664 <?php echo self::description_static( $field, $field['description_tag'] ); ?>
665 <?php
666 if ( ! $field['only_field'] ) {
667 ?>
668 </fieldset></td>
669 </tr>
670 <?php
671 }
672
673 if ( ! $echo ) {
674 return ob_get_clean();
675 }
676 }
677
678
679 /**
680 * Static WP Rich Content Edit Textarea Row
681 *
682 * @param string $field_name - name of field
683 * @param array $field - parameters
684 * @param boolean $echo - show or return reaults {default true}
685 * @return string - html || nothing
686 */
687 public static function field_wp_textarea_row_static( $field_name, $field, $echo = true ) {
688
689 $defaults = array(
690 'title' => '',
691 'disabled' => false,
692 'class' => '',
693 'css' => '',
694 'placeholder' => '',
695 'type' => 'text',
696 'description' => '',
697 'attr' => array(),
698 'rows' => 3,
699 'editor_height' => '',
700 'cols' => 20,
701 'teeny' => true,
702 'show_visual_tabs' => true,
703 'default_editor' => 'tinymce', // 'tinymce' | 'html' // 'html' is used for the "Text" editor tab.
704 'drag_drop_upload' => false,
705 'show_in_2_cols' => false,
706 'group' => 'general',
707 'tr_class' => '',
708 'only_field' => false,
709 'description_tag' => 'p'
710 );
711
712 $field = wp_parse_args( $field, $defaults );
713
714 if ( ! $echo ) {
715 ob_start();
716 }
717 if ( ! $field['only_field'] ) {
718 ?>
719 <tr valign="top" class="wpbc_tr_<?php echo esc_attr( $field_name ), ' ', esc_attr( $field['tr_class'] ); ?>">
720 <th scope="row">
721 <?php echo self::label_static( $field_name, $field ); ?>
722 <?php if ( $field['show_in_2_cols'] ) { ?>
723 </th>
724 <td></td>
725 </tr>
726 <tr valign="top" class="wpbc_tr_<?php echo esc_attr( $field_name ), ' ', esc_attr( $field['tr_class'] ); ?>">
727 <td colspan="2"><fieldset>
728 <?php } else {
729 echo ' </th>'; //FixIn: 9.6.3.3
730 echo ' <td><fieldset>';
731 } ?><?php
732 }
733 ?>
734 <legend class="screen-reader-text"><span><?php echo wp_kses_post( $field['title'] ); ?></span></legend><?php
735 wp_editor(
736 $field['value'],
737 esc_attr( $field_name ),
738 array(
739 'wpautop' => false
740 , 'media_buttons' => false
741 , 'textarea_name' => esc_attr( $field_name )
742 , 'textarea_rows' => $field['rows']
743 , 'editor_class' => 'wpbc-textarea-tinymce ' . esc_attr( $field['class'] ) // Any extra CSS Classes to append to the Editor textarea
744 , 'teeny' => esc_attr( $field['teeny'] ) // Whether to output the minimal editor configuration used in PressThis
745 , 'drag_drop_upload' => esc_attr( $field['drag_drop_upload'] ) // Enable Drag & Drop Upload Support (since WordPress 3.9)
746 , 'tinymce' => $field['show_visual_tabs'] // Remove Visual Mode from the Editor
747 , 'default_editor' => $field['default_editor'] // 'tinymce' | 'html' // 'html' is used for the "Text" editor tab.
748 , 'editor_height' => $field['editor_height'] // '' | '500'
749 )
750 );
751 echo self::description_static( $field, $field['description_tag'] ); ?>
752 <?php
753 if ( ! $field['only_field'] ) {
754 ?>
755 </fieldset></td>
756 </tr>
757 <?php
758 }
759
760 if ( ! $echo ) {
761 return ob_get_clean();
762 }
763 }
764
765
766 /**
767 * Static Radio buttons field row
768 *
769 * @param string $field_name - name of field
770 * @param array $field - parameters
771 * @param boolean $echo - show or return reaults {default true}
772 * @return string - html || nothing
773 */
774 public static function field_radio_row_static( $field_name, $field, $echo = true ) {
775
776 $defaults = array(
777 'title' => '',
778 'label' => '',
779 'disabled' => false,
780 'disabled_options' => array(),
781 'class' => '',
782 'css' => '',
783 'type' => 'radio',
784 'description' => '',
785 'attr' => array(),
786 'options' => array(),
787 'group' => 'general',
788 'tr_class' => '',
789 'only_field' => false,
790 'is_new_line' => false,
791 'description_tag' => 'span'
792 );
793
794 $field = wp_parse_args( $field, $defaults );
795
796 if ( ! $echo ) {
797 ob_start();
798 }
799 if ( ! $field['only_field'] ) {
800 ?>
801 <tr valign="top" class="wpbc_tr_<?php echo esc_attr( $field_name ), ' ', esc_attr( $field['tr_class'] ); ?>">
802 <th scope="row">
803 <?php echo self::label_static( $field_name, $field ); ?>
804 </th>
805 <td><fieldset><?php
806 }
807 ?>
808 <legend class="screen-reader-text"><span><?php echo wp_kses_post( $field['title'] ); ?></span></legend>
809 <?php
810 foreach ($field['options'] as $option_value => $option_title ) {
811
812 $option_parameters = array();
813 if ( is_array( $option_title ) ) {
814 $option_parameters = $option_title;
815 $option_title = $option_title['title'];
816 }
817
818 ?><label class="wpbc-form-radio" title="<?php echo esc_attr( $option_title ); ?>">
819 <input type="radio"
820 name="<?php echo esc_attr( $field_name ); ?>"
821 value="<?php echo esc_attr( $option_value ); ?>"
822 class="<?php echo esc_attr( $field['class'] ); ?>"
823 style="<?php echo esc_attr( $field['css'] ); ?>"
824 <?php echo self::get_custom_attr_static( $field ); ?>
825 <?php echo self::get_custom_attr_static( $option_parameters ); ?>
826 <?php checked( $field['value'], $option_value ); ?>
827 <?php disabled( $field['disabled'], true ); ?>
828 <?php disabled( in_array($option_value, $field['disabled_options'] ), true ); ?>
829 autocomplete="off"
830 /> <?php echo wp_kses_post( $option_title ); ?></label><?php echo ( $field['is_new_line'] ) ? '<br/>' : '&nbsp; &nbsp; &nbsp;';
831
832 if ( isset( $option_parameters['html'] ) ) {
833 echo $option_parameters['html'];
834 }
835 } ?>
836 <?php echo self::description_static( $field, $field['description_tag'] ); ?>
837 <?php
838 if ( ! $field['only_field'] ) {
839 ?>
840 </fieldset></td>
841 </tr>
842 <?php
843 }
844
845 if ( ! $echo ) {
846 return ob_get_clean();
847 }
848 }
849
850
851 /**
852 * Static Selectbox field row
853 *
854 * @param string $field_name - name of field
855 * @param array $field - parameters
856 * @param boolean $echo - show or return reaults {default true}
857 * @return string - html || nothing
858 */
859 public static function field_select_row_static( $field_name, $field, $echo = true ) {
860
861 $defaults = array(
862 'title' => '',
863 'label' => '',
864 'disabled' => false,
865 'disabled_options' => array(),
866 'class' => '',
867 'css' => '',
868 'type' => 'select',
869 'description' => '',
870 'multiple' => false,
871 'attr' => array(),
872 'options' => array(),
873 'group' => 'general',
874 'tr_class' => '',
875 'only_field' => false,
876 'description_tag' => 'span'
877 );
878
879 $field = wp_parse_args( $field, $defaults );
880
881 if ( ! $echo ) {
882 ob_start();
883 }
884
885 if ( ! $field['only_field'] ) {
886 ?>
887 <tr valign="top" class="wpbc_tr_<?php echo esc_attr( $field_name ), ' ', esc_attr( $field['tr_class'] ); ?>">
888 <th scope="row">
889 <?php echo self::label_static( $field_name, $field ); ?>
890 </th>
891 <td><fieldset>
892 <?php
893 }
894 ?>
895 <legend class="screen-reader-text"><span><?php echo wp_kses_post( $field['title'] ); ?></span></legend>
896 <select
897 id="<?php echo esc_attr( $field_name ); ?>"
898 name="<?php echo esc_attr( $field_name ); echo ( $field['multiple'] ? '[]' : '' ); ?>"
899 class="<?php echo esc_attr( $field['class'] ); ?>"
900 style="<?php echo esc_attr( $field['css'] ); ?>"
901 <?php disabled( $field['disabled'], true ); ?>
902 <?php echo self::get_custom_attr_static( $field ); ?>
903 <?php echo ( $field['multiple'] ? ' multiple="MULTIPLE"' : '' ); ?>
904 autocomplete="off"
905 ><?php
906
907 foreach ( (array) $field['options'] as $option_value => $option_title ) {
908
909 $option_parameters = array();
910 if ( is_array( $option_title ) ) {
911 $option_parameters = $option_title;
912 $option_title = ( ! empty( $option_title['title'] ) ) ? $option_title['title'] : '';
913 }
914
915
916 // array(
917 // 'title' => 'Option Group Title' // Title
918 // , 'optgroup' => true // Use only if you need to show OPTGROUP - Also need to use 'title' of start, end 'close' for END
919 // , 'close' => false
920 // )
921
922 if ( ! empty( $option_parameters['optgroup'] ) ) { // OPTGROUP
923
924 if ( ! $option_parameters['close'] ) {
925 ?><optgroup label="<?php echo esc_attr( $option_parameters['title'] ); ?>"><?php
926 } else {
927 ?></optgroup><?php
928 }
929 } else {
930
931
932 ?><option value="<?php echo esc_attr( $option_value ); ?>"
933 <?php
934 if ( ( is_array( $field['value'] ) ) && ( in_array( $option_value, $field['value'] ) ) ) {
935 selected( true );
936 } else {
937 if ( ! is_array( $field['value'] ) ) { //FixIn: 8.1.1.2
938 selected( $option_value, $field['value'] );
939 }
940 }
941 ?>
942 <?php disabled( in_array($option_value, $field['disabled_options'] ), true ); ?>
943 <?php echo self::get_custom_attr_static( $option_parameters ); ?>
944 ><?php echo ( $option_title ); ?></option><?php
945 }
946 } ?>
947 </select>
948 <?php echo self::description_static( $field, $field['description_tag'] ); ?>
949 <?php
950 if ( ! $field['only_field'] ) {
951 ?>
952 </fieldset></td>
953 </tr>
954 <?php
955 }
956
957 if ( ! $echo ) {
958 return ob_get_clean();
959 }
960 }
961
962
963 /**
964 * Static Checkbox field row
965 *
966 * @param string $field_name - name of field
967 * @param array $field - parameters
968 * @param boolean $echo - show or return reaults {default true}
969 * @return string - html || nothing
970 */
971 public static function field_checkbox_row_static( $field_name, $field, $echo = true ) {
972
973 $defaults = array(
974 'title' => '',
975 'label' => '',
976 'disabled' => false,
977 'class' => '',
978 'css' => '',
979 'type' => 'checkbox',
980 'description' => '',
981 'attr' => array(),
982 'group' => 'general',
983 'tr_class' => '',
984 'only_field' => false,
985 'is_new_line' => true,
986 'description_tag' => 'span'
987 );
988
989 $field = wp_parse_args( $field, $defaults );
990
991 if ( ! $echo ) {
992 ob_start();
993 }
994 if ( ! $field['only_field'] ) {
995 ?>
996 <tr valign="top" class="wpbc_tr_<?php echo esc_attr( $field_name ), ' ', esc_attr( $field['tr_class'] ); ?>">
997 <th scope="row">
998 <?php echo self::label_static( $field_name, $field ); ?>
999 </th>
1000 <td><fieldset><?php
1001 }
1002
1003 // Show toggles in Settings pages
1004 if ( 1 ) {
1005
1006 $field['label'] = str_replace( '<span', '<i style="font-style: normal;"', $field['label']);
1007 $field['label'] = str_replace( '/span>', '/i>' , $field['label']);
1008
1009 $params_checkbox = array(
1010 'id' => $field_name // HTML ID of element
1011 , 'name' => $field_name
1012 , 'label' => array( 'title' => wp_kses_post( $field['label'] ) , 'position' => 'right' ) //FixIn: 9.6.1.5
1013 , 'toggle_style' => $field['css'] // CSS of select element
1014 , 'class' => $field['class'] // CSS Class of select element
1015 , 'disabled' => $field['disabled']
1016 , 'attr' => $field['attr'] // Any additional attributes, if this radio | checkbox element
1017 , 'legend' => wp_kses_post( $field['title'] ) // aria-label parameter
1018 , 'value' => $field['value'] // Some Value from optins array that selected by default
1019 , 'selected' => ( ( 'On' == $field['value'] ) ? true : false ) // Selected or not
1020
1021 //, 'onfocus' => "console.log( 'ON FOCUS:', jQuery( this ).is(':checked') , 'in element:' , jQuery( this ) );" // JavaScript code
1022 //, 'onchange' => "wpbc_ajx_booking_send_search_request_with_params( {'ui_usr__send_emails': (jQuery( this ).is(':checked') ? 'send' : 'not_send') } );" // JavaScript code
1023 //, 'hint' => array( 'title' => __('Send email notification to customer about this operation' ,'booking') , 'position' => 'top' )
1024 );
1025 $params_checkbox = wp_parse_args( $params_checkbox, $field );
1026 wpbc_flex_toggle( $params_checkbox );
1027
1028 ?><legend class="screen-reader-text"><span><?php echo wp_kses_post( $field['title'] ); ?></span></legend><?php
1029
1030 echo ( $field['is_new_line'] ) ? '<br/>' : '&nbsp; &nbsp; &nbsp;';
1031 echo self::description_static( $field, $field['description_tag'] );
1032
1033 } else {
1034
1035 ?><legend class="screen-reader-text"><span><?php echo wp_kses_post( $field['title'] ); ?></span></legend>
1036 <label class="wpbc-form-checkbox" for="<?php echo esc_attr( $field_name ); ?>">
1037 <input type="checkbox"
1038 id="<?php echo esc_attr( $field_name ); ?>"
1039 name="<?php echo esc_attr( $field_name ); ?>"
1040 value="<?php echo esc_attr( $field['value'] ); ?>"
1041 class="<?php echo esc_attr( $field['class'] ); ?>"
1042 style="<?php echo esc_attr( $field['css'] ); ?>"
1043 <?php echo self::get_custom_attr_static( $field ); ?>
1044 <?php checked( $field['value'], 'On' ); ?>
1045 <?php disabled( $field['disabled'], true ); ?>
1046 autocomplete="off"
1047 /> <?php echo wp_kses_post( $field['label'] ); ?></label><?php echo ( $field['is_new_line'] ) ? '<br/>' : '&nbsp; &nbsp; &nbsp;'; ?>
1048 <?php echo self::description_static( $field, $field['description_tag'] ); ?><?php
1049 }
1050
1051 if ( ! $field['only_field'] ) {
1052 ?></fieldset></td>
1053 </tr><?php
1054
1055 }
1056
1057 if ( ! $echo ) {
1058 return ob_get_clean();
1059 }
1060 }
1061
1062
1063 ////////////////////////////////////////////////////////////////////////////
1064 // Text, Help, HTML, JS Elements
1065 ////////////////////////////////////////////////////////////////////////////
1066
1067
1068 /**
1069 * Help Info Row
1070 *
1071 * @param string $field_name - name of field
1072 * @param array $field - parameters
1073 * @param boolean $echo - show or return reaults {default true}
1074 * @return string - html || nothing
1075 */
1076 public function field_help_row( $field_name, $field, $echo = true ) {
1077 if ( ! $echo )
1078 return self::field_help_row_static( $field_name, $field, $echo );
1079 else
1080 self::field_help_row_static( $field_name, $field, $echo );
1081 }
1082
1083
1084 /**
1085 * Static Help Info Row
1086 *
1087 * @param string $field_name - name of field
1088 * @param array $field - parameters
1089 * @param boolean $echo - show or return reaults {default true}
1090 * @return string - html || nothing
1091 */
1092 public static function field_help_row_static( $field_name, $field, $echo = true ) {
1093
1094 $defaults = array(
1095 'value' => array(),
1096 'class' => '',
1097 'css' => '',
1098 'description' => '',
1099 'cols' => 1,
1100 'group' => 'general',
1101 'tr_class' => '',
1102 'description_tag' => 'span',
1103 'only_field' => false
1104 );
1105
1106 $field = wp_parse_args( $field, $defaults );
1107
1108 if ( ! $echo ) {
1109 ob_start();
1110 }
1111 if ( ! $field['only_field'] ) {
1112
1113 ?><tr valign="top" class="wpbc_tr_<?php echo esc_attr( $field_name ), ' ', esc_attr( $field['tr_class'] ); ?>">
1114 <?php if ( $field['cols'] == 1 ) { ?>
1115 <th scope="row"></th>
1116 <td>
1117 <?php } else { ?>
1118 <td colspan="2">
1119 <?php }
1120 }
1121 ?><div class="wpbc-help-message <?php echo esc_attr( $field['class'] ); ?>" style="margin-top:10px; <?php echo esc_attr( $field['css'] ); ?>">
1122 <?php
1123 $field['value'] = (array) $field['value'];
1124
1125 foreach ( $field['value'] as $help_text ) {
1126 ?><p class="description" style="font-weight: 400;"><?php
1127
1128 echo $help_text;
1129
1130 ?></p><?php
1131 }
1132 ?>
1133 </div>
1134 <?php echo self::description_static( $field, $field['description_tag'] ); ?>
1135 <?php if ( ! $field['only_field'] ) { ?>
1136 </td>
1137 </tr>
1138 <?php
1139 }
1140
1141 if ( ! $echo ) {
1142 return ob_get_clean();
1143 }
1144 }
1145
1146
1147 /**
1148 * Horizontal Separator
1149 *
1150 * @param string $field_name - name of field
1151 * @param array $field - parameters
1152 * @param boolean $echo - show or return reaults {default true}
1153 * @return string - html || nothing
1154 */
1155 public function field_hr_row( $field_name, $field, $echo = true ) {
1156 if ( ! $echo )
1157 return self::field_hr_row_static( $field_name, $field, $echo );
1158 else
1159 self::field_hr_row_static( $field_name, $field, $echo );
1160 }
1161
1162
1163 /**
1164 * Static Horizontal Separator
1165 *
1166 * @param string $field_name - name of field
1167 * @param array $field - parameters
1168 * @param boolean $echo - show or return reaults {default true}
1169 * @return string - html || nothing
1170 */
1171 public static function field_hr_row_static( $field_name, $field, $echo = true ) {
1172
1173 $defaults = array(
1174 'class' => '',
1175 'css' => '',
1176 'group' => 'general',
1177 'tr_class' => ''
1178 );
1179
1180 $field = wp_parse_args( $field, $defaults );
1181
1182 if ( ! $echo ) {
1183 ob_start();
1184 }
1185 ?>
1186 <tr valign="top" class="wpbc_tr_<?php echo esc_attr( $field_name ), ' ', esc_attr( $field['tr_class'] ); ?>">
1187 <td style="padding:10px 0px; " colspan="2">
1188 <div class="<?php echo esc_attr( $field['class'] ); ?>" style="border-bottom:1px solid #cccccc;<?php echo esc_attr( $field['css'] ); ?>" ></div>
1189 </td>
1190 </tr>
1191 <?php
1192
1193 if ( ! $echo ) {
1194 return ob_get_clean();
1195 }
1196 }
1197
1198
1199 /**
1200 * HTML Row - show any html. Warning parameter html do not escaped, check it before assign.
1201 *
1202 * @param string $field_name - name of field
1203 * @param array $field - parameters
1204 * @param boolean $echo - show or return reaults {default true}
1205 * @return string - html || nothing
1206 */
1207 public function field_html_row( $field_name, $field, $echo = true ) {
1208 if ( ! $echo )
1209 return self::field_html_row_static( $field_name, $field, $echo );
1210 else
1211 self::field_html_row_static( $field_name, $field, $echo );
1212 }
1213
1214
1215 /**
1216 * Static HTML Row - show any html. Warning parameter html do not escaped, check it before assign.
1217 *
1218 * @param string $field_name - name of field
1219 * @param array $field - parameters
1220 * @param boolean $echo - show or return reaults {default true}
1221 * @return string - html || nothing
1222 */
1223 public static function field_html_row_static( $field_name, $field, $echo = true ) {
1224
1225 $defaults = array(
1226 'html' => '',
1227 'cols' => 1,
1228 'group' => 'general',
1229 'tr_class' => ''
1230 );
1231
1232 $field = wp_parse_args( $field, $defaults );
1233
1234 if ( ! $echo ) {
1235 ob_start();
1236 }
1237 ?>
1238 <tr valign="top" class="wpbc_tr_<?php echo esc_attr( $field_name ), ' ', esc_attr( $field['tr_class'] ); ?>">
1239 <?php if ( $field['cols'] == 1 ) { ?>
1240 <th scope="row"></th>
1241 <td>
1242 <?php } else { ?>
1243 <td colspan="2">
1244 <?php }?>
1245 <?php echo $field['html']; ?>
1246 </td>
1247 </tr>
1248 <?php
1249
1250 if ( ! $echo ) {
1251 return ob_get_clean();
1252 }
1253 }
1254
1255
1256 /**
1257 * HTML - show only this html, Without any Table Rows. Warning parameter html do not escaped, check it before assign.
1258 *
1259 * @param string $field_name - name of field
1260 * @param array $field - parameters
1261 * @param boolean $echo - show or return reaults {default true}
1262 * @return string - html || nothing
1263 */
1264 public function field_pure_html_row( $field_name, $field, $echo = true ) {
1265 if ( ! $echo )
1266 return self::field_pure_html_row_static( $field_name, $field, $echo );
1267 else
1268 self::field_pure_html_row_static( $field_name, $field, $echo );
1269 }
1270
1271
1272 /**
1273 * Static HTML - show only this html, Without any Table Rows. Warning parameter html do not escaped, check it before assign.
1274 *
1275 * @param string $field_name - name of field
1276 * @param array $field - parameters
1277 * @param boolean $echo - show or return reaults {default true}
1278 * @return string - html || nothing
1279 */
1280 public static function field_pure_html_row_static( $field_name, $field, $echo = true ) {
1281
1282 $defaults = array(
1283 'html' => '',
1284 'group' => 'general'
1285 );
1286
1287 $field = wp_parse_args( $field, $defaults );
1288
1289 if ( ! $echo ) {
1290 ob_start();
1291 }
1292
1293 echo $field['html'];
1294
1295 if ( ! $echo ) {
1296 return ob_get_clean();
1297 }
1298 }
1299
1300
1301 /**
1302 * JavaScript Row - insert JavaScript
1303 *
1304 * @param string $field_name - name of field
1305 * @param array $field - parameters
1306 * @param boolean $echo - show or return reaults {default true}
1307 * @return string - html || nothing
1308 */
1309 public function field_js_row( $field_name, $field, $echo = true ) {
1310 if ( ! $echo )
1311 return self::field_js_row_static( $field_name, $field, $echo );
1312 else
1313 self::field_js_row_static( $field_name, $field, $echo );
1314 }
1315
1316
1317 /**
1318 * Static JavaScript Row - insert JavaScript
1319 *
1320 * @param string $field_name - name of field
1321 * @param array $field - parameters
1322 * @param boolean $echo - show or return reaults {default true}
1323 * @return string - html || nothing
1324 */
1325 public static function field_js_row_static( $field_name, $field, $echo = true ) {
1326
1327 $defaults = array(
1328 'js' => '',
1329 'group' => 'general'
1330 );
1331
1332 $field = wp_parse_args( $field, $defaults );
1333
1334 if ( ! $echo ) {
1335 ob_start();
1336 }
1337 ?>
1338 <script type="text/javascript">
1339 <?php echo $field['js']; ?>
1340 </script>
1341 <?php
1342
1343 if ( ! $echo ) {
1344 return ob_get_clean();
1345 }
1346 }
1347
1348
1349 ////////////////////////////////////////////////////////////////////////////
1350 // Support functions
1351 ////////////////////////////////////////////////////////////////////////////
1352
1353 /**
1354 * Get custom attributes
1355 *
1356 * @param array $field
1357 * @return string
1358 */
1359 public static function get_custom_attr_static( $field ) {
1360
1361 $attributes = array();
1362
1363 if ( ! empty( $field['attr'] ) && is_array( $field['attr'] ) ) {
1364
1365 foreach ( $field['attr'] as $attr => $attr_v ) {
1366 $attributes[] = esc_attr( $attr ) . '="' . esc_attr( $attr_v ) . '"';
1367 }
1368 }
1369
1370 return implode( ' ', $attributes );
1371 }
1372
1373
1374 /**
1375 * Get Description
1376 *
1377 * @param array $field
1378 * @param string $html_tag - HTML element, which will be used as separator. Default - 'p'
1379 * @return string
1380 */
1381 public static function description_static( $field, $html_tag = 'p' ) {
1382 if ( empty( $html_tag ) )
1383 $html_tag = 'p';
1384
1385 return $field['description'] ? '<'.$html_tag.' class="description">' . wp_kses_post( $field['description'] ) . '</'.$html_tag.'>' . "\n" : '';
1386 }
1387
1388 public static function label_static( $field_name, $field ) {
1389
1390 if ( empty( $field['title'] ) )
1391 return '';
1392
1393 $defaults = array(
1394 'title' => '',
1395 'label_class' => '',
1396 'label_css' => '',
1397 'type' => ''
1398 );
1399 $field = wp_parse_args( $field, $defaults );
1400
1401 if ( ! empty($field['type'] ) )
1402 $field['label_class'] .= ' wpbc-form-' . $field['type'];
1403
1404 if ( ! empty( $field['label_css'] ) )
1405 $field['label_css'] = ' style="' . $field['label_css'] . '"';
1406
1407 return '<label for="' . esc_attr( $field_name ) . '" class="' . $field['label_class'] . '"'.$field['label_css'].'>' . wp_kses_post( $field['title'] ) . '</label>';
1408 }
1409
1410 // </editor-fold>
1411
1412
1413 ////////////////////////////////////////////////////////////////////////////
1414 // Validate POSTs
1415 ////////////////////////////////////////////////////////////////////////////
1416
1417 /**
1418 * Validate Settings Field Data.
1419 *
1420 * Validate the data on the "Settings" form.
1421 *
1422 * @since 1.0.0
1423 * @uses method_exists()
1424 * @param array $form_fields (default: array())
1425 */
1426 public function validate_post( $form_fields = array() ) {
1427
1428 // Check server restrictions in php.ini file relative to length of $_POST variabales
1429 if ( function_exists ('wpbc_check_post_key_max_number')) { wpbc_check_post_key_max_number(); }
1430
1431 if ( ! $form_fields ) {
1432 $form_fields = $this->get_form_fields();
1433 }
1434
1435 $this->validated_fields = array();
1436
1437 foreach ( $form_fields as $k => $v ) {
1438
1439 if ( ! empty( $v['is_demo_safe'] ) ) { // Skip saving values for fields with this parameter ( 'is_demo_safe' => true )
1440 continue;
1441 }
1442
1443 if ( empty( $v['type'] ) ) {
1444 $v['type'] = 'text';
1445 }
1446
1447 // Look for a validate_FIELDID_post method for special handling
1448 if ( method_exists( $this, 'validate_' . $k . '_post' ) ) {
1449 $field = $this->{'validate_' . $k . '_post'}( $this->id . '_' . $k );
1450 $this->validated_fields[ $k ] = $field;
1451
1452 // Look for a validate_FIELDTYPE_post method
1453 } elseif ( method_exists( $this, 'validate_' . $v['type'] . '_post' ) ) {
1454 $field = $this->{'validate_' . $v['type'] . '_post'}( $this->id . '_' . $k );
1455 $this->validated_fields[ $k ] = $field;
1456
1457 // Default to text
1458 } else {
1459 if ( isset( $_POST[ $this->id . '_' . $k ] ) ) { //Check this for non Fields elements, like simple HTML or TEXT or JS
1460 $field = $this->{'validate_text_post'}( $this->id . '_' . $k );
1461 $this->validated_fields[ $k ] = $field;
1462 }
1463 }
1464 }
1465
1466 return $this->validated_fields;
1467 }
1468
1469
1470 // <editor-fold defaultstate="collapsed" desc=" V a l i d a t e P O S T F i e l d s " >
1471
1472 /**
1473 * Validate Text in POST request - escape data correctly.
1474 *
1475 * @param string $post_key - key for POST
1476 * @return string | false, if no such POST
1477 */
1478 public function validate_text_post( $post_key ) {
1479 return self::validate_text_post_static( $post_key );
1480 }
1481
1482
1483 /**
1484 * Static Validate Text in POST request - escape data correctly.
1485 *
1486 * @param string $post_key - key for POST
1487 * @return string | false, if no such POST
1488 */
1489 public static function validate_text_post_static( $post_key, $post_index = false ) {
1490
1491 $value = false;
1492 //FixIn: 9.2.4.2
1493 if ( isset( $_POST[ $post_key ] ) ) {
1494 if ( $post_index !== false ) {
1495 $post_value = $_POST[ $post_key ][ $post_index ];
1496 } else {
1497 $post_value = $_POST[ $post_key ];
1498 }
1499 }
1500
1501 if ( isset( $post_value ) ) {
1502
1503 $value = wp_kses_post( trim( stripslashes( $post_value ) ) );
1504 }
1505
1506 return $value;
1507 }
1508
1509
1510 /**
1511 * Validate Email field in POST request - escape data correctly.
1512 *
1513 * @param string $post_key - key for POST
1514 * @return string | false, if no such POST
1515 */
1516 public function validate_email_post( $post_key ) {
1517 return self::validate_email_post_static( $post_key );
1518 }
1519
1520
1521 /**
1522 * Static Validate Email field in POST request - escape data correctly.
1523 *
1524 * @param string $post_key - key for POST
1525 * @return string | false, if no such POST
1526 */
1527 public static function validate_email_post_static( $post_key ) {
1528
1529 $value = false;
1530
1531 if ( isset( $_POST[ $post_key ] ) ) {
1532 $value = sanitize_email( trim( stripslashes( $_POST[ $post_key ] ) ) );
1533 }
1534
1535 return $value;
1536 }
1537
1538
1539 /**
1540 * Validate Textarea in POST request - escape data correctly.
1541 *
1542 * @param string $post_key - key for POST
1543 * @return string | false, if no such POST
1544 */
1545 public function validate_textarea_post( $post_key ) {
1546 return self::validate_textarea_post_static( $post_key );
1547 }
1548
1549
1550 /**
1551 * Static Validate Textarea in POST request - escape data correctly.
1552 *
1553 * @param string $post_key - key for POST
1554 * @return string | false, if no such POST
1555 */
1556 public static function validate_textarea_post_static( $post_key ) {
1557
1558 $value = false;
1559
1560 if ( isset( $_POST[ $post_key ] ) ) {
1561
1562 $value = wp_kses( trim( stripslashes( $_POST[ $post_key ] ) ),
1563 array_merge(
1564 array(
1565 'iframe' => array( 'src' => true, 'style' => true, 'id' => true, 'class' => true )
1566 ),
1567 wp_kses_allowed_html( 'post' )
1568 )
1569 );
1570 }
1571
1572 return $value;
1573 }
1574
1575
1576 /**
1577 * Validate WP Textarea in POST request - escape data correctly.
1578 *
1579 * Same as Textarea
1580 *
1581 * @param string $post_key - key for POST
1582 * @return string | false, if no such POST
1583 */
1584 public function validate_wp_textarea_post( $post_key ){
1585 return self::validate_wp_textarea_post_static( $post_key );
1586 }
1587
1588
1589 /**
1590 * Static Validate WP Textarea in POST request - escape data correctly.
1591 *
1592 * Same as Textarea
1593 *
1594 * @param string $post_key - key for POST
1595 * @return string | false, if no such POST
1596 */
1597 public static function validate_wp_textarea_post_static( $post_key ){
1598 return self::validate_textarea_post_static( $post_key );
1599 }
1600
1601
1602 /**
1603 * Validate Checkbox in POST request - escape data correctly.
1604 *
1605 * @param string $post_key - key for POST
1606 * @return string: 'On' | 'Off'
1607 */
1608 public function validate_checkbox_post( $post_key ) {
1609 return self::validate_checkbox_post_static( $post_key );
1610 }
1611
1612
1613 /**
1614 * Static Validate Checkbox in POST request - escape data correctly.
1615 *
1616 * @param string $post_key - key for POST
1617 * @return string: 'On' | 'Off'
1618 */
1619 public static function validate_checkbox_post_static( $post_key ) {
1620
1621 $status = 'Off';
1622
1623 if ( isset( $_POST[ $post_key ] ) && ( in_array( $_POST[ $post_key ], array( 'On', 'Off' ) ) ) ) {
1624 $status = 'On';
1625 }
1626
1627 return $status;
1628 }
1629
1630
1631 /**
1632 * Validate Select in POST request - escape data correctly.
1633 *
1634 * @param string $post_key - key for POST
1635 * @return string | array | false, if no such POST
1636 */
1637 public function validate_select_post( $post_key ) {
1638 return self::validate_select_post_static( $post_key );
1639 }
1640
1641
1642 /**
1643 * Static Validate Select in POST request - escape data correctly.
1644 *
1645 * @param string $post_key - key for POST
1646 * @return string | array | false, if no such POST
1647 */
1648 public static function validate_select_post_static( $post_key ) {
1649
1650 $value = false;
1651
1652 if ( isset( $_POST[ $post_key ] ) ) {
1653
1654 if ( is_array( $_POST[ $post_key ] ) ) {
1655 $value = array_map( 'sanitize_text_field', array_map( 'stripslashes', (array) $_POST[ $post_key ] ) );
1656 } else {
1657 $value = sanitize_text_field( stripslashes( $_POST[ $post_key ] ) );
1658 }
1659 }
1660
1661 return $value;
1662 }
1663
1664
1665 /**
1666 * Validate Radio in POST request - escape data correctly.
1667 *
1668 * Its the same as Select field
1669 *
1670 * @param string $post_key - key for POST
1671 * @return string | array | false, if no such POST
1672 */
1673 public function validate_radio_post( $post_key ) {
1674 return self::validate_radio_post_static( $post_key );
1675 }
1676
1677
1678 /**
1679 * Static Validate Radio in POST request - escape data correctly.
1680 *
1681 * Its the same as Select field
1682 *
1683 * @param string $post_key - key for POST
1684 * @return string | array | false, if no such POST
1685 */
1686 public static function validate_radio_post_static( $post_key ) {
1687 return self::validate_select_post_static( $post_key );
1688 }
1689
1690 // </editor-fold>
1691
1692 ////////////////////////////////////////////////////////////////////////////
1693 // Save | Get from DB
1694 ////////////////////////////////////////////////////////////////////////////
1695
1696 /**
1697 * Save Setting Options to DB, then Reinit Settings Fields with these new Values...
1698 *
1699 * @param string $settings_id - ID of the settings
1700 * @param array $validated_fields - List of validated fields in format array( field_name => field_value, ... )
1701 * @param string $how_to_save - 'togather' - default - save as one field | 'separately_with_prefix' - save separately but with adding settings_id | 'separately' - separately each field only with adding db prefix
1702 */
1703 public function save_to_db( $validated_fields ){
1704
1705 $settings_id = $this->get_id();
1706
1707 $how_to_save = $this->options['db_saving_type'];
1708
1709
1710 if ( $how_to_save == 'togather' ) {
1711 /* wpbc_ settings_general =
1712 Array (
1713 [date_format] => F j, Y
1714 [time_format] => H:i
1715 [hr_time_format] =>
1716 ) */
1717 update_bk_option( $this->options['db_prefix_option'] . $settings_id , $validated_fields );
1718
1719 } elseif ( $how_to_save == 'separate_prefix' ) { // wpbc_ settings_general_ date_format => F j, Y
1720
1721 foreach ( (array) $validated_fields as $field_name => $field_value ) {
1722 update_bk_option( $this->options['db_prefix_option'] . $settings_id . '_' . $field_name , $field_value );
1723 }
1724 } else { // $how_to_save == 'separate' // wpbc_ date_format => F j, Y
1725
1726 foreach ( (array) $validated_fields as $field_name => $field_value ) {
1727 update_bk_option( $this->options['db_prefix_option'] . $field_name , $field_value );
1728 }
1729 }
1730
1731
1732 // Redefine fields values for New Saved values
1733 $this->define_fields_values( $validated_fields , false );
1734
1735 // Set Values for the fields
1736 $this->set_values_to_fields(); // Assign $this->fields_values['some_field'] to $this->fields[ 'some_field' ]['value']
1737
1738
1739 $this->fields = apply_filters( 'wpbc_fields_after_saving_to_db', $this->fields, $this->id );
1740
1741 //debuge('After Saving to DB', $this->fields_values, $this->fields);
1742 }
1743
1744
1745 public function define_fields_values_from_db() {
1746
1747 $how_to_save = $this->options['db_saving_type'];
1748
1749 switch ( $how_to_save ) {
1750
1751 case 'togather':
1752
1753 $fields_values_from_db = get_bk_option( $this->options['db_prefix_option'] . $this->get_id() );
1754
1755 $this->define_fields_values( $fields_values_from_db ); // Define Fields by values from DB
1756
1757 break;
1758
1759 case 'separate_prefix':
1760
1761 $this->define_fields_values(); // Reinit Fields - need to know how many fields and names of fields
1762
1763 $fields_values_from_db = array();
1764
1765 foreach ( $this->fields_values as $field_name => $field_value ) {
1766
1767 $got_value = get_bk_option( $this->options['db_prefix_option'] . $this->get_id() . '_' . $field_name );
1768
1769 // If we do not have this value in DB ( === false ), then do not assing this value - will have Default Value
1770 if ( $got_value !== false )
1771 $fields_values_from_db[ $field_name ] = $got_value;
1772 }
1773
1774 $this->define_fields_values( $fields_values_from_db ); // Define Fields by values from DB
1775
1776 break;
1777
1778 default: // 'separate'
1779
1780 $this->define_fields_values(); // Reinit Fields - need to know how many fields and names of fields
1781
1782 $fields_values_from_db = array();
1783 //debuge('Before Loading from DB', $this->fields_values, $this->fields);
1784 foreach ( $this->fields_values as $field_name => $field_value ) {
1785
1786 $got_value = get_bk_option( $this->options['db_prefix_option'] . $field_name );
1787
1788 // If we do not have this value in DB ( === false ), then do not assing this value - will have Default Value
1789 if ( $got_value !== false )
1790 $fields_values_from_db[ $field_name ] = $got_value;
1791 }
1792
1793 $this->define_fields_values( $fields_values_from_db ); // Define Fields by values from DB
1794 //debuge('After loaded from DB', $this->fields_values, $this->fields);
1795 break;
1796 }
1797
1798 }
1799
1800
1801
1802 ////////////////////////////////////////////////////////////////////////////
1803 // Install | Uninstall
1804 ////////////////////////////////////////////////////////////////////////////
1805
1806 /** Actiovation of Plugin. Save to DB initial values of Settings Fields. */
1807 public function activate() {
1808
1809 $settings_id = $this->get_id();
1810
1811 $how_to_save = $this->options['db_saving_type'];
1812
1813
1814 $default_values = $this->get_default_values(); // Get "Default" values from $this->fields array.
1815
1816 if ( $how_to_save == 'togather' ) {
1817 /* wpbc_ settings_general =
1818 Array (
1819 [date_format] => F j, Y
1820 [time_format] => H:i
1821 [hr_time_format] =>
1822 ) */
1823 add_bk_option( $this->options['db_prefix_option'] . $settings_id , $default_values );
1824
1825 } elseif ( $how_to_save == 'separate_prefix' ) { // wpbc_ settings_general_ date_format => F j, Y
1826
1827 foreach ( (array) $default_values as $field_name => $field_value ) {
1828 add_bk_option( $this->options['db_prefix_option'] . $settings_id . '_' . $field_name , $field_value );
1829 }
1830 } else { // $how_to_save == 'separate' // wpbc_ date_format => F j, Y
1831
1832 foreach ( (array) $default_values as $field_name => $field_value ) {
1833 add_bk_option( $this->options['db_prefix_option'] . $field_name , $field_value );
1834 }
1835 }
1836 }
1837
1838
1839 /** Uninstall. Deactivation of Plugin. Delete Settings Fields from DB. */
1840 public function deactivate() {
1841
1842 $settings_id = $this->get_id();
1843
1844 $how_to_save = $this->options['db_saving_type'];
1845
1846 $default_values = $this->get_default_values(); // Get "Default" values from $this->fields array.
1847
1848
1849 if ( $how_to_save == 'togather' ) {
1850 /* wpbc_ settings_general =
1851 Array (
1852 [date_format] => F j, Y
1853 [time_format] => H:i
1854 [hr_time_format] =>
1855 ) */
1856 delete_bk_option( $this->options['db_prefix_option'] . $settings_id );
1857
1858 } elseif ( $how_to_save == 'separate_prefix' ) { // wpbc_ settings_general_ date_format => F j, Y
1859
1860 foreach ( (array) $default_values as $field_name => $field_value ) {
1861 delete_bk_option( $this->options['db_prefix_option'] . $settings_id . '_' . $field_name );
1862 }
1863 } else { // $how_to_save == 'separate' // wpbc_ date_format => F j, Y
1864
1865 foreach ( (array) $default_values as $field_name => $field_value ) {
1866 delete_bk_option( $this->options['db_prefix_option'] . $field_name );
1867 }
1868 }
1869 }
1870
1871 }