| 1 |
<?php |
| 2 |
|
| 3 |
namespace Boxzilla\Licensing; |
| 4 |
|
| 5 |
use Boxzilla\Admin\Notices; |
| 6 |
use Boxzilla\Collection; |
| 7 |
use Exception; |
| 8 |
|
| 9 |
class LicenseManager |
| 10 |
{ |
| 11 |
|
| 12 |
/** |
| 13 |
* @var array |
| 14 |
*/ |
| 15 |
protected $extensions = array(); |
| 16 |
|
| 17 |
/** |
| 18 |
* @var License |
| 19 |
*/ |
| 20 |
protected $license; |
| 21 |
|
| 22 |
/** |
| 23 |
* @var API |
| 24 |
*/ |
| 25 |
protected $api; |
| 26 |
|
| 27 |
/** |
| 28 |
* @var array |
| 29 |
*/ |
| 30 |
protected $notices = array(); |
| 31 |
|
| 32 |
/** |
| 33 |
* @param array $extensions |
| 34 |
* @param API $api |
| 35 |
* @param License $license |
| 36 |
* @param Notices $notices |
| 37 |
*/ |
| 38 |
public function __construct(array $extensions, API $api, License $license) |
| 39 |
{ |
| 40 |
$this->extensions = $extensions; |
| 41 |
$this->license = $license; |
| 42 |
$this->api = $api; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* @param mixed $object |
| 47 |
* @param string $property |
| 48 |
* @param string $default |
| 49 |
* |
| 50 |
* @return string |
| 51 |
*/ |
| 52 |
protected function get_object_property($object, $property, $default = '') |
| 53 |
{ |
| 54 |
return isset($object->$property) ? $object->$property : $default; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* @return void |
| 59 |
*/ |
| 60 |
public function hook() |
| 61 |
{ |
| 62 |
|
| 63 |
// do nothing if no extensions are registered at this point |
| 64 |
if (empty($this->extensions)) { |
| 65 |
return; |
| 66 |
} |
| 67 |
|
| 68 |
// hooks |
| 69 |
add_action('boxzilla_after_settings', array( $this, 'show_license_form' )); |
| 70 |
add_action('admin_notices', array( $this, 'show_notice' ), 1); |
| 71 |
|
| 72 |
// listen for activation / deactivation requests |
| 73 |
$this->listen(); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Maybe show notice to activate license. |
| 78 |
*/ |
| 79 |
public function show_notice() |
| 80 |
{ |
| 81 |
global $current_screen; |
| 82 |
|
| 83 |
if ($this->license->activated) { |
| 84 |
return; |
| 85 |
} |
| 86 |
|
| 87 |
if ($this->get_object_property($current_screen, 'post_type') !== 'boxzilla-box') { |
| 88 |
return; |
| 89 |
} |
| 90 |
|
| 91 |
$plugin = $this->extensions[ array_rand($this->extensions) ]; |
| 92 |
$message = sprintf('Please <a href="%s">activate your Boxzilla license</a> to use %s.', admin_url('edit.php?post_type=boxzilla-box&page=boxzilla-settings'), '<strong>' . $plugin->name() . '</strong>'); |
| 93 |
echo sprintf('<div class="notice notice-%s"><p>%s</p></div>', 'warning', $message); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* @return void |
| 98 |
*/ |
| 99 |
protected function listen() |
| 100 |
{ |
| 101 |
|
| 102 |
// do nothing if not authenticated |
| 103 |
if (! current_user_can('manage_options')) { |
| 104 |
return; |
| 105 |
} |
| 106 |
|
| 107 |
// nothing to do |
| 108 |
if (! isset($_POST['boxzilla_license_form'])) { |
| 109 |
return; |
| 110 |
} |
| 111 |
|
| 112 |
$action = isset($_POST['action']) ? $_POST['action'] : 'activate'; |
| 113 |
$key_changed = false; |
| 114 |
|
| 115 |
// did key change or was "activate" button pressed? |
| 116 |
$new_license_key = sanitize_text_field($_POST['boxzilla_license_key']); |
| 117 |
if ($new_license_key !== $this->license->key) { |
| 118 |
$this->license->key = $new_license_key; |
| 119 |
$key_changed = true; |
| 120 |
} |
| 121 |
|
| 122 |
// run actions |
| 123 |
if ($action === 'deactivate') { |
| 124 |
$this->deactivate_license(); |
| 125 |
} elseif ($action === 'activate' || $key_changed) { |
| 126 |
$this->activate_license(); |
| 127 |
} |
| 128 |
|
| 129 |
$this->license->save(); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Deactivate the license |
| 134 |
*/ |
| 135 |
protected function deactivate_license() |
| 136 |
{ |
| 137 |
try { |
| 138 |
$this->api->deactivate_license(); |
| 139 |
$this->notices[] = array( |
| 140 |
'type' => 'info', |
| 141 |
'message' => 'Your license was successfully deactivated!', |
| 142 |
); |
| 143 |
} catch (API_Exception $e) { |
| 144 |
$this->notices[] = array( |
| 145 |
'type' => 'warning', |
| 146 |
'message' => $e->getMessage(), |
| 147 |
); |
| 148 |
} |
| 149 |
|
| 150 |
$this->license->activated = false; |
| 151 |
$this->license->activation_key = ''; |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Activate the license |
| 156 |
*/ |
| 157 |
protected function activate_license() |
| 158 |
{ |
| 159 |
try { |
| 160 |
$activation = $this->api->activate_license(); |
| 161 |
} catch (API_Exception $e) { |
| 162 |
$message = $e->getMessage(); |
| 163 |
if ($e->getApiCode() == 'license_at_limit') { |
| 164 |
$message .= ' You can <a href="https://platform.boxzillaplugin.com/licenses">manage your site activations here</a>.'; |
| 165 |
} |
| 166 |
$this->notices[] = array( |
| 167 |
'type' => 'warning', |
| 168 |
'message' => $message, |
| 169 |
); |
| 170 |
return; |
| 171 |
} |
| 172 |
|
| 173 |
$this->license->activation_key = $activation->token; |
| 174 |
$this->license->activated = true; |
| 175 |
|
| 176 |
$this->notices[] = array( |
| 177 |
'type' => 'info', |
| 178 |
'message' => 'Your license was successfully activated!', |
| 179 |
); |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Shows the license form |
| 184 |
*/ |
| 185 |
public function show_license_form() |
| 186 |
{ |
| 187 |
$license = $this->license; |
| 188 |
require __DIR__ . '/views/license-form.php'; |
| 189 |
} |
| 190 |
} |
| 191 |
|