Passed
Push — 122-star-email-log ( 0e8628...f6bca9 )
by Sudar
20:10 queued 14:52
created

Licenser::deactivate_bundle_license()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 3
nop 0
dl 0
loc 11
ccs 0
cts 10
cp 0
crap 6
rs 10
c 0
b 0
f 0
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
71
		add_action( 'el_bundle_license_activate', array( $this, 'activate_bundle_license' ) );
72
		add_action( 'el_bundle_license_deactivate', array( $this, 'deactivate_bundle_license' ) );
73
74
		add_action( 'el_license_activate', array( $this, 'activate_addon_license' ) );
75
		add_action( 'el_license_deactivate', array( $this, 'deactivate_addon_license' ) );
76
	}
77
78
	/**
79
	 * Add an Add-on Updater.
80
	 *
81
	 * @param \EmailLog\Addon\API\EDDUpdater $updater Add-on Updater.
82
	 */
83
	public function add_updater( $updater ) {
84
		if ( $updater instanceof EDDUpdater ) {
0 ignored issues
show
introduced by
$updater is always a sub-type of EmailLog\Addon\API\EDDUpdater.
Loading history...
85
			$this->updaters[ $updater->get_slug() ] = $updater;
86
		}
87
	}
88
89
	/**
90
	 * Get list of add-ons.
91
	 *
92
	 * @return \EmailLog\Addon\AddonList Add-on List.
93
	 */
94
	public function get_addon_list() {
95
		return $this->addon_list;
96
	}
97
98
	/**
99
	 * Render the Bundle License Form.
100
	 */
