Automattic\WooCommerce\Internal\ProductAttributesLookup

Filterer::limit_layered_nav_count_cache_entries │ public static │ WC 10.9.0

Limit the number of query result entries stored in a layered nav count transient.

The layered nav count cache stores many query hashes inside a single transient per taxonomy. Without a cap, bot or faceted-search enumeration can grow that single option without bound.

Method of the class: Filterer{}

Returns

array. Bounded cached count entries.

Usage

$result = Filterer::limit_layered_nav_count_cache_entries( $cached_counts, $current_hash ): array;
$cached_counts(array) (required)
Cached count entries keyed by query hash.
$current_hash(string) (required)
Hash for the query that was just added or read.

Changelog

Since 10.9.0 Introduced.

Filterer::limit_layered_nav_count_cache_entries() code WC 11.1.2

public static function limit_layered_nav_count_cache_entries( array $cached_counts, string $current_hash ): array {
	/**
	 * Maximum number of query result entries to store in each layered nav count transient.
	 *
	 * Set to 0 to disable the cap.
	 *
	 * @hook woocommerce_layered_nav_count_cache_max_entries
	 * @since 10.9.0
	 *
	 * @param int $max_entries Maximum number of cached query result entries. Default 1000.
	 * @return int
	 */
	$max_entries = (int) apply_filters( 'woocommerce_layered_nav_count_cache_max_entries', 1000 );

	if ( $max_entries < 1 || count( $cached_counts ) <= $max_entries ) {
		return $cached_counts;
	}

	if ( isset( $cached_counts[ $current_hash ] ) ) {
		$current_counts = $cached_counts[ $current_hash ];
		unset( $cached_counts[ $current_hash ] );
		$cached_counts[ $current_hash ] = $current_counts;
	}

	return array_slice( $cached_counts, -$max_entries, null, true );
}