WC_Checkout::get_posted_data()publicWC 3.1.0

Get posted data from the checkout form.

Метод класса: WC_Checkout{}

Возвращает

Массив. of data.

Использование

$WC_Checkout = new WC_Checkout();
$WC_Checkout->get_posted_data();

Список изменений

С версии 3.1.0 Введена.

Код WC_Checkout::get_posted_data() WC 8.7.0

public function get_posted_data() {
	// phpcs:disable WordPress.Security.NonceVerification.Missing
	$data = array(
		'terms'                              => (int) isset( $_POST['terms'] ),
		'terms-field'                        => (int) isset( $_POST['terms-field'] ),
		'createaccount'                      => (int) ( $this->is_registration_enabled() ? ! empty( $_POST['createaccount'] ) : false ),
		'payment_method'                     => isset( $_POST['payment_method'] ) ? wc_clean( wp_unslash( $_POST['payment_method'] ) ) : '',
		'shipping_method'                    => isset( $_POST['shipping_method'] ) ? wc_clean( wp_unslash( $_POST['shipping_method'] ) ) : '',
		'ship_to_different_address'          => ! empty( $_POST['ship_to_different_address'] ) && ! wc_ship_to_billing_address_only(),
		'woocommerce_checkout_update_totals' => isset( $_POST['woocommerce_checkout_update_totals'] ),
	);
	// phpcs:enable WordPress.Security.NonceVerification.Missing

	$skipped        = array();
	$form_was_shown = isset( $_POST['woocommerce-process-checkout-nonce'] ); // phpcs:disable WordPress.Security.NonceVerification.Missing

	foreach ( $this->get_checkout_fields() as $fieldset_key => $fieldset ) {
		if ( $this->maybe_skip_fieldset( $fieldset_key, $data ) ) {
			$skipped[] = $fieldset_key;
			continue;
		}

		foreach ( $fieldset as $key => $field ) {
			$type = sanitize_title( isset( $field['type'] ) ? $field['type'] : 'text' );

			if ( isset( $_POST[ $key ] ) && '' !== $_POST[ $key ] ) { // phpcs:disable WordPress.Security.NonceVerification.Missing
				$value = wp_unslash( $_POST[ $key ] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
			} elseif ( isset( $field['default'] ) && 'checkbox' !== $type && ! $form_was_shown ) {
				$value = $field['default'];
			} else {
				$value = '';
			}

			if ( '' !== $value ) {
				switch ( $type ) {
					case 'checkbox':
						$value = 1;
						break;
					case 'multiselect':
						$value = implode( ', ', wc_clean( $value ) );
						break;
					case 'textarea':
						$value = wc_sanitize_textarea( $value );
						break;
					case 'password':
						if ( $data['createaccount'] && 'account_password' === $key ) {
							$value = wp_slash( $value ); // Passwords are encrypted with slashes on account creation, so we need to slash here too.
						}
						break;
					default:
						$value = wc_clean( $value );
						break;
				}
			}

			$data[ $key ] = apply_filters( 'woocommerce_process_checkout_' . $type . '_field', apply_filters( 'woocommerce_process_checkout_field_' . $key, $value ) );
		}
	}

	if ( in_array( 'shipping', $skipped, true ) && ( WC()->cart->needs_shipping_address() || wc_ship_to_billing_address_only() ) ) {
		foreach ( $this->get_checkout_fields( 'shipping' ) as $key => $field ) {
			$data[ $key ] = isset( $data[ 'billing_' . substr( $key, 9 ) ] ) ? $data[ 'billing_' . substr( $key, 9 ) ] : '';
		}
	}

	// BW compatibility.
	$this->legacy_posted_data = $data;

	return apply_filters( 'woocommerce_checkout_posted_data', $data );
}