-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathUpdater.php
More file actions
203 lines (186 loc) · 4.26 KB
/
Updater.php
File metadata and controls
203 lines (186 loc) · 4.26 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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php // phpcs:disable WordPress.Files.FileName
/**
* Gridd Theme Updater.
*
* @package Gridd
* @since 1.1
*/
namespace Gridd;
/**
* Updater class.
*
* The theme-review process on w.org takes months.
* In the meantime this can serve as a simple updater.
*/
class Updater {
/**
* The repository.
*
* @access private
* @since 1.0
* @var string
*/
private $repo;
/**
* Theme name.
*
* @access private
* @since 1.0
* @var string
*/
private $name;
/**
* Theme slug.
*
* @access private
* @since 1.0
* @var string
*/
private $slug;
/**
* Theme version.
*
* @access private
* @since 1.1
* @var string
*/
private $ver;
/**
* Theme URL.
*
* @access private
* @since 1.0
* @var string
*/
private $url;
/**
* The response from the API.
*
* @access private
* @since 1.0
* @var array
*/
private $response;
/**
* Constructor.
*
* @access public
* @since 1.0
* @param array $args The arguments for this theme.
*/
public function __construct( $args ) {
$this->name = $args['name'];
$this->slug = $args['slug'];
$this->repo = $args['repo'];
$this->ver = $args['ver'];
$this->url = $args['url'];
$this->response = $this->get_response();
// Check for theme updates.
add_filter( 'http_request_args', [ $this, 'update_check' ], 5, 2 );
// Inject theme updates into the response array.
add_filter( 'pre_set_site_transient_update_themes', [ $this, 'update_themes' ] );
add_filter( 'pre_set_transient_update_themes', [ $this, 'update_themes' ] );
}
/**
* Gets the releases URL.
*
* @access private
* @since 1.0
* @return string
*/
private function get_releases_url() {
return 'https://api.github.com/repos/' . $this->repo . '/releases';
}
/**
* Get the response from the Github API.
*
* @access private
* @since 1.0
* @return array
*/
private function get_response() {
// Check transient.
$cache = get_site_transient( md5( $this->get_releases_url() ) );
if ( $cache ) {
return $cache;
}
$response = wp_remote_get( $this->get_releases_url() );
if ( ! is_wp_error( $response ) && 200 === wp_remote_retrieve_response_code( $response ) ) {
$response = json_decode( wp_remote_retrieve_body( $response ), true );
set_site_transient( md5( $this->get_releases_url() ), $response, 12 * HOUR_IN_SECONDS );
}
}
/**
* Get the new version file.
*
* @access private
* @since 1.0
* @return string
*/
private function get_latest_package() {
if ( ! $this->response ) {
return;
}
foreach ( $this->response as $release ) {
if ( isset( $release['assets'] ) && isset( $release['assets'][0] ) && isset( $release['assets'][0]['browser_download_url'] ) ) {
return $release['assets'][0]['browser_download_url'];
}
}
}
/**
* Get the new version.
*
* @access private
* @since 1.0
* @return string
*/
private function get_latest_version() {
if ( ! $this->response ) {
return;
}
foreach ( $this->response as $release ) {
if ( isset( $release['tag_name'] ) ) {
return str_replace( 'v', '', $release['tag_name'] );
}
}
}
/**
* Disables requests to the wp.org repository for this theme.
*
* @since 1.0
*
* @param array $request An array of HTTP request arguments.
* @param string $url The request URL.
* @return array
*/
public function update_check( $request, $url ) {
if ( false !== strpos( $url, '//api.wordpress.org/themes/update-check/1.1/' ) ) {
$data = json_decode( $request['body']['themes'] );
unset( $data->themes->{$this->slug} );
$request['body']['themes'] = wp_json_encode( $data );
}
return $request;
}
/**
* Inject update data for this theme.
*
* @since 1.0
*
* @param object $transient The pre-saved value of the `update_themes` site transient.
* @return object
*/
public function update_themes( $transient ) {
if ( isset( $transient->checked ) ) {
$current_version = $this->ver;
if ( version_compare( $current_version, $this->get_latest_version(), '<' ) ) {
$transient->response[ $this->name ] = [
'theme' => $this->name,
'new_version' => $this->get_latest_version(),
'url' => 'https://github.com/' . $this->repo . '/releases',
'package' => $this->get_latest_package(),
];
}
}
return $transient;
}
}