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.

Метод класса: WC_Helper_Updater{}

Хуков нет.

Возвращает

array. Update data for each requested product.

Использование

$result = WC_Helper_Updater::_update_check( $payload );
$payload(массив) (обязательный)
Information about the plugin to update.

Код WC_Helper_Updater::_update_check() WC 11.3.0

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'];
	}

	/*
	 * After a failed or rate-limited update-check, skip the remote call until the
	 * backoff window passes. This check ignores the payload hash, so a changed
	 * payload or flushed cache can't slip past it, but the Marketplace "Refresh"
	 * button clears it. Return the last cached products, if any.
	 */
	if ( WC_Helper_API_Backoff::is_rate_limited( WC_Helper_API_Backoff::REQUEST_TYPE_UPDATE_CHECK ) ) {
		return self::get_cached_products( $data, $hash );
	}

	$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 );
	$products      = 200 === $response_code ? json_decode( wp_remote_retrieve_body( $request ), true ) : null;
	if ( ! is_array( $products ) ) {
		$data['errors'][] = 'http-error';

		// On an outage (including a 200 that isn't JSON), leave the cache untouched and back off, so a failed check doesn't hide extension updates for 12 hours.
		if ( self::is_temporary_failure( $request, $response_code ) ) {
			if ( 429 === $response_code && is_array( $request ) ) {
				WC_Helper_API_Backoff::record_from_response( WC_Helper_API_Backoff::REQUEST_TYPE_UPDATE_CHECK, $request );
			} else {
				WC_Helper_API_Backoff::record( WC_Helper_API_Backoff::REQUEST_TYPE_UPDATE_CHECK, 15 * MINUTE_IN_SECONDS );
			}

			return self::get_cached_products( $cached_data, $hash );
		}
	} else {
		$data['products'] = $products;
	}

	set_transient( $cache_key, $data, WEEK_IN_SECONDS );
	delete_transient( '_woocommerce_helper_updates_count' );
	return $data['products'];
}