: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
namespace WPForms\Forms\Fields\PaymentTotal;
use WPForms\Forms\Fields\Helpers\RequirementsAlerts;
class Field extends \WPForms_Field {
* Primary class constructor.
// Define field type information.
$this->name = esc_html__( 'Total', 'wpforms-lite' );
$this->keywords = esc_html__( 'store, ecommerce, pay, payment, sum', 'wpforms-lite' );
$this->type = 'payment-total';
$this->icon = 'fa-money';
$this->group = 'payment';
private function hooks() {
// Define additional field properties.
add_filter( "wpforms_field_properties_{$this->type}", [ $this, 'field_properties' ], 5, 3 );
// Recalculate total for a form.
add_filter( 'wpforms_process_filter', [ $this, 'calculate_total' ], 10, 3 );
// Add classes to the builder field preview.
add_filter( 'wpforms_field_preview_class', [ $this, 'preview_field_class' ], 10, 2 );
// Add new option on the confirmation page.
add_action( 'wpforms_form_settings_confirmations_single_after', [ $this, 'add_confirmation_setting' ], 10, 2 );
add_action( 'wpforms_lite_form_settings_confirmations_single_after', [ $this, 'add_confirmation_setting' ], 10, 2 );
add_action( 'wpforms_frontend_confirmation_message_after', [ $this, 'order_summary_confirmation' ], 10, 4 );
* Define additional field properties.
* @param array $properties Field properties.
* @param array $field Field data and settings.
* @param array $form_data Form data and settings.
public function field_properties( $properties, $field, $form_data ) {
// Input Primary: initial total is always zero.
$properties['inputs']['primary']['attr']['value'] = '0';
// Input Primary: add class for targeting calculations.
$properties['inputs']['primary']['class'][] = 'wpforms-payment-total';
// Input Primary: add data attribute if total is required.
if ( ! empty( $field['required'] ) ) {
$properties['inputs']['primary']['data']['rule-required-payment'] = true;
if ( ! empty( $field['size'] ) ) {
$properties['container']['class'][] = 'wpforms-field-' . esc_attr( $field['size'] );
// Input Primary: add class for targeting summary.
if ( $this->is_summary_enabled( $field ) ) {
$properties['container']['class'][] = 'wpforms-summary-enabled';
// Unset for attribute for label.
unset( $properties['label']['attr']['for'] );
* Whether current field can be populated dynamically.
* @param array $properties Field properties.
* @param array $field Current field specific data.
public function is_dynamic_population_allowed( $properties, $field ) {
* Whether current field can be populated dynamically.
* @param array $properties Field properties.
* @param array $field Current field specific data.
public function is_fallback_population_allowed( $properties, $field ) {
* Do not trust the posted total since that relies on javascript.
* Instead we re-calculate server side.
* @param array $fields List of fields with their data.
* @param array $entry Submitted form data.
* @param array $form_data Form data and settings.
public function calculate_total( $fields, $entry, $form_data ) {
return self::calculate_total_static( $fields, $entry, $form_data );
* Static version of calculate_total().
* @param array $fields List of fields with their data.
* @param array $entry Submitted form data.
* @param array $form_data Form data and settings.
public static function calculate_total_static( $fields, $entry, $form_data ) {
if ( ! is_array( $fields ) ) {
// At this point we have passed processing and validation, so we know
// the amounts in $fields are safe to use.
$total = wpforms_get_total_payment( $fields );
$amount = wpforms_sanitize_amount( $total );
foreach ( $fields as $id => $field ) {
if ( ! empty( $field['type'] ) && $field['type'] === 'payment-total' ) {
$fields[ $id ]['value'] = wpforms_format_amount( $amount, true );
$fields[ $id ]['amount'] = wpforms_format_amount( $amount );
$fields[ $id ]['amount_raw'] = $amount;
* Field options panel inside the builder.
* @param array $field Field data and settings.
public function field_options( $field ) {
$this->field_option( 'basic-options', $field, $args );
$this->field_option( 'label', $field );
$this->field_option( 'description', $field );
$this->summary_option( $field );
$this->summary_option_notice( $field );
$this->field_option( 'required', $field );
$this->field_option( 'basic-options', $field, $args );
* Advanced field options.
$this->field_option( 'advanced-options', $field, $args );
$this->field_option( 'size', $field, [ 'exclude' => [ 'small' ] ] );
$this->field_option( 'css', $field );
$this->field_option( 'label_hide', $field );
$this->field_option( 'advanced-options', $field, $args );
* Field preview inside the builder.
* @param array $field Field data and settings.
public function field_preview( $field ) {
$this->field_preview_option( 'label', $field );
list( $items, $foot, $total_width ) = $this->prepare_builder_preview_data();
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
'fields/total/summary-preview',
'total_width' => $total_width,
echo '<div class="wpforms-total-amount">' . esc_html( wpforms_format_amount( 0, true ) ) . '</div>';
$this->field_preview_option( 'description', $field );
* Field display on the form front-end.
* @param array $field Field data and settings.
* @param array $deprecated Deprecated, not used parameter.
* @param array $form_data Form data and settings.
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 );
if ( $is_summary_enabled ) {
list( $items, $foot, $total_width ) = $this->prepare_payment_fields_data( $form_data );
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
'fields/total/summary-preview',
'total_width' => $total_width,
// Always print total to cover a case when field is embedded into Layout column with 25% width.
$hidden_style = $is_summary_enabled ? 'display:none' : '';
// This displays the total the user sees.
'<div class="wpforms-payment-total" style="%1$s">%2$s</div>',
esc_attr( $hidden_style ),
esc_html( wpforms_format_amount( 0, true ) )
// Hidden input for processing.
wpforms_html_attributes( $primary['id'], $primary['class'], $primary['data'], $attrs ) // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
* Validate field on form submit.
* @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_sanitize_amount( $field_submit ) <= 0 ) ) {
wpforms()->get( 'process' )->errors[ $form_data['id'] ][ $field_id ] = esc_html__( 'Payment is required.', 'wpforms-lite' );
* Format and sanitize field.
* @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 ) {
$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()->get( 'process' )->fields[ $field_id ] = [
'name' => sanitize_text_field( $name ),
'value' => wpforms_format_amount( $amount, true ),
'amount' => wpforms_format_amount( $amount ),
'id' => absint( $field_id ),
'type' => sanitize_key( $this->type ),
* @param array $field Field data and settings.
private function summary_option( array $field ) {
$is_allowed = RequirementsAlerts::is_order_summary_allowed();
'value' => $this->is_summary_enabled( $field ),
'desc' => esc_html__( 'Enable Summary', 'wpforms-lite' ),
'tooltip' => esc_html__( 'Enable order summary for this field.', 'wpforms-lite' ),
$toggle_data['attrs'] = [ 'disabled' => 'disabled' ];
$toggle_data['control-class'] = 'wpforms-toggle-control-disabled';
$output = $this->field_element(
'slug' => 'summary_alert',
'content' => RequirementsAlerts::get_order_summary_alert(),
* Summary notice on the options tab.
* @param array $field Field data and settings.
private function summary_option_notice( array $field ) {
$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' : '';
'<div class="wpforms-alert-info wpforms-alert wpforms-total-summary-alert %1$s">
esc_attr( $is_notice_hidden ),
* Determine if summary option is enabled.
* @param array $field Field data and settings.
private function is_summary_enabled( array $field ) {
return ! empty( $field['summary'] );
* Prepare fake fields data for builder preview.
private function prepare_builder_preview_data(): array {
'label' => __( 'Example Product 1', 'wpforms-lite' ),
'amount' => wpforms_format_amount( 30, true ),
'label' => __( 'Example Product 2', 'wpforms-lite' ),
'amount' => wpforms_format_amount( 20, true ),
'label' => __( 'Example Product 3', 'wpforms-lite' ),
'amount' => wpforms_format_amount( 10, true ),
* Allow to filter items in the footer on the order summary table (builder screen).