-
Notifications
You must be signed in to change notification settings - Fork 3
/
class-plugin-updater.php
216 lines (184 loc) · 5.23 KB
/
class-plugin-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
<?php
/**
* The name of this class should be unique to your plugin to
* avoid conflicts with other plugins using an updater class.
*/
class ExamplePluginUpdater {
/**
* @var string
*/
public $plugin_id;
/**
* @var string
*/
public $plugin_slug;
/**
* @var string
*/
public $version;
/**
* @var string
*/
public $api_url;
/**
* @var string
*/
public $cache_key;
/**
* @var boolean
*/
public $cache_allowed;
/**
* @param string $plugin_id The ID of the plugin.
* @param string $plugin_slug The slug of the plugin.
* @param string $version The current version of the plugin.
* @param string $version The API URL to the update server.
*/
public function __construct( $plugin_id, $plugin_slug, $version, $api_url ) {
$this->plugin_id = $plugin_id;
$this->plugin_slug = $plugin_slug;
$this->version = $version;
$this->api_url = $api_url;
$this->cache_key = str_replace( '-', '_', $this->plugin_slug ) . '_updater';
$this->cache_allowed = true; // Only disable this for debugging
add_filter( 'plugins_api', array( $this, 'info' ), 20, 3 );
add_filter( 'site_transient_update_plugins', array( $this, 'update' ) );
add_action( 'upgrader_process_complete', array( $this, 'purge' ), 10, 2 );
}
/**
* Get the license key. Normally, your plugin would have a settings page where
* you ask for and store a license key. Fetch it here.
*
* @return string
*/
protected function get_license_key() {
// See class-settings-page.php
$example_plugin_settings_options = get_option( 'example_plugin_settings_option_name' );
return isset( $example_plugin_settings_options['api_key_0'] ) ? $example_plugin_settings_options['api_key_0'] : null;
}
/**
* Fetch the update info from the remote server running the Lemon Squeezy plugin.
*
* @return object|bool
*/
public function request() {
$lsq_license_key = $this->get_license_key();
if ( ! $lsq_license_key ) {
return false;
}
$remote = get_transient( $this->cache_key );
if ( false !== $remote && $this->cache_allowed ) {
if ( 'error' === $remote ) {
return false;
}
return json_decode( $remote );
}
$remote = wp_remote_get(
$this->api_url . "/update?license_key={$lsq_license_key}",
array(
'timeout' => 10,
)
);
if (
is_wp_error( $remote )
|| 200 !== wp_remote_retrieve_response_code( $remote )
|| empty( wp_remote_retrieve_body( $remote ) )
) {
set_transient( $this->cache_key, 'error', MINUTE_IN_SECONDS * 10 );
return false;
}
$payload = wp_remote_retrieve_body( $remote );
set_transient( $this->cache_key, $payload, DAY_IN_SECONDS );
return json_decode( $payload );
}
/**
* Override the WordPress request to return the correct plugin info.
*
* @see https://developer.wordpress.org/reference/hooks/plugins_api/
*
* @param false|object|array $result
* @param string $action
* @param object $args
* @return object|bool
*/
public function info( $result, $action, $args ) {
if ( 'plugin_information' !== $action ) {
return false;
}
if ( $this->plugin_slug !== $args->slug ) {
return false;
}
$remote = $this->request();
if ( ! $remote || ! $remote->success || empty( $remote->update ) ) {
return false;
}
$plugin_data = get_plugin_data( __FILE__ );
$result = $remote->update;
$result->name = $plugin_data['Name'];
$result->slug = $this->plugin_slug;
$result->sections = (array) $result->sections;
return $result;
}
/**
* Override the WordPress request to check if an update is available.
*
* @see https://make.wordpress.org/core/2020/07/30/recommended-usage-of-the-updates-api-to-support-the-auto-updates-ui-for-plugins-and-themes-in-wordpress-5-5/
*
* @param object $transient
* @return object
*/
public function update( $transient ) {
if ( empty( $transient->checked ) ) {
return $transient;
}
$res = (object) array(
'id' => $this->plugin_id,
'slug' => $this->plugin_slug,
'plugin' => $this->plugin_id,
'new_version' => $this->version,
'url' => '',
'package' => '',
'icons' => array(),
'banners' => array(),
'banners_rtl' => array(),
'tested' => '',
'requires_php' => '',
'compatibility' => new stdClass(),
);
$remote = $this->request();
if (
$remote && $remote->success && ! empty( $remote->update )
&& version_compare( $this->version, $remote->update->version, '<' )
) {
$res->new_version = $remote->update->version;
$res->package = $remote->update->download_link;
$transient->response[ $res->plugin ] = $res;
} else {
$transient->no_update[ $res->plugin ] = $res;
}
return $transient;
}
/**
* When the update is complete, purge the cache.
*
* @see https://developer.wordpress.org/reference/hooks/upgrader_process_complete/
*
* @param WP_Upgrader $upgrader
* @param array $options
* @return void
*/
public function purge( $upgrader, $options ) {
if (
$this->cache_allowed
&& 'update' === $options['action']
&& 'plugin' === $options['type']
&& ! empty( $options['plugins'] )
) {
foreach ( $options['plugins'] as $plugin ) {
if ( $plugin === $this->plugin_id ) {
delete_transient( $this->cache_key );
}
}
}
}
}