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

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