name = esc_html__( 'Dropdown', 'wpforms-lite' ); $this->type = 'select'; $this->icon = 'fa-caret-square-o-down'; $this->order = 70; $this->defaults = array( 1 => array( 'label' => esc_html__( 'First Choice', 'wpforms-lite' ), 'value' => '', 'default' => '', ), 2 => array( 'label' => esc_html__( 'Second Choice', 'wpforms-lite' ), 'value' => '', 'default' => '', ), 3 => array( 'label' => esc_html__( 'Third Choice', 'wpforms-lite' ), 'value' => '', 'default' => '', ), ); // Define additional field properties. add_filter( 'wpforms_field_properties_' . $this->type, array( $this, 'field_properties' ), 5, 3 ); } /** * Define additional field properties. * * @since 1.5.0 * * @param array $properties Field properties. * @param array $field Field settings. * @param array $form_data Form data and settings. * * @return array */ public function field_properties( $properties, $field, $form_data ) { // Remove primary input. unset( $properties['inputs']['primary'] ); // Define data. $form_id = absint( $form_data['id'] ); $field_id = absint( $field['id'] ); $choices = $field['choices']; $dynamic = wpforms_get_field_dynamic_choices( $field, $form_id, $form_data ); if ( $dynamic ) { $choices = $dynamic; $field['show_values'] = true; } // Set options container (', (int) $field['id'], wpforms_html_attributes( $container['id'], $container['class'], $container['data'] ), $field_required ); // WPCS: XSS ok. // Optional placeholder. if ( ! empty( $field_placeholder ) ) { printf( '', selected( false, $has_default, false ), esc_html( $field_placeholder ) ); } // Build the select options. foreach ( $choices as $key => $choice ) { printf( '', esc_attr( $choice['attr']['value'] ), selected( true, ! empty( $choice['default'] ), false ), esc_html( $choice['label']['text'] ) ); } echo ''; } /** * Formats and sanitizes field. * * @since 1.0.2 * * @param int $field_id Field ID. * @param string $field_submit Submitted field value (selected option). * @param array $form_data Form data and settings. */ public function format( $field_id, $field_submit, $form_data ) { $field = $form_data['fields'][ $field_id ]; $dynamic = ! empty( $field['dynamic_choices'] ) ? $field['dynamic_choices'] : false; $name = sanitize_text_field( $field['label'] ); $value_raw = sanitize_text_field( $field_submit ); $value = ''; $data = array( 'name' => $name, 'value' => '', 'value_raw' => $value_raw, 'id' => absint( $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 ( ! is_wp_error( $post ) && ! empty( $post ) && $data['dynamic_post_type'] === $post->post_type ) { $data['value'] = esc_html( $post->post_title ); } } 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 ( ! is_wp_error( $term ) && ! empty( $term ) ) { $data['value'] = esc_html( $term->name ); } } else { // Normal processing, dynamic population is off. // If show_values is true, that means values posted are the raw values // and not the labels. So we need to get the label values. if ( ! empty( $field['show_values'] ) && '1' == $field['show_values'] ) { foreach ( $field['choices'] as $choice ) { if ( $choice['value'] === $field_submit ) { $value = $choice['label']; break; } } $data['value'] = sanitize_text_field( $value ); } else { $data['value'] = $value_raw; } } // Push field details to be saved. wpforms()->process->fields[ $field_id ] = $data; } } new WPForms_Field_Select();