PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 4.4.1
MainWP Dashboard: Self-hosted WordPress Management for Agencies v4.4.1
6.2 6.1.8 6.1.7 6.1.6 6.1.5 6.1.4 6.1.3 6.1.2 6.1.1 6.1 6.0.12 6.0.11 4.6.0.1 5.0 5.0.1 5.0.2 5.0.3 5.0.3.1 5.0.3.2 5.1 5.1.1 5.2 5.2.1 5.2.2 5.3 All 153 releases
mainwp / class / class-mainwp-api-manager.php

class-mainwp-api-manager.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies 4.4.1, at class/class-mainwp-api-manager.php

568 lines 16.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * MainWP API Manager
4 *
5 * This class handles MainWP Extensions API licensing.
6 *
7 * @package MainWP/MainWP API Manager
8 * @author Todd Lahman LLC
9 * @copyright Copyright (c) Todd Lahman LLC
10 * @since 1.0.0
11 */
12
13 namespace MainWP\Dashboard;
14
15 /**
16 * Class MainWP_Api_Manager
17 *
18 * @package MainWP\Dashboard
19 */
20 class MainWP_Api_Manager {
21
22 /**
23 * Extensions upgrade URL.
24 *
25 * @var string Default URL 'https://mainwp.com/'
26 */
27 private $upgrade_url = 'https://mainwp.com/';
28
29 /**
30 * Extensions license renewal URL.
31 *
32 * @var string Default 'https://mainwp.com/my-account'
33 */
34 private $renew_license_url = 'https://mainwp.com/my-account';
35
36 /**
37 * Protected static variable to hold the single instance of the class.
38 *
39 * @var mixed Default null
40 */
41 protected static $instance = null;
42
43 /**
44 * MainWP installation domain name.
45 *
46 * @var string $domain Empty by default.
47 */
48 public $domain = '';
49
50 /**
51 * Return the single instance of the class.
52 *
53 * @return mixed $instance The single instance of the class.
54 */
55 public static function instance() {
56
57 if ( is_null( self::$instance ) ) {
58 self::$instance = new self();
59 }
60
61 return self::$instance;
62 }
63
64 /**
65 * Cloning is forbidden.
66 *
67 * @since 1.2
68 */
69 public function __clone() {
70 }
71
72 /**
73 * Un-serializing instances of this class is forbidden.
74 *
75 * @since 1.2
76 */
77 public function __wakeup() {
78 }
79
80 /**
81 * MainWP_Api_Manager constructor.
82 *
83 * Run each time the class is called.
84 *
85 * Replace HTTP protocol to HTTPS.
86 */
87 public function __construct() {
88 $this->domain = str_ireplace( array( 'http://', 'https://' ), '', home_url() );
89 }
90
91 /**
92 * Get Upgrade URL.
93 *
94 * @return string Activation upgrade URL.
95 */
96 public function get_upgrade_url() {
97 $url = apply_filters( 'mainwp_api_manager_upgrade_url', $this->upgrade_url );
98 return $url;
99 }
100
101 /**
102 * Get domain URL.
103 *
104 * @return string domain URL.
105 */
106 public function get_domain() {
107 return $this->domain;
108 }
109
110 /**
111 * Get activation info.
112 *
113 * @param mixed $ext_key extension key.
114 *
115 * @return mixed get_option() get activation information.
116 */
117 public function get_activation_info( $ext_key ) {
118 if ( empty( $ext_key ) ) {
119 return array();
120 }
121
122 return get_option( $ext_key . '_APIManAdder' );
123 }
124
125 /**
126 * Store activation info.
127 *
128 * @param mixed $ext_key Extension key.
129 * @param mixed $info Activation information.
130 *
131 * @return mixed Set activation info.
132 * @uses \MainWP\Dashboard\MainWP_Utility::update_option()
133 */
134 public function set_activation_info( $ext_key, $info ) {
135
136 if ( empty( $ext_key ) ) {
137 return false;
138 }
139
140 // Clear cached of all activations to reload for next loading.
141 update_option( 'mainwp_extensions_all_activation_cached', '' );
142
143 return MainWP_Utility::update_option( $ext_key . '_APIManAdder', $info );
144 }
145
146 /**
147 * Remove activation info.
148 *
149 * @param mixed $ext_key Extension key.
150 *
151 * @return mixed Remove activation info.
152 */
153 public function remove_activation_info( $ext_key ) {
154 self::instance()->set_activation_info( $ext_key, '' );
155 }
156
157 /**
158 * Check API Key & API Email again MainWP Servers.
159 *
160 * @param array $api_slug Extension activation info.
161 * @param string $api_key API license key.
162 *
163 * @return array Activation info.
164 *
165 * @uses \MainWP\Dashboard\MainWP_Api_Manager_Key::activate()
166 * @uses \MainWP\Dashboard\MainWP_Utility::update_option()
167 */
168 public function license_key_activation( $api_slug, $api_key ) {
169
170 $options = $this->get_activation_info( $api_slug );
171
172 if ( ! is_array( $options ) ) {
173 $options = array();
174 }
175 $current_api_key = isset( $options['api_key'] ) ? $options['api_key'] : '';
176 $activation_status = isset( $options['activated_key'] ) ? $options['activated_key'] : '';
177
178 if ( 'Deactivated' == $activation_status || '' == $activation_status || '' == $api_key || $current_api_key != $api_key ) {
179 if ( $current_api_key != $api_key ) {
180 $reset = $this->replace_license_key(
181 array(
182 'api_key' => $api_key,
183 'product_id' => $options['product_id'],
184 'instance' => $options['instance_id'],
185 )
186 );
187 if ( ! $reset ) {
188 return array( 'error' => esc_html__( 'The license could not be deactivated.', 'mainwp' ) );
189 }
190 }
191
192 $return = array();
193
194 $args = array(
195 'api_key' => $api_key,
196 'product_id' => $options['product_id'],
197 'instance' => $options['instance_id'],
198 'software_version' => $options['software_version'],
199 'object' => $this->domain,
200 );
201
202 $activate_results = MainWP_Api_Manager_Key::instance()->activate( $args );
203 if ( ! empty( $activate_results ) ) {
204 $activate_results = json_decode( $activate_results, true );
205 } else {
206 $activate_results = array();
207 }
208
209 if ( true == $activate_results['activated'] ) {
210 $return['result'] = 'SUCCESS';
211 $mess = isset( $activate_results['message'] ) ? $activate_results['message'] : '';
212 $return['message'] = esc_html__( 'The extension has been activated. ', 'mainwp' ) . $mess;
213 $options['api_key'] = $api_key;
214 $options['activated_key'] = 'Activated';
215 $options['deactivate_checkbox'] = 'off';
216 }
217
218 if ( false == $activate_results ) {
219 $apisslverify = get_option( 'mainwp_api_sslVerifyCertificate' );
220 if ( 1 == $apisslverify ) {
221 MainWP_Utility::update_option( 'mainwp_api_sslVerifyCertificate', 0 );
222 $return['retry_action'] = 1;
223 } else {
224 $return['error'] = esc_html__( 'Connection failed to the License Key API server. Try again later.', 'mainwp' );
225 }
226 $options['api_key'] = '';
227 $options['activated_key'] = 'Deactivated';
228 }
229
230 $error = $this->check_response_for_api_errors( $activate_results );
231 if ( ! empty( $error ) ) {
232 $return['error'] = $error;
233 }
234
235 $this->set_activation_info( $api_slug, $options );
236
237 return $return;
238 } else {
239 return array( 'result' => 'SUCCESS' );
240 }
241 }
242
243 /**
244 * Deactivate the current license key before activating the new license key.
245 *
246 * @param array $args Request arguments.
247 *
248 * @return bool True on success, false on failure.
249 *
250 * @uses \MainWP\Dashboard\MainWP_Api_Manager_Key::deactivate()
251 */
252 private function replace_license_key( $args ) {
253 $reset = MainWP_Api_Manager_Key::instance()->deactivate( $args ); // reset license key activation.
254
255 if ( true == $reset ) {
256 return true;
257 }
258 }
259
260 /**
261 * Deactivate license Key.
262 *
263 * @param array $api_slug Extension activation info.
264 * @param array $api_key Extension activation api key.
265 *
266 * @return array Deactivation info.
267 *
268 * @uses \MainWP\Dashboard\MainWP_Api_Manager_Key::deactivate()
269 */
270 public function license_key_deactivation( $api_slug, $api_key ) {
271
272 $options = $this->get_activation_info( $api_slug );
273 if ( ! is_array( $options ) ) {
274 $options = array();
275 }
276
277 $activation_status = isset( $options['activated_key'] ) ? $options['activated_key'] : '';
278
279 if ( empty( $api_key ) ) {
280 $options['api_key'] = '';
281 $this->set_activation_info( $api_slug, $options );
282 }
283
284 $return = array();
285
286 if ( 'Activated' == $activation_status && '' != $api_key ) {
287 $activate_results = MainWP_Api_Manager_Key::instance()->deactivate(
288 array(
289 'product_id' => $options['product_id'],
290 'instance' => $options['instance_id'],
291 'api_key' => $api_key,
292 'object' => $this->domain,
293 )
294 ); // reset license key activation.
295
296 $activate_results = json_decode( $activate_results, true );
297 if ( isset( $activate_results['deactivated'] ) && true == $activate_results['deactivated'] ) {
298 $options['api_key'] = '';
299 $options['activated_key'] = 'Deactivated';
300 $options['deactivate_checkbox'] = 'on';
301 $return['result'] = 'SUCCESS';
302 $return['activations_remaining'] = $activate_results['activations_remaining'];
303 }
304
305 $error = $this->check_response_for_api_errors( $activate_results );
306 if ( ! empty( $error ) ) {
307 $return['error'] = $error;
308 $options['api_key'] = '';
309 $options['activated_key'] = 'Deactivated';
310 }
311
312 $this->set_activation_info( $api_slug, $options );
313
314 return $return;
315 }
316 // to fix: clear cached of all activations to reload for next loading.
317 update_option( 'mainwp_extensions_all_activation_cached', '' );
318 return array( 'result' => 'SUCCESS' );
319 }
320
321 /**
322 * Test the users MainWP.com Login details against MainWP Server.
323 *
324 * @param string $api_key MainWP api key.
325 *
326 * @return mixed Login test result.
327 *
328 * @uses \MainWP\Dashboard\MainWP_Api_Manager_Key::verify_api_key()
329 */
330 public function verify_mainwp_api( $api_key ) {
331 if ( empty( $api_key ) ) {
332 return false;
333 }
334
335 return MainWP_Api_Manager_Key::instance()->verify_api_key(
336 array(
337 'api_key' => $api_key,
338 )
339 );
340 }
341
342
343 /**
344 * Check if the user purchased the software.
345 *
346 * @param string $productId extension (product) ID.
347 *
348 * @return mixed purchase_software() purchase extensions.
349 *
350 * @uses \MainWP\Dashboard\MainWP_Api_Manager_Key::purchase_software()
351 */
352 public function purchase_software( $productId ) {
353 return MainWP_Api_Manager_Key::instance()->purchase_software(
354 array(
355 'product_id' => $productId,
356 )
357 );
358 }
359
360 /**
361 * Get users purchased extensions.
362 *
363 * @param string $api_key api key.
364 * @param string $productId extension (product) ID.
365 * @param bool $no_register registration request.
366 *
367 * @return array Purchased extensions.
368 *
369 * @uses \MainWP\Dashboard\MainWP_Api_Manager_Key::get_purchased_software()
370 */
371 public function get_purchased_extension( $api_key, $productId = '', $no_register = false ) {
372 if ( empty( $api_key ) ) {
373 return false;
374 }
375
376 return MainWP_Api_Manager_Key::instance()->get_purchased_software(
377 array(
378 'product_id' => $productId,
379 'api_key' => $api_key,
380 'noauth' => $no_register ? 1 : 0,
381 )
382 );
383 }
384
385 /**
386 * Grab users associate MainWP License key for the selected Extension.
387 *
388 * @param array $api_slug Extension activation info.
389 * @param string $master_api_key MainWP master api key.
390 *
391 * @return mixed Activation info.
392 *
393 * @uses \MainWP\Dashboard\MainWP_Api_Manager_Key::grab_api_key()
394 */
395 public function grab_license_key( $api_slug, $master_api_key ) { // phpcs:ignore -- Current complexity is the only way to achieve desired results, pull request solutions appreciated.
396
397 $options = $this->get_activation_info( $api_slug );
398 if ( ! is_array( $options ) ) {
399 $options = array();
400 }
401 $activation_status = isset( $options['activated_key'] ) ? $options['activated_key'] : '';
402 $api_key = isset( $options['api_key'] ) ? $options['api_key'] : '';
403
404 if ( ! isset( $options['product_item_id'] ) ) {
405 $options['product_item_id'] = 0; // to compatible.
406 }
407
408 if ( 'Deactivated' == $activation_status || '' == $activation_status || '' == $api_key || empty( $options['product_item_id'] ) ) {
409 $return = array();
410 if ( ! empty( $master_api_key ) ) {
411 $args = array(
412 'api_key' => $master_api_key,
413 'product_id' => isset( $options['product_id'] ) ? $options['product_id'] : '',
414 'instance' => isset( $options['instance_id'] ) ? $options['instance_id'] : '',
415 'software_version' => isset( $options['software_version'] ) ? $options['software_version'] : '',
416 'object' => $this->domain,
417 );
418
419 $activate_results = MainWP_Api_Manager_Key::instance()->grab_api_key( $args );
420 if ( ! empty( $activate_results ) ) {
421 $activate_results = json_decode( $activate_results, true );
422 } else {
423 $activate_results = array();
424 }
425
426 $options['api_key'] = '';
427 $options['activated_key'] = 'Deactivated';
428
429 if ( ! isset( $options['product_item_id'] ) ) {
430 $options['product_item_id'] = 0; // to compatible.
431 }
432
433 if ( is_array( $activate_results ) && isset( $activate_results['activated'] ) && ( true == $activate_results['activated'] ) && ! empty( $activate_results['api_key'] ) ) {
434 $return['result'] = 'SUCCESS';
435 $mess = isset( $activate_results['message'] ) ? $activate_results['message'] : '';
436 $return['message'] = esc_html__( 'Extension activated. ', 'mainwp' ) . $mess;
437 $options['api_key'] = $activate_results['api_key'];
438 $return['api_key'] = $activate_results['api_key'];
439 $options['activated_key'] = 'Activated';
440 $options['product_item_id'] = isset( $activate_results['product_item_id'] ) ? intval( $activate_results['product_item_id'] ) : 0;
441 $options['deactivate_checkbox'] = 'off';
442 } else {
443
444 if ( false == $activate_results ) {
445 $return['error'] = esc_html__( 'Connection with the API license server could not be established. Please, try again later.', 'mainwp' );
446 } elseif ( isset( $activate_results['error'] ) ) {
447 $return['error'] = $activate_results['error'];
448 } elseif ( empty( $activate_results['api_key'] ) ) {
449 $return['error'] = esc_html__( 'License key could not be found.', 'mainwp' );
450 } else {
451 $return['error'] = esc_html__( 'An undefined error occurred. Please try again later.', 'mainwp' );
452 }
453 }
454
455 $error = $this->check_response_for_api_errors( $activate_results );
456 if ( ! empty( $error ) ) {
457 $return['error'] = $error;
458 }
459
460 $this->set_activation_info( $api_slug, $options );
461
462 return $return;
463 } else {
464 return array( 'error' => esc_html__( 'MainWP API key are required in order to grab extensions API keys.', 'mainwp' ) );
465 }
466 }
467
468 return array( 'result' => 'SUCCESS' );
469 }
470
471 /**
472 * Check if $response contains any api errors.
473 *
474 * @param array $response Response array.
475 *
476 * @return string $error Error message.
477 */
478 public function check_response_for_api_errors( $response ) {
479 if ( ! is_array( $response ) || ! isset( $response['code'] ) ) {
480 return false;
481 }
482
483 if ( isset( $response['error'] ) ) {
484 return $response['error'];
485 }
486
487 $error = '';
488 switch ( $response['code'] ) {
489 case '100':
490 $error = esc_html__( 'Activation error! Please try to deactivate and re-activate the extension on the WP > Plugins page and try to activate API key again.', 'mainwp' );
491 break;
492 case '102':
493 $error = esc_html__( 'Activation error! Download permission for this product could not be found.', 'mainwp' );
494 break;
495 case '101':
496 $error = esc_html__( 'Activation error! Matching API key could not be found.', 'mainwp' );
497 break;
498 case '103':
499 case '104':
500 $error = esc_html__( 'Invalid Instance ID! Please try to deactivate and re-activate the extension on the WP > Plugins page and try to activate API key again.', 'mainwp' );
501 break;
502 case '105':
503 case '106':
504 $error = isset( $response['error'] ) ? $response['error'] : '';
505 $info = isset( $response['additional info'] ) ? ' ' . $response['additional info'] : '';
506 $error = $error . $info;
507 break;
508 case '900':
509 $error = esc_html__( 'Your membership is on hold. Reactivate your membership to activate MainWP extensions', 'mainwp' );
510 break;
511 case '901':
512 $error = esc_html__( 'Your membership has been canceled. Reactivate your membership to activate MainWP extensions', 'mainwp' );
513 break;
514 case '902':
515 $error = esc_html__( 'Your membership has expired. Reactivate your membership to activate MainWP extensions', 'mainwp' );
516 break;
517 }
518 return $error;
519 }
520
521 /**
522 * Check if $response contains any install errors.
523 *
524 * @param array $response Response array.
525 * @param string $software_title Extension title.
526 *
527 * @return string $return Installation Error messages.
528 */
529 public function check_response_for_intall_errors( $response, $software_title = '' ) {
530 if ( ! is_array( $response ) || ! isset( $response['error'] ) ) {
531 return false;
532 }
533
534 $return = false;
535 switch ( $response['error'] ) {
536 case 'subscription_on_hold':
537 $return = esc_html__( 'Your membership is on hold. Reactivate your membership to install MainWP extensions.', 'mainwp' );
538 break;
539 case 'subscription_cancelled':
540 $return = esc_html__( 'Your membership has been canceled. Reactivate your membership to install MainWP extensions.', 'mainwp' );
541 break;
542 case 'subscription_expired':
543 $return = esc_html__( 'Your membership has expired. Reactivate your membership to install MainWP extensions.', 'mainwp' );
544 break;
545 default: // download_revoked.
546 $return = sprintf( esc_html__( 'Download permission for %1$s has been revoked possibly due to a license key or membership expiring. You can reactivate or purchase a license key from your account <a href="%2$s" target="_blank">dashboard</a>.', 'mainwp' ), $software_title, $this->renew_license_url );
547 break;
548 }
549 return $return;
550 }
551
552 /**
553 * Request plugin information
554 *
555 * @param array $args Request arguments.
556 *
557 * @return array Plugin info.
558 *
559 * @uses MainWP_Api_Manager_Plugin_Update::request()
560 */
561 public function request_extension_information( $args ) {
562 return MainWP_Api_Manager_Plugin_Update::instance()->request( $args );
563 }
564
565 }
566
567 // End of class.
568