Automattic\WooCommerce\Internal\ProductFilters

FilterData::normalize_query_varsprivateWC 10.8.0

Normalise query vars for cache key generation so that logically equivalent filter combinations produce the same hash.

Rules applied (cache key only – the original $query_vars are never modified):

  • All keys are sorted alphabetically (ksort).
  • Values for keys that start with "filter_", equal "rating_filter", or are built-in taxonomy short-names ("categories", "tags", "brands"): comma-separated items are trimmed, lower-cased, sorted, then re-joined.
  • Values for keys that start with "query_type_": trimmed and lower-cased.
  • Values for "min_price" / "max_price": trimmed.

Method of the class: FilterData{}

No Hooks.

Returns

Array. Normalised copy of $query_vars.

Usage

// private - for code of main (parent) class only
$result = $this->normalize_query_vars( $query_vars ): array;
$query_vars(array) (required)
Raw query vars.

Changelog

Since 10.8.0 Introduced.

FilterData::normalize_query_vars() code WC 10.9.1

private function normalize_query_vars( array $query_vars ): array {
	// Built-in taxonomy filter params that are treated as unordered sets.
	// See Params::get_taxonomy_params() for the source of these short names.
	$taxonomy_set_params = array( 'categories', 'tags', 'brands' );

	ksort( $query_vars );

	foreach ( $query_vars as $key => $value ) {
		if ( ! is_string( $key ) || ! is_string( $value ) ) {
			continue;
		}

		if ( str_starts_with( $key, 'filter_' ) || 'rating_filter' === $key || in_array( $key, $taxonomy_set_params, true ) ) {
			$pieces = array_map( 'trim', explode( ',', $value ) );
			$pieces = array_map( 'strtolower', $pieces );
			$pieces = array_values( array_unique( array_filter( $pieces, static fn( string $p ): bool => '' !== $p ) ) );
			sort( $pieces );
			$query_vars[ $key ] = implode( ',', $pieces );
		} elseif ( str_starts_with( $key, 'query_type_' ) ) {
			$query_vars[ $key ] = strtolower( trim( $value ) );
		} elseif ( 'min_price' === $key || 'max_price' === $key ) {
			$query_vars[ $key ] = trim( $value );
		}
	}

	return $query_vars;
}