WP_CLI

WpHttpCacheManager::filter_pre_http_request()publicWP-CLI 1.0

short circuit wp http api with cached file

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

Хуков нет.

Возвращает

null. Ничего (null).

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

$WpHttpCacheManager = new WpHttpCacheManager();
$WpHttpCacheManager->filter_pre_http_request( $response, $args, $url );
$response (обязательный)
-
$args (обязательный)
-
$url (обязательный)
-

Код WpHttpCacheManager::filter_pre_http_request() WP-CLI 2.8.0-alpha

public function filter_pre_http_request( $response, $args, $url ) {
	// check if whitelisted
	if ( ! isset( $this->whitelist[ $url ] ) ) {
		return $response;
	}
	// check if downloading
	if ( 'GET' !== $args['method'] || empty( $args['filename'] ) ) {
		return $response;
	}
	// check cache and export to designated location
	$filename = $this->cache->has( $this->whitelist[ $url ]['key'], $this->whitelist[ $url ]['ttl'] );
	if ( $filename ) {
		WP_CLI::log( sprintf( 'Using cached file \'%s\'...', $filename ) );
		if ( copy( $filename, $args['filename'] ) ) {
			// simulate successful download response
			return [
				'response' => [
					'code'    => 200,
					'message' => 'OK',
				],
				'filename' => $args['filename'],
			];
		}

		WP_CLI::error( sprintf( 'Error copying cached file %s to %s', $filename, $url ) );
	}
	return $response;
}