Completed
Push — master ( 49679a...617b2a )
by Sudar
02:03
created

Addon::get_license_key()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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