-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathBFIGitHubPluginUploader.php
More file actions
376 lines (267 loc) · 11.5 KB
/
BFIGitHubPluginUploader.php
File metadata and controls
376 lines (267 loc) · 11.5 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
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
<?php
class BFIGitHubPluginUpdater {
private $slug; // plugin slug
private $pluginData; // plugin data
private $username; // GitHub username
private $repo; // GitHub repo name
private $pluginFile; // __FILE__ of our plugin
private $githubAPIResult; // holds data from GitHub
private $githubAPIResults; // holds data from GitHub
private $accessToken; // GitHub private repo token
public $headers; // GitHub private repo token
private $tested; // plugin info
private $requires; // plugin info
function __construct( $pluginFile, $gitHubUsername, $gitHubProjectName, $pluginData, $accessToken = '' ) {
add_filter( "pre_set_site_transient_update_plugins", array( $this, "setTransitent" ) );
add_filter( "plugins_api", array( $this, "setPluginInfo" ), 10, 3 );
add_filter( "upgrader_post_install", array( $this, "postInstall" ), 10, 3 );
$this->pluginFile = $pluginFile;
$this->username = $gitHubUsername;
$this->repo = $gitHubProjectName;
$this->accessToken = $accessToken;
$this->headers = '';
$this->tested = $pluginData['tested'];
$this->requires = $pluginData['requires'];
}
// Get information regarding our plugin from WordPress
private function initPluginData() {
$this->slug = plugin_basename( $this->pluginFile );
$this->pluginData = get_plugin_data( $this->pluginFile );
}
public function admin_notice() {
echo '<div class="notice notice-error is-dismissible">';
echo '<p><strong>Die Verfügbarkeit von Updates für das Plugin "WordPressToolset" kann im Moment nicht geprüft werden.</strong><br>';
echo 'Das Limit von ' . $this->headers['x-ratelimit-limit'] . ' API-Verbindungen zum <a href="https://github.com/johannheyne/wordpress-toolset" target="_blank">GitHub Repository des Plugins</a> war erreicht. Das Limit wird am ' . date( 'd.m.Y', $this->headers['x-ratelimit-reset'] ) . ' um ' . date( 'H:i', $this->headers['x-ratelimit-reset'] ) . ' Uhr wieder zurückgesetzt. Bitte prüfe dann noch einmal auf Updates.</p>';
echo '</div>';
}
// Get information regarding our plugin from GitHub
private function getRepoReleaseInfo() {
// Only do this once
if (
empty( $_GET['force-check'] ) AND
!empty( $this->githubAPIResult )
) {
return;
}
if ( !empty( $_GET['force-check'] ) ) {
$old_release_data = get_transient( 'plugin_wordpress_toolset_latest_release_data' );
delete_transient( 'plugin_wordpress_toolset_latest_release_data' );
}
if ( false === ( $this->githubAPIResults = get_transient( 'plugin_wordpress_toolset_latest_release_data' ) ) ) {
// Query the GitHub API
$url = "https://api.github.com/repos/{$this->username}/{$this->repo}/releases";
// We need the access token for private repos
/*if ( !empty( $this->accessToken ) ) {
$url = add_query_arg( array( "access_token" => $this->accessToken ), $url );
}*/
$args = array(
'headers' => array(
'Accept' => 'application/vnd.github.v3+json',
//'Authorization' => 'token ' . $this->accessToken,
)
);
// Get the results
$response = wp_remote_get( $url, $args );
// CHECK GITHUB API CALL LIMIT {
$this->headers = wp_remote_retrieve_headers( $response );
$this->headers = (array) $this->headers;
$this->headers = $this->headers[ chr(0) . '*' . chr(0) . 'data']; //trick to get protected array key
// LIMIT REACHED {
if ( $this->headers['x-ratelimit-remaining'] == '0' ) {
add_action( 'admin_notices', array( $this, 'admin_notice' ) );
if ( !empty( $old_release_data ) ) {
// recover transient
set_transient( 'plugin_wordpress_toolset_latest_release_data', $old_release_data, HOUR_IN_SECONDS );
// recover release data
$this->githubAPIResults = $old_release_data;
}
}
// }
// LIMIT NOT REACHED {
else {
$response_body = wp_remote_retrieve_body( $response );
$this->githubAPIResults = @json_decode( $response_body );
}
// }
// }
// Set the transient and var by remote release data
if (
!empty( $this->githubAPIResults ) AND
is_array( $this->githubAPIResults )
) {
set_transient( 'plugin_wordpress_toolset_latest_release_data', $this->githubAPIResults, DAY_IN_SECONDS );
}
}
// Set the transient and var by remote release data
if (
!empty( $this->githubAPIResults ) AND
is_array( $this->githubAPIResults )
) {
$this->githubAPIResult = $this->githubAPIResults[0]; // Use only the latest release
}
}
// Push in plugin version information to get the update notification
public function setTransitent( $transient ) {
// If we have checked the plugin data before, don't re-check
if ( empty( $transient->checked ) ) {
return $transient;
}
// Get plugin & GitHub release information
$this->initPluginData();
$this->getRepoReleaseInfo();
// Check the versions if we need to do an update
$doUpdate = version_compare( $this->githubAPIResult->tag_name, $transient->checked[$this->slug] );
// Update the transient to include our updated plugin data
if ( $doUpdate == 1 ) {
$package = $this->githubAPIResult->zipball_url;
// Include the access token for private GitHub repos
if ( !empty( $this->accessToken ) ) {
$package = add_query_arg( array( "access_token" => $this->accessToken ), $package );
}
$obj = new stdClass();
$obj->slug = $this->slug;
$obj->new_version = $this->githubAPIResult->tag_name;
$obj->url = $this->pluginData["PluginURI"];
$obj->package = $package;
$obj->tested = $this->tested;
$obj->require = $this->requires;
$transient->response[$this->slug] = $obj;
}
return $transient;
}
public function infoWindowStyles() {
echo '<style type="text/css">
#plugin-information pre {
display: block !important;
background: #f7f7f7 !important;
overflow: auto !important;
}
#plugin-information pre code {
display: block !important;
background: none !important;
//min-width: 1000px !important;
}
</style>';
}
// Push in plugin version information to display in the details lightbox
public function setPluginInfo( $false, $action, $response ) {
// Get plugin & GitHub release information
$this->initPluginData();
$this->getRepoReleaseInfo();
// If nothing is found, do nothing
if ( empty( $response->slug ) || $response->slug != $this->slug ) {
return false;
}
$this->infoWindowStyles();
// Add our plugin information
$response->last_updated = $this->githubAPIResult->published_at;
$response->slug = $this->slug;
$response->name = $this->pluginData["Name"];
$response->plugin_name = $this->pluginData["Name"];
$response->version = $this->githubAPIResult->tag_name;
$response->author = $this->pluginData["AuthorName"];
$response->homepage = $this->pluginData["PluginURI"];
$response->tested = $this->tested;
$response->requires = $this->requires;
// This is our release download zip file
$downloadLink = $this->githubAPIResult->zipball_url;
// Include the access token for private GitHub repos
if ( !empty( $this->accessToken ) ) {
$downloadLink = add_query_arg(
array( "access_token" => $this->accessToken ),
$downloadLink
);
}
$response->download_link = $downloadLink;
// We're going to parse the GitHub markdown release notes, include the parser
require_once( plugin_dir_path( __FILE__ ) . "Parsedown.php" );
// DESCRIPTION CONTENT {
$description_content[ 0 ] = '#Changes Since Last Plugin Update';
$description_content[ 1 ] = 'This is the only chance to see what happend since the last plugin update. So please read the changes carefully and adapt your theme-code, before you update the plugin.';
if ( is_array( $this->githubAPIResults ) and count( $this->githubAPIResults ) > 0 ) {
foreach ( $this->githubAPIResults as $key => $item ) {
if ( version_compare( $this->pluginData['Version'], $item->tag_name, '<' ) ) {
$description_content[ $item->tag_name ] = '......................................................................................................................' . "\n\n";
$description_content[ $item->tag_name ] .= '[Release v' . $item->tag_name . '](https://github.com/johannheyne/wordpress-toolset/releases/tag/' . $item->tag_name . ')' . "\n\n";
//$description_content[ $item->tag_name ] .= date( "Y.m.d H.i.s", strtotime( $item->published_at ) ) . "\n\n";
$description_content[ $item->tag_name ] .= str_replace( '####', '##', $item->body );
$description_content[ $item->tag_name ] = preg_replace( "/requires WordPress:\s([\d\.]+)/i", '', $description_content[ $item->tag_name ] );
$description_content[ $item->tag_name ] = preg_replace( "/tested WordPress:\s([\d\.]+)/i", '', $description_content[ $item->tag_name ] );
$description_content[ $item->tag_name ] = preg_replace( "/requires PHP:\s([\d\.]+)/i", '', $description_content[ $item->tag_name ] );
}
}
}
$description_content[ 9999999998 ] = '......................................................................................................................' . "\n\n";
$description_content[ 9999999999 ] = '<a href="' . $response->homepage . '/releases" target="_blank">You can also go to the releases and their changes on GitHub.</a> ';
if ( ! empty( $description_content ) ) {
$description_content = implode( "\n\n", $description_content );
}
// }
// Create tabs in the lightbox
$response->sections = array(
'description' => $this->pluginData["Description"],
'changelog' => class_exists( "Parsedown" )
? Parsedown::instance()->parse( $description_content )
: $description_content
);
// Gets the required version of WP if available {
$matches = null;
$all_matches = array();
if ( is_array( $this->githubAPIResults ) and count( $this->githubAPIResults ) > 0 ) {
foreach ( $this->githubAPIResults as $key => $item ) {
preg_match( "/requires WordPress:\s([\d\.]+)/i", $item->body, $matches );
if ( !empty( $matches ) ) {
if ( is_array( $matches ) ) {
if ( count( $matches ) > 1 ) {
$all_matches[ $matches[1] ] = $matches[1];
}
}
}
}
}
if ( count( $all_matches ) != 0 ) {
krsort( $all_matches );
$response->requires = reset( $all_matches );
}
// }
// Gets the tested version of WP if available {
if ( is_array( $this->githubAPIResults ) and count( $this->githubAPIResults ) > 0 ) {
$matches = null;
$all_matches = null;
foreach ( $this->githubAPIResults as $key => $item ) {
preg_match( "/tested WordPress:\s([\d\.]+)/i", $item->body, $matches );
if ( !empty( $matches ) ) {
if ( is_array( $matches ) ) {
if ( count( $matches ) > 1 ) {
$all_matches[ $matches[1] ] = $matches[1];
}
}
}
}
}
if ( $all_matches ) {
krsort( $all_matches );
$response->tested = reset( $all_matches );
}
// }
return $response;
}
// Perform additional actions to successfully install our plugin
public function postInstall( $true, $hook_extra, $result ) {
// Get plugin information
$this->initPluginData();
// Since we are hosted in GitHub, our plugin folder would have a dirname of
// reponame-tagname change it to our original one:
global $wp_filesystem;
$pluginFolder = WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . dirname( $this->slug );
$wp_filesystem->move( $result['destination'], $pluginFolder );
$result['destination'] = $pluginFolder;
// Remember if our plugin was previously activated
// $wasActivated = is_plugin_active( $this->slug );
// Re-activate plugin if needed
//if ( $wasActivated ) {
$activate = activate_plugin( $this->slug );
//}
return $result;
}
}