PluginProbe
Polylang / 3.7.1
Polylang v3.7.1
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 3.7.1, at install/plugin-updater.php

729 lines 21.5 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:
11 * - 'polylang' text domain,
12 * - missing comments for translators,
13 * - a bug fix (https://github.com/polylang/polylang/pull/1629).
14 *
15 * @author Easy Digital Downloads
16 * @version 1.9.4
17 */
18 class PLL_Plugin_Updater {
19
20 private $api_url = '';
21 private $api_data = array();
22 private $plugin_file = '';
23 private $name = '';
24 private $slug = '';
25 private $version = '';
26 private $wp_override = false;
27 private $beta = false;
28 private $failed_request_cache_key;
29
30 /**
31 * Class constructor.
32 *
33 * @uses plugin_basename()
34 * @uses hook()
35 *
36 * @param string $_api_url The URL pointing to the custom API endpoint.
37 * @param string $_plugin_file Path to the plugin file.
38 * @param array $_api_data Optional data to send with API calls.
39 */
40 public function __construct( $_api_url, $_plugin_file, $_api_data = null ) {
41
42 global $edd_plugin_data;
43
44 $this->api_url = trailingslashit( $_api_url );
45 $this->api_data = $_api_data;
46 $this->plugin_file = $_plugin_file;
47 $this->name = plugin_basename( $_plugin_file );
48 $this->slug = basename( dirname( $_plugin_file ) );
49 $this->version = $_api_data['version'];
50 $this->wp_override = isset( $_api_data['wp_override'] ) ? (bool) $_api_data['wp_override'] : false;
51 $this->beta = ! empty( $this->api_data['beta'] ) ? true : false;
52 $this->failed_request_cache_key = 'edd_sl_failed_http_' . md5( $this->api_url );
53
54 $edd_plugin_data[ $this->slug ] = $this->api_data;
55
56 /**
57 * Fires after the $edd_plugin_data is setup.
58 *
59 * @since x.x.x
60 *
61 * @param array $edd_plugin_data Array of EDD SL plugin data.
62 */
63 do_action( 'post_edd_sl_plugin_updater_setup', $edd_plugin_data );
64
65 // Set up hooks.
66 $this->init();
67 }
68
69 /**
70 * Set up WordPress filters to hook into WP's update process.
71 *
72 * @uses add_filter()
73 *
74 * @return void
75 */
76 public function init() {
77
78 add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'check_update' ) );
79 add_filter( 'plugins_api', array( $this, 'plugins_api_filter' ), 10, 3 );
80 add_action( 'after_plugin_row', array( $this, 'show_update_notification' ), 10, 2 );
81 add_action( 'admin_init', array( $this, 'show_changelog' ) );
82 }
83
84 /**
85 * Check for Updates at the defined API endpoint and modify the update array.
86 *
87 * This function dives into the update API just when WordPress creates its update array,
88 * then adds a custom API call and injects the custom plugin data retrieved from the API.
89 * It is reassembled from parts of the native WordPress plugin update code.
90 * See wp-includes/update.php line 121 for the original wp_update_plugins() function.
91 *
92 * @uses api_request()
93 *
94 * @param array $_transient_data Update array build by WordPress.
95 * @return array Modified update array with custom plugin data.
96 */
97 public function check_update( $_transient_data ) {
98
99 if ( ! is_object( $_transient_data ) ) {
100 $_transient_data = new stdClass();
101 }
102
103 if ( ! empty( $_transient_data->response ) && ! empty( $_transient_data->response[ $this->name ] ) && false === $this->wp_override ) {
104 return $_transient_data;
105 }
106
107 $current = $this->get_update_transient_data();
108 if ( false !== $current && is_object( $current ) && isset( $current->new_version ) ) {
109 if ( version_compare( $this->version, $current->new_version, '<' ) ) {
110 $_transient_data->response[ $this->name ] = $current;
111 } else {
112 // Populating the no_update information is required to support auto-updates in WordPress 5.5.
113 $_transient_data->no_update[ $this->name ] = $current;
114 }
115 }
116 $_transient_data->last_checked = time();
117 $_transient_data->checked[ $this->name ] = $this->version;
118
119 return $_transient_data;
120 }
121
122 /**
123 * Get repo API data from store.
124 * Save to cache.
125 *
126 * @return \stdClass
127 */
128 public function get_repo_api_data() {
129 $version_info = $this->get_cached_version_info();
130
131 if ( false === $version_info ) {
132 $version_info = $this->api_request(
133 'plugin_latest_version',
134 array(
135 'slug' => $this->slug,
136 'beta' => $this->beta,
137 )
138 );
139 if ( ! $version_info ) {
140 return false;
141 }
142
143 // This is required for your plugin to support auto-updates in WordPress 5.5.
144 $version_info->plugin = $this->name;
145 $version_info->id = $this->name;
146 $version_info->tested = $this->get_tested_version( $version_info );
147 if ( ! isset( $version_info->requires ) ) {
148 $version_info->requires = '';
149 }
150 if ( ! isset( $version_info->requires_php ) ) {
151 $version_info->requires_php = '';
152 }
153
154 $this->set_version_info_cache( $version_info );
155 }
156
157 // Added by Polylang.
158 $defaults = array(
159 'url' => '',
160 'package' => '',
161 'new_version' => '',
162 'tested' => '',
163 'requires' => '',
164 'requires_php' => '',
165 'icons' => new stdClass(),
166 'banners' => new stdClass(),
167 );
168
169 $version_info = (object) array_merge( $defaults, (array) $version_info );
170 // End of added by Polylang.
171
172 return $version_info;
173 }
174
175 /**
176 * Gets a limited set of data from the API response.
177 * This is used for the update_plugins transient.
178 *
179 * @since 3.8.12
180 * @return \stdClass|false
181 */
182 private function get_update_transient_data() {
183 $version_info = $this->get_repo_api_data();
184
185 if ( ! $version_info ) {
186 return false;
187 }
188
189 $limited_data = new \stdClass();
190 $limited_data->slug = $this->slug;
191 $limited_data->plugin = $this->name;
192 $limited_data->url = $version_info->url;
193 $limited_data->package = $version_info->package;
194 $limited_data->icons = $this->convert_object_to_array( $version_info->icons );
195 $limited_data->banners = $this->convert_object_to_array( $version_info->banners );
196 $limited_data->new_version = $version_info->new_version;
197 $limited_data->tested = $version_info->tested;
198 $limited_data->requires = $version_info->requires;
199 $limited_data->requires_php = $version_info->requires_php;
200
201 return $limited_data;
202 }
203
204 /**
205 * Gets the plugin's tested version.
206 *
207 * @since 1.9.2
208 * @param object $version_info
209 * @return null|string
210 */
211 private function get_tested_version( $version_info ) {
212
213 // There is no tested version.
214 if ( empty( $version_info->tested ) ) {
215 return null;
216 }
217
218 // Strip off extra version data so the result is x.y or x.y.z.
219 list( $current_wp_version ) = explode( '-', get_bloginfo( 'version' ) );
220
221 // The tested version is greater than or equal to the current WP version, no need to do anything.
222 if ( version_compare( $version_info->tested, $current_wp_version, '>=' ) ) {
223 return $version_info->tested;
224 }
225 $current_version_parts = explode( '.', $current_wp_version );
226 $tested_parts = explode( '.', $version_info->tested );
227
228 // The current WordPress version is x.y.z, so update the tested version to match it.
229 if ( isset( $current_version_parts[2] ) && $current_version_parts[0] === $tested_parts[0] && $current_version_parts[1] === $tested_parts[1] ) {
230 $tested_parts[2] = $current_version_parts[2];
231 }
232
233 return implode( '.', $tested_parts );
234 }
235
236 /**
237 * Show the update notification on multisite subsites.
238 *
239 * @param string $file
240 * @param array $plugin
241 */
242 public function show_update_notification( $file, $plugin ) {
243
244 // Return early if in the network admin, or if this is not a multisite install.
245 if ( is_network_admin() || ! is_multisite() ) {
246 return;
247 }
248
249 // Allow single site admins to see that an update is available.
250 if ( ! current_user_can( 'activate_plugins' ) ) {
251 return;
252 }
253
254 if ( $this->name !== $file ) {
255 return;
256 }
257
258 // Do not print any message if update does not exist.
259 $update_cache = get_site_transient( 'update_plugins' );
260
261 if ( ! isset( $update_cache->response[ $this->name ] ) ) {
262 if ( ! is_object( $update_cache ) ) {
263 $update_cache = new stdClass();
264 }
265 $update_cache->response[ $this->name ] = $this->get_repo_api_data();
266 }
267
268 // Return early if this plugin isn't in the transient->response or if the site is running the current or newer version of the plugin.
269 if ( empty( $update_cache->response[ $this->name ] ) || version_compare( $this->version, $update_cache->response[ $this->name ]->new_version, '>=' ) ) {
270 return;
271 }
272
273 printf(
274 '<tr class="plugin-update-tr %3$s" id="%1$s-update" data-slug="%1$s" data-plugin="%2$s">',
275 $this->slug,
276 $file,
277 in_array( $this->name, $this->get_active_plugins(), true ) ? 'active' : 'inactive'
278 );
279
280 echo '<td colspan="3" class="plugin-update colspanchange">';
281 echo '<div class="update-message notice inline notice-warning notice-alt"><p>';
282
283 $changelog_link = '';
284 if ( ! empty( $update_cache->response[ $this->name ]->sections->changelog ) ) {
285 $changelog_link = add_query_arg(
286 array(
287 'edd_sl_action' => 'view_plugin_changelog',
288 'plugin' => urlencode( $this->name ),
289 'slug' => urlencode( $this->slug ),
290 'TB_iframe' => 'true',
291 'width' => 77,
292 'height' => 911,
293 ),
294 self_admin_url( 'index.php' )
295 );
296 }
297 $update_link = add_query_arg(
298 array(
299 'action' => 'upgrade-plugin',
300 'plugin' => urlencode( $this->name ),
301 ),
302 self_admin_url( 'update.php' )
303 );
304
305 printf(
306 /* translators: the plugin name. */
307 esc_html__( 'There is a new version of %1$s available.', 'polylang' ),
308 esc_html( $plugin['Name'] )
309 );
310
311 if ( ! current_user_can( 'update_plugins' ) ) {
312 echo ' ';
313 esc_html_e( 'Contact your network administrator to install the update.', 'polylang' );
314 } elseif ( empty( $update_cache->response[ $this->name ]->package ) && ! empty( $changelog_link ) ) {
315 echo ' ';
316 printf(
317 /* translators: 1. opening anchor tag, do not translate 2. the new plugin version 3. closing anchor tag, do not translate. */
318 __( '%1$sView version %2$s details%3$s.', 'polylang' ),
319 '<a target="_blank" class="thickbox open-plugin-details-modal" href="' . esc_url( $changelog_link ) . '">',
320 esc_html( $update_cache->response[ $this->name ]->new_version ),
321 '</a>'
322 );
323 } elseif ( ! empty( $changelog_link ) ) {
324 echo ' ';
325 printf(
326 /* translators: 1. and 4. are opening anchor tags 2. the new plugin version 3. and 5. are closing anchor tags. */
327 __( '%1$sView version %2$s details%3$s or %4$supdate now%5$s.', 'polylang' ),
328 '<a target="_blank" class="thickbox open-plugin-details-modal" href="' . esc_url( $changelog_link ) . '">',
329 esc_html( $update_cache->response[ $this->name ]->new_version ),
330 '</a>',
331 '<a target="_blank" class="update-link" href="' . esc_url( wp_nonce_url( $update_link, 'upgrade-plugin_' . $file ) ) . '">',
332 '</a>'
333 );
334 } else {
335 printf(
336 ' %1$s%2$s%3$s',
337 '<a target="_blank" class="update-link" href="' . esc_url( wp_nonce_url( $update_link, 'upgrade-plugin_' . $file ) ) . '">',
338 esc_html__( 'Update now.', 'polylang' ),
339 '</a>'
340 );
341 }
342
343 do_action( "in_plugin_update_message-{$file}", $plugin, $plugin );
344
345 echo '</p></div></td></tr>';
346 }
347
348 /**
349 * Gets the plugins active in a multisite network.
350 *
351 * @return array
352 */
353 private function get_active_plugins() {
354 $active_plugins = (array) get_option( 'active_plugins' );
355 $active_network_plugins = (array) get_site_option( 'active_sitewide_plugins' );
356
357 return array_merge( $active_plugins, array_keys( $active_network_plugins ) );
358 }
359
360 /**
361 * Updates information on the "View version x.x details" page with custom data.
362 *
363 * @uses api_request()
364 *
365 * @param mixed $_data
366 * @param string $_action
367 * @param object $_args
368 * @return object $_data
369 */
370 public function plugins_api_filter( $_data, $_action = '', $_args = null ) {
371
372 if ( 'plugin_information' !== $_action ) {
373
374 return $_data;
375
376 }
377
378 if ( ! isset( $_args->slug ) || ( $_args->slug !== $this->slug ) ) {
379
380 return $_data;
381
382 }
383
384 $to_send = array(
385 'slug' => $this->slug,
386 'is_ssl' => is_ssl(),
387 'fields' => array(
388 'banners' => array(),
389 'reviews' => false,
390 'icons' => array(),
391 ),
392 );
393
394 // Get the transient where we store the api request for this plugin for 24 hours
395 $edd_api_request_transient = $this->get_cached_version_info();
396
397 //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.
398 if ( empty( $edd_api_request_transient ) ) {
399
400 $api_response = $this->api_request( 'plugin_information', $to_send );
401
402 // Expires in 3 hours
403 $this->set_version_info_cache( $api_response );
404
405 if ( false !== $api_response ) {
406 $_data = $api_response;
407 }
408 } else {
409 $_data = $edd_api_request_transient;
410 }
411
412 // Convert sections into an associative array, since we're getting an object, but Core expects an array.
413 if ( isset( $_data->sections ) && ! is_array( $_data->sections ) ) {
414 $_data->sections = $this->convert_object_to_array( $_data->sections );
415 }
416
417 // Convert banners into an associative array, since we're getting an object, but Core expects an array.
418 if ( isset( $_data->banners ) && ! is_array( $_data->banners ) ) {
419 $_data->banners = $this->convert_object_to_array( $_data->banners );
420 }
421
422 // Convert icons into an associative array, since we're getting an object, but Core expects an array.
423 if ( isset( $_data->icons ) && ! is_array( $_data->icons ) ) {
424 $_data->icons = $this->convert_object_to_array( $_data->icons );
425 }
426
427 // Convert contributors into an associative array, since we're getting an object, but Core expects an array.
428 if ( isset( $_data->contributors ) && ! is_array( $_data->contributors ) ) {
429 $_data->contributors = $this->convert_object_to_array( $_data->contributors );
430 }
431
432 if ( ! isset( $_data->plugin ) ) {
433 $_data->plugin = $this->name;
434 }
435
436 if ( ! isset( $_data->version ) && ! empty( $_data->new_version ) ) {
437 $_data->version = $_data->new_version;
438 }
439
440 return $_data;
441 }
442
443 /**
444 * Convert some objects to arrays when injecting data into the update API
445 *
446 * Some data like sections, banners, and icons are expected to be an associative array, however due to the JSON
447 * decoding, they are objects. This method allows us to pass in the object and return an associative array.
448 *
449 * @since 3.6.5
450 *
451 * @param stdClass $data
452 *
453 * @return array
454 */
455 private function convert_object_to_array( $data ) {
456 if ( ! is_array( $data ) && ! is_object( $data ) ) {
457 return array();
458 }
459 $new_data = array();
460 foreach ( $data as $key => $value ) {
461 $new_data[ $key ] = is_object( $value ) ? $this->convert_object_to_array( $value ) : $value;
462 }
463
464 return $new_data;
465 }
466
467 /**
468 * Disable SSL verification in order to prevent download update failures
469 *
470 * @param array $args
471 * @param string $url
472 * @return object $array
473 */
474 public function http_request_args( $args, $url ) {
475
476 if ( strpos( $url, 'https://' ) !== false && strpos( $url, 'edd_action=package_download' ) ) {
477 $args['sslverify'] = $this->verify_ssl();
478 }
479 return $args;
480 }
481
482 /**
483 * Calls the API and, if successfull, returns the object delivered by the API.
484 *
485 * @uses get_bloginfo()
486 * @uses wp_remote_post()
487 * @uses is_wp_error()
488 *
489 * @param string $_action The requested action.
490 * @param array $_data Parameters for the API action.
491 * @return false|object|void
492 */
493 private function api_request( $_action, $_data ) {
494 $data = array_merge( $this->api_data, $_data );
495
496 if ( $data['slug'] !== $this->slug ) {
497 return;
498 }
499
500 // Don't allow a plugin to ping itself
501 if ( trailingslashit( home_url() ) === $this->api_url ) {
502 return false;
503 }
504
505 if ( $this->request_recently_failed() ) {
506 return false;
507 }
508
509 return $this->get_version_from_remote();
510 }
511
512 /**
513 * Determines if a request has recently failed.
514 *
515 * @since 1.9.1
516 *
517 * @return bool
518 */
519 private function request_recently_failed() {
520 $failed_request_details = get_option( $this->failed_request_cache_key );
521
522 // Request has never failed.
523 if ( empty( $failed_request_details ) || ! is_numeric( $failed_request_details ) ) {
524 return false;
525 }
526
527 /*
528 * Request previously failed, but the timeout has expired.
529 * This means we're allowed to try again.
530 */
531 if ( time() > $failed_request_details ) {
532 delete_option( $this->failed_request_cache_key );
533
534 return false;
535 }
536
537 return true;
538 }
539
540 /**
541 * Logs a failed HTTP request for this API URL.
542 * We set a timestamp for 1 hour from now. This prevents future API requests from being
543 * made to this domain for 1 hour. Once the timestamp is in the past, API requests
544 * will be allowed again. This way if the site is down for some reason we don't bombard
545 * it with failed API requests.
546 *
547 * @see EDD_SL_Plugin_Updater::request_recently_failed
548 *
549 * @since 1.9.1
550 */
551 private function log_failed_request() {
552 update_option( $this->failed_request_cache_key, strtotime( '+1 hour' ) );
553 }
554
555 /**
556 * If available, show the changelog for sites in a multisite install.
557 */
558 public function show_changelog() {
559
560 if ( empty( $_REQUEST['edd_sl_action'] ) || 'view_plugin_changelog' !== $_REQUEST['edd_sl_action'] ) {
561 return;
562 }
563
564 if ( empty( $_REQUEST['plugin'] ) ) {
565 return;
566 }
567
568 if ( empty( $_REQUEST['slug'] ) || $this->slug !== $_REQUEST['slug'] ) {
569 return;
570 }
571
572 if ( ! current_user_can( 'update_plugins' ) ) {
573 wp_die( esc_html__( 'You do not have permission to install plugin updates', 'polylang' ), esc_html__( 'Error', 'polylang' ), array( 'response' => 403 ) );
574 }
575
576 $version_info = $this->get_repo_api_data();
577 if ( isset( $version_info->sections ) ) {
578 $sections = $this->convert_object_to_array( $version_info->sections );
579 if ( ! empty( $sections['changelog'] ) ) {
580 echo '<div style="background:#fff;padding:10px;">' . wp_kses_post( $sections['changelog'] ) . '</div>';
581 }
582 }
583
584 exit;
585 }
586
587 /**
588 * Gets the current version information from the remote site.
589 *
590 * @return array|false
591 */
592 private function get_version_from_remote() {
593 $api_params = array(
594 'edd_action' => 'get_version',
595 'license' => ! empty( $this->api_data['license'] ) ? $this->api_data['license'] : '',
596 'item_name' => isset( $this->api_data['item_name'] ) ? $this->api_data['item_name'] : false,
597 'item_id' => isset( $this->api_data['item_id'] ) ? $this->api_data['item_id'] : false,
598 'version' => isset( $this->api_data['version'] ) ? $this->api_data['version'] : false,
599 'slug' => $this->slug,
600 'author' => $this->api_data['author'],
601 'url' => home_url(),
602 'beta' => $this->beta,
603 'php_version' => phpversion(),
604 'wp_version' => get_bloginfo( 'version' ),
605 );
606
607 /**
608 * Filters the parameters sent in the API request.
609 *
610 * @param array $api_params The array of data sent in the request.
611 * @param array $this->api_data The array of data set up in the class constructor.
612 * @param string $this->plugin_file The full path and filename of the file.
613 */
614 $api_params = apply_filters( 'edd_sl_plugin_updater_api_params', $api_params, $this->api_data, $this->plugin_file );
615
616 $request = wp_remote_post(
617 $this->api_url,
618 array(
619 'timeout' => 15,
620 'sslverify' => $this->verify_ssl(),
621 'body' => $api_params,
622 )
623 );
624
625 if ( is_wp_error( $request ) || ( 200 !== wp_remote_retrieve_response_code( $request ) ) ) {
626 $this->log_failed_request();
627
628 return false;
629 }
630
631 $request = json_decode( wp_remote_retrieve_body( $request ) );
632
633 if ( $request && isset( $request->sections ) ) {
634 $request->sections = maybe_unserialize( $request->sections );
635 } else {
636 $request = false;
637 }
638
639 if ( $request && isset( $request->banners ) ) {
640 $request->banners = maybe_unserialize( $request->banners );
641 }
642
643 if ( $request && isset( $request->icons ) ) {
644 $request->icons = maybe_unserialize( $request->icons );
645 }
646
647 if ( ! empty( $request->sections ) ) {
648 foreach ( $request->sections as $key => $section ) {
649 $request->$key = (array) $section;
650 }
651 }
652
653 return $request;
654 }
655
656 /**
657 * Get the version info from the cache, if it exists.
658 *
659 * @param string $cache_key
660 * @return object
661 */
662 public function get_cached_version_info( $cache_key = '' ) {
663
664 if ( empty( $cache_key ) ) {
665 $cache_key = $this->get_cache_key();
666 }
667
668 $cache = get_option( $cache_key );
669
670 // Cache is expired
671 if ( empty( $cache['timeout'] ) || time() > $cache['timeout'] ) {
672 return false;
673 }
674
675 // We need to turn the icons into an array, thanks to WP Core forcing these into an object at some point.
676 $cache['value'] = json_decode( $cache['value'] );
677 if ( ! empty( $cache['value']->icons ) ) {
678 $cache['value']->icons = (array) $cache['value']->icons;
679 }
680
681 return $cache['value'];
682 }
683
684 /**
685 * Adds the plugin version information to the database.
686 *
687 * @param string $value
688 * @param string $cache_key
689 */
690 public function set_version_info_cache( $value = '', $cache_key = '' ) {
691
692 if ( empty( $cache_key ) ) {
693 $cache_key = $this->get_cache_key();
694 }
695
696 $data = array(
697 'timeout' => strtotime( '+3 hours', time() ),
698 'value' => wp_json_encode( $value ),
699 );
700
701 update_option( $cache_key, $data, 'no' );
702
703 // Delete the duplicate option
704 delete_option( 'edd_api_request_' . md5( serialize( $this->slug . $this->api_data['license'] . $this->beta ) ) );
705 }
706
707 /**
708 * Returns if the SSL of the store should be verified.
709 *
710 * @since 1.6.13
711 * @return bool
712 */
713 private function verify_ssl() {
714 return (bool) apply_filters( 'edd_sl_api_request_verify_ssl', true, $this );
715 }
716
717 /**
718 * Gets the unique key (option name) for a plugin.
719 *
720 * @since 1.9.0
721 * @return string
722 */
723 private function get_cache_key() {
724 $string = $this->slug . $this->api_data['license'] . $this->beta;
725
726 return 'edd_sl_' . md5( serialize( $string ) );
727 }
728 }
729