This repository has been archived by the owner on Mar 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
yit-upgrade.php
543 lines (454 loc) · 22.4 KB
/
yit-upgrade.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
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
<?php
/*
* This file belongs to the YIT Framework.
*
* This source file is subject to the GNU GENERAL PUBLIC LICENSE (GPL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://www.gnu.org/licenses/gpl-3.0.txt
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
} // Exit if accessed directly
if ( ! class_exists( 'YIT_Upgrade' ) ) {
/**
* YIT Upgrade
*
* Notify and Update plugin
*
* @class YIT_Upgrade
* @package Yithemes
* @since 1.0
* @author Your Inspiration Themes
* @see WP_Updater Class
*/
class YIT_Upgrade {
/**
* @var string XML notifier update
*/
protected $_xml = 'http://update.yithemes.com/plugins/%plugin_slug%.xml';
/**
* @var string api server url
*/
protected $_package_url = 'http://www.yithemes.com';
/**
* @var array The registered plugins
*/
protected $_plugins = array();
/**
* @var YIT_Upgrade The main instance
*/
protected static $_instance;
/**
* Construct
*
* @author Andrea Grillo <andrea.grillo@yithemes.com>
* @since 1.0
*/
public function __construct() {
add_filter( 'upgrader_pre_download', array( $this, 'upgrader_pre_download') , 10, 3 );
add_action( 'update-custom_upgrade-plugin-multisite', array( $this, 'upgrade_plugin_multisite' ) );
if( is_network_admin() ){
add_action( 'admin_enqueue_scripts', array( $this, 'network_admin_enqueue_scripts' ) );
}
if( defined( 'YIT_LICENCE_DEBUG' ) && YIT_LICENCE_DEBUG ){
$this->_package_url = 'http://dev.yithemes.com';
}
}
/**
* Main plugin Instance
*
* @param $plugin_slug | string The plugin slug
* @param $plugin_init | string The plugin init file
*
* @return void
*
* @since 1.0
* @author Andrea Grillo <andrea.grillo@yithemes.com>
*/
public function register( $plugin_slug, $plugin_init ) {
if( ! function_exists( 'get_plugins' ) ){
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
}
$plugins = get_plugins();
$plugin_info = $plugins[ $plugin_init ];
$this->_plugins[ $plugin_init ] = array(
'info' => $plugin_info,
'slug' => $plugin_slug,
);
/* === HOOKS === */
if( ! is_multisite() || is_plugin_active_for_network( $plugin_init ) ){
add_action( 'admin_init', array( $this, 'remove_wp_plugin_update_row' ), 15 );
add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'check_update' ) );
} else if( is_multisite() && current_user_can( 'update_plugins' ) ) {
$xml = str_replace( '%plugin_slug%', $plugin_slug, $this->_xml );
$remote_xml = wp_remote_get( $xml );
if( ! is_wp_error( $remote_xml ) && isset( $remote_xml['response']['code'] ) && '200' == $remote_xml['response']['code'] ) {
$plugin_remote_info = new SimpleXmlElement( $remote_xml['body'] );
$this->_plugins[ $plugin_init ]['info']['Latest'] = (string) $plugin_remote_info->latest;
$this->_plugins[ $plugin_init ]['info']['changelog'] = (string) $plugin_remote_info->changelog;
add_action( 'admin_enqueue_scripts', array( $this, 'multisite_updater_script' ) );
}
}
}
/**
* Add the multisite updater scripts
*
* @return void
*
* @since 1.0
* @author Andrea Grillo <andrea.grillo@yithemes.com>
*/
public function multisite_updater_script(){
$update_url = array();
$changelogs = array();
$strings = array(
'new_version' => __( 'There is a new version of %plugin_name% available.', 'yith-plugin-fw' ),
'latest' => __( 'View version %latest% details.', 'yith-plugin-fw' ),
'unavailable' => __( 'Automatic update is unavailable for this plugin,', 'yith-plugin-fw' ),
'activate' => __( 'please <a href="%activate_link%">activate</a> your copy of %plugin_name%.', 'yith-plugin-fw' ),
'update_now' => __( 'Update now.', 'yith-plugin-fw' )
);
foreach( $this->_plugins as $init => $info ){
YIT_Plugin_Licence()->check( $init );
$update_url[ $init ] = wp_nonce_url( self_admin_url('update.php?action=upgrade-plugin-multisite&plugin=') . $init, 'upgrade-plugin-multisite_' . $init );
$changelog_id = str_replace( array( '/', '.php', '.' ), array( '-', '', '-' ), $init );
$details_url[ $init ] = '#TB_inline' . esc_url( add_query_arg( array( 'width' => 722, 'height' => 914, 'inlineId' => $changelog_id ) , '' ) );
$changelogs[ $init ] = $this->in_theme_update_message( $this->_plugins[ $init ], $this->_plugins[ $init ]['info']['changelog'], $changelog_id, false );
}
$localize_script_args = array(
'registered' => $this->_plugins,
'activated' => YIT_Plugin_Licence()->get_activated_products(),
'licence_activation_url' => YIT_Plugin_Licence()->get_licence_activation_page_url(),
'update_url' => $update_url,
'details_url' => $details_url,
'strings' => $strings,
'changelogs' => $changelogs
);
yit_enqueue_script( 'yit-multisite-updater', YIT_CORE_PLUGIN_URL . '/assets/js/multisite-updater.min.js', array( 'jquery' ), false, true );
wp_localize_script( 'yit-multisite-updater', 'plugins', $localize_script_args );
}
public function network_admin_enqueue_scripts(){
yit_enqueue_style( 'yit-upgrader', YIT_CORE_PLUGIN_URL . '/assets/css/yit-upgrader.css' );
}
/**
* Call the protected method _upgrader_pre_download to retrive the zip package file
*
* @param bool $reply Whether to bail without returning the package. Default false.
* @param string $package The package file name.
* @param \WP_Upgrader $upgrader WP_Upgrader instance.
*
* @return string | The download file
*
* @since 1.0
* @see wp-admin/includes/class-wp-upgrader.php
* @access public
* @author Andrea Grillo <andrea.grillo@yithemes.com>
*/
public function upgrader_pre_download( $reply, $package, $upgrader ){
return $this->_upgrader_pre_download( $reply, $package, $upgrader );
}
/**
* Retrive the zip package file
*
* @param bool $reply Whether to bail without returning the package. Default false.
* @param string $package The package file name.
* @param \WP_Upgrader $upgrader WP_Upgrader instance.
*
* @return string | The download file
*
* @since 1.0
* @see wp-admin/includes/class-wp-upgrader.php
* @access protected
* @author Andrea Grillo <andrea.grillo@yithemes.com>
*/
protected function _upgrader_pre_download( $reply, $package, $upgrader ) {
/**
* It isn't YITH Premium plugins, please wordpress update it for me!
*/
if( ! isset( $upgrader->skin->plugin ) ) {
return $reply;
}
$plugin_info = YIT_Plugin_Licence()->get_product( $upgrader->skin->plugin );
/**
* False ? It isn't YITH Premium plugins, please wordpress update it for me!
*/
if( false === $plugin_info ) {
return $reply;
}
$licence = YIT_Plugin_Licence()->get_licence();
$product_id = $plugin_info['product_id'];
$args = array(
'email' => $licence[ $product_id ]['email'],
'licence_key' => $licence[$product_id]['licence_key'],
'product_id' => $plugin_info['product_id'],
'secret_key' => $plugin_info['secret_key'],
'instance' => YIT_Plugin_Licence()->get_home_url(),
'wc-api' => 'download-api',
'request' => 'download'
);
if ( ! preg_match( '!^(http|https|ftp)://!i', $package ) && file_exists( $package ) ) {
//Local file or remote?
return $package;
}
if ( empty( $package ) ) {
return new WP_Error( 'no_package', $upgrader->strings['no_package'] );
}
$upgrader->skin->feedback( 'downloading_package', __( 'Yithemes Repository', 'yith-plugin-fw' ) );
$download_file = $this->_download_url( $package, $args );
/**
* Regenerate update_plugins transient
*/
$this->force_regenerate_update_transient();
if ( is_wp_error( $download_file ) ) {
return new WP_Error( 'download_failed', $upgrader->strings['download_failed'], $download_file->get_error_message() );
}
return $download_file;
}
/**
* Retrive the temp filename
*
* @param string $url The package url
* @param string $body The post data fields
* @param int $timeout Execution timeout (default: 300)
*
* @return string | The temp filename
*
* @since 1.0
* @see wp-admin/includes/class-wp-upgrader.php
* @access protected
* @author Andrea Grillo <andrea.grillo@yithemes.com>
*/
protected function _download_url( $url, $body, $timeout = 300 ) {
//WARNING: The file is not automatically deleted, The script must unlink() the file.
if ( ! $url ) {
return new WP_Error( 'http_no_url', __( 'Invalid URL Provided.' ) );
}
$tmpfname = wp_tempnam( $url );
$args = array(
'timeout' => $timeout,
'stream' => true,
'filename' => $tmpfname,
'body' => $body
);
if ( ! $tmpfname ) {
return new WP_Error( 'http_no_file', __( 'Could not create Temporary file.' ) );
}
$response = wp_safe_remote_post( $url, $args );
if ( is_wp_error( $response ) ) {
unlink( $tmpfname );
return $response;
}
if ( 200 != wp_remote_retrieve_response_code( $response ) ) {
unlink( $tmpfname );
return new WP_Error( 'http_404', trim( wp_remote_retrieve_response_message( $response ) ) );
}
$content_md5 = wp_remote_retrieve_header( $response, 'content-md5' );
if ( $content_md5 ) {
$md5_check = verify_file_md5( $tmpfname, $content_md5 );
if ( is_wp_error( $md5_check ) ) {
unlink( $tmpfname );
return $md5_check;
}
}
return $tmpfname;
}
/**
* Main plugin Instance
*
* @static
* @return object Main instance
*
* @since 1.0
* @author Andrea Grillo <andrea.grillo@yithemes.com>
*/
public static function instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new self();
}
return self::$_instance;
}
/**
* Delete the update plugins transient
*
* @return void
*
* @since 1.0
* @see update_plugins transient and pre_set_site_transient_update_plugins hooks
* @author Andrea Grillo <andrea.grillo@yithemes.com>
*/
public function force_regenerate_update_transient(){
delete_site_transient( 'update_plugins' );
}
/**
* Check for plugins update
*
* If a new plugin version is available set it in the pre_set_site_transient_update_plugins hooks
*
* @param mixed $transient | update_plugins transient value
* @param bool $save | Default: false. Set true to regenerate the update_transient plugins
*
* @return mixed $transient | The new update_plugins transient value
*
* @since 1.0
* @see update_plugins transient and pre_set_site_transient_update_plugins hooks
* @author Andrea Grillo <andrea.grillo@yithemes.com>
*/
public function check_update( $transient, $save = false ) {
foreach ($this->_plugins as $init => $plugin) {
$xml = str_replace('%plugin_slug%', $this->_plugins[$init]['slug'], $this->_xml);
$remote_xml = wp_remote_get($xml);
if (!is_wp_error($remote_xml) && isset($remote_xml['response']['code']) && '200' == $remote_xml['response']['code']) {
$plugin_remote_info = new SimpleXmlElement($remote_xml['body']);
if (version_compare($plugin_remote_info->latest, $plugin['info']['Version'], '>') && !isset($transient->response[$init])) {
$package = YIT_Plugin_Licence()->check($init) ? $this->_package_url : null;
$obj = new stdClass();
$obj->slug = (string)$init;
$obj->new_version = (string)$plugin_remote_info->latest;
$obj->changelog = (string)$plugin_remote_info->changelog;
$obj->package = $package;
$obj->plugin = $init;
$transient->response[$init] = $obj;
}
}
}
if( $save ) {
set_site_transient( 'update_plugins', $transient );
}
return $transient;
}
/**
* Add the plugin update row in plugin page
*
* @return void
* @fire "in_theme_update_message-{$init}" action
*
* @since 1.0
* @see after_plugin_row_{$init} action
* @author Andrea Grillo <andrea.grillo@yithemes.com>
*/
public function plugin_update_row() {
$current = get_site_transient( 'update_plugins' );
$init = str_replace( 'after_plugin_row_', '', current_filter() );
if ( ! isset( $current->response[ $init ] ) ) {
return false;
}
/**
* stdClass Object
*/
$r = $current->response[ $init ];
$changelog_id = str_replace( array( '/', '.php', '.' ), array( '-', '', '-' ), $init );
$details_url = '#TB_inline' . esc_url( add_query_arg( array( 'width' => 722, 'height' => 914, 'inlineId' => $changelog_id ) , '' ) );
/**
* @see wp_plugin_update_rows() in wp-single\wp-admin\includes\update.php
*/
$wp_list_table = _get_list_table( 'WP_MS_Themes_List_Table' );
if( is_network_admin() || ! is_multisite() || true ) {
echo '<tr class="plugin-update-tr"><td colspan="' . $wp_list_table->get_column_count() . '" class="plugin-update colspanchange"><div class="update-message">';
if( ! current_user_can( 'update_plugins' ) ){
printf( __('There is a new version of %1$s available. <a href="%2$s" class="thickbox yit-changelog-button" title="%3$s">View version %4$s details</a>.', 'yith-plugin-fw'), $this->_plugins[ $init ]['info']['Name'], esc_url( $details_url ), esc_attr( $this->_plugins[ $init ]['info']['Name'] ), $r->new_version );
}elseif( is_plugin_active_for_network( $init ) ){
printf( __( 'There is a new version of %1$s available. <a href="%2$s" class="thickbox yit-changelog-button" title="%3$s">View version %4$s details</a>. <em>You have to activate the plugin on a single site of the network to benefit from automatic updates.</em>', 'yith-plugin-fw' ), $this->_plugins[ $init ]['info']['Name'], esc_url( $details_url ), esc_attr( $this->_plugins[ $init ]['info']['Name'] ), $r->new_version );
}elseif ( empty( $r->package ) ) {
printf( __( 'There is a new version of %1$s available. <a href="%2$s" class="thickbox yit-changelog-button" title="%3$s">View version %4$s details</a>. <em>Automatic update is unavailable for this plugin, please <a href="%5$s" title="Licence activation">activate</a> your copy of %6s.</em>', 'yith-plugin-fw' ), $this->_plugins[ $init ]['info']['Name'], esc_url( $details_url ), esc_attr( $this->_plugins[ $init ]['info']['Name'] ), $r->new_version, YIT_Plugin_Licence()->get_licence_activation_page_url(), $this->_plugins[ $init ]['info']['Name'] );
} else {
printf( __('There is a new version of %1$s available. <a href="%2$s" class="thickbox yit-changelog-button" title="%3$s">View version %4$s details</a> or <a href="%5$s">update now</a>.', 'yith-plugin-fw'), $this->_plugins[ $init ]['info']['Name'], esc_url($details_url), esc_attr( $this->_plugins[ $init ]['info']['Name'] ), $r->new_version, wp_nonce_url( self_admin_url('update.php?action=upgrade-plugin&plugin=') . $init, 'upgrade-plugin_' . $init ) );
}
/**
* Fires at the end of the update message container in each
* row of the themes list table.
*
* The dynamic portion of the hook name, `$theme_key`, refers to
* the theme slug as found in the WordPress.org themes repository.
*
* @since Wordpress 3.1.0
* }
*/
do_action( "in_theme_update_message-{$init}", $this->_plugins[ $init ], $r->changelog, $changelog_id );
echo '</div></td></tr>';
}
}
/**
* Remove the standard plugin_update_row
*
* Remove the standard plugin_update_row and Add a custom plugin update row in plugin page.
*
* @return void
* @fire "in_theme_update_message-{$init}" action
*
* @since 1.0
* @see after_plugin_row_{$init} action
* @author Andrea Grillo <andrea.grillo@yithemes.com>
*/
public function remove_wp_plugin_update_row() {
foreach( $this->_plugins as $init => $plugin ){
remove_action( "after_plugin_row_{$init}", 'wp_plugin_update_row', 10, 2 );
add_action( "after_plugin_row_{$init}", array( $this, 'plugin_update_row' ) );
add_action( "in_theme_update_message-{$init}", array( $this, 'in_theme_update_message' ), 10, 3 );
}
}
public function in_theme_update_message( $plugin, $changelog, $changelog_id, $echo = true ){
$res = "<div id='{$changelog_id}' class='yit-plugin-changelog-wrapper'>
<div class='yit-plugin-changelog'>
<h2 class='yit-plugin-changelog-title'>{$plugin['info']['Name']} - Changelog</h2>
<p>{$changelog}</p>
</div>
</div>";
if( $echo ){
echo $res;
}
else{
return $res;
}
}
/**
* Auto-Update Plugin in multisite
*
* Manage the non standard upgrade-plugin-multisite action
*
* @return void
*
* @since 1.0
* @see upgrade-plugin action
* @author Andrea Grillo <andrea.grillo@yithemes.com>
*/
public function upgrade_plugin_multisite(){
$plugin = isset($_REQUEST['plugin']) ? trim($_REQUEST['plugin']) : '';
$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : '';
if( 'upgrade-plugin-multisite' != $action ){
wp_die( __( 'You can\'t update the plugins for this site.', 'yith-plugin-fw' ) );
}
if ( ! current_user_can( 'update_plugins' ) ) {
wp_die( __( 'You do not have sufficient permissions to update the plugins for this site.', 'yith-plugin-fw' ) );
}
$this->check_update( get_site_transient( 'update_plugins') , true );
check_admin_referer( 'upgrade-plugin-multisite_' . $plugin );
$title = __( 'Update Plugin' );
$parent_file = 'plugins.php';
$submenu_file = 'plugins.php';
wp_enqueue_script( 'updates' );
require_once( ABSPATH . 'wp-admin/admin-header.php' );
$nonce = 'upgrade-plugin-multisite_' . $plugin;
$url = 'update.php?action=upgrade-plugin-multisite&plugin=' . urlencode( $plugin );
$upgrader = new Plugin_Upgrader( new Plugin_Upgrader_Skin( compact( 'title', 'nonce', 'url', 'plugin' ) ) );
$upgrader->upgrade( $plugin );
include( ABSPATH . 'wp-admin/admin-footer.php' );
}
}
}
if ( ! function_exists( 'YIT_Upgrade' ) ) {
/**
* Main instance of plugin
*
* @return object
* @since 1.0
* @author Andrea Grillo <andrea.grillo@yithemes.com>
*/
function YIT_Upgrade() {
return YIT_Upgrade::instance();
}
}
/**
* Instance a YIT_Upgrade object
*/
YIT_Upgrade();