Completed
Push — master ( 186893...053e09 )
by Sudar
02:09
created

Addon::get_download_url()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 1
b 0
f 1
1
<?php namespace EmailLog\Addon;
2
3
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
4
5
/**
6
 * Encapsulate Add-on Data.
7
 *
8
 * @since 2.0.0
9
 *
10
 * @property-read string $name
11
 * @property-read string $version
12
 * @property-read string $thumbnail
13
 * @property-read string $description
14
 * @property-read string $link
15
 * @property-read string $slug
16
 * @property-read string $file
17
 * @property-read string $author
18
 */
19
class Addon {
20
21
	private $name;
22
	private $version;
23
	private $thumbnail;
24
	private $description;
25
	private $link;
26
	private $slug;
27
	private $file;
28
	private $author;
29
30
	/**
31
	 * Construct Addon object from data array.
32
	 *
33
	 * @param array $data Data array.
34
	 */
35
	public function __construct( $data ) {
36
		$this->name        = $data['info']['title'];
37
		$this->version     = $data['info']['version'];
38
		$this->thumbnail   = $data['info']['thumbnail'];
39
		$this->description = $data['info']['excerpt'];
40
		$this->link        = $data['info']['permalink'];
41
		$this->slug        = 'email-log-' . $data['info']['slug'];
42
		$this->file         = sprintf( '%1$s/%1$s.php', $this->slug );
43
		$this->author      = 'Sudar Muthu';
44
	}
45
46
	/**
47
	 * Render the add-on in Addon list page.
48
	 */
49
	public function render() {
50
		?>
51
		<div class="el-addon">
52
			<h3 class="el-addon-title"> <?php echo esc_html( $this->name ); ?> </h3>
53
54
			<a href="<?php echo esc_url( $this->link ); ?>" title="<?php echo esc_attr( $this->name ); ?>">
55
				<img src="<?php echo esc_url( $this->thumbnail ); ?>" class="attachment-showcase wp-post-image"
56
				     alt="<?php echo esc_attr( $this->name ); ?>" title="<?php echo esc_attr( $this->name ); ?>">
57
			</a>
58
59
			<p> <?php echo esc_html( $this->description ); ?> </p>
60
61
			<?php echo $this->get_actions(); ?>
62
		</div> <!-- .el-addon -->
63
64
		<?php
65
	}
66
67
	/**
68
	 * Have a magic getter instead of having individual getters.
69
	 *
70
	 * @param string $property Property.
71
	 *
72
	 * @return string Value for the property.
73
	 */
74
	public function __get( $property ) {
75
		if ( isset( $this->{$property} ) ) {
76
			return $this->{$property};
77
		}
78
79
		return false;
80
	}
81
82
	/**
83
	 * Get action links for add-ons.
84
	 *
85
	 * @return string Action links.
86
	 */
87
	protected function get_actions() {
88
		if ( ! $this->is_license_valid() ) {
89
			if ( $this->is_installed() ) {
90
				return sprintf(
91
					'<a disabled class="button-secondary" title="%s" href="#">%s</a>',
92
					__( 'You need an active license to install the add-on', 'email-log' ),
93
					_x( 'Activate License to Use', 'Download and activate addon', 'email-log' )
94
				);
95
			} else {
96
				return sprintf(
97
					'<a disabled class="button-secondary" title="%s" href="#">%s</a>',
98
					__( 'You need an active license to install the add-on', 'email-log' ),
99
					_x( 'Activate License to Install', 'Download and activate addon', 'email-log' )
100
				);
101
			}
102
		}
103
104
		if ( $this->is_installed() ) {
105
			$actions = '<a disabled class="button button-secondary">' . _x( 'Installed', 'Installed on website but not activated', 'email-log' );
106
107
			if ( $this->is_active() ) {
108
				$actions .= ' &amp; ' . _x( 'Activated', 'Installed and activated on website', 'email-log' ) . '</a>';
109
			} else {
110
				$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' ) );
111
			}
112
		} else {
113
			// TODO: Make sure WordPress core can handle add-on installation.
114
			$actions = sprintf( '<a class="button button-primary" href="%s">%s</a>', $this->get_install_url(), _x( 'Install', 'Download and activate addon', 'email-log' ) );
115
		}
116
117
		$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' ) );
118
119
		return $actions;
120
	}
121
122
	/**
123
	 * Is the add-on installed?
124
	 *
125
	 * @return bool True, if installed. False otherwise.
126
	 */
127
	public function is_installed() {
128
		$installed_plugins = array_keys( get_plugins() );
129
130
		return in_array( $this->file, $installed_plugins, true );
131
	}
132
133
	/**
134
	 * Get the version of the add-on.
135
	 * If the add-on is installed then it returns the installed version,
136
	 * otherwise returns the latest add-on version from server.
137
	 *
138
	 * @return string Add-on version.
139
	 */
140
	public function get_version() {
141
		if ( ! $this->is_installed() ) {
142
			return $this->version;
143
		}
144
145
		$plugins_data = get_plugins();
146
147
		return $plugins_data[ $this->file ]['Version'];
148
	}
149
150
	/**
151
	 * Is the add-on active?
152
	 *
153
	 * @return bool True if the add-on is active, False otherwise.
154
	 */
155
	public function is_active() {
156
		return is_plugin_active( $this->file );
157
	}
158
159
	/**
160
	 * Get the activate url for the add-on.
161
	 *
162
	 * @return string Activate url with nonce.
163
	 */
164
	protected function get_activate_url() {
165
		return wp_nonce_url( network_admin_url( 'plugins.php?action=activate&amp;plugin=' . $this->file ), 'activate-plugin_' . $this->file );
166
	}
167
168
	/**
169
	 * Get the install url for the add-on.
170
	 *
171
	 * @return string Install url with nonce.
172
	 */
173
	protected function get_install_url() {
174
		return wp_nonce_url( network_admin_url( 'update.php?action=install-plugin&plugin=' . $this->slug ), 'install-plugin_' . $this->slug );
175
	}
176
177
	/**
178
	 * Get the download url for add-on.
179
	 *
180
	 * @return string Download url for add-on.
181
	 */
182
	public function get_download_url() {
183
		$email_log = email_log();
184
185
		return $email_log->get_licenser()->get_addon_download_url( $this->slug );
186
	}
187
188
	/**
189
	 * Is the license of this add-on valid?
190
	 *
191
	 * @return bool True if valid, False otherwise.
192
	 */
193
	protected function is_license_valid() {
194
		$email_log = email_log();
195
196
		return $email_log->get_licenser()->is_addon_license_valid( $this->name );
197
	}
198
199
	/**
200
	 * Get license key if the add-on has a valid license.
201
	 *
202
	 * @return bool|string License key if found, False otherwise.
203
	 */
204
	protected function get_license_key() {
205
		$email_log = email_log();
206
207
		return $email_log->get_licenser()->get_addon_license_key( $this->name );
208
	}
209
}
210