class-evf-field-address.php
1 week ago
class-evf-field-ai.php
1 year ago
class-evf-field-captcha.php
1 month ago
class-evf-field-checkbox.php
3 months ago
class-evf-field-color.php
1 month ago
class-evf-field-country.php
3 months ago
class-evf-field-credit-card.php
1 month ago
class-evf-field-date-time.php
1 month ago
class-evf-field-divider.php
2 years ago
class-evf-field-email.php
1 year ago
class-evf-field-file-upload.php
1 year ago
class-evf-field-first-name.php
1 year ago
class-evf-field-hcaptcha.php
5 months ago
class-evf-field-hidden.php
1 year ago
class-evf-field-html.php
10 months ago
class-evf-field-image-upload.php
1 year ago
class-evf-field-last-name.php
1 year ago
class-evf-field-likert.php
1 year ago
class-evf-field-lookup.php
1 month ago
class-evf-field-number.php
1 year ago
class-evf-field-password.php
1 month ago
class-evf-field-payment-authorize-net.php
1 month ago
class-evf-field-payment-checkbox.php
1 month ago
class-evf-field-payment-coupon.php
1 month ago
class-evf-field-payment-gateway-selector.php
1 month ago
class-evf-field-payment-quantity.php
1 month ago
class-evf-field-payment-radio.php
1 month ago
class-evf-field-payment-single.php
1 month ago
class-evf-field-payment-square.php
2 years ago
class-evf-field-payment-subscription-plan.php
1 month ago
class-evf-field-payment-subtotal.php
1 month ago
class-evf-field-payment-total.php
1 month ago
class-evf-field-phone.php
1 year ago
class-evf-field-privacy-policy.php
3 months ago
class-evf-field-private-note.php
1 year ago
class-evf-field-progress.php
1 month ago
class-evf-field-radio.php
3 months ago
class-evf-field-range-slider.php
1 month ago
class-evf-field-rating.php
3 months ago
class-evf-field-recaptcha.php
5 months ago
class-evf-field-repeater.php
1 month ago
class-evf-field-reset.php
1 month ago
class-evf-field-scale-rating.php
1 year ago
class-evf-field-select.php
1 year ago
class-evf-field-signature.php
1 month ago
class-evf-field-text.php
1 year ago
class-evf-field-textarea.php
1 year ago
class-evf-field-title.php
2 years ago
class-evf-field-turnstile.php
5 months ago
class-evf-field-url.php
1 year ago
class-evf-field-wysiwyg.php
3 months ago
class-evf-field-yes-no.php
1 year ago
payment-amount-helpers.php
1 month ago
class-evf-field-yes-no.php
627 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Yes/No field |
| 4 | * |
| 5 | * @package EverestForms\Fields |
| 6 | * @since 1.2.0 |
| 7 | */ |
| 8 | |
| 9 | defined( 'ABSPATH' ) || exit; |
| 10 | |
| 11 | /** |
| 12 | * EVF_Field_Yes_No Class. |
| 13 | */ |
| 14 | class EVF_Field_Yes_No extends EVF_Form_Fields { |
| 15 | |
| 16 | /** |
| 17 | * Constructor. |
| 18 | */ |
| 19 | public function __construct() { |
| 20 | $this->name = esc_html__( 'Yes/No', 'everest-forms' ); |
| 21 | $this->type = 'yes-no'; |
| 22 | $this->icon = 'evf-icon evf-icon-yes-no'; |
| 23 | $this->order = 15; |
| 24 | $this->group = 'survey'; |
| 25 | $this->settings = array( |
| 26 | 'basic-options' => array( |
| 27 | 'field_options' => array( |
| 28 | 'label', |
| 29 | 'yes_value', |
| 30 | 'no_value', |
| 31 | 'description', |
| 32 | 'required', |
| 33 | 'required_field_message_setting', |
| 34 | 'required_field_message', |
| 35 | ), |
| 36 | ), |
| 37 | 'advanced-options' => array( |
| 38 | 'field_options' => array( |
| 39 | 'meta', |
| 40 | 'style', |
| 41 | 'yes_icon_color', |
| 42 | 'no_icon_color', |
| 43 | 'yes_label', |
| 44 | 'no_label', |
| 45 | 'label_hide', |
| 46 | 'css', |
| 47 | ), |
| 48 | ), |
| 49 | ); |
| 50 | |
| 51 | parent::__construct(); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Hook in tabs. |
| 56 | */ |
| 57 | public function init_hooks() { |
| 58 | add_filter( 'everest_forms_field_properties_' . $this->type, array( $this, 'field_properties' ), 5, 3 ); |
| 59 | add_filter( 'everest_forms_field_exporter_' . $this->type, array( $this, 'field_exporter' ) ); |
| 60 | add_filter( 'everest_forms_entries_field_editable', array( $this, 'field_editable' ), 10, 2 ); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Returns a SVG for use in the yes no field. |
| 65 | * |
| 66 | * @param string $icon_type Type of the icon. |
| 67 | * @param string $icon_color Color of the icon. |
| 68 | */ |
| 69 | private function get_icon_svg( $icon_type = 'yes', $icon_color = '' ) { |
| 70 | $svg_icons = array( |
| 71 | 'yes' => '<svg width="32" height="32" viewBox="0 0 24 24" style="fill:' . $icon_color . '"><path d="M1 21h4V9H1v12zm22-11c0-1.1-.9-2-2-2h-6.31l.95-4.57.03-.32c0-.41-.17-.79-.44-1.06L14.17 1 7.59 7.59C7.22 7.95 7 8.45 7 9v10c0 1.1.9 2 2 2h9c.83 0 1.54-.5 1.84-1.22l3.02-7.05c.09-.23.14-.47.14-.73v-1.91l-.01-.01L23 10z"/></path></svg>', |
| 72 | 'no' => '<svg width="32" height="32" viewBox="0 0 24 24" style="fill:' . $icon_color . '"><path d="M15 3H6c-.83 0-1.54.5-1.84 1.22l-3.02 7.05c-.09.23-.14.47-.14.73v1.91l.01.01L1 14c0 1.1.9 2 2 2h6.31l-.95 4.57-.03.32c0 .41.17.79.44 1.06L9.83 23l6.59-6.59c.36-.36.58-.86.58-1.41V5c0-1.1-.9-2-2-2zm4 0v12h4V3h-4z"></path></svg>', |
| 73 | ); |
| 74 | |
| 75 | if ( isset( $svg_icons[ $icon_type ] ) ) { |
| 76 | return $svg_icons[ $icon_type ]; |
| 77 | } |
| 78 | |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Yes field option. |
| 84 | * |
| 85 | * @param array $field Field settings. |
| 86 | */ |
| 87 | public function yes_value( $field ) { |
| 88 | |
| 89 | $lbl = $this->field_element( |
| 90 | 'label', |
| 91 | $field, |
| 92 | array( |
| 93 | 'slug' => 'yes_value', |
| 94 | 'value' => esc_html__( 'Yes Value', 'everest-forms' ), |
| 95 | 'tooltip' => esc_html__( 'Enter the value for Yes.', 'everest-forms' ), |
| 96 | ), |
| 97 | false |
| 98 | ); |
| 99 | $fld = $this->field_element( |
| 100 | 'text', |
| 101 | $field, |
| 102 | array( |
| 103 | 'slug' => 'yes_value', |
| 104 | 'value' => ! empty( $field['yes_value'] ) ? esc_attr( $field['yes_value'] ) : 'yes', |
| 105 | 'class' => 'evf-yes', |
| 106 | ), |
| 107 | false |
| 108 | ); |
| 109 | $this->field_element( |
| 110 | 'row', |
| 111 | $field, |
| 112 | array( |
| 113 | 'slug' => 'yes_value', |
| 114 | 'content' => $lbl . $fld, |
| 115 | ) |
| 116 | ); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * No field option. |
| 121 | * |
| 122 | * @param array $field Field settings. |
| 123 | */ |
| 124 | public function no_value( $field ) { |
| 125 | |
| 126 | $lbl = $this->field_element( |
| 127 | 'label', |
| 128 | $field, |
| 129 | array( |
| 130 | 'slug' => 'no_value', |
| 131 | 'value' => esc_html__( 'No Value', 'everest-forms' ), |
| 132 | 'tooltip' => esc_html__( 'Enter the value for No.', 'everest-forms' ), |
| 133 | ), |
| 134 | false |
| 135 | ); |
| 136 | $fld = $this->field_element( |
| 137 | 'text', |
| 138 | $field, |
| 139 | array( |
| 140 | 'slug' => 'no_value', |
| 141 | 'value' => ! empty( $field['no_value'] ) ? esc_attr( $field['no_value'] ) : 'no', |
| 142 | 'class' => 'evf-yes', |
| 143 | ), |
| 144 | false |
| 145 | ); |
| 146 | $this->field_element( |
| 147 | 'row', |
| 148 | $field, |
| 149 | array( |
| 150 | 'slug' => 'no_value', |
| 151 | 'content' => $lbl . $fld, |
| 152 | ) |
| 153 | ); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Icon color for yes field option. |
| 158 | * |
| 159 | * @param array $field Field settings. |
| 160 | */ |
| 161 | public function style( $field ) { |
| 162 | |
| 163 | $lbl = $this->field_element( |
| 164 | 'label', |
| 165 | $field, |
| 166 | array( |
| 167 | 'slug' => 'style', |
| 168 | 'value' => esc_html__( 'Style', 'everest-forms' ), |
| 169 | 'tooltip' => esc_html__( 'Choose the field design variation.', 'everest-forms' ), |
| 170 | ), |
| 171 | false |
| 172 | ); |
| 173 | $input_field = $this->field_element( |
| 174 | 'select', |
| 175 | $field, |
| 176 | array( |
| 177 | 'type' => 'select', |
| 178 | 'slug' => 'style', |
| 179 | 'class' => 'evf-yes-no-class', |
| 180 | 'options' => array( |
| 181 | 'with_icon' => esc_html__( 'Icon', 'everest-forms' ), |
| 182 | 'with_text' => esc_html__( 'Text', 'everest-forms' ), |
| 183 | 'with_icon_text' => esc_html__( 'Text and Icon', 'everest-forms' ), |
| 184 | ), |
| 185 | 'value' => ! empty( $field['style'] ) ? esc_attr( $field['style'] ) : 'with_icon', |
| 186 | ), |
| 187 | false |
| 188 | ); |
| 189 | $this->field_element( |
| 190 | 'row', |
| 191 | $field, |
| 192 | array( |
| 193 | 'slug' => 'style', |
| 194 | 'content' => $lbl . $input_field, |
| 195 | ) |
| 196 | ); |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Icon color for yes field option. |
| 201 | * |
| 202 | * @param array $field Field settings. |
| 203 | */ |
| 204 | public function yes_icon_color( $field ) { |
| 205 | |
| 206 | $lbl = $this->field_element( |
| 207 | 'label', |
| 208 | $field, |
| 209 | array( |
| 210 | 'slug' => 'yes_icon_color', |
| 211 | 'value' => esc_html__( 'Yes Icon Color', 'everest-forms' ), |
| 212 | 'tooltip' => esc_html__( 'Select the primary color for the Yes icon.', 'everest-forms' ), |
| 213 | ), |
| 214 | false |
| 215 | ); |
| 216 | $fld = $this->field_element( |
| 217 | 'text', |
| 218 | $field, |
| 219 | array( |
| 220 | 'slug' => 'yes_icon_color', |
| 221 | 'value' => ! empty( $field['yes_icon_color'] ) ? esc_attr( $field['yes_icon_color'] ) : 'green', |
| 222 | 'class' => 'evf-colorpicker', |
| 223 | 'data' => array( |
| 224 | 'default-color' => 'green', |
| 225 | ), |
| 226 | ), |
| 227 | false |
| 228 | ); |
| 229 | $this->field_element( |
| 230 | 'row', |
| 231 | $field, |
| 232 | array( |
| 233 | 'slug' => 'yes_icon_color', |
| 234 | 'content' => $lbl . $fld, |
| 235 | ) |
| 236 | ); |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Icon color for no field option. |
| 241 | * |
| 242 | * @param array $field Field settings. |
| 243 | */ |
| 244 | public function no_icon_color( $field ) { |
| 245 | |
| 246 | $lbl = $this->field_element( |
| 247 | 'label', |
| 248 | $field, |
| 249 | array( |
| 250 | 'slug' => 'no_icon_color', |
| 251 | 'value' => esc_html__( 'No Icon Color', 'everest-forms' ), |
| 252 | 'tooltip' => esc_html__( 'Select the primary color for the No icon.', 'everest-forms' ), |
| 253 | ), |
| 254 | false |
| 255 | ); |
| 256 | $fld = $this->field_element( |
| 257 | 'text', |
| 258 | $field, |
| 259 | array( |
| 260 | 'slug' => 'no_icon_color', |
| 261 | 'value' => ! empty( $field['no_icon_color'] ) ? esc_attr( $field['no_icon_color'] ) : 'red', |
| 262 | 'class' => 'evf-colorpicker', |
| 263 | 'data' => array( |
| 264 | 'default-color' => 'red', |
| 265 | ), |
| 266 | ), |
| 267 | false |
| 268 | ); |
| 269 | $this->field_element( |
| 270 | 'row', |
| 271 | $field, |
| 272 | array( |
| 273 | 'slug' => 'no_icon_color', |
| 274 | 'content' => $lbl . $fld, |
| 275 | ) |
| 276 | ); |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * Label for yes field option. |
| 281 | * |
| 282 | * @param array $field Field settings. |
| 283 | */ |
| 284 | public function yes_label( $field ) { |
| 285 | |
| 286 | $lbl = $this->field_element( |
| 287 | 'label', |
| 288 | $field, |
| 289 | array( |
| 290 | 'slug' => 'yes_label', |
| 291 | 'value' => esc_html__( 'Yes Label', 'everest-forms' ), |
| 292 | 'tooltip' => esc_html__( 'Enter the label text for Yes.', 'everest-forms' ), |
| 293 | ), |
| 294 | false |
| 295 | ); |
| 296 | $fld = $this->field_element( |
| 297 | 'text', |
| 298 | $field, |
| 299 | array( |
| 300 | 'slug' => 'yes_label', |
| 301 | 'value' => ! empty( $field['yes_label'] ) ? esc_attr( $field['yes_label'] ) : 'Yes', |
| 302 | 'class' => 'evf-yes-label', |
| 303 | ), |
| 304 | false |
| 305 | ); |
| 306 | $this->field_element( |
| 307 | 'row', |
| 308 | $field, |
| 309 | array( |
| 310 | 'slug' => 'yes_label', |
| 311 | 'content' => $lbl . $fld, |
| 312 | ) |
| 313 | ); |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * Label for no field option. |
| 318 | * |
| 319 | * @param array $field Field settings. |
| 320 | */ |
| 321 | public function no_label( $field ) { |
| 322 | |
| 323 | $lbl = $this->field_element( |
| 324 | 'label', |
| 325 | $field, |
| 326 | array( |
| 327 | 'slug' => 'no_label', |
| 328 | 'value' => esc_html__( 'No Label', 'everest-forms' ), |
| 329 | 'tooltip' => esc_html__( 'Enter the label text for No.', 'everest-forms' ), |
| 330 | ), |
| 331 | false |
| 332 | ); |
| 333 | $fld = $this->field_element( |
| 334 | 'text', |
| 335 | $field, |
| 336 | array( |
| 337 | 'slug' => 'no_label', |
| 338 | 'value' => ! empty( $field['no_label'] ) ? esc_attr( $field['no_label'] ) : 'No', |
| 339 | 'class' => 'evf-no-label', |
| 340 | ), |
| 341 | false |
| 342 | ); |
| 343 | $this->field_element( |
| 344 | 'row', |
| 345 | $field, |
| 346 | array( |
| 347 | 'slug' => 'no_label', |
| 348 | 'content' => $lbl . $fld, |
| 349 | ) |
| 350 | ); |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * Define additional field properties. |
| 355 | * |
| 356 | * @since 1.2.0 |
| 357 | * |
| 358 | * @param array $properties Field properties. |
| 359 | * @param array $field Field settings. |
| 360 | * @param array $form_data Form data and settings. |
| 361 | * |
| 362 | * @return array of additional field properties. |
| 363 | */ |
| 364 | public function field_properties( $properties, $field, $form_data ) { |
| 365 | $properties['inputs']['primary']['yes_no']['yes_value'] = ! empty( $field['yes_value'] ) ? esc_attr( $field['yes_value'] ) : 'yes'; |
| 366 | $properties['inputs']['primary']['yes_no']['no_value'] = ! empty( $field['no_value'] ) ? esc_attr( $field['no_value'] ) : 'no'; |
| 367 | $properties['inputs']['primary']['yes_no']['yes_icon_color'] = ! empty( $field['yes_icon_color'] ) ? esc_attr( $field['yes_icon_color'] ) : 'green'; |
| 368 | $properties['inputs']['primary']['yes_no']['no_icon_color'] = ! empty( $field['no_icon_color'] ) ? esc_attr( $field['no_icon_color'] ) : 'red'; |
| 369 | $properties['inputs']['primary']['yes_no']['yes_label'] = ! empty( $field['yes_label'] ) ? esc_attr( $field['yes_label'] ) : 'Yes'; |
| 370 | $properties['inputs']['primary']['yes_no']['no_label'] = ! empty( $field['no_label'] ) ? esc_attr( $field['no_label'] ) : 'No'; |
| 371 | $properties['inputs']['primary']['yes_no']['field_style'] = ! empty( $field['style'] ) ? esc_attr( $field['style'] ) : 'with_icon'; |
| 372 | |
| 373 | if ( 'with_icon' === $properties['inputs']['primary']['yes_no']['field_style'] ) { |
| 374 | $properties['container']['class'][] = 'evf-yes-no-field-with-icon'; |
| 375 | } elseif ( 'with_text' === $properties['inputs']['primary']['yes_no']['field_style'] ) { |
| 376 | $properties['container']['class'][] = 'evf-yes-no-field-with-text'; |
| 377 | |
| 378 | } elseif ( 'with_icon_text' === $properties['inputs']['primary']['yes_no']['field_style'] ) { |
| 379 | $properties['container']['class'][] = 'evf-yes-no-field-with-icon-text'; |
| 380 | } |
| 381 | |
| 382 | return $properties; |
| 383 | } |
| 384 | |
| 385 | /** |
| 386 | * Filter callback for outputting formatted data. |
| 387 | * |
| 388 | * @param array $field Field Data. |
| 389 | */ |
| 390 | public function field_exporter( $field ) { |
| 391 | return array( |
| 392 | 'label' => ! empty( $field['name'] ) ? $field['name'] : ucfirst( str_replace( '_', ' ', $field['type'] ) ) . " - {$field['id']}", |
| 393 | 'value' => ! empty( $field['value'] ) ? $field['value'] : false, |
| 394 | ); |
| 395 | } |
| 396 | |
| 397 | /** |
| 398 | * Allow this field to be editable. |
| 399 | * |
| 400 | * @param bool $is_editable True if editable. False if not. |
| 401 | * @param string $field_type Field type to check for editable. |
| 402 | */ |
| 403 | public function field_editable( $is_editable, $field_type ) { |
| 404 | return ! empty( $field_type ) && $field_type === $this->type ? true : $is_editable; |
| 405 | } |
| 406 | |
| 407 | /** |
| 408 | * Field preview inside the builder. |
| 409 | * |
| 410 | * @since 1.2.0 |
| 411 | * |
| 412 | * @param array $field Field data and settings. |
| 413 | */ |
| 414 | public function field_preview( $field ) { |
| 415 | $yes_icon_color = ! empty( $field['yes_icon_color'] ) ? esc_attr( $field['yes_icon_color'] ) : 'green'; |
| 416 | $no_icon_color = ! empty( $field['no_icon_color'] ) ? esc_attr( $field['no_icon_color'] ) : 'red'; |
| 417 | $yes_label = ! empty( $field['yes_label'] ) ? esc_attr( $field['yes_label'] ) : 'Yes'; |
| 418 | $no_label = ! empty( $field['no_label'] ) ? esc_attr( $field['no_label'] ) : 'No'; |
| 419 | $yes_icon = $this->get_icon_svg( 'yes', $yes_icon_color ); |
| 420 | $no_icon = $this->get_icon_svg( 'no', $no_icon_color ); |
| 421 | $field_style = ! empty( $field['style'] ) ? esc_attr( $field['style'] ) : 'with_icon'; |
| 422 | |
| 423 | // Label. |
| 424 | $this->field_preview_option( 'label', $field ); |
| 425 | |
| 426 | // Primary input. |
| 427 | $additional_class = 'with_icon_text' === $field_style ? ' icon-text' : ( 'with_text' === $field_style ? ' text-only' : '' ); |
| 428 | |
| 429 | printf( '<div class="yes-no-preview' . esc_attr( $additional_class ) . '">' ); |
| 430 | if ( 'with_icon' === $field_style ) { |
| 431 | printf( |
| 432 | '<span class="%s yes-no-icon">%s</span>', |
| 433 | esc_attr( 'yes' ), |
| 434 | $yes_icon // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 435 | ); |
| 436 | |
| 437 | printf( |
| 438 | '<span class="%s yes-no-icon">%s</span>', |
| 439 | esc_attr( 'no' ), |
| 440 | $no_icon // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 441 | ); |
| 442 | } elseif ( 'with_text' === $field_style ) { |
| 443 | printf( '<input type="text" value="%s" disabled /><input type="text" value="%s" disabled />', esc_attr( $yes_label ), esc_attr( $no_label ) ); |
| 444 | } elseif ( 'with_icon_text' === $field_style ) { |
| 445 | printf( |
| 446 | '<span class="%s yes-no-icon">%s <label>%s</label></span>', |
| 447 | esc_attr( 'yes' ), |
| 448 | $yes_icon, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 449 | esc_html( $yes_label ) |
| 450 | ); |
| 451 | printf( |
| 452 | '<span class="%s yes-no-icon">%s <label>%s</label></span>', |
| 453 | esc_attr( 'no' ), |
| 454 | $no_icon, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 455 | esc_html( $no_label ) |
| 456 | ); |
| 457 | } |
| 458 | |
| 459 | printf( '</div>' ); |
| 460 | |
| 461 | // Description. |
| 462 | $this->field_preview_option( 'description', $field ); |
| 463 | } |
| 464 | |
| 465 | /** |
| 466 | * Field display on the form front-end. |
| 467 | * |
| 468 | * @since 1.2.0 |
| 469 | * |
| 470 | * @param array $field Field Data. |
| 471 | * @param array $field_atts Field attributes. |
| 472 | * @param array $form_data All Form Data. |
| 473 | */ |
| 474 | public function field_display( $field, $field_atts, $form_data ) { |
| 475 | // Define data. |
| 476 | $primary = $field['properties']['inputs']['primary']; |
| 477 | $yes_no = $primary['yes_no']; |
| 478 | $yes_value = esc_attr( $yes_no['yes_value'] ); |
| 479 | $no_value = esc_attr( $yes_no['no_value'] ); |
| 480 | $yes_icon_color = $yes_no['yes_icon_color']; |
| 481 | $no_icon_color = $yes_no['no_icon_color']; |
| 482 | $yes_icon = $this->get_icon_svg( 'yes', $yes_icon_color ); |
| 483 | $no_icon = $this->get_icon_svg( 'no', $no_icon_color ); |
| 484 | $yes_label = $yes_no['yes_label']; |
| 485 | $no_label = $yes_no['no_label']; |
| 486 | $field_style = $yes_no['field_style']; |
| 487 | |
| 488 | echo '<div id="evf-' . absint( $form_data['id'] ) . '-field_' . esc_attr( $field['id'] ) . '" class="everest-forms-field-yes-no-container">'; |
| 489 | |
| 490 | printf( |
| 491 | '<label class="everest-forms-field-yes-no yes" for="everest-forms-%d-field_%s_1">', |
| 492 | absint( $form_data['id'] ), |
| 493 | esc_attr( $field['id'] ) |
| 494 | ); |
| 495 | |
| 496 | // Primary field. |
| 497 | $primary['id'] = sprintf( |
| 498 | 'everest-forms-%d-field_%s_1', |
| 499 | absint( $form_data['id'] ), |
| 500 | $field['id'] |
| 501 | ); |
| 502 | |
| 503 | $primary['attr']['value'] = esc_attr( $yes_value ); |
| 504 | |
| 505 | if ( 'with_icon' === $field_style ) { |
| 506 | printf( |
| 507 | '<input type="radio" %s %s>', |
| 508 | evf_html_attributes( $primary['id'], $primary['class'], $primary['data'], $primary['attr'] ), |
| 509 | esc_attr( $primary['required'] ) |
| 510 | ); |
| 511 | |
| 512 | echo $yes_icon; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 513 | } elseif ( 'with_text' === $field_style ) { |
| 514 | printf( |
| 515 | '<input type="radio" %s %s>', |
| 516 | evf_html_attributes( $primary['id'], $primary['class'], $primary['data'], $primary['attr'] ), |
| 517 | esc_attr( $primary['required'] ) |
| 518 | ); |
| 519 | printf( '<label for="%s">%s</label>', esc_attr( $primary['id'] ), esc_html( $yes_label ) ); |
| 520 | } elseif ( 'with_icon_text' === $field_style ) { |
| 521 | printf( |
| 522 | '<input type="radio" %s %s>', |
| 523 | evf_html_attributes( $primary['id'], $primary['class'], $primary['data'], $primary['attr'] ), |
| 524 | esc_attr( $primary['required'] ) |
| 525 | ); |
| 526 | |
| 527 | echo $yes_icon; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 528 | printf( '<label for="%s">%s</label>', esc_attr( $primary['id'] ), esc_html( $yes_label ) ); |
| 529 | } |
| 530 | |
| 531 | echo '</label>'; |
| 532 | |
| 533 | printf( |
| 534 | '<label class="everest-forms-field-yes-no no" for="everest-forms-%d-field_%s_0">', |
| 535 | absint( $form_data['id'] ), |
| 536 | esc_attr( $field['id'] ) |
| 537 | ); |
| 538 | |
| 539 | // Primary field. |
| 540 | $primary['id'] = sprintf( |
| 541 | 'everest-forms-%d-field_%s_0', |
| 542 | absint( $form_data['id'] ), |
| 543 | $field['id'] |
| 544 | ); |
| 545 | |
| 546 | $primary['attr']['value'] = esc_js( $no_value ); |
| 547 | |
| 548 | |
| 549 | if ( 'with_icon' === $field_style ) { |
| 550 | printf( |
| 551 | '<input type="radio" %s %s>', |
| 552 | evf_html_attributes( $primary['id'], $primary['class'], $primary['data'], $primary['attr'] ), |
| 553 | esc_attr( $primary['required'] ) |
| 554 | ); |
| 555 | |
| 556 | echo $no_icon; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 557 | } elseif ( 'with_text' === $field_style ) { |
| 558 | printf( |
| 559 | '<input type="radio" %s %s>', |
| 560 | evf_html_attributes( $primary['id'], $primary['class'], $primary['data'], $primary['attr'] ), |
| 561 | esc_attr( $primary['required'] ) |
| 562 | ); |
| 563 | printf( '<label for="%s">%s</label>', esc_attr( $primary['id'] ), esc_html( $no_label ) ); |
| 564 | } elseif ( 'with_icon_text' === $field_style ) { |
| 565 | printf( |
| 566 | '<input type="radio" %s %s>', |
| 567 | evf_html_attributes( $primary['id'], $primary['class'], $primary['data'], $primary['attr'] ), |
| 568 | esc_attr( $primary['required'] ) |
| 569 | ); |
| 570 | |
| 571 | echo $no_icon; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 572 | printf( '<label for="%s">%s</label>', esc_attr( $primary['id'] ), esc_html( $no_label ) ); |
| 573 | } |
| 574 | |
| 575 | echo '</label>'; |
| 576 | echo '</div>'; |
| 577 | } |
| 578 | |
| 579 | |
| 580 | |
| 581 | |
| 582 | /** |
| 583 | * Validates field on form submit. |
| 584 | * |
| 585 | * @param int $field_id Field ID. |
| 586 | * @param array $field_submit Submitted form field data. |
| 587 | * @param array $form_data Form data. |
| 588 | */ |
| 589 | public function validate( $field_id, $field_submit, $form_data ) { |
| 590 | $form_id = $form_data['id']; |
| 591 | $entry = isset( $form_data['entry'] ) ? $form_data['entry'] : array(); |
| 592 | $visible = apply_filters( 'everest_forms_visible_fields', true, $form_data['form_fields'][ $field_id ], $entry, $form_data ); |
| 593 | $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'] : get_option( 'everest_forms_required_validation' ); |
| 594 | |
| 595 | if ( false === $visible ) { |
| 596 | return; |
| 597 | } |
| 598 | |
| 599 | if ( ! empty( $form_data['form_fields'][ $field_id ]['required'] ) && empty( $field_submit ) && '0' !== $field_submit ) { |
| 600 | EVF()->task->errors[ $form_id ][ $field_id ] = $required_message; |
| 601 | update_option( 'evf_validation_error', 'yes' ); |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | /** |
| 606 | * Formats field. |
| 607 | * |
| 608 | * @param int $field_id Field ID. |
| 609 | * @param array $field_submit Submitted form field data. |
| 610 | * @param array $form_data Form data. |
| 611 | * @param mixed $meta_key Meta Key. |
| 612 | */ |
| 613 | public function format( $field_id, $field_submit, $form_data, $meta_key ) { |
| 614 | |
| 615 | $value = '' !== $field_submit ? $field_submit : ''; |
| 616 | $name = ! empty( $form_data['form_fields'][ $field_id ]['label'] ) ? $form_data['form_fields'][ $field_id ]['label'] : ''; |
| 617 | |
| 618 | EVF()->task->form_fields[ $field_id ] = array( |
| 619 | 'name' => sanitize_text_field( $name ), |
| 620 | 'value' => sanitize_text_field( $value ), |
| 621 | 'id' => $field_id, |
| 622 | 'type' => $this->type, |
| 623 | 'meta_key' => $meta_key, |
| 624 | ); |
| 625 | } |
| 626 | } |
| 627 |