class-projects.php
12 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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
<?php
declare( strict_types=1 );
namespace LearnDash\Hub\Component;
use LearnDash\Hub\Framework\Base;
/**
*
*/
class Projects extends Base {
/**
* Return all the current installed projects with metadata.
*
* @param array $api_data The get_projects API data.
*
* @return array
*/
public function get_installed_projects( array $api_data ): array {
foreach ( $api_data as $slug => &$item ) {
$plugin_slug = $this->get_plugin_slug( $slug, $item['name'] );
// the learndash-hub always activated.
if ( ! $plugin_slug && 'learndash-hub' !== $slug ) {
unset( $api_data[ $slug ] );
continue;
}
// check it is update available.
$plugin_data = null;
if ( 'learndash-hub' === $slug ) {
$mu_path = WPMU_PLUGIN_DIR . DIRECTORY_SEPARATOR . 'learndash-hub-mu.php';
if ( file_exists( $mu_path ) ) {
$plugin_data = get_plugin_data( $mu_path );
}
}
$plugin_path = WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . $plugin_slug;
if ( null === $plugin_data && is_file( $plugin_path ) ) {
$plugin_data = get_plugin_data( $plugin_path );
}
$item['has_update'] = $this->has_newer_version( $plugin_data['Version'], $item['latest_version'], '<' );
$item['is_active'] = is_plugin_active( $plugin_slug );
$item['version'] = $plugin_data['Version'];
$item['folder_slug'] = $plugin_slug;
}
return $api_data;
}
/**
* Return the last check timestamp.
*
* @return int
*/
public function get_project_check_time() {
$cached = get_option( 'learndash-hub-projects-api' );
if ( ! isset( $cached['last_check'] ) ) {
return time();
}
return $cached['last_check'];
}
/**
* Install, activate, deactivate or delete a plugin.
*
* @param string $action
* @param string $slug
*
* @return bool|int|true|\WP_Error|null
*/
public function handle_plugin( string $action, string $slug ) {
switch ( $action ) {
case 'install':
return $this->install_plugin( $slug );
case 'activate':
return $this->activate_plugin( $slug );
case 'deactivate':
return $this->deactivate_plugin( $slug );
case 'delete':
return $this->delete_plugin( $slug );
case 'update':
return $this->update_plugin( $slug );
default:
return false;
}
}
/**
* Delete our plugin.
*
* @param string $slug The plugin slug.
*
* @return bool|\WP_Error
*/
public function delete_plugin( string $slug ) {
if ( 'learndash-hub' === $slug && file_exists( WPMU_PLUGIN_DIR . DIRECTORY_SEPARATOR . 'learndash-hub-mu-plugin.php' ) ) {
WP_Filesystem();
global $wp_filesystem;
$wp_filesystem->delete( WPMU_PLUGIN_DIR . DIRECTORY_SEPARATOR . 'learndash-hub', true );
$wp_filesystem->delete( WPMU_PLUGIN_DIR . DIRECTORY_SEPARATOR . 'learndash-hub-mu-plugin.php' );
return true;
}
$plugin_slug = $this->get_plugin_slug( $slug );
if ( ! $plugin_slug ) {
return new \WP_Error( 'err', __( 'Invalid plugin file.', 'learndash-hub' ) );
}
$ret = delete_plugins( array( $plugin_slug ) );
if ( null === $ret ) {
return new \WP_Error( 'err', __( 'Filesystem credentials are required to proceed', 'learndash-hub' ) );
}
// clear cache first.
wp_cache_delete( 'plugins', 'plugins' );
delete_site_transient( 'update_plugins' );
return $ret;
}
/**
* Activate our plugin.
*
* @param string $slug The plugin slug.
*
* @return true|\WP_Error
*/
public function activate_plugin( string $slug ) {
$plugin_slug = $this->get_plugin_slug( $slug );
if ( ! $plugin_slug ) {
return new \WP_Error( 'err', __( 'Invalid plugin file.', 'learndash-hub' ) );
}
$ret = activate_plugin( $plugin_slug );
if ( is_wp_error( $ret ) ) {
return $ret;
}
return true;
}
/**
* Deactivate a plugin.
*
* @param string $slug
*
* @return bool|\WP_Error
*/
public function deactivate_plugin( string $slug ) {
$plugin_slug = $this->get_plugin_slug( $slug );
if ( ! $plugin_slug ) {
return new \WP_Error( 'err', __( 'Invalid plugin file.', 'learndash-hub' ) );
}
deactivate_plugins( $plugin_slug );
return ! is_plugin_active( $plugin_slug );
}
/**
* Update our plugin.
*
* @param string $slug The plugin slug.
*
* @return true|\WP_Error
*/
public function update_plugin( string $slug ) {
if ( ! function_exists( 'plugins_api' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
}
delete_site_option( 'learndash_hub_update_plugins_cache' );
$api = plugins_api(
'plugin_information',
array(
'slug' => $slug,
)
);
if ( is_wp_error( $api ) ) {
return $api;
}
$status = install_plugin_install_status( $api );
if ( 'update_available' === $status['status'] ) {
return $this->install( $slug, true );
}
return new \WP_Error( 'err', __( 'Plugin update failed.', 'learndash-hub' ) );
}
/**
* Install our plugin.
*
* @param string $slug The plugin slug.
*
* @return true|\WP_Error
*/
public function install_plugin( string $slug ) {
if ( ! function_exists( 'plugins_api' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
}
$api = plugins_api(
'plugin_information',
array(
'slug' => $slug,
)
);
if ( is_wp_error( $api ) ) {
return $api;
}
$status = install_plugin_install_status( $api );
if ( 'install' === $status['status'] ) {
// install it.
return $this->install( $slug );
}
return new \WP_Error( 'err1', __( 'Plugin install failed', 'learndash-hub' ) );
}
/**
* Fetch the plugins category so we can use it for filter.
*
* @param array $api_data The get_projects API data.
*
* @return array
*/
public function get_projects_category( array $api_data ): array {
$categories = array();
foreach ( $api_data as $slug => $item ) {
$tags = explode( ',', $item['tags'] );
$categories = array_merge( $categories, $tags );
}
$categories = array_unique( $categories );
$categories = array_filter( $categories );
return array_map( 'trim', $categories );
}
/**
* Return the projects that has not installed.
*
* @param array $api_data The get_projects API data.
*
* @return array
*/
public function get_projects( array $api_data ): array {
$installed_projects = $this->get_installed_projects( $api_data );
foreach ( $api_data as $slug => $item ) {
if ( isset( $installed_projects[ $slug ] ) ) {
unset( $api_data[ $slug ] );
}
}
return $api_data;
}
/**
* Look up a project object, this is use when pulling the plugin info.
*
* @param string $slug The project slug, eg: sfwd-lms.
* @param array $api_data The plugins data, pulling from the api.
*
* @return false|array Return the project data as an array, or false if nothing found.
*/
public function look_project( string $slug, array $api_data ) {
$installed = $this->get_installed_projects( $api_data );
if ( isset( $installed[ $slug ] ) ) {
return $installed[ $slug ];
}
$projects = $this->get_projects( $api_data );
return $projects[ $slug ] ?? false;
}
/**
* Install a project
*
* @param string $slug The plugin folder name.
* @param bool $is_update Update instead of install.
*
* @return bool|\WP_Error
*/
public function install( string $slug, bool $is_update = false ) {
// prepare for install.
include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
include_once ABSPATH . 'wp-admin/includes/plugin-install.php';
include_once ABSPATH . 'wp-admin/includes/theme-install.php';
include_once ABSPATH . 'wp-admin/includes/plugin.php';
include_once ABSPATH . 'wp-admin/includes/file.php';
$skin = new \WP_Ajax_Upgrader_Skin();
$api = plugins_api(
'plugin_information',
array(
'slug' => sanitize_key( $slug ),
'fields' => array( 'sections' => false ),
)
);
if ( is_wp_error( $api ) ) {
return false;
}
$upgrader = new \Plugin_Upgrader( $skin );
if ( $is_update ) {
$plugin_slug = $this->get_plugin_slug( $slug );
$result = $upgrader->upgrade( $plugin_slug );
} else {
$result = $upgrader->install( $api->download_url );
}
if ( true === $result ) {
$this->install_translation( $api );
// delete the update cache.
delete_site_option( 'learndash_hub_fetch_projects' );
delete_site_option( 'learndash_hub_update_plugins_cache' );
if ( $is_update ) {
$this->activate_plugin( $api->slug );
}
return true;
}
return new \WP_Error( 'err', implode( PHP_EOL, $upgrader->skin->get_upgrade_messages() ) );
}
/**
* Pull the translation from glotpress
*
* @param mixed $api
*/
private function install_translation( $api ) {
// get plugin text domain.
$slug = $this->get_plugin_slug( $api->slug );
$wp_installed_languages = get_available_languages();
if ( ! in_array( 'en_US', $wp_installed_languages, true ) ) {
$wp_installed_languages = array_merge( array( 'en_US' ), $wp_installed_languages );
}
if ( ! empty( $slug ) ) {
$plugin_data = get_plugin_data( WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . $slug );
if ( is_array( $plugin_data ) && isset( $plugin_data['TextDomain'] ) && ! empty( $plugin_data['TextDomain'] ) ) {
// loop through the languages and download the locale files.
$text_domain = $plugin_data['TextDomain'];
$url = add_query_arg(
array(
'ldlms-glotpress' => 1,
'action' => 'translation_sets',
'project' => $api->slug,
),
trailingslashit( 'https://translations.learndash.com/' )
);
$response = wp_remote_get( $url );
if ( ( is_array( $response ) ) && ( wp_remote_retrieve_response_code( $response ) === 200 ) ) {
$response_body = wp_remote_retrieve_body( $response );
if ( ! empty( $response_body ) ) {
$ld_translation_sets = json_decode( $response_body, true );
if ( ! $ld_translation_sets || ! isset( $ld_translation_sets[ $api->slug ] ) ) {
return;
}
foreach ( $ld_translation_sets[ $api->slug ] as $key => $translate ) {
foreach ( $wp_installed_languages as $locale ) {
if ( $translate['wp_locale'] === $locale ) {
foreach ( $translate['links'] as $ext => $link ) {
$tmp_name = download_url( $link );
if ( ! is_wp_error( $tmp_name ) ) {
$language_dir = WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . $api->slug . DIRECTORY_SEPARATOR . 'languages';
if ( ! is_dir( $language_dir ) ) {
wp_mkdir_p( $language_dir );
}
$path = $language_dir . DIRECTORY_SEPARATOR . $api->slug . '-' . $locale . '.' . $ext;
rename( $tmp_name, $path );
}
}
}
}
}
}
}
}
}
}
/**
* Get the plugin slug from folder name.
*
* @param string $slug The plugin folder.
* @param string $plugin_name The plugin name.
*
* @return string|bool
*/
public function get_plugin_slug( string $slug, string $plugin_name = '' ) {
$plugins = get_plugins();
// lookup the installed plugins.
foreach ( $plugins as $folder_slug => $plugin ) {
$base = dirname( $folder_slug );
if ( $slug === $base ) {
return $folder_slug;
}
}
// normally it should not here, however if we are development then it will be chances
// we use different slug.
if ( ! defined( 'LEARNDASH_HUB_DEVELOPMENT' ) || empty( $plugin_name ) ) {
return false;
}
// if nothing return, means the plugin is not installed, or use different path.
foreach ( $plugins as $folder_slug => $plugin ) {
if ( strtolower( $plugin['Author'] ) !== 'learndash' ) {
continue;
}
if ( strtolower( $plugin_name ) === strtolower( $plugin['Name'] ) ) {
return $folder_slug;
}
if ( $slug === 'learndash-hub' && $plugin['Name'] === 'LearnDash Licensing & Management' ) {
return $folder_slug;
}
}
return false;
}
/**
* @param string $plugin_version
* @param string $repo_version
*
* @return bool
*/
public function has_newer_version( string $plugin_version, string $repo_version ): bool {
// since plugin version always sematic.
$parts = explode( '.', $plugin_version );
if ( count( $parts ) === 2 ) {
// this means the plugins not PHP standard.
$parts[] = '0';
$plugin_version = implode( '.', $parts );
}
return version_compare( $plugin_version, $repo_version, '<' );
}
}