forked from wp-cli/wp-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWpHttpCacheManager.php
More file actions
134 lines (120 loc) · 3.12 KB
/
WpHttpCacheManager.php
File metadata and controls
134 lines (120 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
namespace WP_CLI;
use WP_CLI;
/**
* Manage caching with whitelisting
*
* @package WP_CLI
*/
class WpHttpCacheManager {
/**
* @var array map whitelisted urls to keys and ttls
*/
protected $whitelist = [];
/**
* @var FileCache
*/
protected $cache;
/**
* @param FileCache $cache
*/
public function __construct( FileCache $cache ) {
$this->cache = $cache;
// hook into wp http api
add_filter( 'pre_http_request', [ $this, 'filter_pre_http_request' ], 10, 3 );
add_filter( 'http_response', [ $this, 'filter_http_response' ], 10, 3 );
}
/**
* short circuit wp http api with cached file
*/
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;
}
/**
* cache wp http api downloads
*
* @param array $response
* @param array $args
* @param string $url
* @return array
*/
public function filter_http_response( $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 if download was successful
if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
return $response;
}
// cache downloaded file
$this->cache->import( $this->whitelist[ $url ]['key'], $response['filename'] );
return $response;
}
/**
* whitelist a package url
*
* @param string $url
* @param string $group package group (themes, plugins, ...)
* @param string $slug package slug
* @param string $version package version
* @param int $ttl
*/
public function whitelist_package( $url, $group, $slug, $version, $ttl = null ) {
$ext = pathinfo( Utils\parse_url( $url, PHP_URL_PATH ), PATHINFO_EXTENSION );
$key = "$group/$slug-$version.$ext";
$this->whitelist_url( $url, $key, $ttl );
wp_update_plugins();
}
/**
* whitelist a url
*
* @param string $url
* @param string $key
* @param int $ttl
*/
public function whitelist_url( $url, $key = null, $ttl = null ) {
$key = $key ? : $url;
$this->whitelist[ $url ] = [
'key' => $key,
'ttl' => $ttl,
];
}
/**
* check if url is whitelisted
*
* @param string $url
* @return bool
*/
public function is_whitelisted( $url ) {
return isset( $this->whitelist[ $url ] );
}
}