name = esc_html__( 'Dropdown', 'wpforms-lite' ); $this->keywords = esc_html__( 'choice', 'wpforms-lite' ); $this->type = 'select'; $this->icon = 'fa-caret-square-o-down'; $this->order = 70; $this->defaults = [ 1 => [ 'label' => esc_html__( 'First Choice', 'wpforms-lite' ), 'value' => '', 'default' => '', ], 2 => [ 'label' => esc_html__( 'Second Choice', 'wpforms-lite' ), 'value' => '', 'default' => '', ], 3 => [ 'label' => esc_html__( 'Third Choice', 'wpforms-lite' ), 'value' => '', 'default' => '', ], ]; // Define additional field properties. add_filter( 'wpforms_field_properties_' . $this->type, [ $this, 'field_properties' ], 5, 3 ); // Form frontend CSS enqueues. add_action( 'wpforms_frontend_css', [ $this, 'enqueue_frontend_css' ] ); // Form frontend JS enqueues. add_action( 'wpforms_frontend_js', [ $this, 'enqueue_frontend_js' ] ); add_action( 'enqueue_block_editor_assets', [ $this, 'enqueue_block_editor_assets' ] ); } /** * 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 !== false ) { $choices = $dynamic; $field['show_values'] = true; } // Set options container (', wpforms_html_attributes( $container['id'], $container['class'], $container['data'], $container['attr'] ) ); // Optional placeholder. if ( ! empty( $field_placeholder ) ) { printf( '', selected( false, $has_default || $is_multiple, 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 ''; } /** * Validate field. * * @since 1.8.2 * * @param int $field_id Field ID. * @param string|array $field_submit Submitted field value (selected option). * @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 ); } /** * Format and sanitize field. * * @since 1.0.2 * @since 1.6.1 Added a support for multiple values. * * @param int $field_id Field ID. * @param string|array $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; $multiple = ! empty( $field['multiple'] ); $name = sanitize_text_field( $field['label'] ); $value = []; // Convert submitted field value to array. if ( ! is_array( $field_submit ) ) { $field_submit = [ $field_submit ]; } $value_raw = wpforms_sanitize_array_combine( $field_submit ); $data = [ '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 (like for a `Checkboxes` field). $value_raw = implode( ',', array_map( 'absint', $field_submit ) ); $data['value_raw'] = $value_raw; $data['dynamic'] = 'post_type'; $data['dynamic_items'] = $value_raw; $data['dynamic_post_type'] = $field['dynamic_post_type']; $posts = []; foreach ( $field_submit as $id ) { $post = get_post( $id ); if ( ! is_wp_error( $post ) && ! empty( $post ) && $data['dynamic_post_type'] === $post->post_type ) { $posts[] = esc_html( wpforms_get_post_title( $post ) ); } } $data['value'] = ! empty( $posts ) ? wpforms_sanitize_array_combine( $posts ) : ''; } elseif ( 'taxonomy' === $dynamic && ! empty( $field['dynamic_taxonomy'] ) ) { // Dynamic population is enabled using taxonomy (like for a `Checkboxes` field). $value_raw = implode( ',', array_map( 'absint', $field_submit ) ); $data['value_raw'] = $value_raw; $data['dynamic'] = 'taxonomy'; $data['dynamic_items'] = $value_raw; $data['dynamic_taxonomy'] = $field['dynamic_taxonomy']; $terms = []; foreach ( $field_submit as $id ) { $term = get_term( $id, $field['dynamic_taxonomy'] ); if ( ! is_wp_error( $term ) && ! empty( $term ) ) { $terms[] = esc_html( wpforms_get_term_name( $term ) ); } } $data['value'] = ! empty( $terms ) ? wpforms_sanitize_array_combine( $terms ) : ''; } 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'] ) && (int) $field['show_values'] === 1 ) { foreach ( $field_submit as $item ) { foreach ( $field['choices'] as $choice ) { if ( $item === $choice['value'] ) { $value[] = $choice['label']; break; } } } $data['value'] = ! empty( $value ) ? wpforms_sanitize_array_combine( $value ) : ''; } else { $data['value'] = $value_raw; } } // Backward compatibility: for single dropdown save a string, for multiple - array. if ( ! $multiple && is_array( $data ) && ( 1 === count( $data ) ) ) { $data = reset( $data ); } // Push field details to be saved. wpforms()->process->fields[ $field_id ] = $data; } /** * Form frontend CSS enqueues. * * @since 1.6.1 * * @param array $forms Forms on the current page. */ public function enqueue_frontend_css( $forms ) { $has_modern_select = false; foreach ( $forms as $form ) { if ( $this->is_field_style( $form, self::STYLE_MODERN ) ) { $has_modern_select = true; break; } } if ( $has_modern_select || wpforms()->frontend->assets_global() ) { $min = wpforms_get_min_suffix(); wp_enqueue_style( 'wpforms-choicesjs', WPFORMS_PLUGIN_URL . "assets/css/choices{$min}.css", [], self::CHOICES_VERSION ); } } /** * Form frontend JS enqueues. * * @since 1.6.1 * * @param array $forms Forms on the current page. */ public function enqueue_frontend_js( $forms ) { $has_modern_select = false; foreach ( $forms as $form ) { if ( $this->is_field_style( $form, self::STYLE_MODERN ) ) { $has_modern_select = true; break; } } if ( $has_modern_select || wpforms()->frontend->assets_global() ) { $this->enqueue_choicesjs_once( $forms ); } } /** * Load WPForms Gutenberg block scripts. * * @since 1.8.1 */ public function enqueue_block_editor_assets() { $min = wpforms_get_min_suffix(); wp_enqueue_style( 'wpforms-choicesjs', WPFORMS_PLUGIN_URL . "assets/css/choices{$min}.css", [], self::CHOICES_VERSION ); $this->enqueue_choicesjs_once( [] ); } /** * Whether the provided form has a dropdown field with a specified style. * * @since 1.6.1 * * @param array $form Form data. * @param string $style Desired field style. * * @return bool */ protected function is_field_style( $form, $style ) { $is_field_style = false; if ( empty( $form['fields'] ) ) { return $is_field_style; } foreach ( (array) $form['fields'] as $field ) { if ( ! empty( $field['type'] ) && $field['type'] === $this->type && ! empty( $field['style'] ) && sanitize_key( $style ) === $field['style'] ) { $is_field_style = true; break; } } return $is_field_style; } /** * Get field name for ajax error message. * * @since 1.6.3 * * @param string $name Field name for error triggered. * @param array $field Field settings. * @param array $props List of properties. * @param string $error Error message. * * @return string */ public function ajax_error_field_name( $name, $field, $props, $error ) { if ( ! isset( $field['type'] ) || 'select' !== $field['type'] ) { return $name; } if ( ! empty( $field['multiple'] ) ) { $input = isset( $props['inputs'] ) ? end( $props['inputs'] ) : []; return isset( $input['attr']['name'] ) ? $input['attr']['name'] . '[]' : ''; } return $name; } } new WPForms_Field_Select();