PluginProbe
Polylang / 2.9
Polylang v2.9
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.9, at install/plugin-updater.php

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