class-be-updater.php
10.8 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
<?php
/*
* Hook into WordPress Automatic Updates
*
* Based On Framework Provided By omarabid
* https://github.com/omarabid/Self-Hosted-WordPress-Plugin-repository
*/
class BolderElements_Plugin_Updater {
/**
* Author name
*
* @var string
*/
public $author_name = "Bolder Elements";
/**
* Envato Plugin ID
*
* @var string
*/
public $plugin_id;
/**
* Envato Plugin ID
*
* @var string
*/
public $plugin_name;
/**
* The plugin slug
*
* @var string
*/
private $plugin_slug;
/**
* The plugin directory
*
* @var string
*/
private $plugin_dir;
/**
* The plugin current version
*
* @var string
*/
public $current_version;
/**
* URL to plugin update api
*
* @var string
*/
private $remote_url;
/**
* Data taken from plugin file meta data
*
* @var array
*/
private $pluginData;
/**
* If plugin is activated
*
* @var bool
*/
public $pluginActivated;
/**
* Initialize a new instance of the WordPress Auto-Update class
* @param string $plugin_file
* @param string $current_version
*/
function __construct( $plugin_file, $current_version, $plugin_id, $plugin_slug, $plugin_name ) {
// Set the class public variables
$this->plugin_id = $plugin_id;
$this->plugin_slug = $plugin_slug;
$this->plugin_name = $plugin_name;
$this->plugin_file = $plugin_file;
$this->plugin_dir = plugin_basename( $this->plugin_file );
$this->current_version = $current_version;
$this->remote_url = "http://bolderelements.net/updates/" . $this->plugin_slug;
// define the alternative API for updating and information checking
add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'check_update' ) );
add_filter( 'plugins_api', array( $this, 'check_info' ), 10, 3 );
// modify error messages when downloading
add_filter( "upgrader_pre_download", array( $this, "updatePackageEnvato"), 10, 4 );
add_filter( "upgrader_pre_install", array( $this, "preInstall" ), 10, 3 );
add_filter( "upgrader_post_install", array( $this, "postInstall" ), 10, 3 );
add_filter( "in_plugin_update_message-" . $this->plugin_dir, array( $this, "update_message" ), 10, 2 );
}
/**
* Retrieve Plugin Data
*
* @param $transient
* @return object $ transient
*/
function initPluginData () {
$this->pluginData = get_plugin_data( $this->plugin_file );
}
/**
* Display update message if provided
*
* @param $transient
* @return object $ transient
*/
function update_message( $plugin_data, $r ) {
// Retrieve update message
$information = $this->getRemote_information();
// Display message if one is set
if( isset( $information->update_message ) && !empty( $information->update_message ) ) :
?>
<p style="background: #fbe3e4; border: 1px solid #d77163; padding: 5px 15px; margin-top: 10px;"><?php echo wp_kses_post( $information->update_message ); ?></p>
<?php
endif;
}
/**
* Add our self-hosted autoupdate plugin to the filter transient
*
* @param $transient
* @return object $ transient
*/
public function check_update( $transient ) {
if( empty( $transient->checked ) )
return $transient;
// Get plugin information
$this->initPluginData();
// Get the remote version
$remote_version = $this->getRemote_version();
// If a newer version is available, add the update
if( version_compare( $this->current_version, $remote_version, '<' ) ) {
$obj = new stdClass();
$obj->slug = $this->plugin_slug;
$obj->plugin = $this->plugin_dir;
$obj->name = $this->plugin_name;
$obj->new_version = $remote_version;
$obj->package = $this->plugin_slug; // Allows 'View Details' link to appear
$transient->response[ $this->plugin_dir ] = $obj;
}
return $transient;
}
/**
* Add our self-hosted description to the filter
*
* @param boolean $false
* @param array $action
* @param object $arg
* @return bool|object
*/
public function check_info( $false, $action, $response ) {
$array_pattern = array(
'/^((\d)+(\.)+(\d)+(\.)*(\d)*([\s\-\s])+([0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])))/m',
'/^(\t\-)+([a-zA-Z0-9,.:&()\/\-\'\"\ ]+)/m',
'/\n\n/',
);
$array_replace = array(
'<h4>$1</h4><ul>',
'<li>$2</li>',
'</ul>'
);
// Create tabs in the lightbox
if( isset( $response->slug ) && $response->slug === $this->plugin_slug ) {
$this->pluginData = get_plugin_data( $this->plugin_file );
$information = $this->getRemote_information();
$information->package = $this->plugin_slug; // Allows 'View Details' link to appear
$information->name = $this->plugin_name;
$information->author = $this->author_name;
$information->sections['changelog'] = '<div>' . preg_replace( $array_pattern, $array_replace, $information->sections['changelog'] ) . '</div>';
return $information;
}
return $false;
}
/**
* Perform check before installation starts.
*
* @param boolean $true
* @param array $args
* @return null
*/
public function preInstall( $true, $args ) {
// Get plugin information
$this->initPluginData();
$this->pluginActivated = is_plugin_active( $this->plugin_dir );
}
/**
* Perform additional actions to successfully install our plugin
*
* @param boolean $true
* @param string $hook_extra
* @param object $result
* @return object
*/
public function postInstall( $true, $hook_extra, $result ) {
global $wp_filesystem;
// Re-activate plugin if needed
if ( $this->pluginActivated )
$activate = activate_plugin( $this->plugin_dir );
return $result;
}
/**
* Return the remote version
* @return string $remote_version
*/
public function getRemote_version() {
$request = wp_remote_post( $this->remote_url, array( 'body' => array( 'action' => 'version' ) ) );
if( !is_wp_error( $request ) || wp_remote_retrieve_response_code( $request ) === 200 ) {
return $request[ 'body' ];
}
return false;
}
/**
* Get information about the remote version
* @return bool|object
*/
public function getRemote_information() {
$request = wp_remote_post( $this->remote_url, array( 'body' => array( 'action' => 'info' ) ) );
if (!is_wp_error( $request ) || wp_remote_retrieve_response_code( $request ) === 200) {
return unserialize( $request[ 'body' ] );
}
return false;
}
/**
* Retrieve CodeCanyon download link if registration works
* @return void
*/
function getEnvatoUpdateInfo() {
// select data from database
$settings = get_site_option( 'be_config_data-' . $this->plugin_id );
$return = array();
if( $settings && is_array( $settings ) ) {
$return[ 'username' ] = ( isset( $settings[ 'username' ] ) ) ? $settings[ 'username' ] : '';
$return[ 'api_key' ] = ( isset( $settings[ 'api_key' ] ) ) ? $settings[ 'api_key' ] : '';
$return[ 'purchase_code'] = ( isset( $settings[ 'purchase_code' ] ) ) ? $settings[ 'purchase_code' ] : '';
} else
$return = false;
return $return;
}
/**
* Return the download of the plugin
* @return boolean $remote_license
*/
protected function envatoDownloadPluginUrl( $username, $purchase_code, $api_key ) {
return 'http://marketplace.envato.com/api/edge/' . rawurlencode( $username ) . '/' . rawurlencode( $api_key ) . '/download-purchase:' . rawurlencode( $purchase_code ) . '.json';
}
/**
* Update package variable to correct downloadable package
*
* @param $reply
* @param $package
* @param $updater
* @return mixed|string|WP_Error
*/
public function updatePackageEnvato( $reply, $package, $updater ) {
global $wp_filesystem;
// Verify proper update
if( !isset( $updater->skin->plugin_info ) && !isset( $updater->skin->plugin ) ) return $reply;
// Verify currently updating plugin: Bulk Update & Quick Update
if( ( isset( $updater->skin->plugin_info ) && $updater->skin->plugin_info['Name'] !== $this->plugin_name ) ||
( isset( $updater->skin->plugin ) && $updater->skin->plugin !== $this->plugin_dir ) ) return $reply;
$user_settings = $this->getEnvatoUpdateInfo();
if( $user_settings && is_array( $user_settings ) && ( isset( $user_settings[ 'username' ] ) && isset( $user_settings[ 'api_key' ] ) && isset( $user_settings[ 'purchase_code' ] ) ) ) {
// Change download text to be more accurate
$updater->strings['downloading_package'] = __( 'Downloading package from Envato Marketplace', 'be-config' ) . '...';
// Use saved credentials to retrieve plugin package info
$download_query = wp_remote_get( $this->envatoDownloadPluginUrl( $user_settings[ 'username' ], $user_settings[ 'purchase_code' ], $user_settings[ 'api_key' ] ) );
$download_url = json_decode( $download_query['body'], true );
if( !isset( $download_url[ 'download-purchase' ][ 'download_url' ] ) )
return new WP_Error( 'invalid_credentials', __( 'Could not connect to Envato API', 'be-config' ) );
// Retrieve downloadable package or error message
$download_pkg = download_url( $download_url[ 'download-purchase' ][ 'download_url' ] );
if( is_wp_error( $download_pkg ) )
return $download_pkg;
// Setup temporary 'uploads' directory
$tmp_folder = $wp_filesystem->wp_content_dir() . 'uploads/' . $this->plugin_slug;
if( is_dir( $tmp_folder ) )
$wp_filesystem->delete( $tmp_folder );
// Create temporary directory for updating
$result = unzip_file( $download_pkg, $tmp_folder );
$pkg_location = $tmp_folder . '/' . $this->plugin_slug . '.zip';
if ( $result && is_file( $pkg_location ) )
return $pkg_location;
} else
return new WP_Error( 'missing_credentials', __( "Missing login credentials", 'be-config' ) . '. <a href="' . admin_url( 'admin.php?page=be-manage-plugins' ) . '" target="_blank">' . __( 'Register Plugin', 'be-config' ) . '</a>' );
return $reply;
}
}
?>