PluginProbe
Boxzilla – WordPress Popup Builder / 3.4.9
Boxzilla – WordPress Popup Builder v3.4.9
3.4.11 3.4.10 3.4.9 3.4.3 3.4.4 3.4.5 3.4.6 3.4.7 3.4.8 trunk 3.0 3.0.1 3.0.2 3.0.3 3.1 3.1.1 3.1.10 3.1.11 3.1.12 3.1.13 3.1.14 3.1.15 3.1.16 3.1.17 3.1.18 All 72 releases
boxzilla / src / licensing / class-license-manager.php

class-license-manager.php in Boxzilla – WordPress Popup Builder 3.4.9, at src/licensing/class-license-manager.php

209 lines 5.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Boxzilla\Licensing;
4
5 if (! defined('ABSPATH')) {
6 exit;
7 }
8
9 class LicenseManager
10 {
11 /**
12 * @var array
13 */
14 protected $extensions = [];
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 = [];
30
31 /**
32 * @param array $extensions
33 * @param API $api
34 * @param License $license
35 */
36 public function __construct(array $extensions, API $api, License $license)
37 {
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 {
52 return isset($object->$property) ? $object->$property : $default;
53 }
54
55 /**
56 * @return void
57 */
58 public function init()
59 {
60 // do nothing if no extensions are registered at this point
61 if (empty($this->extensions)) {
62 return;
63 }
64
65 // hooks
66 add_action('boxzilla_after_settings', [ $this, 'show_license_form' ]);
67 add_action('admin_notices', [ $this, 'show_notice' ], 1);
68
69 // listen for activation / deactivation requests
70 $this->listen();
71 }
72
73 /**
74 * Maybe show notice to activate license.
75 */
76 public function show_notice()
77 {
78 global $current_screen;
79
80 if ($this->license->activated) {
81 return;
82 }
83
84 if ($this->get_object_property($current_screen, 'post_type') !== 'boxzilla-box') {
85 return;
86 }
87
88 $plugin = $this->extensions[ array_rand($this->extensions) ];
89 $settings_url = esc_url(admin_url('edit.php?post_type=boxzilla-box&page=boxzilla-settings'));
90 $plugin_name = esc_html($plugin->name());
91 $message = sprintf('Please <a href="%1$s">activate your Boxzilla license</a> to use <strong>%2$s</strong>.', $settings_url, $plugin_name);
92 echo '<div class="notice notice-warning"><p>' . wp_kses_post($message) . '</p></div>';
93 }
94
95 /**
96 * @return void
97 */
98 protected function listen()
99 {
100
101 // do nothing if not authenticated
102 if (! current_user_can('manage_options')) {
103 return;
104 }
105
106 // nothing to do
107 if (! isset($_POST['boxzilla_license_form'])) {
108 return;
109 }
110
111 $nonce = isset($_POST['boxzilla_license_nonce']) ? sanitize_text_field(wp_unslash($_POST['boxzilla_license_nonce'])) : '';
112 if (! wp_verify_nonce($nonce, 'boxzilla_license_form_action')) {
113 $this->notices[] = [
114 'type' => 'warning',
115 'message' => esc_html__('Security check failed. Please refresh the page and try again.', 'boxzilla'),
116 ];
117 return;
118 }
119
120 $action = isset($_POST['action']) ? sanitize_text_field(wp_unslash($_POST['action'])) : 'activate';
121 $key_changed = false;
122
123 // did key change or was "activate" button pressed?
124 $new_license_key = isset($_POST['boxzilla_license_key']) ? sanitize_text_field(wp_unslash($_POST['boxzilla_license_key'])) : '';
125 if ($new_license_key !== $this->license->key) {
126 $this->license->key = $new_license_key;
127 $key_changed = true;
128 }
129
130 // run actions
131 if ($action === 'deactivate') {
132 $this->deactivate_license();
133 } elseif ($action === 'activate' || $key_changed) {
134 $this->activate_license();
135 }
136
137 $this->license->save();
138 }
139
140 /**
141 * Deactivate the license
142 */
143 protected function deactivate_license()
144 {
145 try {
146 $this->api->deactivate_license();
147 $this->notices[] = [
148 'type' => 'info',
149 'message' => 'Your license was successfully deactivated!',
150 ];
151 } catch (API_Exception $e) {
152 $this->notices[] = [
153 'type' => 'warning',
154 'message' => $e->getMessage(),
155 ];
156 }
157
158 $this->license->activated = false;
159 $this->license->activation_key = '';
160 }
161
162 /**
163 * Activate the license
164 */
165 protected function activate_license()
166 {
167 try {
168 $activation = $this->api->activate_license();
169 } catch (API_Exception $e) {
170 $message = $e->getMessage();
171
172 switch ($e->getApiCode()) {
173 case 'license_at_limit':
174 $message .= ' You can <a href="https://my.boxzillaplugin.com/licenses">manage your site activations here</a>.';
175 break;
176
177 case 'license_expired':
178 $message .= ' Please <a href="https://my.boxzillaplugin.com/licenses">renew your license here</a>.';
179 break;
180 }
181
182 $this->notices[] = [
183 'type' => 'warning',
184 'message' => $message,
185 ];
186 return;
187 }
188
189 $this->license->activation_key = $activation->token;
190 $this->license->activated = true;
191
192 $this->notices[] = [
193 'type' => 'info',
194 'message' => 'Your license was successfully activated!',
195 ];
196 }
197
198 /**
199 * Shows the license form
200 */
201 public function show_license_form()
202 {
203 $license = $this->license;
204 $extensions = $this->extensions;
205 $notices = $this->notices;
206 require __DIR__ . '/views/license-form.php';
207 }
208 }
209