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