-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhost-cache.php
173 lines (133 loc) · 4.88 KB
/
host-cache.php
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
/**
* Plugin Name: Host Cache
* Description: Downloads WordPress plugins, themes, and updates from the official repo through a proxy cache.
* Version: 0.1.0
* Author: Talkington Tech
* Author URI: http://talkingtontech.com
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/
class Host_Cache {
public $options;
public function __construct( $args = array() ) {
$this->options = wp_parse_args( $args, array(
'host' => 'wpcache.host',
'ssl' => false,
'catchall' => false,
'arg_nocache' => 'nocache'
) );
add_action( 'init', array( $this, 'init' ), 10 );
}
public function init() {
add_filter( 'pre_set_site_transient_update_core', array( $this, 'core_updates' ), 10 );
add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'plugin_updates' ), 10 );
add_filter( 'pre_set_site_transient_update_themes', array( $this, 'theme_updates' ), 10 );
add_filter( 'plugins_api_result', array( $this, 'plugins_api_result' ), 10, 2 );
add_filter( 'themes_api_result', array( $this, 'themes_api_result' ), 10, 2 );
add_filter( 'pre_http_request', array( $this, 'http_request_catchall' ), 10, 3 );
add_filter( 'http_request_host_is_external', array( $this, 'http_request_always_allow' ), 10, 3 );
}
private function cached_download_url( $url ) {
$scheme = $this->options['ssl'] ? 'https' : 'http';
$url = str_replace( array( 'downloads.wordpress.org', 'wordpress.org' ), $this->options['host'], $url );
$url = preg_replace( '#^\w+://#', $scheme . '://', $url );
if ( !preg_match( '/\d+(\.\d+)+/m', $url ) ) {
$url = add_query_arg( $this->options['arg_nocache'], 1, $url );
}
return $url;
}
public function core_updates( $transient ) {
if ( !isset( $transient->updates ) ) {
return $transient;
}
foreach ( $transient->updates as $item => $update_details ) {
if ( $this->is_download_url( $update_details->download ) ) {
$update_details->download = $this->cached_download_url( $update_details->download );
}
if ( isset( $update_details->packages ) ) {
$packages = get_object_vars( $update_details->packages );
foreach ( $packages as $package => $value ) {
if ( !$this->is_download_url( $value ) ) {
continue;
}
$update_details->packages->$package = $this->cached_download_url( $value );
}
}
$transient->updates[$item] = $update_details;
}
return $transient;
}
public function http_request_always_allow( $result, $host, $url ) {
if ( $this->options['host'] === $host ) {
return true;
}
return $result;
}
public function http_request_catchall( $result, $request_args, $url ) {
if ( !$this->options['catchall'] || !$this->is_download_url( $url ) ) {
return $result;
}
$url = $this->cached_download_url( $url );
$http = _wp_http_get_object();
return $http->request( $url, $request_args );
}
private function is_download_url( $url ) {
if ( false !== strpos( $url, 'downloads.wordpress.org' ) ) {
return true;
} else if ( false !== strpos( $url, 'wordpress.org/themes/download/' ) ) {
return true;
}
return false;
}
public function plugin_updates( $transient ) {
if ( !isset( $transient->response ) ) {
return $transient;
}
foreach ( $transient->response as $item => $update_details ) {
if ( !$this->is_download_url( $update_details->package ) ) {
continue;
}
$update_details->package = $this->cached_download_url( $update_details->package );
$transient->response[$item] = $update_details;
}
return $transient;
}
public function plugins_api_result( $result, $action ) {
if ( is_wp_error( $result ) ) {
return $result;
}
if ( 'plugin_information' === $action ) {
if ( $this->is_download_url( $result->download_link ) ) {
$result->download_link = $this->cached_download_url( $result->download_link );
}
}
return $result;
}
public function theme_updates( $transient ) {
if ( !isset( $transient->response ) ) {
return $transient;
}
foreach ( $transient->response as $item => $update_details ) {
if ( !$this->is_download_url( $update_details['package'] ) ) {
continue;
}
$update_details['package'] = $this->cached_download_url( $update_details['package'] );
$transient->response[$item] = $update_details;
}
return $transient;
}
public function themes_api_result( $result, $action ) {
if ( is_wp_error( $result ) ) {
return $result;
}
if ( 'theme_information' === $action ) {
if ( $this->is_download_url( $result->download_link ) ) {
$result->download_link = $this->cached_download_url( $result->download_link );
}
}
return $result;
}
}
global $host_cache;
$host_cache = new Host_Cache();