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

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