-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordpress_Version_Tracker.php
More file actions
237 lines (207 loc) · 4.44 KB
/
Wordpress_Version_Tracker.php
File metadata and controls
237 lines (207 loc) · 4.44 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
<?php
namespace SpiroAB;
/**
* Class Wordpress_Version_Tracker
*
* @package SpiroAB
*
* @property Wpvt_Container[] plugins
*
*/
class Wordpress_Version_Tracker
{
public $plugins = [];
/**
* Wordpress_Version_Tracker constructor.
*
* Add action-listner for init
*/
public function __construct()
{
add_action('init', [$this, 'init']);
}
/**
* Add action-listners, filters, and load plugin-info
*
* @return void
*/
public function init()
{
add_filter('pre_set_site_transient_update_plugins', [$this, 'update']);
add_filter('plugins_api', [$this, 'info'], 10, 3);
$this->load();
}
/**
* Load info from other plugins
*
* @return void
*/
public function load()
{
foreach(glob(WP_PLUGIN_DIR . "/*/version.json") as $filename)
{
if(!is_file($filename)) continue;
if(!is_readable($filename)) continue;
$data = file_get_contents($filename);
if(empty($data)) continue;
/** @var null|Phpdoc_Wpvt_Row $json */
$json = json_decode($data);
if(!is_object($json)) continue;
if(empty($json->slug)) continue;
$this->plugins[$json->slug] = new Wpvt_Container($json);
}
}
/**
* Fetch info from remote location
*
* @param string $slug
*
* @return bool|null|Phpdoc_Wpvt_Row
*/
public function fetch($slug)
{
if(empty($this->plugins[$slug]))
{
return NULL;
}
if($this->plugins[$slug]->remote)
{
return $this->plugins[$slug]->remote;
}
if(empty($this->plugins[$slug]->local->latest_json))
{
return NULL;
}
$remote_json_raw = file_get_contents($this->plugins[$slug]->local->latest_json);
if(empty($remote_json_raw))
{
return FALSE;
}
$this->plugins[$slug]->remote = json_decode($remote_json_raw);
return $this->plugins[$slug]->remote;
}
/**
* Append wordpress plugin version update information
*
* @param Phpdoc_Wpvt_transient $transient
*
* @return Phpdoc_Wpvt_transient
*/
public function update($transient)
{
if (empty($transient->checked)) {
return $transient;
}
foreach(array_keys($this->plugins) as $slug)
{
$wp_slug = "{$slug}/{$slug}.php";
$info = $this->update_info($slug);
if(empty($info->new_version))
{
$transient->no_update[$wp_slug] = $info;
continue;
}
if($info->new_version === $info->version)
{
$transient->no_update[$wp_slug] = $info;
continue;
}
$transient->response[$wp_slug] = $info;
}
return $transient;
}
/**
* @param bool|Phpdoc_Wpvt_Row $false
* @param string $action
* @param object $arg TODO
*
* @return bool|Phpdoc_Wpvt_Row
*/
public function info($false, $action, $arg)
{
if(empty($this->plugins[$arg->slug])) {
return $false;
}
return $this->update_info($arg->slug);
}
/**
* @param $slug
*
* @param string $slug
*
* @return bool|Phpdoc_Wpvt_Row
*/
public function update_info($slug)
{
if(empty($this->plugins[$slug])) {
return FALSE;
}
if(empty($this->plugins[$slug]->local->new_version))
{
if(!$this->plugins[$slug]->remote)
{
$this->fetch($slug);
}
if(isset($this->plugins[$slug]->remote->version))
{
$this->plugins[$slug]->local->new_version = $this->plugins[$slug]->remote->version;
}
}
return $this->plugins[$slug]->local;
}
}
/**
* Class Wpvt_Container
* @package SpiroAB
*
* @property Phpdoc_Wpvt_Row $local
* @property Phpdoc_Wpvt_Row $remote
*/
class Wpvt_Container {
public $local;
public $remote;
/**
* Wpvt_Container constructor.
*
* @param Phpdoc_Wpvt_Row $local
* @param Phpdoc_Wpvt_Row $remote
*/
public function __construct($local = NULL, $remote = NULL)
{
$this->local = $local;
$this->remote = $remote;
}
}
/**
* Class Phpdoc_Wpvt_Row
* @package SpiroAB
*
* @property string $slug
* @property string $version
* @property string $new_version
* @property string $latest_json
*/
class Phpdoc_Wpvt_Row {
public $slug;
public $version;
public $new_version;
public $latest_json;
}
/**
* Class Phpdoc_Wpvt_transient
* @package SpiroAB
*
* @property int $last_checked
* @property string[] $checked
* @property Phpdoc_Wpvt_Row[] $response
* @property mixed[] $translations
* @property Phpdoc_Wpvt_Row[] $no_update
*/
class Phpdoc_Wpvt_transient
{
public $last_checked;
public $checked;
public $response;
public $translations;
public $no_update;
}