/home/techb158/public_html/wp-content/plugins/wpforms-lite/src/Forms/Fields/PaymentSelect
NameSizeModeActions
Field.php173260666editdlrm
Edit: /home/techb158/public_html/wp-content/plugins/wpforms-lite/src/Forms/Fields/PaymentSelect/Field.php (17326B)
name = esc_html__( 'Dropdown Items', 'wpforms-lite' ); $this->keywords = esc_html__( 'product, store, ecommerce, pay, payment', 'wpforms-lite' ); $this->type = 'payment-select'; $this->icon = 'fa-caret-square-o-down'; $this->order = 70; $this->group = 'payment'; $this->defaults = [ 1 => [ 'label' => esc_html__( 'First Item', 'wpforms-lite' ), 'value' => '10', 'default' => '', ], 2 => [ 'label' => esc_html__( 'Second Item', 'wpforms-lite' ), 'value' => '25', 'default' => '', ], 3 => [ 'label' => esc_html__( 'Third Item', 'wpforms-lite' ), 'value' => '50', 'default' => '', ], ]; $this->default_settings = [ 'choices' => $this->defaults, ]; $this->hooks(); } /** * Register hooks. * * @since 1.8.2 */ private function hooks() { // 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' ] ); // Customize HTML field value. add_filter( 'wpforms_html_field_value', [ $this, 'field_html_value' ], 10, 4 ); } /** * Define additional field properties. * * @since 1.8.2 * * @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']; // Set options container (', wpforms_html_attributes( $container['id'], $container['class'], $container['data'], $container['attr'] ) // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ); // Optional placeholder. if ( ! empty( $field_placeholder ) || $is_modern ) { printf( '', selected( false, $has_default, false ), esc_html( $field_placeholder ) ); } // Format string for option. if ( $is_modern ) { // The `data-custom-properties` is a Choices.js attribute, and it stores a copy of `data-amount` attribute. $option_format = ''; } else { $option_format = ''; } // Build the select options. foreach ( $choices as $key => $choice ) { $amount = wpforms_format_amount( wpforms_sanitize_amount( $choice['attr']['value'] ) ); $label = $choice['label']['text'] ?? ''; /* translators: %s - item number. */ $label = $label !== '' ? $label : sprintf( esc_html__( 'Item %s', 'wpforms-lite' ), $key ); $label .= ! empty( $field['show_price_after_labels'] ) && isset( $choice['attr']['value'] ) ? ' - ' . wpforms_format_amount( wpforms_sanitize_amount( $choice['attr']['value'] ), true ) : ''; printf( $option_format, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped esc_attr( $key ), esc_attr( $amount ), selected( true, ! empty( $choice['default'] ), false ), esc_html( $label ) ); } echo ''; $this->display_quantity_dropdown( $field ); } /** * Validate field on submitting the form. * * @since 1.8.2 * * @param int $field_id Field ID. * @param string $field_submit Submitted field value (raw data). * @param array $form_data Form data and settings. */ public function validate( $field_id, $field_submit, $form_data ) { // Basic required check - If field is marked as required, check for entry data. if ( ! empty( $form_data['fields'][ $field_id ]['required'] ) && empty( $field_submit ) ) { wpforms()->obj( 'process' )->errors[ $form_data['id'] ][ $field_id ] = wpforms_get_required_label(); } // Validate that the option selected is real. if ( ! empty( $field_submit ) && empty( $form_data['fields'][ $field_id ]['choices'][ $field_submit ] ) ) { wpforms()->obj( 'process' )->errors[ $form_data['id'] ][ $field_id ] = esc_html__( 'Invalid payment option', 'wpforms-lite' ); } } /** * Format and sanitize field. * * @since 1.8.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 ) { $choice_label = ''; $field = $form_data['fields'][ $field_id ]; $name = ! empty( $field['label'] ) ? sanitize_text_field( $field['label'] ) : ''; // Fetch the amount. if ( ! empty( $field['choices'][ $field_submit ]['value'] ) ) { $amount = wpforms_sanitize_amount( $field['choices'][ $field_submit ]['value'] ); } else { $amount = 0; } $value = wpforms_format_amount( $amount, true ); if ( empty( $field_submit ) ) { $value = ''; } elseif ( ! empty( $field['choices'][ $field_submit ]['label'] ) ) { $choice_label = sanitize_text_field( $field['choices'][ $field_submit ]['label'] ); $value = $choice_label . ' - ' . $value; } $field_data = [ 'name' => $name, 'value' => $value, 'value_choice' => $choice_label, 'value_raw' => sanitize_text_field( $field_submit ), 'amount' => wpforms_format_amount( $amount ), 'amount_raw' => $amount, 'currency' => wpforms_get_currency(), 'id' => absint( $field_id ), 'type' => sanitize_key( $this->type ), ]; if ( $this->is_payment_quantities_enabled( $field ) ) { $field_data['quantity'] = $this->get_submitted_field_quantity( $field, $form_data ); } wpforms()->obj( 'process' )->fields[ $field_id ] = $field_data; } /** * Form frontend CSS enqueues. * * @since 1.8.2 * * @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()->obj( 'frontend' )->assets_global() ) { $min = wpforms_get_min_suffix(); wp_enqueue_style( 'wpforms-choicesjs', WPFORMS_PLUGIN_URL . "assets/css/choices{$min}.css", [], '10.2.0' ); } } /** * Form frontend JS enqueues. * * @since 1.8.2 * * @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()->obj( 'frontend' )->assets_global() ) { $this->enqueue_choicesjs_once( $forms ); } } /** * Whether the provided form has a dropdown field with a specified style. * * @since 1.8.2 * * @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 false; } 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 an ajax error message. * * @since 1.8.2 * * @param string|mixed $name Field name for error triggered. * @param array $field Field settings. * @param array $props List of properties. * @param string|string[] $error Error message. * * @return string * @noinspection PhpMissingReturnTypeInspection * @noinspection ReturnTypeCanBeDeclaredInspection */ public function ajax_error_field_name( $name, $field, $props, $error ) { $name = (string) $name; if ( ! isset( $field['type'] ) || $field['type'] !== $this->type ) { return $name; } return $props['input_container']['attr']['name'] ?? ''; } }