Add updater to the Free [PREMIUM-22]
This commit is contained in:
@ -76,6 +76,7 @@ class Initializer {
|
|||||||
$this->maybeDbUpdate();
|
$this->maybeDbUpdate();
|
||||||
$this->setupRenderer();
|
$this->setupRenderer();
|
||||||
$this->setupInstaller();
|
$this->setupInstaller();
|
||||||
|
$this->setupUpdater();
|
||||||
$this->setupLocalizer();
|
$this->setupLocalizer();
|
||||||
$this->setupMenu();
|
$this->setupMenu();
|
||||||
$this->setupAnalytics();
|
$this->setupAnalytics();
|
||||||
@ -144,6 +145,20 @@ class Initializer {
|
|||||||
$installer->init();
|
$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() {
|
function setupLocalizer() {
|
||||||
$localizer = new Localizer($this->renderer);
|
$localizer = new Localizer($this->renderer);
|
||||||
$localizer->init();
|
$localizer->init();
|
||||||
|
@ -89,7 +89,7 @@ class Installer {
|
|||||||
return $installed_plugin;
|
return $installed_plugin;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static function getPluginFile($slug) {
|
static function getPluginFile($slug) {
|
||||||
$plugin_file = false;
|
$plugin_file = false;
|
||||||
$installed_plugin = self::getInstalledPlugin($slug);
|
$installed_plugin = self::getInstalledPlugin($slug);
|
||||||
if(!empty($installed_plugin)) {
|
if(!empty($installed_plugin)) {
|
||||||
|
49
lib/Config/Updater.php
Normal file
49
lib/Config/Updater.php
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
<?php
|
||||||
|
namespace MailPoet\Config;
|
||||||
|
|
||||||
|
use MailPoet\Models\Setting;
|
||||||
|
use MailPoet\Services\Bridge;
|
||||||
|
use MailPoet\Services\Release\API;
|
||||||
|
|
||||||
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
|
class Updater {
|
||||||
|
private $plugin;
|
||||||
|
private $slug;
|
||||||
|
private $version;
|
||||||
|
|
||||||
|
function __construct($plugin_name, $slug, $version) {
|
||||||
|
$this->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;
|
||||||
|
}
|
||||||
|
}
|
70
tests/unit/Config/UpdaterTest.php
Normal file
70
tests/unit/Config/UpdaterTest.php
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Codeception\Util\Stub;
|
||||||
|
use MailPoet\Config\Updater;
|
||||||
|
|
||||||
|
class UpdaterTest extends MailPoetTest {
|
||||||
|
function _before() {
|
||||||
|
$this->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();
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user