Completed
Push — master ( bd97eb...77fdab )
by Sudar
08:35
created

BaseLicense::get_option_name()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
nc 1
dl 0
loc 1
ccs 0
cts 0
cp 0
c 0
b 0
f 0
1
<?php namespace EmailLog\Addon\License;
2
3
use EmailLog\Addon\API\EDDAPI;
4
5
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
6
7
/**
8
 * Base class for for Bundle License and Add-on License.
9
 *
10
 * @since 2.0.0
11
 */
12
abstract class BaseLicense {
13
14
	protected $addon_name;
15
	protected $license_key;
16
	protected $license_data;
17
18
	/**
19
	 * EDD API Wrapper.
20
	 *
21
	 * @var \EmailLog\Addon\API\EDDAPI
22
	 */
23
	protected $edd_api;
24
25
	/**
26
	 * Is the license activated and valid?
27
	 *
28
	 * @return bool True if license is active, False otherwise.
29
	 */
30
	abstract public function is_valid();
31
32
	/**
33
	 * Get the option name in which license data will be stored.
34
	 *
35
	 * @return string Option name.
36
	 */
37
	abstract protected function get_option_name();
38
39
	/**
40
	 * Construct a new License object.
41
	 * If the API Wrapper is not provided, then a new one is initialized.
42
	 *
43
	 * @param \EmailLog\Addon\API\EDDAPI|null $edd_api (Optional) EDD API Wrapper instance. Default is null.
44
	 */
45
	public function __construct( $edd_api = null ) {
46
		if ( is_null( $edd_api ) ) {
47
			$edd_api = new EDDAPI();
48
		}
49
50
		$this->edd_api = $edd_api;
51
	}
52
53
	/**
54
	 * Set the license Key.
55
	 *
56
	 * @param string $license_key License Key.
57
	 */
58
	public function set_license_key( $license_key ) {
59
		$this->license_key = $license_key;
60
	}
61
62
	/**
63
	 * Get the license key.
64
	 *
65
	 * @return string|null License Key.
66
	 */
67
	public function get_license_key() {
68
		return $this->license_key;
69
	}
70
71
	/**
72
	 * Set add-on name.
73
	 *
74
	 * @param string $addon_name Add-on Name.
75
	 */
76
	public function set_addon_name( $addon_name ) {
77
		$this->addon_name = $addon_name;
78
	}
79
80
	/**
81
	 * Get the add-on name.
82
	 *
83
	 * @return string Add-on name.
84
	 */
85
	public function get_addon_name() {
86
		return $this->addon_name;
87
	}
88
89
	/**
90
	 * Get the expiry date of the license.
91
	 *
92
	 * @return string|false Expiry date in `yyyy-mm-dd hh:mm:ss` format if license is valid, False otherwise.
93
	 */
94
	public function get_expiry_date() {
95
		if ( ! empty( $this->license_data ) && isset( $this->license_data->expires ) ) {
96
			return $this->license_data->expires;
97
		}
98
99
		return false;
100
	}
101
102
	/**
103
	 * Activate License by calling EDD API.
104
	 * The license data returned by API is stored in an option.
105
	 *
106
	 * @return object API Response JSON Object.
107
	 * @throws \Exception In case of communication errors or License Issues.
108
	 */
109
	public function activate() {
110
		$response = $this->edd_api->activate_license( $this->get_license_key(), $this->get_addon_name() );
111
112
		if ( $response->success && 'valid' === $response->license ) {
113
			$response->license_key = $this->get_license_key();
114
115
			$this->store( $response );
116
			return $response;
117
		}
118
119
		switch ( $response->error ) {
120
			case 'expired':
121
				$message = sprintf(
122
					__( 'Your license key expired on %s.' ),
123
					date_i18n( get_option( 'date_format' ), strtotime( $response->expires, current_time( 'timestamp' ) ) )
124
				);
125
				break;
126
127
			case 'revoked':
128
				$message = __( 'Your license key has been disabled.' );
129
				break;
130
131
			case 'missing':
132
				$message = __( 'Your license key is invalid.' );
133
				break;
134
135
			case 'invalid':
136
			case 'site_inactive':
137
				$message = __( 'Your license is not active for this URL.' );
138
				break;
139
140
			case 'item_name_mismatch':
141
				$message = sprintf( __( 'Your license key is not valid for %s.' ), $this->get_addon_name() );
142
				break;
143
144
			case 'no_activations_left':
145
				$message = __( 'Your license key has reached its activation limit.' );
146
				break;
147
148
			default:
149
				$message = __( 'An error occurred, please try again.' );
150
				break;
151
		}
152
153
		throw new \Exception( $message );
154
	}
155
156
	/**
157
	 * Deactivate the license by calling EDD API.
158
	 * The stored license data will be cleared.
159
	 *
160
	 * @return object API Response JSON object.
161
	 * @throws \Exception In case of communication errors.
162
	 */
163
	public function deactivate() {
164
		$response = $this->edd_api->deactivate_license( $this->get_license_key(), $this->get_addon_name() );
165
166
		if ( $response->success && 'deactivated' === $response->license ) {
167
			$this->clear();
168
			return $response;
169
		}
170
171
		$message = __( 'An error occurred, please try again.', 'email-log' );
172
173
		if ( isset( $response->error ) ) {
174
			$message .= ' ' . $response->error;
175
		}
176
177
		throw new \Exception( $message );
178
	}
179
180
	/**
181
	 * Get version information by calling EDD API.
182
	 * // TODO: Currently not used. Will be removed eventually.
183
	 *
184
	 * @return object API Response JSON Object.
185
	 * @throws \Exception In case of communication errors.
186
	 */
187
	public function get_version() {
188
		$response = $this->edd_api->get_version( $this->get_license_key(), $this->get_addon_name() );
189
190
		if ( isset( $response->new_version ) && ! isset( $response->msg ) ) {
191
			return $response;
192
		}
193
194
		$message = __( 'An error occurred, please try again', 'email-log' ) . $response->error;
195
		throw new \Exception( $message );
196
	}
197
198
	/**
199
	 * Load the license data from DB option.
200
	 */
201
	public function load() {
202
		$this->license_data = get_option( $this->get_option_name(), null );
203
	}
204
205
	/**
206
	 * Store License data in DB option.
207
	 *
208
	 * @access protected
209
	 *
210
	 * @param object $license_data License data.
211
	 */
212
	protected function store( $license_data ) {
213
		$this->license_data = $license_data;
214
		update_option( $this->get_option_name(), $license_data );
215
	}
216
217
	/**
218
	 * Clear stored license data.
219
	 *
220
	 * @access protected
221
	 */
222
	protected function clear() {
223
		$this->license_data = null;
224
		$this->license_key = null;
225
		delete_option( $this->get_option_name() );
226
	}
227
}
228