class-evf-field-address.php
7 years ago
class-evf-field-ai.php
2 years ago
class-evf-field-captcha.php
2 years ago
class-evf-field-checkbox.php
3 years ago
class-evf-field-color.php
3 years ago
class-evf-field-country.php
7 years ago
class-evf-field-credit-card.php
2 years ago
class-evf-field-date-time.php
2 years ago
class-evf-field-divider.php
4 years ago
class-evf-field-email.php
2 years ago
class-evf-field-file-upload.php
6 years ago
class-evf-field-first-name.php
2 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
2 years ago
class-evf-field-likert.php
2 years ago
class-evf-field-lookup.php
2 years ago
class-evf-field-number.php
2 years ago
class-evf-field-password.php
7 years ago
class-evf-field-payment-authorize-net.php
2 years ago
class-evf-field-payment-checkbox.php
7 years ago
class-evf-field-payment-coupon.php
2 years ago
class-evf-field-payment-quantity.php
5 years ago
class-evf-field-payment-radio.php
7 years ago
class-evf-field-payment-single.php
7 years ago
class-evf-field-payment-subtotal.php
3 years ago
class-evf-field-payment-total.php
6 years ago
class-evf-field-phone.php
7 years ago
class-evf-field-privacy-policy.php
4 years ago
class-evf-field-progress.php
3 years ago
class-evf-field-radio.php
3 years ago
class-evf-field-range-slider.php
6 years ago
class-evf-field-rating.php
7 years ago
class-evf-field-repeater.php
2 years ago
class-evf-field-reset.php
3 years ago
class-evf-field-scale-rating.php
2 years ago
class-evf-field-select.php
3 years ago
class-evf-field-signature.php
7 years ago
class-evf-field-text.php
2 years ago
class-evf-field-textarea.php
3 years ago
class-evf-field-title.php
6 years ago
class-evf-field-url.php
2 years ago
class-evf-field-wysiwyg.php
4 years ago
class-evf-field-yes-no.php
2 years ago
class-evf-field-radio.php
419 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Radio field. |
| 4 | * |
| 5 | * @package EverestForms\Fields |
| 6 | * @since 1.0.0 |
| 7 | */ |
| 8 | |
| 9 | defined( 'ABSPATH' ) || exit; |
| 10 | |
| 11 | /** |
| 12 | * EVF_Field_Radio class. |
| 13 | */ |
| 14 | class EVF_Field_Radio extends EVF_Form_Fields { |
| 15 | |
| 16 | /** |
| 17 | * Constructor. |
| 18 | */ |
| 19 | public function __construct() { |
| 20 | $this->name = esc_html__( 'Multiple Choice', 'everest-forms' ); |
| 21 | $this->type = 'radio'; |
| 22 | $this->icon = 'evf-icon evf-icon-multiple-choices-radio'; |
| 23 | $this->order = 60; |
| 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_setting', |
| 55 | 'required_field_message', |
| 56 | ), |
| 57 | ), |
| 58 | 'advanced-options' => array( |
| 59 | 'field_options' => array( |
| 60 | 'randomize', |
| 61 | 'show_values', |
| 62 | 'input_columns', |
| 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'] ) : 'radio'; |
| 96 | |
| 97 | if ( $field_type === $this->type ) { |
| 98 | if ( |
| 99 | 'entry-table' !== $context |
| 100 | && ! empty( $field_value['label'] ) |
| 101 | && ! empty( $field_value['image'] ) |
| 102 | && apply_filters( 'everest_forms_checkbox_field_html_value_images', true, $context ) |
| 103 | ) { |
| 104 | return sprintf( |
| 105 | '<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', |
| 106 | esc_url( $field_value['image'] ), |
| 107 | esc_html( $field_value['label'] ) |
| 108 | ); |
| 109 | } elseif ( isset( $field_value['label'] ) ) { |
| 110 | return esc_html( $field_value['label'] ); |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | return $value; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Define additional field properties. |
| 120 | * |
| 121 | * @since 1.0.0 |
| 122 | * |
| 123 | * @param array $properties Field properties. |
| 124 | * @param array $field Field settings. |
| 125 | * @param array $form_data Form data and settings. |
| 126 | * |
| 127 | * @return array of additional field properties. |
| 128 | */ |
| 129 | public function field_properties( $properties, $field, $form_data ) { |
| 130 | // Define data. |
| 131 | $form_id = absint( $form_data['id'] ); |
| 132 | $field_id = $field['id']; |
| 133 | $choices = $field['choices']; |
| 134 | |
| 135 | // Remove primary input. |
| 136 | unset( $properties['inputs']['primary'] ); |
| 137 | |
| 138 | // Set input container (ul) properties. |
| 139 | $properties['input_container'] = array( |
| 140 | 'class' => array( ! empty( $field['random'] ) ? 'everest-forms-randomize' : '' ), |
| 141 | 'data' => array(), |
| 142 | 'attr' => array(), |
| 143 | 'id' => "evf-{$form_id}-field_{$field_id}", |
| 144 | ); |
| 145 | |
| 146 | // Set input properties. |
| 147 | foreach ( $choices as $key => $choice ) { |
| 148 | $depth = isset( $choice['depth'] ) ? absint( $choice['depth'] ) : 1; |
| 149 | $properties['inputs'][ $key ] = array( |
| 150 | 'container' => array( |
| 151 | 'attr' => array(), |
| 152 | 'class' => array( "choice-{$key}", "depth-{$depth}" ), |
| 153 | 'data' => array(), |
| 154 | 'id' => '', |
| 155 | ), |
| 156 | 'label' => array( |
| 157 | 'attr' => array( |
| 158 | 'for' => "evf-{$form_id}-field_{$field_id}_{$key}", |
| 159 | ), |
| 160 | 'class' => array( 'everest-forms-field-label-inline' ), |
| 161 | 'data' => array(), |
| 162 | 'id' => '', |
| 163 | 'text' => evf_string_translation( $form_id, $field_id, $choice['label'], '-choice-' . $key ), |
| 164 | ), |
| 165 | 'attr' => array( |
| 166 | 'name' => "everest_forms[form_fields][{$field_id}]", |
| 167 | 'value' => isset( $field['show_values'] ) ? $choice['value'] : $choice['label'], |
| 168 | ), |
| 169 | 'class' => array( 'input-text' ), |
| 170 | 'data' => array(), |
| 171 | 'id' => "evf-{$form_id}-field_{$field_id}_{$key}", |
| 172 | 'image' => isset( $choice['image'] ) ? $choice['image'] : '', |
| 173 | 'required' => ! empty( $field['required'] ) ? 'required' : '', |
| 174 | 'default' => isset( $choice['default'] ), |
| 175 | ); |
| 176 | } |
| 177 | |
| 178 | // Required class for validation. |
| 179 | if ( ! empty( $field['required'] ) ) { |
| 180 | $properties['input_container']['class'][] = 'evf-field-required'; |
| 181 | } |
| 182 | |
| 183 | // Custom properties if enabled image choices. |
| 184 | if ( ! empty( $field['choices_images'] ) ) { |
| 185 | $properties['input_container']['class'][] = 'everest-forms-image-choices'; |
| 186 | |
| 187 | foreach ( $properties['inputs'] as $key => $inputs ) { |
| 188 | $properties['inputs'][ $key ]['container']['class'][] = 'everest-forms-image-choices-item'; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | // Add selected class for choices with defaults. |
| 193 | foreach ( $properties['inputs'] as $key => $inputs ) { |
| 194 | if ( ! empty( $inputs['default'] ) ) { |
| 195 | $properties['inputs'][ $key ]['container']['class'][] = 'everest-forms-selected'; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | return $properties; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Randomize order of choices. |
| 204 | * |
| 205 | * @since 1.6.0 |
| 206 | * @param array $field Field Data. |
| 207 | */ |
| 208 | public function randomize( $field ) { |
| 209 | $args = array( |
| 210 | 'slug' => 'random', |
| 211 | 'content' => $this->field_element( |
| 212 | 'checkbox', |
| 213 | $field, |
| 214 | array( |
| 215 | 'slug' => 'random', |
| 216 | 'value' => isset( $field['random'] ) ? '1' : '0', |
| 217 | 'desc' => esc_html__( 'Randomize Choices', 'everest-forms' ), |
| 218 | 'tooltip' => esc_html__( 'Check this option to randomize the order of the choices.', 'everest-forms' ), |
| 219 | ), |
| 220 | false |
| 221 | ), |
| 222 | ); |
| 223 | $this->field_element( 'row', $field, $args ); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Show values field option. |
| 228 | * |
| 229 | * @param array $field Field Data. |
| 230 | */ |
| 231 | public function show_values( $field ) { |
| 232 | // Show Values toggle option. This option will only show if already used or if manually enabled by a filter. |
| 233 | if ( ! empty( $field['show_values'] ) || apply_filters( 'everest_forms_fields_show_options_setting', false ) ) { |
| 234 | $args = array( |
| 235 | 'slug' => 'show_values', |
| 236 | 'content' => $this->field_element( |
| 237 | 'checkbox', |
| 238 | $field, |
| 239 | array( |
| 240 | 'slug' => 'show_values', |
| 241 | 'value' => isset( $field['show_values'] ) ? $field['show_values'] : '0', |
| 242 | 'desc' => __( 'Show Values', 'everest-forms' ), |
| 243 | 'tooltip' => __( 'Check this to manually set form field values.', 'everest-forms' ), |
| 244 | ), |
| 245 | false |
| 246 | ), |
| 247 | ); |
| 248 | $this->field_element( 'row', $field, $args ); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Field preview inside the builder. |
| 254 | * |
| 255 | * @since 1.0.0 |
| 256 | * |
| 257 | * @param array $field Field data and settings. |
| 258 | */ |
| 259 | public function field_preview( $field ) { |
| 260 | // Label. |
| 261 | $this->field_preview_option( 'label', $field ); |
| 262 | |
| 263 | // Choices. |
| 264 | $this->field_preview_option( 'choices', $field ); |
| 265 | |
| 266 | // Description. |
| 267 | $this->field_preview_option( 'description', $field ); |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Field display on the form front-end. |
| 272 | * |
| 273 | * @since 1.0.0 |
| 274 | * |
| 275 | * @param array $field Field Data. |
| 276 | * @param array $field_atts Field attributes. |
| 277 | * @param array $form_data All Form Data. |
| 278 | */ |
| 279 | public function field_display( $field, $field_atts, $form_data ) { |
| 280 | // Define data. |
| 281 | $container = $field['properties']['input_container']; |
| 282 | $choices = $field['properties']['inputs']; |
| 283 | |
| 284 | // List. |
| 285 | printf( '<ul %s>', evf_html_attributes( $container['id'], $container['class'], $container['data'], $container['attr'] ) ); |
| 286 | |
| 287 | foreach ( $choices as $choice ) { |
| 288 | if ( empty( $choice['container'] ) ) { |
| 289 | continue; |
| 290 | } |
| 291 | |
| 292 | // Conditional logic. |
| 293 | if ( isset( $choices['primary'] ) ) { |
| 294 | $choice['attr']['conditional_id'] = $choices['primary']['attr']['conditional_id']; |
| 295 | |
| 296 | if ( isset( $choices['primary']['attr']['conditional_rules'] ) ) { |
| 297 | $choice['attr']['conditional_rules'] = $choices['primary']['attr']['conditional_rules']; |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | printf( '<li %s>', evf_html_attributes( $choice['container']['id'], $choice['container']['class'], $choice['container']['data'], $choice['container']['attr'] ) ); |
| 302 | |
| 303 | if ( ! empty( $field['choices_images'] ) ) { |
| 304 | // Make image choices keyboard-accessible. |
| 305 | $choice['label']['attr']['tabindex'] = 0; |
| 306 | |
| 307 | // Image choices. |
| 308 | printf( '<label %s>', evf_html_attributes( $choice['label']['id'], $choice['label']['class'], $choice['label']['data'], $choice['label']['attr'] ) ); |
| 309 | |
| 310 | if ( ! empty( $choice['image'] ) ) { |
| 311 | printf( |
| 312 | '<span class="everest-forms-image-choices-image"><img src="%s" alt="%s"%s></span>', |
| 313 | esc_url( $choice['image'] ), |
| 314 | esc_attr( $choice['label']['text'] ), |
| 315 | ! empty( $choice['label']['text'] ) ? ' title="' . esc_attr( $choice['label']['text'] ) . '"' : '' |
| 316 | ); |
| 317 | } |
| 318 | |
| 319 | echo '<br>'; |
| 320 | |
| 321 | $choice['attr']['tabindex'] = '-1'; |
| 322 | |
| 323 | printf( '<input type="radio" %s %s %s >', evf_html_attributes( $choice['id'], $choice['class'], $choice['data'], $choice['attr'] ), esc_attr( $choice['required'] ), checked( '1', $choice['default'], false ) ); |
| 324 | echo '<label class="everest-forms-image-choices-label">' . wp_kses_post( $choice['label']['text'] ) . '</label>'; |
| 325 | echo '</label>'; |
| 326 | } else { |
| 327 | // Normal display. |
| 328 | printf( '<input type="radio" %s %s %s>', evf_html_attributes( $choice['id'], $choice['class'], $choice['data'], $choice['attr'] ), esc_attr( $choice['required'] ), checked( '1', $choice['default'], false ) ); |
| 329 | 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'] ) ); |
| 330 | } |
| 331 | |
| 332 | echo '</li>'; |
| 333 | } |
| 334 | |
| 335 | echo '</ul>'; |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * Edit form field display on the entry back-end. |
| 340 | * |
| 341 | * @since 1.7.0 |
| 342 | * |
| 343 | * @param array $entry_field Entry field data. |
| 344 | * @param array $field Field data. |
| 345 | * @param array $form_data Form data and settings. |
| 346 | */ |
| 347 | public function edit_form_field_display( $entry_field, $field, $form_data ) { |
| 348 | $value = isset( $entry_field['value_raw'] ) ? $entry_field['value_raw'] : ''; |
| 349 | |
| 350 | $this->remove_field_choices_defaults( $field, $field['properties'] ); |
| 351 | |
| 352 | if ( '' !== $value ) { |
| 353 | $field['properties'] = $this->get_single_field_property_value( $value, 'primary', $field['properties'], $field ); |
| 354 | } |
| 355 | |
| 356 | $this->field_display( $field, null, $form_data ); |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * Formats and sanitizes field. |
| 361 | * |
| 362 | * @since 1.0.0 |
| 363 | * |
| 364 | * @param string $field_id Field Id. |
| 365 | * @param array $field_submit Submitted Field. |
| 366 | * @param array $form_data All Form Data. |
| 367 | * @param string $meta_key Field Meta Key. |
| 368 | */ |
| 369 | public function format( $field_id, $field_submit, $form_data, $meta_key ) { |
| 370 | $field = $form_data['form_fields'][ $field_id ]; |
| 371 | $name = make_clickable( $field['label'] ); |
| 372 | $value_raw = sanitize_text_field( $field_submit ); |
| 373 | $choice_key = ''; |
| 374 | |
| 375 | $data = array( |
| 376 | 'id' => $field_id, |
| 377 | 'type' => $this->type, |
| 378 | 'value' => array( |
| 379 | 'name' => $name, |
| 380 | 'type' => $this->type, |
| 381 | ), |
| 382 | 'meta_key' => $meta_key, |
| 383 | 'value_raw' => $value_raw, |
| 384 | ); |
| 385 | |
| 386 | /* |
| 387 | * If show_values is true, that means values posted are the raw values |
| 388 | * and not the labels. So we need to get the label values. |
| 389 | */ |
| 390 | if ( ! empty( $field['show_values'] ) && '1' === $field['show_values'] ) { |
| 391 | foreach ( $field['choices'] as $key => $choice ) { |
| 392 | if ( $choice['value'] === $field_submit ) { |
| 393 | $data['value']['label'] = sanitize_text_field( $choice['label'] ); |
| 394 | $choice_key = $key; |
| 395 | break; |
| 396 | } |
| 397 | } |
| 398 | } else { |
| 399 | $data['value']['label'] = $value_raw; |
| 400 | |
| 401 | // Determine choice key, this is needed for image choices. |
| 402 | foreach ( $field['choices'] as $key => $choice ) { |
| 403 | if ( $choice['label'] === $field_submit ) { |
| 404 | $choice_key = $key; |
| 405 | break; |
| 406 | } |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | // Images choices are enabled, lookup and store image URL. |
| 411 | if ( ! empty( $choice_key ) && ! empty( $field['choices_images'] ) ) { |
| 412 | $data['value']['image'] = ! empty( $field['choices'][ $choice_key ]['image'] ) ? esc_url_raw( $field['choices'][ $choice_key ]['image'] ) : ''; |
| 413 | } |
| 414 | |
| 415 | // Push field details to be saved. |
| 416 | evf()->task->form_fields[ $field_id ] = $data; |
| 417 | } |
| 418 | } |
| 419 |