PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 6.0.11
MainWP Dashboard: Self-hosted WordPress Management for Agencies v6.0.11
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 6.0.11, at class/class-mainwp-api-manager.php

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