-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathGithubUpdater.php
175 lines (146 loc) · 5.41 KB
/
GithubUpdater.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
<?php
class GithubUpdater
{
private string $file;
private array $plugin;
private string $basename;
private bool $active;
private ?string $repository;
private string $asset_name;
private string $readme_url;
public static function make(): self
{
return new self();
}
public function boot(string $file): self
{
$this->file = $file;
$this->plugin = get_plugin_data($this->file);
$this->basename = plugin_basename($this->file);
$this->active = is_plugin_active($this->basename);
if ($this->repository) {
add_filter('pre_set_site_transient_update_plugins', [$this, 'modify_transient'], 10, 1);
add_filter('plugins_api', [$this, 'plugin_popup'], 10, 3);
add_filter('upgrader_post_install', [$this, 'after_install'], 10, 3);
}
return $this;
}
public function repository(string $repository): self
{
$this->repository = $repository;
return $this;
}
public function asset_name(string $asset_name): self
{
$this->asset_name = $asset_name;
return $this;
}
public function readme_url(string $readme_url): self
{
$this->readme_url = $readme_url;
return $this;
}
public function modify_transient($transient)
{
if ($update = $this->_get_update_from_repository()) {
$transient->response[$this->basename] = $update;
} else {
// No update, return a fake update to enable auto update check
$transient->no_update[$this->basename] = (object)[
'id' => $this->basename,
'slug' => dirname($this->basename),
'plugin' => $this->basename,
'new_version' => $this->plugin['Version'],
'url' => '',
'package' => '',
'icons' => [],
'banners' => [],
'banners_rtl' => [],
'tested' => '',
'requires_php' => '',
'compatibility' => new stdClass(),
];
}
return $transient;
}
public function plugin_popup($result, $action, $args)
{
if ($action !== 'plugin_information') {
return false;
}
if (!empty($args->slug)) {
if ($args->slug == current(explode('/', $this->basename))) {
$gh = $this->_get_repository();
$plugin = [
'name' => $this->plugin['Name'],
'slug' => $this->basename,
'requires' => '5.0',
'tested' => '6.1.1',
'version' => $gh['tag_name'],
'author' => $this->plugin['AuthorName'],
'author_profile' => $this->plugin['AuthorURI'],
'last_updated' => $gh['published_at'],
'homepage' => $this->plugin['PluginURI'],
'short_description' => $this->plugin['Description'],
'sections' => [
'Description' => nl2br(file_get_contents($this->readme_url)),
'Updates' => nl2br($gh['body']),
],
'download_link' => $gh['zipball_url']
];
return (object)$plugin;
}
}
return $result;
}
public function after_install($response, $hook_extra, $result)
{
global $wp_filesystem;
$install_directory = plugin_dir_path($this->file);
$wp_filesystem->move($result['destination'], $install_directory);
$result['destination'] = $install_directory;
if ($this->active) {
activate_plugin($this->basename);
}
return $result;
}
private function _get_repository(): array
{
$request_uri = sprintf('https://api.github.com/repos/%s/releases', $this->repository);
$response = wp_remote_get($request_uri);
if (!is_wp_error($response) || wp_remote_retrieve_response_code($response) === 200) {
return current(json_decode(wp_remote_retrieve_body($response), true));
}
return [];
}
private function _get_update_from_repository(): ?object
{
$response = $this->_get_repository();
$current_version = $this->plugin['Version'];
$response_version = $response['tag_name'];
if (version_compare($response_version, $current_version, '>')) {
// Search for an asset with the correct name
$asset = current(array_filter($response['assets'], function ($asset) {
return $asset['name'] === $this->asset_name;
}));
// If we have an asset, use it to build the update object
if ($asset) {
return (object)[
'id' => $this->basename,
'slug' => dirname($this->basename),
'plugin' => $this->basename,
'new_version' => $response_version,
'url' => $response['url'],
'package' => $asset['browser_download_url'],
'icons' => array(),
'banners' => array(),
'banners_rtl' => array(),
'tested' => '',
'requires_php' => '',
'compatibility' => new stdClass(),
];
}
}
return null;
}
}