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