class-evf-field-address.php
7 years ago
class-evf-field-checkbox.php
6 years ago
class-evf-field-country.php
7 years ago
class-evf-field-credit-card.php
7 years ago
class-evf-field-date-time.php
6 years ago
class-evf-field-email.php
6 years ago
class-evf-field-file-upload.php
6 years ago
class-evf-field-first-name.php
6 years ago
class-evf-field-hidden.php
7 years ago
class-evf-field-html.php
7 years ago
class-evf-field-image-upload.php
7 years ago
class-evf-field-last-name.php
6 years ago
class-evf-field-likert.php
7 years ago
class-evf-field-number.php
6 years ago
class-evf-field-password.php
7 years ago
class-evf-field-payment-checkbox.php
7 years ago
class-evf-field-payment-quantity.php
6 years ago
class-evf-field-payment-radio.php
7 years ago
class-evf-field-payment-single.php
7 years ago
class-evf-field-payment-total.php
6 years ago
class-evf-field-phone.php
7 years ago
class-evf-field-radio.php
6 years ago
class-evf-field-rating.php
7 years ago
class-evf-field-scale-rating.php
7 years ago
class-evf-field-select.php
6 years ago
class-evf-field-signature.php
7 years ago
class-evf-field-text.php
6 years ago
class-evf-field-textarea.php
6 years ago
class-evf-field-title.php
6 years ago
class-evf-field-url.php
6 years ago
class-evf-field-checkbox.php
506 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Checkbox field. |
| 4 | * |
| 5 | * @package EverestForms\Fields |
| 6 | * @since 1.0.0 |
| 7 | */ |
| 8 | |
| 9 | defined( 'ABSPATH' ) || exit; |
| 10 | |
| 11 | /** |
| 12 | * EVF_Field_Checkbox class. |
| 13 | */ |
| 14 | class EVF_Field_Checkbox extends EVF_Form_Fields { |
| 15 | |
| 16 | /** |
| 17 | * Constructor. |
| 18 | */ |
| 19 | public function __construct() { |
| 20 | $this->name = esc_html__( 'Checkboxes', 'everest-forms' ); |
| 21 | $this->type = 'checkbox'; |
| 22 | $this->icon = 'evf-icon evf-icon-checkbox'; |
| 23 | $this->order = 70; |
| 24 | $this->group = 'general'; |
| 25 | $this->defaults = array( |
| 26 | 1 => array( |
| 27 | 'label' => esc_html__( 'First Choice', 'everest-forms' ), |
| 28 | 'value' => '', |
| 29 | 'image' => '', |
| 30 | 'default' => '', |
| 31 | ), |
| 32 | 2 => array( |
| 33 | 'label' => esc_html__( 'Second Choice', 'everest-forms' ), |
| 34 | 'value' => '', |
| 35 | 'image' => '', |
| 36 | 'default' => '', |
| 37 | ), |
| 38 | 3 => array( |
| 39 | 'label' => esc_html__( 'Third Choice', 'everest-forms' ), |
| 40 | 'value' => '', |
| 41 | 'image' => '', |
| 42 | 'default' => '', |
| 43 | ), |
| 44 | ); |
| 45 | $this->settings = array( |
| 46 | 'basic-options' => array( |
| 47 | 'field_options' => array( |
| 48 | 'label', |
| 49 | 'meta', |
| 50 | 'choices', |
| 51 | 'choices_images', |
| 52 | 'description', |
| 53 | 'required', |
| 54 | 'required_field_message', |
| 55 | ), |
| 56 | ), |
| 57 | 'advanced-options' => array( |
| 58 | 'field_options' => array( |
| 59 | 'randomize', |
| 60 | 'show_values', |
| 61 | 'input_columns', |
| 62 | 'choice_limit', |
| 63 | 'label_hide', |
| 64 | 'css', |
| 65 | ), |
| 66 | ), |
| 67 | ); |
| 68 | |
| 69 | parent::__construct(); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Hook in tabs. |
| 74 | */ |
| 75 | public function init_hooks() { |
| 76 | add_filter( 'everest_forms_html_field_value', array( $this, 'html_field_value' ), 10, 4 ); |
| 77 | add_filter( 'everest_forms_field_properties_' . $this->type, array( $this, 'field_properties' ), 5, 3 ); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Return images, if any, for HTML supported values. |
| 82 | * |
| 83 | * @since 1.6.0 |
| 84 | * |
| 85 | * @param string $value Field value. |
| 86 | * @param array $field Field settings. |
| 87 | * @param array $form_data Form data and settings. |
| 88 | * @param string $context Value display context. |
| 89 | * |
| 90 | * @return string |
| 91 | */ |
| 92 | public function html_field_value( $value, $field, $form_data = array(), $context = '' ) { |
| 93 | if ( is_serialized( $field ) || in_array( $context, array( 'email-html', 'export-pdf' ), true ) ) { |
| 94 | $field_value = maybe_unserialize( $field ); |
| 95 | $field_type = isset( $field_value['type'] ) ? sanitize_text_field( $field_value['type'] ) : 'checkbox'; |
| 96 | |
| 97 | if ( $field_type === $this->type ) { |
| 98 | if ( |
| 99 | 'entry-table' !== $context |
| 100 | && ! empty( $field_value['label'] ) |
| 101 | && ! empty( $field_value['images'] ) |
| 102 | && apply_filters( 'everest_forms_checkbox_field_html_value_images', true, $context ) |
| 103 | ) { |
| 104 | $items = array(); |
| 105 | |
| 106 | if ( ! empty( $field_value['label'] ) ) { |
| 107 | foreach ( $field_value['label'] as $key => $value ) { |
| 108 | if ( ! empty( $field_value['images'][ $key ] ) ) { |
| 109 | $items[] = sprintf( |
| 110 | '<span style="max-width:200px;display:block;margin:0 0 5px 0;"><img src="%s" style="max-width:100%%;display:block;margin:0;"></span>%s', |
| 111 | esc_url( $field_value['images'][ $key ] ), |
| 112 | esc_html( $value ) |
| 113 | ); |
| 114 | } else { |
| 115 | $items[] = esc_html( $value ); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | return implode( 'export-csv' !== $context ? '<br><br>' : '|', $items ); |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | return $value; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Define additional field properties. |
| 130 | * |
| 131 | * @param array $properties Field properties. |
| 132 | * @param array $field Field data. |
| 133 | * @param array $form_data Form data. |
| 134 | * |
| 135 | * @return array |
| 136 | */ |
| 137 | public function field_properties( $properties, $field, $form_data ) { |
| 138 | // Define data. |
| 139 | $form_id = absint( $form_data['id'] ); |
| 140 | $field_id = $field['id']; |
| 141 | $choices = $field['choices']; |
| 142 | |
| 143 | // Remove primary input. |
| 144 | unset( $properties['inputs']['primary'] ); |
| 145 | |
| 146 | // Set input container (ul) properties. |
| 147 | $properties['input_container'] = array( |
| 148 | 'class' => array( ! empty( $field['random'] ) ? 'everest-forms-randomize' : '' ), |
| 149 | 'data' => array(), |
| 150 | 'attr' => array(), |
| 151 | 'id' => "evf-{$form_id}-field_{$field_id}", |
| 152 | ); |
| 153 | |
| 154 | // Set choice limit. |
| 155 | $field['choice_limit'] = empty( $field['choice_limit'] ) ? 0 : (int) $field['choice_limit']; |
| 156 | if ( $field['choice_limit'] > 0 ) { |
| 157 | $properties['input_container']['data']['choice-limit'] = $field['choice_limit']; |
| 158 | } |
| 159 | |
| 160 | // Set input properties. |
| 161 | foreach ( $choices as $key => $choice ) { |
| 162 | $depth = isset( $choice['depth'] ) ? absint( $choice['depth'] ) : 1; |
| 163 | |
| 164 | // Choice labels should not be left blank, but if they are we provide a basic value. |
| 165 | $value = isset( $field['show_values'] ) ? $choice['value'] : $choice['label']; |
| 166 | if ( '' === $value ) { |
| 167 | if ( 1 === count( $choices ) ) { |
| 168 | $value = esc_html__( 'Checked', 'everest-forms' ); |
| 169 | } else { |
| 170 | /* translators: %s - Choice Number. */ |
| 171 | $value = sprintf( esc_html__( 'Choice %s', 'everest-forms' ), $key ); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | $properties['inputs'][ $key ] = array( |
| 176 | 'container' => array( |
| 177 | 'attr' => array(), |
| 178 | 'class' => array( "choice-{$key}", "depth-{$depth}" ), |
| 179 | 'data' => array(), |
| 180 | 'id' => '', |
| 181 | ), |
| 182 | 'label' => array( |
| 183 | 'attr' => array( |
| 184 | 'for' => "evf-{$form_id}-field_{$field_id}_{$key}", |
| 185 | ), |
| 186 | 'class' => array( 'everest-forms-field-label-inline' ), |
| 187 | 'data' => array(), |
| 188 | 'id' => '', |
| 189 | 'text' => $choice['label'], |
| 190 | ), |
| 191 | 'attr' => array( |
| 192 | 'name' => "everest_forms[form_fields][{$field_id}][]", |
| 193 | 'value' => $value, |
| 194 | ), |
| 195 | 'class' => array( 'input-text' ), |
| 196 | 'data' => array(), |
| 197 | 'id' => "evf-{$form_id}-field_{$field_id}_{$key}", |
| 198 | 'image' => isset( $choice['image'] ) ? $choice['image'] : '', |
| 199 | 'required' => ! empty( $field['required'] ) ? 'required' : '', |
| 200 | 'default' => isset( $choice['default'] ), |
| 201 | ); |
| 202 | |
| 203 | // Rule for choice limit validator. |
| 204 | if ( $field['choice_limit'] > 0 ) { |
| 205 | $properties['inputs'][ $key ]['data']['rule-check-limit'] = 'true'; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | // Required class for validation. |
| 210 | if ( ! empty( $field['required'] ) ) { |
| 211 | $properties['input_container']['class'][] = 'evf-field-required'; |
| 212 | } |
| 213 | |
| 214 | // Custom properties if enabled image choices. |
| 215 | if ( ! empty( $field['choices_images'] ) ) { |
| 216 | $properties['input_container']['class'][] = 'everest-forms-image-choices'; |
| 217 | |
| 218 | foreach ( $properties['inputs'] as $key => $inputs ) { |
| 219 | $properties['inputs'][ $key ]['container']['class'][] = 'everest-forms-image-choices-item'; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | // Add selected class for choices with defaults. |
| 224 | foreach ( $properties['inputs'] as $key => $inputs ) { |
| 225 | if ( ! empty( $inputs['default'] ) ) { |
| 226 | $properties['inputs'][ $key ]['container']['class'][] = 'everest-forms-selected'; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | return $properties; |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Randomize order of choices. |
| 235 | * |
| 236 | * @since 1.6.0 |
| 237 | * @param array $field Field Data. |
| 238 | */ |
| 239 | public function randomize( $field ) { |
| 240 | $args = array( |
| 241 | 'slug' => 'random', |
| 242 | 'content' => $this->field_element( |
| 243 | 'checkbox', |
| 244 | $field, |
| 245 | array( |
| 246 | 'slug' => 'random', |
| 247 | 'value' => isset( $field['random'] ) ? '1' : '0', |
| 248 | 'desc' => esc_html__( 'Randomize Choices', 'everest-forms' ), |
| 249 | 'tooltip' => esc_html__( 'Check this option to randomize the order of the choices.', 'everest-forms' ), |
| 250 | ), |
| 251 | false |
| 252 | ), |
| 253 | ); |
| 254 | $this->field_element( 'row', $field, $args ); |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Choice limit field option. |
| 259 | * |
| 260 | * @since 1.6.0 |
| 261 | * @param array $field Field data. |
| 262 | */ |
| 263 | public function choice_limit( $field ) { |
| 264 | $choice_limit_label = $this->field_element( |
| 265 | 'label', |
| 266 | $field, |
| 267 | array( |
| 268 | 'slug' => 'choice_limit', |
| 269 | 'value' => esc_html__( 'Choice Limit', 'everest-forms' ), |
| 270 | 'tooltip' => esc_html__( 'Check this option to limit the number of checkboxes a user can select.', 'everest-forms' ), |
| 271 | ), |
| 272 | false |
| 273 | ); |
| 274 | $choice_limit_input = $this->field_element( |
| 275 | 'text', |
| 276 | $field, |
| 277 | array( |
| 278 | 'slug' => 'choice_limit', |
| 279 | 'value' => ( isset( $field['choice_limit'] ) && $field['choice_limit'] > 0 ) ? (int) $field['choice_limit'] : '', |
| 280 | 'type' => 'number', |
| 281 | ), |
| 282 | false |
| 283 | ); |
| 284 | |
| 285 | $args = array( |
| 286 | 'slug' => 'choice_limit', |
| 287 | 'content' => $choice_limit_label . $choice_limit_input, |
| 288 | ); |
| 289 | $this->field_element( 'row', $field, $args ); |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Show values field option. |
| 294 | * |
| 295 | * @param array $field Field Data. |
| 296 | */ |
| 297 | public function show_values( $field ) { |
| 298 | // Show Values toggle option. This option will only show if already used or if manually enabled by a filter. |
| 299 | if ( ! empty( $field['show_values'] ) || apply_filters( 'everest_forms_fields_show_options_setting', false ) ) { |
| 300 | $args = array( |
| 301 | 'slug' => 'show_values', |
| 302 | 'content' => $this->field_element( |
| 303 | 'checkbox', |
| 304 | $field, |
| 305 | array( |
| 306 | 'slug' => 'show_values', |
| 307 | 'value' => isset( $field['show_values'] ) ? $field['show_values'] : '0', |
| 308 | 'desc' => __( 'Show Values', 'everest-forms' ), |
| 309 | 'tooltip' => __( 'Check this to manually set form field values.', 'everest-forms' ), |
| 310 | ), |
| 311 | false |
| 312 | ), |
| 313 | ); |
| 314 | $this->field_element( 'row', $field, $args ); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * Field preview inside the builder. |
| 320 | * |
| 321 | * @since 1.0.0 |
| 322 | * |
| 323 | * @param array $field Field settings. |
| 324 | */ |
| 325 | public function field_preview( $field ) { |
| 326 | // Label. |
| 327 | $this->field_preview_option( 'label', $field ); |
| 328 | |
| 329 | // Choices. |
| 330 | $this->field_preview_option( 'choices', $field ); |
| 331 | |
| 332 | // Description. |
| 333 | $this->field_preview_option( 'description', $field ); |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * Field display on the form front-end. |
| 338 | * |
| 339 | * @since 1.0.0 |
| 340 | * |
| 341 | * @param array $field Field settings. |
| 342 | * @param array $field_atts Field attributes. |
| 343 | * @param array $form_data Form data and settings. |
| 344 | */ |
| 345 | public function field_display( $field, $field_atts, $form_data ) { |
| 346 | // Define data. |
| 347 | $container = $field['properties']['input_container']; |
| 348 | $choices = $field['properties']['inputs']; |
| 349 | |
| 350 | // List. |
| 351 | printf( '<ul %s>', evf_html_attributes( $container['id'], $container['class'], $container['data'], $container['attr'] ) ); |
| 352 | |
| 353 | foreach ( $choices as $choice ) { |
| 354 | if ( empty( $choice['container'] ) ) { |
| 355 | continue; |
| 356 | } |
| 357 | |
| 358 | // Conditional logic. |
| 359 | if ( isset( $choices['primary'] ) ) { |
| 360 | $choice['attr']['conditional_id'] = $choices['primary']['attr']['conditional_id']; |
| 361 | |
| 362 | if ( isset( $choices['primary']['attr']['conditional_rules'] ) ) { |
| 363 | $choice['attr']['conditional_rules'] = $choices['primary']['attr']['conditional_rules']; |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | printf( '<li %s>', evf_html_attributes( $choice['container']['id'], $choice['container']['class'], $choice['container']['data'], $choice['container']['attr'] ) ); |
| 368 | |
| 369 | if ( ! empty( $field['choices_images'] ) ) { |
| 370 | // Make image choices keyboard-accessible. |
| 371 | $choice['label']['attr']['tabindex'] = 0; |
| 372 | |
| 373 | // Image choices. |
| 374 | printf( '<label %s>', evf_html_attributes( $choice['label']['id'], $choice['label']['class'], $choice['label']['data'], $choice['label']['attr'] ) ); |
| 375 | |
| 376 | if ( ! empty( $choice['image'] ) ) { |
| 377 | printf( |
| 378 | '<span class="everest-forms-image-choices-image"><img src="%s" alt="%s"%s></span>', |
| 379 | esc_url( $choice['image'] ), |
| 380 | esc_attr( $choice['label']['text'] ), |
| 381 | ! empty( $choice['label']['text'] ) ? ' title="' . esc_attr( $choice['label']['text'] ) . '"' : '' |
| 382 | ); |
| 383 | } |
| 384 | |
| 385 | echo '<br>'; |
| 386 | |
| 387 | $choice['attr']['tabindex'] = '-1'; |
| 388 | |
| 389 | printf( '<input type="checkbox" %s %s %s>', evf_html_attributes( $choice['id'], $choice['class'], $choice['data'], $choice['attr'] ), esc_attr( $choice['required'] ), checked( '1', $choice['default'], false ) ); |
| 390 | echo '<span class="everest-forms-image-choices-label">' . wp_kses_post( $choice['label']['text'] ) . '</span>'; |
| 391 | echo '</label>'; |
| 392 | } else { |
| 393 | // Normal display. |
| 394 | printf( '<input type="checkbox" %s %s %s>', evf_html_attributes( $choice['id'], $choice['class'], $choice['data'], $choice['attr'] ), esc_attr( $choice['required'] ), checked( '1', $choice['default'], false ) ); |
| 395 | printf( '<label %s>%s</label>', evf_html_attributes( $choice['label']['id'], $choice['label']['class'], $choice['label']['data'], $choice['label']['attr'] ), wp_kses_post( $choice['label']['text'] ) ); |
| 396 | } |
| 397 | |
| 398 | echo '</li>'; |
| 399 | } |
| 400 | |
| 401 | echo '</ul>'; |
| 402 | } |
| 403 | |
| 404 | /** |
| 405 | * Validates field on form submit. |
| 406 | * |
| 407 | * @since 1.6.0 |
| 408 | * |
| 409 | * @param int $field_id Field ID. |
| 410 | * @param array $field_submit Submitted data. |
| 411 | * @param array $form_data Form data. |
| 412 | */ |
| 413 | public function validate( $field_id, $field_submit, $form_data ) { |
| 414 | $field_submit = (array) $field_submit; |
| 415 | $form_id = $form_data['id']; |
| 416 | $fields = $form_data['form_fields']; |
| 417 | $choice_limit = empty( $fields[ $field_id ]['choice_limit'] ) ? 0 : (int) $fields[ $field_id ]['choice_limit']; |
| 418 | |
| 419 | // Generating the error. |
| 420 | if ( $choice_limit > 0 && $choice_limit < count( $field_submit ) ) { |
| 421 | $error = get_option( 'everest_forms_check_limit_validation', esc_html__( 'You have exceeded number of allowed selections: {#}.', 'everest-forms' ) ); |
| 422 | $error = str_replace( '{#}', $choice_limit, $error ); |
| 423 | } |
| 424 | |
| 425 | // Basic required check. |
| 426 | if ( ! empty( $fields[ $field_id ]['required'] ) && ( empty( $field_submit ) || ( 1 === count( $field_submit ) && empty( $field_submit[0] ) ) ) ) { |
| 427 | $error = evf_get_required_label(); |
| 428 | } |
| 429 | |
| 430 | if ( ! empty( $error ) ) { |
| 431 | evf()->task->errors[ $form_id ][ $field_id ] = $error; |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | /** |
| 436 | * Formats and sanitizes field. |
| 437 | * |
| 438 | * @since 1.0.0 |
| 439 | * |
| 440 | * @param string $field_id Field Id. |
| 441 | * @param array $field_submit Submitted Field. |
| 442 | * @param array $form_data All Form Data. |
| 443 | * @param string $meta_key Field Meta Key. |
| 444 | */ |
| 445 | public function format( $field_id, $field_submit, $form_data, $meta_key ) { |
| 446 | $field_submit = (array) $field_submit; |
| 447 | $field = $form_data['form_fields'][ $field_id ]; |
| 448 | $name = sanitize_text_field( $field['label'] ); |
| 449 | $value_raw = evf_sanitize_array_combine( $field_submit ); |
| 450 | $choice_keys = array(); |
| 451 | |
| 452 | $data = array( |
| 453 | 'id' => $field_id, |
| 454 | 'type' => $this->type, |
| 455 | 'value' => array( |
| 456 | 'name' => $name, |
| 457 | 'type' => $this->type, |
| 458 | ), |
| 459 | 'meta_key' => $meta_key, |
| 460 | 'value_raw' => $value_raw, |
| 461 | ); |
| 462 | |
| 463 | /* |
| 464 | * If show_values is true, that means values posted are the raw values |
| 465 | * and not the labels. So we need to get the label values. |
| 466 | */ |
| 467 | if ( ! empty( $field['show_values'] ) && '1' === $field['show_values'] ) { |
| 468 | foreach ( $field_submit as $item ) { |
| 469 | foreach ( $field['choices'] as $key => $choice ) { |
| 470 | if ( $item === $choice['value'] || ( empty( $choice['value']['label'] ) && (int) str_replace( 'Choice ', '', $item ) === $key ) ) { |
| 471 | $value[] = $choice['label']; |
| 472 | $choice_keys[] = $key; |
| 473 | break; |
| 474 | } |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | $data['value']['label'] = ! empty( $value ) ? evf_sanitize_array_combine( $value ) : ''; |
| 479 | } else { |
| 480 | $data['value']['label'] = $value_raw; |
| 481 | |
| 482 | // Determine choices keys, this is needed for image choices. |
| 483 | foreach ( $field_submit as $item ) { |
| 484 | foreach ( $field['choices'] as $key => $choice ) { |
| 485 | if ( $item === $choice['label'] ) { |
| 486 | $choice_keys[] = $key; |
| 487 | break; |
| 488 | } |
| 489 | } |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | // Images choices are enabled, lookup and store image URLs. |
| 494 | if ( ! empty( $choice_keys ) && ! empty( $field['choices_images'] ) ) { |
| 495 | $data['value']['images'] = array(); |
| 496 | |
| 497 | foreach ( $choice_keys as $key ) { |
| 498 | $data['value']['images'][] = ! empty( $field['choices'][ $key ]['image'] ) ? esc_url_raw( $field['choices'][ $key ]['image'] ) : ''; |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | // Push field details to be saved. |
| 503 | evf()->task->form_fields[ $field_id ] = $data; |
| 504 | } |
| 505 | } |
| 506 |