: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
namespace WPForms\Forms\Fields\PaymentSelect;
* Dropdown payment field.
class Field extends \WPForms_Field {
const STYLE_CLASSIC = 'classic';
const STYLE_MODERN = 'modern';
* Primary class constructor.
// Define field type information.
$this->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->group = 'payment';
'label' => esc_html__( 'First Item', 'wpforms-lite' ),
'label' => esc_html__( 'Second Item', 'wpforms-lite' ),
'label' => esc_html__( 'Third Item', 'wpforms-lite' ),
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.
* @param array $properties Field properties.
* @param array $field Field settings.
* @param array $form_data Form data and settings.
public function field_properties( $properties, $field, $form_data ) {
unset( $properties['inputs']['primary'] );
$form_id = absint( $form_data['id'] );
$field_id = absint( $field['id'] );
$choices = $field['choices'];
// Set options container (<select>) properties.
$properties['input_container'] = [
'class' => [ 'wpforms-payment-price' ],
'id' => "wpforms-{$form_id}-field_{$field_id}",
'name' => "wpforms[fields][{$field_id}]",
foreach ( $choices as $key => $choice ) {
$properties['inputs'][ $key ] = [
'class' => [ "choice-{$key}" ],
'for' => "wpforms-{$form_id}-field_{$field_id}_{$key}",
'class' => [ 'wpforms-field-label-inline' ],
'text' => $choice['label'],
'value' => $choice['value'],
'amount' => wpforms_format_amount( wpforms_sanitize_amount( $choice['value'] ) ),
'id' => "wpforms-{$form_id}-field_{$field_id}_{$key}",
'required' => ! empty( $field['required'] ) ? 'required' : '',
'default' => isset( $choice['default'] ),
// Add class that changes the field size.
if ( ! empty( $field['size'] ) ) {
$properties['input_container']['class'][] = 'wpforms-field-' . esc_attr( $field['size'] );
// Required class for pagebreak validation.
if ( ! empty( $field['required'] ) ) {
$properties['input_container']['class'][] = 'wpforms-field-required';
// Add additional class for container.
! empty( $field['style'] ) &&
in_array( $field['style'], [ self::STYLE_CLASSIC, self::STYLE_MODERN ], true )
$properties['container']['class'][] = "wpforms-field-select-style-{$field['style']}";
if ( $this->is_payment_quantities_enabled( $field ) ) {
$properties['container']['class'][] = ' wpforms-payment-quantities-enabled';
* Get the value, that is used to prefill via dynamic or fallback population.
* Based on field data and current properties.
* @param string $raw_value Value from a GET param, always a string.
* @param string $input Represent a subfield inside the field. May be empty.
* @param array $properties Field properties.
* @param array $field Current field specific data.
* @return array Modified field properties.
protected function get_field_populated_single_property_value( $raw_value, $input, $properties, $field ) {
* When the form is submitted we get from Fallback only values (choice ID).
* As payment-dropdown field doesn't support 'show_values' option -
* we should transform value into label to check against using general logic in parent method.
! is_string( $raw_value ) ||
empty( $field['choices'] ) ||
! is_array( $field['choices'] )
// The form submits only the choice ID, so shortcut for Dynamic when we have a label there.
if ( ! is_numeric( $raw_value ) ) {
return parent::get_field_populated_single_property_value( $raw_value, $input, $properties, $field );
! empty( $field['choices'][ $raw_value ]['label'] ) &&
! empty( $field['choices'][ $raw_value ]['value'] )
return parent::get_field_populated_single_property_value( $field['choices'][ $raw_value ]['label'], $input, $properties, $field );
* Field options panel inside the builder.
* @param array $field Field settings.
public function field_options( $field ) {
$this->field_option( 'basic-options', $field, [ 'markup' => 'open' ] );
$this->field_option( 'label', $field );
$this->field_option( 'choices_payments', $field );
// Show price after item labels.
$fld = $this->field_element(
'slug' => 'show_price_after_labels',
'value' => isset( $field['show_price_after_labels'] ) ? '1' : '0',
'desc' => esc_html__( 'Show price after item labels', 'wpforms-lite' ),
'tooltip' => esc_html__( 'Check this option to show price of the item after the label.', 'wpforms-lite' ),
'slug' => 'show_price_after_labels',
$this->field_element( 'row', $field, $args );
$this->field_option( 'quantity', $field );
$this->field_option( 'description', $field );
$this->field_option( 'required', $field );
$this->field_option( 'basic-options', $field, [ 'markup' => 'close' ] );
* Advanced field options.
$this->field_option( 'advanced-options', $field, [ 'markup' => 'open' ] );
$lbl = $this->field_element(
'value' => esc_html__( 'Style', 'wpforms-lite' ),
'tooltip' => esc_html__( 'Classic style is the default one generated by your browser. Modern has a fresh look and displays all selected options in a single row.', 'wpforms-lite' ),
$fld = $this->field_element(
'value' => ! empty( $field['style'] ) ? $field['style'] : self::STYLE_CLASSIC,
self::STYLE_CLASSIC => esc_html__( 'Classic', 'wpforms-lite' ),
self::STYLE_MODERN => esc_html__( 'Modern', 'wpforms-lite' ),
'content' => $lbl . $fld,
$this->field_option( 'size', $field );
$this->field_option( 'placeholder', $field );
$this->field_option( 'css', $field );
$this->field_option( 'label_hide', $field );
$this->field_option( 'advanced-options', $field, [ 'markup' => 'close' ] );
* Field preview inside the builder.
* @param array $field Field settings.
public function field_preview( $field ) {
$this->field_preview_option( 'label', $field );
! empty( $field['style'] ) &&
$field['style'] === self::STYLE_MODERN
$args['class'] = 'choicesjs-select';
$this->field_preview_option( 'choices', $field, $args );
$this->field_preview_option( 'quantity', $field );
$this->field_preview_option( 'description', $field );
* Field display on the form front-end.
* @param array $field Field data and settings.
* @param array $deprecated Deprecated array of field attributes.
* @param array $form_data Form data and settings.
public function field_display( $field, $deprecated, $form_data ) { // phpcs:ignore Generic.Metrics.CyclomaticComplexity.MaxExceeded
$container = $field['properties']['input_container'];
$field_placeholder = ! empty( $field['placeholder'] ) ? $field['placeholder'] : '';
$is_modern = ! empty( $field['style'] ) && $field['style'] === self::STYLE_MODERN;
$choices = $field['properties']['inputs'];
if ( ! empty( $field['required'] ) ) {
$container['attr']['required'] = 'required';
// Add a class for Choices.js initialization.
$container['class'][] = 'choicesjs-select';
// Add a size-class to data attribute - it is used when Choices.js is initialized.
if ( ! empty( $field['size'] ) ) {
$container['data']['size-class'] = 'wpforms-field-row wpforms-field-' . sanitize_html_class( $field['size'] );
$container['data']['search-enabled'] = $this->is_choicesjs_search_enabled( count( $choices ) );
// Check to see if any of the options were selected by default.
foreach ( $choices as $choice ) {
if ( ! empty( $choice['default'] ) ) {
// Fake placeholder for Modern style.
if ( $is_modern && empty( $field_placeholder ) ) {
$first_choices = reset( $choices );
$field_placeholder = $first_choices['label']['text'];
$field_placeholder .= ! empty( $field['show_price_after_labels'] ) && isset( $first_choices['attr']['value'] ) ? ' - ' . wpforms_format_amount( wpforms_sanitize_amount( $first_choices['attr']['value'] ), true ) : '';
// Preselect default if no other choices were marked as default.
wpforms_html_attributes( $container['id'], $container['class'], $container['data'], $container['attr'] ) // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
if ( ! empty( $field_placeholder ) ) {
'<option value="" class="placeholder" disabled %s>%s</option>',
selected( false, $has_default, false ),
esc_html( $field_placeholder )
// Format string for option.
// `data-custom-properties` - it's a Choices.js attribite and it store a copy of `data-amount` attribute.
$option_format = '<option value="%1$s" data-amount="%2$s" data-custom-properties="%2$s" %3$s>%4$s</option>';
$option_format = '<option value="%1$s" data-amount="%2$s" %3$s>%4$s</option>';
// Build the select options.
foreach ( $choices as $key => $choice ) {
$amount = wpforms_format_amount( wpforms_sanitize_amount( $choice['attr']['value'] ) );
$label = isset( $choice['label']['text'] ) ? $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 ) : '';
$option_format, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
selected( true, ! empty( $choice['default'] ), false ),
$this->display_quantity_dropdown( $field );
* 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()->get( '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()->get( 'process' )->errors[ $form_data['id'] ][ $field_id ] = esc_html__( 'Invalid payment option', 'wpforms-lite' );
* Format and sanitize field.
* @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 ) {
$field = $form_data['fields'][ $field_id ];
$name = ! empty( $field['label'] ) ? sanitize_text_field( $field['label'] ) : '';
if ( ! empty( $field['choices'][ $field_submit ]['value'] ) ) {
$amount = wpforms_sanitize_amount( $field['choices'][ $field_submit ]['value'] );