',
wpforms_html_attributes( $choice['container']['id'], $choice['container']['class'], $choice['container']['data'], $choice['container']['attr'] )
);
if ( $using_image_choices ) {
// Make sure the image choices are keyboard-accessible.
$choice['label']['attr']['tabindex'] = 0;
if ( wpforms_is_amp() ) {
$choice['label']['attr']['on'] = sprintf(
'tap:AMP.setState(%s)',
wp_json_encode( [ $amp_state_id => $choice['attr']['value'] ] )
);
$choice['label']['attr']['role'] = 'button';
}
if ( is_array( $choice['label']['class'] ) && wpforms_is_empty_string( $label ) ) {
$choice['label']['class'][] = 'wpforms-field-label-inline-empty';
}
// Image choices.
printf(
'';
} elseif ( $using_icon_choices ) {
if ( wpforms_is_amp() ) {
$choice['label']['attr']['on'] = sprintf(
'tap:AMP.setState(%s)',
wp_json_encode( [ $amp_state_id => $choice['attr']['value'] ] )
);
$choice['label']['attr']['role'] = 'button';
}
// Icon Choices.
wpforms()->obj( 'icon_choices' )->field_display( $field, $choice, 'radio' );
} else {
// Normal display.
printf(
'',
wpforms_html_attributes( $choice['id'], $choice['class'], $choice['data'], $choice['attr'] ),
esc_attr( $choice['required'] ),
checked( '1', $choice['default'], false )
);
printf(
'',
wpforms_html_attributes( $choice['label']['id'], $choice['label']['class'], $choice['label']['data'], $choice['label']['attr'] ),
wp_kses_post( $label )
);
}
// Capture the text field for "Other" choice to render separately below the list.
if ( ! empty( $choice['data']['other-choice'] ) ) {
$default_value = '';
$size = ! empty( $field['other_size'] ) ? sanitize_html_class( $field['other_size'] ) : 'medium';
$size_class = 'wpforms-field-' . $size;
// Do not hide the Other input if this choice is set as default.
$hidden_class = ! empty( $choice['default'] ) ? '' : ' wpforms-hidden';
if ( isset( $field['choices'][ $key ]['value'] ) && $field['choices'][ $key ]['value'] !== '' ) {
$default_value = $field['choices'][ $key ]['value'];
}
/**
* Filters the default value of the Other choice field option.
*
* This filter allows modifying what value should be prefilled for the Other choice inputs.
*
* @since 1.9.8.3
*
* @param string $default_value Default value for the "Other" choice input.
* @param array $field Field data and settings.
* @param string $label Field label.
*/
$default_value = apply_filters( 'wpforms_field_radio_other_choice_default_value', $default_value, $field, $label );
$other_atts = [
'name' => "wpforms[fields][{$field['id']}][other]",
'value' => $default_value,
];
if ( empty( $choice['default'] ) ) {
$other_atts['disabled'] = 'disabled';
}
if ( ! empty( $field['other_placeholder'] ) ) {
$other_atts['placeholder'] = $field['other_placeholder'];
}
$other_input_html = sprintf(
'',
wpforms_html_attributes(
"wpforms-{$form_data['id']}-field_{$field['id']}_other",
[ 'wpforms-other-input', 'wpforms-field-required', $size_class, $hidden_class ],
[],
$other_atts
)
);
}
echo '
';
}
echo '
';
// Render the captured "Other" input separately, under the list of options.
if ( ! empty( $other_input_html ) ) {
echo $other_input_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
}
/**
* Validate field.
*
* @since 1.8.2
*
* @param int $field_id Field ID.
* @param string|array $field_submit Submitted field value (raw data).
* @param array $form_data Form data and settings.
*/
public function validate( $field_id, $field_submit, $form_data ) {
$field = $form_data['fields'][ $field_id ];
// Skip validation if field is dynamic and choices are empty.
if ( $this->is_dynamic_choices_empty( $field, $form_data ) ) {
return;
}
parent::validate( $field_id, $field_submit, $form_data );
$this->validate_choices_allowlist( $field_id, $field_submit, $form_data );
}
/**
* Format and sanitize field.
*
* @since 1.0.2
* @since 1.9.8.3 Changed the expected $field_submit from string to mixed as in case with the Other option we can expect the array to arrive here.
*
* @param int $field_id Field ID.
* @param mixed $field_submit Submitted form data.
* @param array $form_data Form data and settings.
*/
public function format( $field_id, $field_submit, $form_data ) {
$field = $form_data['fields'][ $field_id ];
$field_submit = $this->sanitize_choices_submission( $field_submit, $field, $form_data );
$dynamic = ! empty( $field['dynamic_choices'] ) ? $field['dynamic_choices'] : false;
$name = sanitize_text_field( $field['label'] );
$value_raw = is_array( $field_submit ) ? '' : sanitize_text_field( $field_submit );
$data = [
'name' => $name,
'value' => '',
'value_raw' => $value_raw,
'id' => wpforms_validate_field_id( $field_id ),
'type' => $this->type,
];
if ( 'post_type' === $dynamic && ! empty( $field['dynamic_post_type'] ) ) {
// Dynamic population is enabled using post type.
$data['dynamic'] = 'post_type';
$data['dynamic_items'] = absint( $value_raw );
$data['dynamic_post_type'] = $field['dynamic_post_type'];
$post = get_post( $value_raw );
if ( ! empty( $post ) && ! is_wp_error( $post ) && $data['dynamic_post_type'] === $post->post_type ) {
$data['value'] = esc_html( wpforms_get_post_title( $post ) );
}
} elseif ( 'taxonomy' === $dynamic && ! empty( $field['dynamic_taxonomy'] ) ) {
// Dynamic population is enabled using taxonomy.
$data['dynamic'] = 'taxonomy';
$data['dynamic_items'] = absint( $value_raw );
$data['dynamic_taxonomy'] = $field['dynamic_taxonomy'];
$term = get_term( $value_raw, $data['dynamic_taxonomy'] );
if ( ! empty( $term ) && ! is_wp_error( $term ) ) {
$data['value'] = esc_html( wpforms_get_term_name( $term ) );
}
} else {
// Normal processing, dynamic population is off.
$choice_key = '';
// If show_values is true, that means value posted is the raw value
// and not the label. So we need to set label value. Also store
// the choice key.
if ( ! empty( $field['show_values'] ) ) {
foreach ( $field['choices'] as $key => $choice ) {
if ( ! empty( $field_submit ) && $choice['value'] === $field_submit ) {
$data['value'] = sanitize_text_field( isset( $choice['label'] ) ? $choice['label'] : '' );
$choice_key = $key;
break;
}
}
} else {
$data['value'] = $value_raw;
// Determine choice key, this is needed for image choices.
foreach ( $field['choices'] as $key => $choice ) {
/* translators: %s - choice number. */
$label = isset( $choice['label'] ) ? $choice['label'] : '';
/* translators: %s is the choice number. */
if ( $field_submit === $label || $value_raw === sprintf( esc_html__( 'Choice %s', 'wpforms-lite' ), $key ) ) {
$choice_key = $key;
break;
}
}
}
// Images choices are enabled, lookup and store image URL.
if ( ! empty( $choice_key ) && ! empty( $field['choices_images'] ) ) {
$data['image'] = ! empty( $field['choices'][ $choice_key ]['image'] ) ? esc_url_raw( $field['choices'][ $choice_key ]['image'] ) : '';
}
}
// For the Other option the value_raw is the option and the value is text from the input.
if ( is_array( $field_submit ) && ! empty( $field_submit['other'] ) ) {
$data['value'] = sanitize_text_field( $field_submit['other'] );
// Save the flag that the saved value is from the other option field.
$data['is_other'] = true;
foreach ( $field['choices'] as $choice ) {
if ( isset( $choice['other'] ) ) {
$data['value_raw'] = isset( $choice['label'] ) ? $choice['label'] : '';
$data['image'] = ! empty( $field['choices_images'] ) && ! empty( $choice['image'] ) ? esc_url_raw( $choice['image'] ) : '';
break;
}
}
}
// Push field details to be saved.
wpforms()->obj( 'process' )->fields[ $field_id ] = $data;
}
/**
* Export entry field data.
*
* @since 1.9.8.3
*
* @param array|mixed $field Field data.
*
* @return array
*/
public function export_entry_field_data( $field ): array {
$field = (array) $field;
if ( empty( $field['is_other'] ) ) {
return $field;
}
$value = (string) ( $field['value'] ?? '' );
$value_raw = (string) ( $field['value_raw'] ?? '' );
$field['value'] = $value_raw . ': ' . $value;
return $field;
}
/**
* Include a radio field in allowed field types for keyword search.
*
* @since 1.9.8.3
*
* @param array|mixed $fields Array of field types.
*
* @return array
*/
public function add_field_to_anti_spam_keyword_filter( $fields ): array {
$fields[] = $this->type;
return $fields;
}
/**
* Generate the HTML value for a field.
*
* It overrides the parent method because of fields with Other choices.
*
* @since 1.9.8.3
*
* @param mixed $value The value of the field.
* @param array $field Field data and settings.
* @param array $form_data Optional. Additional form data.
* @param string $context Optional. The context in which the value is being rendered.
*
* @return string
*/
public function field_html_value( $value, $field, $form_data = [], $context = '' ) {
if ( empty( $field['type'] ) || $field['type'] !== $this->type ) {
return $value;
}
$other_value = $this->get_other_choice_value( $field );
$field_value = $other_value ?? $value ?? '';
return parent::field_html_value(
$field_value,
$field,
$form_data,
$context
);
}
/**
* Return choice value, including Other label if applicable.
* It overrides the parent method because of fields with Other choices.
*
* @since 1.9.8.3
*
* @param array $field Field settings.
* @param array $form_data Form data.
*
* @return string
*/
protected function get_choices_value( array $field, array $form_data ): string {
$other_value = $this->get_other_choice_value( $field );
return $other_value ?? parent::get_choices_value( $field, $form_data );
}
/**
* Retrieve the value for the "Other" choice option in a field.
*
* This method handles the retrieval of the "Other" choice value, considering
* both raw and processed values. It supports cases where the "Other" choice
* is enabled and attempts to construct a meaningful representation of the value
* based on the available inputs.
*
* @since 1.9.8.3
*
* @param array $field Field data.
*
* @return string|null Returns the constructed "Other" choice value if available,
* or null if the "Other" choice is not enabled.
*/
private function get_other_choice_value( array $field ): ?string {
// Bail out early if it's not an Other choice.
if ( empty( $field['is_other'] ) ) {
return null;
}
$value = $field['value'] ?? '';
$value_raw = $field['value_raw'] ?? '';
if ( wpforms_is_empty_string( $value_raw ) ) {
return (string) $value;
}
// Return a value with a value_raw as a prefix only if both are not empty.
if ( ! wpforms_is_empty_string( $value ) ) {
return sprintf( '%1$s: %2$s', $value_raw, $value );
}
return (string) $value_raw;
}
/**
* Adjust the entry field before saving.
*
* It's necessary for fields with other choices.
*
* @since 1.9.8.3
*
* @param array $field Field data.
* @param array $form_data Form data.
* @param int $entry_id Entry ID.
*
* @return array
* @noinspection PhpUnused
*/
public function save_field( $field, $form_data, $entry_id ) {
if ( empty( $field['type'] ) || $field['type'] !== $this->type ) {
return $field;
}
// Save `value_raw: value` for fields with Other choices.
// It's necessary for search functionality on the Form Entries Overview table.
// Admins should be able to search for entries that have used an other value.
$other_value = $this->get_other_choice_value( $field );
if ( $other_value !== null ) {
$field['value'] = $other_value;
}
return $field;
}
}
new WPForms_Field_Radio();