-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimplelib-plugin-upgrader-class.php
282 lines (259 loc) · 9.51 KB
/
simplelib-plugin-upgrader-class.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<?php
/**
* Class SimpleLibPluginUpgrader.
* Version 1.4
* Author: minimus
* Author URI: http://simplelib.com
*/
if ( ! class_exists( 'SimpleLibPluginUpgrader' ) ) {
class SimpleLibPluginUpgrader {
private $itemId = null;
private $personalToken = null;
private $currentVersion = null;
private $slug = null;
private $pluginSlug = null;
private $name = null;
private $homepage = '';
private $errorString = '';
private $defaultSections = array(
'description',
'installation',
'faq',
'screenshots',
'changelog',
'reviews',
'other_notes'
);
public $enabled = false;
public $callback = null;
/**
* SimpleLibPluginUpgrader constructor.
*
* @param string $id Envato Item ID
* @param array $data Contains the required parameters
* token — Personal Token of buyer
* version — plugin current version
* slug — slug of plugin (i.e.: sam-pro-lite)
* pluginSlug — full slug of plugin (plugin folder + name of main plugin file,
* i.e.: sam-pro-lite/sam-pro-lite.php)
* name — name of plugin
* homepage – plugin homepage URL, not required
* errorString - localized error string
* @param null|callable $callback The function provides splitting of the content of the Envato plugin description
* to the standard sections.
*/
public function __construct( $id, $data, $callback = null ) {
if ( ! empty( $id ) ) {
$this->itemId = $id;
$this->personalToken = ( isset( $data['token'] ) ) ? $data['token'] : null;
$this->currentVersion = ( isset( $data['version'] ) ) ? $data['version'] : null;
$this->slug = ( isset( $data['slug'] ) ) ? $data['slug'] : null;
$this->pluginSlug = ( isset( $data['pluginSlug'] ) ) ? $data['pluginSlug'] : null;
$this->name = ( isset( $data['name'] ) ) ? $data['name'] : null;
$this->homepage = ( isset( $data['homepage'] ) ) ? $data['homepage'] : '';
$this->errorString = ( isset( $data['errorString'] ) ) ? $data['errorString'] : 'An unknown API error occurred.';
$this->callback = $callback;
}
$this->enabled = self::is_enabled();
if ( $this->enabled ) {
add_filter( 'pre_set_site_transient_update_plugins', array( &$this, 'checkUpdate' ) );
add_filter( 'plugins_api', array( &$this, 'checkInfo' ), 10, 3 );
add_filter( 'upgrader_package_options', array( &$this, 'setUpdatePackage' ) );
}
}
/**
* Checking for all the transmitted data to the class
*
* @return bool
*/
private function is_enabled() {
return (
! is_null( $this->itemId ) &&
! is_null( $this->personalToken ) &&
! is_null( $this->currentVersion ) &&
! is_null( $this->slug ) &&
! is_null( $this->pluginSlug ) &&
! is_null( $this->name )
);
}
/**
* Preparing of the part of received data
*
* @param array $data preparing data
* @param string $name the name of input data part
*
* @return array|bool|string
*/
private function getAttribute( $data, $name ) {
$out = '';
foreach ( $data as $key => $val ) {
if ( $val['name'] === $name ) {
switch ( $name ) {
case 'compatible-software':
$out = array(
'required' => str_replace( 'WordPress ', '', $val['value'][ count( $val['value'] ) - 1 ] ),
'tested' => str_replace( 'WordPress ', '', $val['value'][0] )
);
break;
default:
$out = false;
}
}
}
return $out;
}
/**
* Default function for splitting content. If user function is not defined, provides splitting of the content
* of the Envato plugin description to the standard sections.
* Default sections: description, installation, faq, screenshots, changelog, reviews, other_notes.
*
* @param null|array $data content of the Envato plugin description
*
* @return array
*/
private function getSections( $data = null ) {
if ( is_null( $data ) || empty( $data ) ) {
return array();
}
$out = array();
$m = preg_match_all( "/<h2(.*?)>(.+?)<\/h2>/", $data, $matches );
$sections = preg_split( "/<h2(.*?)>(.+?)<\/h2>/", $data );
$out['description'] = ( isset( $sections[0] ) ) ? $sections[0] : '';
foreach ( $matches[2] as $key => $match ) {
$out[ strtolower( $match ) ] = $sections[ $key + 1 ];
}
return $out;
}
/**
* Request data from Envato API
*
* @param string $data type of data for request
*
* @return array|mixed|null|object|WP_Error
*/
public function request( $data = 'info' ) {
$args = array(
'headers' => array(
'Authorization' => 'Bearer ' . $this->personalToken,
),
'timeout' => 30,
);
switch ( $data ) {
case 'info':
$url = 'https://api.envato.com/v3/market/catalog/item?id=' . $this->itemId;
break;
case 'link':
$url = 'https://api.envato.com/v3/market/buyer/download?item_id=' . $this->itemId . '&shorten_url=true';
break;
default:
$url = 'https://api.envato.com/v3/market/catalog/item?id=' . $this->itemId;
}
$response = wp_remote_get( esc_url_raw( $url ), $args );
$response_code = wp_remote_retrieve_response_code( $response );
$response_message = wp_remote_retrieve_response_message( $response );
if ( 200 !== $response_code && ! empty( $response_message ) ) {
return new WP_Error( $response_code, $response_message );
} elseif ( 200 !== $response_code ) {
return new WP_Error( $response_code, $this->errorString );
} elseif ( 200 == $response_code ) {
$out = json_decode( wp_remote_retrieve_body( $response ), true );
if ( null === $out ) {
return new WP_Error( 'api_error', $this->errorString );
}
return $out;
} else {
return null;
}
}
/**
* pre_set_site_transient_update_plugins filter handler. Checking the availability of an update of the plugin
* on the CodeCanyon.
*
* @param object $transient
*
* @return object
*/
public function checkUpdate( $transient ) {
if ( empty( $transient->checked ) ) {
return $transient;
}
$pluginInfo = self::request();
if ( is_array( $pluginInfo ) && isset( $pluginInfo['wordpress_plugin_metadata'] ) ) {
$info = $pluginInfo['wordpress_plugin_metadata'];
if ( version_compare( $this->currentVersion, $info['version'], '<' ) ) {
$plugin = new stdClass();
$plugin->slug = $this->slug;
$plugin->new_version = $info['version'];
$plugin->url = '';
$plugin->package = $this->pluginSlug;
$plugin->name = $info['plugin_name'];
$plugin->plugin = $this->pluginSlug;
$transient->response[ $this->pluginSlug ] = $plugin;
}
}
return $transient;
}
/**
* plugins_api filter handler. Retrieving plugin information from Envato API.
*
* @param false|object|array $result The result object or array. Default false.
* @param string $action The type of information being requested from the Plugin Install API.
* @param object $args Plugin API arguments.
*
* @return bool|object
*/
public function checkInfo( $result, $action, $args ) {
if ( $args->slug === $this->slug ) {
$pluginInfo = self::request();
if ( is_array( $pluginInfo ) && isset( $pluginInfo['wordpress_plugin_metadata'] ) ) {
$info = $pluginInfo['wordpress_plugin_metadata'];
$versions = self::getAttribute( $pluginInfo['attributes'], 'compatible-software' );
$sections = ( is_null( $this->callback ) ) ?
self::getSections( $pluginInfo['description'] ) :
call_user_func( $this->callback, $pluginInfo['description'] );
$plugin = new stdClass();
$plugin->name = $info['plugin_name'];
$plugin->author = $info['author'];
$plugin->slug = $this->slug;
$plugin->version = $info['version'];
$plugin->requires = $versions['required'];
$plugin->tested = $versions['tested'];
$plugin->rating = ( (int) $pluginInfo['rating']['count'] < 3 ) ? 100.0 : 20 * (float) $pluginInfo['rating']['rating'];
$plugin->num_ratings = (int) $pluginInfo['rating']['count'];
$plugin->active_installs = (int) $pluginInfo['number_of_sales'];
$plugin->last_updated = $pluginInfo['updated_at'];
$plugin->added = $pluginInfo['published_at'];
$plugin->homepage = $this->homepage;
$plugin->sections = $sections;
$plugin->download_link = $pluginInfo['url'];
$plugin->banners = array(
'high' => $pluginInfo['previews']['landscape_preview']['landscape_url']
);
return $plugin;
} else {
return false;
}
} else {
return false;
}
}
/**
* upgrader_package_options filter handler. Retrieving plugin package URI from Envato API.
*
* @param array $options The package options before running an update.
*
* @return array
*/
public function setUpdatePackage( $options ) {
$package = $options['package'];
if ( $package === $this->pluginSlug ) {
$response = self::request( 'link' );
$options['package'] =
( is_wp_error( $response ) || empty( $response ) || ! empty( $response['error'] ) ) ?
'' :
$response['wordpress_plugin'];
}
return $options;
}
}
}