Completed
Push — 212-fix/add-upsell-notice ( a1169d...441743 )
by Sudar
10:20 queued 06:54
created

Licenser::is_addon_installed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php namespace EmailLog\Addon\License;
2
3
use EmailLog\Addon\AddonList;
4
use EmailLog\Addon\Upseller;
5
use EmailLog\Addon\API\EDDUpdater;
6
use EmailLog\Core\Loadie;
7
8
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
9
10
/**
11
 * Handles the add-on licensing for Email Log.
12
 *
13
 * There can be one normal license for each add-on or one bundle license for all add-ons.
14
 * This class is final because we don't want other plugins to interfere with Email Log licensing.
15
 *
16
 * @since 2.0.0
17
 */
18
final class Licenser implements Loadie {
19
20
	/**
21
	 * Bundle License object.
22
	 *
23
	 * @var \EmailLog\Addon\License\BundleLicense
24
	 */
25
	private $bundle_license;
26
27
	/**
28
	 * List of Add-on updaters.
29
	 *
30
	 * @var \EmailLog\Addon\API\EDDUpdater[]
31
	 */
32
	private $updaters = array();
33
34
	/**
35
	 * List of add-ons.
36
	 *
37
	 * @var \EmailLog\Addon\AddonList
38
	 */
39
	private $addon_list;
40
41
	/**
42
	 * Licenser constructor.
43
	 * If the bundle_license object is not passed a new object is created.
44
	 * If the addon_list object is not passed a new object is created.
45
	 *
46
	 * @param \EmailLog\Addon\License\BundleLicense|null $bundle_license Optional. Bundle License.
47
	 * @param \EmailLog\Addon\AddonList|null             $addon_list     Optional. Add-on List.
48
	 */
49
	public function __construct( $bundle_license = null, $addon_list = null ) {
50
		if ( ! $bundle_license instanceof BundleLicense ) {
51
			$bundle_license = new BundleLicense();
52
		}
53
54
		if ( ! $addon_list instanceof AddonList ) {
55
			$addon_list = new AddonList();
56
		}
57
58
		$this->bundle_license = $bundle_license;
59
		$this->addon_list     = $addon_list;
60
	}
61
62
	/**
63
	 * Load all Licenser related hooks.
64
	 *
65
	 * @inheritdoc
66
	 */
67
	public function load() {
68
		$this->bundle_license->load();
69
70
		add_action( 'el_before_addon_list', array( $this, 'render_bundle_license_form' ) );
71
		add_action( 'el_before_logs_list_table', array( $this, 'render_more_fields_addon_upsell_message' ) );
72
73
		add_action( 'el_bundle_license_activate', array( $this, 'activate_bundle_license' ) );
74
		add_action( 'el_bundle_license_deactivate', array( $this, 'deactivate_bundle_license' ) );
75
76
		add_action( 'el_license_activate', array( $this, 'activate_addon_license' ) );
77
		add_action( 'el_license_deactivate', array( $this, 'deactivate_addon_license' ) );
78
	}
79
80
	/**
81
	 * Add an Add-on Updater.
82
	 *
83
	 * @param \EmailLog\Addon\API\EDDUpdater $updater Add-on Updater.
84
	 */
85
	public function add_updater( $updater ) {
86
		if ( $updater instanceof EDDUpdater ) {
0 ignored issues
show
introduced by
$updater is always a sub-type of EmailLog\Addon\API\EDDUpdater.
Loading history...
87
			$this->updaters[ $updater->get_slug() ] = $updater;
88
		}
89
	}
90
91
	/**
92
	 * Get list of add-ons.
93
	 *
94
	 * @return \EmailLog\Addon\AddonList Add-on List.
95
	 */
96
	public function get_addon_list() {
97
		return $this->addon_list;
98
	}
99
100
	/**
101
	 * Render the Bundle License Form.
102
	 */
103
	public function render_bundle_license_form() {
104
		$action       = 'el_bundle_license_activate';
105
		$action_text  = __( 'Activate', 'email-log' );
106
		$button_class = 'button-primary';
107
108
		$expiry_details       = '';
109
		$expiry_details_class = '';
110
111
		if ( $this->is_bundle_license_valid() ) {
112
			$action       = 'el_bundle_license_deactivate';
113
			$action_text  = __( 'Deactivate', 'email-log' );
114
			$button_class = '';
115
			$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

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