Completed
Push — 249-fix/log-buddy-press-emails ( 49b806...80a150 )
by Sudar
07:06 queued 03:38
created

Licenser::render_bundle_license_form()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 57
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 47
c 1
b 0
f 0
nc 12
nop 0
dl 0
loc 57
rs 8.8452

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 ) {
0 ignored issues
show
introduced by
$updater is always a sub-type of EmailLog\Addon\API\EDDUpdater.
Loading history...
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
107
		$expiry_details       = '';
108
		$expiry_details_class = '';
109
110
		if ( $this->is_bundle_license_valid() ) {
111
			$action       = 'el_bundle_license_deactivate';
112
			$action_text  = __( 'Deactivate', 'email-log' );
113
			$button_class = '';
114
			$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

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