Automattic\WooCommerce\Internal\Caches

VersionStringGenerator::get_version │ public │ WC 10.4.0

Get the current version string for an ID.

If no valid version exists and $generate is true, a new version will be created. If no valid version exists and $generate is false, null will be returned.

Cached values that aren't non-empty strings are treated as invalid and are never returned. When $generate is true they are overwritten by the newly generated version; when $generate is false they are deleted, which means a call with $generate set to false can write to the cache.

Method of the class: VersionStringGenerator{}

No Hooks.

Returns

string|null. Version string, or null if not found and $generate is false.

Usage

$VersionStringGenerator = new VersionStringGenerator();
$VersionStringGenerator->get_version( $id, $generate ): ?string;
$id(string) (required)
The ID to get the version string for.
$generate(true|false)
Whether to generate a new version if one doesn't exist.
Default: true

Changelog

Since 10.4.0 Introduced.

VersionStringGenerator::get_version() code WC 11.1.2

public function get_version( string $id, bool $generate = true ): ?string {
	$this->validate_input( $id );

	$cache_key = $this->get_cache_key( $id );
	$found     = false;
	$version   = wp_cache_get( $cache_key, self::CACHE_GROUP, false, $found );

	if ( ! is_string( $version ) || '' === $version ) {
		$entry_exists = $this->cache_entry_exists( $version, $found );

		if ( $entry_exists ) {
			$this->log_invalid_cached_value( $id, $version, $generate );
		}

		if ( $generate ) {
			// The new version is written to the same cache key, replacing the invalid
			// value, so deleting it first would only add a redundant round-trip.
			return $this->generate_version( $id );
		}

		if ( $entry_exists ) {
			wp_cache_delete( $cache_key, self::CACHE_GROUP );
		}

		return null;
	}

	// Refresh the cache lifetime.
	$this->store_version( $id, $version );
	return $version;
}