PluginProbe
Polylang / 2.7.4
Polylang v2.7.4
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
polylang / install / plugin-updater.php

plugin-updater.php in Polylang 2.7.4, at install/plugin-updater.php

635 lines 19.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // Exit if accessed directly
4 if ( ! defined( 'ABSPATH' ) ) {
5 exit;
6 }
7
8 /**
9 * Allows plugins to use their own update API.
10 * Modified version with 'polylang' text domain and comments for translators
11 *
12 * @author Easy Digital Downloads
13 * @version 1.7.1
14 */
15 class PLL_Plugin_Updater {
16
17 private $api_url = '';
18 private $api_data = array();
19 private $name = '';
20 private $slug = '';
21 private $version = '';
22 private $wp_override = false;
23 private $cache_key = '';
24
25 private $health_check_timeout = 5;
26
27 /**
28 * Class constructor.
29 *
30 * @uses plugin_basename()
31 * @uses hook()
32 *
33 * @param string $_api_url The URL pointing to the custom API endpoint.
34 * @param string $_plugin_file Path to the plugin file.
35 * @param array $_api_data Optional data to send with API calls.
36 */
37 public function __construct( $_api_url, $_plugin_file, $_api_data = null ) {
38
39 global $edd_plugin_data;
40
41 $this->api_url = trailingslashit( $_api_url );
42 $this->api_data = $_api_data;
43 $this->name = plugin_basename( $_plugin_file );
44 $this->slug = basename( $_plugin_file, '.php' );
45 $this->version = $_api_data['version'];
46 $this->wp_override = isset( $_api_data['wp_override'] ) ? (bool) $_api_data['wp_override'] : false;
47 $this->beta = ! empty( $this->api_data['beta'] ) ? true : false;
48 $this->cache_key = 'edd_sl_' . md5( serialize( $this->slug . $this->api_data['license'] . $this->beta ) );
49
50 $edd_plugin_data[ $this->slug ] = $this->api_data;
51
52 /**
53 * Fires after the $edd_plugin_data is setup.
54 *
55 * @since x.x.x
56 *
57 * @param array $edd_plugin_data Array of EDD SL plugin data.
58 */
59 do_action( 'post_edd_sl_plugin_updater_setup', $edd_plugin_data );
60
61 // Set up hooks.
62 $this->init();
63
64 }
65
66 /**
67 * Set up WordPress filters to hook into WP's update process.
68 *
69 * @uses add_filter()
70 *
71 * @return void
72 */
73 public function init() {
74
75 add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'check_update' ) );
76 add_filter( 'plugins_api', array( $this, 'plugins_api_filter' ), 10, 3 );
77 remove_action( 'after_plugin_row_' . $this->name, 'wp_plugin_update_row', 10 );
78 add_action( 'after_plugin_row_' . $this->name, array( $this, 'show_update_notification' ), 10, 2 );
79 add_action( 'admin_init', array( $this, 'show_changelog' ) );
80
81 }
82
83 /**
84 * Check for Updates at the defined API endpoint and modify the update array.
85 *
86 * This function dives into the update API just when WordPress creates its update array,
87 * then adds a custom API call and injects the custom plugin data retrieved from the API.
88 * It is reassembled from parts of the native WordPress plugin update code.
89 * See wp-includes/update.php line 121 for the original wp_update_plugins() function.
90 *
91 * @uses api_request()
92 *
93 * @param array $_transient_data Update array build by WordPress.
94 * @return array Modified update array with custom plugin data.
95 */
96 public function check_update( $_transient_data ) {
97
98 global $pagenow;
99
100 if ( ! is_object( $_transient_data ) ) {
101 $_transient_data = new stdClass;
102 }
103
104 if ( 'plugins.php' == $pagenow && is_multisite() ) {
105 return $_transient_data;
106 }
107
108 if ( ! empty( $_transient_data->response ) && ! empty( $_transient_data->response[ $this->name ] ) && false === $this->wp_override ) {
109 return $_transient_data;
110 }
111
112 $version_info = $this->get_cached_version_info();
113
114 if ( false === $version_info ) {
115 $version_info = $this->api_request( 'plugin_latest_version', array( 'slug' => $this->slug, 'beta' => $this->beta ) );
116
117 $this->set_version_info_cache( $version_info );
118
119 }
120
121 if ( false !== $version_info && is_object( $version_info ) && isset( $version_info->new_version ) ) {
122
123 $no_update = false;
124 if ( version_compare( $this->version, $version_info->new_version, '<' ) ) {
125
126 $_transient_data->response[ $this->name ] = $version_info;
127
128 // Make sure the plugin property is set to the plugin's name/location. See issue 1463 on Software Licensing's GitHub repo.
129 $_transient_data->response[ $this->name ]->plugin = $this->name;
130
131 } else {
132 $no_update = new stdClass();
133 $no_update->id = '';
134 $no_update->slug = $this->slug;
135 $no_update->plugin = $this->name;
136 $no_update->new_version = $version_info->new_version;
137 $no_update->url = $version_info->homepage;
138 $no_update->package = isset( $version_info->package ) ? $version_info->package : ''; #modified#
139 $no_update->icons = $version_info->icons;
140 $no_update->banners = $version_info->banners;
141 $no_update->banners_rtl = array();
142 }
143
144 $_transient_data->last_checked = time();
145 $_transient_data->checked[ $this->name ] = $this->version;
146
147 if ( $no_update ) {
148 $_transient_data->no_update[ $this->name ] = $no_update;
149 }
150 }
151
152 return $_transient_data;
153 }
154
155 /**
156 * show update nofication row -- needed for multisite subsites, because WP won't tell you otherwise!
157 *
158 * @param string $file
159 * @param array $plugin
160 */
161 public function show_update_notification( $file, $plugin ) {
162
163 if ( is_network_admin() ) {
164 return;
165 }
166
167 if( ! current_user_can( 'update_plugins' ) ) {
168 return;
169 }
170
171 if( ! is_multisite() ) {
172 return;
173 }
174
175 if ( $this->name != $file ) {
176 return;
177 }
178
179 // Remove our filter on the site transient
180 remove_filter( 'pre_set_site_transient_update_plugins', array( $this, 'check_update' ), 10 );
181
182 $update_cache = get_site_transient( 'update_plugins' );
183
184 $update_cache = is_object( $update_cache ) ? $update_cache : new stdClass();
185
186 if ( empty( $update_cache->response ) || empty( $update_cache->response[ $this->name ] ) ) {
187
188 $version_info = $this->get_cached_version_info();
189
190 if ( false === $version_info ) {
191 $version_info = $this->api_request( 'plugin_latest_version', array( 'slug' => $this->slug, 'beta' => $this->beta ) );
192
193 // Since we disabled our filter for the transient, we aren't running our object conversion on banners, sections, or icons. Do this now:
194 if ( isset( $version_info->banners ) && ! is_array( $version_info->banners ) ) {
195 $version_info->banners = $this->convert_object_to_array( $version_info->banners );
196 }
197
198 if ( isset( $version_info->sections ) && ! is_array( $version_info->sections ) ) {
199 $version_info->sections = $this->convert_object_to_array( $version_info->sections );
200 }
201
202 if ( isset( $version_info->icons ) && ! is_array( $version_info->icons ) ) {
203 $version_info->icons = $this->convert_object_to_array( $version_info->icons );
204 }
205
206 if ( isset( $version_info->contributors ) && ! is_array( $version_info->contributors ) ) {
207 $version_info->contributors = $this->convert_object_to_array( $version_info->contributors );
208 }
209
210 $this->set_version_info_cache( $version_info );
211 }
212
213 if ( ! is_object( $version_info ) ) {
214 return;
215 }
216
217 $no_update = false;
218 if ( version_compare( $this->version, $version_info->new_version, '<' ) ) {
219
220 $update_cache->response[ $this->name ] = $version_info;
221
222 } else {
223 $no_update = new stdClass();
224 $no_update->id = '';
225 $no_update->slug = $this->slug;
226 $no_update->plugin = $this->name;
227 $no_update->new_version = $version_info->new_version;
228 $no_update->url = $version_info->homepage;
229 $no_update->package = isset( $version_info->package ) ? $version_info->package : ''; #modified#
230 $no_update->icons = $version_info->icons;
231 $no_update->banners = $version_info->banners;
232 $no_update->banners_rtl = array();
233 }
234
235 $update_cache->last_checked = time();
236 $update_cache->checked[ $this->name ] = $this->version;
237 if ( $no_update ) {
238 $update_cache->no_update[ $this->name ] = $no_update;
239 }
240
241 set_site_transient( 'update_plugins', $update_cache );
242
243 } else {
244
245 $version_info = $update_cache->response[ $this->name ];
246
247 }
248
249 // Restore our filter
250 add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'check_update' ) );
251
252 if ( ! empty( $update_cache->response[ $this->name ] ) && version_compare( $this->version, $version_info->new_version, '<' ) ) {
253
254 // build a plugin list row, with update notification
255 $wp_list_table = _get_list_table( 'WP_Plugins_List_Table' );
256 # <tr class="plugin-update-tr"><td colspan="' . $wp_list_table->get_column_count() . '" class="plugin-update colspanchange">
257 echo '<tr class="plugin-update-tr" id="' . $this->slug . '-update" data-slug="' . $this->slug . '" data-plugin="' . $this->slug . '/' . $file . '">';
258 echo '<td colspan="3" class="plugin-update colspanchange">';
259 echo '<div class="update-message notice inline notice-warning notice-alt">';
260
261 $changelog_link = self_admin_url( 'index.php?edd_sl_action=view_plugin_changelog&plugin=' . $this->name . '&slug=' . $this->slug . '&TB_iframe=true&width=772&height=911' );
262
263 if ( empty( $version_info->download_link ) ) {
264 printf(
265 /* translators: %1$s plugin name, %3$s plugin version, %2$s is link start tag, %4$s is link end tag. */
266 __( 'There is a new version of %1$s available. %2$sView version %3$s details%4$s.', 'polylang' ),
267 esc_html( $version_info->name ),
268 '<a target="_blank" class="thickbox" href="' . esc_url( $changelog_link ) . '">',
269 esc_html( $version_info->new_version ),
270 '</a>'
271 );
272 } else {
273 printf(
274 /* translators: %1$s plugin name, %3$s plugin version, %2$s and %5$s are link start tags, %4$s and %6$s are link end tags. */
275 __( 'There is a new version of %1$s available. %2$sView version %3$s details%4$s or %5$supdate now%6$s.', 'polylang' ),
276 esc_html( $version_info->name ),
277 '<a target="_blank" class="thickbox" href="' . esc_url( $changelog_link ) . '">',
278 esc_html( $version_info->new_version ),
279 '</a>',
280 '<a href="' . esc_url( wp_nonce_url( self_admin_url( 'update.php?action=upgrade-plugin&plugin=' ) . $this->name, 'upgrade-plugin_' . $this->name ) ) .'">',
281 '</a>'
282 );
283 }
284
285 do_action( "in_plugin_update_message-{$file}", $plugin, $version_info );
286
287 echo '</div></td></tr>';
288 }
289 }
290
291 /**
292 * Updates information on the "View version x.x details" page with custom data.
293 *
294 * @uses api_request()
295 *
296 * @param mixed $_data
297 * @param string $_action
298 * @param object $_args
299 * @return object $_data
300 */
301 public function plugins_api_filter( $_data, $_action = '', $_args = null ) {
302
303 if ( $_action != 'plugin_information' ) {
304
305 return $_data;
306
307 }
308
309 if ( ! isset( $_args->slug ) || ( $_args->slug != $this->slug ) ) {
310
311 return $_data;
312
313 }
314
315 $to_send = array(
316 'slug' => $this->slug,
317 'is_ssl' => is_ssl(),
318 'fields' => array(
319 'banners' => array(),
320 'reviews' => false,
321 'icons' => array(),
322 )
323 );
324
325 // Get the transient where we store the api request for this plugin for 24 hours
326 $edd_api_request_transient = $this->get_cached_version_info();
327
328 //If we have no transient-saved value, run the API, set a fresh transient with the API value, and return that value too right now.
329 if ( empty( $edd_api_request_transient ) ) {
330
331 $api_response = $this->api_request( 'plugin_information', $to_send );
332
333 // Expires in 3 hours
334 $this->set_version_info_cache( $api_response );
335
336 if ( false !== $api_response ) {
337 $_data = $api_response;
338 }
339
340 } else {
341 $_data = $edd_api_request_transient;
342 }
343
344 // Convert sections into an associative array, since we're getting an object, but Core expects an array.
345 if ( isset( $_data->sections ) && ! is_array( $_data->sections ) ) {
346 $_data->sections = $this->convert_object_to_array( $_data->sections );
347 }
348
349 // Convert banners into an associative array, since we're getting an object, but Core expects an array.
350 if ( isset( $_data->banners ) && ! is_array( $_data->banners ) ) {
351 $_data->banners = $this->convert_object_to_array( $_data->banners );
352 }
353
354 // Convert icons into an associative array, since we're getting an object, but Core expects an array.
355 if ( isset( $_data->icons ) && ! is_array( $_data->icons ) ) {
356 $_data->icons = $this->convert_object_to_array( $_data->icons );
357 }
358
359 // Convert contributors into an associative array, since we're getting an object, but Core expects an array.
360 if ( isset( $_data->contributors ) && ! is_array( $_data->contributors ) ) {
361 $_data->contributors = $this->convert_object_to_array( $_data->contributors );
362 }
363
364 if( ! isset( $_data->plugin ) ) {
365 $_data->plugin = $this->name;
366 }
367
368 return $_data;
369 }
370
371 /**
372 * Convert some objects to arrays when injecting data into the update API
373 *
374 * Some data like sections, banners, and icons are expected to be an associative array, however due to the JSON
375 * decoding, they are objects. This method allows us to pass in the object and return an associative array.
376 *
377 * @since 3.6.5
378 *
379 * @param stdClass $data
380 *
381 * @return array
382 */
383 private function convert_object_to_array( $data ) {
384 $new_data = array();
385 foreach ( $data as $key => $value ) {
386 $new_data[ $key ] = is_object( $value ) ? $this->convert_object_to_array( $value ) : $value;
387 }
388
389 return $new_data;
390 }
391
392 /**
393 * Disable SSL verification in order to prevent download update failures
394 *
395 * @param array $args
396 * @param string $url
397 * @return object $array
398 */
399 public function http_request_args( $args, $url ) {
400
401 $verify_ssl = $this->verify_ssl();
402 if ( strpos( $url, 'https://' ) !== false && strpos( $url, 'edd_action=package_download' ) ) {
403 $args['sslverify'] = $verify_ssl;
404 }
405 return $args;
406
407 }
408
409 /**
410 * Calls the API and, if successfull, returns the object delivered by the API.
411 *
412 * @uses get_bloginfo()
413 * @uses wp_remote_post()
414 * @uses is_wp_error()
415 *
416 * @param string $_action The requested action.
417 * @param array $_data Parameters for the API action.
418 * @return false|object
419 */
420 private function api_request( $_action, $_data ) {
421
422 global $wp_version, $edd_plugin_url_available;
423
424 $verify_ssl = $this->verify_ssl();
425
426 // Do a quick status check on this domain if we haven't already checked it.
427 $store_hash = md5( $this->api_url );
428 if ( ! is_array( $edd_plugin_url_available ) || ! isset( $edd_plugin_url_available[ $store_hash ] ) ) {
429 $test_url_parts = parse_url( $this->api_url );
430
431 $scheme = ! empty( $test_url_parts['scheme'] ) ? $test_url_parts['scheme'] : 'http';
432 $host = ! empty( $test_url_parts['host'] ) ? $test_url_parts['host'] : '';
433 $port = ! empty( $test_url_parts['port'] ) ? ':' . $test_url_parts['port'] : '';
434
435 if ( empty( $host ) ) {
436 $edd_plugin_url_available[ $store_hash ] = false;
437 } else {
438 $test_url = $scheme . '://' . $host . $port;
439 $response = wp_remote_get( $test_url, array( 'timeout' => $this->health_check_timeout, 'sslverify' => $verify_ssl ) );
440 $edd_plugin_url_available[ $store_hash ] = is_wp_error( $response ) ? false : true;
441 }
442 }
443
444 if ( false === $edd_plugin_url_available[ $store_hash ] ) {
445 return;
446 }
447
448 $data = array_merge( $this->api_data, $_data );
449
450 if ( $data['slug'] != $this->slug ) {
451 return;
452 }
453
454 if( $this->api_url == trailingslashit ( home_url() ) ) {
455 return false; // Don't allow a plugin to ping itself
456 }
457
458 $api_params = array(
459 'edd_action' => 'get_version',
460 'license' => ! empty( $data['license'] ) ? $data['license'] : '',
461 'item_name' => isset( $data['item_name'] ) ? $data['item_name'] : false,
462 'item_id' => isset( $data['item_id'] ) ? $data['item_id'] : false,
463 'version' => isset( $data['version'] ) ? $data['version'] : false,
464 'slug' => $data['slug'],
465 'author' => $data['author'],
466 'url' => home_url(),
467 'beta' => ! empty( $data['beta'] ),
468 );
469
470 $request = wp_remote_post( $this->api_url, array( 'timeout' => 15, 'sslverify' => $verify_ssl, 'body' => $api_params ) );
471
472 if ( ! is_wp_error( $request ) ) {
473 $request = json_decode( wp_remote_retrieve_body( $request ) );
474 }
475
476 if ( $request && isset( $request->sections ) ) {
477 $request->sections = maybe_unserialize( $request->sections );
478 } else {
479 $request = false;
480 }
481
482 if ( $request && isset( $request->banners ) ) {
483 $request->banners = maybe_unserialize( $request->banners );
484 }
485
486 if ( $request && isset( $request->icons ) ) {
487 $request->icons = maybe_unserialize( $request->icons );
488 }
489
490 if( ! empty( $request->sections ) ) {
491 foreach( $request->sections as $key => $section ) {
492 $request->$key = (array) $section;
493 }
494 }
495
496 return $request;
497 }
498
499 /**
500 * If available, show the changelog for sites in a multisite install.
501 */
502 public function show_changelog() {
503
504 global $edd_plugin_data;
505
506 if( empty( $_REQUEST['edd_sl_action'] ) || 'view_plugin_changelog' != $_REQUEST['edd_sl_action'] ) {
507 return;
508 }
509
510 if( empty( $_REQUEST['plugin'] ) ) {
511 return;
512 }
513
514 if( empty( $_REQUEST['slug'] ) ) {
515 return;
516 }
517
518 if( ! current_user_can( 'update_plugins' ) ) {
519 wp_die( __( 'You do not have permission to install plugin updates', 'polylang' ), __( 'Error', 'polylang' ), array( 'response' => 403 ) );
520 }
521
522 $data = $edd_plugin_data[ $_REQUEST['slug'] ];
523 $version_info = $this->get_cached_version_info();
524
525 if( false === $version_info ) {
526
527 $api_params = array(
528 'edd_action' => 'get_version',
529 'item_name' => isset( $data['item_name'] ) ? $data['item_name'] : false,
530 'item_id' => isset( $data['item_id'] ) ? $data['item_id'] : false,
531 'slug' => $_REQUEST['slug'],
532 'author' => $data['author'],
533 'url' => home_url(),
534 'beta' => ! empty( $data['beta'] )
535 );
536
537 $verify_ssl = $this->verify_ssl();
538 $request = wp_remote_post( $this->api_url, array( 'timeout' => 15, 'sslverify' => $verify_ssl, 'body' => $api_params ) );
539
540 if ( ! is_wp_error( $request ) ) {
541 $version_info = json_decode( wp_remote_retrieve_body( $request ) );
542 }
543
544 if ( ! empty( $version_info ) && isset( $version_info->sections ) ) {
545 $version_info->sections = maybe_unserialize( $version_info->sections );
546 } else {
547 $version_info = false;
548 }
549
550 if( ! empty( $version_info ) ) {
551 foreach( $version_info->sections as $key => $section ) {
552 $version_info->$key = (array) $section;
553 }
554 }
555
556 $this->set_version_info_cache( $version_info );
557
558 // Delete the unneeded option
559 delete_option( md5( 'edd_plugin_' . sanitize_key( $_REQUEST['plugin'] ) . '_' . $this->beta . '_version_info' ) );
560 }
561
562 if ( isset( $version_info->sections ) ) {
563 $sections = $this->convert_object_to_array( $version_info->sections );
564 if ( ! empty( $sections['changelog'] ) ) {
565 echo '<div style="background:#fff;padding:10px;">' . wp_kses_post( $sections['changelog'] ) . '</div>';
566 }
567 }
568
569 exit;
570 }
571
572 /**
573 * Gets the plugin's cached version information from the database.
574 *
575 * @param string $cache_key
576 * @return boolean|string
577 */
578 public function get_cached_version_info( $cache_key = '' ) {
579
580 if( empty( $cache_key ) ) {
581 $cache_key = $this->cache_key;
582 }
583
584 $cache = get_option( $cache_key );
585
586 if( empty( $cache['timeout'] ) || time() > $cache['timeout'] ) {
587 return false; // Cache is expired
588 }
589
590 // We need to turn the icons into an array, thanks to WP Core forcing these into an object at some point.
591 $cache['value'] = json_decode( $cache['value'] );
592 if ( ! empty( $cache['value']->icons ) ) {
593 $cache['value']->icons = (array) $cache['value']->icons;
594 }
595
596 return $cache['value'];
597
598 }
599
600 /**
601 * Adds the plugin version information to the database.
602 *
603 * @param string $value
604 * @param string $cache_key
605 */
606 public function set_version_info_cache( $value = '', $cache_key = '' ) {
607
608 if( empty( $cache_key ) ) {
609 $cache_key = $this->cache_key;
610 }
611
612 $data = array(
613 'timeout' => strtotime( '+3 hours', time() ),
614 'value' => json_encode( $value )
615 );
616
617 update_option( $cache_key, $data, 'no' );
618
619 // Delete the duplicate option
620 delete_option( 'edd_api_request_' . md5( serialize( $this->slug . $this->api_data['license'] . $this->beta ) ) );
621 }
622
623 /**
624 * Returns if the SSL of the store should be verified.
625 *
626 * @since 1.6.13
627 * @return bool
628 */
629 private function verify_ssl() {
630 return (bool) apply_filters( 'edd_sl_api_request_verify_ssl', true, $this );
631 }
632
633 }
634
635