101
	public function render_bundle_license_form() {
102
		$action       = 'el_bundle_license_activate';
103
		$action_text  = __( 'Activate', 'email-log' );
104
		$button_class = 'button-primary';
105
106
		$expiry_details       = '';
107
		$expiry_details_class = '';
108
109
		if ( $this->is_bundle_license_valid() ) {
110
			$action       = 'el_bundle_license_deactivate';
111
			$action_text  = __( 'Deactivate', 'email-log' );
112
			$button_class = '';
113
			$expiry_date  = date( 'F d, Y', strtotime( $this->get_bundle_license_expiry_date() ) );
0 ignored issues
show
Bug introduced by
It seems like $this->get_bundle_license_expiry_date() can also be of type false; however, parameter $time of strtotime() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

113
			$expiry_date  = date( 'F d, Y', strtotime( /** @scrutinizer ignore-type */ $this->get_bundle_license_expiry_date() ) );
Loading history...
114
115
			if ( $this->bundle_license->has_expired() ) {
116
				/* translators: 1 License expiry date, 2 License Renewal link */
117
				$expiry_details       = sprintf( __( 'Your license has expired on %1$s. Please <a href="%2$s">renew it</a> to receive automatic updates and support.', 'email-log' ), $expiry_date, esc_url( $this->bundle_license->get_renewal_link() ) );
118
				$expiry_details_class = 'notice notice-warning';
119
			} else {
120
				/* translators: 1 License expiry date */
121
				$expiry_details       = sprintf( __( 'Your license is valid till %s', 'email-log' ), $expiry_date );
122
				$expiry_details_class = 'expires';
123
			}
124
		}
125
		?>
126
127
		<div class="bundle-license">
128
			<?php if ( ! $this->is_bundle_license_valid() ) : ?>
129
				<p class="notice notice-warning">
130
					<?php
131
					printf(
132
						__( "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' ),
133
						'https://wpemaillog.com/store/?utm_campaign=Upsell&utm_medium=wpadmin&utm_source=notice&utm_content=buy-it'
134
					);
135
					?>
136
				</p>
137
			<?php endif; ?>
138
139
			<form method="post">
140
				<input type="text" name="el-license" class="el-license" size="40"
141
					   title="<?php _e( 'Email Log Bundle License Key', 'email-log' ); ?>"
142
					   placeholder="<?php _e( 'Email Log Bundle License Key', 'email-log' ); ?>"
143
					   value="<?php echo esc_attr( $this->bundle_license->get_license_key() ); ?>">
144
145
				<input type="submit" class="button <?php echo sanitize_html_class( $button_class ); ?>"
146
					   value="<?php echo esc_attr( $action_text ); ?>">
147
148
				<?php if ( ! empty( $expiry_details ) ) : ?>
149
					<p class="<?php echo esc_attr( $expiry_details_class ); ?>">
150
						<?php echo $expiry_details; ?>
151
					</p>
152
				<?php endif; ?>
153
154
				<input type="hidden" name="el-action" value="<?php echo esc_attr( $action ); ?>">
155
156
				<?php wp_nonce_field( $action, $action . '_nonce' ); ?>
157
			</form>
158
		</div>
159
		<?php
160
	}
161
162
	/**
163
	 * Activate Bundle License.
164
	 *
165
	 * @param array $request Request Object.
166
	 */
167
	public function activate_bundle_license( $request ) {
168
		$license_key = sanitize_text_field( $request['el-license'] );
169
170
		$this->bundle_license->set_license_key( $license_key );
171
172
		try {
173
			$this->bundle_license->activate();
174
			$message = __( 'Your license has been activated. You can now install add-ons, will receive automatic updates and access to email support.', 'email-log' );
175
			$type    = 'updated';
176
		} catch ( \Exception $e ) {
177
			$message = $e->getMessage();
178
			$type    = 'error';
179
		}
180
181
		add_settings_error( 'bundle-license', 'bundle-license', $message, $type );
182
	}
183
184
	/**
185
	 * Deactivate Bundle License.
186
	 */
187
	public function deactivate_bundle_license() {
188
		try {
189
			$this->bundle_license->deactivate();
190
			$message = __( 'Your license has been deactivated. You will not receive automatic updates.', 'email-log' );
191
			$type    = 'updated';
192
		} catch ( \Exception $e ) {
193
			$message = $e->getMessage();
194
			$type    = 'error';
195
		}
196
197
		add_settings_error( 'bundle-license', 'bundle-license', $message, $type );
198
	}
199
200
	/**
201
	 * Is the bundle license valid?
202
	 *
203
	 * @return bool True, if Bundle License is active, False otherwise.
204
	 */
205
	public function is_bundle_license_valid() {
206
		return $this->bundle_license->is_valid();
207
	}
208
209
	/**
210
	 * Get the expiry date of the Bundle License.
211
	 *
212
	 * @return false|string Expiry date, False if license is not valid.
213
	 */
214
	protected function get_bundle_license_expiry_date() {
215
		return $this->bundle_license->get_expiry_date();
216
	}
217
218
	/**
219
	 * Activate individual add-on License.
220
	 *
221
	 * @param array $request Request Array.
222
	 */
223
	public function activate_addon_license( $request ) {
224
		$license_key = sanitize_text_field( $request['el-license'] );
225
		$addon_name  = sanitize_text_field( $request['el-addon'] );
226
227
		$license = $this->addon_list->get_addon_by_name( $addon_name )->get_license();
228
		$license->set_license_key( $license_key );
229
230
		try {
231
			$license->activate();
232
			$message = sprintf(
233
				__( 'Your license for %s has been activated. You will receive automatic updates and access to email support.', 'email-log' ),
234
				$addon_name
235
			);
236
			$type = 'updated';
237
		} catch ( \Exception $e ) {
238
			$message = $e->getMessage();
239
			$type    = 'error';
240
		}
241
242
		add_settings_error( 'addon-license', 'addon-license', $message, $type );
243
	}
244
245
	/**
246
	 * Deactivate individual add-on License.
247
	 *
248
	 * @param array $request Request Array.
249
	 */
250
	public function deactivate_addon_license( $request ) {
251
		$license_key = sanitize_text_field( $request['el-license'] );
252
		$addon_name  = sanitize_text_field( $request['el-addon'] );
253
254
		$license = $this->addon_list->get_addon_by_name( $addon_name )->get_license();
255
		$license->set_license_key( $license_key );
256
257
		try {
258
			$license->deactivate();
259
			$message = sprintf(
260
				__( 'Your license for %s has been deactivated. You will not receive automatic updates.', 'email-log' ),
261
				$addon_name
262
			);
263
			$type = 'updated';
264
		} catch ( \Exception $e ) {
265
			$message = $e->getMessage();
266
			$type    = 'error';
267
		}
268
269
		add_settings_error( 'addon-license', 'addon-license', $message, $type );
270
	}
271
272
	/**
273
	 * Get the license key of an add-on.
274
	 *
275
	 * @param string $addon_name Addon.
276
	 *
277
	 * @return bool|string License key if found, False otherwise.
278
	 */
279
	public function get_addon_license_key( $addon_name ) {
280
		if ( $this->is_bundle_license_valid() ) {
281
			return $this->bundle_license->get_addon_license_key( $addon_name );
282
		}
283
284
		$addon = $this->addon_list->get_addon_by_name( $addon_name );
285
286
		if ( ! $addon ) {
287
			return false;
288
		}
289
290
		return $addon->get_addon_license_key();
291
	}
292
293
	/**
294
	 * Get the Download URL of an add-on.
295
	 *
296
	 * @param string $addon_slug Add-on slug.
297
	 *
298
	 * @return string Download URL.
299
	 */
300
	public function get_addon_download_url( $addon_slug ) {
301
		if ( isset( $this->updaters[ $addon_slug ] ) ) {
302
			return $this->updaters[ $addon_slug ]->get_download_url();
303
		}
304
305
		return '';
306
	}
307
308
	/**
309
	 * Is an add-on active?
310
	 *
311
	 * @since 2.4.0
312
	 *
313
	 * @param string $addon_name Add-on name.
314
	 *
315
	 * @return bool True if add-on is present and is active, false otherwise.
316
	 */
317
	public function is_addon_active( $addon_name ) {
318
		return $this->addon_list->is_addon_active( $addon_name );
319
	}
320
321
	/**
322
	 * Is an add-on installed?
323
	 *
324
	 * @since 2.4.0
325
	 *
326
	 * @param string $addon_name Add-on name.
327
	 *
328
	 * @return bool True if add-on is present and is installed, false otherwise.
329
	 */
330
	public function is_addon_installed( $addon_name ) {
331
		return $this->addon_list->is_addon_installed( $addon_name );
332
	}
333
334
	/**
335
	 * Return the bundle license.
336
	 *
337
	 * @return \EmailLog\Addon\License\BundleLicense|null Bundle license or null if no bundle license.
338
	 */
339
	public function get_bundle_license() {
340
		return $this->bundle_license;
341
	}
342
}
343