Completed
Push — master ( cc7d18...eaea62 )
by Sudar
02:25
created

Addon   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 327
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 4
Bugs 0 Features 3
Metric Value
c 4
b 0
f 3
dl 0
loc 327
ccs 0
cts 144
cp 0
rs 9.8
wmc 31
lcom 1
cbo 3

19 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 3
A render() 0 18 1
A __get() 0 7 2
A get_license() 0 3 1
A print_actions() 0 15 3
A print_valid_actions() 0 22 3
A get_download_button_js() 0 7 1
A print_invalid_actions() 0 13 2
B render_individual_license() 0 44 2
A is_installed() 0 5 1
A get_version() 0 9 2
A is_active() 0 3 1
A get_activate_url() 0 3 1
A get_install_url() 0 3 1
A get_download_url() 0 9 2
A has_valid_bundle_license() 0 9 2
A has_valid_addon_license() 0 3 1
A get_addon_license_key() 0 3 1
A parse_data() 0 10 1
1
<?php namespace EmailLog\Addon;
2
3
use EmailLog\Addon\License\AddonLicense;
4
5
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
6
7
/**
8
 * Encapsulate Add-on Data.
9
 *
10
 * @since 2.0.0
11
 *
12
 * @property-read string $name
13
 * @property-read string $version
14
 * @property-read string $thumbnail
15
 * @property-read string $description
16
 * @property-read string $link
17
 * @property-read string $slug
18
 * @property-read string $file
19
 * @property-read string $author
20
 */
