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

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