PluginProbe
YayMail – WooCommerce Email Customizer / 3.3.9
YayMail – WooCommerce Email Customizer v3.3.9
4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 trunk 1.9.6 2.1.4 2.1.5 3.2.2 3.2.6 3.2.7.1 3.2.8.1 3.2.9 3.3 3.3.1 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9 3.4 3.4.1 All 57 releases
yaymail / includes / License / EDD_SL_Plugin_Updater.php

EDD_SL_Plugin_Updater.php in YayMail – WooCommerce Email Customizer 3.3.9, at includes/License/EDD_SL_Plugin_Updater.php

641 lines 18.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace YayMail\License;
3
4 // Exit if accessed directly
5 if ( ! defined( 'ABSPATH' ) ) {
6 exit;
7 }
8
9 /**
10 * Allows plugins to use their own update API.
11 *
12 * @author Easy Digital Downloads
13 * @version 1.8.0
14 */
15 class EDD_SL_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 // add_action( 'load-plugins.php', array( $this, 'remove_default_after_row' ), 100 );
78 add_action( 'after_plugin_row_' . $this->name, array( $this, 'show_update_notification' ), 11, 2 );
79 add_action( 'admin_init', array( $this, 'show_changelog' ) );
80
81 }
82
83 public function remove_default_after_row() {
84 remove_action( 'after_plugin_row_' . $this->name, 'wp_plugin_update_row', 10, 2 );
85 }
86
87 /**
88 * Check for Updates at the defined API endpoint and modify the update array.
89 *
90 * This function dives into the update API just when WordPress creates its update array,
91 * then adds a custom API call and injects the custom plugin data retrieved from the API.
92 * It is reassembled from parts of the native WordPress plugin update code.
93 * See wp-includes/update.php line 121 for the original wp_update_plugins() function.
94 *
95 * @uses api_request()
96 *
97 * @param array $_transient_data Update array build by WordPress.
98 * @return array Modified update array with custom plugin data.
99 */
100 public function check_update( $_transient_data ) {
101
102 global $pagenow;
103
104 if ( ! is_object( $_transient_data ) ) {
105 $_transient_data = new \stdClass();
106 }
107
108 if ( 'plugins.php' == $pagenow ) {
109 return $_transient_data;
110 }
111
112 if ( ! empty( $_transient_data->response ) && ! empty( $_transient_data->response[ $this->name ] ) && false === $this->wp_override ) {
113 return $_transient_data;
114 }
115
116 $current = $this->get_repo_api_data();
117 if ( false !== $current && is_object( $current ) && isset( $current->new_version ) ) {
118 if ( version_compare( $this->version, $current->new_version, '<' ) ) {
119 $_transient_data->response[ $this->name ] = $current;
120 } else {
121 // Populating the no_update information is required to support auto-updates in WordPress 5.5.
122 $_transient_data->no_update[ $this->name ] = $current;
123 }
124 }
125 $_transient_data->last_checked = time();
126 $_transient_data->checked[ $this->name ] = $this->version;
127
128 return $_transient_data;
129 }
130
131 /**
132 * Get repo API data from store.
133 * Save to cache.
134 *
135 * @return \stdClass
136 */
137 public function get_repo_api_data() {
138 $version_info = $this->get_cached_version_info();
139
140 if ( false === $version_info ) {
141 $version_info = $this->api_request(
142 'plugin_latest_version',
143 array(
144 'slug' => $this->slug,
145 'beta' => $this->beta,
146 )
147 );
148 if ( ! $version_info ) {
149 return false;
150 }
151
152 // This is required for your plugin to support auto-updates in WordPress 5.5.
153 $version_info->plugin = $this->name;
154 $version_info->id = $this->name;
155
156 $this->set_version_info_cache( $version_info );
157 }
158
159 return $version_info;
160 }
161
162 public function yaycommerce_update_transient() {
163 if ( is_network_admin() ) {
164 return;
165 }
166
167 if ( ! current_user_can( 'update_plugins' ) ) {
168 return;
169 }
170
171 // Remove our filter on the site transient
172 remove_filter( 'pre_set_site_transient_update_plugins', array( $this, 'check_update' ), 10 );
173
174 $update_cache = get_site_transient( 'update_plugins' );
175
176 $update_cache = is_object( $update_cache ) ? $update_cache : new \stdClass();
177
178 if ( empty( $update_cache->response ) || empty( $update_cache->response[ $this->name ] ) ) {
179
180 $version_info = $this->get_repo_api_data();
181
182 if ( false === $version_info ) {
183 $version_info = $this->api_request(
184 'plugin_latest_version',
185 array(
186 'slug' => $this->slug,
187 'beta' => $this->beta,
188 )
189 );
190
191 // Since we disabled our filter for the transient, we aren't running our object conversion on banners, sections, or icons. Do this now:
192 if ( isset( $version_info->banners ) && ! is_array( $version_info->banners ) ) {
193 $version_info->banners = $this->convert_object_to_array( $version_info->banners );
194 }
195
196 if ( isset( $version_info->sections ) && ! is_array( $version_info->sections ) ) {
197 $version_info->sections = $this->convert_object_to_array( $version_info->sections );
198 }
199
200 if ( isset( $version_info->icons ) && ! is_array( $version_info->icons ) ) {
201 $version_info->icons = $this->convert_object_to_array( $version_info->icons );
202 }
203
204 if ( isset( $version_info->contributors ) && ! is_array( $version_info->contributors ) ) {
205 $version_info->contributors = $this->convert_object_to_array( $version_info->contributors );
206 }
207
208 $this->set_version_info_cache( $version_info );
209 }
210
211 if ( ! is_object( $version_info ) ) {
212 return;
213 }
214
215 $has_update = false;
216 if ( version_compare( $this->version, $version_info->new_version, '<' ) ) {
217 $update_cache->response[ $this->name ] = $version_info;
218 $has_update = true;
219 } else {
220 $update_cache->no_update[ $this->name ] = $version_info;
221 }
222
223 $update_cache->last_checked = time();
224 $update_cache->checked[ $this->name ] = $this->version;
225
226 if ( $has_update ) {
227 set_site_transient( 'update_plugins', $update_cache );
228 }
229 } else {
230
231 $version_info = $update_cache->response[ $this->name ];
232
233 }
234 }
235
236 /**
237 * show update nofication row -- needed for multisite subsites, because WP won't tell you otherwise!
238 *
239 * @param string $file
240 * @param array $plugin
241 */
242 public function show_update_notification( $file, $plugin ) {
243 $output = '';
244
245 if ( $this->name != $file ) {
246 return $output;
247 }
248
249 $this->yaycommerce_update_transient();
250
251 $update_cache = get_site_transient( 'update_plugins' );
252 $update_cache = is_object( $update_cache ) ? $update_cache : new \stdClass();
253 $version_info = isset( $update_cache->response[ $this->name ] ) ? $update_cache->response[ $this->name ] : '';
254
255 // Restore our filter
256 add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'check_update' ) );
257
258 if ( ! empty( $update_cache->response[ $this->name ] ) && version_compare( $this->version, $version_info->new_version, '<' ) && empty( $version_info->download_link ) ) {
259
260 $output .= '<tr class="plugin-update-tr active" id="' . esc_attr( $this->slug ) . '-update" data-slug="' . esc_attr( $this->slug ) . '" data-plugin="' . esc_attr( $this->slug ) . '/' . esc_attr( $file ) . '">';
261 $output .= '<td colspan="4" class="plugin-update colspanchange">';
262 $output .= '<div class="update-message notice inline notice-warning notice-alt">';
263
264 $changelog_link = self_admin_url( 'plugin-install.php?tab=plugin-information&plugin=' . $this->slug . '&section=changelog&TB_iframe=true&width=600&height=800' );
265
266 $output .= sprintf(
267 '<p>%s <a target="_blank" class="thickbox" href="%s">%s</a></p>',
268 esc_html( sprintf( __( 'There is a new version of %1$s available. %2$sView version %3$s details%4$s.', 'easy-digital-downloads' ), esc_html( $plugin['Name'] ), '<a target="_blank" class="thickbox" href="' . esc_url( $changelog_link ) . '">', esc_html( $version_info->new_version ), '</a>' ) )
269 );
270 $output .= '</div></td></tr>';
271
272 }
273
274 do_action( "in_plugin_update_message-{$file}", $plugin, $version_info );
275
276 echo wp_kses_post( $output );
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 ( 'plugin_information' != $_action ) {
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 } else {
328 $_data = $edd_api_request_transient;
329 }
330
331 // Convert sections into an associative array, since we're getting an object, but Core expects an array.
332 if ( isset( $_data->sections ) && ! is_array( $_data->sections ) ) {
333 $_data->sections = $this->convert_object_to_array( $_data->sections );
334 }
335
336 // Convert banners into an associative array, since we're getting an object, but Core expects an array.
337 if ( isset( $_data->banners ) && ! is_array( $_data->banners ) ) {
338 $_data->banners = $this->convert_object_to_array( $_data->banners );
339 }
340
341 // Convert icons into an associative array, since we're getting an object, but Core expects an array.
342 if ( isset( $_data->icons ) && ! is_array( $_data->icons ) ) {
343 $_data->icons = $this->convert_object_to_array( $_data->icons );
344 }
345
346 // Convert contributors into an associative array, since we're getting an object, but Core expects an array.
347 if ( isset( $_data->contributors ) && ! is_array( $_data->contributors ) ) {
348 $_data->contributors = $this->convert_object_to_array( $_data->contributors );
349 }
350
351 if ( ! isset( $_data->plugin ) ) {
352 $_data->plugin = $this->name;
353 }
354
355 return $_data;
356 }
357
358 /**
359 * Convert some objects to arrays when injecting data into the update API
360 *
361 * Some data like sections, banners, and icons are expected to be an associative array, however due to the JSON
362 * decoding, they are objects. This method allows us to pass in the object and return an associative array.
363 *
364 * @since 3.6.5
365 *
366 * @param stdClass $data
367 *
368 * @return array
369 */
370 private function convert_object_to_array( $data ) {
371 $new_data = array();
372 foreach ( $data as $key => $value ) {
373 $new_data[ $key ] = is_object( $value ) ? $this->convert_object_to_array( $value ) : $value;
374 }
375
376 return $new_data;
377 }
378
379 /**
380 * Disable SSL verification in order to prevent download update failures
381 *
382 * @param array $args
383 * @param string $url
384 * @return object $array
385 */
386 public function http_request_args( $args, $url ) {
387
388 $verify_ssl = $this->verify_ssl();
389 if ( strpos( $url, 'https://' ) !== false && strpos( $url, 'edd_action=package_download' ) ) {
390 $args['sslverify'] = $verify_ssl;
391 }
392 return $args;
393
394 }
395
396 /**
397 * Calls the API and, if successfull, returns the object delivered by the API.
398 *
399 * @uses get_bloginfo()
400 * @uses wp_remote_post()
401 * @uses is_wp_error()
402 *
403 * @param string $_action The requested action.
404 * @param array $_data Parameters for the API action.
405 * @return false|object
406 */
407 private function api_request( $_action, $_data ) {
408
409 global $wp_version, $edd_plugin_url_available;
410
411 $verify_ssl = $this->verify_ssl();
412
413 // Do a quick status check on this domain if we haven't already checked it.
414 $store_hash = md5( $this->api_url );
415 if ( ! is_array( $edd_plugin_url_available ) || ! isset( $edd_plugin_url_available[ $store_hash ] ) ) {
416 $test_url_parts = parse_url( $this->api_url );
417
418 $scheme = ! empty( $test_url_parts['scheme'] ) ? $test_url_parts['scheme'] : 'http';
419 $host = ! empty( $test_url_parts['host'] ) ? $test_url_parts['host'] : '';
420 $port = ! empty( $test_url_parts['port'] ) ? ':' . $test_url_parts['port'] : '';
421
422 if ( empty( $host ) ) {
423 $edd_plugin_url_available[ $store_hash ] = false;
424 } else {
425 $test_url = $scheme . '://' . $host . $port;
426 $response = wp_remote_get(
427 $test_url,
428 array(
429 'timeout' => $this->health_check_timeout,
430 'sslverify' => $verify_ssl,
431 )
432 );
433 $edd_plugin_url_available[ $store_hash ] = is_wp_error( $response ) ? false : true;
434 }
435 }
436
437 if ( false === $edd_plugin_url_available[ $store_hash ] ) {
438 return false;
439 }
440
441 $data = array_merge( $this->api_data, $_data );
442
443 if ( $data['slug'] != $this->slug ) {
444 return false;
445 }
446
447 if ( trailingslashit( home_url() ) == $this->api_url ) {
448 return false; // Don't allow a plugin to ping itself
449 }
450
451 $api_params = array(
452 'edd_action' => 'get_version',
453 'license' => ! empty( $data['license'] ) ? $data['license'] : '',
454 'item_name' => isset( $data['item_name'] ) ? $data['item_name'] : false,
455 'item_id' => isset( $data['item_id'] ) ? $data['item_id'] : false,
456 'version' => isset( $data['version'] ) ? $data['version'] : false,
457 'slug' => $data['slug'],
458 'author' => $data['author'],
459 'url' => home_url(),
460 'beta' => ! empty( $data['beta'] ),
461 );
462
463 $request = wp_remote_post(
464 $this->api_url,
465 array(
466 'timeout' => 15,
467 'sslverify' => $verify_ssl,
468 'body' => $api_params,
469 )
470 );
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( esc_html__( 'You do not have permission to install plugin updates', 'easy-digital-downloads' ), esc_html__( 'Error', 'easy-digital-downloads' ), 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(
539 $this->api_url,
540 array(
541 'timeout' => 15,
542 'sslverify' => $verify_ssl,
543 'body' => $api_params,
544 )
545 );
546
547 if ( ! is_wp_error( $request ) ) {
548 $version_info = json_decode( wp_remote_retrieve_body( $request ) );
549 }
550
551 if ( ! empty( $version_info ) && isset( $version_info->sections ) ) {
552 $version_info->sections = maybe_unserialize( $version_info->sections );
553 } else {
554 $version_info = false;
555 }
556
557 if ( ! empty( $version_info ) ) {
558 foreach ( $version_info->sections as $key => $section ) {
559 $version_info->$key = (array) $section;
560 }
561 }
562
563 $this->set_version_info_cache( $version_info );
564
565 // Delete the unneeded option
566 delete_option( md5( 'edd_plugin_' . sanitize_key( $_REQUEST['plugin'] ) . '_' . $this->beta . '_version_info' ) );
567 }
568
569 if ( isset( $version_info->sections ) ) {
570 $sections = $this->convert_object_to_array( $version_info->sections );
571 if ( ! empty( $sections['changelog'] ) ) {
572 echo '<div style="background:#fff;padding:10px;">' . wp_kses_post( $sections['changelog'] ) . '</div>';
573 }
574 }
575
576 exit;
577 }
578
579 /**
580 * Gets the plugin's cached version information from the database.
581 *
582 * @param string $cache_key
583 * @return boolean|string
584 */
585 public function get_cached_version_info( $cache_key = '' ) {
586
587 if ( empty( $cache_key ) ) {
588 $cache_key = $this->cache_key;
589 }
590
591 $cache = get_option( $cache_key );
592
593 if ( empty( $cache['timeout'] ) || time() > $cache['timeout'] ) {
594 return false; // Cache is expired
595 }
596
597 // We need to turn the icons into an array, thanks to WP Core forcing these into an object at some point.
598 $cache['value'] = json_decode( $cache['value'] );
599 if ( ! empty( $cache['value']->icons ) ) {
600 $cache['value']->icons = (array) $cache['value']->icons;
601 }
602
603 return $cache['value'];
604
605 }
606
607 /**
608 * Adds the plugin version information to the database.
609 *
610 * @param string $value
611 * @param string $cache_key
612 */
613 public function set_version_info_cache( $value = '', $cache_key = '' ) {
614
615 if ( empty( $cache_key ) ) {
616 $cache_key = $this->cache_key;
617 }
618
619 $data = array(
620 'timeout' => strtotime( '+3 hours', time() ),
621 'value' => json_encode( $value ),
622 );
623
624 update_option( $cache_key, $data, 'no' );
625
626 // Delete the duplicate option
627 delete_option( 'edd_api_request_' . md5( serialize( $this->slug . $this->api_data['license'] . $this->beta ) ) );
628 }
629
630 /**
631 * Returns if the SSL of the store should be verified.
632 *
633 * @since 1.6.13
634 * @return bool
635 */
636 private function verify_ssl() {
637 return (bool) apply_filters( 'edd_sl_api_request_verify_ssl', true, $this );
638 }
639
640 }
641