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