1
|
|
|
<?php namespace EmailLog\Addon\License; |
2
|
|
|
|
3
|
|
|
use EmailLog\Addon\AddonList; |
4
|
|
|
use EmailLog\Addon\API\EDDUpdater; |
5
|
|
|
use EmailLog\Core\Loadie; |
6
|
|
|
|
7
|
|
|
defined( 'ABSPATH' ) || exit; // Exit if accessed directly. |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Handles the add-on licensing for Email Log. |
11
|
|
|
* |
12
|
|
|
* There can be one normal license for each add-on or one bundle license for all add-ons. |
13
|
|
|
* This class is final because we don't want other plugins to interfere with Email Log licensing. |
14
|
|
|
* |
15
|
|
|
* @since 2.0.0 |
16
|
|
|
*/ |
17
|
|
|
final class Licenser implements Loadie { |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Bundle License object. |
21
|
|
|
* |
22
|
|
|
* @var \EmailLog\Addon\License\BundleLicense |
23
|
|
|
*/ |
24
|
|
|
private $bundle_license; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* List of Add-on updaters. |
28
|
|
|
* |
29
|
|
|
* @var \EmailLog\Addon\API\EDDUpdater[] |
30
|
|
|
*/ |
31
|
|
|
private $updaters = array(); |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* List of add-ons. |
35
|
|
|
* |
36
|
|
|
* @var \EmailLog\Addon\AddonList |
37
|
|
|
*/ |
38
|
|
|
private $addon_list; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Licenser constructor. |
42
|
|
|
* If the bundle_license object is not passed a new object is created. |
43
|
|
|
* If the addon_list object is not passed a new object is created. |
44
|
|
|
* |
45
|
|
|
* @param \EmailLog\Addon\License\BundleLicense|null $bundle_license Optional. Bundle License. |
46
|
|
|
* @param \EmailLog\Addon\AddonList|null $addon_list Optional. Add-on List. |
47
|
|
|
*/ |
48
|
|
|
public function __construct( $bundle_license = null, $addon_list = null ) { |
49
|
|
|
if ( ! $bundle_license instanceof BundleLicense ) { |
50
|
|
|
$bundle_license = new BundleLicense(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if ( ! $addon_list instanceof AddonList ) { |
54
|
|
|
$addon_list = new AddonList(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$this->bundle_license = $bundle_license; |
58
|
|
|
$this->addon_list = $addon_list; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Load all Licenser related hooks. |
63
|
|
|
* |
64
|
|
|
* @inheritdoc |
65
|
|
|
*/ |
66
|
|
|
public function load() { |
67
|
|
|
$this->bundle_license->load(); |
68
|
|
|
|
69
|
|
|
add_action( 'el_before_addon_list', array( $this, 'render_bundle_license_form' ) ); |
70
|
|
|
add_action( 'el_before_logs_list_table', array( $this, 'render_more_fields_addon_upsell_message' ) ); |
71
|
|
|
|
72
|
|
|
add_action( 'el_bundle_license_activate', array( $this, 'activate_bundle_license' ) ); |
73
|
|
|
add_action( 'el_bundle_license_deactivate', array( $this, 'deactivate_bundle_license' ) ); |
74
|
|
|
|
75
|
|
|
add_action( 'el_license_activate', array( $this, 'activate_addon_license' ) ); |
76
|
|
|
add_action( 'el_license_deactivate', array( $this, 'deactivate_addon_license' ) ); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Add an Add-on Updater. |
81
|
|
|
* |
82
|
|
|
* @param \EmailLog\Addon\API\EDDUpdater $updater Add-on Updater. |
83
|
|
|
*/ |
84
|
|
|
public function add_updater( $updater ) { |
85
|
|
|
if ( $updater instanceof EDDUpdater ) { |
86
|
|
|
$this->updaters[ $updater->get_slug() ] = $updater; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get list of add-ons. |
92
|
|
|
* |
93
|
|
|
* @return \EmailLog\Addon\AddonList Add-on List. |
94
|
|
|
*/ |
95
|
|
|
public function get_addon_list() { |
96
|
|
|
return $this->addon_list; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Render the Bundle License Form. |
101
|
|
|
*/ |
102
|
|
|
public function render_bundle_license_form() { |
103
|
|
|
$action = 'el_bundle_license_activate'; |
104
|
|
|
$action_text = __( 'Activate', 'email-log' ); |
105
|
|
|
$button_class = 'button-primary'; |
106
|
|
|
$expires = ''; |
107
|
|
|
|
108
|
|
|
if ( $this->is_bundle_license_valid() ) { |
109
|
|
|
$action = 'el_bundle_license_deactivate'; |
110
|
|
|
$action_text = __( 'Deactivate', 'email-log' ); |
111
|
|
|
$button_class = ''; |
112
|
|
|
$expiry_date = date( 'F d, Y', strtotime( $this->get_bundle_license_expiry_date() ) ); |
113
|
|
|
$expires = sprintf( __( 'Your license expires on %s', 'email-log' ), $expiry_date ); |
114
|
|
|
} |
115
|
|
|
?> |
116
|
|
|
|
117
|
|
|
<div class="bundle-license"> |
118
|
|
|
<?php if ( ! $this->is_bundle_license_valid() ) : ?> |
119
|
|
|
<p class="notice notice-warning"> |
120
|
|
|
<?php |
121
|
|
|
printf( |
122
|
|
|
__( "Enter your license key to activate add-ons. If you don't have a license, then you can <a href='%s' target='_blank'>buy it</a>", 'email-log' ), |
123
|
|
|
'https://wpemaillog.com/store/?utm_campaign=Upsell&utm_medium=wpadmin&utm_source=notice&utm_content=buy-it' |
124
|
|
|
); |
125
|
|
|
?> |
126
|
|
|
</p> |
127
|
|
|
<?php endif; ?> |
128
|
|
|
|
129
|
|
|
<form method="post"> |
130
|
|
|
<input type="text" name="el-license" class="el-license" size="40" |
131
|
|
|
title="<?php _e( 'Email Log Bundle License Key', 'email-log' ); ?>" |
132
|
|
|
placeholder="<?php _e( 'Email Log Bundle License Key', 'email-log' ); ?>" |
133
|
|
|
value="<?php echo esc_attr( $this->bundle_license->get_license_key() ); ?>"> |
134
|
|
|
|
135
|
|
|
<input type="submit" class="button button-large <?php echo sanitize_html_class( $button_class ); ?>" |
136
|
|
|
value="<?php echo esc_attr( $action_text ); ?>"> |
137
|
|
|
|
138
|
|
|
<p class="expires"><?php echo esc_html( $expires ); ?></p> |
139
|
|
|
|
140
|
|
|
<input type="hidden" name="el-action" value="<?php echo esc_attr( $action ); ?>"> |
141
|
|
|
|
142
|
|
|
<?php wp_nonce_field( $action, $action . '_nonce' ); ?> |
143
|
|
|
</form> |
144
|
|
|
</div> |
145
|
|
|
<?php |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Renders Upsell message for More Fields add-on. |
150
|
|
|
* |
151
|
|
|
* @since 2.2.5 |
152
|
|
|
*/ |
153
|
|
|
public function render_more_fields_addon_upsell_message() { |
154
|
|
|
echo '<span id = "el-pro-msg">'; |
155
|
|
|
_e( 'Additional fields are available through More Fields add-on. ', 'email-log' ); |
156
|
|
|
|
157
|
|
|
if ( $this->is_bundle_license_valid() ) { |
158
|
|
|
echo '<a href="admin.php?page=email-log-addons">'; |
159
|
|
|
_e( 'Install it', 'email-log' ); |
160
|
|
|
echo '</a>'; |
161
|
|
|
} else { |
162
|
|
|
echo '<a href="https://wpemaillog.com/addons/more-fields/?utm_campaign=Upsell&utm_medium=wpadmin&utm_source=inline&utm_content=mf" style="color:red">'; |
163
|
|
|
_e( 'Buy Now', 'email-log' ); |
164
|
|
|
echo '</a>'; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
echo '</span>'; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Activate Bundle License. |
172
|
|
|
* |
173
|
|
|
* @param array $request Request Object. |
174
|
|
|
*/ |
175
|
|
|
public function activate_bundle_license( $request ) { |
176
|
|
|
$license_key = sanitize_text_field( $request['el-license'] ); |
177
|
|
|
|
178
|
|
|
$this->bundle_license->set_license_key( $license_key ); |
179
|
|
|
|
180
|
|
|
try { |
181
|
|
|
$this->bundle_license->activate(); |
182
|
|
|
$message = __( 'Your license has been activated. You can now install add-ons, will receive automatic updates and access to email support.', 'email-log' ); |
183
|
|
|
$type = 'updated'; |
184
|
|
|
} catch ( \Exception $e ) { |
185
|
|
|
$message = $e->getMessage(); |
186
|
|
|
$type = 'error'; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
add_settings_error( 'bundle-license', 'bundle-license', $message, $type ); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Deactivate Bundle License. |
194
|
|
|
*/ |
195
|
|
|
public function deactivate_bundle_license() { |
196
|
|
|
try { |
197
|
|
|
$this->bundle_license->deactivate(); |
198
|
|
|
$message = __( 'Your license has been deactivated. You will not receive automatic updates.', 'email-log' ); |
199
|
|
|
$type = 'updated'; |
200
|
|
|
} catch ( \Exception $e ) { |
201
|
|
|
$message = $e->getMessage(); |
202
|
|
|
$type = 'error'; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
add_settings_error( 'bundle-license', 'bundle-license', $message, $type ); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Is the bundle license valid? |
210
|
|
|
* |
211
|
|
|
* @return bool True, if Bundle License is active, False otherwise. |
212
|
|
|
*/ |
213
|
|
|
public function is_bundle_license_valid() { |
214
|
|
|
return $this->bundle_license->is_valid(); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Get the expiry date of the Bundle License. |
219
|
|
|
* |
220
|
|
|
* @return false|string Expiry date, False if license is not valid. |
221
|
|
|
*/ |
222
|
|
|
protected function get_bundle_license_expiry_date() { |
223
|
|
|
return $this->bundle_license->get_expiry_date(); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Activate individual add-on License. |
228
|
|
|
* |
229
|
|
|
* @param array $request Request Array. |
230
|
|
|
*/ |
231
|
|
|
public function activate_addon_license( $request ) { |
232
|
|
|
$license_key = sanitize_text_field( $request['el-license'] ); |
233
|
|
|
$addon_name = sanitize_text_field( $request['el-addon'] ); |
234
|
|
|
|
235
|
|
|
$license = $this->addon_list->get_addon_by_name( $addon_name )->get_license(); |
236
|
|
|
$license->set_license_key( $license_key ); |
237
|
|
|
|
238
|
|
|
try { |
239
|
|
|
$license->activate(); |
240
|
|
|
$message = sprintf( |
241
|
|
|
__( 'Your license for %s has been activated. You will receive automatic updates and access to email support.', 'email-log' ), |
242
|
|
|
$addon_name |
243
|
|
|
); |
244
|
|
|
$type = 'updated'; |
245
|
|
|
} catch ( \Exception $e ) { |
246
|
|
|
$message = $e->getMessage(); |
247
|
|
|
$type = 'error'; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
add_settings_error( 'addon-license', 'addon-license', $message, $type ); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Deactivate individual add-on License. |
255
|
|
|
* |
256
|
|
|
* @param array $request Request Array. |
257
|
|
|
*/ |
258
|
|
|
public function deactivate_addon_license( $request ) { |
259
|
|
|
$license_key = sanitize_text_field( $request['el-license'] ); |
260
|
|
|
$addon_name = sanitize_text_field( $request['el-addon'] ); |
261
|
|
|
|
262
|
|
|
$license = $this->addon_list->get_addon_by_name( $addon_name )->get_license(); |
263
|
|
|
$license->set_license_key( $license_key ); |
264
|
|
|
|
265
|
|
|
try { |
266
|
|
|
$license->deactivate(); |
267
|
|
|
$message = sprintf( |
268
|
|
|
__( 'Your license for %s has been deactivated. You will not receive automatic updates.', 'email-log' ), |
269
|
|
|
$addon_name |
270
|
|
|
); |
271
|
|
|
$type = 'updated'; |
272
|
|
|
} catch ( \Exception $e ) { |
273
|
|
|
$message = $e->getMessage(); |
274
|
|
|
$type = 'error'; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
add_settings_error( 'addon-license', 'addon-license', $message, $type ); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Get the license key of an add-on. |
282
|
|
|
* |
283
|
|
|
* @param string $addon_name Addon. |
284
|
|
|
* |
285
|
|
|
* @return bool|string License key if found, False otherwise. |
286
|
|
|
*/ |
287
|
|
|
public function get_addon_license_key( $addon_name ) { |
288
|
|
|
if ( $this->is_bundle_license_valid() ) { |
289
|
|
|
return $this->bundle_license->get_addon_license_key( $addon_name ); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
$addon = $this->addon_list->get_addon_by_name( $addon_name ); |
293
|
|
|
|
294
|
|
|
if ( ! $addon ) { |
295
|
|
|
return false; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
return $addon->get_addon_license_key(); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Get the Download URL of an add-on. |
303
|
|
|
* |
304
|
|
|
* @param string $addon_slug Add-on slug. |
305
|
|
|
* |
306
|
|
|
* @return string Download URL. |
307
|
|
|
*/ |
308
|
|
|
public function get_addon_download_url( $addon_slug ) { |
309
|
|
|
if ( isset( $this->updaters[ $addon_slug ] ) ) { |
310
|
|
|
return $this->updaters[ $addon_slug ]->get_download_url(); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
return ''; |
314
|
|
|
} |
315
|
|
|
} |
316
|
|
|
|