/home/techb158/public_html/wp-content/plugins/wpforms-lite/src/Forms/Fields/PaymentSingle
NameSizeModeActions
Field.php233390666editdlrm
Edit: /home/techb158/public_html/wp-content/plugins/wpforms-lite/src/Forms/Fields/PaymentSingle/Field.php (23339B)
name = esc_html__( 'Single Item', 'wpforms-lite' ); $this->keywords = esc_html__( 'product, store, ecommerce, pay, payment', 'wpforms-lite' ); $this->type = 'payment-single'; $this->icon = 'fa-file-o'; $this->order = 30; $this->group = 'payment'; $this->hooks(); } /** * Define additional field hooks. * * @since 1.8.2 */ private function hooks() { // Define additional field properties. add_filter( "wpforms_field_properties_{$this->type}", [ $this, 'field_properties' ], 5, 3 ); add_action( 'wpforms_display_field_after', [ $this, 'field_minimum_price_description' ], 10, 2 ); add_filter( 'wpforms_field_preview_class', [ $this, 'preview_field_class' ], 10, 2 ); // 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 ) { // phpcs:ignore Generic.Metrics.CyclomaticComplexity.TooHigh // Basic IDs. $form_id = absint( $form_data['id'] ); $field_id = absint( $field['id'] ); // Set options container (', esc_attr( $placeholder ), esc_attr( $value ) ); $hidden = $this->is_min_price_passed( $field ) ? 'wpforms-hidden' : ''; echo ''; echo '
'; $this->field_preview_option( 'description', $field ); $hidden = ! isset( $field['min_price'] ) || empty( (float) wpforms_sanitize_amount( $field['min_price'] ) ) ? 'wpforms-hidden' : ''; echo '
'; printf( wp_kses( /* translators: %1$s - Minimum Price value. */ __( 'Minimum Price: %1$s', 'wpforms-lite' ), [ 'span' => [ 'class' => [], ], ] ), esc_html( $min_price ) ); echo '
'; echo '

'; esc_html_e( 'Note: Item type is set to hidden and will not be visible when viewing the form.', 'wpforms-lite' ); echo '

'; echo ''; } /** * Field display on the form front-end. * * @since 1.8.2 * * @param array $field Field data and settings. * @param array $deprecated Deprecated field attributes. * @param array $form_data Form data and settings. */ public function field_display( $field, $deprecated, $form_data ) { // Shortcut for easier access. $primary = $field['properties']['inputs']['primary']; $field_format = ! empty( $field['format'] ) ? $field['format'] : self::FORMAT_SINGLE; // Placeholder attribute is only applicable to password, search, tel, text and url inputs, not hidden. // aria-errormessage attribute is not allowed for hidden inputs. if ( ! $this->is_user_defined( $field ) ) { unset( $primary['attr']['placeholder'], $primary['attr']['aria-errormessage'] ); } switch ( $field_format ) { case self::FORMAT_SINGLE: case self::FORMAT_HIDDEN: if ( $field_format === self::FORMAT_SINGLE ) { $price = ! empty( $field['price'] ) ? $field['price'] : 0; $field_label = str_replace( '{price}', '' . esc_html( wpforms_format_amount( wpforms_sanitize_amount( $price ), true ) ) . '', $this->get_single_item_price_label( $field ) ); echo '
'; echo '
'; echo wp_kses( $field_label, [ 'span' => [ 'class' => [], ], ] ); echo '
'; $this->display_quantity_dropdown( $field ); echo '
'; } // Primary price field. printf( '', wpforms_html_attributes( $primary['id'], $primary['class'], $primary['data'], $primary['attr'] ) // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ); break; case self::FORMAT_USER: printf( '', wpforms_html_attributes( $primary['id'], $primary['class'], $primary['data'], $primary['attr'] ) // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ); break; default: break; } } /** * Validate field on form submit. * * @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 ) { $is_required = ! empty( $form_data['fields'][ $field_id ]['required'] ); // If field is required, check for data. if ( empty( $field_submit ) && $is_required ) { wpforms()->obj( 'process' )->errors[ $form_data['id'] ][ $field_id ] = wpforms_get_required_label(); return; } /** * Whether to validate amount or not of the Payment Single item field. * * @since 1.8.4 * * @param bool $validate Whether to validate amount or not. Default true. * @param int $field_id Field ID. * @param string $field_submit Field data submitted by a user. * @param array $form_data Form data and settings. */ $validate_amount = apply_filters( 'wpforms_forms_fields_payment_single_field_validate_amount', true, $field_id, $field_submit, $form_data ); // If field format is not user provided, validate the amount posted. if ( ! empty( $field_submit ) && $validate_amount && ! $this->is_user_defined( $form_data['fields'][ $field_id ] ) ) { $price = wpforms_sanitize_amount( $form_data['fields'][ $field_id ]['price'] ); $submit = wpforms_sanitize_amount( $field_submit ); if ( $price !== $submit ) { wpforms()->obj( 'process' )->errors[ $form_data['id'] ][ $field_id ] = esc_html__( 'Amount mismatch', 'wpforms-lite' ); } } // If field format is provided by user, additionally compare the amount with a minimum price. if ( ! empty( $field_submit ) && $validate_amount && $this->is_user_defined( $form_data['fields'][ $field_id ] ) ) { $submit = wpforms_sanitize_amount( $field_submit ); if ( $submit < 0 ) { wpforms()->obj( 'process' )->errors[ $form_data['id'] ][ $field_id ] = esc_html__( 'Amount can\'t be negative' , 'wpforms-lite' ); } if ( empty( $form_data['fields'][ $field_id ]['min_price'] ) && ! $is_required ) { return; } $min_price = wpforms_sanitize_amount( $form_data['fields'][ $field_id ]['min_price'] ); if ( $submit < $min_price ) { wpforms()->obj( 'process' )->errors[ $form_data['id'] ][ $field_id ] = esc_html__( 'Amount can\'t be less than the required minimum.' , 'wpforms-lite' ); } } } /** * Format and sanitize field. * * @since 1.8.2 * * @param int $field_id Field ID. * @param string $field_submit Field data submitted by a user. * @param array $form_data Form data and settings. */ public function format( $field_id, $field_submit, $form_data ) { $field = $form_data['fields'][ $field_id ]; $name = ! empty( $field['label'] ) ? sanitize_text_field( $field['label'] ) : ''; // Only trust the value if the field has the user defined format OR it is the entry preview. if ( $this->is_user_defined( $field ) || wpforms_is_ajax( 'wpforms_get_entry_preview' ) ) { $amount = wpforms_sanitize_amount( $field_submit ); } else { $amount = wpforms_sanitize_amount( $field['price'] ); } $field_data = [ 'name' => $name, 'value' => wpforms_format_amount( $amount, true ), '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; } /** * Display the minimum price description for the field. * * @since 1.8.6 * * @param array $field Field data and settings. * @param array $form_data Form data and settings. */ public function field_minimum_price_description( $field, $form_data ) { if ( ! $this->is_user_defined( $field ) || ! isset( $field['min_price'] ) || empty( (float) wpforms_sanitize_amount( $field['min_price'] ) ) ) { return; } $description = sprintf( /* translators: %1$s - Minimum Price value. */ __( 'Minimum Price: %1$s', 'wpforms-lite' ), wpforms_format_amount( wpforms_sanitize_amount( $field['min_price'] ), true ) ); printf( '
%s
', esc_html( $description ) ); } /** * Add class to the builder field preview. * * @since 1.8.6 * * @param string $css Class names. * @param array $field Field properties. * * @return string */ public function preview_field_class( $css, $field ) { $css = parent::preview_field_class( $css, $field ); if ( $field['type'] !== $this->type ) { return $css; } if ( ! $this->is_user_defined( $field ) ) { return $css; } if ( $this->is_min_price_passed( $field ) ) { return $css; } $css .= ' min-price-warning'; return $css; } /** * Define if format of field is User Defined. * * @since 1.8.6 * * @param array $field Field data. * * @return bool */ private function is_user_defined( $field ) { return ! empty( $field['format'] ) && $field['format'] === self::FORMAT_USER; } /** * Define if format of field is Single Item. * * @since 1.8.7 * * @param array $field Field data. * * @return bool */ private function is_single_item( $field ) { return empty( $field['format'] ) || $field['format'] === self::FORMAT_SINGLE; } /** * Define if format of field is Hidden. * * @since 1.8.8 * * @param array $field Field data. * * @return bool */ private function is_hidden( $field ) { return empty( $field['format'] ) || $field['format'] === self::FORMAT_HIDDEN; } /** * Define if minimum price is equal or more than default one. * * @since 1.8.6 * * @param array $field Field data. * * @return bool */ private function is_min_price_passed( $field ) { return isset( $field['min_price'] ) && (float) wpforms_sanitize_amount( $field['min_price'] ) >= (float) self::MIN_PRICE_DEFAULT; } }