PluginProbe
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent / 3.4.7
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent v3.4.7
3.5.3 3.5.2 3.5.1 3.4.9 3.5.0 3.4.8 3.4.7 trunk 2.3.1 3.3.6 3.3.7 3.3.8 3.3.9 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.6
supportcandy / includes / EDD_SL_Plugin_Updater.php

EDD_SL_Plugin_Updater.php in SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent 3.4.7, at includes/EDD_SL_Plugin_Updater.php

643 lines 18.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php // phpcs:ignorefile
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 *
11 * @author Easy Digital Downloads
12 * @version 1.9.1
13 */
14 class WPSC_EDD_SL_Plugin_Updater {
15
16 private $api_url = '';
17 private $api_data = array();
18 private $plugin_file = '';
19 private $name = '';
20 private $slug = '';
21 private $version = '';
22 private $wp_override = false;
23 private $beta = false;
24 private $failed_request_cache_key;
25
26 /**
27 * Class constructor.
28 *
29 * @uses plugin_basename()
30 * @uses hook()
31 *
32 * @param string $_api_url The URL pointing to the custom API endpoint.
33 * @param string $_plugin_file Path to the plugin file.
34 * @param array $_api_data Optional data to send with API calls.
35 */
36 public function __construct( $_api_url, $_plugin_file, $_api_data = null ) {
37
38 global $edd_plugin_data;
39
40 $this->api_url = trailingslashit( $_api_url );
41 $this->api_data = $_api_data;
42 $this->plugin_file = $_plugin_file;
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->failed_request_cache_key = 'edd_sl_failed_http_' . md5( $this->api_url );
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 add_action( 'after_plugin_row', array( $this, 'show_update_notification' ), 10, 2 );
78 add_action( 'admin_init', array( $this, 'show_changelog' ) );
79
80 }
81
82 /**
83 * Check for Updates at the defined API endpoint and modify the update array.
84 *
85 * This function dives into the update API just when WordPress creates its update array,
86 * then adds a custom API call and injects the custom plugin data retrieved from the API.
87 * It is reassembled from parts of the native WordPress plugin update code.
88 * See wp-includes/update.php line 121 for the original wp_update_plugins() function.
89 *
90 * @uses api_request()
91 *
92 * @param array $_transient_data Update array build by WordPress.
93 * @return array Modified update array with custom plugin data.
94 */
95 public function check_update( $_transient_data ) {
96
97 global $pagenow;
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_repo_api_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
147 $this->set_version_info_cache( $version_info );
148 }
149
150 return $version_info;
151 }
152
153 /**
154 * Show the update notification on multisite subsites.
155 *
156 * @param string $file
157 * @param array $plugin
158 */
159 public function show_update_notification( $file, $plugin ) {
160
161 // Return early if in the network admin, or if this is not a multisite install.
162 if ( is_network_admin() || ! is_multisite() ) {
163 return;
164 }
165
166 // Allow single site admins to see that an update is available.
167 if ( ! current_user_can( 'activate_plugins' ) ) {
168 return;
169 }
170
171 if ( $this->name !== $file ) {
172 return;
173 }
174
175 // Do not print any message if update does not exist.
176 $update_cache = get_site_transient( 'update_plugins' );
177
178 if ( ! isset( $update_cache->response[ $this->name ] ) ) {
179 if ( ! is_object( $update_cache ) ) {
180 $update_cache = new stdClass();
181 }
182 $update_cache->response[ $this->name ] = $this->get_repo_api_data();
183 }
184
185 // 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.
186 if ( empty( $update_cache->response[ $this->name ] ) || version_compare( $this->version, $update_cache->response[ $this->name ]->new_version, '>=' ) ) {
187 return;
188 }
189
190 printf(
191 '<tr class="plugin-update-tr %3$s" id="%1$s-update" data-slug="%1$s" data-plugin="%2$s">',
192 $this->slug,
193 $file,
194 in_array( $this->name, $this->get_active_plugins(), true ) ? 'active' : 'inactive'
195 );
196
197 echo '<td colspan="3" class="plugin-update colspanchange">';
198 echo '<div class="update-message notice inline notice-warning notice-alt"><p>';
199
200 $changelog_link = '';
201 if ( ! empty( $update_cache->response[ $this->name ]->sections->changelog ) ) {
202 $changelog_link = add_query_arg(
203 array(
204 'edd_sl_action' => 'view_plugin_changelog',
205 'plugin' => urlencode( $this->name ),
206 'slug' => urlencode( $this->slug ),
207 'TB_iframe' => 'true',
208 'width' => 77,
209 'height' => 911,
210 ),
211 self_admin_url( 'index.php' )
212 );
213 }
214 $update_link = add_query_arg(
215 array(
216 'action' => 'upgrade-plugin',
217 'plugin' => urlencode( $this->name ),
218 ),
219 self_admin_url( 'update.php' )
220 );
221
222 printf(
223 esc_html( wpsc__( 'There is a new version of %1$s available.', 'easy-digital-downloads' ) ),
224 esc_html( $plugin['Name'] )
225 );
226
227 if ( ! current_user_can( 'update_plugins' ) ) {
228 echo ' ';
229 echo esc_html( wpsc__( 'Contact your network administrator to install the update.', 'easy-digital-downloads' ) );
230 } elseif ( empty( $update_cache->response[ $this->name ]->package ) && ! empty( $changelog_link ) ) {
231 echo ' ';
232 printf(
233 /* translators: 1. opening anchor tag, do not translate 2. the new plugin version 3. closing anchor tag, do not translate. */
234 wpsc__( '%1$sView version %2$s details%3$s.', 'easy-digital-downloads' ),
235 '<a target="_blank" class="thickbox open-plugin-details-modal" href="' . esc_url( $changelog_link ) . '">',
236 esc_html( $update_cache->response[ $this->name ]->new_version ),
237 '</a>'
238 );
239 } elseif ( ! empty( $changelog_link ) ) {
240 echo ' ';
241 printf(
242 wpsc__( '%1$sView version %2$s details%3$s or %4$supdate now%5$s.', 'easy-digital-downloads' ),
243 '<a target="_blank" class="thickbox open-plugin-details-modal" href="' . esc_url( $changelog_link ) . '">',
244 esc_html( $update_cache->response[ $this->name ]->new_version ),
245 '</a>',
246 '<a target="_blank" class="update-link" href="' . esc_url( wp_nonce_url( $update_link, 'upgrade-plugin_' . $file ) ) . '">',
247 '</a>'
248 );
249 } else {
250 printf(
251 ' %1$s%2$s%3$s',
252 '<a target="_blank" class="update-link" href="' . esc_url( wp_nonce_url( $update_link, 'upgrade-plugin_' . $file ) ) . '">',
253 esc_html( wpsc__( 'Update now.', 'easy-digital-downloads' ) ),
254 '</a>'
255 );
256 }
257
258 do_action( "in_plugin_update_message-{$file}", $plugin, $plugin );
259
260 echo '</p></div></td></tr>';
261 }
262
263 /**
264 * Gets the plugins active in a multisite network.
265 *
266 * @return array
267 */
268 private function get_active_plugins() {
269 $active_plugins = (array) get_option( 'active_plugins' );
270 $active_network_plugins = (array) get_site_option( 'active_sitewide_plugins' );
271
272 return array_merge( $active_plugins, array_keys( $active_network_plugins ) );
273 }
274
275 /**
276 * Updates information on the "View version x.x details" page with custom data.
277 *
278 * @uses api_request()
279 *
280 * @param mixed $_data
281 * @param string $_action
282 * @param object $_args
283 * @return object $_data
284 */
285 public function plugins_api_filter( $_data, $_action = '', $_args = null ) {
286
287 if ( 'plugin_information' !== $_action ) {
288
289 return $_data;
290
291 }
292
293 if ( ! isset( $_args->slug ) || ( $_args->slug !== $this->slug ) ) {
294
295 return $_data;
296
297 }
298
299 $to_send = array(
300 'slug' => $this->slug,
301 'is_ssl' => is_ssl(),
302 'fields' => array(
303 'banners' => array(),
304 'reviews' => false,
305 'icons' => array(),
306 ),
307 );
308
309 // Get the transient where we store the api request for this plugin for 24 hours
310 $edd_api_request_transient = $this->get_cached_version_info();
311
312 //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.
313 if ( empty( $edd_api_request_transient ) ) {
314
315 $api_response = $this->api_request( 'plugin_information', $to_send );
316
317 // Expires in 3 hours
318 $this->set_version_info_cache( $api_response );
319
320 if ( false !== $api_response ) {
321 $_data = $api_response;
322 }
323 } else {
324 $_data = $edd_api_request_transient;
325 }
326
327 // Convert sections into an associative array, since we're getting an object, but Core expects an array.
328 if ( isset( $_data->sections ) && ! is_array( $_data->sections ) ) {
329 $_data->sections = $this->convert_object_to_array( $_data->sections );
330 }
331
332 // Convert banners into an associative array, since we're getting an object, but Core expects an array.
333 if ( isset( $_data->banners ) && ! is_array( $_data->banners ) ) {
334 $_data->banners = $this->convert_object_to_array( $_data->banners );
335 }
336
337 // Convert icons into an associative array, since we're getting an object, but Core expects an array.
338 if ( isset( $_data->icons ) && ! is_array( $_data->icons ) ) {
339 $_data->icons = $this->convert_object_to_array( $_data->icons );
340 }
341
342 // Convert contributors into an associative array, since we're getting an object, but Core expects an array.
343 if ( isset( $_data->contributors ) && ! is_array( $_data->contributors ) ) {
344 $_data->contributors = $this->convert_object_to_array( $_data->contributors );
345 }
346
347 if ( ! isset( $_data->plugin ) ) {
348 $_data->plugin = $this->name;
349 }
350
351 return $_data;
352 }
353
354 /**
355 * Convert some objects to arrays when injecting data into the update API
356 *
357 * Some data like sections, banners, and icons are expected to be an associative array, however due to the JSON
358 * decoding, they are objects. This method allows us to pass in the object and return an associative array.
359 *
360 * @since 3.6.5
361 *
362 * @param stdClass $data
363 *
364 * @return array
365 */
366 private function convert_object_to_array( $data ) {
367 if ( ! is_array( $data ) && ! is_object( $data ) ) {
368 return array();
369 }
370 $new_data = array();
371 foreach ( $data as $key => $value ) {
372 $new_data[ $key ] = is_object( $value ) ? $this->convert_object_to_array( $value ) : $value;
373 }
374
375 return $new_data;
376 }
377
378 /**
379 * Disable SSL verification in order to prevent download update failures
380 *
381 * @param array $args
382 * @param string $url
383 * @return object $array
384 */
385 public function http_request_args( $args, $url ) {
386
387 if ( strpos( $url, 'https://' ) !== false && strpos( $url, 'edd_action=package_download' ) ) {
388 $args['sslverify'] = $this->verify_ssl();
389 }
390 return $args;
391
392 }
393
394 /**
395 * Calls the API and, if successfull, returns the object delivered by the API.
396 *
397 * @uses get_bloginfo()
398 * @uses wp_remote_post()
399 * @uses is_wp_error()
400 *
401 * @param string $_action The requested action.
402 * @param array $_data Parameters for the API action.
403 * @return false|object|void
404 */
405 private function api_request( $_action, $_data ) {
406 $data = array_merge( $this->api_data, $_data );
407
408 if ( $data['slug'] !== $this->slug ) {
409 return;
410 }
411
412 // Don't allow a plugin to ping itself
413 if ( trailingslashit( home_url() ) === $this->api_url ) {
414 return false;
415 }
416
417 if ( $this->request_recently_failed() ) {
418 return false;
419 }
420
421 return $this->get_version_from_remote();
422 }
423
424 /**
425 * Determines if a request has recently failed.
426 *
427 * @since 1.9.1
428 *
429 * @return bool
430 */
431 private function request_recently_failed() {
432 $failed_request_details = get_option( $this->failed_request_cache_key );
433
434 // Request has never failed.
435 if ( empty( $failed_request_details ) || ! is_numeric( $failed_request_details ) ) {
436 return false;
437 }
438
439 /*
440 * Request previously failed, but the timeout has expired.
441 * This means we're allowed to try again.
442 */
443 if ( time() > $failed_request_details ) {
444 delete_option( $this->failed_request_cache_key );
445
446 return false;
447 }
448
449 return true;
450 }
451
452 /**
453 * Logs a failed HTTP request for this API URL.
454 * We set a timestamp for 1 hour from now. This prevents future API requests from being
455 * made to this domain for 1 hour. Once the timestamp is in the past, API requests
456 * will be allowed again. This way if the site is down for some reason we don't bombard
457 * it with failed API requests.
458 *
459 * @see WPSC_EDD_SL_Plugin_Updater::request_recently_failed
460 *
461 * @since 1.9.1
462 */
463 private function log_failed_request() {
464 update_option( $this->failed_request_cache_key, strtotime( '+1 hour' ) );
465 }
466
467 /**
468 * If available, show the changelog for sites in a multisite install.
469 */
470 public function show_changelog() {
471
472 if ( empty( $_REQUEST['edd_sl_action'] ) || 'view_plugin_changelog' !== $_REQUEST['edd_sl_action'] ) {
473 return;
474 }
475
476 if ( empty( $_REQUEST['plugin'] ) ) {
477 return;
478 }
479
480 if ( empty( $_REQUEST['slug'] ) || $this->slug !== $_REQUEST['slug'] ) {
481 return;
482 }
483
484 if ( ! current_user_can( 'update_plugins' ) ) {
485 wp_die( esc_html( wpsc__( 'You do not have permission to install plugin updates', 'easy-digital-downloads' ) ), esc_html( wpsc__( 'Error', 'easy-digital-downloads' ) ), array( 'response' => 403 ) );
486 }
487
488 $version_info = $this->get_repo_api_data();
489 if ( isset( $version_info->sections ) ) {
490 $sections = $this->convert_object_to_array( $version_info->sections );
491 if ( ! empty( $sections['changelog'] ) ) {
492 echo '<div style="background:#fff;padding:10px;">' . wp_kses_post( $sections['changelog'] ) . '</div>';
493 }
494 }
495
496 exit;
497 }
498
499 /**
500 * Gets the current version information from the remote site.
501 *
502 * @return array|false
503 */
504 private function get_version_from_remote() {
505 $api_params = array(
506 'edd_action' => 'get_version',
507 'license' => ! empty( $this->api_data['license'] ) ? $this->api_data['license'] : '',
508 'item_name' => isset( $this->api_data['item_name'] ) ? $this->api_data['item_name'] : false,
509 'item_id' => isset( $this->api_data['item_id'] ) ? $this->api_data['item_id'] : false,
510 'version' => isset( $this->api_data['version'] ) ? $this->api_data['version'] : false,
511 'slug' => $this->slug,
512 'author' => $this->api_data['author'],
513 'url' => home_url(),
514 'beta' => $this->beta,
515 'php_version' => phpversion(),
516 'wp_version' => get_bloginfo( 'version' ),
517 );
518
519 /**
520 * Filters the parameters sent in the API request.
521 *
522 * @param array $api_params The array of data sent in the request.
523 * @param array $this->api_data The array of data set up in the class constructor.
524 * @param string $this->plugin_file The full path and filename of the file.
525 */
526 $api_params = apply_filters( 'edd_sl_plugin_updater_api_params', $api_params, $this->api_data, $this->plugin_file );
527
528 $request = wp_remote_post(
529 $this->api_url,
530 array(
531 'timeout' => 15,
532 'sslverify' => $this->verify_ssl(),
533 'body' => $api_params,
534 )
535 );
536
537 if ( is_wp_error( $request ) || ( 200 !== wp_remote_retrieve_response_code( $request ) ) ) {
538 $this->log_failed_request();
539
540 return false;
541 }
542
543 $request = json_decode( wp_remote_retrieve_body( $request ) );
544
545 if ( $request && isset( $request->sections ) ) {
546 $request->sections = maybe_unserialize( $request->sections );
547 } else {
548 $request = false;
549 }
550
551 if ( $request && isset( $request->banners ) ) {
552 $request->banners = maybe_unserialize( $request->banners );
553 }
554
555 if ( $request && isset( $request->icons ) ) {
556 $request->icons = maybe_unserialize( $request->icons );
557 }
558
559 if ( ! empty( $request->sections ) ) {
560 foreach ( $request->sections as $key => $section ) {
561 $request->$key = (array) $section;
562 }
563 }
564
565 return $request;
566 }
567
568 /**
569 * Get the version info from the cache, if it exists.
570 *
571 * @param string $cache_key
572 * @return object
573 */
574 public function get_cached_version_info( $cache_key = '' ) {
575
576 if ( empty( $cache_key ) ) {
577 $cache_key = $this->get_cache_key();
578 }
579
580 $cache = get_option( $cache_key );
581
582 // Cache is expired
583 if ( empty( $cache['timeout'] ) || time() > $cache['timeout'] ) {
584 return false;
585 }
586
587 // We need to turn the icons into an array, thanks to WP Core forcing these into an object at some point.
588 $cache['value'] = json_decode( $cache['value'] );
589 if ( ! empty( $cache['value']->icons ) ) {
590 $cache['value']->icons = (array) $cache['value']->icons;
591 }
592
593 return $cache['value'];
594
595 }
596
597 /**
598 * Adds the plugin version information to the database.
599 *
600 * @param string $value
601 * @param string $cache_key
602 */
603 public function set_version_info_cache( $value = '', $cache_key = '' ) {
604
605 if ( empty( $cache_key ) ) {
606 $cache_key = $this->get_cache_key();
607 }
608
609 $data = array(
610 'timeout' => strtotime( '+3 hours', time() ),
611 'value' => wp_json_encode( $value ),
612 );
613
614 update_option( $cache_key, $data, 'no' );
615
616 // Delete the duplicate option
617 delete_option( 'edd_api_request_' . md5( serialize( $this->slug . $this->api_data['license'] . $this->beta ) ) );
618 }
619
620 /**
621 * Returns if the SSL of the store should be verified.
622 *
623 * @since 1.6.13
624 * @return bool
625 */
626 private function verify_ssl() {
627 return (bool) apply_filters( 'edd_sl_api_request_verify_ssl', true, $this );
628 }
629
630 /**
631 * Gets the unique key (option name) for a plugin.
632 *
633 * @since 1.9.0
634 * @return string
635 */
636 private function get_cache_key() {
637 $string = $this->slug . $this->api_data['license'] . $this->beta;
638
639 return 'edd_sl_' . md5( serialize( $string ) );
640 }
641
642 }
643