-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathUpdate.php
111 lines (94 loc) · 2.52 KB
/
Update.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
<?php
/**
* ClassifAI Auto Update Integration
*
* @package classifai
*/
namespace Classifai\Admin;
use YahnisElsts\PluginUpdateChecker\v5\PucFactory;
/**
* Plugin update class.
*/
class Update {
/**
* The plugin repository URL for retrieving updates.
*
* @var string
*/
public static $repo_url = 'https://github.com/10up/classifai/';
/**
* The update checker object.
*
* @var YahnisElsts\PluginUpdateChecker\v5p1\Vcs\PluginUpdateChecker
*/
protected $updater;
/**
* Check to see if we can register this class.
*
* @return bool
*/
public function can_register(): bool {
return class_exists( '\YahnisElsts\PluginUpdateChecker\v5\PucFactory' ) && self::license_check();
}
/**
* Prepare the updater and register hooks.
*/
public function register() {
$this->init();
add_filter( 'pre_set_site_transient_update_plugins', [ $this, 'maybe_update' ], 10, 1 );
}
/**
* Initialize the update checker object.
*
* @return void
*/
public function init() {
$this->updater = PucFactory::buildUpdateChecker(
self::$repo_url,
CLASSIFAI_PLUGIN,
'classifai'
);
$this->updater->getVcsApi()->enableReleaseAssets();
$this->updater->addResultFilter(
function ( $plugin_info ) {
$plugin_info->icons = array(
'svg' => CLASSIFAI_PLUGIN_URL . 'assets/img/icon.svg',
);
return $plugin_info;
}
);
}
/**
* Initialize the auto update if an update is available.
*
* @param object $transient The transient object.
* @return object The modified transient object.
*/
public function maybe_update( $transient ) {
// Check for an updated version
$update = $this->updater->getUpdate();
if ( $update ) {
// If update is available, add it to the transient.
$transient->response[ $update->filename ] = $update->toWpFormat();
} else {
// No update available, get current plugin info.
$update = $this->updater->getUpdateState()->getUpdate();
// Adding the plugin info to the `no_update` property is required
// for the enable/disable auto-update links to appear correctly in the UI.
if ( $update ) {
$transient->no_update[ $update->filename ] = $update;
}
}
return $transient;
}
/**
* Verify the site has a valid license.
*
* @return boolean True if valid license, false otherwise.
*/
public static function license_check() {
$service_manager = new \Classifai\Services\ServicesManager();
$settings = $service_manager->get_settings();
return isset( $settings['valid_license'] ) && $settings['valid_license'];
}
}