forked from adventistchurch/alps-gutenberg-blocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
updater.php
121 lines (101 loc) · 4.1 KB
/
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
<?php
namespace ALPS\Gutenberg;
class PluginUpdater
{
private $name;
private $version;
private $metaUrl;
private $cacheKey;
private $cacheTtl = 3600;
public function __construct($name, $version, $metaUrl)
{
$this->name = $name;
$this->version = $version;
$this->cacheKey = $name . '_updater';
$this->metaUrl = $metaUrl;
}
public function init()
{
add_filter('plugins_api', [$this, 'pluginInfo'], 20, 3);
add_filter('site_transient_update_plugins', [$this, 'checkUpdate']);
add_action('upgrader_process_complete', [$this, 'afterUpdate'], 10, 2);
}
public function pluginInfo($res, $action, $args)
{
// do nothing if this is not about getting plugin information
if ('plugin_information' !== $action) {
return false;
}
if ($this->name !== $args->slug) {
return false;
}
if (false == $remote = get_transient( $this->cacheKey )) {
$remote = wp_remote_get( $this->metaUrl, [
'timeout' => 10,
'headers' => [
'Accept' => 'application/json',
],
]);
if ( !is_wp_error($remote) && isset($remote['response']['code']) && $remote['response']['code'] == 200 && !empty($remote['body'])) {
set_transient( $this->cacheKey, $remote, $this->cacheTtl ); // 1 hour cache
}
}
if( !is_wp_error($remote) && isset( $remote['response']['code'] ) && $remote['response']['code'] == 200 && !empty($remote['body'])) {
$remote = json_decode($remote['body']);
$res = new \stdClass();
$res->name = $remote->name;
$res->slug = $this->name;
$res->version = $remote->version;
$res->tested = $remote->tested;
$res->requires = $remote->requires;
$res->download_link = $remote->download_url;
$res->trunk = $remote->download_url;
$res->requires_php = $remote->requires_php;
$res->last_updated = $remote->last_updated;
$res->sections = [
'description' => $remote->sections->description,
];
return $res;
}
return false;
}
public function checkUpdate($transient)
{
if ( empty($transient->checked ) ) {
return $transient;
}
// trying to get from cache first
if ( false == $remote = get_transient( $this->cacheKey ) ) {
// info.json is the file with the actual plugin information on your server
$remote = wp_remote_get( $this->metaUrl, [
'timeout' => 10,
'headers' => [
'Accept' => 'application/json',
]
]);
if ( !is_wp_error( $remote ) && isset( $remote['response']['code'] ) && $remote['response']['code'] == 200 && !empty( $remote['body'] ) ) {
set_transient( $this->cacheKey, $remote, $this->cacheTtl ); // 12 hours cache
}
}
if( !is_wp_error($remote) && isset( $remote['response']['code'] ) && $remote['response']['code'] == 200 && !empty($remote['body'])) {
$remote = json_decode( $remote['body'] );
if ($remote && version_compare( $this->version, $remote->version, '<' ) && version_compare($remote->requires, get_bloginfo('version'), '<' )) {
$res = new \stdClass();
$res->slug = $this->name;
$res->plugin = $this->name . '/plugin.php';
$res->new_version = $remote->version;
$res->tested = $remote->tested;
$res->package = $remote->download_url;
$transient->response[$res->plugin] = $res;
}
}
return $transient;
}
public function afterUpdate($upgrader_object, $options)
{
if ($options['action'] == 'update' && $options['type'] === 'plugin') {
// just clean the cache when new plugin version is installed
delete_transient($this->cacheKey);
}
}
}