21
class Addon {
22
23
	private $name;
24
	private $version;
25
	private $thumbnail;
26
	private $description;
27
	private $link;
28
	private $slug;
29
	private $file;
30
	private $author;
31
32
	protected $email_log;
33
	protected $license;
34
35
	/**
36
	 * Construct Addon object from data array.
37
	 *
38
	 * @param array                                     $data      Data array.
39
	 * @param \EmailLog\Addon\License\AddonLicense|null $license   Add-on License.
40
	 * @param \EmailLog\Core\EmailLog|null              $email_log Email Log instance.
41
	 */
42
	public function __construct( $data, $license = null, $email_log = null ) {
43
		$this->parse_data( $data );
44
45
		if ( null === $license ) {
46
			$license = new AddonLicense();
47
			$license->set_addon_name( $this->name );
48
			$license->load();
49
		}
50
		$this->license = $license;
51
52
		if ( null === $email_log ) {
53
			$email_log = email_log();
54
		}
55
		$this->email_log = $email_log;
56
	}
57
58
	/**
59
	 * Render the add-on in Addon list page.
60
	 */
61
	public function render() {
62
		?>
63
		<div class="el-addon">
64
			<h3 class="el-addon-title"> <?php echo esc_html( $this->name ); ?> </h3>
65
66
			<a href="<?php echo esc_url( $this->link ); ?>?utm_campaign=Upsell&utm_medium=wpadmin&utm_source=addon-grid&utm_content=<?php echo $this->name; ?>"
67
			   title="<?php echo esc_attr( $this->name ); ?>">
68
				<img src="<?php echo esc_url( $this->thumbnail ); ?>" class="attachment-showcase wp-post-image"
69
				     alt="<?php echo esc_attr( $this->name ); ?>" title="<?php echo esc_attr( $this->name ); ?>">
70
			</a>
71
72
			<p> <?php echo esc_html( $this->description ); ?> </p>
73
74
			<?php $this->print_actions(); ?>
75
		</div> <!-- .el-addon -->
76
77
		<?php
78
	}
79
80
	/**
81
	 * Have a magic getter instead of having individual getters.
82
	 *
83
	 * @param string $property Property.
84
	 *
85
	 * @return string Value for the property.
86
	 */
87
	public function __get( $property ) {
88
		if ( isset( $this->{$property} ) ) {
89
			return $this->{$property};
90
		}
91
92
		return false;
93
	}
94
95
	/**
96
	 * Get Add-on License object.
97
	 *
98
	 * @return \EmailLog\Addon\License\AddonLicense License object.
99
	 */
100
	public function get_license() {
101
		return $this->license;
102
	}
103
104
	/**
105
	 * Get action links for add-ons.
106
	 */
107
	protected function print_actions() {
108
		if ( $this->has_valid_bundle_license() ) {
109
			$this->print_valid_actions();
110
111
			return;
112
		}
113
114
		if ( ! $this->has_valid_addon_license() ) {
115
			$this->print_invalid_actions();
116
		} else {
117
			$this->print_valid_actions();
118
		}
119
120
		$this->render_individual_license();
121
	}
122
123
	/**
124
	 * Print actions that are available when the license is valid.
125
	 */
126
	protected function print_valid_actions() {
127
		$actions = '';
128
129
		if ( $this->is_installed() ) {
130
			$actions = '<a disabled class="button button-secondary">' . _x( 'Installed', 'Installed on website but not activated', 'email-log' );
131
132
			if ( $this->is_active() ) {
133
				$actions .= ' &amp; ' . _x( 'Activated', 'Installed and activated on website', 'email-log' ) . '</a>';
134
			} else {
135
				$actions .= sprintf( '</a> <a class="button button-primary" href="%s">%s</a>', $this->get_activate_url(), _x( 'Activate', 'Enable addon so it may be used', 'email-log' ) );
136
			}
137
		}
138
139
		$actions .= sprintf(
140
			' <a class="button button-secondary" target="_blank" onclick="%s" href="%s">%s</a>',
141
			$this->get_download_button_js(),
142
			$this->get_download_url(),
143
			_x( 'Download', 'Download to your computer', 'email-log' )
144
		);
145
146
		echo $actions;
147
	}
148
149
	/**
150
	 * Return the JavaScript that shows the message when the Download button is clicked.
151
	 *
152
	 * @since 2.2.4
153
	 *
154
	 * @return string JavaScript.
155
	 */
156
	protected function get_download_button_js() {
157
		ob_start();
158
		?>
159
		javascript:alert( 'You will now be able to download the zip file. Once the zip file is downloaded, upload it from the plugin page to install the add-on.' );
160
		<?php
161
		return ob_get_clean();
162
	}
163
164
	/**
165
	 * Print actions that are available when the license is not valid.
166
	 */
167
	protected function print_invalid_actions() {
168
		$label = _x( 'Activate License to Download', 'Download add-on', 'email-log' );
169
170
		if ( $this->is_installed() ) {
171
			$label = _x( 'Activate License to Use', 'Download and activate addon', 'email-log' );
172
		}
173
174
		printf(
175
			'<a disabled class="button-secondary disabled" title="%s" href="#">%s</a>',
176
			__( 'You need an active license to use the add-on', 'email-log' ),
177
			$label
178
		);
179
	}
180
181
	/**
182
	 * Render Individual license form.
183
	 */
184
	protected function render_individual_license() {
185
		$action       = 'el_license_activate';
186
		$action_text  = __( 'Activate', 'email-log' );
187
		$button_class = 'button-primary';
188
		$dashicon     = 'down';
189
		$license_wrap = 'hidden';
190
		$expires      = '';
191
192
		if ( $this->has_valid_addon_license() ) {
193
			$action       = 'el_license_deactivate';
194
			$action_text  = __( 'Deactivate', 'email-log' );
195
			$button_class = '';
196
			$dashicon     = 'up';
197
			$license_wrap = '';
198
199
			$expiry_date = date( 'F d, Y', strtotime( $this->get_license()->get_expiry_date() ) );
200
			$expires     = sprintf( __( 'Your license expires on %s', 'email-log' ), $expiry_date );
201
		}
202
		?>
203
204
		<span class="el-expander dashicons dashicons-arrow-<?php echo sanitize_html_class( $dashicon ); ?>"
205
			title="<?php _e( 'Individual add-on license', 'email-log' ); ?>"></span>
206
207
		<div class="individual-license <?php echo sanitize_html_class( $license_wrap ); ?>">
208
			<form method="post">
209
				<input type="text" name="el-license" class="el-license" size="36"
210
				       title="<?php _e( 'Email Log License Key', 'email-log' ); ?>"
211
				       placeholder="<?php echo esc_attr( sprintf( __( '%s Add-on License Key', 'email-log' ), $this->name ) ); ?>"
212
				       value="<?php echo esc_attr( $this->get_addon_license_key() ); ?>">
213
214
				<input type="submit" class="button button-large <?php echo sanitize_html_class( $button_class ); ?>"
215
				       value="<?php echo esc_attr( $action_text ); ?>">
216
217
				<p class="expires"><?php echo esc_html( $expires ); ?></p>
218
219
				<input type="hidden" name="el-addon" value="<?php echo esc_attr( $this->name ); ?>">
220
				<input type="hidden" name="el-action" value="<?php echo esc_attr( $action ); ?>">
221
222
				<?php wp_nonce_field( $action, $action . '_nonce' ); ?>
223
			</form>
224
		</div>
225
		<?php
226
227
	}
228
229
	/**
230
	 * Is the add-on installed?
231
	 *
232
	 * @return bool True, if installed. False otherwise.
233
	 */
234
	public function is_installed() {
235
		$installed_plugins = array_keys( get_plugins() );
236
237
		return in_array( $this->file, $installed_plugins, true );
238
	}
239
240
	/**
241
	 * Get the version of the add-on.
242
	 * If the add-on is installed then it returns the installed version,
243
	 * otherwise returns the latest add-on version from server.
244
	 *
245
	 * @return string Add-on version.
246
	 */
247
	public function get_version() {
248
		if ( ! $this->is_installed() ) {
249
			return $this->version;
250
		}
251
252
		$plugins_data = get_plugins();
253
254
		return $plugins_data[ $this->file ]['Version'];
255
	}
256
257
	/**
258
	 * Is the add-on active?
259
	 *
260
	 * @return bool True if the add-on is active, False otherwise.
261
	 */
262
	public function is_active() {
263
		return is_plugin_active( $this->file );
264
	}
265
266
	/**
267
	 * Get the activate url for the add-on.
268
	 *
269
	 * @return string Activate url with nonce.
270
	 */
271
	protected function get_activate_url() {
272
		return wp_nonce_url( network_admin_url( 'plugins.php?action=activate&amp;plugin=' . $this->file ), 'activate-plugin_' . $this->file );
273
	}
274
275
	/**
276
	 * Get the install url for the add-on.
277
	 *
278
	 * @return string Install url with nonce.
279
	 */
280
	protected function get_install_url() {
281
		return wp_nonce_url( network_admin_url( 'update.php?action=install-plugin&plugin=' . $this->slug ), 'install-plugin_' . $this->slug );
282
	}
283
284
	/**
285
	 * Get the download url for add-on.
286
	 *
287
	 * @return string Download url for add-on.
288
	 */
289
	public function get_download_url() {
290
		$licenser = $this->email_log->get_licenser();
291
292
		if ( is_null( $licenser ) ) {
293
			return '';
294
		}
295
296
		return $licenser->get_addon_download_url( $this->slug );
297
	}
298
299
	/**
300
	 * Is there a valid bundle license?
301
	 *
302
	 * @return bool True if valid, False otherwise.
303
	 */
304
	protected function has_valid_bundle_license() {
305
		$licenser = $this->email_log->get_licenser();
306
307
		if ( is_null( $licenser ) ) {
308
			return false;
309
		}
310
311
		return $licenser->is_bundle_license_valid();
312
	}
313
314
	/**
315
	 * Is the license of this add-on valid?
316
	 *
317
	 * @return bool True if valid, False otherwise.
318
	 */
319
	protected function has_valid_addon_license() {
320
		return $this->get_license()->is_valid();
321
	}
322
323
	/**
324
	 * Get license key if the add-on has a valid license.
325
	 *
326
	 * @return string|null License key if found, null otherwise.
327
	 */
328
	public function get_addon_license_key() {
329
		return $this->get_license()->get_license_key();
330
	}
331
332
	/**
333
	 * Parse and store add-on data from data array.
334
	 *
335
	 * @param array $data Data array.
336
	 */
337
	protected function parse_data( $data ) {
338
		$this->name        = $data['info']['title'];
339
		$this->version     = $data['info']['version'];
340
		$this->thumbnail   = $data['info']['thumbnail'];
341
		$this->description = $data['info']['excerpt'];
342
		$this->link        = $data['info']['permalink'];
343
		$this->slug        = 'email-log-' . $data['info']['slug'];
344
		$this->file        = sprintf( '%1$s/%1$s.php', $this->slug );
345
		$this->author      = 'Sudar Muthu';
346
	}
347
}
348