WP_CLI

WpHttpCacheManager::filter_pre_http_request()publicWP-CLI 1.0

short circuit wp http api with cached file

Method of the class: WpHttpCacheManager{}

No Hooks.

Return

null. Nothing.

Usage

$WpHttpCacheManager = new WpHttpCacheManager();
$WpHttpCacheManager->filter_pre_http_request( $response, $args, $url );
$response (required)
-
$args (required)
-
$url (required)
-

WpHttpCacheManager::filter_pre_http_request() code 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;
}