Merge pull request #937 from mailpoet/premium_updates_fix
Add updater to the Free [PREMIUM-22]
This commit is contained in:
@@ -85,6 +85,7 @@ class Initializer {
|
||||
$this->maybeDbUpdate();
|
||||
$this->setupRenderer();
|
||||
$this->setupInstaller();
|
||||
$this->setupUpdater();
|
||||
$this->setupLocalizer();
|
||||
$this->setupMenu();
|
||||
$this->setupAnalytics();
|
||||
@@ -153,6 +154,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();
|
||||
|
@@ -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)) {
|
||||
|
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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user