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

Licenser::add_updater()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 3
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 5
rs 9.4285
1
<?php namespace EmailLog\License;
2
3
use EmailLog\Addon\AddonList;
4
use EmailLog\Addon\API\EDDUpdater;
5
use EmailLog\Core\Loadie;
6
7
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
8
9
/**
10
 * Handles the add-on licensing for Email Log.
11
 *
12
 * There can be one normal license for each add-on or one bundle license for all add-ons.
13
 * This class is final because we don't want other plugins to interfere with Email Log licensing.
14
 *
15
 * @since 2.0.0
16
 */
17
final class Licenser implements Loadie {
18
19
	/**
20
	 * Bundle License object.
21
	 *
22
	 * @var \EmailLog\License\BundleLicense
23
	 */
24
	private $bundle_license;
25
26
	/**
27
	 * List of Add-on updaters.
28
	 *
29
	 * @var \EmailLog\Addon\API\EDDUpdater[]
30
	 */
31
	private $updaters = array();
32
33
	/**
34
	 * List of add-ons.
35
	 *
36
	 * @var \EmailLog\Addon\AddonList
37
	 */
38
	private $addon_list;
39
40
	/**
41
	 * Licenser constructor.
42
	 * If the bundle_license object is not passed a new object is created.
43
	 * If the addon_list object is not passed a new object is created.
44
	 *
45
	 * @param null|\EmailLog\License\BundleLicense $bundle_license Optional. Bundle License.
46
	 * @param null|\EmailLog\Addon\AddonList       $addon_list     Optional. Add-on List.
47
	 */
48
	public function __construct( $bundle_license = null, $addon_list = null ) {
49
		if ( ! $bundle_license instanceof BundleLicense ) {
50
			$bundle_license = new BundleLicense();
51
		}
52
53
		if ( ! $addon_list instanceof AddonList ) {
54
			$addon_list = new AddonList();
55
		}
56
57
		$this->bundle_license = $bundle_license;
58
		$this->addon_list = $addon_list;
59
	}
60
61
	/**
62
	 * Load all Licenser related hooks.
63
	 *
64
	 * @inheritdoc
65
	 */
66
	public function load() {
67
		$this->bundle_license->load();
68
69
		add_action( 'el_before_addon_list', array( $this, 'render_bundle_license_form' ) );
70
71
		add_action( 'el_bundle_license_activate', array( $this, 'activate_bundle_license' ) );
72
		add_action( 'el_bundle_license_deactivate', array( $this, 'deactivate_bundle_license' ) );
73
	}
74
75
	/**
76
	 * Add an Add-on Updater.
77
	 *
78
	 * @param \EmailLog\Addon\API\EDDUpdater $updater Add-on Updater.
79
	 */
80
	public function add_updater( $updater ) {
81
		if ( $updater instanceof EDDUpdater ) {
82
			$this->updaters[ $updater->get_slug() ] = $updater;
83
		}
84
	}
85
86
	/**
87
	 * Get list of add-ons.
88
	 *
89
	 * @return \EmailLog\Addon\AddonList Add-on List.
90
	 */
91
	public function get_addon_list() {
92
		return $this->addon_list;
93
	}
94
95
	/**
96
	 * Render the Bundle License Form.
97
	 */
98
	public function render_bundle_license_form() {
99
		$action       = 'el_bundle_license_activate';
100
		$action_text  = __( 'Activate', 'email-log' );
101
		$button_class = 'button-primary';
102
103
		if ( $this->is_bundle_license_valid() ) {
104
			$action       = 'el_bundle_license_deactivate';
105
			$action_text  = __( 'Deactivate', 'email-log' );
106
			$button_class = '';
107
		}
108
		?>
109
110
		<div class="bundle-license">
111
			<?php if ( ! $this->is_bundle_license_valid() ) : ?>
112
				<p class="notice notice-warning">
113
					<?php
114
					printf(
115
						__( "Enter your license key to activate add-ons. If you don't have a license, then you can <a href='%s' target='_blank'>buy it</a>", 'email-log' ),
116
						'https://wpemaillog.com'
117
					);
118
					?>
119
				</p>
120
			<?php endif; ?>
121
122
			<form method="post">
123
				<input type="text" name="el-license" class="el-license" size="40"
124
				       title="<?php _e( 'Email Log Bundle License Key', 'email-log' ); ?>"
125
				       placeholder="<?php _e( 'Email Log Bundle License Key', 'email-log' ); ?>"
126
				       value="<?php echo esc_attr( $this->bundle_license->get_license_key() ); ?>">
127
128
				<input type="submit" class="button button-large <?php echo sanitize_html_class( $button_class ); ?>"
129
				       value="<?php echo esc_attr( $action_text ); ?>">
130
131
				<input type="hidden" name="el-action" value="<?php echo esc_attr( $action ); ?>">
132
133
				<?php wp_nonce_field( $action, $action . '_nonce' ); ?>
134
			</form>
135
		</div>
136
		<?php
137
	}
138
139
	/**
140
	 * Activate Bundle License.
141
	 *
142
	 * @param array $request Request Object.
143
	 */
144
	public function activate_bundle_license( $request ) {
145
		$license_key = sanitize_text_field( $request['el-license'] );
146
147
		$this->bundle_license->set_license_key( $license_key );
148
149
		try {
150
			$this->bundle_license->activate();
151
			$message = __( 'Your license has been activated. You can now install add-ons, will receive automatic updates and access to email support.', 'email-log' );
152
			$type    = 'updated';
153
		} catch ( \Exception $e ) {
154
			$message = $e->getMessage();
155
			$type    = 'error';
156
		}
157
158
		add_settings_error( 'bundle-license', 'bundle-license', $message, $type );
159
	}
160
161
	/**
162
	 * Deactivate Bundle License.
163
	 */
164
	public function deactivate_bundle_license() {
165
		try {
166
			$this->bundle_license->deactivate();
167
			$message = __( 'Your license has been deactivated. You will not receive automatic updates.', 'email-log' );
168
			$type    = 'updated';
169
		} catch ( \Exception $e ) {
170
			$message = $e->getMessage();
171
			$type    = 'error';
172
		}
173
174
		add_settings_error( 'bundle-license', 'bundle-license', $message, $type );
175
	}
176
177
	/**
178
	 * Is the bundle license valid?
179
	 *
180
	 * @return bool True, if Bundle License is active, False otherwise.
181
	 */
182
	public function is_bundle_license_valid() {
183
		return $this->bundle_license->is_valid();
184
	}
185
186
	/**
187
	 * Is the add-on license valid?
188
	 *
189
	 * @param string $addon_name Addon Name.
190
	 *
191
	 * @return bool True if license is valid, False otherwise.
192
	 */
193
	public function is_addon_license_valid( $addon_name ) {
0 ignored issues
show
Unused Code introduced by
The parameter $addon_name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
194
		if ( $this->is_bundle_license_valid() ) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return $this->is_bundle_license_valid();.
Loading history...
195
			return true;
196
		}
197
198
		// TODO: Handle individual license.
199
		return false;
200
	}
201
202
	/**
203
	 * Get the license key of an add-on.
204
	 *
205
	 * @param string $addon_name Addon.
206
	 *
207
	 * @return bool|string License key if found, False otherwise.
208
	 */
209
	public function get_addon_license_key( $addon_name ) {
210
		if ( ! $this->is_bundle_license_valid() ) {
211
			return false;
212
		}
213
214
		return $this->bundle_license->get_addon_license_key( $addon_name );
215
	}
216
217
	/**
218
	 * Get the Download URL of an add-on.
219
	 *
220
	 * @param string $addon_slug Add-on slug.
221
	 *
222
	 * @return string Download URL.
223
	 */
224
	public function get_addon_download_url( $addon_slug ) {
225
		if ( isset( $this->updaters[ $addon_slug ] ) ) {
226
			return $this->updaters[ $addon_slug ]->get_download_url();
227
		}
228
229
		return '';
230
	}
231
}
232