forked from nicko170/wp-openid
-
Notifications
You must be signed in to change notification settings - Fork 2
/
GithubUpdater.php
208 lines (172 loc) · 7.13 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
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
<?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;
private string $slug = 'wp-openid-siu-upm'; // Forzamos el slug correcto
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 = $this->slug . '/' . basename($this->file); // Usamos el slug forzado
$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' => $this->slug, // Usamos el slug correcto
'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 == $this->slug) { // Comparar con el slug forzado
$gh = $this->_get_repository();
$plugin = [
'name' => $this->plugin['Name'],
'slug' => $this->slug, // Usamos el slug correcto
'requires' => '5.0',
'tested' => '6.6.2',
'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);
$new_directory = trailingslashit(WP_PLUGIN_DIR) . $this->slug; // Ruta de la carpeta renombrada
// Mueve el directorio descargado al nuevo directorio
$wp_filesystem->move($result['destination'], $new_directory);
// Actualiza la propiedad basename con el nuevo directorio
$this->basename = plugin_basename($new_directory . '/' . basename($this->file));
$result['destination'] = $new_directory;
// Actualizar las referencias en la base de datos
$this->update_database_references();
// Reactiva el plugin si estaba activo antes
if ($this->active) {
activate_plugin($this->basename);
}
return $result;
}
private function update_database_references()
{
// Obtener la lista de plugins activos
$active_plugins = get_option('active_plugins', []);
// Buscar la referencia antigua del plugin en los plugins activos y reemplazarla
$old_basename = plugin_basename($this->file); // Referencia antigua
$new_basename = $this->slug . '/' . basename($this->file); // Nueva referencia
if (in_array($old_basename, $active_plugins)) {
// Reemplazar la referencia antigua por la nueva en la lista de plugins activos
$active_plugins = array_map(function ($plugin) use ($old_basename, $new_basename) {
return ($plugin === $old_basename) ? $new_basename : $plugin;
}, $active_plugins);
// Guardar la nueva lista de plugins activos
update_option('active_plugins', $active_plugins);
}
// Actualizar otros datos de configuración relacionados, si es necesario
$this->update_plugin_options($old_basename, $new_basename);
}
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' => $this->slug, // Usamos el slug correcto
'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;
}
}