-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-updater.php
265 lines (213 loc) · 5.84 KB
/
class-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
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
<?php
/**
* Updater component.
*
* @package HivePress\Components
*/
namespace HivePress\Components;
use HivePress\Helpers as hp;
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
/**
* Updater component class.
*
* @class Updater
*/
final class Updater extends Component {
/**
* Class constructor.
*
* @param array $args Component arguments.
*/
public function __construct( $args = [] ) {
// Check license key.
if ( ! get_option( 'hp_hivepress_license_key' ) ) {
return;
}
// Check theme updates.
add_filter( 'pre_set_site_transient_update_themes', [ $this, 'check_theme_updates' ] );
// Check plugin updates.
add_filter( 'pre_set_site_transient_update_plugins', [ $this, 'check_plugin_updates' ] );
// Set plugin details.
add_filter( 'plugins_api', [ $this, 'set_plugin_details' ], 10, 3 );
parent::__construct( $args );
}
/**
* Gets license key.
*
* @return string
*/
protected function get_license_key() {
return implode( ',', explode( "\n", get_option( 'hp_hivepress_license_key' ) ) );
}
/**
* Gets themes.
*
* @return array
*/
protected function get_themes() {
// Get license key.
$license_key = $this->get_license_key();
// Get cache key.
$cache_key = 'themes_' . md5( $license_key );
// Get cached themes.
$themes = hivepress()->cache->get_cache( $cache_key );
if ( is_null( $themes ) ) {
$themes = [];
// Get API response.
$response = json_decode(
wp_remote_retrieve_body(
wp_remote_get(
'https://store.hivepress.io/api/v1/products?' . http_build_query(
[
'type' => 'theme',
'license_key' => $license_key,
]
)
)
),
true
);
if ( is_array( $response ) && isset( $response['data'] ) ) {
foreach ( $response['data'] as $theme ) {
// Add theme.
$themes[ $theme['slug'] ] = [
'theme' => $theme['slug'],
'new_version' => $theme['version'],
'requires' => $theme['wp_min_version'],
'requires_php' => $theme['php_version'],
'url' => $theme['buy_url'],
'package' => $theme['download_url'],
];
}
// Cache themes.
hivepress()->cache->set_cache( $cache_key, null, $themes, HOUR_IN_SECONDS );
}
}
return $themes;
}
/**
* Gets plugins.
*
* @return array
*/
protected function get_plugins() {
// Get license key.
$license_key = $this->get_license_key();
// Get cache key.
$cache_key = 'plugins_' . md5( $license_key );
// Get cached plugins.
$plugins = hivepress()->cache->get_cache( $cache_key );
if ( is_null( $plugins ) ) {
$plugins = [];
// Get API response.
$response = json_decode(
wp_remote_retrieve_body(
wp_remote_get(
'https://store.hivepress.io/api/v1/products?' . http_build_query(
[
'type' => 'extension',
'license_key' => $license_key,
]
)
)
),
true
);
if ( is_array( $response ) && isset( $response['data'] ) ) {
foreach ( $response['data'] as $extension ) {
// Add plugin.
$plugins[ $extension['slug'] ] = (object) [
'name' => $extension['name'],
'slug' => $extension['slug'],
'version' => $extension['version'],
'new_version' => $extension['version'],
'tested' => $extension['wp_max_version'],
'requires' => $extension['wp_min_version'],
'requires_php' => $extension['php_version'],
'id' => $extension['buy_url'],
'url' => $extension['buy_url'],
'package' => $extension['download_url'],
'download_link' => $extension['download_url'],
'plugin' => $extension['slug'] . '/' . $extension['slug'] . '.php',
'author' => '<a href="https://hivepress.io/" target="_blank">HivePress</a>',
'banners' => [],
'banners_rtl' => [],
'compatibility' => (object) [],
'icons' => [
'1x' => $extension['image_url'],
'2x' => $extension['image_url'],
],
'sections' => [
'description' => $extension['description'],
],
];
}
// Cache plugins.
hivepress()->cache->set_cache( $cache_key, null, $plugins, HOUR_IN_SECONDS );
}
}
return $plugins;
}
/**
* Checks theme updates.
*
* @param object $transient Transient object.
* @return object
*/
public function check_theme_updates( $transient ) {
if ( ! empty( $transient->checked ) ) {
// Get themes.
$themes = $this->get_themes();
foreach ( $themes as $theme ) {
// Get version.
$version = hp\get_array_value( $transient->checked, $theme['theme'] );
if ( $version && version_compare( $version, $theme['new_version'], '<' ) ) {
// Add update.
$transient->response[ $theme['theme'] ] = $theme;
}
}
}
return $transient;
}
/**
* Checks plugin updates.
*
* @param object $transient Transient object.
* @return object
*/
public function check_plugin_updates( $transient ) {
if ( ! empty( $transient->checked ) ) {
// Get plugins.
$plugins = $this->get_plugins();
foreach ( $plugins as $plugin ) {
// Get version.
$version = hp\get_array_value( $transient->checked, $plugin->plugin );
if ( $version && version_compare( $version, $plugin->version, '<' ) ) {
// Add update.
$transient->response[ $plugin->plugin ] = $plugin;
}
}
}
return $transient;
}
/**
* Sets plugin details.
*
* @param object $response API response.
* @param string $action API action.
* @param array $args Request arguments.
* @return object
*/
public function set_plugin_details( $response, $action, $args ) {
if ( 'plugin_information' === $action ) {
// Get plugin.
$plugin = hp\get_array_value( $this->get_plugins(), $args->slug );
if ( $plugin ) {
// Set details.
$response = $plugin;
}
}
return $response;
}
}