Brak opisu

upgrader.php 3.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. use NSL\Notices;
  3. class NextendSocialUpgrader {
  4. public static function init() {
  5. add_filter('plugins_api', 'NextendSocialUpgrader::plugins_api', 20, 3); // WooCommerce use priority 20, so better to follow
  6. add_filter('upgrader_pre_download', 'NextendSocialUpgrader::upgrader_pre_download', 10, 3);
  7. add_filter('pre_set_site_transient_update_plugins', 'NextendSocialUpgrader::injectUpdate');
  8. }
  9. public static function plugins_api($res, $action, $args) {
  10. if ($action === 'plugin_information' && $args->slug === 'nextend-social-login-pro') {
  11. try {
  12. $res = (object)NextendSocialLoginAdmin::apiCall($action, (array)$args);
  13. } catch (Exception $e) {
  14. $res = new WP_Error('error', $e->getMessage());
  15. }
  16. }
  17. return $res;
  18. }
  19. public static function upgrader_pre_download($reply, $package, $upgrader) {
  20. $needle = NextendSocialLoginAdmin::getEndpoint();
  21. if (substr($package, 0, strlen($needle)) == $needle) {
  22. add_filter('http_response', 'NextendSocialUpgrader::http_response', 10, 3);
  23. }
  24. return $reply;
  25. }
  26. public static function http_response($response, $r, $url) {
  27. $needle = NextendSocialLoginAdmin::getEndpoint();
  28. if (substr($url, 0, strlen($needle)) == $needle && 200 != wp_remote_retrieve_response_code($response) || is_wp_error($response)) {
  29. if (isset($response['filename']) && file_exists($response['filename'])) {
  30. $body = @json_decode(@file_get_contents($response['filename']), true);
  31. if (is_array($body) && isset($body['message'])) {
  32. $message = 'Nextend Social Login Pro Addon: ' . $body['message'];
  33. if (isset($body['code']) && $body['code'] == 'license_invalid' && NextendSocialLogin::hasLicense()) {
  34. NextendSocialLogin::$settings->update(array(
  35. 'license_key' => ''
  36. ));
  37. $message .= ' - the stored license key has been removed!';
  38. }
  39. Notices::addError($message);
  40. return new WP_Error('error', $message);
  41. }
  42. }
  43. }
  44. return $response;
  45. }
  46. public static function injectUpdate($transient) {
  47. if (!class_exists('NextendSocialLoginPRO', false)) {
  48. return $transient;
  49. }
  50. $filename = "nextend-social-login-pro/nextend-social-login-pro.php";
  51. if (!isset($transient->response[$filename])) {
  52. try {
  53. $item = (object)NextendSocialLoginAdmin::apiCall('plugin_information', array('slug' => 'nextend-social-login-pro'));
  54. } catch (Exception $e) {
  55. $item = new WP_Error('error', $e->getMessage());
  56. }
  57. if (!is_wp_error($item)) {
  58. $item->plugin = 'nextend-social-login-pro/nextend-social-login-pro.php';
  59. if (version_compare(NextendSocialLoginPRO::$version, $item->new_version, '<')) {
  60. $transient->response[$filename] = (object)$item;
  61. unset($transient->no_update[$filename]);
  62. } else {
  63. $transient->no_update[$filename] = (object)$item;
  64. unset($transient->response[$filename]);
  65. }
  66. }
  67. }
  68. return $transient;
  69. }
  70. }