diff --git a/lib/Config/Initializer.php b/lib/Config/Initializer.php index 743d010dbe..cb61550aa0 100644 --- a/lib/Config/Initializer.php +++ b/lib/Config/Initializer.php @@ -76,6 +76,7 @@ class Initializer { $this->maybeDbUpdate(); $this->setupRenderer(); $this->setupInstaller(); + $this->setupUpdater(); $this->setupLocalizer(); $this->setupMenu(); $this->setupAnalytics(); @@ -144,6 +145,20 @@ class Initializer { $installer->init(); } + function setupUpdater() { + $slug = Installer::PREMIUM_PLUGIN_SLUG; + $plugin_file = Installer::getPluginFile($slug); + if(empty($plugin_file) || !defined('MAILPOET_PREMIUM_VERSION')) { + return false; + } + $updater = new Updater( + $plugin_file, + $slug, + MAILPOET_PREMIUM_VERSION + ); + $updater->init(); + } + function setupLocalizer() { $localizer = new Localizer($this->renderer); $localizer->init(); diff --git a/lib/Config/Installer.php b/lib/Config/Installer.php index 9f85ebca2e..84d50637c0 100644 --- a/lib/Config/Installer.php +++ b/lib/Config/Installer.php @@ -89,7 +89,7 @@ class Installer { return $installed_plugin; } - private static function getPluginFile($slug) { + static function getPluginFile($slug) { $plugin_file = false; $installed_plugin = self::getInstalledPlugin($slug); if(!empty($installed_plugin)) { diff --git a/lib/Config/Updater.php b/lib/Config/Updater.php new file mode 100644 index 0000000000..c5d8fff0f1 --- /dev/null +++ b/lib/Config/Updater.php @@ -0,0 +1,49 @@ +plugin = plugin_basename($plugin_name); + $this->slug = $slug; + $this->version = $version; + } + + function init() { + add_filter('pre_set_site_transient_update_plugins', array($this, 'checkForUpdate')); + } + + function checkForUpdate($update_transient) { + if(!is_object($update_transient)) { + $update_transient = new \stdClass; + } + + $latest_version = $this->getLatestVersion(); + + if(isset($latest_version->new_version)) { + if(version_compare($this->version, $latest_version->new_version, '<')) { + $update_transient->response[$this->plugin] = $latest_version; + } + $update_transient->last_checked = time(); + $update_transient->checked[$this->plugin] = $this->version; + } + + return $update_transient; + } + + function getLatestVersion() { + $key = Setting::getValue(Bridge::PREMIUM_KEY_SETTING_NAME); + $api = new API($key); + $data = $api->getPluginInformation($this->slug . '/latest'); + return $data; + } +} diff --git a/tests/unit/Config/UpdaterTest.php b/tests/unit/Config/UpdaterTest.php new file mode 100644 index 0000000000..d4098d6af9 --- /dev/null +++ b/tests/unit/Config/UpdaterTest.php @@ -0,0 +1,70 @@ +plugin_name = 'some-plugin/some-plugin.php'; + $this->slug = 'some-plugin'; + $this->version = '0.1'; + + $this->updater = new Updater( + $this->plugin_name, + $this->slug, + $this->version + ); + } + + function testItInitializes() { + $updater = Stub::make( + $this->updater, + array( + 'checkForUpdate' => Stub::once() + ) + ); + $updater->init(); + apply_filters('pre_set_site_transient_update_plugins', null); + } + + function testItChecksForUpdates() { + $update_transient = new \StdClass; + $update_transient->last_checked = time(); + $updater = Stub::construct( + $this->updater, + array( + $this->plugin_name, + $this->slug, + $this->version + ), + array( + 'getLatestVersion' => function () { + return (object)array( + 'id' => 76630, + 'slug' => $this->slug, + 'plugin' => $this->plugin_name, + 'new_version' => $this->version . 1, + 'url' => 'http://www.mailpoet.com/wordpress-newsletter-plugin-premium/', + 'package' => home_url() . '/wp-content/uploads/mailpoet-premium.zip' + ); + } + ) + ); + $result = $updater->checkForUpdate($update_transient); + expect($result->last_checked)->greaterOrEquals($update_transient->last_checked); + expect($result->checked[$this->plugin_name])->equals($this->version); + expect($result->response[$this->plugin_name]->slug)->equals($this->slug); + expect($result->response[$this->plugin_name]->plugin)->equals($this->plugin_name); + expect(version_compare( + $this->version, + $result->response[$this->plugin_name]->new_version, + '<' + ))->true(); + expect($result->response[$this->plugin_name]->package)->notEmpty(); + } + + function testItReturnsObjectIfPassedNonObjectWhenCheckingForUpdates() { + $result = $this->updater->checkForUpdate(null); + expect($result instanceof \StdClass)->true(); + } +}