-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgithub-plugin-updater.php
294 lines (234 loc) · 6.76 KB
/
github-plugin-updater.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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<?php
/*
Plugin Name: GitHub Plugin Updater
Description: Updates plugins that are hosted on GitHub.
Version: 1.0
Author: Codepress
Author URI: http://codepress.nl
License: GPLv2 or later
*/
if ( ! class_exists( 'GitHub_Plugin_Updater' ) ) :
/**
* Register a new GitHub plugin
*
* @param array $config
*/
function github_plugin_updater_register( $config ) {
new GitHub_Plugin_Updater( $config );
}
/**
* Update a WordPress plugin via GitHub
*
* It's best avoided to call when is_admin() would return false.
*
* @version 1.0
*/
class GitHub_Plugin_Updater {
/**
* Stores the config.
*
* @since 1.0
* @var type
*/
protected $config;
/**
* Constructor.
*
* @since 1.0
* @param array $config
*/
public function __construct( $config ) {
$defaults = array(
'repo' => null,
'owner' => null,
'slug' => null,
'access_token' => null,
'http_args' => array(),
);
$this->config = (object) array_merge( $defaults, $config );
// default slug equals the repo name
if ( empty( $this->config->slug ) )
$this->config->slug = $this->config->repo . '/' . $this->config->repo . '.php';
add_filter( 'http_request_args', array( $this, 'add_http_args' ), 10, 2 );
add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'update_available' ) );
if ( isset( $_GET['get-zipball'] ) && $_GET['get-zipball'] == $this->config->slug )
add_action( 'admin_init', array( $this, 'get_zipball' ) );
}
/**
* Call the GitHub API and return a json decoded body.
*
* @since 1.0
* @param string $url
* @see http://developer.github.com/v3/
* @return boolean|object
*/
protected function api( $url ) {
add_filter( 'http_request_args', array( $this, 'add_http_args' ), 10, 2 );
$response = wp_remote_get( $this->get_api_url( $url ) );
remove_filter( 'http_request_args', array( $this, 'add_http_args' ) );
if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) != '200' )
return false;
return json_decode( wp_remote_retrieve_body( $response ) );
}
/**
* Return API url.
*
* @todo Maybe allow a filter to add or modify segments.
* @since 1.0
* @param string $endpoint
* @return string
*/
protected function get_api_url( $endpoint ) {
$segments = array(
'owner' => $this->config->owner,
'repo' => $this->config->repo,
'archive_format' => 'zipball',
);
foreach ( $segments as $segment => $value ) {
$endpoint = str_replace( '/:' . $segment, '/' . $value, $endpoint );
}
if ( ! empty( $this->config->access_token ) )
$endpoint = add_query_arg( 'access_token', $this->config->access_token );
return 'https://api.github.com' . $endpoint;
}
/**
* Reads the remote plugin file.
*
* Uses a transient to limit the calls to the API.
*
* @since 1.0
*/
protected function get_remote_info() {
$remote = get_site_transient( __CLASS__ . ':remote' );
if ( ! $remote ) {
$remote = $this->api( '/repos/:owner/:repo/contents/' . basename( $this->config->slug ) );
if ( $remote )
set_site_transient( __CLASS__ . ':remote', $remote, 60 * 60 );
}
return $remote;
}
/**
* Retrieves the local version from the file header of the plugin
*
* @since 1.0
* @return string|boolean
*/
protected function get_local_version() {
$data = get_plugin_data( WP_PLUGIN_DIR . '/' . $this->config->slug );
if ( ! empty( $data['Version'] ) )
return $data['Version'];
return false;
}
/**
* Retrieves the remote version from the file header of the plugin
*
* @since 1.0
* @return string|boolean
*/
protected function get_remote_version() {
$response = $this->get_remote_info();
if ( ! $response )
return false;
preg_match( '#^\s*Version\:\s*(.*)$#im', base64_decode( $response->content ), $matches );
if ( ! empty( $matches[1] ) )
return $matches[1];
return false;
}
/**
* Hooks into pre_set_site_transient_update_plugins to update from GitHub.
*
* @since 1.0
* @todo fill url with value from remote repostory
* @param $transient
* @return $transient If all goes well, an updated one.
*/
public function update_available( $transient ) {
if ( empty( $transient->checked ) )
return $transient;
$local_version = $this->get_local_version();
$remote_version = $this->get_remote_version();
if ( $local_version && $remote_version && version_compare( $remote_version, $local_version, '>' ) ) {
$plugin = array(
'slug' => dirname( $this->config->slug ),
'new_version' => $remote_version,
'url' => null,
'package' => admin_url( '?get-zipball=' . $this->config->slug ),
);
$transient->response[ $this->config->slug ] = (object) $plugin;
}
return $transient;
}
/**
* Allows to change any args used on downloading the zipball
*
* @since 1.0
* @param type $args
* @return type
*/
public function add_http_args( $args, $url ) {
if ( 0 === strpos( $url, $this->get_api_url( '/repos/:owner/:repo/:archive_format' ) ) ) {
foreach( $this->config->http_args as $name => $value ) {
$args[ $name ] = $value;
}
}
if ( admin_url( '?get-zipball=' . $this->config->slug ) == $url ) {
$cookie = new WP_Http_Cookie( array(
'name' => AUTH_COOKIE,
'path' => ADMIN_COOKIE_PATH,
'value' => $_COOKIE[ AUTH_COOKIE ],
'expires' => 300,
'domain' => defined( 'COOKIE_DOMAIN' ) ? COOKIE_DOMAIN : $_SERVER['HTTP_HOST'],
) );
$args['cookies'][] = $cookie;
}
return $args;
}
/**
* GitHub decides on the root directory in the zipball, but we might disagree.
*
* @since 1.0
*/
public function get_zipball() {
add_filter( 'http_request_args', array( $this, 'add_http_args' ), 10, 2 );
$zipball = download_url( $this->get_api_url( '/repos/:owner/:repo/:archive_format' ) );
remove_filter( 'http_request_args', array( $this, 'add_http_args' ) );
if ( is_wp_error( $zipball ) )
$this->return_404();
$z = new ZipArchive();
if ( true === $z->open( $zipball ) ) {
$length = strlen( $z->getNameIndex( 0 ) );
$status = true;
for ( $i=0; $i<$z->numFiles; $i++ ) {
$name = $z->getNameIndex( $i );
if ( ! $name )
$status = false;
$newname = substr_replace( $name, $this->config->repo, 0, $length - 1 );
if ( ! $z->renameName( $name, $newname ) )
$status = false;
}
$z->close();
if ( $status ) {
header( 'Content-Disposition: attachment; filename=' . $this->config->repo . '.zip' );
header( 'Content-Type: application/zip' );
header( 'Content-Length: ' . filesize( $zipball ) );
ob_clean();
flush();
readfile( $zipball );
unlink( $zipball );
exit;
}
}
unlink( $zipball );
$this->return_404();
}
/**
* Getting the zipball has failed. All hail the zipball.
*
* @since 1.0
*/
protected function return_404() {
header( 'HTTP/1.1 404 Not Found', true, 404 );
exit;
}
}
endif;