PluginProbe
YayMail – WooCommerce Email Customizer / 3.2.2
YayMail – WooCommerce Email Customizer v3.2.2
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 3.4.2 3.4.3 All 55 releases
yaymail / includes / License / EDD_SL_Plugin_Updater.php

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

660 lines 19.6 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
244 if ( $this->name != $file ) {
245 return;
246 }
247 $this->yaycommerce_update_transient();
248
249 $update_cache = get_site_transient( 'update_plugins' );
250
251 $update_cache = is_object( $update_cache ) ? $update_cache : new \stdClass();
252
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, '<' ) ) {
259
260 // build a plugin list row, with update notification
261 $wp_list_table = _get_list_table( 'WP_Plugins_List_Table' );
262 // <tr class="plugin-update-tr"><td colspan="' . $wp_list_table->get_column_count() . '" class="plugin-update colspanchange">
263 echo '<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 ) . '">';
264 echo '<td colspan="4" class="plugin-update colspanchange">';
265 echo '<div class="update-message notice inline notice-warning notice-alt">';
266
267 $changelog_link = self_admin_url( 'plugin-install.php?tab=plugin-information&plugin=' . $this->slug . '&section=changelog&TB_iframe=true&width=600&height=800' );
268 echo '<p>';
269 if ( empty( $version_info->download_link ) ) {
270 printf(
271 // translators: %s: param
272 esc_html__( 'There is a new version of %1$s available. %2$sView version %3$s details%4$s.', 'easy-digital-downloads' ),
273 esc_html( $plugin['Name'] ),
274 '<a target="_blank" class="thickbox" href="' . esc_url( $changelog_link ) . '">',
275 esc_html( $version_info->new_version ),
276 '</a>'
277 );
278 } else {
279 printf(
280 // translators: %s: param
281 esc_html__( 'There is a new version of %1$s available. %2$sView version %3$s details%4$s or %5$supdate now%6$s.', 'easy-digital-downloads' ),
282 esc_html( $plugin['Name'] ),
283 '<a target="_blank" class="thickbox" href="' . esc_url( $changelog_link ) . '">',
284 esc_html( $version_info->new_version ),
285 '</a>',
286 '<a href="' . esc_url( wp_nonce_url( self_admin_url( 'update.php?action=upgrade-plugin&plugin=' ) . $this->name, 'upgrade-plugin_' . $this->name ) ) . '">',
287 '</a>'
288 );
289 }
290
291 echo '</p>';
292 do_action( "in_plugin_update_message-{$file}", $plugin, $version_info );
293
294 echo '</div></td></tr>';
295 }
296 }
297
298 /**
299 * Updates information on the "View version x.x details" page with custom data.
300 *
301 * @uses api_request()
302 *
303 * @param mixed $_data
304 * @param string $_action
305 * @param object $_args
306 * @return object $_data
307 */
308 public function plugins_api_filter( $_data, $_action = '', $_args = null ) {
309
310 if ( 'plugin_information' != $_action ) {
311
312 return $_data;
313
314 }
315
316 if ( ! isset( $_args->slug ) || ( $_args->slug != $this->slug ) ) {
317
318 return $_data;
319
320 }
321
322 $to_send = array(
323 'slug' => $this->slug,
324 'is_ssl' => is_ssl(),
325 'fields' => array(
326 'banners' => array(),
327 'reviews' => false,
328 'icons' => array(),
329 ),
330 );
331
332 // Get the transient where we store the api request for this plugin for 24 hours
333 $edd_api_request_transient = $this->get_cached_version_info();
334
335 // 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.
336 if ( empty( $edd_api_request_transient ) ) {
337
338 $api_response = $this->api_request( 'plugin_information', $to_send );
339
340 // Expires in 3 hours
341 $this->set_version_info_cache( $api_response );
342
343 if ( false !== $api_response ) {
344 $_data = $api_response;
345 }
346 } else {
347 $_data = $edd_api_request_transient;
348 }
349
350 // Convert sections into an associative array, since we're getting an object, but Core expects an array.
351 if ( isset( $_data->sections ) && ! is_array( $_data->sections ) ) {
352 $_data->sections = $this->convert_object_to_array( $_data->sections );
353 }
354
355 // Convert banners into an associative array, since we're getting an object, but Core expects an array.
356 if ( isset( $_data->banners ) && ! is_array( $_data->banners ) ) {
357 $_data->banners = $this->convert_object_to_array( $_data->banners );
358 }
359
360 // Convert icons into an associative array, since we're getting an object, but Core expects an array.
361 if ( isset( $_data->icons ) && ! is_array( $_data->icons ) ) {
362 $_data->icons = $this->convert_object_to_array( $_data->icons );
363 }
364
365 // Convert contributors into an associative array, since we're getting an object, but Core expects an array.
366 if ( isset( $_data->contributors ) && ! is_array( $_data->contributors ) ) {
367 $_data->contributors = $this->convert_object_to_array( $_data->contributors );
368 }
369
370 if ( ! isset( $_data->plugin ) ) {
371 $_data->plugin = $this->name;
372 }
373
374 return $_data;
375 }
376
377 /**
378 * Convert some objects to arrays when injecting data into the update API
379 *
380 * Some data like sections, banners, and icons are expected to be an associative array, however due to the JSON
381 * decoding, they are objects. This method allows us to pass in the object and return an associative array.
382 *
383 * @since 3.6.5
384 *
385 * @param stdClass $data
386 *
387 * @return array
388 */
389 private function convert_object_to_array( $data ) {
390 $new_data = array();
391 foreach ( $data as $key => $value ) {
392 $new_data[ $key ] = is_object( $value ) ? $this->convert_object_to_array( $value ) : $value;
393 }
394
395 return $new_data;
396 }
397
398 /**
399 * Disable SSL verification in order to prevent download update failures
400 *
401 * @param array $args
402 * @param string $url
403 * @return object $array
404 */
405 public function http_request_args( $args, $url ) {
406
407 $verify_ssl = $this->verify_ssl();
408 if ( strpos( $url, 'https://' ) !== false && strpos( $url, 'edd_action=package_download' ) ) {
409 $args['sslverify'] = $verify_ssl;
410 }
411 return $args;
412
413 }
414
415 /**
416 * Calls the API and, if successfull, returns the object delivered by the API.
417 *
418 * @uses get_bloginfo()
419 * @uses wp_remote_post()
420 * @uses is_wp_error()
421 *
422 * @param string $_action The requested action.
423 * @param array $_data Parameters for the API action.
424 * @return false|object
425 */
426 private function api_request( $_action, $_data ) {
427
428 global $wp_version, $edd_plugin_url_available;
429
430 $verify_ssl = $this->verify_ssl();
431
432 // Do a quick status check on this domain if we haven't already checked it.
433 $store_hash = md5( $this->api_url );
434 if ( ! is_array( $edd_plugin_url_available ) || ! isset( $edd_plugin_url_available[ $store_hash ] ) ) {
435 $test_url_parts = parse_url( $this->api_url );
436
437 $scheme = ! empty( $test_url_parts['scheme'] ) ? $test_url_parts['scheme'] : 'http';
438 $host = ! empty( $test_url_parts['host'] ) ? $test_url_parts['host'] : '';
439 $port = ! empty( $test_url_parts['port'] ) ? ':' . $test_url_parts['port'] : '';
440
441 if ( empty( $host ) ) {
442 $edd_plugin_url_available[ $store_hash ] = false;
443 } else {
444 $test_url = $scheme . '://' . $host . $port;
445 $response = wp_remote_get(
446 $test_url,
447 array(
448 'timeout' => $this->health_check_timeout,
449 'sslverify' => $verify_ssl,
450 )
451 );
452 $edd_plugin_url_available[ $store_hash ] = is_wp_error( $response ) ? false : true;
453 }
454 }
455
456 if ( false === $edd_plugin_url_available[ $store_hash ] ) {
457 return false;
458 }
459
460 $data = array_merge( $this->api_data, $_data );
461
462 if ( $data['slug'] != $this->slug ) {
463 return false;
464 }
465
466 if ( trailingslashit( home_url() ) == $this->api_url ) {
467 return false; // Don't allow a plugin to ping itself
468 }
469
470 $api_params = array(
471 'edd_action' => 'get_version',
472 'license' => ! empty( $data['license'] ) ? $data['license'] : '',
473 'item_name' => isset( $data['item_name'] ) ? $data['item_name'] : false,
474 'item_id' => isset( $data['item_id'] ) ? $data['item_id'] : false,
475 'version' => isset( $data['version'] ) ? $data['version'] : false,
476 'slug' => $data['slug'],
477 'author' => $data['author'],
478 'url' => home_url(),
479 'beta' => ! empty( $data['beta'] ),
480 );
481
482 $request = wp_remote_post(
483 $this->api_url,
484 array(
485 'timeout' => 15,
486 'sslverify' => $verify_ssl,
487 'body' => $api_params,
488 )
489 );
490
491 if ( ! is_wp_error( $request ) ) {
492 $request = json_decode( wp_remote_retrieve_body( $request ) );
493 }
494
495 if ( $request && isset( $request->sections ) ) {
496 $request->sections = maybe_unserialize( $request->sections );
497 } else {
498 $request = false;
499 }
500
501 if ( $request && isset( $request->banners ) ) {
502 $request->banners = maybe_unserialize( $request->banners );
503 }
504
505 if ( $request && isset( $request->icons ) ) {
506 $request->icons = maybe_unserialize( $request->icons );
507 }
508
509 if ( ! empty( $request->sections ) ) {
510 foreach ( $request->sections as $key => $section ) {
511 $request->$key = (array) $section;
512 }
513 }
514
515 return $request;
516 }
517
518 /**
519 * If available, show the changelog for sites in a multisite install.
520 */
521 public function show_changelog() {
522
523 global $edd_plugin_data;
524
525 if ( empty( $_REQUEST['edd_sl_action'] ) || 'view_plugin_changelog' != $_REQUEST['edd_sl_action'] ) {
526 return;
527 }
528
529 if ( empty( $_REQUEST['plugin'] ) ) {
530 return;
531 }
532
533 if ( empty( $_REQUEST['slug'] ) ) {
534 return;
535 }
536
537 if ( ! current_user_can( 'update_plugins' ) ) {
538 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 ) );
539 }
540
541 $data = $edd_plugin_data[ $_REQUEST['slug'] ];
542 $version_info = $this->get_cached_version_info();
543
544 if ( false === $version_info ) {
545
546 $api_params = array(
547 'edd_action' => 'get_version',
548 'item_name' => isset( $data['item_name'] ) ? $data['item_name'] : false,
549 'item_id' => isset( $data['item_id'] ) ? $data['item_id'] : false,
550 'slug' => $_REQUEST['slug'],
551 'author' => $data['author'],
552 'url' => home_url(),
553 'beta' => ! empty( $data['beta'] ),
554 );
555
556 $verify_ssl = $this->verify_ssl();
557 $request = wp_remote_post(
558 $this->api_url,
559 array(
560 'timeout' => 15,
561 'sslverify' => $verify_ssl,
562 'body' => $api_params,
563 )
564 );
565
566 if ( ! is_wp_error( $request ) ) {
567 $version_info = json_decode( wp_remote_retrieve_body( $request ) );
568 }
569
570 if ( ! empty( $version_info ) && isset( $version_info->sections ) ) {
571 $version_info->sections = maybe_unserialize( $version_info->sections );
572 } else {
573 $version_info = false;
574 }
575
576 if ( ! empty( $version_info ) ) {
577 foreach ( $version_info->sections as $key => $section ) {
578 $version_info->$key = (array) $section;
579 }
580 }
581
582 $this->set_version_info_cache( $version_info );
583
584 // Delete the unneeded option
585 delete_option( md5( 'edd_plugin_' . sanitize_key( $_REQUEST['plugin'] ) . '_' . $this->beta . '_version_info' ) );
586 }
587
588 if ( isset( $version_info->sections ) ) {
589 $sections = $this->convert_object_to_array( $version_info->sections );
590 if ( ! empty( $sections['changelog'] ) ) {
591 echo '<div style="background:#fff;padding:10px;">' . wp_kses_post( $sections['changelog'] ) . '</div>';
592 }
593 }
594
595 exit;
596 }
597
598 /**
599 * Gets the plugin's cached version information from the database.
600 *
601 * @param string $cache_key
602 * @return boolean|string
603 */
604 public function get_cached_version_info( $cache_key = '' ) {
605
606 if ( empty( $cache_key ) ) {
607 $cache_key = $this->cache_key;
608 }
609
610 $cache = get_option( $cache_key );
611
612 if ( empty( $cache['timeout'] ) || time() > $cache['timeout'] ) {
613 return false; // Cache is expired
614 }
615
616 // We need to turn the icons into an array, thanks to WP Core forcing these into an object at some point.
617 $cache['value'] = json_decode( $cache['value'] );
618 if ( ! empty( $cache['value']->icons ) ) {
619 $cache['value']->icons = (array) $cache['value']->icons;
620 }
621
622 return $cache['value'];
623
624 }
625
626 /**
627 * Adds the plugin version information to the database.
628 *
629 * @param string $value
630 * @param string $cache_key
631 */
632 public function set_version_info_cache( $value = '', $cache_key = '' ) {
633
634 if ( empty( $cache_key ) ) {
635 $cache_key = $this->cache_key;
636 }
637
638 $data = array(
639 'timeout' => strtotime( '+3 hours', time() ),
640 'value' => json_encode( $value ),
641 );
642
643 update_option( $cache_key, $data, 'no' );
644
645 // Delete the duplicate option
646 delete_option( 'edd_api_request_' . md5( serialize( $this->slug . $this->api_data['license'] . $this->beta ) ) );
647 }
648
649 /**
650 * Returns if the SSL of the store should be verified.
651 *
652 * @since 1.6.13
653 * @return bool
654 */
655 private function verify_ssl() {
656 return (bool) apply_filters( 'edd_sl_api_request_verify_ssl', true, $this );
657 }
658
659 }
660