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

Addon::get_license()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php namespace EmailLog\Addon;
2
3
use EmailLog\License\License;
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\License\License $license   Add-on License.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $license not be License|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
40
	 * @param \EmailLog\Core\EmailLog   $email_log Email Log instance.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $email_log not be \EmailLog\Core\EmailLog|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
41
	 */
42
	public function __construct( $data, $license = null, $email_log = null ) {
43
		$this->parse_data( $data );
44
45
		if ( null === $license ) {
46
			$license = new License();
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 ); ?>" title="<?php echo esc_attr( $this->name ); ?>">
67
				<img src="<?php echo esc_url( $this->thumbnail ); ?>" class="attachment-showcase wp-post-image"
68
				     alt="<?php echo esc_attr( $this->name ); ?>" title="<?php echo esc_attr( $this->name ); ?>">
69
			</a>
70
71
			<p> <?php echo esc_html( $this->description ); ?> </p>
72
73
			<?php $this->print_actions(); ?>
74
		</div> <!-- .el-addon -->
75
76
		<?php
77
	}
78
79
	/**
80
	 * Have a magic getter instead of having individual getters.
81
	 *
82
	 * @param string $property Property.
83
	 *
84
	 * @return string Value for the property.
85
	 */
86
	public function __get( $property ) {
87
		if ( isset( $this->{$property} ) ) {
88
			return $this->{$property};
89
		}
90
91
		return false;
92
	}
93
94
	/**
95
	 * Get Add-on License object.
96
	 *
97
	 * @return \EmailLog\License\License License object.
98
	 */
99
	public function get_license() {
100
		return $this->license;
101
	}
102
103
	/**
104
	 * Get action links for add-ons.
105
	 *
106
	 * @return string Action links.
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

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...
107
	 */
108
	protected function print_actions() {
109
		// bundle license is active
110
		// print valid actions
111
		if ( $this->has_valid_bundle_license() ) {
112
			$this->print_valid_actions();
113
			return;
114
		}
115
116
		// if valid, print valid actions
117
		// if not, print invalid actions
118
		if ( ! $this->has_valid_addon_license() ) {
119
			$this->print_invalid_actions();
120
		} else {
121
			$this->print_valid_actions();
122
		}
123
124
		// print individual form
125
		$this->render_individual_license();
126
	}
127
128
	/**
129
	 * Print actions that are available when the license is valid.
130
	 */
131
	protected function print_valid_actions() {
132
		if ( $this->is_installed() ) {
133
			$actions = '<a disabled class="button button-secondary">' . _x( 'Installed', 'Installed on website but not activated', 'email-log' );
134
135
			if ( $this->is_active() ) {
136
				$actions .= ' &amp; ' . _x( 'Activated', 'Installed and activated on website', 'email-log' ) . '</a>';
137
			} else {
138
				$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' ) );
139
			}
140
		} else {
141
			$actions = sprintf( '<a class="button button-primary" href="%s">%s</a>', $this->get_install_url(), _x( 'Install', 'Download and activate addon', 'email-log' ) );
142
		}
143
144
		$actions .= sprintf( ' <a class="button button-secondary" target="_blank" href="%s">%s</a>', $this->get_download_url(), _x( 'Download', 'Download to your computer', 'email-log' ) );
145
146
		echo $actions;
147
	}
148
149
	/**
150
	 * Print actions that are available when the license is not valid.
151
	 */
152
	protected function print_invalid_actions() {
153
		$label = _x( 'Activate License to Install', 'Download and activate addon', 'email-log' );
154
155
		if ( $this->is_installed() ) {
156
			$label = _x( 'Activate License to Use', 'Download and activate addon', 'email-log' );
157
		}
158
159
		printf(
160
			'<a disabled class="button-secondary" title="%s" href="#">%s</a>',
161
			__( 'You need an active license to install the add-on', 'email-log' ),
162
			$label
163
		);
164
	}
165
166
	/**
167
	 * Render Individual license form.
168
	 */
