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