Completed
Push — master ( 053e09...4ba1a0 )
by Sudar
02:24
created

Licenser::is_addon_license_valid()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 4
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 8
rs 9.4285
1
<?php namespace EmailLog\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\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\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
106
		if ( $this->is_bundle_license_valid() ) {
107
			$action       = 'el_bundle_license_deactivate';
108
			$action_text  = __( 'Deactivate', 'email-log' );
109
			$button_class = '';
110
		}
111
		?>
112
113
		<div class="bundle-license">
114
			<?php if ( ! $this->is_bundle_license_valid() ) : ?>
115
				<p class="notice notice-warning">
116
					<?php
117
					printf(
118
						__( "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' ),
119
						'https://wpemaillog.com'
120
					);
121
					?>
122
				</p>
123
			<?php endif; ?>
124
125
			<form method="post">
126
				<input type="text" name="el-license" class="el-license" size="40"
127
				       title="<?php _e( 'Email Log Bundle License Key', 'email-log' ); ?>"
128
				       placeholder="<?php _e( 'Email Log Bundle License Key', 'email-log' ); ?>"
129
				       value="<?php echo esc_attr( $this->bundle_license->get_license_key() ); ?>">
130
131
				<input type="submit" class="button button-large <?php echo sanitize_html_class( $button_class ); ?>"
132
				       value="<?php echo esc_attr( $action_text ); ?>">
133
134
				<input type="hidden" name="el-action" value="<?php echo esc_attr( $action ); ?>">
135
136
				<?php wp_nonce_field( $action, $action . '_nonce' ); ?>
137
			</form>
138
		</div>
139
		<?php
140
	}
141
142
	/**
143
	 * Activate Bundle License.
144
	 *
145
	 * @param array $request Request Object.
146
	 */
147
	public function activate_bundle_license( $request ) {
148
		$license_key = sanitize_text_field( $request['el-license'] );
149
150
		$this->bundle_license->set_license_key( $license_key );
151
152
		try {
153
			$this->bundle_license->activate();
154
			$message = __( 'Your license has been activated. You can now install add-ons, will receive automatic updates and access to email support.', 'email-log' );
155
			$type    = 'updated';
156
		} catch ( \Exception $e ) {
157
			$message = $e->getMessage();
158
			$type    = 'error';
159
		}
160
161
		add_settings_error( 'bundle-license', 'bundle-license', $message, $type );
162
	}
163
164
	/**
165
	 * Deactivate Bundle License.
166
	 */
167
	public function deactivate_bundle_license() {
168
		try {
169
			$this->bundle_license->deactivate();
170
			$message = __( 'Your license has been deactivated. You will not receive automatic updates.', 'email-log' );
171
			$type    = 'updated';
172
		} catch ( \Exception $e ) {
173
			$message = $e->getMessage();
174
			$type    = 'error';
175
		}
176
177
		add_settings_error( 'bundle-license', 'bundle-license', $message, $type );
178
	}
179
180
	/**
181
	 * Is the bundle license valid?
182
	 *
183
	 * @return bool True, if Bundle License is active, False otherwise.
184
	 */
185
	public function is_bundle_license_valid() {
186
		return $this->bundle_license->is_valid();
187
	}
188
189
	/**
190
	 * Activate individual add-on License.
191
	 *
192
	 * @param array $request Request Array.
193
	 */
194
	public function activate_addon_license( $request ) {
195
		$license_key = sanitize_text_field( $request['el-license'] );
196
		$addon_name  = sanitize_text_field( $request['el-addon'] );
197
198
		$license = $this->addon_list->get_addon_by_name( $addon_name )->get_license();
199
		$license->set_license_key( $license_key );
200
201
		try {
202
			$license->activate();
203
			$message = sprintf(
204
				__( 'Your license for %s has been activated. You will receive automatic updates and access to email support.', 'email-log' ),
205
				$addon_name
206
			);
207
			$type    = 'updated';
208
		} catch ( \Exception $e ) {
209
			$message = $e->getMessage();
210
			$type    = 'error';
211
		}
212
213
		add_settings_error( 'addon-license', 'addon-license', $message, $type );
214
	}
215
216
	/**
217
	 * Deactivate individual add-on License.
218
	 *
219
	 * @param array $request Request Array.
220
	 */
221
	public function deactivate_addon_license( $request ) {
222
		$license_key = sanitize_text_field( $request['el-license'] );
223
		$addon_name  = sanitize_text_field( $request['el-addon'] );
224
225
		$license = $this->addon_list->get_addon_by_name( $addon_name )->get_license();
226
		$license->set_license_key( $license_key );
227
228
		try {
229
			$license->deactivate();
230
			$message = sprintf(
231
				__( 'Your license for %s has been deactivated. You will not receive automatic updates.', 'email-log' ),
232
				$addon_name
233
			);
234
			$type    = 'updated';
235
		} catch ( \Exception $e ) {
236
			$message = $e->getMessage();
237
			$type    = 'error';
238
		}
239
240
		add_settings_error( 'addon-license', 'addon-license', $message, $type );
241
	}
242
243
	/**
244
	 * Get the license key of an add-on.
245
	 *
246
	 * @param string $addon_name Addon.
247
	 *
248
	 * @return bool|string License key if found, False otherwise.
249
	 */
250
	public function get_addon_license_key( $addon_name ) {
251
		if ( $this->is_bundle_license_valid() ) {
252
			return $this->bundle_license->get_addon_license_key( $addon_name );
253
		}
254
255
		return $this->addon_list->get_addon_by_name( $addon_name )->get_addon_license_key();
256
	}
257
258
	/**
259
	 * Get the Download URL of an add-on.
260
	 *
261
	 * @param string $addon_slug Add-on slug.
262
	 *
263
	 * @return string Download URL.
264
	 */
265
	public function get_addon_download_url( $addon_slug ) {
266
		if ( isset( $this->updaters[ $addon_slug ] ) ) {
267
			return $this->updaters[ $addon_slug ]->get_download_url();
268
		}
269
270
		return '';
271
	}
272
}
273