| 1 |
<?php |
| 2 |
namespace MailPoet\Config; |
| 3 |
|
| 4 |
use MailPoet\Models\Setting; |
| 5 |
use MailPoet\Services\Bridge; |
| 6 |
use MailPoet\Services\Release\API; |
| 7 |
use MailPoet\Util\License\License; |
| 8 |
|
| 9 |
if(!defined('ABSPATH')) exit; |
| 10 |
|
| 11 |
class Installer { |
| 12 |
const PREMIUM_PLUGIN_SLUG = 'mailpoet-premium'; |
| 13 |
|
| 14 |
private $slug; |
| 15 |
|
| 16 |
function __construct($slug) { |
| 17 |
$this->slug = $slug; |
| 18 |
} |
| 19 |
|
| 20 |
function init() { |
| 21 |
add_filter('plugins_api', array($this, 'getPluginInformation'), 10, 3); |
| 22 |
} |
| 23 |
|
| 24 |
function getPluginInformation($data, $action = '', $args = null) { |
| 25 |
if($action === 'plugin_information' |
| 26 |
&& isset($args->slug) |
| 27 |
&& $args->slug === $this->slug |
| 28 |
) { |
| 29 |
$data = $this->retrievePluginInformation(); |
| 30 |
} |
| 31 |
|
| 32 |
return $data; |
| 33 |
} |
| 34 |
|
| 35 |
static function getPremiumStatus() { |
| 36 |
$slug = self::PREMIUM_PLUGIN_SLUG; |
| 37 |
|
| 38 |
$premium_plugin_active = License::getLicense(); |
| 39 |
$premium_plugin_installed = $premium_plugin_active || self::isPluginInstalled($slug); |
| 40 |
$premium_install_url = $premium_plugin_installed ? '' : self::getPluginInstallationUrl($slug); |
| 41 |
$premium_activate_url = $premium_plugin_active ? '' : self::getPluginActivationUrl($slug); |
| 42 |
|
| 43 |
return compact( |
| 44 |
'premium_plugin_active', |
| 45 |
'premium_plugin_installed', |
| 46 |
'premium_install_url', |
| 47 |
'premium_activate_url' |
| 48 |
); |
| 49 |
} |
| 50 |
|
| 51 |
static function isPluginInstalled($slug) { |
| 52 |
$installed_plugin = self::getInstalledPlugin($slug); |
| 53 |
return !empty($installed_plugin); |
| 54 |
} |
| 55 |
|
| 56 |
static function getPluginInstallationUrl($slug) { |
| 57 |
$install_url = add_query_arg( |
| 58 |
array( |
| 59 |
'action' => 'install-plugin', |
| 60 |
'plugin' => $slug, |
| 61 |
'_wpnonce' => wp_create_nonce('install-plugin_' . $slug), |
| 62 |
), |
| 63 |
self_admin_url('update.php') |
| 64 |
); |
| 65 |
return $install_url; |
| 66 |
} |
| 67 |
|
| 68 |
static function getPluginActivationUrl($slug) { |
| 69 |
$plugin_file = self::getPluginFile($slug); |
| 70 |
if(empty($plugin_file)) { |
| 71 |
return false; |
| 72 |
} |
| 73 |
$activate_url = add_query_arg( |
| 74 |
array( |
| 75 |
'action' => 'activate', |
| 76 |
'plugin' => $plugin_file, |
| 77 |
'_wpnonce' => wp_create_nonce('activate-plugin_' . $plugin_file), |
| 78 |
), |
| 79 |
self_admin_url('plugins.php') |
| 80 |
); |
| 81 |
return $activate_url; |
| 82 |
} |
| 83 |
|
| 84 |
private static function getInstalledPlugin($slug) { |
| 85 |
$installed_plugin = array(); |
| 86 |
if(is_dir(WP_PLUGIN_DIR . '/' . $slug)) { |
| 87 |
$installed_plugin = get_plugins('/' . $slug); |
| 88 |
} |
| 89 |
return $installed_plugin; |
| 90 |
} |
| 91 |
|
| 92 |
static function getPluginFile($slug) { |
| 93 |
$plugin_file = false; |
| 94 |
$installed_plugin = self::getInstalledPlugin($slug); |
| 95 |
if(!empty($installed_plugin)) { |
| 96 |
$plugin_file = $slug . '/' . key($installed_plugin); |
| 97 |
} |
| 98 |
return $plugin_file; |
| 99 |
} |
| 100 |
|
| 101 |
function retrievePluginInformation() { |
| 102 |
$key = Setting::getValue(Bridge::PREMIUM_KEY_SETTING_NAME); |
| 103 |
$api = new API($key); |
| 104 |
$info = $api->getPluginInformation($this->slug); |
| 105 |
$info = $this->formatInformation($info); |
| 106 |
return $info; |
| 107 |
} |
| 108 |
|
| 109 |
private function formatInformation($info) { |
| 110 |
// cast sections object to array for WP to understand |
| 111 |
if(isset($info->sections)) { |
| 112 |
$info->sections = (array)$info->sections; |
| 113 |
} |
| 114 |
return $info; |
| 115 |
} |
| 116 |
} |
| 117 |
|