PluginProbe ʕ •ᴥ•ʔ
Everest Forms – Contact Form, Payment Form, Quiz, Survey & Custom Form Builder with AI / 2.0.3
Everest Forms – Contact Form, Payment Form, Quiz, Survey & Custom Form Builder with AI v2.0.3
3.5.2 3.5.1 3.5.0 3.4.8 3.4.7 3.4.6 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.5.1 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.5.0 1.5.1 1.5.10 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.6.1 1.6.7 1.7.0 1.7.0.1 1.7.0.2 1.7.0.3 1.7.1 1.7.2 1.7.2.1 1.7.2.2 1.7.3 1.7.4 1.7.5 1.7.5.1 1.7.5.2 1.7.6 1.7.7 1.7.7.1 1.7.7.2 1.7.8 1.7.9 1.8.0 1.8.0.1 1.8.1 1.8.2 1.8.2.1 1.8.2.2 1.8.2.3 1.8.3 1.8.4 1.8.5 1.8.6 1.8.7 1.8.8 1.8.9 1.9.0 1.9.0.1 1.9.1 1.9.2 1.9.3 1.9.4 1.9.4.1 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0.0 2.0.0.1 2.0.1 2.0.2 2.0.3 2.0.3.1 2.0.4 2.0.4.1 2.0.5 2.0.6 2.0.7 2.0.8 2.0.8.1 2.0.9 3.0.0 3.0.0.1 3.0.1 3.0.2 3.0.3 3.0.3.1 3.0.4 3.0.4.1 3.0.4.2 3.0.5 3.0.5.1 3.0.5.2 3.0.6 3.0.6.1 3.0.7.1 3.0.8 3.0.8.1 3.0.9 3.0.9.1 3.0.9.2 3.0.9.3 3.0.9.4 3.0.9.5 3.1.0 3.1.1 3.1.2 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.3.0 3.4.0 3.4.1 3.4.2 3.4.2.1 3.4.3 3.4.4 3.4.5 trunk 1.0 1.0.1 1.0.2 1.0.3
everest-forms / includes / abstracts / class-evf-settings-api.php
everest-forms / includes / abstracts Last commit date
legacy 6 years ago class-evf-background-process.php 7 years ago class-evf-deprecated-hooks.php 6 years ago class-evf-form-fields.php 2 years ago class-evf-integration.php 5 years ago class-evf-log-handler.php 6 years ago class-evf-session.php 7 years ago class-evf-settings-api.php 4 years ago
class-evf-settings-api.php
806 lines
1 <?php
2 /**
3 * Abstract Settings API Class
4 *
5 * Admin Settings API used by Integrations, Shipping Methods, and Payment Gateways.
6 *
7 * @package EverestForms\Abstracts
8 * @since 1.3.0
9 */
10
11 defined( 'ABSPATH' ) || exit;
12
13 /**
14 * EVF_Settings_API class.
15 */
16 abstract class EVF_Settings_API {
17
18 /**
19 * The plugin ID. Used for option names.
20 *
21 * @var string
22 */
23 public $plugin_id = 'everest_forms_';
24
25 /**
26 * ID of the class extending the settings API. Used in option names.
27 *
28 * @var string
29 */
30 public $id = '';
31
32 /**
33 * Validation errors.
34 *
35 * @var array of strings
36 */
37 public $errors = array();
38
39 /**
40 * Setting values.
41 *
42 * @var array
43 */
44 public $settings = array();
45
46 /**
47 * Form option fields.
48 *
49 * @var array
50 */
51 public $form_fields = array();
52
53 /**
54 * The posted settings data. When empty, $_POST data will be used.
55 *
56 * @var array
57 */
58 protected $data = array();
59
60 /**
61 * Get the form fields after they are initialized.
62 *
63 * @return array of options
64 */
65 public function get_form_fields() {
66 return apply_filters( 'everest_forms_settings_api_form_fields_' . $this->id, array_map( array( $this, 'set_defaults' ), $this->form_fields ) );
67 }
68
69 /**
70 * Set default required properties for each field.
71 *
72 * @param array $field Setting field array.
73 * @return array
74 */
75 protected function set_defaults( $field ) {
76 if ( ! isset( $field['default'] ) ) {
77 $field['default'] = '';
78 }
79 return $field;
80 }
81
82 /**
83 * Output the admin options table.
84 */
85 public function admin_options() {
86 echo '<table class="form-table">' . wp_kses_post( $this->generate_settings_html( $this->get_form_fields(), false ) ) . '</table>';
87 }
88
89 /**
90 * Initialise settings form fields.
91 */
92 public function init_form_fields() {}
93
94 /**
95 * Return the name of the option in the WP DB.
96 *
97 * @return string
98 */
99 public function get_option_key() {
100 return $this->plugin_id . $this->id . '_settings';
101 }
102
103 /**
104 * Get a fields type. Defaults to "text" if not set.
105 *
106 * @param array $field Field key.
107 * @return string
108 */
109 public function get_field_type( $field ) {
110 return empty( $field['type'] ) ? 'text' : $field['type'];
111 }
112
113 /**
114 * Get a fields default value. Defaults to "" if not set.
115 *
116 * @param array $field Field key.
117 * @return string
118 */
119 public function get_field_default( $field ) {
120 return empty( $field['default'] ) ? '' : $field['default'];
121 }
122
123 /**
124 * Get a field's posted and validated value.
125 *
126 * @param string $key Field key.
127 * @param array $field Field array.
128 * @param array $post_data Posted data.
129 * @return string
130 */
131 public function get_field_value( $key, $field, $post_data = array() ) {
132 $type = $this->get_field_type( $field );
133 $field_key = $this->get_field_key( $key );
134 $post_data = empty( $post_data ) ? $_POST : $post_data; // phpcs:ignore WordPress.Security.NonceVerification
135 $value = isset( $post_data[ $field_key ] ) ? $post_data[ $field_key ] : null;
136
137 if ( isset( $field['sanitize_callback'] ) && is_callable( $field['sanitize_callback'] ) ) {
138 return call_user_func( $field['sanitize_callback'], $value );
139 }
140
141 // Look for a validate_FIELDID_field method for special handling.
142 if ( is_callable( array( $this, 'validate_' . $key . '_field' ) ) ) {
143 return $this->{'validate_' . $key . '_field'}( $key, $value );
144 }
145
146 // Look for a validate_FIELDTYPE_field method.
147 if ( is_callable( array( $this, 'validate_' . $type . '_field' ) ) ) {
148 return $this->{'validate_' . $type . '_field'}( $key, $value );
149 }
150
151 // Fallback to text.
152 return $this->validate_text_field( $key, $value );
153 }
154
155 /**
156 * Sets the POSTed data. This method can be used to set specific data, instead of taking it from the $_POST array.
157 *
158 * @param array $data Posted data.
159 */
160 public function set_post_data( $data = array() ) {
161 $this->data = $data;
162 }
163
164 /**
165 * Returns the POSTed data, to be used to save the settings.
166 *
167 * @return array
168 */
169 public function get_post_data() {
170 if ( ! empty( $this->data ) && is_array( $this->data ) ) {
171 return $this->data;
172 }
173 return $_POST; // phpcs:ignore WordPress.Security.NonceVerification
174 }
175
176 /**
177 * Update a single option.
178 *
179 * @param string $key Option key.
180 * @param mixed $value Value to set.
181 * @return bool was anything saved?
182 */
183 public function update_option( $key, $value = '' ) {
184 if ( empty( $this->settings ) ) {
185 $this->init_settings();
186 }
187
188 $this->settings[ $key ] = $value;
189
190 return update_option( $this->get_option_key(), apply_filters( 'everest_forms_settings_api_sanitized_fields_' . $this->id, $this->settings ), 'yes' );
191 }
192
193 /**
194 * Processes and saves options.
195 * If there is an error thrown, will continue to save and validate fields, but will leave the erroring field out.
196 *
197 * @return bool was anything saved?
198 */
199 public function process_admin_options() {
200 $this->init_settings();
201
202 $post_data = $this->get_post_data();
203
204 foreach ( $this->get_form_fields() as $key => $field ) {
205 if ( 'title' !== $this->get_field_type( $field ) ) {
206 try {
207 $this->settings[ $key ] = $this->get_field_value( $key, $field, $post_data );
208 } catch ( Exception $e ) {
209 $this->add_error( $e->getMessage() );
210 }
211 }
212 }
213
214 return update_option( $this->get_option_key(), apply_filters( 'everest_forms_settings_api_sanitized_fields_' . $this->id, $this->settings ), 'yes' );
215 }
216
217 /**
218 * Add an error message for display in admin on save.
219 *
220 * @param string $error Error message.
221 */
222 public function add_error( $error ) {
223 $this->errors[] = $error;
224 }
225
226 /**
227 * Get admin error messages.
228 */
229 public function get_errors() {
230 return $this->errors;
231 }
232
233 /**
234 * Display admin error messages.
235 */
236 public function display_errors() {
237 if ( $this->get_errors() ) {
238 echo '<div id="everest_forms_errors" class="error notice is-dismissible">';
239 foreach ( $this->get_errors() as $error ) {
240 echo '<p>' . wp_kses_post( $error ) . '</p>';
241 }
242 echo '</div>';
243 }
244 }
245
246 /**
247 * Initialise Settings.
248 *
249 * Store all settings in a single database entry
250 * and make sure the $settings array is either the default
251 * or the settings stored in the database.
252 *
253 * @uses get_option(), add_option()
254 */
255 public function init_settings() {
256 $this->settings = get_option( $this->get_option_key(), null );
257
258 // If there are no settings defined, use defaults.
259 if ( ! is_array( $this->settings ) ) {
260 $form_fields = $this->get_form_fields();
261 $this->settings = array_merge( array_fill_keys( array_keys( $form_fields ), '' ), wp_list_pluck( $form_fields, 'default' ) );
262 }
263 }
264
265 /**
266 * Get option from DB.
267 *
268 * Gets an option from the settings API, using defaults if necessary to prevent undefined notices.
269 *
270 * @param string $key Option key.
271 * @param mixed $empty_value Value when empty.
272 * @return string The value specified for the option or a default value for the option.
273 */
274 public function get_option( $key, $empty_value = null ) {
275 if ( empty( $this->settings ) ) {
276 $this->init_settings();
277 }
278
279 // Get option default if unset.
280 if ( ! isset( $this->settings[ $key ] ) ) {
281 $form_fields = $this->get_form_fields();
282 $this->settings[ $key ] = isset( $form_fields[ $key ] ) ? $this->get_field_default( $form_fields[ $key ] ) : '';
283 }
284
285 if ( ! is_null( $empty_value ) && '' === $this->settings[ $key ] ) {
286 $this->settings[ $key ] = $empty_value;
287 }
288
289 return $this->settings[ $key ];
290 }
291
292 /**
293 * Prefix key for settings.
294 *
295 * @param string $key Field key.
296 * @return string
297 */
298 public function get_field_key( $key ) {
299 return $this->plugin_id . $this->id . '_' . $key;
300 }
301
302 /**
303 * Generate Settings HTML.
304 *
305 * Generate the HTML for the fields on the "settings" screen.
306 *
307 * @param array $form_fields (default: array()) Array of form fields.
308 * @param bool $echo Echo or return.
309 * @return string the html for the settings
310 * @uses method_exists()
311 */
312 public function generate_settings_html( $form_fields = array(), $echo = true ) {
313 if ( empty( $form_fields ) ) {
314 $form_fields = $this->get_form_fields();
315 }
316
317 if ( $echo ) {
318 foreach ( $form_fields as $k => $v ) {
319 $type = $this->get_field_type( $v );
320 if ( method_exists( $this, 'generate_' . $type . '_html' ) ) {
321 echo $this->{'generate_' . $type . '_html'}( $k, $v ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
322 } else {
323 echo $this->generate_text_html( $k, $v ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
324 }
325 }
326 } else {
327 $html = '';
328 foreach ( $form_fields as $k => $v ) {
329 $type = $this->get_field_type( $v );
330
331 if ( method_exists( $this, 'generate_' . $type . '_html' ) ) {
332 $html .= $this->{'generate_' . $type . '_html'}( $k, $v );
333 } else {
334 $html .= $this->generate_text_html( $k, $v );
335 }
336 }
337 return $html;
338 }
339 }
340
341 /**
342 * Get HTML for tooltips.
343 *
344 * @param array $data Data for the tooltip.
345 * @return string
346 */
347 public function get_tooltip_html( $data ) {
348 if ( true === $data['desc_tip'] ) {
349 $tip = $data['description'];
350 } elseif ( ! empty( $data['desc_tip'] ) ) {
351 $tip = $data['desc_tip'];
352 } else {
353 $tip = '';
354 }
355
356 return $tip ? evf_help_tip( $tip, true ) : '';
357 }
358
359 /**
360 * Get HTML for descriptions.
361 *
362 * @param array $data Data for the description.
363 * @return string
364 */
365 public function get_description_html( $data ) {
366 if ( true === $data['desc_tip'] ) {
367 $description = '';
368 } elseif ( ! empty( $data['desc_tip'] ) ) {
369 $description = $data['description'];
370 } elseif ( ! empty( $data['description'] ) ) {
371 $description = $data['description'];
372 } else {
373 $description = '';
374 }
375
376 return $description ? '<p class="description">' . wp_kses_post( $description ) . '</p>' . "\n" : '';
377 }
378
379 /**
380 * Get custom attributes.
381 *
382 * @param array $data Field data.
383 * @return string
384 */
385 public function get_custom_attribute_html( $data ) {
386 $custom_attributes = array();
387
388 if ( ! empty( $data['custom_attributes'] ) && is_array( $data['custom_attributes'] ) ) {
389 foreach ( $data['custom_attributes'] as $attribute => $attribute_value ) {
390 $custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $attribute_value ) . '"';
391 }
392 }
393
394 return implode( ' ', $custom_attributes );
395 }
396
397 /**
398 * Generate Text Input HTML.
399 *
400 * @param string $key Field key.
401 * @param array $data Field data.
402 * @return string
403 */
404 public function generate_text_html( $key, $data ) {
405 $field_key = $this->get_field_key( $key );
406 $defaults = array(
407 'title' => '',
408 'disabled' => false,
409 'class' => '',
410 'css' => '',
411 'placeholder' => '',
412 'type' => 'text',
413 'desc_tip' => false,
414 'description' => '',
415 'custom_attributes' => array(),
416 );
417
418 $data = wp_parse_args( $data, $defaults );
419
420 ob_start();
421 ?>
422 <tr valign="top">
423 <th scope="row" class="titledesc">
424 <label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?> <?php echo wp_kses_post( $this->get_tooltip_html( $data ) ); ?></label>
425 </th>
426 <td class="forminp">
427 <fieldset>
428 <legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
429 <input class="input-text regular-input <?php echo esc_attr( $data['class'] ); ?>" type="<?php echo esc_attr( $data['type'] ); ?>" name="<?php echo esc_attr( $field_key ); ?>" id="<?php echo esc_attr( $field_key ); ?>" style="<?php echo esc_attr( $data['css'] ); ?>" value="<?php echo esc_attr( $this->get_option( $key ) ); ?>" placeholder="<?php echo esc_attr( $data['placeholder'] ); ?>" <?php disabled( $data['disabled'], true ); ?> <?php echo wp_kses_post( $this->get_custom_attribute_html( $data ) ); ?> />
430 <?php echo wp_kses_post( $this->get_description_html( $data ) ); ?>
431 </fieldset>
432 </td>
433 </tr>
434 <?php
435
436 return ob_get_clean();
437 }
438
439 /**
440 * Generate Password Input HTML.
441 *
442 * @param string $key Field key.
443 * @param array $data Field data.
444 * @return string
445 */
446 public function generate_password_html( $key, $data ) {
447 $data['type'] = 'password';
448 return $this->generate_text_html( $key, $data );
449 }
450
451 /**
452 * Generate Color Picker Input HTML.
453 *
454 * @param string $key Field key.
455 * @param array $data Field data.
456 * @return string
457 */
458 public function generate_color_html( $key, $data ) {
459 $field_key = $this->get_field_key( $key );
460 $defaults = array(
461 'title' => '',
462 'disabled' => false,
463 'class' => '',
464 'css' => '',
465 'placeholder' => '',
466 'desc_tip' => false,
467 'description' => '',
468 'custom_attributes' => array(),
469 );
470
471 $data = wp_parse_args( $data, $defaults );
472
473 ob_start();
474 ?>
475 <tr valign="top">
476 <th scope="row" class="titledesc">
477 <label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?> <?php echo wp_kses_post( $this->get_tooltip_html( $data ) ); ?></label>
478 </th>
479 <td class="forminp">
480 <fieldset>
481 <legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
482 <span class="colorpickpreview" style="background:<?php echo esc_attr( $this->get_option( $key ) ); ?>;">&nbsp;</span>
483 <input class="colorpick <?php echo esc_attr( $data['class'] ); ?>" type="text" name="<?php echo esc_attr( $field_key ); ?>" id="<?php echo esc_attr( $field_key ); ?>" style="<?php echo esc_attr( $data['css'] ); ?>" value="<?php echo esc_attr( $this->get_option( $key ) ); ?>" placeholder="<?php echo esc_attr( $data['placeholder'] ); ?>" <?php disabled( $data['disabled'], true ); ?> <?php echo wp_kses_post( $this->get_custom_attribute_html( $data ) ); ?> />
484 <div id="colorPickerDiv_<?php echo esc_attr( $field_key ); ?>" class="colorpickdiv" style="z-index: 100; background: #eee; border: 1px solid #ccc; position: absolute; display: none;"></div>
485 <?php echo wp_kses_post( $this->get_description_html( $data ) ); ?>
486 </fieldset>
487 </td>
488 </tr>
489 <?php
490
491 return ob_get_clean();
492 }
493
494 /**
495 * Generate Textarea HTML.
496 *
497 * @param string $key Field key.
498 * @param array $data Field data.
499 * @return string
500 */
501 public function generate_textarea_html( $key, $data ) {
502 $field_key = $this->get_field_key( $key );
503 $defaults = array(
504 'title' => '',
505 'disabled' => false,
506 'class' => '',
507 'css' => '',
508 'placeholder' => '',
509 'type' => 'text',
510 'desc_tip' => false,
511 'description' => '',
512 'custom_attributes' => array(),
513 );
514
515 $data = wp_parse_args( $data, $defaults );
516
517 ob_start();
518 ?>
519 <tr valign="top">
520 <th scope="row" class="titledesc">
521 <label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?> <?php echo wp_kses_post( $this->get_tooltip_html( $data ) ); ?></label>
522 </th>
523 <td class="forminp">
524 <fieldset>
525 <legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
526 <textarea rows="3" cols="20" class="input-text wide-input <?php echo esc_attr( $data['class'] ); ?>" type="<?php echo esc_attr( $data['type'] ); ?>" name="<?php echo esc_attr( $field_key ); ?>" id="<?php echo esc_attr( $field_key ); ?>" style="<?php echo esc_attr( $data['css'] ); ?>" placeholder="<?php echo esc_attr( $data['placeholder'] ); ?>" <?php disabled( $data['disabled'], true ); ?> <?php echo wp_kses_post( $this->get_custom_attribute_html( $data ) ); ?>><?php echo esc_textarea( $this->get_option( $key ) ); ?></textarea>
527 <?php echo wp_kses_post( $this->get_description_html( $data ) ); ?>
528 </fieldset>
529 </td>
530 </tr>
531 <?php
532
533 return ob_get_clean();
534 }
535
536 /**
537 * Generate Checkbox HTML.
538 *
539 * @param string $key Field key.
540 * @param array $data Field data.
541 * @return string
542 */
543 public function generate_checkbox_html( $key, $data ) {
544 $field_key = $this->get_field_key( $key );
545 $defaults = array(
546 'title' => '',
547 'label' => '',
548 'disabled' => false,
549 'class' => '',
550 'css' => '',
551 'type' => 'text',
552 'desc_tip' => false,
553 'description' => '',
554 'custom_attributes' => array(),
555 );
556
557 $data = wp_parse_args( $data, $defaults );
558
559 if ( ! $data['label'] ) {
560 $data['label'] = $data['title'];
561 }
562
563 ob_start();
564 ?>
565 <tr valign="top">
566 <th scope="row" class="titledesc">
567 <label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?> <?php echo wp_kses_post( $this->get_tooltip_html( $data ) ); ?></label>
568 </th>
569 <td class="forminp">
570 <fieldset>
571 <legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
572 <label for="<?php echo esc_attr( $field_key ); ?>">
573 <input <?php disabled( $data['disabled'], true ); ?> class="<?php echo esc_attr( $data['class'] ); ?>" type="checkbox" name="<?php echo esc_attr( $field_key ); ?>" id="<?php echo esc_attr( $field_key ); ?>" style="<?php echo esc_attr( $data['css'] ); ?>" value="1" <?php checked( $this->get_option( $key ), 'yes' ); ?> <?php echo wp_kses_post( $this->get_custom_attribute_html( $data ) ); ?> /> <?php echo wp_kses_post( $data['label'] ); ?></label><br/>
574 <?php echo wp_kses_post( $this->get_description_html( $data ) ); ?>
575 </fieldset>
576 </td>
577 </tr>
578 <?php
579
580 return ob_get_clean();
581 }
582
583 /**
584 * Generate Select HTML.
585 *
586 * @param string $key Field key.
587 * @param array $data Field data.
588 * @return string
589 */
590 public function generate_select_html( $key, $data ) {
591 $field_key = $this->get_field_key( $key );
592 $defaults = array(
593 'title' => '',
594 'disabled' => false,
595 'class' => '',
596 'css' => '',
597 'placeholder' => '',
598 'type' => 'text',
599 'desc_tip' => false,
600 'description' => '',
601 'custom_attributes' => array(),
602 'options' => array(),
603 );
604
605 $data = wp_parse_args( $data, $defaults );
606
607 ob_start();
608 ?>
609 <tr valign="top">
610 <th scope="row" class="titledesc">
611 <label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?> <?php echo wp_kses_post( $this->get_tooltip_html( $data ) ); ?></label>
612 </th>
613 <td class="forminp">
614 <fieldset>
615 <legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
616 <select class="select <?php echo esc_attr( $data['class'] ); ?>" name="<?php echo esc_attr( $field_key ); ?>" id="<?php echo esc_attr( $field_key ); ?>" style="<?php echo esc_attr( $data['css'] ); ?>" <?php disabled( $data['disabled'], true ); ?> <?php echo wp_kses_post( $this->get_custom_attribute_html( $data ) ); ?>>
617 <?php foreach ( (array) $data['options'] as $option_key => $option_value ) : ?>
618 <option value="<?php echo esc_attr( $option_key ); ?>" <?php selected( $option_key, esc_attr( $this->get_option( $key ) ) ); ?>><?php echo esc_attr( $option_value ); ?></option>
619 <?php endforeach; ?>
620 </select>
621 <?php echo wp_kses_post( $this->get_description_html( $data ) ); ?>
622 </fieldset>
623 </td>
624 </tr>
625 <?php
626
627 return ob_get_clean();
628 }
629
630 /**
631 * Generate Multiselect HTML.
632 *
633 * @param string $key Field key.
634 * @param array $data Field data.
635 * @return string
636 */
637 public function generate_multiselect_html( $key, $data ) {
638 $field_key = $this->get_field_key( $key );
639 $defaults = array(
640 'title' => '',
641 'disabled' => false,
642 'class' => '',
643 'css' => '',
644 'placeholder' => '',
645 'type' => 'text',
646 'desc_tip' => false,
647 'description' => '',
648 'custom_attributes' => array(),
649 'select_buttons' => false,
650 'options' => array(),
651 );
652
653 $data = wp_parse_args( $data, $defaults );
654 $value = (array) $this->get_option( $key, array() );
655
656 ob_start();
657 ?>
658 <tr valign="top">
659 <th scope="row" class="titledesc">
660 <label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?> <?php echo wp_kses_post( $this->get_tooltip_html( $data ) ); ?></label>
661 </th>
662 <td class="forminp">
663 <fieldset>
664 <legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
665 <select multiple="multiple" class="multiselect <?php echo esc_attr( $data['class'] ); ?>" name="<?php echo esc_attr( $field_key ); ?>[]" id="<?php echo esc_attr( $field_key ); ?>" style="<?php echo esc_attr( $data['css'] ); ?>" <?php disabled( $data['disabled'], true ); ?> <?php echo wp_kses_post( $this->get_custom_attribute_html( $data ) ); ?>>
666 <?php foreach ( (array) $data['options'] as $option_key => $option_value ) : ?>
667 <?php if ( is_array( $option_value ) ) : ?>
668 <optgroup label="<?php echo esc_attr( $option_key ); ?>">
669 <?php foreach ( $option_value as $option_key_inner => $option_value_inner ) : ?>
670 <option value="<?php echo esc_attr( $option_key_inner ); ?>" <?php selected( in_array( $option_key_inner, $value, true ), true ); ?>><?php echo esc_attr( $option_value_inner ); ?></option>
671 <?php endforeach; ?>
672 </optgroup>
673 <?php else : ?>
674 <option value="<?php echo esc_attr( $option_key ); ?>" <?php selected( in_array( $option_key, $value, true ), true ); ?>><?php echo esc_attr( $option_value ); ?></option>
675 <?php endif; ?>
676 <?php endforeach; ?>
677 </select>
678 <?php echo wp_kses_post( $this->get_description_html( $data ) ); ?>
679 <?php if ( $data['select_buttons'] ) : ?>
680 <br/><a class="select_all button" href="#"><?php esc_html_e( 'Select all', 'everest-forms' ); ?></a> <a class="select_none button" href="#"><?php esc_html_e( 'Select none', 'everest-forms' ); ?></a>
681 <?php endif; ?>
682 </fieldset>
683 </td>
684 </tr>
685 <?php
686
687 return ob_get_clean();
688 }
689
690 /**
691 * Generate Title HTML.
692 *
693 * @param string $key Field key.
694 * @param array $data Field data.
695 * @return string
696 */
697 public function generate_title_html( $key, $data ) {
698 $field_key = $this->get_field_key( $key );
699 $defaults = array(
700 'title' => '',
701 'class' => '',
702 );
703
704 $data = wp_parse_args( $data, $defaults );
705
706 ob_start();
707 ?>
708 </table>
709 <h3 class="evf-settings-sub-title <?php echo esc_attr( $data['class'] ); ?>" id="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></h3>
710 <?php if ( ! empty( $data['description'] ) ) : ?>
711 <p><?php echo wp_kses_post( $data['description'] ); ?></p>
712 <?php endif; ?>
713 <table class="form-table">
714 <?php
715
716 return ob_get_clean();
717 }
718
719 /**
720 * Validate Text Field.
721 *
722 * Make sure the data is escaped correctly, etc.
723 *
724 * @param string $key Field key.
725 * @param string $value Posted Value.
726 * @return string
727 */
728 public function validate_text_field( $key, $value ) {
729 $value = is_null( $value ) ? '' : $value;
730 return wp_kses_post( trim( wp_unslash( $value ) ) );
731 }
732
733 /**
734 * Validate Password Field. No input sanitization is used to avoid corrupting passwords.
735 *
736 * @param string $key Field key.
737 * @param string $value Posted Value.
738 * @return string
739 */
740 public function validate_password_field( $key, $value ) {
741 $value = is_null( $value ) ? '' : $value;
742 return trim( wp_unslash( $value ) );
743 }
744
745 /**
746 * Validate Textarea Field.
747 *
748 * @param string $key Field key.
749 * @param string $value Posted Value.
750 * @return string
751 */
752 public function validate_textarea_field( $key, $value ) {
753 $value = is_null( $value ) ? '' : $value;
754 return wp_kses(
755 trim( wp_unslash( $value ) ),
756 array_merge(
757 array(
758 'iframe' => array(
759 'src' => true,
760 'style' => true,
761 'id' => true,
762 'class' => true,
763 ),
764 ),
765 wp_kses_allowed_html( 'post' )
766 )
767 );
768 }
769
770 /**
771 * Validate Checkbox Field.
772 *
773 * If not set, return "no", otherwise return "yes".
774 *
775 * @param string $key Field key.
776 * @param string $value Posted Value.
777 * @return string
778 */
779 public function validate_checkbox_field( $key, $value ) {
780 return ! is_null( $value ) ? 'yes' : 'no';
781 }
782
783 /**
784 * Validate Select Field.
785 *
786 * @param string $key Field key.
787 * @param string $value Posted Value.
788 * @return string
789 */
790 public function validate_select_field( $key, $value ) {
791 $value = is_null( $value ) ? '' : $value;
792 return evf_clean( wp_unslash( $value ) );
793 }
794
795 /**
796 * Validate Multiselect Field.
797 *
798 * @param string $key Field key.
799 * @param string $value Posted Value.
800 * @return string|array
801 */
802 public function validate_multiselect_field( $key, $value ) {
803 return is_array( $value ) ? array_map( 'evf_clean', array_map( 'stripslashes', $value ) ) : '';
804 }
805 }
806