WC_Helper_Updater::_update_check │ private static │ WC 1.0

Run an update check API call.

The call is cached based on the payload (product ids, file ids, installed versions). If the payload changes, the cache is going to miss. The installed version has to be part of the key: the server decides the autoupdate flag from it, so a response cached under another version, after an update or a rollback, would carry a decision made for a build that is no longer installed.

Method of the class: WC_Helper_Updater{}

No Hooks.

Returns

array. Update data for each requested product.

Usage

$result = WC_Helper_Updater::_update_check( $payload );
$payload(array) (required)
Information about the plugin to update.

WC_Helper_Updater::_update_check() code WC 11.2.2

private static function _update_check( $payload ) {
	if ( empty( $payload ) ) {
		return array();
	}
	ksort( $payload );

	$hash = md5( wp_json_encode( $payload ) );

	$cache_key = '_woocommerce_helper_updates';
	$data      = get_transient( $cache_key );

	if ( self::should_use_cached_update_data( $data, $hash ) ) {
		return $data['products'];
	}

	// If a previous update-check was rate limited (HTTP 429), honor the
	// server's reset window and skip the remote call until it passes. This
	// backoff is independent of the payload hash above, so a changed payload
	// (or a flushed cache) can't slip past it — but clicking the Marketplace
	// "Refresh" button bypasses and clears it. Return the last cached
	// products, if any, rather than an empty set.
	if ( WC_Helper_API_Backoff::is_rate_limited( WC_Helper_API_Backoff::REQUEST_TYPE_UPDATE_CHECK ) ) {
		return self::get_cached_products( $data );
	}

	$cached_data = $data;

	$data = array(
		'hash'     => $hash,
		'updated'  => time(),
		'products' => array(),
		'errors'   => array(),
	);

	// Detect if this is a manual refresh button click.
	$source = WC_Helper_API_Backoff::is_refresh_request() ? 'refresh-button' : '';

	$request_body = array( 'products' => $payload );
	if ( ! empty( $source ) ) {
		$request_body['source'] = $source;
	}

	if ( WC_Helper::is_site_connected() ) {
		$request = WC_Helper_API::post(
			'update-check',
			array(
				'body'          => wp_json_encode( $request_body ),
				'authenticated' => true,
			)
		);
	} else {
		$request = WC_Helper_API::post(
			'update-check-public',
			array(
				'body' => wp_json_encode( $request_body ),
			)
		);
	}

	$response_code = (int) wp_remote_retrieve_response_code( $request );
	if ( 200 !== $response_code ) {
		$data['errors'][] = 'http-error';

		// Respect server-side rate limiting: on a 429, record the reset window so
		// we hold off on further update-check calls until then, and return the
		// previously cached products without touching the cache. Caching this
		// empty result for 12 hours would outlive the reset window, and it would
		// discard the very products the backoff branch above serves while we wait.
		if ( 429 === $response_code && is_array( $request ) ) {
			WC_Helper_API_Backoff::record_from_response( WC_Helper_API_Backoff::REQUEST_TYPE_UPDATE_CHECK, $request );

			return self::get_cached_products( $cached_data );
		}
	} else {
		$data['products'] = json_decode( wp_remote_retrieve_body( $request ), true );
	}

	set_transient( $cache_key, $data, 12 * HOUR_IN_SECONDS );
	return $data['products'];
}