Completed
Push — master ( bd97eb...77fdab )
by Sudar
08:35
created

Licenser::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 4
nop 2
dl 0
loc 12
ccs 0
cts 10
cp 0
crap 12
rs 9.4285
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 null|\EmailLog\Addon\License\BundleLicense $bundle_license Optional. Bundle License.
46
	 * @param null|\EmailLog\Addon\AddonList             $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 ) {
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
		$expires = '';
106
107
		if ( $this->is_bundle_license_valid() ) {
108
			$action       = 'el_bundle_license_deactivate';
109
			$action_text  = __( 'Deactivate', 'email-log' );
110
			$button_class = '';
111
			$expiry_date = date( 'F d, Y', strtotime( $this->get_bundle_license_expiry_date() ) );
112
			$expires = sprintf( __( 'Your license expires on %s', 'email-log' ), $expiry_date );
113
		}
114
		?>
115
116
		<div class="bundle-license">
117
			<?php if ( ! $this->is_bundle_license_valid() ) : ?>
118
				<p class="notice notice-warning">
119
					<?php
120
					printf(
121
						__( "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' ),
122
						'https://wpemaillog.com'
123
					);
124
					?>
125
				</p>
126
			<?php endif; ?>
127
128
			<form method="post">
129
				<input type="text" name="el-license" class="el-license" size="40"
130
					   title="<?php _e( 'Email Log Bundle License Key', 'email-log' ); ?>"
131
					   placeholder="<?php _e( 'Email Log Bundle License Key', 'email-log' ); ?>"
132
					   value="<?php echo esc_attr( $this->bundle_license->get_license_key() ); ?>">
133
134
				<input type="submit" class="button button-large <?php echo sanitize_html_class( $button_class ); ?>"
135
					   value="<?php echo esc_attr( $action_text ); ?>">
136
137
				<p class="expires"><?php echo esc_html( $expires ); ?></p>
138
139
				<input type="hidden" name="el-action" value="<?php echo esc_attr( $action ); ?>">
140
141
				<?php wp_nonce_field( $action, $action . '_nonce' ); ?>
142
			</form>
143
		</div>
144
		<?php
145
	}
146
147
	/**
148
	 * Activate Bundle License.
149
	 *
150
	 * @param array $request Request Object.
151
	 */
152
	public function activate_bundle_license( $request ) {
153
		$license_key = sanitize_text_field( $request['el-license'] );
154
155
		$this->bundle_license->set_license_key( $license_key );
156
157
		try {
158
			$this->bundle_license->activate();
159
			$message = __( 'Your license has been activated. You can now install add-ons, will receive automatic updates and access to email support.', 'email-log' );
160
			$type    = 'updated';
161
		} catch ( \Exception $e ) {
162
			$message = $e->getMessage();
163
			$type    = 'error';
164
		}
165
166
		add_settings_error( 'bundle-license', 'bundle-license', $message, $type );
167
	}
168
169
	/**
170
	 * Deactivate Bundle License.
171
	 */
172
	public function deactivate_bundle_license() {
173
		try {
174
			$this->bundle_license->deactivate();
175
			$message = __( 'Your license has been deactivated. You will not receive automatic updates.', 'email-log' );
176
			$type    = 'updated';
177
		} catch ( \Exception $e ) {
178
			$message = $e->getMessage();
179
			$type    = 'error';
180
		}
181
182
		add_settings_error( 'bundle-license', 'bundle-license', $message, $type );
183
	}
184
185
	/**
186
	 * Is the bundle license valid?
187
	 *
188
	 * @return bool True, if Bundle License is active, False otherwise.
189
	 */
190
	public function is_bundle_license_valid() {
191
		return $this->bundle_license->is_valid();
192
	}
193
194
	/**
195
	 * Get the expiry date of the Bundle License.
196
	 *
197
	 * @return string Expiry date.
0 ignored issues
show
Documentation introduced by
Should the return type not be string|false?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
198
	 */
199
	protected function get_bundle_license_expiry_date() {
200
		return $this->bundle_license->get_expiry_date();
201
	}
202
203
	/**
204
	 * Activate individual add-on License.
205
	 *
206
	 * @param array $request Request Array.
207
	 */
208
	public function activate_addon_license( $request ) {
209
		$license_key = sanitize_text_field( $request['el-license'] );
210
		$addon_name  = sanitize_text_field( $request['el-addon'] );
211
212
		$license = $this->addon_list->get_addon_by_name( $addon_name )->get_license();
213
		$license->set_license_key( $license_key );
214
215
		try {
216
			$license->activate();
217
			$message = sprintf(
218
				__( 'Your license for %s has been activated. You will receive automatic updates and access to email support.', 'email-log' ),
219
				$addon_name
220
			);
221
			$type    = 'updated';
222
		} catch ( \Exception $e ) {
223
			$message = $e->getMessage();
224
			$type    = 'error';
225
		}
226
227
		add_settings_error( 'addon-license', 'addon-license', $message, $type );
228
	}
229
230
	/**
231
	 * Deactivate individual add-on License.
232
	 *
233
	 * @param array $request Request Array.
234
	 */
235
	public function deactivate_addon_license( $request ) {
236
		$license_key = sanitize_text_field( $request['el-license'] );
237
		$addon_name  = sanitize_text_field( $request['el-addon'] );
238
239
		$license = $this->addon_list->get_addon_by_name( $addon_name )->get_license();
240
		$license->set_license_key( $license_key );
241
242
		try {
243
			$license->deactivate();
244
			$message = sprintf(
245
				__( 'Your license for %s has been deactivated. You will not receive automatic updates.', 'email-log' ),
246
				$addon_name
247
			);
248
			$type    = 'updated';
249
		} catch ( \Exception $e ) {
250
			$message = $e->getMessage();
251
			$type    = 'error';
252
		}
253
254
		add_settings_error( 'addon-license', 'addon-license', $message, $type );
255
	}
256
257
	/**
258
	 * Get the license key of an add-on.
259
	 *
260
	 * @param string $addon_name Addon.
261
	 *
262
	 * @return bool|string License key if found, False otherwise.
263
	 */
264
	public function get_addon_license_key( $addon_name ) {
265
		if ( $this->is_bundle_license_valid() ) {
266
			return $this->bundle_license->get_addon_license_key( $addon_name );
267
		}
268
269
		return $this->addon_list->get_addon_by_name( $addon_name )->get_addon_license_key();
270
	}
271
272
	/**
273
	 * Get the Download URL of an add-on.
274
	 *
275
	 * @param string $addon_slug Add-on slug.
276
	 *
277
	 * @return string Download URL.
278
	 */
279
	public function get_addon_download_url( $addon_slug ) {
280
		if ( isset( $this->updaters[ $addon_slug ] ) ) {
281
			return $this->updaters[ $addon_slug ]->get_download_url();
282
		}
283
284
		return '';
285
	}
286
}
287