Нет описания

Updater.php 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace MailPoet\Config;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoet\Services\Bridge;
  5. use MailPoet\Services\Release\API;
  6. use MailPoet\Settings\SettingsController;
  7. use MailPoet\WP\Functions as WPFunctions;
  8. class Updater {
  9. private $plugin;
  10. private $slug;
  11. private $version;
  12. /** @var SettingsController */
  13. private $settings;
  14. public function __construct(
  15. $pluginName,
  16. $slug,
  17. $version
  18. ) {
  19. $this->plugin = WPFunctions::get()->pluginBasename($pluginName);
  20. $this->slug = $slug;
  21. $this->version = $version;
  22. $this->settings = SettingsController::getInstance();
  23. }
  24. public function init() {
  25. WPFunctions::get()->addFilter('pre_set_site_transient_update_plugins', [$this, 'checkForUpdate']);
  26. }
  27. public function checkForUpdate($updateTransient) {
  28. if (!$updateTransient instanceof \stdClass) {
  29. $updateTransient = new \stdClass;
  30. }
  31. $latestVersion = $this->getLatestVersion();
  32. if (isset($latestVersion->new_version)) { // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
  33. if (version_compare($this->version, $latestVersion->new_version, '<')) { // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
  34. $updateTransient->response[$this->plugin] = $latestVersion;
  35. }
  36. $updateTransient->last_checked = time(); // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
  37. $updateTransient->checked[$this->plugin] = $this->version;
  38. }
  39. return $updateTransient;
  40. }
  41. public function getLatestVersion() {
  42. $key = $this->settings->get(Bridge::PREMIUM_KEY_SETTING_NAME);
  43. $api = new API($key);
  44. $data = $api->getPluginInformation($this->slug . '/latest');
  45. return $data;
  46. }
  47. }