This repository has been archived by the owner on Nov 30, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
acf-beta-tester.php
447 lines (296 loc) · 8.27 KB
/
acf-beta-tester.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
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
<?php
/*
Plugin Name: Advanced Custom Fields Beta Tester
Plugin URI: http://www.advancedcustomfields.com/
Description: Get access to the latest versions of Advanced Custom Fields
Version: 1.0.0
Author: Elliot Condon
Author URI: http://www.elliotcondon.com/
License: GPL
Copyright: Elliot Condon
*/
if( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
if( !class_exists('acf_beta_tester') ):
class acf_beta_tester {
/** @var array An array of plugin info */
var $settings;
/** @var string The selected version used to update */
var $version = '';
/**
* __construct
*
* This function will setup the class functionality
*
* @type function
* @date 12/9/17
* @since 1.0.0
*
* @param n/a
* @return n/a
*/
function __construct() {
// vars
$this->settings = array(
// basic
'name' => __('Advanced Custom Fields Beta Tester', 'acf'),
'version' => '1.0.0',
// urls
'slug' => dirname(plugin_basename( __FILE__ )),
'basename' => plugin_basename( __FILE__ ),
'path' => plugin_dir_path( __FILE__ ),
'dir' => plugin_dir_url( __FILE__ ),
);
// actions
if( is_admin() ) {
add_action( 'init', array($this, 'init'), 10 );
}
}
/**
* init
*
* This function will run after WP is initialized
*
* @type function
* @date 11/9/17
* @since 1.0.0
*
* @param n/a
* @return n/a
*/
function init() {
// modify plugins transient
add_filter( 'pre_set_site_transient_update_plugins', array($this, 'modify_plugins_transient'), 10, 1 );
// allow custom version to be selected
if( current_user_can('install_plugins') ) {
add_filter( 'plugin_row_meta', array($this, 'plugin_row_meta'), 10, 4 );
add_action( 'load-plugins.php', array($this, 'load'), 10 );
}
}
/**
* request
*
* This function will make a request to an external server
*
* @type function
* @date 8/4/17
* @since 1.0.0
*
* @param $url (string)
* @param $body (array)
* @return (mixed)
*/
function request( $url = '', $body = null ) {
// post
$raw_response = wp_remote_post($url, array(
'timeout' => 10,
'body' => $body
));
// wp error
if( is_wp_error($raw_response) ) {
return $raw_response;
// http error
} elseif( wp_remote_retrieve_response_code($raw_response) != 200 ) {
return new WP_Error( 'server_error', wp_remote_retrieve_response_message($raw_response) );
}
// vars
$raw_body = wp_remote_retrieve_body($raw_response);
// attempt object
$obj = @unserialize( $raw_body );
if( $obj ) return $obj;
// attempt json
$json = json_decode( $raw_body, true );
if( $json ) return $json;
// return
return $json;
}
/**
* get_plugin_info
*
* This function will get plugin info and save as transient
*
* @type function
* @date 9/4/17
* @since 1.0.0
*
* @param n/a
* @return (array)
*/
function get_plugin_info() {
// var
$transient_name = 'acf_beta_tester_info';
// delete transient (force-check is used to refresh)
if( !empty($_GET['force-check']) ) {
delete_transient($transient_name);
}
// try transient
$transient = get_transient($transient_name);
if( $transient !== false ) return $transient;
// connect
$response = $this->request('http://api.wordpress.org/plugins/info/1.0/advanced-custom-fields');
// ensure response is expected object
if( !is_wp_error($response) ) {
// store minimal data
$info = array(
'version' => $response->version,
'versions' => array_keys( $response->versions ),
'tested' => $response->tested
);
// order versions (latest first)
$info['versions'] = array_reverse($info['versions']);
// update var
$response = $info;
}
// update transient
set_transient($transient_name, $response, HOUR_IN_SECONDS);
// return
return $response;
}
/**
* plugin_row_meta
*
* This function will append extra HTML to the plugin row
*
* @type function
* @date 9/4/17
* @since 1.0.0
*
* @param (mixed)
* @return (mixed)
*/
function plugin_row_meta( $plugin_meta, $plugin_file, $plugin_data, $status ) {
// check if is acf
if( $plugin_file == 'advanced-custom-fields/acf.php' ) {
// append html
$plugin_meta[] = $this->plugin_row_html();
}
// return
return $plugin_meta;
}
/**
* plugin_row_html
*
* This function will return HTML used for the plugin row
*
* @type function
* @date 11/9/17
* @since 1.0.0
*
* @param n/a
* @return n/a
*/
function plugin_row_html() {
// vars
$info = $this->get_plugin_info();
// start
ob_start();
?>
<span id="acfbt-settings">
<?php _e('Change update to'); ?>
<select name="acfbt_version">
<option value=""><?php _e('Select version') ?></option>
<?php foreach( $info['versions'] as $version ):
$label = ( $version == 'trunk' ) ? 'Stable ('.$info['version'].')' : $version;
?>
<option value="<?php echo esc_attr($version); ?>"><?php echo esc_html($label); ?></option>
<?php endforeach; ?>
</select>
<button class="button" name="acfbt" value="<?php echo wp_create_nonce('acfbt'); ?>"><?php _e('Apply') ?></button>
</span>
<?php
// get
$html = ob_get_contents();
ob_end_clean();
// return
return $html;
}
/**
* load
*
* This function will run when loading the wp-admin/plugins.php page
*
* @type function
* @date 11/9/17
* @since 1.0.0
*
* @param n/a
* @return n/a
*/
function load() {
// vars
$nonce = isset($_POST['acfbt']) ? $_POST['acfbt'] : '';
$version = isset($_POST['acfbt_version']) ? $_POST['acfbt_version'] : '';
// bail early nonce does not match
if( !$nonce || !wp_verify_nonce($nonce, 'acfbt') ) return;
// vars
$info = $this->get_plugin_info();
// validate version
if( empty($info['versions']) || !in_array($version, $info['versions'] ) ) return;
// convert 'trunk' to latest version
if( $version == 'trunk' ) $version = $info['version'];
// set specific version
$this->version = $version;
// get transient
$transient = get_site_transient( 'update_plugins' );
// do nothing
// trigger update
set_site_transient( 'update_plugins', $transient );
}
/**
* modify_plugins_transient
*
* This function will modify the 'update_plugins' transient with custom data
*
* @type function
* @date 11/9/17
* @since 1.0.0
*
* @param $transient (object)
* @return $transient
*/
function modify_plugins_transient( $transient ) {
// bail early if empty
if( !$transient || empty($transient->checked) ) return $transient;
// bail early if acf was not checked
if( !isset($transient->checked['advanced-custom-fields/acf.php']) ) return $transient;
// vars
$info = $this->get_plugin_info();
$old_version = $transient->checked['advanced-custom-fields/acf.php'];
$new_version = $this->version;
// no version selected, find latest tag
if( !$new_version ) {
// attempt to find latest tag
foreach( $info['versions'] as $version ) {
// ignore trunk
if( $version == 'trunk' ) continue;
// ignore if older than $old_version
if( version_compare($version, $old_version, '<') ) continue;
// ignore if older than $new_version
if( version_compare($version, $new_version, '<') ) continue;
// this tag is a newer version!
$new_version = $version;
}
}
// bail ealry if no $new_version
if( !$new_version ) return $transient;
// response
$response = new stdClass();
$response->id = 'w.org/plugins/advanced-custom-fields';
$response->slug = 'advanced-custom-fields';
$response->plugin = 'advanced-custom-fields/acf.php';
$response->new_version = $new_version;
$response->url = 'https://wordpress.org/plugins/advanced-custom-fields/';
$response->package = 'https://downloads.wordpress.org/plugin/advanced-custom-fields.'.$new_version.'.zip';
$response->tested = $info['tested'];
// append
$transient->response['advanced-custom-fields/acf.php'] = $response;
// return
return $transient;
}
}
// globals
global $acf_beta_tester;
// instantiate
$acf_beta_tester = new acf_beta_tester();
// end class
endif;
?>