Automattic\WooCommerce\Internal\StockNotifications\Frontend

SignupService::parse_posted_attributes │ private │ WC 1.0

Parse variation attributes from source data.

This method extracts attributes that are defined as 'any' in the variation and need to be explicitly specified during signup. These attributes cannot be retrieved directly from the variation since they are not fixed values.

For example, if a t-shirt variation has 'any' size but a specific color, we need to capture the chosen size from the form submission while the color comes from the variation itself.

Only 'any' attributes are read from the request. Every attribute the variation fixes is already identified by the variation ID, so a posted value for one carries no information and is ignored.

Posted values are checked against the attribute's declared values, the same way WC_Cart::add_to_cart() checks them for an 'any' attribute, so a request cannot store a value the store never offered and mint a sign-up row that no later request can match.

Method of the class: SignupService{}

No Hooks.

Returns

array|\WP_Error. The posted attributes, or a WP_Error if a posted value is too long or not one the store offers.

Usage

// private - for code of main (parent) class only
$result = $this->parse_posted_attributes( $source, $variation );
$source(array) (required)
The source data, e.g. $_POST or $_REQUEST.
$variation(WC_Product) (required)
The variation.

Notes

See: [WC_Cart::add_to_cart()](/plugin/woocommerce/function/WC_Cart::add_to_cart) for similar attribute parsing and validation logic.

SignupService::parse_posted_attributes() code WC 11.3.0

private function parse_posted_attributes( array $source, \WC_Product $variation ) {

	if ( ! $variation instanceof \WC_Product_Variation ) {
		return array();
	}

	$product = wc_get_product( $variation->get_parent_id() );
	if ( ! $product ) {
		return array();
	}

	// Empty values are the 'any' attributes, so what is left is the set the variation fixes.
	$fixed_attributes = array_filter( $variation->get_variation_attributes(), 'wc_array_filter_default_attributes' );

	$posted_attributes = array();
	foreach ( $product->get_attributes() as $attribute ) {
		if ( ! $attribute instanceof \WC_Product_Attribute || ! $attribute['is_variation'] ) {
			continue;
		}

		$attribute_key = 'attribute_' . sanitize_title( $attribute['name'] );
		if ( isset( $fixed_attributes[ $attribute_key ] ) || ! isset( $source[ $attribute_key ] ) ) {
			continue;
		}

		// A request can post the value as an array, which the string sanitizers below cannot take.
		$raw_value = wp_unslash( $source[ $attribute_key ] );
		if ( ! is_string( $raw_value ) ) {
			return new \WP_Error( self::ERROR_INVALID_REQUEST );
		}

		if ( $attribute['is_taxonomy'] ) {
			$value = sanitize_title( $raw_value );
		} else {
			$value = html_entity_decode( wc_clean( $raw_value ), ENT_QUOTES, get_bloginfo( 'charset' ) );
		}

		// Don't include if it's empty.
		if ( empty( $value ) && '0' !== $value ) {
			continue;
		}

		// Length first, so an oversized value is rejected without reading the declared ones.
		if ( strlen( $value ) > self::MAX_ATTRIBUTE_LENGTH || ! in_array( $value, $attribute->get_slugs(), true ) ) {
			return new \WP_Error( self::ERROR_INVALID_REQUEST );
		}

		$posted_attributes[ $attribute_key ] = $value;
	}

	return $posted_attributes;
}