';
// Description.
$this->field_preview_option( 'description', $field );
}
/**
* Field display on the form front-end.
*
* @since 1.8.2
*
* @param array $field Field data and settings.
* @param array $deprecated Deprecated, not used parameter.
* @param array $form_data Form data and settings.
*
* @noinspection HtmlWrongAttributeValue
* @noinspection HtmlUnknownAttribute
*/
public function field_display( $field, $deprecated, $form_data ) {
$primary = $field['properties']['inputs']['primary'];
$type = ! empty( $field['required'] ) ? 'text' : 'hidden';
$attrs = $primary['attr'];
if ( ! empty( $field['required'] ) ) {
$attrs['style'] = 'position:absolute!important;clip:rect(0,0,0,0)!important;height:1px!important;width:1px!important;border:0!important;overflow:hidden!important;padding:0!important;margin:0!important;';
$attrs['readonly'] = 'readonly';
}
// aria-errormessage attribute is not allowed for hidden inputs.
unset( $attrs['aria-errormessage'] );
$is_summary_enabled = $this->is_summary_enabled( $field );
// Prepare data for the order summary preview if summary is enabled, or we are on the editor page.
if ( $is_summary_enabled || wpforms_is_editor_page() ) {
[ $items, $foot, $total_width ] = $this->prepare_payment_fields_data( $form_data );
}
if ( $is_summary_enabled ) {
/**
* Allow filtering form data before displaying the order summary table.
*
* @since 1.9.3
*
* @param array $form_data Form data.
*
* @return array
*/
$form_data = apply_filters( 'wpforms_forms_fields_payment_total_field_display_form_data', $form_data );
// Summary preview.
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo wpforms_render(
'fields/total/summary-preview',
[
'items' => $items,
'foot' => $foot,
'total_width' => $total_width,
],
true
);
}
$amount = wpforms_format_amount( 0, true );
// If we are on the editor page, we need to get the total amount from the last item in the foot.
if ( ! empty( $foot ) && wpforms_is_editor_page() ) {
$foot_item = end( $foot );
$amount = $foot_item['amount'] ?? 0;
}
// Always print total to cover a case when a field is embedded into a Layout column with 25% width.
$hidden_style = $is_summary_enabled ? 'display:none' : '';
// This displays the total the user sees.
printf(
'
%2$s
',
esc_attr( $hidden_style ),
esc_html( $amount )
);
// Hidden input for processing.
printf(
'',
esc_attr( $type ),
wpforms_html_attributes( $primary['id'], $primary['class'], $primary['data'], $attrs ) // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
);
}
/**
* Validate field on form submitting.
*
* @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 a field is marked as required, check for entry data.
if ( ! empty( $form_data['fields'][ $field_id ]['required'] ) && ( empty( $field_submit ) || wpforms_sanitize_amount( $field_submit ) <= 0 ) ) {
wpforms()->obj( 'process' )->errors[ $form_data['id'] ][ $field_id ] = esc_html__( 'Payment is required.', 'wpforms-lite' );
}
}
/**
* Format and sanitize field.
*
* @since 1.8.2
*
* @param int $field_id Field ID.
* @param string $field_submit Field value submitted by a user.
* @param array $form_data Form data and settings.
*/
public function format( $field_id, $field_submit, $form_data ) {
// Define data.
$name = ! empty( $form_data['fields'][ $field_id ]['label'] ) ? $form_data['fields'][ $field_id ]['label'] : '';
$amount = wpforms_sanitize_amount( $field_submit );
// Set final field details.
wpforms()->obj( 'process' )->fields[ $field_id ] = [
'name' => sanitize_text_field( $name ),
'value' => wpforms_format_amount( $amount, true ),
'amount' => wpforms_format_amount( $amount ),
'amount_raw' => $amount,
'id' => absint( $field_id ),
'type' => sanitize_key( $this->type ),
];
}
/**
* Summary option.
*
* @since 1.8.7
*
* @param array $field Field data and settings.
*/
private function summary_option( array $field ): void {
$is_allowed = RequirementsAlerts::is_order_summary_allowed();
$toggle_data = [
'slug' => 'summary',
'value' => $this->is_summary_enabled( $field ),
'desc' => esc_html__( 'Enable Summary', 'wpforms-lite' ),
'tooltip' => esc_html__( 'Enable order summary for this field.', 'wpforms-lite' ),
];
if ( ! $is_allowed ) {
$toggle_data['attrs'] = [ 'disabled' => 'disabled' ];
$toggle_data['control-class'] = 'wpforms-toggle-control-disabled';
}
$output = $this->field_element(
'toggle',
$field,
$toggle_data,
false
);
$this->field_element(
'row',
$field,
[
'slug' => 'summary',
'content' => $output,
]
);
if ( ! $is_allowed ) {
$this->field_element(
'row',
$field,
[
'slug' => 'summary_alert',
'content' => RequirementsAlerts::get_order_summary_alert(),
]
);
}
}
/**
* Summary notice on the options' tab.
*
* @since 1.8.7
*
* @param array $field Field data and settings.
*/
private function summary_option_notice( array $field ): void {
$notice = __( 'Example data is shown in the form editor. Actual products and totals will be displayed when you preview or embed your form.', 'wpforms-lite' );
$is_notice_hidden = ! $this->is_summary_enabled( $field ) ? 'wpforms-hidden' : '';
printf(
'
';
}
/**
* Calculates the total amount for a single payment field based on its price and quantity.
*
* @since 1.9.5
*
* @param array $field The payment field data containing the price.
* @param int $quantity The quantity of the field specified.
*
* @return float|int The calculated total amount for the payment field.
*/
private function get_payment_field_single_amount( array $field, int $quantity ) {
if ( empty( $field['price'] ) ) {
return 0;
}
return wpforms_sanitize_amount( $field['price'] ) * $quantity;
}
/**
* Determines if a field is conditionally hidden based on its settings and conditions.
*
* Note: This is a simplified implementation that assumes fields with 'show' conditional
* logic are hidden by default, without evaluating the actual conditions. This approach
* was chosen to avoid complex condition evaluation during form rendering.
*
* @since 1.9.5
*
* @param array $field Field data, including conditional logic settings.
*
* @return bool True if the field is conditionally hidden, false otherwise.
*/
private function is_conditionally_hidden( array $field ): bool {
return wpforms()->is_pro() &&
wpforms_conditional_logic_fields()->field_is_conditional( $field ) &&
( $field['conditional_type'] ?? '' ) === 'show';
}
}