PluginProbe ʕ •ᴥ•ʔ
Everest Forms – Contact Form, Payment Form, Quiz, Survey & Custom Form Builder with AI / 1.6.3
Everest Forms – Contact Form, Payment Form, Quiz, Survey & Custom Form Builder with AI v1.6.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 6 years ago class-evf-integration.php 7 years ago class-evf-log-handler.php 6 years ago class-evf-session.php 8 years ago class-evf-settings-api.php 6 years ago
class-evf-settings-api.php
800 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">' . $this->generate_settings_html( $this->get_form_fields(), false ) . '</table>'; // phpcs:ignore WordPress.Security.EscapeOutput
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 $html = '';
318 foreach ( $form_fields as $k => $v ) {
319 $type = $this->get_field_type( $v );
320
321 if ( method_exists( $this, 'generate_' . $type . '_html' ) ) {
322 $html .= $this->{'generate_' . $type . '_html'}( $k, $v );
323 } else {
324 $html .= $this->generate_text_html( $k, $v );
325 }
326 }
327
328 if ( $echo ) {
329 echo $html; // phpcs:ignore WordPress.Security.EscapeOutput
330 } else {
331 return $html;
332 }
333 }
334
335 /**
336 * Get HTML for tooltips.
337 *
338 * @param array $data Data for the tooltip.
339 * @return string
340 */
341 public function get_tooltip_html( $data ) {
342 if ( true === $data['desc_tip'] ) {
343 $tip = $data['description'];
344 } elseif ( ! empty( $data['desc_tip'] ) ) {
345 $tip = $data['desc_tip'];
346 } else {
347 $tip = '';
348 }
349
350 return $tip ? evf_help_tip( $tip, true ) : '';
351 }
352
353 /**
354 * Get HTML for descriptions.
355 *
356 * @param array $data Data for the description.
357 * @return string
358 */
359 public function get_description_html( $data ) {
360 if ( true === $data['desc_tip'] ) {
361 $description = '';
362 } elseif ( ! empty( $data['desc_tip'] ) ) {
363 $description = $data['description'];
364 } elseif ( ! empty( $data['description'] ) ) {
365 $description = $data['description'];
366 } else {
367 $description = '';
368 }
369
370 return $description ? '<p class="description">' . wp_kses_post( $description ) . '</p>' . "\n" : '';
371 }
372
373 /**
374 * Get custom attributes.
375 *
376 * @param array $data Field data.
377 * @return string
378 */
379 public function get_custom_attribute_html( $data ) {
380 $custom_attributes = array();
381
382 if ( ! empty( $data['custom_attributes'] ) && is_array( $data['custom_attributes'] ) ) {
383 foreach ( $data['custom_attributes'] as $attribute => $attribute_value ) {
384 $custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $attribute_value ) . '"';
385 }
386 }
387
388 return implode( ' ', $custom_attributes );
389 }
390
391 /**
392 * Generate Text Input HTML.
393 *
394 * @param string $key Field key.
395 * @param array $data Field data.
396 * @return string
397 */
398 public function generate_text_html( $key, $data ) {
399 $field_key = $this->get_field_key( $key );
400 $defaults = array(
401 'title' => '',
402 'disabled' => false,
403 'class' => '',
404 'css' => '',
405 'placeholder' => '',
406 'type' => 'text',
407 'desc_tip' => false,
408 'description' => '',
409 'custom_attributes' => array(),
410 );
411
412 $data = wp_parse_args( $data, $defaults );
413
414 ob_start();
415 ?>
416 <tr valign="top">
417 <th scope="row" class="titledesc">
418 <label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?> <?php echo $this->get_tooltip_html( $data ); // phpcs:ignore WordPress.Security.EscapeOutput ?></label>
419 </th>
420 <td class="forminp">
421 <fieldset>
422 <legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
423 <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 $this->get_custom_attribute_html( $data ); // phpcs:ignore WordPress.Security.EscapeOutput ?> />
424 <?php echo $this->get_description_html( $data ); // phpcs:ignore WordPress.Security.EscapeOutput ?>
425 </fieldset>
426 </td>
427 </tr>
428 <?php
429
430 return ob_get_clean();
431 }
432
433 /**
434 * Generate Password Input HTML.
435 *
436 * @param string $key Field key.
437 * @param array $data Field data.
438 * @return string
439 */
440 public function generate_password_html( $key, $data ) {
441 $data['type'] = 'password';
442 return $this->generate_text_html( $key, $data );
443 }
444
445 /**
446 * Generate Color Picker Input HTML.
447 *
448 * @param string $key Field key.
449 * @param array $data Field data.
450 * @return string
451 */
452 public function generate_color_html( $key, $data ) {
453 $field_key = $this->get_field_key( $key );
454 $defaults = array(
455 'title' => '',
456 'disabled' => false,
457 'class' => '',
458 'css' => '',
459 'placeholder' => '',
460 'desc_tip' => false,
461 'description' => '',
462 'custom_attributes' => array(),
463 );
464
465 $data = wp_parse_args( $data, $defaults );
466
467 ob_start();
468 ?>
469 <tr valign="top">
470 <th scope="row" class="titledesc">
471 <label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?> <?php echo $this->get_tooltip_html( $data ); // phpcs:ignore WordPress.Security.EscapeOutput ?></label>
472 </th>
473 <td class="forminp">
474 <fieldset>
475 <legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
476 <span class="colorpickpreview" style="background:<?php echo esc_attr( $this->get_option( $key ) ); ?>;">&nbsp;</span>
477 <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 $this->get_custom_attribute_html( $data ); // phpcs:ignore WordPress.Security.EscapeOutput ?> />
478 <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>
479 <?php echo $this->get_description_html( $data ); // phpcs:ignore WordPress.Security.EscapeOutput ?>
480 </fieldset>
481 </td>
482 </tr>
483 <?php
484
485 return ob_get_clean();
486 }
487
488 /**
489 * Generate Textarea HTML.
490 *
491 * @param string $key Field key.
492 * @param array $data Field data.
493 * @return string
494 */
495 public function generate_textarea_html( $key, $data ) {
496 $field_key = $this->get_field_key( $key );
497 $defaults = array(
498 'title' => '',
499 'disabled' => false,
500 'class' => '',
501 'css' => '',
502 'placeholder' => '',
503 'type' => 'text',
504 'desc_tip' => false,
505 'description' => '',
506 'custom_attributes' => array(),
507 );
508
509 $data = wp_parse_args( $data, $defaults );
510
511 ob_start();
512 ?>
513 <tr valign="top">
514 <th scope="row" class="titledesc">
515 <label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?> <?php echo $this->get_tooltip_html( $data ); // phpcs:ignore WordPress.Security.EscapeOutput ?></label>
516 </th>
517 <td class="forminp">
518 <fieldset>
519 <legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
520 <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 $this->get_custom_attribute_html( $data ); // phpcs:ignore WordPress.Security.EscapeOutput ?>><?php echo esc_textarea( $this->get_option( $key ) ); ?></textarea>
521 <?php echo $this->get_description_html( $data ); // phpcs:ignore WordPress.Security.EscapeOutput ?>
522 </fieldset>
523 </td>
524 </tr>
525 <?php
526
527 return ob_get_clean();
528 }
529
530 /**
531 * Generate Checkbox HTML.
532 *
533 * @param string $key Field key.
534 * @param array $data Field data.
535 * @return string
536 */
537 public function generate_checkbox_html( $key, $data ) {
538 $field_key = $this->get_field_key( $key );
539 $defaults = array(
540 'title' => '',
541 'label' => '',
542 'disabled' => false,
543 'class' => '',
544 'css' => '',
545 'type' => 'text',
546 'desc_tip' => false,
547 'description' => '',
548 'custom_attributes' => array(),
549 );
550
551 $data = wp_parse_args( $data, $defaults );
552
553 if ( ! $data['label'] ) {
554 $data['label'] = $data['title'];
555 }
556
557 ob_start();
558 ?>
559 <tr valign="top">
560 <th scope="row" class="titledesc">
561 <label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?> <?php echo $this->get_tooltip_html( $data ); // phpcs:ignore WordPress.Security.EscapeOutput ?></label>
562 </th>
563 <td class="forminp">
564 <fieldset>
565 <legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
566 <label for="<?php echo esc_attr( $field_key ); ?>">
567 <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 $this->get_custom_attribute_html( $data ); // phpcs:ignore WordPress.Security.EscapeOutput ?> /> <?php echo wp_kses_post( $data['label'] ); ?></label><br/>
568 <?php echo $this->get_description_html( $data ); // phpcs:ignore WordPress.Security.EscapeOutput ?>
569 </fieldset>
570 </td>
571 </tr>
572 <?php
573
574 return ob_get_clean();
575 }
576
577 /**
578 * Generate Select HTML.
579 *
580 * @param string $key Field key.
581 * @param array $data Field data.
582 * @return string
583 */
584 public function generate_select_html( $key, $data ) {
585 $field_key = $this->get_field_key( $key );
586 $defaults = array(
587 'title' => '',
588 'disabled' => false,
589 'class' => '',
590 'css' => '',
591 'placeholder' => '',
592 'type' => 'text',
593 'desc_tip' => false,
594 'description' => '',
595 'custom_attributes' => array(),
596 'options' => array(),
597 );
598
599 $data = wp_parse_args( $data, $defaults );
600
601 ob_start();
602 ?>
603 <tr valign="top">
604 <th scope="row" class="titledesc">
605 <label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?> <?php echo $this->get_tooltip_html( $data ); // phpcs:ignore WordPress.Security.EscapeOutput ?></label>
606 </th>
607 <td class="forminp">
608 <fieldset>
609 <legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
610 <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 $this->get_custom_attribute_html( $data ); // phpcs:ignore WordPress.Security.EscapeOutput ?>>
611 <?php foreach ( (array) $data['options'] as $option_key => $option_value ) : ?>
612 <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>
613 <?php endforeach; ?>
614 </select>
615 <?php echo $this->get_description_html( $data ); // phpcs:ignore WordPress.Security.EscapeOutput ?>
616 </fieldset>
617 </td>
618 </tr>
619 <?php
620
621 return ob_get_clean();
622 }
623
624 /**
625 * Generate Multiselect HTML.
626 *
627 * @param string $key Field key.
628 * @param array $data Field data.
629 * @return string
630 */
631 public function generate_multiselect_html( $key, $data ) {
632 $field_key = $this->get_field_key( $key );
633 $defaults = array(
634 'title' => '',
635 'disabled' => false,
636 'class' => '',
637 'css' => '',
638 'placeholder' => '',
639 'type' => 'text',
640 'desc_tip' => false,
641 'description' => '',
642 'custom_attributes' => array(),
643 'select_buttons' => false,
644 'options' => array(),
645 );
646
647 $data = wp_parse_args( $data, $defaults );
648 $value = (array) $this->get_option( $key, array() );
649
650 ob_start();
651 ?>
652 <tr valign="top">
653 <th scope="row" class="titledesc">
654 <label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?> <?php echo $this->get_tooltip_html( $data ); // phpcs:ignore WordPress.Security.EscapeOutput ?></label>
655 </th>
656 <td class="forminp">
657 <fieldset>
658 <legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
659 <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 $this->get_custom_attribute_html( $data ); // phpcs:ignore WordPress.Security.EscapeOutput ?>>
660 <?php foreach ( (array) $data['options'] as $option_key => $option_value ) : ?>
661 <?php if ( is_array( $option_value ) ) : ?>
662 <optgroup label="<?php echo esc_attr( $option_key ); ?>">
663 <?php foreach ( $option_value as $option_key_inner => $option_value_inner ) : ?>
664 <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>
665 <?php endforeach; ?>
666 </optgroup>
667 <?php else : ?>
668 <option value="<?php echo esc_attr( $option_key ); ?>" <?php selected( in_array( $option_key, $value, true ), true ); ?>><?php echo esc_attr( $option_value ); ?></option>
669 <?php endif; ?>
670 <?php endforeach; ?>
671 </select>
672 <?php echo $this->get_description_html( $data ); // phpcs:ignore WordPress.Security.EscapeOutput ?>
673 <?php if ( $data['select_buttons'] ) : ?>
674 <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>
675 <?php endif; ?>
676 </fieldset>
677 </td>
678 </tr>
679 <?php
680
681 return ob_get_clean();
682 }
683
684 /**
685 * Generate Title HTML.
686 *
687 * @param string $key Field key.
688 * @param array $data Field data.
689 * @return string
690 */
691 public function generate_title_html( $key, $data ) {
692 $field_key = $this->get_field_key( $key );
693 $defaults = array(
694 'title' => '',
695 'class' => '',
696 );
697
698 $data = wp_parse_args( $data, $defaults );
699
700 ob_start();
701 ?>
702 </table>
703 <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>
704 <?php if ( ! empty( $data['description'] ) ) : ?>
705 <p><?php echo wp_kses_post( $data['description'] ); ?></p>
706 <?php endif; ?>
707 <table class="form-table">
708 <?php
709
710 return ob_get_clean();
711 }
712
713 /**
714 * Validate Text Field.
715 *
716 * Make sure the data is escaped correctly, etc.
717 *
718 * @param string $key Field key.
719 * @param string $value Posted Value.
720 * @return string
721 */
722 public function validate_text_field( $key, $value ) {
723 $value = is_null( $value ) ? '' : $value;
724 return wp_kses_post( trim( stripslashes( $value ) ) );
725 }
726
727 /**
728 * Validate Password Field. No input sanitization is used to avoid corrupting passwords.
729 *
730 * @param string $key Field key.
731 * @param string $value Posted Value.
732 * @return string
733 */
734 public function validate_password_field( $key, $value ) {
735 $value = is_null( $value ) ? '' : $value;
736 return trim( stripslashes( $value ) );
737 }
738
739 /**
740 * Validate Textarea Field.
741 *
742 * @param string $key Field key.
743 * @param string $value Posted Value.
744 * @return string
745 */
746 public function validate_textarea_field( $key, $value ) {
747 $value = is_null( $value ) ? '' : $value;
748 return wp_kses(
749 trim( stripslashes( $value ) ),
750 array_merge(
751 array(
752 'iframe' => array(
753 'src' => true,
754 'style' => true,
755 'id' => true,
756 'class' => true,
757 ),
758 ),
759 wp_kses_allowed_html( 'post' )
760 )
761 );
762 }
763
764 /**
765 * Validate Checkbox Field.
766 *
767 * If not set, return "no", otherwise return "yes".
768 *
769 * @param string $key Field key.
770 * @param string $value Posted Value.
771 * @return string
772 */
773 public function validate_checkbox_field( $key, $value ) {
774 return ! is_null( $value ) ? 'yes' : 'no';
775 }
776
777 /**
778 * Validate Select Field.
779 *
780 * @param string $key Field key.
781 * @param string $value Posted Value.
782 * @return string
783 */
784 public function validate_select_field( $key, $value ) {
785 $value = is_null( $value ) ? '' : $value;
786 return evf_clean( stripslashes( $value ) );
787 }
788
789 /**
790 * Validate Multiselect Field.
791 *
792 * @param string $key Field key.
793 * @param string $value Posted Value.
794 * @return string|array
795 */
796 public function validate_multiselect_field( $key, $value ) {
797 return is_array( $value ) ? array_map( 'evf_clean', array_map( 'stripslashes', $value ) ) : '';
798 }
799 }
800