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-form-fields.php
2721 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Abstract EVF_Form_Fields Class |
| 4 | * |
| 5 | * @version 1.0.0 |
| 6 | * @package EverestFroms/Abstracts |
| 7 | */ |
| 8 | |
| 9 | defined( 'ABSPATH' ) || exit; |
| 10 | |
| 11 | /** |
| 12 | * Form fields class. |
| 13 | */ |
| 14 | abstract class EVF_Form_Fields { |
| 15 | |
| 16 | /** |
| 17 | * Field name. |
| 18 | * |
| 19 | * @var string |
| 20 | */ |
| 21 | public $name; |
| 22 | |
| 23 | /** |
| 24 | * Field type. |
| 25 | * |
| 26 | * @var string |
| 27 | */ |
| 28 | public $type; |
| 29 | |
| 30 | /** |
| 31 | * Field icon. |
| 32 | * |
| 33 | * @var mixed |
| 34 | */ |
| 35 | public $icon = ''; |
| 36 | |
| 37 | /** |
| 38 | * Field class. |
| 39 | * |
| 40 | * @var string |
| 41 | */ |
| 42 | public $class = ''; |
| 43 | |
| 44 | /** |
| 45 | * Form ID. |
| 46 | * |
| 47 | * @var int|mixed |
| 48 | */ |
| 49 | public $form_id; |
| 50 | |
| 51 | /** |
| 52 | * Field group. |
| 53 | * |
| 54 | * @var string |
| 55 | */ |
| 56 | public $group = 'general'; |
| 57 | |
| 58 | /** |
| 59 | * Is available in Pro? |
| 60 | * |
| 61 | * @var boolean |
| 62 | */ |
| 63 | public $is_pro = false; |
| 64 | |
| 65 | /** |
| 66 | * Placeholder to hold default value(s) for some field types. |
| 67 | * |
| 68 | * @var mixed |
| 69 | */ |
| 70 | public $defaults; |
| 71 | |
| 72 | /** |
| 73 | * Array of form data. |
| 74 | * |
| 75 | * @var array |
| 76 | */ |
| 77 | public $form_data; |
| 78 | |
| 79 | /** |
| 80 | * Array of field settings. |
| 81 | * |
| 82 | * @var array |
| 83 | */ |
| 84 | protected $settings = array(); |
| 85 | |
| 86 | /** |
| 87 | * Constructor. |
| 88 | */ |
| 89 | public function __construct() { |
| 90 | $get_license = evf_get_license_plan(); |
| 91 | $this->class = $this->is_pro ? ( false === $get_license ? 'upgrade-modal' : 'evf-upgrade-addon' ) : $this->class; |
| 92 | $this->form_id = isset( $_GET['form_id'] ) ? absint( $_GET['form_id'] ) : false; // phpcs:ignore WordPress.Security.NonceVerification |
| 93 | |
| 94 | // Init hooks. |
| 95 | $this->init_hooks(); |
| 96 | |
| 97 | // Hooks. |
| 98 | add_action( 'everest_forms_builder_fields_options_' . $this->type, array( $this, 'field_options' ) ); |
| 99 | add_action( 'everest_forms_builder_fields_preview_' . $this->type, array( $this, 'field_preview' ) ); |
| 100 | add_action( 'wp_ajax_everest_forms_new_field_' . $this->type, array( $this, 'field_new' ) ); |
| 101 | add_action( 'everest_forms_display_field_' . $this->type, array( $this, 'field_display' ), 10, 3 ); |
| 102 | add_action( 'everest_forms_display_edit_form_field_' . $this->type, array( $this, 'edit_form_field_display' ), 10, 3 ); |
| 103 | add_action( 'everest_forms_process_validate_' . $this->type, array( $this, 'validate' ), 10, 3 ); |
| 104 | add_action( 'everest_forms_process_format_' . $this->type, array( $this, 'format' ), 10, 4 ); |
| 105 | add_filter( 'everest_forms_field_properties', array( $this, 'field_prefill_value_property' ), 10, 3 ); |
| 106 | add_filter( 'everest_forms_field_exporter_' . $this->type, array( $this, 'field_exporter' ) ); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Hook in tabs. |
| 111 | */ |
| 112 | public function init_hooks() {} |
| 113 | |
| 114 | /** |
| 115 | * Prefill field value with either fallback or dynamic data. |
| 116 | * Needs to be public (although internal) to be used in WordPress hooks. |
| 117 | * |
| 118 | * @since 1.6.5 |
| 119 | * |
| 120 | * @param array $properties Field properties. |
| 121 | * @param array $field Current field specific data. |
| 122 | * @param array $form_data Prepared form data/settings. |
| 123 | * |
| 124 | * @return array Modified field properties. |
| 125 | */ |
| 126 | public function field_prefill_value_property( $properties, $field, $form_data ) { |
| 127 | // Process only for current field. |
| 128 | if ( $this->type !== $field['type'] ) { |
| 129 | return $properties; |
| 130 | } |
| 131 | |
| 132 | // Set the form data, so we can reuse it later, even on front-end. |
| 133 | $this->form_data = $form_data; |
| 134 | |
| 135 | return $properties; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Get the form fields after they are initialized. |
| 140 | * |
| 141 | * @return array of options |
| 142 | */ |
| 143 | public function get_field_settings() { |
| 144 | return apply_filters( 'everest_forms_get_field_settings_' . $this->type, $this->settings ); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Output form fields options. |
| 149 | * |
| 150 | * Loops though the field options array and outputs each field. |
| 151 | * |
| 152 | * @param array $field Field data. |
| 153 | */ |
| 154 | public function field_options( $field ) { |
| 155 | $settings = apply_filters( 'everest_forms_builder_fields_option', $this->get_field_settings() ); |
| 156 | |
| 157 | foreach ( $settings as $option_key => $option ) { |
| 158 | $this->field_option( |
| 159 | $option_key, |
| 160 | $field, |
| 161 | array( |
| 162 | 'markup' => 'open', |
| 163 | ) |
| 164 | ); |
| 165 | |
| 166 | if ( ! empty( $option['field_options'] ) ) { |
| 167 | foreach ( $option['field_options'] as $option_name ) { |
| 168 | $this->field_option( $option_name, $field ); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | $this->field_option( |
| 173 | $option_key, |
| 174 | $field, |
| 175 | array( |
| 176 | 'markup' => 'close', |
| 177 | ) |
| 178 | ); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Field preview inside the builder. |
| 184 | * |
| 185 | * @since 1.0.0 |
| 186 | * |
| 187 | * @param array $field Field data and settings. |
| 188 | */ |
| 189 | public function field_preview( $field ) {} |
| 190 | |
| 191 | /** |
| 192 | * Helper function to create field option elements. |
| 193 | * |
| 194 | * Field option elements are pieces that help create a field option. |
| 195 | * They are used to quickly build field options. |
| 196 | * |
| 197 | * @since 1.0.0 |
| 198 | * |
| 199 | * @param string $option Field option to render. |
| 200 | * @param array $field Field data and settings. |
| 201 | * @param array $args Field preview arguments. |
| 202 | * @param boolean $echo Print or return the value. Print by default. |
| 203 | * |
| 204 | * @return mixed echo or return string |
| 205 | */ |
| 206 | public function field_element( $option, $field, $args = array(), $echo = true ) { |
| 207 | $id = (string) $field['id']; |
| 208 | $class = ! empty( $args['class'] ) && is_string( $args['class'] ) ? esc_attr( $args['class'] ) : ''; |
| 209 | $slug = ! empty( $args['slug'] ) ? sanitize_title( $args['slug'] ) : ''; |
| 210 | $data = ''; |
| 211 | $output = ''; |
| 212 | |
| 213 | if ( ! empty( $args['data'] ) ) { |
| 214 | foreach ( $args['data'] as $key => $val ) { |
| 215 | if ( is_array( $val ) ) { |
| 216 | $val = wp_json_encode( $val ); |
| 217 | } |
| 218 | $data .= ' data-' . $key . '=\'' . $val . '\''; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | // BW compat for number attrs. |
| 223 | if ( ! empty( $args['min'] ) ) { |
| 224 | $args['attrs']['min'] = esc_attr( $args['min'] ); |
| 225 | unset( $args['min'] ); |
| 226 | } |
| 227 | if ( ! empty( $args['max'] ) ) { |
| 228 | $args['attrs']['max'] = esc_attr( $args['max'] ); |
| 229 | unset( $args['min'] ); |
| 230 | } |
| 231 | if ( ! empty( $args['required'] ) && $args['required'] ) { |
| 232 | $args['attrs']['required'] = 'required'; |
| 233 | unset( $args['required'] ); |
| 234 | } |
| 235 | |
| 236 | if ( ! empty( $args['attrs'] ) ) { |
| 237 | foreach ( $args['attrs'] as $arg_key => $val ) { |
| 238 | if ( is_array( $val ) ) { |
| 239 | $val = wp_json_encode( $val ); |
| 240 | } |
| 241 | $data .= $arg_key . '=\'' . $val . '\''; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | switch ( $option ) { |
| 246 | |
| 247 | // Row. |
| 248 | case 'row': |
| 249 | $output = sprintf( '<div class="everest-forms-field-option-row everest-forms-field-option-row-%s %s" id="everest-forms-field-option-row-%s-%s" data-field-id="%s" %s>%s</div>', $slug, $class, $id, $slug, $id, $data, $args['content'] ); |
| 250 | break; |
| 251 | |
| 252 | // Icon. |
| 253 | case 'icon': |
| 254 | $element_tooltip = isset( $args['tooltip'] ) ? $args['tooltip'] : 'Edit Label'; |
| 255 | $icon = isset( $args['icon'] ) ? $args['icon'] : 'dashicons-edit'; |
| 256 | $output .= sprintf( ' <i class="dashicons %s everest-forms-icon %s" title="%s" %s></i>', esc_attr( $icon ), $class, esc_attr( $element_tooltip ), $data ); |
| 257 | break; |
| 258 | |
| 259 | // Label. |
| 260 | case 'label': |
| 261 | $output = sprintf( '<label for="everest-forms-field-option-%s-%s" class="%s" %s>%s', $id, $slug, $class, $data, esc_html( $args['value'] ) ); |
| 262 | if ( isset( $args['tooltip'] ) && ! empty( $args['tooltip'] ) ) { |
| 263 | $output .= ' ' . sprintf( '<i class="dashicons dashicons-editor-help everest-forms-help-tooltip" title="%s"></i>', esc_attr( $args['tooltip'] ) ); |
| 264 | } |
| 265 | if ( isset( $args['after_tooltip'] ) && ! empty( $args['after_tooltip'] ) ) { |
| 266 | $output .= $args['after_tooltip']; |
| 267 | } |
| 268 | $output .= '</label>'; |
| 269 | break; |
| 270 | |
| 271 | // Text input. |
| 272 | case 'text': |
| 273 | $type = ! empty( $args['type'] ) ? esc_attr( $args['type'] ) : 'text'; |
| 274 | $placeholder = ! empty( $args['placeholder'] ) ? esc_attr( $args['placeholder'] ) : ''; |
| 275 | $before = ! empty( $args['before'] ) ? '<span class="before-input">' . esc_html( $args['before'] ) . '</span>' : ''; |
| 276 | if ( ! empty( $before ) ) { |
| 277 | $class .= ' has-before'; |
| 278 | } |
| 279 | |
| 280 | $output = sprintf( '%s<input type="%s" class="widefat %s" id="everest-forms-field-option-%s-%s" name="form_fields[%s][%s]" value="%s" placeholder="%s" %s>', $before, $type, $class, $id, $slug, $id, $slug, esc_attr( $args['value'] ), $placeholder, $data ); |
| 281 | break; |
| 282 | |
| 283 | // Textarea. |
| 284 | case 'textarea': |
| 285 | $rows = ! empty( $args['rows'] ) ? (int) $args['rows'] : '3'; |
| 286 | $output = sprintf( '<textarea class="widefat %s" id="everest-forms-field-option-%s-%s" name="form_fields[%s][%s]" rows="%s" %s>%s</textarea>', $class, $id, $slug, $id, $slug, $rows, $data, $args['value'] ); |
| 287 | break; |
| 288 | |
| 289 | // Checkbox. |
| 290 | case 'checkbox': |
| 291 | $checked = checked( '1', $args['value'], false ); |
| 292 | $output = sprintf( '<input type="checkbox" class="widefat %s" id="everest-forms-field-option-%s-%s" name="form_fields[%s][%s]" value="1" %s %s>', $class, $id, $slug, $id, $slug, $checked, $data ); |
| 293 | $output .= sprintf( '<label for="everest-forms-field-option-%s-%s" class="inline">%s', $id, $slug, $args['desc'] ); |
| 294 | if ( isset( $args['tooltip'] ) && ! empty( $args['tooltip'] ) ) { |
| 295 | $output .= ' ' . sprintf( '<i class="dashicons dashicons-editor-help everest-forms-help-tooltip" title="%s"></i>', esc_attr( $args['tooltip'] ) ); |
| 296 | } |
| 297 | $output .= '</label>'; |
| 298 | break; |
| 299 | |
| 300 | // Toggle. |
| 301 | case 'toggle': |
| 302 | $checked = checked( '1', $args['value'], false ); |
| 303 | $icon = $args['value'] ? 'fa-toggle-on' : 'fa-toggle-off'; |
| 304 | $cls = $args['value'] ? 'everest-forms-on' : 'everest-forms-off'; |
| 305 | $status = $args['value'] ? __( 'On', 'everest-forms' ) : __( 'Off', 'everest-forms' ); |
| 306 | $output = sprintf( '<span class="everest-forms-toggle-icon %s"><i class="fa %s" aria-hidden="true"></i> <span class="everest-forms-toggle-icon-label">%s</span>', $cls, $icon, $status ); |
| 307 | $output .= sprintf( '<input type="checkbox" class="widefat %s" id="everest-forms-field-option-%s-%s" name="form_fields[%s][%s]" value="1" %s %s></span>', $class, $id, $slug, $id, $slug, $checked, $data ); |
| 308 | break; |
| 309 | |
| 310 | // Select. |
| 311 | case 'select': |
| 312 | $options = $args['options']; |
| 313 | $value = isset( $args['value'] ) ? $args['value'] : ''; |
| 314 | $is_multiple = isset( $args['multiple'] ) && true === $args['multiple']; |
| 315 | |
| 316 | if ( true === $is_multiple ) { |
| 317 | $output = sprintf( '<select class="widefat %s" id="everest-forms-field-option-%s-%s" name="form_fields[%s][%s][]" %s multiple>', $class, $id, $slug, $id, $slug, $data ); |
| 318 | } else { |
| 319 | $output = sprintf( '<select class="widefat %s" id="everest-forms-field-option-%s-%s" name="form_fields[%s][%s]" %s >', $class, $id, $slug, $id, $slug, $data ); |
| 320 | } |
| 321 | |
| 322 | foreach ( $options as $key => $option_value ) { |
| 323 | |
| 324 | if ( true === $is_multiple && is_array( $value ) ) { |
| 325 | $selected_value = in_array( $key, $value, true ) ? 'selected="selected"' : ''; |
| 326 | } else { |
| 327 | $selected_value = ( $value == $key ) ? 'selected="selected"' : ''; |
| 328 | } |
| 329 | $output .= sprintf( '<option value="%s" %s>%s</option>', esc_attr( $key ), $selected_value, $option_value ); |
| 330 | } |
| 331 | $output .= '</select>'; |
| 332 | break; |
| 333 | |
| 334 | // Radio. |
| 335 | case 'radio': |
| 336 | $options = $args['options']; |
| 337 | $default = isset( $args['default'] ) ? $args['default'] : ''; |
| 338 | $output = '<label>' . $args['desc']; |
| 339 | |
| 340 | if ( isset( $args['tooltip'] ) && ! empty( $args['tooltip'] ) ) { |
| 341 | $output .= ' ' . sprintf( '<i class="dashicons dashicons-editor-help everest-forms-help-tooltip" title="%s"></i></label>', esc_attr( $args['tooltip'] ) ); |
| 342 | } else { |
| 343 | $output .= '</label>'; |
| 344 | } |
| 345 | $output .= '<ul>'; |
| 346 | |
| 347 | foreach ( $options as $key => $option ) { |
| 348 | $output .= '<li>'; |
| 349 | $output .= sprintf( '<label><input type="radio" class="widefat %s" id="everest-forms-field-option-%s-%s-%s" value="%s" name="form_fields[%s][%s]" %s %s>%s</label>', $class, $id, $slug, $key, $key, $id, $slug, $data, checked( $key, $default, false ), $option ); |
| 350 | $output .= '</li>'; |
| 351 | } |
| 352 | $output .= '</ul>'; |
| 353 | break; |
| 354 | } |
| 355 | |
| 356 | if ( $echo ) { |
| 357 | echo wp_kses( $output, evf_get_allowed_html_tags( 'builder' ) ); |
| 358 | } else { |
| 359 | return $output; |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | /** |
| 364 | * Helper function to create common field options that are used frequently. |
| 365 | * |
| 366 | * @since 1.0.0 |
| 367 | * |
| 368 | * @param string $option Option. |
| 369 | * @param array $field Field data. |
| 370 | * @param array $args Arguments. |
| 371 | * @param boolean $echo True to echo. |
| 372 | * |
| 373 | * @return mixed echo or return string |
| 374 | */ |
| 375 | public function field_option( $option, $field, $args = array(), $echo = true ) { |
| 376 | $output = ''; |
| 377 | $markup = ! empty( $args['markup'] ) ? $args['markup'] : 'open'; |
| 378 | $class = ! empty( $args['class'] ) ? esc_html( $args['class'] ) : ''; |
| 379 | |
| 380 | if ( $echo && in_array( $option, array( 'basic-options', 'advanced-options' ), true ) && 'open' === $markup ) { |
| 381 | do_action( "everest_forms_field_options_before_{$option}", $field, $this ); |
| 382 | } elseif ( $echo && in_array( $option, array( 'basic-options', 'advanced-options' ), true ) && 'close' === $markup ) { |
| 383 | do_action( "everest_forms_field_options_bottom_{$option}", $field, $this ); |
| 384 | } |
| 385 | |
| 386 | switch ( $option ) { |
| 387 | /** |
| 388 | * Basic Fields. |
| 389 | */ |
| 390 | |
| 391 | /* |
| 392 | * Basic Options markup. |
| 393 | */ |
| 394 | case 'basic-options': |
| 395 | if ( 'open' === $markup ) { |
| 396 | if ( $echo ) { |
| 397 | echo sprintf( '<div class="everest-forms-field-option-group everest-forms-field-option-group-basic open" id="everest-forms-field-option-basic-%s">', esc_attr( $field['id'] ) ); |
| 398 | echo sprintf( '<a href="#" class="everest-forms-field-option-group-toggle">%s<span> (ID #%s)</span> <i class="handlediv"></i></a>', esc_html( $this->name ), esc_html( $field['id'] ) ); |
| 399 | echo sprintf( '<div class="everest-forms-field-option-group-inner %s">', esc_attr( $class ) ); |
| 400 | } else { |
| 401 | $output = sprintf( '<div class="everest-forms-field-option-group everest-forms-field-option-group-basic open" id="everest-forms-field-option-basic-%s">', $field['id'] ); |
| 402 | $output .= sprintf( '<a href="#" class="everest-forms-field-option-group-toggle">%s<span> (ID #%s)</span> <i class="handlediv"></i></a>', $this->name, $field['id'] ); |
| 403 | $output .= sprintf( '<div class="everest-forms-field-option-group-inner %s">', $class ); |
| 404 | } |
| 405 | } else { |
| 406 | if ( $echo ) { |
| 407 | echo '</div></div>'; |
| 408 | } else { |
| 409 | $output = '</div></div>'; |
| 410 | } |
| 411 | } |
| 412 | break; |
| 413 | |
| 414 | /* |
| 415 | * Field Label. |
| 416 | */ |
| 417 | case 'label': |
| 418 | $value = ! empty( $field['label'] ) ? esc_attr( $field['label'] ) : ''; |
| 419 | $tooltip = esc_html__( 'Enter text for the form field label. This is recommended and can be hidden in the Advanced Settings.', 'everest-forms' ); |
| 420 | $output = $this->field_element( |
| 421 | 'label', |
| 422 | $field, |
| 423 | array( |
| 424 | 'slug' => 'label', |
| 425 | 'value' => esc_html__( 'Label', 'everest-forms' ), |
| 426 | 'tooltip' => $tooltip, |
| 427 | ), |
| 428 | false |
| 429 | ); |
| 430 | $output .= $this->field_element( |
| 431 | 'text', |
| 432 | $field, |
| 433 | array( |
| 434 | 'slug' => 'label', |
| 435 | 'value' => $value, |
| 436 | ), |
| 437 | false |
| 438 | ); |
| 439 | $output = $this->field_element( |
| 440 | 'row', |
| 441 | $field, |
| 442 | array( |
| 443 | 'slug' => 'label', |
| 444 | 'content' => $output, |
| 445 | ), |
| 446 | $echo |
| 447 | ); |
| 448 | break; |
| 449 | |
| 450 | /* |
| 451 | * Field Meta. |
| 452 | */ |
| 453 | case 'meta': |
| 454 | $value = ! empty( $field['meta-key'] ) ? esc_attr( $field['meta-key'] ) : evf_get_meta_key_field_option( $field ); |
| 455 | $tooltip = esc_html__( 'Enter meta key to be stored in database.', 'everest-forms' ); |
| 456 | $output = $this->field_element( |
| 457 | 'label', |
| 458 | $field, |
| 459 | array( |
| 460 | 'slug' => 'meta-key', |
| 461 | 'value' => esc_html__( 'Meta Key', 'everest-forms' ), |
| 462 | 'tooltip' => $tooltip, |
| 463 | ), |
| 464 | false |
| 465 | ); |
| 466 | $output .= $this->field_element( |
| 467 | 'text', |
| 468 | $field, |
| 469 | array( |
| 470 | 'slug' => 'meta-key', |
| 471 | 'class' => 'evf-input-meta-key', |
| 472 | 'value' => $value, |
| 473 | ), |
| 474 | false |
| 475 | ); |
| 476 | $output = $this->field_element( |
| 477 | 'row', |
| 478 | $field, |
| 479 | array( |
| 480 | 'slug' => 'meta-key', |
| 481 | 'content' => $output, |
| 482 | ), |
| 483 | $echo |
| 484 | ); |
| 485 | break; |
| 486 | |
| 487 | /* |
| 488 | * Field Description. |
| 489 | */ |
| 490 | case 'description': |
| 491 | $value = ! empty( $field['description'] ) ? esc_attr( $field['description'] ) : ''; |
| 492 | $tooltip = esc_html__( 'Enter text for the form field description.', 'everest-forms' ); |
| 493 | $output = $this->field_element( |
| 494 | 'label', |
| 495 | $field, |
| 496 | array( |
| 497 | 'slug' => 'description', |
| 498 | 'value' => esc_html__( 'Description', 'everest-forms' ), |
| 499 | 'tooltip' => $tooltip, |
| 500 | ), |
| 501 | false |
| 502 | ); |
| 503 | $output .= $this->field_element( |
| 504 | 'textarea', |
| 505 | $field, |
| 506 | array( |
| 507 | 'slug' => 'description', |
| 508 | 'value' => $value, |
| 509 | ), |
| 510 | false |
| 511 | ); |
| 512 | $output = $this->field_element( |
| 513 | 'row', |
| 514 | $field, |
| 515 | array( |
| 516 | 'slug' => 'description', |
| 517 | 'content' => $output, |
| 518 | ), |
| 519 | $echo |
| 520 | ); |
| 521 | break; |
| 522 | |
| 523 | /* |
| 524 | * Field Required toggle. |
| 525 | */ |
| 526 | case 'required': |
| 527 | $default = ! empty( $args['default'] ) ? $args['default'] : '0'; |
| 528 | $value = isset( $field['required'] ) ? $field['required'] : $default; |
| 529 | $tooltip = esc_html__( 'Check this option to mark the field required. A form will not submit unless all required fields are provided.', 'everest-forms' ); |
| 530 | $output = $this->field_element( |
| 531 | 'checkbox', |
| 532 | $field, |
| 533 | array( |
| 534 | 'slug' => 'required', |
| 535 | 'value' => $value, |
| 536 | 'desc' => esc_html__( 'Required', 'everest-forms' ), |
| 537 | 'tooltip' => $tooltip, |
| 538 | ), |
| 539 | false |
| 540 | ); |
| 541 | $output = $this->field_element( |
| 542 | 'row', |
| 543 | $field, |
| 544 | array( |
| 545 | 'slug' => 'required', |
| 546 | 'content' => $output, |
| 547 | ), |
| 548 | $echo |
| 549 | ); |
| 550 | break; |
| 551 | |
| 552 | /* |
| 553 | * Field Required toggle. |
| 554 | */ |
| 555 | case 'required_field_message_setting': |
| 556 | $default = ! empty( $args['default'] ) ? $args['default'] : 'global'; |
| 557 | if ( isset( $field['required_field_message_setting'] ) ) { |
| 558 | if ( 'global' === $field['required_field_message_setting'] ) { |
| 559 | $value = $field['required_field_message_setting']; |
| 560 | } elseif ( 'individual' === $field['required_field_message_setting'] ) { |
| 561 | $value = $field['required_field_message_setting']; |
| 562 | } |
| 563 | } elseif ( ! empty( $field['required-field-message'] ) ) { |
| 564 | $value = 'individual'; |
| 565 | } else { |
| 566 | $value = $default; |
| 567 | } |
| 568 | $output = $this->field_element( |
| 569 | 'radio', |
| 570 | $field, |
| 571 | array( |
| 572 | 'slug' => 'required_field_message_setting', |
| 573 | 'default' => $value, |
| 574 | 'desc' => '', |
| 575 | 'options' => array( |
| 576 | 'global' => esc_html__( 'Show Required Message From Global Setting', 'everest-forms' ), |
| 577 | 'individual' => esc_html__( 'Custom Required Message', 'everest-forms' ), |
| 578 | ), |
| 579 | ), |
| 580 | false |
| 581 | ); |
| 582 | $output = $this->field_element( |
| 583 | 'row', |
| 584 | $field, |
| 585 | array( |
| 586 | 'slug' => 'required_field_message_setting', |
| 587 | 'class' => isset( $field['required'] ) ? '' : 'hidden', |
| 588 | 'content' => $output, |
| 589 | ), |
| 590 | $echo |
| 591 | ); |
| 592 | break; |
| 593 | /** |
| 594 | * Required Field Message. |
| 595 | */ |
| 596 | case 'required_field_message': |
| 597 | $has_sub_fields = false; |
| 598 | $sub_fields = array(); |
| 599 | $required_validation = get_option( 'everest_forms_required_validation' ); |
| 600 | if ( in_array( $field['type'], array( 'number', 'email', 'url', 'phone' ), true ) ) { |
| 601 | $required_validation = get_option( 'everest_forms_' . $field['type'] . '_validation' ); |
| 602 | } |
| 603 | $hidden = true; |
| 604 | if ( isset( $field['required_field_message_setting'], $field['required'] ) ) { |
| 605 | if ( 'global' === $field['required_field_message_setting'] ) { |
| 606 | $hidden = true; |
| 607 | } elseif ( 'individual' === $field['required_field_message_setting'] ) { |
| 608 | $hidden = false; |
| 609 | } |
| 610 | } elseif ( ! empty( $field['required-field-message'] ) && isset( $field['required'] ) ) { |
| 611 | $hidden = false; |
| 612 | } |
| 613 | |
| 614 | if ( 'likert' === $field['type'] ) { |
| 615 | $has_sub_fields = true; |
| 616 | $likert_rows = isset( $field['likert_rows'] ) ? $field['likert_rows'] : array(); |
| 617 | foreach ( $likert_rows as $row_number => $row_label ) { |
| 618 | $row_slug = 'required-field-message-' . $row_number; |
| 619 | $sub_fields[ $row_slug ] = array( |
| 620 | 'label' => array( |
| 621 | 'value' => $row_label, |
| 622 | 'tooltip' => esc_html__( 'Enter a message to show for this row if it\'s required.', 'everest-forms' ), |
| 623 | ), |
| 624 | 'text' => array( |
| 625 | 'value' => isset( $field[ $row_slug ] ) ? esc_attr( $field[ $row_slug ] ) : '', |
| 626 | ), |
| 627 | ); |
| 628 | } |
| 629 | } elseif ( 'address' === $field['type'] ) { |
| 630 | $has_sub_fields = true; |
| 631 | $sub_fields = array( |
| 632 | 'required-field-message-address1' => array( |
| 633 | 'label' => array( |
| 634 | 'value' => esc_html__( 'Address Line 1', 'everest-forms' ), |
| 635 | 'tooltip' => esc_html__( 'Enter a message to show for Address Line 1 if it\'s required.', 'everest-forms' ), |
| 636 | ), |
| 637 | 'text' => array( |
| 638 | 'value' => isset( $field['required-field-message-address1'] ) ? esc_attr( $field['required-field-message-address1'] ) : '', |
| 639 | ), |
| 640 | ), |
| 641 | 'required-field-message-city' => array( |
| 642 | 'label' => array( |
| 643 | 'value' => esc_html__( 'City', 'everest-forms' ), |
| 644 | 'tooltip' => esc_html__( 'Enter a message to show for City if it\'s required.', 'everest-forms' ), |
| 645 | ), |
| 646 | 'text' => array( |
| 647 | 'value' => isset( $field['required-field-message-city'] ) ? esc_attr( $field['required-field-message-city'] ) : '', |
| 648 | ), |
| 649 | ), |
| 650 | 'required-field-message-state' => array( |
| 651 | 'label' => array( |
| 652 | 'value' => esc_html__( 'State / Province / Region', 'everest-forms' ), |
| 653 | 'tooltip' => esc_html__( 'Enter a message to show for State/Province/Region if it\'s required.', 'everest-forms' ), |
| 654 | ), |
| 655 | 'text' => array( |
| 656 | 'value' => isset( $field['required-field-message-state'] ) ? esc_attr( $field['required-field-message-state'] ) : '', |
| 657 | ), |
| 658 | ), |
| 659 | 'required-field-message-postal' => array( |
| 660 | 'label' => array( |
| 661 | 'value' => esc_html__( 'Zip / Postal Code', 'everest-forms' ), |
| 662 | 'tooltip' => esc_html__( 'Enter a message to show for Zip/Postal Code if it\'s required.', 'everest-forms' ), |
| 663 | ), |
| 664 | 'text' => array( |
| 665 | 'value' => isset( $field['required-field-message-postal'] ) ? esc_attr( $field['required-field-message-postal'] ) : '', |
| 666 | ), |
| 667 | ), |
| 668 | 'required-field-message-country' => array( |
| 669 | 'label' => array( |
| 670 | 'value' => esc_html__( 'Country', 'everest-forms' ), |
| 671 | 'tooltip' => esc_html__( 'Enter a message to show for Country if it\'s required.', 'everest-forms' ), |
| 672 | ), |
| 673 | 'text' => array( |
| 674 | 'value' => isset( $field['required-field-message-country'] ) ? esc_attr( $field['required-field-message-country'] ) : '', |
| 675 | ), |
| 676 | ), |
| 677 | ); |
| 678 | } |
| 679 | |
| 680 | if ( true === $has_sub_fields ) { |
| 681 | $sub_field_output_array = array(); |
| 682 | foreach ( $sub_fields as $sub_field_slug => $sub_field_data ) { |
| 683 | $value = isset( $field['required-field-message'] ) ? esc_attr( $field['required-field-message'] ) : ''; |
| 684 | $tooltip = esc_html__( 'Enter a message to show for this field if it\'s required.', 'everest-forms' ); |
| 685 | $output = $this->field_element( |
| 686 | 'label', |
| 687 | $field, |
| 688 | array( |
| 689 | 'slug' => $sub_field_slug, |
| 690 | 'value' => $sub_field_data['label']['value'], |
| 691 | 'tooltip' => $sub_field_data['label']['tooltip'], |
| 692 | ), |
| 693 | false |
| 694 | ); |
| 695 | $output .= $this->field_element( |
| 696 | 'text', |
| 697 | $field, |
| 698 | array( |
| 699 | 'slug' => $sub_field_slug, |
| 700 | 'value' => $sub_field_data['text']['value'], |
| 701 | ), |
| 702 | false |
| 703 | ); |
| 704 | $output = $this->field_element( |
| 705 | 'row', |
| 706 | $field, |
| 707 | array( |
| 708 | 'slug' => $sub_field_slug, |
| 709 | 'content' => $output, |
| 710 | ), |
| 711 | false |
| 712 | ); |
| 713 | |
| 714 | $sub_field_output_array[] = $output; |
| 715 | } |
| 716 | |
| 717 | $output = implode( '', $sub_field_output_array ); |
| 718 | $output = $this->field_element( |
| 719 | 'row', |
| 720 | $field, |
| 721 | array( |
| 722 | 'slug' => 'required-field-message', |
| 723 | 'class' => false === $hidden ? '' : 'hidden', |
| 724 | 'content' => $output, |
| 725 | ), |
| 726 | $echo |
| 727 | ); |
| 728 | } else { |
| 729 | $value = isset( $field['required-field-message'] ) ? esc_attr( $field['required-field-message'] ) : ''; |
| 730 | $tooltip = esc_html__( 'Enter a message to show for this field if it\'s required.', 'everest-forms' ); |
| 731 | |
| 732 | $output = $this->field_element( |
| 733 | 'label', |
| 734 | $field, |
| 735 | array( |
| 736 | 'slug' => 'required-field-message', |
| 737 | 'value' => esc_html__( 'Required Field Message', 'everest-forms' ), |
| 738 | 'tooltip' => $tooltip, |
| 739 | ), |
| 740 | false |
| 741 | ); |
| 742 | $output .= $this->field_element( |
| 743 | 'text', |
| 744 | $field, |
| 745 | array( |
| 746 | 'slug' => 'required-field-message', |
| 747 | 'value' => $value, |
| 748 | 'class' => 'everest-forms-field-option-row', |
| 749 | ), |
| 750 | false |
| 751 | ); |
| 752 | $output = $this->field_element( |
| 753 | 'row', |
| 754 | $field, |
| 755 | array( |
| 756 | 'slug' => 'required-field-message', |
| 757 | 'class' => false === $hidden ? '' : 'hidden', |
| 758 | 'content' => $output, |
| 759 | ), |
| 760 | $echo |
| 761 | ); |
| 762 | } |
| 763 | break; |
| 764 | |
| 765 | /** |
| 766 | * Field Visibilty. |
| 767 | */ |
| 768 | case 'field_visiblity': |
| 769 | $default = ! empty( $args['default'] ) ? $args['default'] : '0'; |
| 770 | $readonly_value = isset( $field['readonly_field_visibility'] ) ? $field['readonly_field_visibility'] : $default; |
| 771 | $hidden_value = isset( $field['hidden_field_visibility'] ) ? $field['hidden_field_visibility'] : $default; |
| 772 | $tooltip = esc_html__( 'Check this option to mark the field readonly and hidden.', 'everest-forms' ); |
| 773 | $label = $this->field_element( |
| 774 | 'label', |
| 775 | $field, |
| 776 | array( |
| 777 | 'slug' => 'field_visibility', |
| 778 | 'value' => esc_html__( 'Field Visibility', 'everest-forms' ), |
| 779 | 'tooltip' => $tooltip, |
| 780 | ), |
| 781 | false |
| 782 | ); |
| 783 | $readonly = $this->field_element( |
| 784 | 'checkbox', |
| 785 | $field, |
| 786 | array( |
| 787 | 'slug' => 'readonly_field_visibility', |
| 788 | 'value' => $readonly_value, |
| 789 | 'class' => 'field_visibility_readonly', |
| 790 | 'desc' => esc_html__( 'Readonly ', 'everest-forms' ), |
| 791 | ), |
| 792 | false |
| 793 | ); |
| 794 | $hidden = $this->field_element( |
| 795 | 'checkbox', |
| 796 | $field, |
| 797 | array( |
| 798 | 'slug' => 'hidden_field_visibility', |
| 799 | 'value' => $hidden_value, |
| 800 | 'class' => 'field_visibility_hidden', |
| 801 | 'desc' => esc_html__( 'Hidden', 'everest-forms' ), |
| 802 | ), |
| 803 | false |
| 804 | ); |
| 805 | $output = $this->field_element( |
| 806 | 'row', |
| 807 | $field, |
| 808 | array( |
| 809 | 'slug' => 'field_visiblity', |
| 810 | 'content' => $label . '' . $readonly . ' ' . $hidden, |
| 811 | ), |
| 812 | $echo |
| 813 | ); |
| 814 | break; |
| 815 | |
| 816 | /** |
| 817 | * No Duplicates. |
| 818 | */ |
| 819 | case 'no_duplicates': |
| 820 | $default = ! empty( $args['default'] ) ? $args['default'] : '0'; |
| 821 | $value = ! empty( $field['no_duplicates'] ) ? esc_attr( $field['no_duplicates'] ) : ''; |
| 822 | $tooltip = esc_html__( 'Select this option to limit user input to unique values only. This will require that a value entered in a field does not currently exist in the entry database for that field..', 'everest-forms' ); |
| 823 | $output = $this->field_element( |
| 824 | 'checkbox', |
| 825 | $field, |
| 826 | array( |
| 827 | 'slug' => 'no_duplicates', |
| 828 | 'value' => $value, |
| 829 | 'desc' => esc_html__( 'Validate as unique', 'everest-forms' ), |
| 830 | 'tooltip' => $tooltip, |
| 831 | ), |
| 832 | false |
| 833 | ); |
| 834 | $output = $this->field_element( |
| 835 | 'row', |
| 836 | $field, |
| 837 | array( |
| 838 | 'slug' => 'no_duplicates', |
| 839 | 'content' => $output, |
| 840 | ), |
| 841 | $echo |
| 842 | ); |
| 843 | break; |
| 844 | case 'validate_message': |
| 845 | $toggle = ''; |
| 846 | $tooltip = esc_html__( 'if the form submission failed it will show this message.', 'everest-forms' ); |
| 847 | $value = ! empty( $field['validate_message'] ) ? esc_attr( $field['validate_message'] ) : 'This field value needs to be unique.'; |
| 848 | |
| 849 | // Build output. |
| 850 | $output = $this->field_element( |
| 851 | 'label', |
| 852 | $field, |
| 853 | array( |
| 854 | 'slug' => 'validate_message', |
| 855 | 'value' => esc_html__( 'Validation Message for Duplicate', 'everest-forms' ), |
| 856 | 'tooltip' => $tooltip, |
| 857 | 'after_tooltip' => $toggle, |
| 858 | ), |
| 859 | false |
| 860 | ); |
| 861 | $output .= $this->field_element( |
| 862 | 'text', |
| 863 | $field, |
| 864 | array( |
| 865 | 'slug' => 'validate_message', |
| 866 | 'value' => $value, |
| 867 | ), |
| 868 | false |
| 869 | ); |
| 870 | $output = $this->field_element( |
| 871 | 'row', |
| 872 | $field, |
| 873 | array( |
| 874 | 'slug' => 'validate_message', |
| 875 | 'content' => $output, |
| 876 | 'class' => isset( $field['no_duplicates'] ) ? '' : 'hidden', |
| 877 | ), |
| 878 | $echo |
| 879 | ); |
| 880 | break; |
| 881 | /** |
| 882 | * No Duplicates. |
| 883 | */ |
| 884 | case 'show_tooltip': |
| 885 | $default = ! empty( $args['default'] ) ? $args['default'] : '0'; |
| 886 | $value = ! empty( $field['show_tooltip'] ) ? esc_attr( $field['show_tooltip'] ) : ''; |
| 887 | $output = $this->field_element( |
| 888 | 'checkbox', |
| 889 | $field, |
| 890 | array( |
| 891 | 'slug' => 'show_tooltip', |
| 892 | 'value' => $value, |
| 893 | 'desc' => esc_html__( 'Enable Tooltip', 'everest-forms' ), |
| 894 | 'tooltip' => esc_html__( 'Check this option to show the form field tooltip.', 'everest-forms' ), |
| 895 | ), |
| 896 | false |
| 897 | ); |
| 898 | $output = $this->field_element( |
| 899 | 'row', |
| 900 | $field, |
| 901 | array( |
| 902 | 'slug' => 'show_tooltip', |
| 903 | 'content' => $output, |
| 904 | ), |
| 905 | $echo |
| 906 | ); |
| 907 | $description_value = ! empty( $field['tooltip_description'] ) ? esc_attr( $field['tooltip_description'] ) : ''; |
| 908 | $output = $this->field_element( |
| 909 | 'textarea', |
| 910 | $field, |
| 911 | array( |
| 912 | 'slug' => 'tooltip_description', |
| 913 | 'value' => $description_value, |
| 914 | 'desc' => esc_html__( 'Enable Tooltip', 'everest-forms' ), |
| 915 | 'attrs' => array( |
| 916 | 'placeholder' => esc_html__( 'Enter the text for tooltip description.', 'everest-forms' ), |
| 917 | ), |
| 918 | ), |
| 919 | false |
| 920 | ); |
| 921 | |
| 922 | $output = $this->field_element( |
| 923 | 'row', |
| 924 | $field, |
| 925 | array( |
| 926 | 'slug' => 'tooltip_description', |
| 927 | 'content' => $output, |
| 928 | ), |
| 929 | $echo |
| 930 | ); |
| 931 | break; |
| 932 | /** |
| 933 | * No Duplicates. |
| 934 | */ |
| 935 | case 'autocomplete_address': |
| 936 | $default = ! empty( $args['default'] ) ? $args['default'] : '0'; |
| 937 | $value = ! empty( $field['autocomplete_address'] ) ? esc_attr( $field['autocomplete_address'] ) : ''; |
| 938 | $tooltip = esc_html__( 'Check this option to autofill address field.', 'everest-forms' ); |
| 939 | $output = $this->field_element( |
| 940 | 'checkbox', |
| 941 | $field, |
| 942 | array( |
| 943 | 'slug' => 'autocomplete_address', |
| 944 | 'value' => $value, |
| 945 | 'desc' => esc_html__( 'Enable Autocomplete Address Field', 'everest-forms' ), |
| 946 | 'tooltip' => $tooltip, |
| 947 | ), |
| 948 | false |
| 949 | ); |
| 950 | $output = $this->field_element( |
| 951 | 'row', |
| 952 | $field, |
| 953 | array( |
| 954 | 'slug' => 'autocomplete_address', |
| 955 | 'content' => $output, |
| 956 | ), |
| 957 | $echo |
| 958 | ); |
| 959 | break; |
| 960 | case 'address_style': |
| 961 | $default = ! empty( $args['default'] ) ? $args['default'] : 'none'; |
| 962 | $tooltip = esc_html__( 'Select the style', 'everest-forms' ); |
| 963 | $value = ! empty( $field['address_style'] ) ? esc_attr( $field['address_style'] ) : ''; |
| 964 | $output = $this->field_element( |
| 965 | 'label', |
| 966 | $field, |
| 967 | array( |
| 968 | 'slug' => 'address_style', |
| 969 | 'value' => esc_html__( 'Style', 'everest-forms' ), |
| 970 | 'tooltip' => $tooltip, |
| 971 | ), |
| 972 | false |
| 973 | ); |
| 974 | $output .= $this->field_element( |
| 975 | 'select', |
| 976 | $field, |
| 977 | array( |
| 978 | 'slug' => 'address_style', |
| 979 | 'value' => $value, |
| 980 | 'tooltip' => $tooltip, |
| 981 | 'options' => array( |
| 982 | 'address' => esc_html__( 'Address', 'everest-forms' ), |
| 983 | 'map' => esc_html__( 'Map', 'everest-forms' ), |
| 984 | 'address_with_map' => esc_html( 'Address With Map' ), |
| 985 | ), |
| 986 | ), |
| 987 | false |
| 988 | ); |
| 989 | $output = $this->field_element( |
| 990 | 'row', |
| 991 | $field, |
| 992 | array( |
| 993 | 'slug' => 'address_style', |
| 994 | 'content' => $output, |
| 995 | ), |
| 996 | $echo |
| 997 | ); |
| 998 | break; |
| 999 | |
| 1000 | /* |
| 1001 | * Code Block. |
| 1002 | */ |
| 1003 | case 'code': |
| 1004 | $value = ! empty( $field['code'] ) ? esc_attr( $field['code'] ) : ''; |
| 1005 | $tooltip = esc_html__( 'Enter code for the form field.', 'everest-forms' ); |
| 1006 | $output = $this->field_element( |
| 1007 | 'label', |
| 1008 | $field, |
| 1009 | array( |
| 1010 | 'slug' => 'code', |
| 1011 | 'value' => esc_html__( 'Code', 'everest-forms' ), |
| 1012 | 'tooltip' => $tooltip, |
| 1013 | ), |
| 1014 | false |
| 1015 | ); |
| 1016 | $output .= $this->field_element( |
| 1017 | 'textarea', |
| 1018 | $field, |
| 1019 | array( |
| 1020 | 'slug' => 'code', |
| 1021 | 'value' => $value, |
| 1022 | ), |
| 1023 | false |
| 1024 | ); |
| 1025 | $output = $this->field_element( |
| 1026 | 'row', |
| 1027 | $field, |
| 1028 | array( |
| 1029 | 'slug' => 'code', |
| 1030 | 'content' => $output, |
| 1031 | ), |
| 1032 | $echo |
| 1033 | ); |
| 1034 | break; |
| 1035 | |
| 1036 | /* |
| 1037 | * Choices. |
| 1038 | */ |
| 1039 | case 'choices': |
| 1040 | $class = array(); |
| 1041 | $label = ! empty( $args['label'] ) ? esc_html( $args['label'] ) : esc_html__( 'Choices', 'everest-forms' ); |
| 1042 | $choices = ! empty( $field['choices'] ) ? $field['choices'] : $this->defaults; |
| 1043 | $input_type = in_array( $field['type'], array( 'radio', 'select', 'payment-multiple' ), true ) ? 'radio' : 'checkbox'; |
| 1044 | |
| 1045 | if ( ! empty( $field['show_values'] ) ) { |
| 1046 | $class[] = 'show-values'; |
| 1047 | } |
| 1048 | |
| 1049 | if ( ! empty( $field['choices_images'] ) ) { |
| 1050 | $class[] = 'show-images'; |
| 1051 | } |
| 1052 | |
| 1053 | if ( ! empty( $field['multiple_choices'] ) ) { |
| 1054 | $input_type = 'checkbox'; |
| 1055 | } |
| 1056 | |
| 1057 | // Add bulk options toggle handle. |
| 1058 | $bulk_add_enabled = apply_filters( 'evf_bulk_add_enabled', true ); |
| 1059 | $licensed = ( false === evf_get_license_plan() ) ? false : true; |
| 1060 | $upgradable_feature_class = ( true === $licensed ) ? '' : 'evf-upgradable-feature'; |
| 1061 | $bulk_options_toggle_handle = sprintf( '<a href="#" class="evf-toggle-bulk-options after-label-description %s">%s</a>', esc_attr( $upgradable_feature_class ), esc_html__( 'Bulk Add', 'everest-forms' ) ); |
| 1062 | |
| 1063 | // Field label. |
| 1064 | $field_label = $this->field_element( |
| 1065 | 'label', |
| 1066 | $field, |
| 1067 | array( |
| 1068 | 'slug' => 'choices', |
| 1069 | 'value' => $label, |
| 1070 | 'tooltip' => esc_html__( 'Add choices for the form field.', 'everest-forms' ), |
| 1071 | 'after_tooltip' => $bulk_options_toggle_handle, // @todo Bulk import and export for choices. |
| 1072 | ) |
| 1073 | ); |
| 1074 | $field_content = ''; |
| 1075 | |
| 1076 | if ( true === $bulk_add_enabled && true === $licensed ) { |
| 1077 | $field_content .= $this->field_option( |
| 1078 | 'add_bulk_options', |
| 1079 | $field, |
| 1080 | array( |
| 1081 | 'class' => 'everest-forms-hidden', |
| 1082 | ) |
| 1083 | ); |
| 1084 | } |
| 1085 | |
| 1086 | if ( 'select' === $field['type'] ) { |
| 1087 | $selection_btn = array(); |
| 1088 | $selection_types = array( |
| 1089 | 'single' => array( |
| 1090 | 'type' => 'radio', |
| 1091 | 'label' => esc_html__( 'Single Selection', 'everest-forms' ), |
| 1092 | ), |
| 1093 | 'multiple' => array( |
| 1094 | 'type' => 'checkbox', |
| 1095 | 'label' => esc_html__( 'Multiple Selection', 'everest-forms' ), |
| 1096 | ), |
| 1097 | ); |
| 1098 | |
| 1099 | $active_type = ! empty( $field['multiple_choices'] ) && '1' === $field['multiple_choices'] ? 'multiple' : 'single'; |
| 1100 | foreach ( $selection_types as $key => $selection_type ) { |
| 1101 | $selection_btn[ $key ] = '<span data-selection="' . esc_attr( $key ) . '" data-type="' . esc_attr( $selection_type['type'] ) . '" class="flex everest-forms-btn ' . ( $active_type === $key ? 'is-active' : '' ) . ' ' . ( false === $licensed && 'multiple' === $key ? 'upgrade-modal' : '' ) . '" data-feature="' . esc_html__( 'Multiple selection', 'everest-forms' ) . '">' . esc_html( $selection_type['label'] ) . '</span>'; |
| 1102 | } |
| 1103 | |
| 1104 | $field_content .= sprintf( |
| 1105 | '<div class="flex everest-forms-btn-group everest-forms-btn-group--inline"><input type="hidden" id="everest-forms-field-option-%1$s-multiple_choices" name="form_fields[%1$s][multiple_choices]" value="%2$s" />%3$s</div>', |
| 1106 | esc_attr( $field['id'] ), |
| 1107 | ! empty( $field['multiple_choices'] ) && '1' === $field['multiple_choices'] ? 1 : 0, |
| 1108 | implode( '', $selection_btn ) |
| 1109 | ); |
| 1110 | } |
| 1111 | |
| 1112 | // Field contents. |
| 1113 | $field_content .= sprintf( |
| 1114 | '<ul data-next-id="%s" class="evf-choices-list %s" data-field-id="%s" data-field-type="%s">', |
| 1115 | max( array_keys( $choices ) ) + 1, |
| 1116 | evf_sanitize_classes( $class, true ), |
| 1117 | $field['id'], |
| 1118 | $this->type |
| 1119 | ); |
| 1120 | foreach ( $choices as $key => $choice ) { |
| 1121 | $default = ! empty( $choice['default'] ) ? $choice['default'] : ''; |
| 1122 | $name = sprintf( 'form_fields[%s][choices][%s]', $field['id'], $key ); |
| 1123 | $image = ! empty( $choice['image'] ) ? $choice['image'] : ''; |
| 1124 | |
| 1125 | // BW compatibility for value in payment fields. |
| 1126 | if ( ! empty( $field['amount'][ $key ]['value'] ) ) { |
| 1127 | $choice['value'] = evf_format_amount( evf_sanitize_amount( $field['amount'][ $key ]['value'] ) ); |
| 1128 | } |
| 1129 | |
| 1130 | $field_content .= sprintf( '<li data-key="%1$d">', absint( $key ) ); |
| 1131 | $field_content .= '<span class="sort"><svg width="18" height="18" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 18 18" role="img" aria-hidden="true" focusable="false"><path d="M13,8c0.6,0,1-0.4,1-1s-0.4-1-1-1s-1,0.4-1,1S12.4,8,13,8z M5,6C4.4,6,4,6.4,4,7s0.4,1,1,1s1-0.4,1-1S5.6,6,5,6z M5,10 c-0.6,0-1,0.4-1,1s0.4,1,1,1s1-0.4,1-1S5.6,10,5,10z M13,10c-0.6,0-1,0.4-1,1s0.4,1,1,1s1-0.4,1-1S13.6,10,13,10z M9,6 C8.4,6,8,6.4,8,7s0.4,1,1,1s1-0.4,1-1S9.6,6,9,6z M9,10c-0.6,0-1,0.4-1,1s0.4,1,1,1s1-0.4,1-1S9.6,10,9,10z"></path></svg></span>'; |
| 1132 | $field_content .= sprintf( '<input type="%1$s" name="%2$s[default]" class="default" value="1" %3$s>', $input_type, $name, checked( '1', $default, false ) ); |
| 1133 | $field_content .= '<div class="evf-choice-list-input">'; |
| 1134 | $field_content .= sprintf( '<input type="text" name="%1$s[label]" value="%2$s" class="label" data-key="%3$s">', $name, esc_attr( $choice['label'] ), absint( $key ) ); |
| 1135 | if ( in_array( $field['type'], array( 'payment-multiple', 'payment-checkbox' ), true ) ) { |
| 1136 | $field_content .= sprintf( '<input type="text" name="%1$s[value]" value="%2$s" class="value evf-money-input" placeholder="%3$s">', $name, esc_attr( $choice['value'] ), evf_format_amount( 0 ) ); |
| 1137 | } else { |
| 1138 | $field_content .= sprintf( '<input type="text" name="%1$s[value]" value="%2$s" class="value">', $name, esc_attr( $choice['value'] ) ); |
| 1139 | } |
| 1140 | $field_content .= '</div>'; |
| 1141 | $field_content .= '<a class="add" href="#"><i class="dashicons dashicons-plus-alt"></i></a>'; |
| 1142 | $field_content .= '<a class="remove" href="#"><i class="dashicons dashicons-dismiss"></i></a>'; |
| 1143 | $field_content .= '<div class="everest-forms-attachment-media-view">'; |
| 1144 | $field_content .= sprintf( '<input type="hidden" class="source" name="%s[image]" value="%s">', $name, esc_url_raw( $image ) ); |
| 1145 | $field_content .= sprintf( '<button type="button" class="upload-button button-add-media"%s>%s</button>', ! empty( $image ) ? ' style="display:none;"' : '', esc_html__( 'Upload Image', 'everest-forms' ) ); |
| 1146 | $field_content .= '<div class="thumbnail thumbnail-image">'; |
| 1147 | if ( ! empty( $image ) ) { |
| 1148 | $field_content .= sprintf( '<img class="attachment-thumb" src="%1$s">', esc_url_raw( $image ) ); |
| 1149 | } |
| 1150 | $field_content .= '</div>'; |
| 1151 | $field_content .= sprintf( '<div class="actions"%s>', empty( $image ) ? ' style="display:none;"' : '' ); |
| 1152 | $field_content .= sprintf( '<button type="button" class="button remove-button">%1$s</button>', esc_html__( 'Remove', 'everest-forms' ) ); |
| 1153 | $field_content .= sprintf( '<button type="button" class="button upload-button">%1$s</button>', esc_html__( 'Change image', 'everest-forms' ) ); |
| 1154 | $field_content .= '</div>'; |
| 1155 | $field_content .= '</div>'; |
| 1156 | $field_content .= '</li>'; |
| 1157 | } |
| 1158 | $field_content .= '</ul>'; |
| 1159 | |
| 1160 | // Final field output. |
| 1161 | $output = $this->field_element( |
| 1162 | 'row', |
| 1163 | $field, |
| 1164 | array( |
| 1165 | 'slug' => 'choices', |
| 1166 | 'content' => $field_label . $field_content, |
| 1167 | ), |
| 1168 | $echo |
| 1169 | ); |
| 1170 | break; |
| 1171 | |
| 1172 | /* |
| 1173 | * Choices Images. |
| 1174 | */ |
| 1175 | case 'choices_images': |
| 1176 | $field_content = sprintf( |
| 1177 | '<div class="notice notice-warning%s"><p>%s</p></div>', |
| 1178 | empty( $field['choices_images'] ) ? ' hidden' : '', |
| 1179 | esc_html__( 'For best results, images should be square and at least 200 × 160 pixels or smaller.', 'everest-forms' ) |
| 1180 | ); |
| 1181 | |
| 1182 | $field_content .= $this->field_element( |
| 1183 | 'checkbox', |
| 1184 | $field, |
| 1185 | array( |
| 1186 | 'slug' => 'choices_images', |
| 1187 | 'value' => isset( $field['choices_images'] ) ? '1' : '0', |
| 1188 | 'desc' => esc_html__( 'Use image choices', 'everest-forms' ), |
| 1189 | 'tooltip' => esc_html__( 'Check this option to enable using images with the choices.', 'everest-forms' ), |
| 1190 | ), |
| 1191 | false |
| 1192 | ); |
| 1193 | |
| 1194 | // Final field output. |
| 1195 | $output = $this->field_element( |
| 1196 | 'row', |
| 1197 | $field, |
| 1198 | array( |
| 1199 | 'slug' => 'choices_images', |
| 1200 | 'content' => $field_content, |
| 1201 | ), |
| 1202 | $echo |
| 1203 | ); |
| 1204 | break; |
| 1205 | |
| 1206 | /** |
| 1207 | * Add bulk options. |
| 1208 | */ |
| 1209 | case 'add_bulk_options': |
| 1210 | $class = ! empty( $args['class'] ) ? esc_attr( $args['class'] ) : ''; |
| 1211 | $label = ! empty( $args['label'] ) ? esc_html( $args['label'] ) : esc_html__( 'Add Bulk Options', 'everest-forms' ); |
| 1212 | |
| 1213 | // Field label. |
| 1214 | $field_label = $this->field_element( |
| 1215 | 'label', |
| 1216 | $field, |
| 1217 | array( |
| 1218 | 'slug' => 'add_bulk_options', |
| 1219 | 'value' => $label, |
| 1220 | 'tooltip' => esc_html__( 'Add multiple options at once.', 'everest-forms' ), |
| 1221 | 'after_tooltip' => sprintf( '<a class="evf-toggle-presets-list" href="#">%s</a>', esc_html__( 'Presets', 'everest-forms' ) ), |
| 1222 | ), |
| 1223 | false |
| 1224 | ); |
| 1225 | |
| 1226 | // Preset contents. |
| 1227 | $presets = array( |
| 1228 | array( |
| 1229 | 'label' => esc_html__( 'Months', 'everest-forms' ), |
| 1230 | 'class' => 'evf-options-preset-months', |
| 1231 | 'options' => array( |
| 1232 | esc_html__( 'January', 'everest-forms' ), |
| 1233 | esc_html__( 'February', 'everest-forms' ), |
| 1234 | esc_html__( 'March', 'everest-forms' ), |
| 1235 | esc_html__( 'April', 'everest-forms' ), |
| 1236 | esc_html__( 'May', 'everest-forms' ), |
| 1237 | esc_html__( 'June', 'everest-forms' ), |
| 1238 | esc_html__( 'July', 'everest-forms' ), |
| 1239 | esc_html__( 'August', 'everest-forms' ), |
| 1240 | esc_html__( 'September', 'everest-forms' ), |
| 1241 | esc_html__( 'October', 'everest-forms' ), |
| 1242 | esc_html__( 'November', 'everest-forms' ), |
| 1243 | esc_html__( 'December', 'everest-forms' ), |
| 1244 | ), |
| 1245 | ), |
| 1246 | array( |
| 1247 | 'label' => esc_html__( 'Week Days', 'everest-forms' ), |
| 1248 | 'class' => 'evf-options-preset-week-days', |
| 1249 | 'options' => array( |
| 1250 | esc_html__( 'Sunday', 'everest-forms' ), |
| 1251 | esc_html__( 'Monday', 'everest-forms' ), |
| 1252 | esc_html__( 'Tuesday', 'everest-forms' ), |
| 1253 | esc_html__( 'Wednesday', 'everest-forms' ), |
| 1254 | esc_html__( 'Thursday', 'everest-forms' ), |
| 1255 | esc_html__( 'Friday', 'everest-forms' ), |
| 1256 | esc_html__( 'Saturday', 'everest-forms' ), |
| 1257 | ), |
| 1258 | ), |
| 1259 | array( |
| 1260 | 'label' => esc_html__( 'Countries', 'everest-forms' ), |
| 1261 | 'class' => 'evf-options-preset-countries', |
| 1262 | 'options' => array_values( evf_get_countries() ), |
| 1263 | ), |
| 1264 | array( |
| 1265 | 'label' => esc_html__( 'Countries Postal Code', 'everest-forms' ), |
| 1266 | 'class' => 'evf-options-preset-countries-postal-code', |
| 1267 | 'options' => array_keys( evf_get_countries() ), |
| 1268 | ), |
| 1269 | array( |
| 1270 | 'label' => esc_html__( 'U.S. States', 'everest-forms' ), |
| 1271 | 'class' => 'evf-options-preset-states', |
| 1272 | 'options' => array_values( evf_get_states()['US'] ), |
| 1273 | ), |
| 1274 | array( |
| 1275 | 'label' => esc_html__( 'U.S. States Postal Code', 'everest-forms' ), |
| 1276 | 'class' => 'evf-options-preset-states-postal-code', |
| 1277 | 'options' => array_keys( evf_get_states()['US'] ), |
| 1278 | ), |
| 1279 | array( |
| 1280 | 'label' => esc_html__( 'Age Groups', 'everest-forms' ), |
| 1281 | 'class' => 'evf-options-preset-age-groups', |
| 1282 | 'options' => array( |
| 1283 | esc_html__( 'Under 18', 'everest-forms' ), |
| 1284 | esc_html__( '18-24', 'everest-forms' ), |
| 1285 | esc_html__( '25-34', 'everest-forms' ), |
| 1286 | esc_html__( '35-44', 'everest-forms' ), |
| 1287 | esc_html__( '45-54', 'everest-forms' ), |
| 1288 | esc_html__( '55-64', 'everest-forms' ), |
| 1289 | esc_html__( '65 or Above', 'everest-forms' ), |
| 1290 | esc_html__( 'Prefer Not to Answer', 'everest-forms' ), |
| 1291 | ), |
| 1292 | ), |
| 1293 | array( |
| 1294 | 'label' => esc_html__( 'Satisfaction', 'everest-forms' ), |
| 1295 | 'class' => 'evf-options-preset-satisfaction', |
| 1296 | 'options' => array( |
| 1297 | esc_html__( 'Very Satisfied', 'everest-forms' ), |
| 1298 | esc_html__( 'Satisfied', 'everest-forms' ), |
| 1299 | esc_html__( 'Neutral', 'everest-forms' ), |
| 1300 | esc_html__( 'Unsatisfied', 'everest-forms' ), |
| 1301 | esc_html__( 'Very Unsatisfied', 'everest-forms' ), |
| 1302 | esc_html__( 'N/A', 'everest-forms' ), |
| 1303 | ), |
| 1304 | ), |
| 1305 | array( |
| 1306 | 'label' => esc_html__( 'Importance', 'everest-forms' ), |
| 1307 | 'class' => 'evf-options-preset-importance', |
| 1308 | 'options' => array( |
| 1309 | esc_html__( 'Very Important', 'everest-forms' ), |
| 1310 | esc_html__( 'Important', 'everest-forms' ), |
| 1311 | esc_html__( 'Neutral', 'everest-forms' ), |
| 1312 | esc_html__( 'Somewhat Important', 'everest-forms' ), |
| 1313 | esc_html__( 'Not at all Important', 'everest-forms' ), |
| 1314 | esc_html__( 'N/A', 'everest-forms' ), |
| 1315 | ), |
| 1316 | ), |
| 1317 | array( |
| 1318 | 'label' => esc_html__( 'Agreement', 'everest-forms' ), |
| 1319 | 'class' => 'evf-options-preset-agreement', |
| 1320 | 'options' => array( |
| 1321 | esc_html__( 'Strongly Agree', 'everest-forms' ), |
| 1322 | esc_html__( 'Agree', 'everest-forms' ), |
| 1323 | esc_html__( 'Neutral', 'everest-forms' ), |
| 1324 | esc_html__( 'Disagree', 'everest-forms' ), |
| 1325 | esc_html__( 'Strongly Disagree', 'everest-forms' ), |
| 1326 | esc_html__( 'N/A', 'everest-forms' ), |
| 1327 | ), |
| 1328 | ), |
| 1329 | ); |
| 1330 | $presets_html = '<div class="evf-options-presets" hidden>'; |
| 1331 | foreach ( $presets as $preset ) { |
| 1332 | $presets_html .= sprintf( '<div class="evf-options-preset %s">', esc_attr( $preset['class'] ) ); |
| 1333 | $presets_html .= sprintf( '<a class="evf-options-preset-label" href="#">%s</a>', $preset['label'] ); |
| 1334 | $presets_html .= sprintf( '<textarea hidden class="evf-options-preset-value">%s</textarea>', implode( "\n", $preset['options'] ) ); |
| 1335 | $presets_html .= '</div>'; |
| 1336 | } |
| 1337 | $presets_html .= '</div>'; |
| 1338 | |
| 1339 | // Field contents. |
| 1340 | $field_content = $this->field_element( |
| 1341 | 'textarea', |
| 1342 | $field, |
| 1343 | array( |
| 1344 | 'slug' => 'add_bulk_options', |
| 1345 | 'value' => '', |
| 1346 | ), |
| 1347 | false |
| 1348 | ); |
| 1349 | $field_content .= sprintf( '<a class="button button-small evf-add-bulk-options" href="#">%s</a>', esc_html__( 'Add New Choices', 'everest-forms' ) ); |
| 1350 | |
| 1351 | // Final field output. |
| 1352 | $output = $this->field_element( |
| 1353 | 'row', |
| 1354 | $field, |
| 1355 | array( |
| 1356 | 'slug' => 'add_bulk_options', |
| 1357 | 'content' => $field_label . $presets_html . $field_content, |
| 1358 | 'class' => $class, |
| 1359 | ), |
| 1360 | $echo |
| 1361 | ); |
| 1362 | break; |
| 1363 | |
| 1364 | /** |
| 1365 | * Advanced Fields. |
| 1366 | */ |
| 1367 | |
| 1368 | /* |
| 1369 | * Default value. |
| 1370 | */ |
| 1371 | case 'default_value': |
| 1372 | $value = ! empty( $field['default_value'] ) || ( isset( $field['default_value'] ) && '0' === (string) $field['default_value'] ) ? esc_attr( $field['default_value'] ) : ''; |
| 1373 | $tooltip = esc_html__( 'Enter text for the default form field value.', 'everest-forms' ); |
| 1374 | $toggle = ''; |
| 1375 | $output = $this->field_element( |
| 1376 | 'label', |
| 1377 | $field, |
| 1378 | array( |
| 1379 | 'slug' => 'default_value', |
| 1380 | 'value' => esc_html__( 'Default Value', 'everest-forms' ), |
| 1381 | 'tooltip' => $tooltip, |
| 1382 | 'after_tooltip' => $toggle, |
| 1383 | ), |
| 1384 | false |
| 1385 | ); |
| 1386 | $output .= $this->field_element( |
| 1387 | 'text', |
| 1388 | $field, |
| 1389 | array( |
| 1390 | 'slug' => 'default_value', |
| 1391 | 'value' => $value, |
| 1392 | ), |
| 1393 | false |
| 1394 | ); |
| 1395 | |
| 1396 | // Smart tag for default value. |
| 1397 | $exclude_fields = array( 'rating', 'number', 'range-slider', 'payment-quantity', 'reset' ); |
| 1398 | |
| 1399 | if ( ! in_array( $field['type'], $exclude_fields, true ) ) { |
| 1400 | $output .= '<a href="#" class="evf-toggle-smart-tag-display" data-type="other"><span class="dashicons dashicons-editor-code"></span></a>'; |
| 1401 | $output .= '<div class="evf-smart-tag-lists" style="display: none">'; |
| 1402 | $output .= '<div class="smart-tag-title other-tag-title">Others</div><ul class="evf-others"></ul></div>'; |
| 1403 | } |
| 1404 | |
| 1405 | $output = $this->field_element( |
| 1406 | 'row', |
| 1407 | $field, |
| 1408 | array( |
| 1409 | 'slug' => 'default_value', |
| 1410 | 'content' => $output, |
| 1411 | 'class' => in_array( $field['type'], $exclude_fields, true ) ? '' : 'evf_smart_tag', |
| 1412 | ), |
| 1413 | $echo |
| 1414 | ); |
| 1415 | break; |
| 1416 | |
| 1417 | /* |
| 1418 | * Advanced Options markup. |
| 1419 | */ |
| 1420 | case 'advanced-options': |
| 1421 | $markup = ! empty( $args['markup'] ) ? $args['markup'] : 'open'; |
| 1422 | |
| 1423 | if ( 'open' === $markup ) { |
| 1424 | $override = apply_filters( 'everest_forms_advanced_options_override', false ); |
| 1425 | $override = ! empty( $override ) ? 'style="display:' . $override . ';"' : ''; |
| 1426 | if ( $echo ) { |
| 1427 | echo sprintf( '<div class="everest-forms-field-option-group everest-forms-field-option-group-advanced everest-forms-hide closed" id="everest-forms-field-option-advanced-%s" %s>', esc_attr( $field['id'] ), ( ! empty( $override ) ? 'style="display:' . esc_attr( apply_filters( 'everest_forms_advanced_options_override', false ) ) . ';"' : '' ) ); |
| 1428 | echo sprintf( '<a href="#" class="everest-forms-field-option-group-toggle">%s<i class="handlediv"></i></a>', esc_html__( 'Advanced Options', 'everest-forms' ) ); |
| 1429 | echo '<div class="everest-forms-field-option-group-inner">'; |
| 1430 | } else { |
| 1431 | $output = sprintf( '<div class="everest-forms-field-option-group everest-forms-field-option-group-advanced everest-forms-hide closed" id="everest-forms-field-option-advanced-%s" %s>', $field['id'], $override ); |
| 1432 | $output .= sprintf( '<a href="#" class="everest-forms-field-option-group-toggle">%s<i class="handlediv"></i></a>', __( 'Advanced Options', 'everest-forms' ) ); |
| 1433 | $output .= '<div class="everest-forms-field-option-group-inner">'; |
| 1434 | } |
| 1435 | } else { |
| 1436 | if ( $echo ) { |
| 1437 | echo '</div></div>'; |
| 1438 | } else { |
| 1439 | $output = '</div></div>'; |
| 1440 | } |
| 1441 | } |
| 1442 | |
| 1443 | break; |
| 1444 | |
| 1445 | /* |
| 1446 | * Placeholder. |
| 1447 | */ |
| 1448 | case 'placeholder': |
| 1449 | $value = ! empty( $field['placeholder'] ) || ( isset( $field['placeholder'] ) && '0' === (string) $field['placeholder'] ) ? esc_attr( $field['placeholder'] ) : ''; |
| 1450 | $tooltip = esc_html__( 'Enter text for the form field placeholder.', 'everest-forms' ); |
| 1451 | $output = $this->field_element( |
| 1452 | 'label', |
| 1453 | $field, |
| 1454 | array( |
| 1455 | 'slug' => 'placeholder', |
| 1456 | 'value' => esc_html__( 'Placeholder Text', 'everest-forms' ), |
| 1457 | 'tooltip' => $tooltip, |
| 1458 | ), |
| 1459 | false |
| 1460 | ); |
| 1461 | $output .= $this->field_element( |
| 1462 | 'text', |
| 1463 | $field, |
| 1464 | array( |
| 1465 | 'slug' => 'placeholder', |
| 1466 | 'value' => $value, |
| 1467 | ), |
| 1468 | false |
| 1469 | ); |
| 1470 | $output = $this->field_element( |
| 1471 | 'row', |
| 1472 | $field, |
| 1473 | array( |
| 1474 | 'slug' => 'placeholder', |
| 1475 | 'content' => $output, |
| 1476 | ), |
| 1477 | $echo |
| 1478 | ); |
| 1479 | break; |
| 1480 | case 'enable_prepopulate': |
| 1481 | $default = ! empty( $args['default'] ) ? $args['default'] : '0'; |
| 1482 | $value = ! empty( $field['enable_prepopulate'] ) ? esc_attr( $field['enable_prepopulate'] ) : ''; |
| 1483 | $tooltip = esc_html__( 'Enable this option to allow field to be populated dynamically', 'everest-forms' ); |
| 1484 | $output = $this->field_element( |
| 1485 | 'checkbox', |
| 1486 | $field, |
| 1487 | array( |
| 1488 | 'slug' => 'enable_prepopulate', |
| 1489 | 'value' => $value, |
| 1490 | 'desc' => esc_html__( 'Enable Autopopulate ', 'everest-forms' ), |
| 1491 | 'tooltip' => $tooltip, |
| 1492 | ), |
| 1493 | false |
| 1494 | ); |
| 1495 | $output = $this->field_element( |
| 1496 | 'row', |
| 1497 | $field, |
| 1498 | array( |
| 1499 | 'slug' => 'enable_prepopulate', |
| 1500 | 'content' => $output, |
| 1501 | ), |
| 1502 | $echo |
| 1503 | ); |
| 1504 | break; |
| 1505 | case 'parameter_name': |
| 1506 | $toggle = ''; |
| 1507 | $tooltip = esc_html__( 'Enter name of the parameter to populate the field.', 'everest-forms' ); |
| 1508 | $value = ! empty( $field['parameter_name'] ) ? esc_attr( $field['parameter_name'] ) : ''; |
| 1509 | |
| 1510 | // Build output. |
| 1511 | $output = $this->field_element( |
| 1512 | 'label', |
| 1513 | $field, |
| 1514 | array( |
| 1515 | 'slug' => 'parameter_name', |
| 1516 | 'value' => esc_html__( 'Parameter Name', 'everest-forms' ), |
| 1517 | 'tooltip' => $tooltip, |
| 1518 | 'after_tooltip' => $toggle, |
| 1519 | ), |
| 1520 | false |
| 1521 | ); |
| 1522 | $output .= $this->field_element( |
| 1523 | 'text', |
| 1524 | $field, |
| 1525 | array( |
| 1526 | 'slug' => 'parameter_name', |
| 1527 | 'value' => $value, |
| 1528 | ), |
| 1529 | false |
| 1530 | ); |
| 1531 | $output = $this->field_element( |
| 1532 | 'row', |
| 1533 | $field, |
| 1534 | array( |
| 1535 | 'slug' => 'parameter_name', |
| 1536 | 'content' => $output, |
| 1537 | 'class' => isset( $field['enable_prepopulate'] ) ? '' : 'hidden', |
| 1538 | ), |
| 1539 | $echo |
| 1540 | ); |
| 1541 | break; |
| 1542 | |
| 1543 | /** |
| 1544 | * Regex Validation |
| 1545 | */ |
| 1546 | case 'regex_validation': |
| 1547 | $default = ! empty( $args['default'] ) ? $args['default'] : '0'; |
| 1548 | $value = ! empty( $field['enable_regex_validation'] ) ? esc_attr( $field['enable_regex_validation'] ) : ''; |
| 1549 | $tooltip = esc_html__( 'Enable this option to allow regex validation for this field.', 'everest-forms' ); |
| 1550 | $output = $this->field_element( |
| 1551 | 'checkbox', |
| 1552 | $field, |
| 1553 | array( |
| 1554 | 'slug' => 'enable_regex_validation', |
| 1555 | 'value' => $value, |
| 1556 | 'desc' => esc_html__( 'Enable Regex Validation ', 'everest-forms' ), |
| 1557 | 'tooltip' => $tooltip, |
| 1558 | ), |
| 1559 | false |
| 1560 | ); |
| 1561 | $output = $this->field_element( |
| 1562 | 'row', |
| 1563 | $field, |
| 1564 | array( |
| 1565 | 'slug' => 'enable_regex_validation', |
| 1566 | 'content' => $output, |
| 1567 | ), |
| 1568 | $echo |
| 1569 | ); |
| 1570 | break; |
| 1571 | |
| 1572 | case 'regex_value': |
| 1573 | $toggle = ''; |
| 1574 | $tooltip = esc_html__( 'Regular expression value is checked against.', 'everest-forms' ); |
| 1575 | $value = ! empty( $field['regex_value'] ) ? esc_attr( $field['regex_value'] ) : ''; |
| 1576 | |
| 1577 | // Build output. |
| 1578 | $output = $this->field_element( |
| 1579 | 'label', |
| 1580 | $field, |
| 1581 | array( |
| 1582 | 'slug' => 'regex_value', |
| 1583 | 'value' => esc_html__( 'Regex Value', 'everest-forms' ), |
| 1584 | 'tooltip' => $tooltip, |
| 1585 | 'after_tooltip' => $toggle, |
| 1586 | ), |
| 1587 | false |
| 1588 | ); |
| 1589 | $output .= $this->field_element( |
| 1590 | 'text', |
| 1591 | $field, |
| 1592 | array( |
| 1593 | 'slug' => 'regex_value', |
| 1594 | 'value' => $value, |
| 1595 | ), |
| 1596 | false |
| 1597 | ); |
| 1598 | // Smart tag for default value. |
| 1599 | $include_fields = array( 'email', 'first-name', 'last-name', 'number', 'text', 'url' ); |
| 1600 | |
| 1601 | if ( in_array( $field['type'], $include_fields, true ) ) { |
| 1602 | $output .= '<a href="#" class="evf-toggle-smart-tag-display" data-type="regex"><span class="dashicons dashicons-editor-code"></span></a>'; |
| 1603 | $output .= '<div class="evf-smart-tag-lists" style="display: none">'; |
| 1604 | $output .= '<div class="smart-tag-title other-tag-title">Regular Expression</div><ul class="evf-regex"></ul></div>'; |
| 1605 | } |
| 1606 | |
| 1607 | $output = $this->field_element( |
| 1608 | 'row', |
| 1609 | $field, |
| 1610 | array( |
| 1611 | 'slug' => 'regex_value', |
| 1612 | 'content' => $output, |
| 1613 | 'class' => ! in_array( $field['type'], $include_fields, true ) && isset( $field['enable_regex_validation'] ) ? '' : ' hidden evf_smart_tag', |
| 1614 | ), |
| 1615 | $echo |
| 1616 | ); |
| 1617 | |
| 1618 | break; |
| 1619 | |
| 1620 | case 'regex_message': |
| 1621 | $toggle = ''; |
| 1622 | $tooltip = esc_html__( 'if the regular expression value does not match it will show this message.', 'everest-forms' ); |
| 1623 | $value = ! empty( $field['regex_message'] ) ? esc_attr( $field['regex_message'] ) : 'Please provide a valid value for this field.'; |
| 1624 | |
| 1625 | // Build output. |
| 1626 | $output = $this->field_element( |
| 1627 | 'label', |
| 1628 | $field, |
| 1629 | array( |
| 1630 | 'slug' => 'regex_message', |
| 1631 | 'value' => esc_html__( 'Validation Message for Regular expression', 'everest-forms' ), |
| 1632 | 'tooltip' => $tooltip, |
| 1633 | 'after_tooltip' => $toggle, |
| 1634 | ), |
| 1635 | false |
| 1636 | ); |
| 1637 | $output .= $this->field_element( |
| 1638 | 'text', |
| 1639 | $field, |
| 1640 | array( |
| 1641 | 'slug' => 'regex_message', |
| 1642 | 'value' => $value, |
| 1643 | ), |
| 1644 | false |
| 1645 | ); |
| 1646 | $output = $this->field_element( |
| 1647 | 'row', |
| 1648 | $field, |
| 1649 | array( |
| 1650 | 'slug' => 'regex_message', |
| 1651 | 'content' => $output, |
| 1652 | 'class' => isset( $field['enable_regex_validation'] ) ? '' : 'hidden', |
| 1653 | ), |
| 1654 | $echo |
| 1655 | ); |
| 1656 | break; |
| 1657 | |
| 1658 | /* |
| 1659 | * CSS classes. |
| 1660 | */ |
| 1661 | case 'css': |
| 1662 | $toggle = ''; |
| 1663 | $tooltip = esc_html__( 'Enter CSS class names for this field container. Multiple class names should be separated with spaces.', 'everest-forms' ); |
| 1664 | $value = ! empty( $field['css'] ) ? esc_attr( $field['css'] ) : ''; |
| 1665 | |
| 1666 | // Build output. |
| 1667 | $output = $this->field_element( |
| 1668 | 'label', |
| 1669 | $field, |
| 1670 | array( |
| 1671 | 'slug' => 'css', |
| 1672 | 'value' => esc_html__( 'CSS Classes', 'everest-forms' ), |
| 1673 | 'tooltip' => $tooltip, |
| 1674 | 'after_tooltip' => $toggle, |
| 1675 | ), |
| 1676 | false |
| 1677 | ); |
| 1678 | $output .= $this->field_element( |
| 1679 | 'text', |
| 1680 | $field, |
| 1681 | array( |
| 1682 | 'slug' => 'css', |
| 1683 | 'value' => $value, |
| 1684 | ), |
| 1685 | false |
| 1686 | ); |
| 1687 | $output = $this->field_element( |
| 1688 | 'row', |
| 1689 | $field, |
| 1690 | array( |
| 1691 | 'slug' => 'css', |
| 1692 | 'content' => $output, |
| 1693 | ), |
| 1694 | $echo |
| 1695 | ); |
| 1696 | break; |
| 1697 | |
| 1698 | /* |
| 1699 | * Hide Label. |
| 1700 | */ |
| 1701 | case 'label_hide': |
| 1702 | $value = isset( $field['label_hide'] ) ? $field['label_hide'] : '0'; |
| 1703 | $tooltip = esc_html__( 'Check this option to hide the form field label.', 'everest-forms' ); |
| 1704 | |
| 1705 | // Build output. |
| 1706 | $output = $this->field_element( |
| 1707 | 'checkbox', |
| 1708 | $field, |
| 1709 | array( |
| 1710 | 'slug' => 'label_hide', |
| 1711 | 'value' => $value, |
| 1712 | 'desc' => esc_html__( 'Hide Label', 'everest-forms' ), |
| 1713 | 'tooltip' => $tooltip, |
| 1714 | ), |
| 1715 | false |
| 1716 | ); |
| 1717 | $output = $this->field_element( |
| 1718 | 'row', |
| 1719 | $field, |
| 1720 | array( |
| 1721 | 'slug' => 'label_hide', |
| 1722 | 'content' => $output, |
| 1723 | ), |
| 1724 | $echo |
| 1725 | ); |
| 1726 | break; |
| 1727 | |
| 1728 | /* |
| 1729 | * Hide Sub-Labels. |
| 1730 | */ |
| 1731 | case 'sublabel_hide': |
| 1732 | $value = isset( $field['sublabel_hide'] ) ? $field['sublabel_hide'] : '0'; |
| 1733 | $tooltip = esc_html__( 'Check this option to hide the form field sub-label.', 'everest-forms' ); |
| 1734 | |
| 1735 | // Build output. |
| 1736 | $output = $this->field_element( |
| 1737 | 'checkbox', |
| 1738 | $field, |
| 1739 | array( |
| 1740 | 'slug' => 'sublabel_hide', |
| 1741 | 'value' => $value, |
| 1742 | 'desc' => esc_html__( 'Hide Sub-Labels', 'everest-forms' ), |
| 1743 | 'tooltip' => $tooltip, |
| 1744 | ), |
| 1745 | false |
| 1746 | ); |
| 1747 | $output = $this->field_element( |
| 1748 | 'row', |
| 1749 | $field, |
| 1750 | array( |
| 1751 | 'slug' => 'sublabel_hide', |
| 1752 | 'content' => $output, |
| 1753 | ), |
| 1754 | $echo |
| 1755 | ); |
| 1756 | break; |
| 1757 | |
| 1758 | /* |
| 1759 | * Input columns. |
| 1760 | */ |
| 1761 | case 'input_columns': |
| 1762 | $value = ! empty( $field['input_columns'] ) ? esc_attr( $field['input_columns'] ) : ''; |
| 1763 | $tooltip = esc_html__( 'Select the column layout for displaying field choices.', 'everest-forms' ); |
| 1764 | $options = array( |
| 1765 | '' => esc_html__( 'One Column', 'everest-forms' ), |
| 1766 | '2' => esc_html__( 'Two Columns', 'everest-forms' ), |
| 1767 | '3' => esc_html__( 'Three Columns', 'everest-forms' ), |
| 1768 | 'inline' => esc_html__( 'Inline', 'everest-forms' ), |
| 1769 | ); |
| 1770 | |
| 1771 | // Build output. |
| 1772 | $output = $this->field_element( |
| 1773 | 'label', |
| 1774 | $field, |
| 1775 | array( |
| 1776 | 'slug' => 'input_columns', |
| 1777 | 'value' => esc_html__( 'Layout', 'everest-forms' ), |
| 1778 | 'tooltip' => $tooltip, |
| 1779 | ), |
| 1780 | false |
| 1781 | ); |
| 1782 | $output .= $this->field_element( |
| 1783 | 'select', |
| 1784 | $field, |
| 1785 | array( |
| 1786 | 'slug' => 'input_columns', |
| 1787 | 'value' => $value, |
| 1788 | 'options' => $options, |
| 1789 | ), |
| 1790 | false |
| 1791 | ); |
| 1792 | $output = $this->field_element( |
| 1793 | 'row', |
| 1794 | $field, |
| 1795 | array( |
| 1796 | 'slug' => 'input_columns', |
| 1797 | 'content' => $output, |
| 1798 | ), |
| 1799 | $echo |
| 1800 | ); |
| 1801 | break; |
| 1802 | |
| 1803 | /** |
| 1804 | * Whitelisted Domain. |
| 1805 | */ |
| 1806 | case 'whitelist_domain': |
| 1807 | $default = ! empty( $args['default'] ) ? $args['default'] : '0'; |
| 1808 | // $value for just backward compatibility. |
| 1809 | $value = ( isset( $field['whitelist_domain'] ) && ! empty( $field['whitelist_domain'] ) ) ? esc_attr( $field['whitelist_domain'] ) : ''; |
| 1810 | $allowed_domains = ( isset( $field['allowed_domains'] ) && ! empty( $field['allowed_domains'] ) ) ? esc_attr( $field['allowed_domains'] ) : ( ( isset( $field['select_whitelist'] ) && 'allow' === $field['select_whitelist'] ) ? $value : '' ); |
| 1811 | $denied_domains = ( isset( $field['denied_domains'] ) && ! empty( $field['denied_domains'] ) ) ? esc_attr( $field['denied_domains'] ) : ( ( isset( $field['select_whitelist'] ) && 'deny' === $field['select_whitelist'] ) ? $value : '' ); |
| 1812 | $style = ( isset( $field['select_whitelist'] ) && ! empty( $field['select_whitelist'] ) ) ? esc_attr( $field['select_whitelist'] ) : 'allow'; |
| 1813 | $tooltip = esc_html__( 'Please enter valid allowed or denied domains, separated by commas. For example: google.com, yahoo.com', 'everest-forms' ); |
| 1814 | $output = $this->field_element( |
| 1815 | 'label', |
| 1816 | $field, |
| 1817 | array( |
| 1818 | 'slug' => 'whitelist_domain', |
| 1819 | 'value' => esc_html__( 'Whitelisted Domains', 'everest-forms' ), |
| 1820 | 'tooltip' => $tooltip, |
| 1821 | ), |
| 1822 | false |
| 1823 | ); |
| 1824 | $output .= $this->field_element( |
| 1825 | 'select', |
| 1826 | $field, |
| 1827 | array( |
| 1828 | 'slug' => 'select_whitelist', |
| 1829 | 'value' => $style, |
| 1830 | 'options' => array( |
| 1831 | 'allow' => esc_html__( 'Allowed Domains', 'everest-forms' ), |
| 1832 | 'deny' => esc_html__( 'Denied Domains', 'everest-forms' ), |
| 1833 | ), |
| 1834 | ), |
| 1835 | false |
| 1836 | ); |
| 1837 | $output = $this->field_element( |
| 1838 | 'row', |
| 1839 | $field, |
| 1840 | array( |
| 1841 | 'slug' => 'whitelist_domain', |
| 1842 | 'content' => $output, |
| 1843 | ), |
| 1844 | $echo |
| 1845 | ); |
| 1846 | |
| 1847 | $output .= $this->field_element( |
| 1848 | 'row', |
| 1849 | $field, |
| 1850 | array( |
| 1851 | 'slug' => 'allowed_domains', |
| 1852 | 'content' => $this->field_element( |
| 1853 | 'text', |
| 1854 | $field, |
| 1855 | array( |
| 1856 | 'slug' => 'allowed_domains', |
| 1857 | 'value' => esc_attr( $allowed_domains ), |
| 1858 | 'placeholder' => esc_attr__( 'Allowed Domain(s)', 'everest-forms' ), |
| 1859 | ), |
| 1860 | false |
| 1861 | ), |
| 1862 | ), |
| 1863 | $echo |
| 1864 | ); |
| 1865 | $output .= $this->field_element( |
| 1866 | 'row', |
| 1867 | $field, |
| 1868 | array( |
| 1869 | 'slug' => 'denied_domains', |
| 1870 | 'content' => $this->field_element( |
| 1871 | 'text', |
| 1872 | $field, |
| 1873 | array( |
| 1874 | 'slug' => 'denied_domains', |
| 1875 | 'value' => esc_attr( $denied_domains ), |
| 1876 | 'placeholder' => esc_attr__( 'Denied Domain(s)', 'everest-forms' ), |
| 1877 | ), |
| 1878 | false |
| 1879 | ), |
| 1880 | ), |
| 1881 | $echo |
| 1882 | ); |
| 1883 | break; |
| 1884 | |
| 1885 | /** |
| 1886 | * Select All. |
| 1887 | */ |
| 1888 | case 'select_all': |
| 1889 | $value = isset( $field['select_all'] ) ? '1' : '0'; |
| 1890 | $tooltip = esc_html__( 'Check this option to hide the form field label.', 'everest-forms' ); |
| 1891 | |
| 1892 | $output = $this->field_element( |
| 1893 | 'checkbox', |
| 1894 | $field, |
| 1895 | array( |
| 1896 | 'slug' => 'select_all', |
| 1897 | 'value' => $value, |
| 1898 | 'class' => 'evf-select-all-chk', |
| 1899 | 'desc' => esc_html__( 'Select All', 'everest-forms' ), |
| 1900 | 'tooltip' => esc_html__( 'Check this option to select all the options.', 'everest-forms' ), |
| 1901 | ), |
| 1902 | false |
| 1903 | ); |
| 1904 | $output = $this->field_element( |
| 1905 | 'row', |
| 1906 | $field, |
| 1907 | array( |
| 1908 | 'slug' => 'select_all', |
| 1909 | 'content' => $output, |
| 1910 | ), |
| 1911 | $echo |
| 1912 | ); |
| 1913 | break; |
| 1914 | |
| 1915 | /* |
| 1916 | * Default. |
| 1917 | */ |
| 1918 | default: |
| 1919 | if ( is_callable( array( $this, $option ) ) ) { |
| 1920 | $this->{$option}( $field ); |
| 1921 | } |
| 1922 | do_action( 'everest_forms_field_options_' . $option, $this, $field, $args ); |
| 1923 | break; |
| 1924 | |
| 1925 | } |
| 1926 | |
| 1927 | if ( $echo && in_array( $option, array( 'basic-options', 'advanced-options' ), true ) && 'open' === $markup ) { |
| 1928 | do_action( "everest_forms_field_options_top_{$option}", $field, $this ); |
| 1929 | } elseif ( $echo && in_array( $option, array( 'basic-options', 'advanced-options' ), true ) && 'close' === $markup ) { |
| 1930 | do_action( "everest_forms_field_options_after_{$option}", $field, $this ); |
| 1931 | } |
| 1932 | |
| 1933 | if ( ! $echo ) { |
| 1934 | return $output; |
| 1935 | } |
| 1936 | } |
| 1937 | |
| 1938 | /** |
| 1939 | * Helper function to create common field options that are used frequently |
| 1940 | * in the field preview. |
| 1941 | * |
| 1942 | * @since 1.0.0 |
| 1943 | * |
| 1944 | * @param string $option Field option to render. |
| 1945 | * @param array $field Field data and settings. |
| 1946 | * @param array $args Field preview arguments. |
| 1947 | * @param boolean $echo Print or return the value. Print by default. |
| 1948 | * |
| 1949 | * @return mixed Print or return a string. |
| 1950 | */ |
| 1951 | public function field_preview_option( $option, $field, $args = array(), $echo = true ) { |
| 1952 | $output = ''; |
| 1953 | $class = ! empty( $args['class'] ) ? evf_sanitize_classes( $args['class'] ) : ''; |
| 1954 | $form_id = isset( $_GET['form_id'] ) ? absint( $_GET['form_id'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification |
| 1955 | $form_data = evf()->form->get( absint( $form_id ), array( 'content_only' => true ) ); |
| 1956 | $markup = ''; |
| 1957 | |
| 1958 | if ( $echo && in_array( $option, array( 'basic-options', 'advanced-options' ), true ) && 'open' === $markup ) { |
| 1959 | do_action( "everest_forms_field_options_before_{$option}", $field, $this ); |
| 1960 | } elseif ( $echo && in_array( $option, array( 'basic-options', 'advanced-options' ), true ) && 'close' === $markup ) { |
| 1961 | do_action( "everest_forms_field_options_bottom_{$option}", $field, $this ); |
| 1962 | } |
| 1963 | |
| 1964 | switch ( $option ) { |
| 1965 | case 'label': |
| 1966 | $label = isset( $field['label'] ) && ! empty( $field['label'] ) ? $field['label'] : ''; |
| 1967 | if ( $echo ) { |
| 1968 | echo sprintf( '<label class="label-title %s"><span class="text">%s</span><span class="required">%s</span></label>', esc_attr( $class ), esc_html( $label ), esc_html( apply_filters( 'everest_form_get_required_type', '*', $field, $form_data ) ) ); |
| 1969 | } else { |
| 1970 | $output = sprintf( '<label class="label-title %s"><span class="text">%s</span><span class="required">%s</span></label>', $class, $label, apply_filters( 'everest_form_get_required_type', '*', $field, $form_data ) ); |
| 1971 | } |
| 1972 | break; |
| 1973 | |
| 1974 | case 'description': |
| 1975 | $description = isset( $field['description'] ) && ! empty( $field['description'] ) ? $field['description'] : ''; |
| 1976 | $description = false !== strpos( $class, 'nl2br' ) ? nl2br( $description ) : $description; |
| 1977 | if ( $echo ) { |
| 1978 | echo sprintf( '<div class="description %s">%s</div>', esc_attr( $class ), esc_html( $description ) ); |
| 1979 | } else { |
| 1980 | $output = sprintf( '<div class="description %s">%s</div>', $class, $description ); |
| 1981 | } |
| 1982 | break; |
| 1983 | |
| 1984 | case 'repeater_fields': |
| 1985 | $repeater_fields = isset( $field['repeater_fields'] ) && ! empty( $field['repeater_fields'] ) ? $field['repeater_fields'] : ''; |
| 1986 | if ( $echo ) { |
| 1987 | echo sprintf( '<div>%s</div>', esc_html( $repeater_fields ) ); |
| 1988 | } else { |
| 1989 | $output = sprintf( '<div>%s</div>', $repeater_fields ); |
| 1990 | } |
| 1991 | break; |
| 1992 | |
| 1993 | case 'repeater_button_add_remove_label': |
| 1994 | $add_new_label = isset( $field['repeater_button_add_new_label'] ) && ! empty( $field['repeater_button_add_new_label'] ) ? $field['repeater_button_add_new_label'] : 'Add'; |
| 1995 | $remove_label = isset( $field['repeater_button_remove_label'] ) && ! empty( $field['repeater_button_remove_label'] ) ? $field['repeater_button_remove_label'] : 'Remove'; |
| 1996 | if ( $echo ) { |
| 1997 | echo sprintf( '<div style="margin-right: %s" class="evf-add-row repeater_button_add_remove_label %s"><span class="everest-forms-btn everest-forms-btn-primary dashicons dashicons-plus">%s</span> <span class="everest-forms-btn everest-forms-btn-primary dashicons dashicons-minus">%s</span></div>', '65%', esc_attr( $class ), esc_html( $add_new_label ), esc_html( $remove_label ) ); |
| 1998 | } else { |
| 1999 | $output = sprintf( '<div style="margin-right: %s" class="evf-add-row repeater_button_add_remove_label %s"><span class="everest-forms-btn everest-forms-btn-primary dashicons dashicons-plus">%s</span> <span class="everest-forms-btn everest-forms-btn-primary dashicons dashicons-minus">%s</span></div>', '65%', $class, $add_new_label, $remove_label ); |
| 2000 | } |
| 2001 | break; |
| 2002 | |
| 2003 | case 'choices': |
| 2004 | $values = ! empty( $field['choices'] ) ? $field['choices'] : $this->defaults; |
| 2005 | $choices_fields = array( 'select', 'radio', 'checkbox', 'payment-multiple', 'payment-checkbox' ); |
| 2006 | |
| 2007 | // Notify if choices source is currently empty. |
| 2008 | if ( empty( $values ) ) { |
| 2009 | $values = array( |
| 2010 | 'label' => esc_html__( '(empty)', 'everest-forms' ), |
| 2011 | ); |
| 2012 | } |
| 2013 | |
| 2014 | // Build output. |
| 2015 | if ( ! in_array( $field['type'], $choices_fields, true ) ) { |
| 2016 | break; |
| 2017 | } |
| 2018 | |
| 2019 | switch ( $field['type'] ) { |
| 2020 | case 'checkbox': |
| 2021 | case 'payment-checkbox': |
| 2022 | $type = 'checkbox'; |
| 2023 | break; |
| 2024 | |
| 2025 | case 'select': |
| 2026 | $type = 'select'; |
| 2027 | break; |
| 2028 | |
| 2029 | default: |
| 2030 | $type = 'radio'; |
| 2031 | break; |
| 2032 | } |
| 2033 | |
| 2034 | $list_class = array( 'widefat', 'primary-input' ); |
| 2035 | $choices_images = ! empty( $field['choices_images'] ); |
| 2036 | |
| 2037 | if ( $choices_images ) { |
| 2038 | $list_class[] = 'everest-forms-image-choices'; |
| 2039 | } |
| 2040 | |
| 2041 | if ( ! empty( $class ) ) { |
| 2042 | $list_class[] = $class; |
| 2043 | } |
| 2044 | |
| 2045 | if ( 'select' === $type ) { |
| 2046 | $multiple = ! empty( $field['multiple_choices'] ) ? ' multiple' : ''; |
| 2047 | $placeholder = ! empty( $field['placeholder'] ) ? esc_attr( $field['placeholder'] ) : ''; |
| 2048 | |
| 2049 | if ( $echo ) { |
| 2050 | echo sprintf( '<select class="%s" %s data-placeholder="%s" disabled>', esc_attr( evf_sanitize_classes( $list_class, true ) ), esc_attr( $multiple ), esc_attr( $placeholder ) ); |
| 2051 | |
| 2052 | // Optional placeholder. |
| 2053 | if ( ! empty( $placeholder ) ) { |
| 2054 | echo sprintf( '<option value="" class="placeholder">%s</option>', esc_html( $placeholder ) ); |
| 2055 | } |
| 2056 | |
| 2057 | // Build the select options (even though user can only see 1st option). |
| 2058 | foreach ( $values as $value ) { |
| 2059 | $default = isset( $value['default'] ) ? (bool) $value['default'] : false; |
| 2060 | $selected = ! empty( $placeholder ) && empty( $multiple ) ? '' : selected( true, $default, false ); |
| 2061 | echo sprintf( '<option %s>%s</option>', esc_attr( $selected ), esc_html( $value['label'] ) ); |
| 2062 | } |
| 2063 | |
| 2064 | echo '</select>'; |
| 2065 | } else { |
| 2066 | $output = sprintf( '<select class="%s" %s data-placeholder="%s" disabled>', evf_sanitize_classes( $list_class, true ), esc_attr( $multiple ), esc_attr( $placeholder ) ); |
| 2067 | |
| 2068 | // Optional placeholder. |
| 2069 | if ( ! empty( $placeholder ) ) { |
| 2070 | $output .= sprintf( '<option value="" class="placeholder">%s</option>', esc_html( $placeholder ) ); |
| 2071 | } |
| 2072 | |
| 2073 | // Build the select options (even though user can only see 1st option). |
| 2074 | foreach ( $values as $value ) { |
| 2075 | $default = isset( $value['default'] ) ? (bool) $value['default'] : false; |
| 2076 | $selected = ! empty( $placeholder ) && empty( $multiple ) ? '' : selected( true, $default, false ); |
| 2077 | $output .= sprintf( '<option %s>%s</option>', $selected, esc_html( $value['label'] ) ); |
| 2078 | } |
| 2079 | |
| 2080 | $output .= '</select>'; |
| 2081 | } |
| 2082 | } else { |
| 2083 | |
| 2084 | if ( $echo ) { |
| 2085 | echo sprintf( '<ul class="%s">', esc_attr( evf_sanitize_classes( $list_class, true ) ) ); |
| 2086 | |
| 2087 | // Individual checkbox/radio options. |
| 2088 | foreach ( $values as $value ) { |
| 2089 | $default = isset( $value['default'] ) ? $value['default'] : ''; |
| 2090 | $selected = checked( '1', $default, false ); |
| 2091 | $placeholder = evf()->plugin_url( 'assets/images/everest-forms-placeholder.png' ); |
| 2092 | $image_src = ! empty( $value['image'] ) ? esc_url( $value['image'] ) : $placeholder; |
| 2093 | $item_class = array(); |
| 2094 | |
| 2095 | if ( ! empty( $value['default'] ) ) { |
| 2096 | $item_class[] = 'everest-forms-selected'; |
| 2097 | } |
| 2098 | |
| 2099 | if ( $choices_images ) { |
| 2100 | $item_class[] = 'everest-forms-image-choices-item'; |
| 2101 | } |
| 2102 | |
| 2103 | echo sprintf( '<li class="%s">', esc_attr( evf_sanitize_classes( $item_class, true ) ) ); |
| 2104 | |
| 2105 | if ( $choices_images ) { |
| 2106 | echo '<label>'; |
| 2107 | echo sprintf( '<span class="everest-forms-image-choices-image"><img src="%s" alt="%s"%s></span>', esc_url( $image_src ), esc_attr( $value['label'] ), ( ! empty( $value['label'] ) ? ' title="' . esc_attr( $value['label'] ) . '"' : '' ) ); |
| 2108 | echo sprintf( '<input type="%s" %s disabled>', esc_attr( $type ), esc_attr( $selected ) ); |
| 2109 | if ( ( 'payment-checkbox' === $field['type'] ) || ( 'payment-multiple' === $field['type'] ) ) { |
| 2110 | echo '<span class="everest-forms-image-choices-label">' . esc_html( $value['label'] . '-' . evf_format_amount( evf_sanitize_amount( $value['value'] ), true ) ) . '</span>'; |
| 2111 | } else { |
| 2112 | echo '<span class="everest-forms-image-choices-label">' . esc_html( $value['label'] ) . '</span>'; |
| 2113 | } |
| 2114 | echo '</label>'; |
| 2115 | } else { |
| 2116 | if ( ( 'payment-checkbox' === $field['type'] ) || ( 'payment-multiple' === $field['type'] ) ) { |
| 2117 | echo sprintf( '<input type="%s" %s disabled>%s - %s', esc_attr( $type ), esc_attr( $selected ), esc_html( $value['label'] ), esc_attr( evf_format_amount( evf_sanitize_amount( $value['value'] ) ), true ) ); |
| 2118 | } else { |
| 2119 | echo sprintf( '<input type="%s" %s disabled>%s', esc_attr( $type ), esc_attr( $selected ), esc_html( $value['label'] ) ); |
| 2120 | } |
| 2121 | } |
| 2122 | |
| 2123 | echo '</li>'; |
| 2124 | } |
| 2125 | |
| 2126 | echo '</ul>'; |
| 2127 | |
| 2128 | } else { |
| 2129 | $output = sprintf( '<ul class="%s">', evf_sanitize_classes( $list_class, true ) ); |
| 2130 | |
| 2131 | // Individual checkbox/radio options. |
| 2132 | foreach ( $values as $value ) { |
| 2133 | $default = isset( $value['default'] ) ? $value['default'] : ''; |
| 2134 | $selected = checked( '1', $default, false ); |
| 2135 | $placeholder = wp_remote_get( evf()->plugin_url( 'assets/images/everest-forms-placeholder.png' ), array( 'sslverify' => false ) ); |
| 2136 | $image_src = ! empty( $value['image'] ) ? esc_url( $value['image'] ) : $placeholder; |
| 2137 | $item_class = array(); |
| 2138 | |
| 2139 | if ( ! empty( $value['default'] ) ) { |
| 2140 | $item_class[] = 'everest-forms-selected'; |
| 2141 | } |
| 2142 | |
| 2143 | if ( $choices_images ) { |
| 2144 | $item_class[] = 'everest-forms-image-choices-item'; |
| 2145 | } |
| 2146 | |
| 2147 | $output .= sprintf( '<li class="%s">', evf_sanitize_classes( $item_class, true ) ); |
| 2148 | |
| 2149 | if ( $choices_images ) { |
| 2150 | $output .= '<label>'; |
| 2151 | $output .= sprintf( '<span class="everest-forms-image-choices-image"><img src="%s" alt="%s"%s></span>', $image_src, esc_attr( $value['label'] ), ! empty( $value['label'] ) ? ' title="' . esc_attr( $value['label'] ) . '"' : '' ); |
| 2152 | $output .= sprintf( '<input type="%s" %s disabled>', $type, $selected ); |
| 2153 | if ( ( 'payment-checkbox' === $field['type'] ) || ( 'payment-multiple' === $field['type'] ) ) { |
| 2154 | $output .= '<span class="everest-forms-image-choices-label">' . wp_kses_post( $value['label'] ) . '-' . evf_format_amount( evf_sanitize_amount( $value['value'] ), true ) . '</span>'; |
| 2155 | } else { |
| 2156 | $output .= '<span class="everest-forms-image-choices-label">' . wp_kses_post( $value['label'] ) . '</span>'; |
| 2157 | } |
| 2158 | $output .= '</label>'; |
| 2159 | } else { |
| 2160 | if ( ( 'payment-checkbox' === $field['type'] ) || ( 'payment-multiple' === $field['type'] ) ) { |
| 2161 | $output .= sprintf( '<input type="%s" %s disabled>%s - %s', $type, $selected, $value['label'], evf_format_amount( evf_sanitize_amount( $value['value'] ), true ) ); |
| 2162 | } else { |
| 2163 | $output .= sprintf( '<input type="%s" %s disabled>%s', $type, $selected, $value['label'] ); |
| 2164 | } |
| 2165 | } |
| 2166 | |
| 2167 | $output .= '</li>'; |
| 2168 | } |
| 2169 | |
| 2170 | $output .= '</ul>'; |
| 2171 | } |
| 2172 | } |
| 2173 | break; |
| 2174 | } |
| 2175 | |
| 2176 | if ( $echo && in_array( $option, array( 'basic-options', 'advanced-options' ), true ) && 'open' === $markup ) { |
| 2177 | do_action( "everest_forms_field_options_top_{$option}", $field, $this ); |
| 2178 | } elseif ( $echo && in_array( $option, array( 'basic-options', 'advanced-options' ), true ) && 'close' === $markup ) { |
| 2179 | do_action( "everest_forms_field_options_after_{$option}", $field, $this ); |
| 2180 | } |
| 2181 | |
| 2182 | if ( ! $echo ) { |
| 2183 | return $output; |
| 2184 | } |
| 2185 | } |
| 2186 | |
| 2187 | /** |
| 2188 | * Create a new field in the admin AJAX editor. |
| 2189 | * |
| 2190 | * @since 1.0.0 |
| 2191 | */ |
| 2192 | public function field_new() { |
| 2193 | // Run a security check. |
| 2194 | check_ajax_referer( 'everest_forms_field_drop', 'security' ); |
| 2195 | |
| 2196 | // Check for form ID. |
| 2197 | if ( ! isset( $_POST['form_id'] ) || empty( $_POST['form_id'] ) ) { |
| 2198 | die( esc_html__( 'No form ID found', 'everest-forms' ) ); |
| 2199 | } |
| 2200 | |
| 2201 | // Check for permissions. |
| 2202 | if ( ! current_user_can( 'everest_forms_edit_form', (int) $_POST['form_id'] ) ) { |
| 2203 | die( esc_html__( 'You don\'t have permission.', 'everest-forms' ) ); |
| 2204 | } |
| 2205 | |
| 2206 | // Check for field type to add. |
| 2207 | if ( ! isset( $_POST['field_type'] ) || empty( $_POST['field_type'] ) ) { |
| 2208 | die( esc_html__( 'No field type found', 'everest-forms' ) ); |
| 2209 | } |
| 2210 | |
| 2211 | // Grab field data. |
| 2212 | $field_args = ! empty( $_POST['defaults'] ) && is_array( $_POST['defaults'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['defaults'] ) ) : array(); |
| 2213 | $field_type = esc_attr( sanitize_text_field( wp_unslash( $_POST['field_type'] ) ) ); |
| 2214 | $field_id = evf()->form->field_unique_key( sanitize_text_field( wp_unslash( $_POST['form_id'] ) ) ); |
| 2215 | $field = array( |
| 2216 | 'id' => $field_id, |
| 2217 | 'type' => $field_type, |
| 2218 | 'label' => $this->name, |
| 2219 | 'description' => '', |
| 2220 | ); |
| 2221 | $field = wp_parse_args( $field_args, $field ); |
| 2222 | $field = apply_filters( 'everest_forms_field_new_default', $field ); |
| 2223 | $field_required = apply_filters( 'everest_forms_field_new_required', '', $field ); |
| 2224 | $field_class = apply_filters( 'everest_forms_field_new_class', '', $field ); |
| 2225 | |
| 2226 | // Field types that default to required. |
| 2227 | if ( ! empty( $field_required ) ) { |
| 2228 | $field_required = 'required'; |
| 2229 | $field['required'] = '1'; |
| 2230 | } |
| 2231 | |
| 2232 | // Build Preview. |
| 2233 | ob_start(); |
| 2234 | $this->field_preview( $field ); |
| 2235 | $preview = sprintf( '<div class="everest-forms-field everest-forms-field-%s %s %s" id="everest-forms-field-%s" data-field-id="%s" data-field-type="%s">', $field_type, $field_required, $field_class, $field['id'], $field['id'], $field_type ); |
| 2236 | $preview .= sprintf( '<div class="evf-field-action">' ); |
| 2237 | if ( 'repeater-fields' !== $field_type ) { |
| 2238 | $preview .= sprintf( '<a href="#" class="everest-forms-field-duplicate" title="%s"><span class="dashicons dashicons-media-default"></span></a>', __( 'Duplicate Field', 'everest-forms' ) ); |
| 2239 | $preview .= sprintf( '<a href="#" class="everest-forms-field-delete" title="%s"><span class="dashicons dashicons-trash"></span></a>', __( 'Delete Field', 'everest-forms' ) ); |
| 2240 | $preview .= sprintf( '<a href="#" class="everest-forms-field-setting" title="%s"><span class="dashicons dashicons-admin-generic"></span></a>', __( 'Settings', 'everest-forms' ) ); |
| 2241 | } else { |
| 2242 | $preview .= sprintf( '<a href="#" class="evf-duplicate-row" title="%s"><span class="dashicons dashicons-media-default"></span></a>', esc_html__( 'Duplicate Field', 'everest-forms' ) ); |
| 2243 | $preview .= sprintf( '<a href="#" class="evf-delete-row" title="%s"><span class="dashicons dashicons-trash"></span></a>', esc_html__( 'Delete Field', 'everest-forms' ) ); |
| 2244 | } |
| 2245 | $preview .= sprintf( '</div>' ); |
| 2246 | $preview .= ob_get_clean(); |
| 2247 | $preview .= '</div>'; |
| 2248 | |
| 2249 | // Build Options. |
| 2250 | $options = sprintf( '<div class="everest-forms-field-option everest-forms-field-option-%s" id="everest-forms-field-option-%s" data-field-id="%s">', esc_attr( $field['type'] ), $field['id'], $field['id'] ); |
| 2251 | $options .= sprintf( '<input type="hidden" name="form_fields[%s][id]" value="%s" class="everest-forms-field-option-hidden-id">', $field['id'], $field['id'] ); |
| 2252 | $options .= sprintf( '<input type="hidden" name="form_fields[%s][type]" value="%s" class="everest-forms-field-option-hidden-type">', $field['id'], esc_attr( $field['type'] ) ); |
| 2253 | ob_start(); |
| 2254 | $this->field_options( $field ); |
| 2255 | $options .= ob_get_clean(); |
| 2256 | $options .= '</div>'; |
| 2257 | |
| 2258 | $form_field_array = explode( '-', $field_id ); |
| 2259 | $field_id_int = absint( $form_field_array[ count( $form_field_array ) - 1 ] ); |
| 2260 | |
| 2261 | // Prepare to return compiled results. |
| 2262 | wp_send_json_success( |
| 2263 | array( |
| 2264 | 'form_id' => (int) $_POST['form_id'], |
| 2265 | 'field' => $field, |
| 2266 | 'preview' => $preview, |
| 2267 | 'options' => $options, |
| 2268 | 'form_field_id' => ( $field_id_int + 1 ), |
| 2269 | ) |
| 2270 | ); |
| 2271 | } |
| 2272 | |
| 2273 | /** |
| 2274 | * Field display on the form front-end. |
| 2275 | * |
| 2276 | * @since 1.0.0 |
| 2277 | * |
| 2278 | * @param array $field Field Data. |
| 2279 | * @param array $field_atts Field attributes. |
| 2280 | * @param array $form_data All Form Data. |
| 2281 | */ |
| 2282 | public function field_display( $field, $field_atts, $form_data ) {} |
| 2283 | |
| 2284 | /** |
| 2285 | * Edit form field display on the entry back-end. |
| 2286 | * |
| 2287 | * @since 1.7.0 |
| 2288 | * |
| 2289 | * @param array $entry_field Entry field data. |
| 2290 | * @param array $field Field data. |
| 2291 | * @param array $form_data Form data and settings. |
| 2292 | */ |
| 2293 | public function edit_form_field_display( $entry_field, $field, $form_data ) { |
| 2294 | |
| 2295 | if ( 'repeater_fields' === $field['type'] ) { |
| 2296 | return; |
| 2297 | } |
| 2298 | |
| 2299 | $value = isset( $entry_field['value'] ) ? $entry_field['value'] : ''; |
| 2300 | |
| 2301 | if ( '' !== $value ) { |
| 2302 | $field['properties'] = $this->get_single_field_property_value( $value, 'primary', $field['properties'], $field ); |
| 2303 | } |
| 2304 | |
| 2305 | $this->field_display( $field, null, $form_data ); |
| 2306 | } |
| 2307 | |
| 2308 | /** |
| 2309 | * Get the value to prefill, based on field data and current properties. |
| 2310 | * |
| 2311 | * @since 1.7.0 |
| 2312 | * |
| 2313 | * @param string $raw_value Raw Value, always a string. |
| 2314 | * @param string $input Subfield inside the field. |
| 2315 | * @param array $properties Field properties. |
| 2316 | * @param array $field Field specific data. |
| 2317 | * |
| 2318 | * @return array Modified field properties. |
| 2319 | */ |
| 2320 | public function get_single_field_property_value( $raw_value, $input, $properties, $field ) { |
| 2321 | if ( ! is_string( $raw_value ) ) { |
| 2322 | return $properties; |
| 2323 | } |
| 2324 | |
| 2325 | $get_value = wp_unslash( sanitize_text_field( $raw_value ) ); |
| 2326 | |
| 2327 | if ( ! empty( $field['choices'] ) && is_array( $field['choices'] ) ) { |
| 2328 | $properties = $this->get_single_field_property_value_choices( $get_value, $properties, $field ); |
| 2329 | } else { |
| 2330 | if ( |
| 2331 | ! empty( $input ) && |
| 2332 | isset( $properties['inputs'][ $input ] ) |
| 2333 | ) { |
| 2334 | $properties['inputs'][ $input ]['attr']['value'] = $get_value; |
| 2335 | |
| 2336 | // Update data attributes depending on the field type. |
| 2337 | if ( isset( $field['type'] ) && 'range-slider' === $field['type'] ) { |
| 2338 | $properties['inputs'][ $input ]['data']['from'] = $get_value; |
| 2339 | } |
| 2340 | } |
| 2341 | } |
| 2342 | |
| 2343 | return $properties; |
| 2344 | } |
| 2345 | |
| 2346 | /** |
| 2347 | * Get the value to prefill for choices section, based on field data and current properties. |
| 2348 | * |
| 2349 | * @since 1.7.0 |
| 2350 | * |
| 2351 | * @param string $get_value Requested value. |
| 2352 | * @param array $properties Field properties. |
| 2353 | * @param array $field Field specific data. |
| 2354 | * |
| 2355 | * @return array Modified field properties. |
| 2356 | */ |
| 2357 | protected function get_single_field_property_value_choices( $get_value, $properties, $field ) { |
| 2358 | $default_key = null; |
| 2359 | |
| 2360 | // For fields with normal choices, we need dafault key. |
| 2361 | foreach ( $field['choices'] as $choice_key => $choice_arr ) { |
| 2362 | $choice_value_key = isset( $field['show_values'] ) ? 'value' : 'label'; |
| 2363 | if ( |
| 2364 | isset( $choice_arr[ $choice_value_key ] ) && |
| 2365 | strtoupper( sanitize_text_field( $choice_arr[ $choice_value_key ] ) ) === strtoupper( $get_value ) |
| 2366 | ) { |
| 2367 | $default_key = $choice_key; |
| 2368 | break; |
| 2369 | } |
| 2370 | } |
| 2371 | |
| 2372 | // Redefine selected choice. |
| 2373 | if ( null !== $default_key ) { |
| 2374 | foreach ( $field['choices'] as $choice_key => $choice_arr ) { |
| 2375 | if ( $choice_key === $default_key ) { |
| 2376 | $properties['inputs'][ $choice_key ]['default'] = true; |
| 2377 | $properties['inputs'][ $choice_key ]['container']['class'][] = 'everest-forms-selected'; |
| 2378 | break; |
| 2379 | } |
| 2380 | } |
| 2381 | } |
| 2382 | |
| 2383 | return $properties; |
| 2384 | } |
| 2385 | |
| 2386 | /** |
| 2387 | * Remove all admin-defined defaults from choices-related fields only. |
| 2388 | * |
| 2389 | * @since 1.7.0 |
| 2390 | * |
| 2391 | * @param array $field Field data and settings. |
| 2392 | * @param array $properties Field Properties to be modified. |
| 2393 | */ |
| 2394 | protected function remove_field_choices_defaults( $field, &$properties ) { |
| 2395 | if ( ! empty( $field['choices'] ) ) { |
| 2396 | array_walk_recursive( |
| 2397 | $properties['inputs'], |
| 2398 | function ( &$value, $key ) { |
| 2399 | if ( 'default' === $key ) { |
| 2400 | $value = false; |
| 2401 | } |
| 2402 | if ( 'everest-forms-selected' === $value ) { |
| 2403 | $value = ''; |
| 2404 | } |
| 2405 | } |
| 2406 | ); |
| 2407 | } |
| 2408 | } |
| 2409 | |
| 2410 | /** |
| 2411 | * Display field input errors if present. |
| 2412 | * |
| 2413 | * @since 1.0.0 |
| 2414 | * |
| 2415 | * @param string $key Input key. |
| 2416 | * @param array $field Field data and settings. |
| 2417 | */ |
| 2418 | public function field_display_error( $key, $field ) { |
| 2419 | // Need an error. |
| 2420 | if ( empty( $field['properties']['error']['value'][ $key ] ) ) { |
| 2421 | return; |
| 2422 | } |
| 2423 | |
| 2424 | printf( |
| 2425 | '<label class="everest-forms-error evf-error" for="%s">%s</label>', |
| 2426 | esc_attr( $field['properties']['inputs'][ $key ]['id'] ), |
| 2427 | esc_html( $field['properties']['error']['value'][ $key ] ) |
| 2428 | ); |
| 2429 | } |
| 2430 | |
| 2431 | /** |
| 2432 | * Display field input sublabel if present. |
| 2433 | * |
| 2434 | * @since 1.0.0 |
| 2435 | * |
| 2436 | * @param string $key Input key. |
| 2437 | * @param string $position Sublabel position. |
| 2438 | * @param array $field Field data and settings. |
| 2439 | */ |
| 2440 | public function field_display_sublabel( $key, $position, $field ) { |
| 2441 | // Need a sublabel value. |
| 2442 | if ( empty( $field['properties']['inputs'][ $key ]['sublabel']['value'] ) ) { |
| 2443 | return; |
| 2444 | } |
| 2445 | |
| 2446 | $pos = ! empty( $field['properties']['inputs'][ $key ]['sublabel']['position'] ) ? $field['properties']['inputs'][ $key ]['sublabel']['position'] : 'after'; |
| 2447 | $hidden = ! empty( $field['properties']['inputs'][ $key ]['sublabel']['hidden'] ) ? 'everest-forms-sublabel-hide' : ''; |
| 2448 | |
| 2449 | if ( $pos !== $position ) { |
| 2450 | return; |
| 2451 | } |
| 2452 | |
| 2453 | printf( |
| 2454 | '<label for="%s" class="everest-forms-field-sublabel %s %s">%s</label>', |
| 2455 | esc_attr( $field['properties']['inputs'][ $key ]['id'] ), |
| 2456 | sanitize_html_class( $pos ), |
| 2457 | esc_html( $hidden ), |
| 2458 | esc_html( evf_string_translation( (int) $this->form_data['id'], $field['id'], $field['properties']['inputs'][ $key ]['sublabel']['value'], '-sublabel-' . $key ) ) |
| 2459 | ); |
| 2460 | } |
| 2461 | |
| 2462 | /** |
| 2463 | * Validates field on form submit. |
| 2464 | * |
| 2465 | * @since 1.0.0 |
| 2466 | * |
| 2467 | * @param string $field_id Field Id. |
| 2468 | * @param string|array $field_submit Submitted Data. |
| 2469 | * @param array $form_data All Form Data. |
| 2470 | */ |
| 2471 | public function validate( $field_id, $field_submit, $form_data ) { |
| 2472 | $field_type = isset( $form_data['form_fields'][ $field_id ]['type'] ) ? $form_data['form_fields'][ $field_id ]['type'] : ''; |
| 2473 | $required_field = isset( $form_data['form_fields'][ $field_id ]['required'] ) ? $form_data['form_fields'][ $field_id ]['required'] : false; |
| 2474 | $field = isset( $form_data['form_fields'][ $field_id ] ) ? $form_data['form_fields'][ $field_id ] : ''; |
| 2475 | $required_validation = get_option( 'everest_forms_required_validation' ); |
| 2476 | if ( in_array( $field_type, array( 'number', 'email', 'url', 'phone' ), true ) ) { |
| 2477 | $required_validation = get_option( 'everest_forms_' . $field['type'] . '_validation' ); |
| 2478 | } |
| 2479 | $required_message = isset( $form_data['form_fields'][ $field_id ]['required-field-message'], $form_data['form_fields'][ $field_id ]['required_field_message_setting'] ) && ! empty( $form_data['form_fields'][ $field_id ]['required-field-message'] ) && 'individual' == $form_data['form_fields'][ $field_id ]['required_field_message_setting'] ? $form_data['form_fields'][ $field_id ]['required-field-message'] : $required_validation; |
| 2480 | $entry = $form_data['entry']; |
| 2481 | $visible = apply_filters( 'everest_forms_visible_fields', true, $form_data['form_fields'][ $field_id ], $entry, $form_data ); |
| 2482 | |
| 2483 | if ( false === $visible ) { |
| 2484 | return; |
| 2485 | } |
| 2486 | // Basic required check - If field is marked as required, check for entry data. |
| 2487 | if ( false !== $required_field && ( empty( $field_submit ) && '0' !== $field_submit ) ) { |
| 2488 | evf()->task->errors[ $form_data['id'] ][ $field_id ] = $required_message; |
| 2489 | update_option( 'evf_validation_error', 'yes' ); |
| 2490 | } |
| 2491 | |
| 2492 | // validate regex validation. |
| 2493 | if ( isset( $form_data['form_fields'][ $field_id ]['enable_regex_validation'] ) && '1' === $form_data['form_fields'][ $field_id ]['enable_regex_validation'] ) { |
| 2494 | $regex_value = ! empty( $form_data['form_fields'][ $field_id ]['regex_value'] ) ? $form_data['form_fields'][ $field_id ]['regex_value'] : ''; |
| 2495 | $regex_message = ! empty( $form_data['form_fields'][ $field_id ]['regex_message'] ) ? $form_data['form_fields'][ $field_id ]['regex_message'] : esc_html__( 'Please provide a valid value for this field', 'everest-forms' ); |
| 2496 | $value = ''; |
| 2497 | if ( is_array( $field_submit ) ) { |
| 2498 | $value = ! empty( $field_submit['primary'] ) ? $field_submit['primary'] : ''; |
| 2499 | } else { |
| 2500 | $value = ! empty( $field_submit ) ? $field_submit : ''; |
| 2501 | } |
| 2502 | if ( ! preg_match( '/' . $regex_value . '/', $value ) ) { |
| 2503 | evf()->task->errors[ $form_data['id'] ][ $field_id ] = $regex_message; |
| 2504 | update_option( 'evf_validation_error', 'yes' ); |
| 2505 | } |
| 2506 | } |
| 2507 | // Type validations. |
| 2508 | switch ( $field_type ) { |
| 2509 | case 'url': |
| 2510 | if ( ! empty( $_POST['everest_forms']['form_fields'][ $field_id ] ) && filter_var( $field_submit, FILTER_VALIDATE_URL ) === false ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 2511 | $validation_text = get_option( 'evf_' . $field_type . '_validation', esc_html__( 'Please enter a valid url', 'everest-forms' ) ); |
| 2512 | } |
| 2513 | |
| 2514 | break; |
| 2515 | case 'email': |
| 2516 | if ( is_array( $field_submit ) ) { |
| 2517 | $value = ! empty( $field_submit['primary'] ) ? $field_submit['primary'] : ''; |
| 2518 | } else { |
| 2519 | $value = ! empty( $field_submit ) ? $field_submit : ''; |
| 2520 | } |
| 2521 | if ( ! empty( $_POST['everest_forms']['form_fields'][ $field_id ] ) && ! is_email( $value ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 2522 | $validation_text = get_option( 'evf_' . $field_type . '_validation', esc_html__( 'Please enter a valid email address', 'everest-forms' ) ); |
| 2523 | } |
| 2524 | break; |
| 2525 | case 'number': |
| 2526 | if ( ! empty( $_POST['everest_forms']['form_fields'][ $field_id ] ) && ! is_numeric( $field_submit ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 2527 | $validation_text = get_option( 'evf_' . $field_type . '_validation', esc_html__( 'Please enter a valid number', 'everest-forms' ) ); |
| 2528 | } |
| 2529 | break; |
| 2530 | case 'textarea': |
| 2531 | case 'text': |
| 2532 | if ( ! empty( $_POST['everest_forms']['form_fields'][ $field_id ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 2533 | |
| 2534 | // Limit Length. |
| 2535 | if ( isset( $field['limit_enabled'], $field['limit_mode'], $field['limit_count'] ) && '1' === $field['limit_enabled'] && in_array( $field['limit_mode'], array( 'characters', 'words' ), true ) && ! empty( $field['limit_count'] ) ) { |
| 2536 | if ( 'words' === $field['limit_mode'] && $field['limit_count'] < evf_word_count( $field_submit ) ) { |
| 2537 | /* translators: %s Number of max words. */ |
| 2538 | $validation_text = sprintf( esc_html__( 'This field contains at most %s words', 'everest-forms' ), $field['limit_count'] ); |
| 2539 | } elseif ( 'characters' === $field['limit_mode'] && $field['limit_count'] < mb_strlen( $field_submit ) ) { //phpcs:ignore PHPCompatibility.ParameterValues.NewIconvMbstringCharsetDefault.NotSet |
| 2540 | /* translators: %s Number of max characters. */ |
| 2541 | $validation_text = sprintf( esc_html__( 'This field contains at most %s characters', 'everest-forms' ), $field['limit_count'] ); |
| 2542 | } |
| 2543 | } |
| 2544 | |
| 2545 | // Min Length. |
| 2546 | if ( isset( $field['min_length_enabled'], $field['min_length_mode'], $field['min_length_count'] ) && '1' === $field['min_length_enabled'] && in_array( $field['min_length_mode'], array( 'characters', 'words' ), true ) && ! empty( $field['min_length_count'] ) ) { |
| 2547 | |
| 2548 | if ( 'words' === $field['min_length_mode'] && $field['min_length_count'] > evf_word_count( $field_submit ) ) { |
| 2549 | /* translators: %s Number of minimum words. */ |
| 2550 | $validation_text = sprintf( esc_html__( 'This field contains at least %s words', 'everest-forms' ), $field['min_length_count'] ); |
| 2551 | } elseif ( 'characters' === $field['min_length_mode'] && $field['min_length_count'] > mb_strlen( $field_submit ) ) { //phpcs:ignore PHPCompatibility.ParameterValues.NewIconvMbstringCharsetDefault.NotSet |
| 2552 | /* translators: %s Number of minimum characters. */ |
| 2553 | $validation_text = sprintf( esc_html__( 'This field contains at least %s characters', 'everest-forms' ), $field['min_length_count'] ); |
| 2554 | } |
| 2555 | } |
| 2556 | } |
| 2557 | |
| 2558 | break; |
| 2559 | case 'date-time': |
| 2560 | $slot_booking = isset( $form_data['form_fields'][ $field_id ]['slot_booking_advanced'] ) ? $form_data['form_fields'][ $field_id ]['slot_booking_advanced'] : ''; |
| 2561 | if ( $slot_booking ) { |
| 2562 | $datetime_format = isset( $form_data['form_fields'][ $field_id ]['datetime_format'] ) ? $form_data['form_fields'][ $field_id ]['datetime_format'] : ''; |
| 2563 | $date_format = isset( $form_data['form_fields'][ $field_id ]['date_format'] ) ? $form_data['form_fields'][ $field_id ]['date_format'] : ''; |
| 2564 | $mode = isset( $form_data['form_fields'][ $field_id ]['date_mode'] ) ? $form_data['form_fields'][ $field_id ]['date_mode'] : ''; |
| 2565 | $time_interval = isset( $form_data['form_fields'][ $field_id ]['time_interval'] ) ? $form_data['form_fields'][ $field_id ]['time_interval'] : ''; |
| 2566 | $datetime_arr = parse_datetime_values( $field_submit, $datetime_format, $date_format, $mode, $time_interval ); |
| 2567 | $booked_slot = maybe_unserialize( get_option( 'evf_booked_slot', '' ) ); |
| 2568 | $form_id = $form_data['id']; |
| 2569 | $is_booked = false; |
| 2570 | if ( ! empty( $booked_slot ) && array_key_exists( $form_id, $booked_slot ) ) { |
| 2571 | foreach ( $datetime_arr as $arr ) { |
| 2572 | |
| 2573 | foreach ( $booked_slot[ $form_id ] as $slot ) { |
| 2574 | if ( $arr[0] >= $slot[0] && $arr[1] <= $slot[1] ) { |
| 2575 | $is_booked = true; |
| 2576 | break; |
| 2577 | } elseif ( $arr[0] >= $slot[0] && $arr[0] < $slot[1] && $arr[1] >= $slot[1] ) { |
| 2578 | $is_booked = true; |
| 2579 | break; |
| 2580 | } |
| 2581 | } |
| 2582 | } |
| 2583 | } |
| 2584 | if ( $is_booked ) { |
| 2585 | $validation_text = get_option( 'evf_' . $field_type . '_validation', esc_html__( 'This slot is already booked. Please choose other slot.', 'everest-forms' ) ); |
| 2586 | } |
| 2587 | } |
| 2588 | break; |
| 2589 | } |
| 2590 | |
| 2591 | if ( isset( $validation_text ) ) { |
| 2592 | evf()->task->errors[ $form_data['id'] ][ $field_id ] = apply_filters( 'everest_forms_type_validation', $validation_text ); |
| 2593 | update_option( 'evf_validation_error', 'yes' ); |
| 2594 | } |
| 2595 | } |
| 2596 | |
| 2597 | /** |
| 2598 | * Formats and sanitizes field. |
| 2599 | * |
| 2600 | * @since 1.0.0 |
| 2601 | * |
| 2602 | * @param int $field_id Field ID. |
| 2603 | * @param mixed $field_submit Submitted field value. |
| 2604 | * @param array $form_data Form data and settings. |
| 2605 | * @param string $meta_key Field meta key. |
| 2606 | */ |
| 2607 | public function format( $field_id, $field_submit, $form_data, $meta_key ) { |
| 2608 | if ( is_array( $field_submit ) ) { |
| 2609 | $field_submit = array_filter( $field_submit ); |
| 2610 | $field_submit = implode( "\r\n", $field_submit ); |
| 2611 | } |
| 2612 | |
| 2613 | $name = ! empty( $form_data['form_fields'][ $field_id ]['label'] ) ? make_clickable( $form_data['form_fields'][ $field_id ]['label'] ) : ''; |
| 2614 | |
| 2615 | // Sanitize but keep line breaks. |
| 2616 | $value = evf_sanitize_textarea_field( $field_submit ); |
| 2617 | |
| 2618 | evf()->task->form_fields[ $field_id ] = array( |
| 2619 | 'name' => $name, |
| 2620 | 'value' => $value, |
| 2621 | 'id' => $field_id, |
| 2622 | 'type' => $this->type, |
| 2623 | 'meta_key' => $meta_key, |
| 2624 | ); |
| 2625 | } |
| 2626 | |
| 2627 | /** |
| 2628 | * Field with limit. |
| 2629 | * |
| 2630 | * @param array $field Field to check. |
| 2631 | * @return boolean |
| 2632 | */ |
| 2633 | protected function field_is_limit( $field ) { |
| 2634 | if ( in_array( $field['type'], array( 'text', 'textarea' ), true ) ) { |
| 2635 | return ( isset( $field['limit_enabled'] ) && ! empty( $field['limit_count'] ) ) || ( isset( $field['min_length_enabled'] ) && ! empty( $field['min_length_count'] ) ); |
| 2636 | } |
| 2637 | } |
| 2638 | |
| 2639 | /** |
| 2640 | * Filter callback for outputting formatted data. |
| 2641 | * |
| 2642 | * @param array $field Field Data. |
| 2643 | */ |
| 2644 | public function field_exporter( $field ) { |
| 2645 | $export = array(); |
| 2646 | |
| 2647 | switch ( $this->type ) { |
| 2648 | case 'radio': |
| 2649 | case 'signature': |
| 2650 | case 'payment-multiple': |
| 2651 | $value = ''; |
| 2652 | $image = ! empty( $field['value']['image'] ) ? sprintf( '<img src="%s" style="width:75px;height:75px;max-height:75px;max-width:75px;" /><br>', $field['value']['image'] ) : ''; |
| 2653 | $filtered_choice = apply_filters( 'evf_custom_choice', false ); |
| 2654 | if ( $filtered_choice ) { |
| 2655 | $value = ! empty( $field['value']['label'] ) ? $field['value']['label'] : ''; |
| 2656 | } else { |
| 2657 | $value = ! empty( $field['value']['label'] ) ? $image . $field['value']['label'] : ''; |
| 2658 | } |
| 2659 | $export = array( |
| 2660 | 'label' => ! empty( $field['value']['name'] ) ? $field['value']['name'] : ucfirst( str_replace( '_', ' ', $field['type'] ) ) . " - {$field['id']}", |
| 2661 | 'value' => ! empty( $value ) ? $value : false, |
| 2662 | ); |
| 2663 | break; |
| 2664 | case 'checkbox': |
| 2665 | case 'payment-checkbox': |
| 2666 | $value = array(); |
| 2667 | |
| 2668 | if ( count( $field['value'] ) ) { |
| 2669 | foreach ( $field['value']['label'] as $key => $choice ) { |
| 2670 | $image = ! empty( $field['value']['images'][ $key ] ) ? sprintf( '<img src="%s" style="width:75px;height:75px;max-height:75px;max-width:75px;" /><br>', $field['value']['images'][ $key ] ) : ''; |
| 2671 | |
| 2672 | if ( ! empty( $choice ) ) { |
| 2673 | $filtered_choice = apply_filters( 'evf_custom_choice', false ); |
| 2674 | if ( $filtered_choice ) { |
| 2675 | $value[ $key ] = $choice; |
| 2676 | } else { |
| 2677 | $value[ $key ] = $image . $choice; |
| 2678 | } |
| 2679 | } |
| 2680 | } |
| 2681 | } |
| 2682 | $export = array( |
| 2683 | 'label' => ! empty( $field['value']['name'] ) ? $field['value']['name'] : ucfirst( str_replace( '_', ' ', $field['type'] ) ) . " - {$field['id']}", |
| 2684 | 'value' => is_array( $value ) ? implode( '<br>', array_values( $value ) ) : false, |
| 2685 | ); |
| 2686 | break; |
| 2687 | default: |
| 2688 | $export = array( |
| 2689 | 'label' => ! empty( $field['name'] ) ? $field['name'] : ucfirst( str_replace( '_', ' ', $field['type'] ) ) . " - {$field['id']}", |
| 2690 | 'value' => ! empty( $field['value'] ) ? ( is_array( $field['value'] ) ? $this->implode_recursive( $field['value'] ) : $field['value'] ) : false, |
| 2691 | ); |
| 2692 | } |
| 2693 | |
| 2694 | return $export; |
| 2695 | } |
| 2696 | |
| 2697 | /** |
| 2698 | * Recursively process an array with an implosion. |
| 2699 | * |
| 2700 | * @since 1.6.6 |
| 2701 | * |
| 2702 | * @param array $array Array that needs to be recursively imploded. |
| 2703 | * @param string $delimiter Delimiter for the implosion - defaults to <br>. |
| 2704 | * |
| 2705 | * @return string $output Imploded array. |
| 2706 | */ |
| 2707 | protected function implode_recursive( $array, $delimiter = '<br>' ) { |
| 2708 | $output = ''; |
| 2709 | |
| 2710 | foreach ( $array as $tuple ) { |
| 2711 | if ( is_array( $tuple ) ) { |
| 2712 | $output .= $this->implode_recursive( $tuple, ' ' ); |
| 2713 | } elseif ( ! empty( $tuple ) ) { |
| 2714 | $output .= $delimiter . $tuple; |
| 2715 | } |
| 2716 | } |
| 2717 | |
| 2718 | return $output; |
| 2719 | } |
| 2720 | } |
| 2721 |