169
	protected function render_individual_license() {
170
		$action       = 'el_license_activate';
171
		$action_text  = __( 'Activate', 'email-log' );
172
		$button_class = 'button-primary';
173
174
		if ( $this->has_valid_addon_license() ) {
175
			$action       = 'el_license_deactivate';
176
			$action_text  = __( 'Deactivate', 'email-log' );
177
			$button_class = '';
178
		}
179
		?>
180
181
		<div class="individual-license">
182
			<form method="post">
183
				<input type="text" name="el-license" class="el-license" size="36"
184
				       title="<?php _e( 'Email Log License Key', 'email-log' ); ?>"
185
				       placeholder="<?php _e( 'Email Log License Key', 'email-log' ); ?>"
186
				       value="<?php echo esc_attr( $this->get_addon_license_key() ); ?>">
187
188
				<input type="submit" class="button button-large <?php echo sanitize_html_class( $button_class ); ?>"
189
				       value="<?php echo esc_attr( $action_text ); ?>">
190
191
				<input type="hidden" name="el-addon" value="<?php echo esc_attr( $this->name ); ?>">
192
				<input type="hidden" name="el-action" value="<?php echo esc_attr( $action ); ?>">
193
194
				<?php wp_nonce_field( $action, $action . '_nonce' ); ?>
195
			</form>
196
		</div>
197
		<?php
198
199
	}
200
201
	/**
202
	 * Is the add-on installed?
203
	 *
204
	 * @return bool True, if installed. False otherwise.
205
	 */
206
	public function is_installed() {
207
		$installed_plugins = array_keys( get_plugins() );
208
209
		return in_array( $this->file, $installed_plugins, true );
210
	}
211
212
	/**
213
	 * Get the version of the add-on.
214
	 * If the add-on is installed then it returns the installed version,
215
	 * otherwise returns the latest add-on version from server.
216
	 *
217
	 * @return string Add-on version.
218
	 */
219
	public function get_version() {
220
		if ( ! $this->is_installed() ) {
221
			return $this->version;
222
		}
223
224
		$plugins_data = get_plugins();
225
226
		return $plugins_data[ $this->file ]['Version'];
227
	}
228
229
	/**
230
	 * Is the add-on active?
231
	 *
232
	 * @return bool True if the add-on is active, False otherwise.
233
	 */
234
	public function is_active() {
235
		return is_plugin_active( $this->file );
236
	}
237
238
	/**
239
	 * Get the activate url for the add-on.
240
	 *
241
	 * @return string Activate url with nonce.
242
	 */
243
	protected function get_activate_url() {
244
		return wp_nonce_url( network_admin_url( 'plugins.php?action=activate&amp;plugin=' . $this->file ), 'activate-plugin_' . $this->file );
245
	}
246
247
	/**
248
	 * Get the install url for the add-on.
249
	 *
250
	 * @return string Install url with nonce.
251
	 */
252
	protected function get_install_url() {
253
		return wp_nonce_url( network_admin_url( 'update.php?action=install-plugin&plugin=' . $this->slug ), 'install-plugin_' . $this->slug );
254
	}
255
256
	/**
257
	 * Get the download url for add-on.
258
	 *
259
	 * @return string Download url for add-on.
260
	 */
261
	public function get_download_url() {
262
		return $this->email_log->get_licenser()->get_addon_download_url( $this->slug );
263
	}
264
265
	/**
266
	 * Is there a valid bundle license?
267
	 *
268
	 * @return bool True if valid, False otherwise.
269
	 */
270
	protected function has_valid_bundle_license() {
271
		return $this->email_log->get_licenser()->is_bundle_license_valid();
272
	}
273
274
	/**
275
	 * Is the license of this add-on valid?
276
	 *
277
	 * @return bool True if valid, False otherwise.
278
	 */
279
	protected function has_valid_addon_license() {
280
		return $this->get_license()->is_valid();
281
	}
282
283
	/**
284
	 * Get license key if the add-on has a valid license.
285
	 *
286
	 * @return bool|string License key if found, False otherwise.
287
	 */
288
	public function get_addon_license_key() {
289
		return $this->get_license()->get_license_key();
290
	}
291
292
	/**
293
	 * Parse and store add-on data from data array.
294
	 *
295
	 * @param array $data Data array.
296
	 */
297
	protected function parse_data( $data ) {
298
		$this->name        = $data['info']['title'];
299
		$this->version     = $data['info']['version'];
300
		$this->thumbnail   = $data['info']['thumbnail'];
301
		$this->description = $data['info']['excerpt'];
302
		$this->link        = $data['info']['permalink'];
303
		$this->slug        = 'email-log-' . $data['info']['slug'];
304
		$this->file        = sprintf( '%1$s/%1$s.php', $this->slug );
305
		$this->author      = 'Sudar Muthu';
306
	}
307
}
308