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-rest-api.php

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

3,567 lines 104.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * MainWP REST API
4 *
5 * This class handles the REST API
6 *
7 * @package MainWP/REST API
8 * @author Martin Gibson
9 */
10
11 namespace MainWP\Dashboard;
12
13 use Exception;
14
15 /**
16 * Class Rest_Api
17 *
18 * @package MainWP\Dashboard
19 */
20 class Rest_Api {
21
22 // phpcs:disable Generic.Metrics.CyclomaticComplexity -- complexity.
23
24 /**
25 * Protected static variable to hold the single instance of the class.
26 *
27 * @var mixed Default null
28 */
29 private static $instance = null;
30
31 /**
32 * Method instance()
33 *
34 * Create public static instance.
35 *
36 * @static
37 * @return self::$instance
38 */
39 public static function instance() {
40 if ( null == self::$instance ) {
41 self::$instance = new self();
42 }
43 return self::$instance;
44 }
45
46 /**
47 * Method init()
48 *
49 * Adds an action to generate API credentials.
50 * Adds an action to create the rest API endpoints if activated in the plugin settings.
51 */
52 public function init() {
53
54 // add ajax action.
55 MainWP_Post_Handler::instance()->add_post_action( 'mainwp_generate_api_credentials', array( &$this, 'mainwp_generate_api_credentials' ) );
56 add_filter( 'mainwp_rest_api_validate', array( &$this, 'rest_api_validate' ), 10, 2 );
57 // only activate the api if enabled in the plugin settings.
58 if ( get_option( 'mainwp_enable_rest_api' ) ) {
59 // check to see whether activated or not.
60 $activated = get_option( 'mainwp_enable_rest_api' );
61
62 if ( $activated ) {
63 // run API.
64 add_action( 'rest_api_init', array( &$this, 'mainwp_register_routes' ) );
65 }
66 }
67 }
68
69 /**
70 * Method mainwp_generate_rand_hash()
71 *
72 * Generates a random hash to be used when generating the consumer key and secret.
73 *
74 * @return string Returns random string.
75 */
76 public function mainwp_generate_rand_hash() {
77 if ( ! function_exists( 'openssl_random_pseudo_bytes' ) ) {
78 return sha1( wp_rand() );
79 }
80
81 return bin2hex( openssl_random_pseudo_bytes( 20 ) ); // @codingStandardsIgnoreLine
82 }
83
84 /**
85 * Method mainwp_generate_api_credentials()
86 *
87 * Generates consumer key and secret and saves to the database encrypted.
88 */
89 public function mainwp_generate_api_credentials() {
90
91 MainWP_Post_Handler::instance()->secure_request( 'mainwp_generate_api_credentials' );
92
93 // we need to generate a consumer key and secret and return the result and save it into the database.
94 $consumer_key = 'ck_' . $this->mainwp_generate_rand_hash();
95 $consumer_secret = 'cs_' . $this->mainwp_generate_rand_hash();
96
97 $return_data = array(
98 'consumer_key' => $consumer_key,
99 'consumer_secret' => $consumer_secret,
100 );
101
102 // hash the password.
103 $consumer_key_hashed = wp_hash_password( $consumer_key );
104 $consumer_secret_hashed = wp_hash_password( $consumer_secret );
105
106 // store the data.
107 update_option( 'mainwp_rest_api_consumer_key', $consumer_key_hashed );
108 update_option( 'mainwp_rest_api_consumer_secret', $consumer_secret_hashed );
109
110 wp_die( wp_json_encode( $return_data ) );
111 }
112
113 /**
114 * Protected variable to hold the API version.
115 *
116 * @var string API version
117 */
118 protected $api_version = '1';
119
120 /**
121 * Method mainwp_rest_api_init()
122 *
123 * Creates the necessary endpoints for the api.
124 * Note, for a request to be successful the URL query parameters consumer_key and consumer_secret need to be set and correct.
125 */
126 public function mainwp_register_routes() {
127
128 // Create an array which holds all the endpoints. Method can be GET, POST, PUT, DELETE.
129 $endpoints = array(
130 array(
131 'route' => 'sites',
132 'method' => 'GET',
133 'callback' => 'all-sites',
134 ),
135 array(
136 'route' => 'sites',
137 'method' => 'GET',
138 'callback' => 'all-sites-count',
139 ),
140 array(
141 'route' => 'sites',
142 'method' => 'GET',
143 'callback' => 'connected-sites',
144 ),
145 array(
146 'route' => 'sites',
147 'method' => 'GET',
148 'callback' => 'connected-sites-count',
149 ),
150 array(
151 'route' => 'sites',
152 'method' => 'GET',
153 'callback' => 'disconnected-sites',
154 ),
155 array(
156 'route' => 'sites',
157 'method' => 'GET',
158 'callback' => 'disconnected-sites-count',
159 ),
160 array(
161 'route' => 'sites',
162 'method' => 'POST',
163 'callback' => 'sync-sites',
164 ),
165 array(
166 'route' => 'sites',
167 'method' => 'POST',
168 'callback' => 'check-sites',
169 ),
170 array(
171 'route' => 'sites',
172 'method' => 'POST',
173 'callback' => 'disconnect-sites',
174 ),
175 array(
176 'route' => 'sites',
177 'method' => 'GET',
178 'callback' => 'http-status',
179 ),
180 array(
181 'route' => 'sites',
182 'method' => 'GET',
183 'callback' => 'health-score',
184 ),
185 array(
186 'route' => 'sites',
187 'method' => 'GET',
188 'callback' => 'security-issues',
189 ),
190 array(
191 'route' => 'sites',
192 'method' => 'GET',
193 'callback' => 'sites-available-updates-count',
194 ),
195 array(
196 'route' => 'sites',
197 'method' => 'GET',
198 'callback' => 'get-sites-by-url',
199 ),
200 array(
201 'route' => 'sites',
202 'method' => 'GET',
203 'callback' => 'get-sites-by-client',
204 ),
205 array(
206 'route' => 'site',
207 'method' => 'GET',
208 'callback' => 'site',
209 ),
210 array(
211 'route' => 'site',
212 'method' => 'GET',
213 'callback' => 'site-info',
214 ),
215 array(
216 'route' => 'site',
217 'method' => 'GET',
218 'callback' => 'site-installed-plugins',
219 ),
220 array(
221 'route' => 'site',
222 'method' => 'GET',
223 'callback' => 'site-installed-plugins-count',
224 ),
225 array(
226 'route' => 'site',
227 'method' => 'GET',
228 'callback' => 'site-active-plugins',
229 ),
230 array(
231 'route' => 'site',
232 'method' => 'GET',
233 'callback' => 'site-active-plugins-count',
234 ),
235 array(
236 'route' => 'site',
237 'method' => 'GET',
238 'callback' => 'site-inactive-plugins',
239 ),
240 array(
241 'route' => 'site',
242 'method' => 'GET',
243 'callback' => 'site-inactive-plugins-count',
244 ),
245 array(
246 'route' => 'site',
247 'method' => 'GET',
248 'callback' => 'site-installed-themes',
249 ),
250 array(
251 'route' => 'site',
252 'method' => 'GET',
253 'callback' => 'site-installed-themes-count',
254 ),
255 array(
256 'route' => 'site',
257 'method' => 'GET',
258 'callback' => 'site-active-themes',
259 ),
260 array(
261 'route' => 'site',
262 'method' => 'GET',
263 'callback' => 'site-inactive-themes',
264 ),
265 array(
266 'route' => 'site',
267 'method' => 'GET',
268 'callback' => 'site-inactive-themes-count',
269 ),
270 array(
271 'route' => 'site',
272 'method' => 'GET',
273 'callback' => 'site-available-updates',
274 ),
275 array(
276 'route' => 'site',
277 'method' => 'GET',
278 'callback' => 'site-available-updates-count',
279 ),
280 array(
281 'route' => 'site',
282 'method' => 'GET',
283 'callback' => 'site-abandoned-plugins',
284 ),
285 array(
286 'route' => 'site',
287 'method' => 'GET',
288 'callback' => 'site-abandoned-plugins-count',
289 ),
290 array(
291 'route' => 'site',
292 'method' => 'GET',
293 'callback' => 'site-abandoned-themes',
294 ),
295 array(
296 'route' => 'site',
297 'method' => 'GET',
298 'callback' => 'site-abandoned-themes-count',
299 ),
300 array(
301 'route' => 'site',
302 'method' => 'GET',
303 'callback' => 'site-http-status',
304 ),
305 array(
306 'route' => 'site',
307 'method' => 'GET',
308 'callback' => 'site-health-score',
309 ),
310 array(
311 'route' => 'site',
312 'method' => 'GET',
313 'callback' => 'site-security-issues',
314 ),
315 array(
316 'route' => 'site',
317 'method' => 'POST',
318 'callback' => 'add-site',
319 ),
320 array(
321 'route' => 'site',
322 'method' => 'PUT',
323 'callback' => 'edit-site',
324 ),
325 array(
326 'route' => 'site',
327 'method' => 'POST',
328 'callback' => 'sync-site',
329 ),
330 array(
331 'route' => 'site',
332 'method' => 'POST',
333 'callback' => 'reconnect-site',
334 ),
335 array(
336 'route' => 'site',
337 'method' => 'POST',
338 'callback' => 'disconnect-site',
339 ),
340 array(
341 'route' => 'site',
342 'method' => 'DELETE',
343 'callback' => 'remove-site',
344 ),
345 array(
346 'route' => 'site',
347 'method' => 'PUT',
348 'callback' => 'site-update-wordpress',
349 ),
350 array(
351 'route' => 'site',
352 'method' => 'PUT',
353 'callback' => 'site-update-plugins',
354 ),
355 array(
356 'route' => 'site',
357 'method' => 'PUT',
358 'callback' => 'site-update-themes',
359 ),
360 array(
361 'route' => 'site',
362 'method' => 'PUT',
363 'callback' => 'site-update-translations',
364 ),
365 array(
366 'route' => 'site',
367 'method' => 'PUT',
368 'callback' => 'site-update-item',
369 ),
370 array(
371 'route' => 'site',
372 'method' => 'POST',
373 'callback' => 'site-manage-plugin',
374 ),
375 array(
376 'route' => 'site',
377 'method' => 'POST',
378 'callback' => 'site-manage-theme',
379 ),
380 array(
381 'route' => 'site',
382 'method' => 'POST',
383 'callback' => 'check-site-http-status',
384 ),
385 array(
386 'route' => 'updates',
387 'method' => 'GET',
388 'callback' => 'available-updates',
389 ),
390 array(
391 'route' => 'updates',
392 'method' => 'GET',
393 'callback' => 'ignored-plugins-updates',
394 ),
395 array(
396 'route' => 'updates',
397 'method' => 'GET',
398 'callback' => 'site-ignored-plugins-updates',
399 ),
400 array(
401 'route' => 'updates',
402 'method' => 'GET',
403 'callback' => 'ignored-themes-updates',
404 ),
405 array(
406 'route' => 'updates',
407 'method' => 'GET',
408 'callback' => 'site-ignored-themes-updates',
409 ),
410 array(
411 'route' => 'updates',
412 'method' => 'POST',
413 'callback' => 'ignore-updates',
414 ),
415 array(
416 'route' => 'updates',
417 'method' => 'POST',
418 'callback' => 'ignore-update',
419 ),
420 array(
421 'route' => 'updates',
422 'method' => 'POST',
423 'callback' => 'unignore-updates',
424 ),
425 array(
426 'route' => 'updates',
427 'method' => 'POST',
428 'callback' => 'unignore-update',
429 ),
430 array(
431 'route' => 'client',
432 'method' => 'POST',
433 'callback' => 'add-client',
434 ),
435 array(
436 'route' => 'client',
437 'method' => 'PUT',
438 'callback' => 'edit-client',
439 ),
440 array(
441 'route' => 'client',
442 'method' => 'GET',
443 'callback' => 'remove-client',
444 ),
445 array(
446 'route' => 'clients',
447 'method' => 'GET',
448 'callback' => 'all-clients',
449 ),
450 array(
451 'route' => 'tags',
452 'method' => 'GET',
453 'callback' => 'all-tags',
454 ),
455 );
456
457 // loop through the endpoints.
458 foreach ( $endpoints as $endpoint ) {
459
460 $function_name = str_replace( '-', '_', $endpoint['callback'] );
461
462 register_rest_route(
463 'mainwp/v' . $this->api_version,
464 '/' . $endpoint['route'] . '/' . $endpoint['callback'],
465 array(
466 'methods' => $endpoint['method'],
467 'callback' => array( &$this, 'mainwp_rest_api_' . $function_name . '_callback' ),
468 'permission_callback' => '__return_true',
469 )
470 );
471 }
472 }
473
474 /**
475 * Method mainwp_rest_api_init()
476 *
477 * Makes sure the correct consumer key and secret are entered.
478 *
479 * @param array $request The request made in the API call which includes all parameters.
480 *
481 * @return bool Whether the api credentials are valid.
482 */
483 public function mainwp_validate_request( $request ) {
484
485 $consumer_key = null;
486 $consumer_secret = null;
487
488 if ( ! empty( $request['consumer_key'] ) && ! empty( $request['consumer_secret'] ) ) {
489 // users entered consumer key and secret.
490 $consumer_key = $request['consumer_key'];
491 $consumer_secret = $request['consumer_secret'];
492 } else {
493 $headers = apache_request_headers();
494
495 if ( isset( $headers['x-api-key'] ) ) {
496 $header_keys = $headers['x-api-key'];
497 $api_keys = json_decode( $header_keys, true );
498 if ( is_array( $api_keys ) && isset( $api_keys['consumer_key'] ) ) {
499 // users entered consumer key and secret.
500 $consumer_key = $api_keys['consumer_key'];
501 $consumer_secret = $api_keys['consumer_secret'];
502 }
503 }
504 }
505
506 // data stored in database.
507 $consumer_key_option = get_option( 'mainwp_rest_api_consumer_key' );
508 $consumer_secret_option = get_option( 'mainwp_rest_api_consumer_secret' );
509
510 if ( wp_check_password( $consumer_key, $consumer_key_option ) && wp_check_password( $consumer_secret, $consumer_secret_option ) ) {
511 if ( ! defined( 'MAINWP_REST_API' ) ) {
512 define( 'MAINWP_REST_API', true );
513 }
514 return true;
515 } else {
516 return false;
517 }
518 }
519
520
521 /**
522 * Method rest_api_validate()
523 *
524 * Hook validate the request.
525 *
526 * @param bool $false input filter value.
527 * @param array $request The request made in the API call which includes all parameters.
528 *
529 * @return bool Whether the api credentials are valid.
530 */
531 public function rest_api_validate( $false, $request ) {
532 return $this->mainwp_validate_request( $request );
533 }
534
535 /**
536 * Method mainwp_authentication_error()
537 *
538 * Common error message when consumer key and secret are wrong.
539 *
540 * @return array $response Array with an error message explaining that the credentials are wrong.
541 */
542 public function mainwp_authentication_error() {
543
544 $data = array( 'ERROR' => esc_html__( 'Incorrect or missing consumer key and/or secret. If the issue persists please reset your authentication details from the MainWP > Settings > REST API page, on your MainWP Dashboard site.', 'mainwp' ) );
545
546 $response = new \WP_REST_Response( $data );
547 $response->set_status( 401 );
548
549 return $response;
550 }
551
552 /**
553 * Method mainwp_missing_data_error()
554 *
555 * Common error message when data is missing from the request.
556 *
557 * @return array $response Array with an error message explaining details are missing.
558 */
559 public function mainwp_missing_data_error() {
560
561 $data = array( 'ERROR' => esc_html__( 'Required parameter is missing.', 'mainwp' ) );
562
563 $response = new \WP_REST_Response( $data );
564 $response->set_status( 400 );
565
566 return $response;
567 }
568
569 /**
570 * Method mainwp_invalid_data_error()
571 *
572 * Common error message when data in request is ivalid.
573 *
574 * @param string $error Error message.
575 *
576 * @return array $response Array with an error message explaining details are missing.
577 */
578 public function mainwp_invalid_data_error( $error = '' ) {
579
580 if ( ! empty( $error ) ) {
581 $data = array( 'ERROR' => $error );
582 } else {
583 $data = array( 'ERROR' => esc_html__( 'Required parameter data is is not valid.', 'mainwp' ) );
584 }
585
586 $response = new \WP_REST_Response( $data );
587 $response->set_status( 400 );
588
589 return $response;
590 }
591
592 /**
593 * Method mainwp_run_process_success()
594 *
595 * Common error message when data is missing from the request.
596 *
597 * @return array $response Array with an error message explaining details are missing.
598 */
599 public function mainwp_run_process_success() {
600
601 $data = array( 'SUCCESS' => esc_html__( 'Process ran.', 'mainwp' ) );
602
603 $response = new \WP_REST_Response( $data );
604 $response->set_status( 200 );
605
606 return $response;
607 }
608
609 /**
610 * Method mainwp_rest_api_all_sites_callback()
611 *
612 * Callback function for managing the response to API requests made for the endpoint: all-sites
613 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/sites/all-sites
614 * API Method: GET
615 *
616 * @param array $request The request made in the API call which includes all parameters.
617 *
618 * @return object $response An object that contains the return data and status of the API request.
619 */
620 public function mainwp_rest_api_all_sites_callback( $request ) {
621
622 // first validate the request.
623 if ( $this->mainwp_validate_request( $request ) ) {
624
625 $params = array(
626 'selectgroups' => ( isset( $request['selectgroups'] ) && true == $request['selectgroups'] ) ? true : false,
627 );
628
629 $format = isset( $request['format'] ) ? $request['format'] : '';
630 $params['format'] = $format;
631 $params['full_data'] = isset( $request['full_data'] ) ? $request['full_data'] : false;
632
633 // get data.
634 $data = MainWP_DB::instance()->get_websites_for_current_user( $params );
635
636 $result = $data;
637
638 if ( 'array' == $format ) {
639 if ( is_array( $data ) ) {
640 $result = array(
641 'data' => $data,
642 );
643 }
644 }
645
646 $response = new \WP_REST_Response( $result );
647 $response->set_status( 200 );
648
649 } else {
650 // throw common error.
651 $response = $this->mainwp_authentication_error();
652 }
653
654 return $response;
655 }
656
657 /**
658 * Method mainwp_rest_api_all_sites_count_callback()
659 *
660 * Callback function for managing the response to API requests made for the endpoint: all-sites-count
661 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/sites/all-sites-count
662 * API Method: GET
663 *
664 * @param array $request The request made in the API call which includes all parameters.
665 *
666 * @return object $response An object that contains the return data and status of the API request.
667 */
668 public function mainwp_rest_api_all_sites_count_callback( $request ) {
669
670 // first validate the request.
671 if ( $this->mainwp_validate_request( $request ) ) {
672
673 // get data.
674 $websites = MainWP_DB::instance()->get_websites_for_current_user();
675
676 $data = array(
677 'count' => count( $websites ),
678 );
679
680 $response = new \WP_REST_Response( $data );
681 $response->set_status( 200 );
682
683 } else {
684 // throw common error.
685 $response = $this->mainwp_authentication_error();
686 }
687
688 return $response;
689 }
690
691 /**
692 * Method mainwp_rest_api_connected_sites_callback()
693 *
694 * Callback function for managing the response to API requests made for the endpoint: connected-sites
695 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/sites/connected-sites
696 * API Method: GET
697 *
698 * @param array $request The request made in the API call which includes all parameters.
699 *
700 * @return object $response An object that contains the return data and status of the API request.
701 */
702 public function mainwp_rest_api_connected_sites_callback( $request ) {
703
704 // first validate the request.
705 if ( $this->mainwp_validate_request( $request ) ) {
706
707 // get data.
708 $data = MainWP_DB::instance()->get_connected_websites();
709
710 $response = new \WP_REST_Response( $data );
711 $response->set_status( 200 );
712
713 } else {
714 // throw common error.
715 $response = $this->mainwp_authentication_error();
716 }
717
718 return $response;
719 }
720
721 /**
722 * Method mainwp_rest_api_connected_sites_count_callback()
723 *
724 * Callback function for managing the response to API requests made for the endpoint: connected-sites-count
725 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/sites/connected-sites-count
726 * API Method: GET
727 *
728 * @param array $request The request made in the API call which includes all parameters.
729 *
730 * @return object $response An object that contains the return data and status of the API request.
731 */
732 public function mainwp_rest_api_connected_sites_count_callback( $request ) {
733
734 // first validate the request.
735 if ( $this->mainwp_validate_request( $request ) ) {
736
737 // get data.
738 $websites = MainWP_DB::instance()->get_connected_websites();
739
740 $data = array(
741 'count' => count( $websites ),
742 );
743
744 $response = new \WP_REST_Response( $data );
745 $response->set_status( 200 );
746
747 } else {
748 // throw common error.
749 $response = $this->mainwp_authentication_error();
750 }
751
752 return $response;
753 }
754
755 /**
756 * Method mainwp_rest_api_disconnected_sites_callback()
757 *
758 * Callback function for managing the response to API requests made for the endpoint: disconnected-sites
759 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/sites/disconnected-sites
760 * API Method: GET
761 *
762 * @param array $request The request made in the API call which includes all parameters.
763 *
764 * @return object $response An object that contains the return data and status of the API request.
765 */
766 public function mainwp_rest_api_disconnected_sites_callback( $request ) {
767
768 // first validate the request.
769 if ( $this->mainwp_validate_request( $request ) ) {
770
771 // get data.
772 $data = MainWP_DB::instance()->get_disconnected_websites();
773
774 $response = new \WP_REST_Response( $data );
775 $response->set_status( 200 );
776
777 } else {
778 // throw common error.
779 $response = $this->mainwp_authentication_error();
780 }
781
782 return $response;
783 }
784
785 /**
786 * Method mainwp_rest_api_disconnected_sites_count_callback()
787 *
788 * Callback function for managing the response to API requests made for the endpoint: disconnected-sites-count
789 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/sites/disconnected-sites-count
790 * API Method: GET
791 *
792 * @param array $request The request made in the API call which includes all parameters.
793 *
794 * @return object $response An object that contains the return data and status of the API request.
795 */
796 public function mainwp_rest_api_disconnected_sites_count_callback( $request ) {
797
798 // first validate the request.
799 if ( $this->mainwp_validate_request( $request ) ) {
800
801 // get data.
802 $websites = MainWP_DB::instance()->get_disconnected_websites();
803
804 $data = array(
805 'count' => count( $websites ),
806 );
807
808 $response = new \WP_REST_Response( $data );
809 $response->set_status( 200 );
810
811 } else {
812 // throw common error.
813 $response = $this->mainwp_authentication_error();
814 }
815
816 return $response;
817 }
818
819 /**
820 * Method mainwp_rest_api_sync_sites_callback()
821 *
822 * Callback function for managing the response to API requests made for the endpoint: sync-sites
823 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/sites/sync-sites
824 * API Method: POST
825 *
826 * @param array $request The request made in the API call which includes all parameters.
827 *
828 * @return object $response An object that contains the return data and status of the API request.
829 */
830 public function mainwp_rest_api_sync_sites_callback( $request ) {
831
832 // first validate the request.
833 if ( $this->mainwp_validate_request( $request ) ) {
834
835 $data = array();
836
837 $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user() );
838 while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
839 try {
840 $ret = MainWP_Sync::sync_site( $website );
841 } catch ( \Exception $e ) {
842 $ret = false;
843 }
844 $data[ $website->id ] = $ret ? 'success' : 'failed';
845 }
846 MainWP_DB::free_result( $websites );
847
848 update_option( 'mainwp_last_synced_all_sites', time() );
849
850 $response = new \WP_REST_Response( $data );
851 $response->set_status( 200 );
852
853 } else {
854 // throw common error.
855 $response = $this->mainwp_authentication_error();
856 }
857
858 return $response;
859 }
860
861 /**
862 * Method mainwp_rest_api_check_sites_callback()
863 *
864 * Callback function for managing the response to API requests made for the endpoint: check-sites
865 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/sites/check-sites
866 * API Method: POST
867 *
868 * @param array $request The request made in the API call which includes all parameters.
869 *
870 * @return object $response An object that contains the return data and status of the API request.
871 */
872 public function mainwp_rest_api_check_sites_callback( $request ) {
873
874 // first validate the request.
875 if ( $this->mainwp_validate_request( $request ) ) {
876
877 $data = array();
878
879 $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user() );
880 while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
881 try {
882 $ret = MainWP_Monitoring_Handler::handle_check_website( $website );
883 } catch ( \Exception $e ) {
884 $ret = false;
885 }
886 $data[ $website->id ] = $ret ? 'success' : 'failed';
887 }
888 MainWP_DB::free_result( $websites );
889
890 $response = new \WP_REST_Response( $data );
891 $response->set_status( 200 );
892
893 } else {
894 // throw common error.
895 $response = $this->mainwp_authentication_error();
896 }
897
898 return $response;
899 }
900
901 /**
902 * Method mainwp_rest_api_disconnect_sites_callback()
903 *
904 * Callback function for managing the response to API requests made for the endpoint: disconnect-sites
905 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/sites/disconnect-sites
906 * API Method: POST
907 *
908 * @param array $request The request made in the API call which includes all parameters.
909 *
910 * @return object $response An object that contains the return data and status of the API request.
911 */
912 public function mainwp_rest_api_disconnect_sites_callback( $request ) {
913
914 // first validate the request.
915 if ( $this->mainwp_validate_request( $request ) ) {
916
917 $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user() );
918 while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
919 try {
920 MainWP_Connect::fetch_url_authed( $website, 'disconnect' );
921 } catch ( \Exception $e ) {
922 // ok.
923 }
924 }
925 MainWP_DB::free_result( $websites );
926
927 // do common process response.
928 $response = $this->mainwp_run_process_success();
929
930 } else {
931 // throw common error.
932 $response = $this->mainwp_authentication_error();
933 }
934
935 return $response;
936 }
937
938 /**
939 * Method mainwp_rest_api_http_status_callback()
940 *
941 * Callback function for managing the response to API requests made for the endpoint: http-status
942 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/sites/http-status
943 * API Method: POST
944 *
945 * @param array $request The request made in the API call which includes all parameters.
946 *
947 * @return object $response An object that contains the return data and status of the API request.
948 */
949 public function mainwp_rest_api_http_status_callback( $request ) {
950 // first validate the request.
951 if ( $this->mainwp_validate_request( $request ) ) {
952 $data = array();
953 $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user() );
954 while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
955 $data[ $website->id ] = $website->http_response_code;
956 }
957 MainWP_DB::free_result( $websites );
958 $response = new \WP_REST_Response( $data );
959 $response->set_status( 200 );
960 } else {
961 // throw common error.
962 $response = $this->mainwp_authentication_error();
963 }
964 return $response;
965 }
966
967
968 /**
969 * Method mainwp_rest_api_health_score_callback()
970 *
971 * Callback function for managing the response to API requests made for the endpoint: health-score
972 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/sites/health-score
973 * API Method: POST
974 *
975 * @param array $request The request made in the API call which includes all parameters.
976 *
977 * @return object $response An object that contains the return data and status of the API request.
978 */
979 public function mainwp_rest_api_health_score_callback( $request ) {
980
981 // first validate the request.
982 if ( $this->mainwp_validate_request( $request ) ) {
983 $data = array();
984 $params['extra_view'] = array( 'health_site_status' );
985 $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user() );
986 while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
987 $health_status = isset( $website->health_site_status ) ? json_decode( $website->health_site_status, true ) : array();
988 $hstatus = MainWP_Utility::get_site_health( $health_status );
989 $hval = $hstatus['val'];
990 $critical = $hstatus['critical'];
991 if ( 80 <= $hval && 0 == $critical ) {
992 $health_score = 'Good';
993 } else {
994 $health_score = 'Should be improved';
995 }
996 $data[ $website->id ] = $health_score;
997 }
998 MainWP_DB::free_result( $websites );
999 $response = new \WP_REST_Response( $data );
1000 $response->set_status( 200 );
1001 } else {
1002 // throw common error.
1003 $response = $this->mainwp_authentication_error();
1004 }
1005
1006 return $response;
1007 }
1008
1009 /**
1010 * Method mainwp_rest_api_security_issues_callback()
1011 *
1012 * Callback function for managing the response to API requests made for the endpoint: security-issues
1013 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/sites/security-issues
1014 * API Method: POST
1015 *
1016 * @param array $request The request made in the API call which includes all parameters.
1017 *
1018 * @return object $response An object that contains the return data and status of the API request.
1019 */
1020 public function mainwp_rest_api_security_issues_callback( $request ) {
1021
1022 // first validate the request.
1023 if ( $this->mainwp_validate_request( $request ) ) {
1024 $data = array();
1025 $params['extra_view'] = array( 'health_site_status' );
1026 $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user() );
1027 while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
1028 $ret = MainWP_Connect::fetch_url_authed( $website, 'security' );
1029 $data[ $website->id ] = $ret;
1030 }
1031 MainWP_DB::free_result( $websites );
1032 $response = new \WP_REST_Response( $data );
1033 $response->set_status( 200 );
1034 } else {
1035 // throw common error.
1036 $response = $this->mainwp_authentication_error();
1037 }
1038
1039 return $response;
1040 }
1041
1042 /**
1043 * Method mainwp_rest_api_sites_available_updates_count_callback()
1044 *
1045 * Callback function for managing the response to API requests made for the endpoint: sites-available-updates-count
1046 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/sites/sites-available-updates-count
1047 * API Method: GET
1048 *
1049 * @param array $request The request made in the API call which includes all parameters.
1050 *
1051 * @return object $response An object that contains the return data and status of the API request.
1052 */
1053 public function mainwp_rest_api_sites_available_updates_count_callback( $request ) {
1054
1055 // first validate the request.
1056 if ( $this->mainwp_validate_request( $request ) ) {
1057 $total_upgrades = MainWP_Common_Handler::instance()->sites_available_updates_count();
1058 $response = new \WP_REST_Response( $total_upgrades );
1059 } else {
1060 // throw common error.
1061 $response = $this->mainwp_authentication_error();
1062 }
1063
1064 return $response;
1065 }
1066
1067 /**
1068 * Method mainwp_rest_api_get_sites_by_url_callback()
1069 *
1070 * Callback function for managing the response to API requests made for the endpoint: get-sites-by-url
1071 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/sites/get-sites-by-url
1072 * API Method: GET
1073 *
1074 * @param array $request The request made in the API call which includes all parameters.
1075 *
1076 * @return object $response An object that contains the return data and status of the API request.
1077 */
1078 public function mainwp_rest_api_get_sites_by_url_callback( $request ) {
1079
1080 // first validate the request.
1081 if ( $this->mainwp_validate_request( $request ) ) {
1082
1083 $params = array(
1084 'full_data' => true,
1085 'selectgroups' => ( isset( $request['selectgroups'] ) && true == $request['selectgroups'] ) ? true : false,
1086 );
1087
1088 $format = isset( $request['format'] ) ? $request['format'] : '';
1089 $params['format'] = $format;
1090
1091 if ( isset( $request['urls'] ) && ! empty( $request['urls'] ) ) {
1092 $params['urls'] = rawurldecode( $request['urls'] );
1093 }
1094
1095 // get data.
1096 $data = MainWP_DB::instance()->get_websites_for_current_user( $params );
1097
1098 $result = $data;
1099
1100 if ( 'array' == $format ) {
1101 if ( is_array( $data ) ) {
1102 $result = array(
1103 'data' => $data,
1104 );
1105 }
1106 }
1107
1108 $response = new \WP_REST_Response( $result );
1109 $response->set_status( 200 );
1110
1111 } else {
1112 // throw common error.
1113 $response = $this->mainwp_authentication_error();
1114 }
1115
1116 return $response;
1117 }
1118
1119 /**
1120 * Method mainwp_rest_api_get_sites_by_client_callback()
1121 *
1122 * Callback function for managing the response to API requests made for the endpoint: get-sites-by-client
1123 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/sites/get-sites-by-client
1124 * API Method: GET
1125 *
1126 * @param array $request The request made in the API call which includes all parameters.
1127 *
1128 * @return object $response An object that contains the return data and status of the API request.
1129 */
1130 public function mainwp_rest_api_get_sites_by_client_callback( $request ) {
1131
1132 // first validate the request.
1133 if ( $this->mainwp_validate_request( $request ) ) {
1134
1135 $params = array(
1136 'full_data' => true,
1137 'selectgroups' => ( isset( $request['selectgroups'] ) && 'yes' === $request['selectgroups'] ) ? true : false,
1138 );
1139
1140 $format = isset( $request['format'] ) ? $request['format'] : 'array';
1141 $params['format'] = $format;
1142
1143 if ( isset( $request['client'] ) && ! empty( $request['client'] ) ) {
1144 $params['client'] = rawurldecode( $request['client'] );
1145 }
1146
1147 // get data.
1148 $data = MainWP_DB::instance()->get_websites_for_current_user( $params );
1149
1150 $result = $data;
1151
1152 if ( 'array' == $format ) {
1153 if ( is_array( $data ) ) {
1154 $result = array(
1155 'data' => $data,
1156 );
1157 }
1158 }
1159
1160 $response = new \WP_REST_Response( $result );
1161 $response->set_status( 200 );
1162
1163 } else {
1164 // throw common error.
1165 $response = $this->mainwp_authentication_error();
1166 }
1167
1168 return $response;
1169 }
1170
1171
1172 /**
1173 * Method mainwp_rest_api_site_callback()
1174 *
1175 * Callback function for managing the response to API requests made for the endpoint: site
1176 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/site/site
1177 * API Method: GET
1178 *
1179 * @param array $request The request made in the API call which includes all parameters.
1180 *
1181 * @return object $response An object that contains the return data and status of the API request.
1182 */
1183 public function mainwp_rest_api_site_callback( $request ) {
1184
1185 // first validate the request.
1186 if ( $this->mainwp_validate_request( $request ) ) {
1187
1188 // get parameters.
1189 if ( null != $request['site_id'] ) {
1190
1191 if ( is_numeric( $request['site_id'] ) ) {
1192
1193 $site_id = $request['site_id'];
1194 $with_tags = isset( $request['with_tags'] ) && $request['with_tags'] ? true : false;
1195
1196 $selectGroups = $with_tags ? true : false;
1197
1198 // get data.
1199 $data = MainWP_DB::instance()->get_website_by_id( $site_id, $selectGroups );
1200
1201 if ( ! empty( $data ) && property_exists( $data, 'privkey' ) ) {
1202 unset( $data->privkey );
1203 }
1204
1205 if ( ! empty( $data ) && property_exists( $data, 'nosslkey' ) ) {
1206 unset( $data->nosslkey );
1207 }
1208
1209 if ( ! empty( $data ) && property_exists( $data, 'adminname' ) ) {
1210 unset( $data->adminname );
1211 }
1212
1213 if ( $with_tags && ! empty( $data ) && property_exists( $data, 'wpgroups' ) && property_exists( $data, 'wpgroupids' ) ) {
1214 $wpgroupids = explode( ',', $data->wpgroupids );
1215 $wpgroups = explode( ',', $data->wpgroups );
1216
1217 $tags = array();
1218 if ( is_array( $wpgroupids ) ) {
1219 foreach ( $wpgroupids as $gidx => $groupid ) {
1220 if ( $groupid && ! isset( $tags[ $groupid ] ) ) {
1221 $tags[ $groupid ] = isset( $wpgroups[ $gidx ] ) ? $wpgroups[ $gidx ] : '';
1222 }
1223 }
1224 }
1225 $data->tags = $tags;
1226 }
1227
1228 $response = new \WP_REST_Response( $data );
1229 $response->set_status( 200 );
1230
1231 } else {
1232 // throw invalid data error.
1233 $response = $this->mainwp_invalid_data_error();
1234 }
1235 } else {
1236 // throw missing data error.
1237 $response = $this->mainwp_missing_data_error();
1238 }
1239 } else {
1240 // throw common error.
1241 $response = $this->mainwp_authentication_error();
1242 }
1243
1244 return $response;
1245 }
1246
1247 /**
1248 * Method mainwp_rest_api_site_info_callback()
1249 *
1250 * Callback function for managing the response to API requests made for the endpoint: site-info
1251 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/site/site-info
1252 * API Method: GET
1253 *
1254 * @param array $request The request made in the API call which includes all parameters.
1255 *
1256 * @return object $response An object that contains the return data and status of the API request.
1257 */
1258 public function mainwp_rest_api_site_info_callback( $request ) {
1259
1260 // first validate the request.
1261 if ( $this->mainwp_validate_request( $request ) ) {
1262
1263 // get parameters.
1264 if ( null != $request['site_id'] ) {
1265
1266 if ( is_numeric( $request['site_id'] ) ) {
1267
1268 $site_id = $request['site_id'];
1269
1270 // get data.
1271 $website = MainWP_DB::instance()->get_website_by_id( $site_id );
1272 $data = MainWP_DB::instance()->get_website_option( $website, 'site_info' );
1273 $data = ( '' != $data ) ? json_decode( $data, true ) : array();
1274
1275 $response = new \WP_REST_Response( $data );
1276 $response->set_status( 200 );
1277
1278 } else {
1279 // throw invalid data error.
1280 $response = $this->mainwp_invalid_data_error();
1281 }
1282 } else {
1283 // throw missing data error.
1284 $response = $this->mainwp_missing_data_error();
1285 }
1286 } else {
1287 // throw common error.
1288 $response = $this->mainwp_authentication_error();
1289 }
1290
1291 return $response;
1292 }
1293
1294 /**
1295 * Method mainwp_rest_api_site_installed_plugins_callback()
1296 *
1297 * Callback function for managing the response to API requests made for the endpoint: site-installed-plugins
1298 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/site/site-installed-plugins
1299 * API Method: GET
1300 *
1301 * @param array $request The request made in the API call which includes all parameters.
1302 *
1303 * @return object $response An object that contains the return data and status of the API request.
1304 */
1305 public function mainwp_rest_api_site_installed_plugins_callback( $request ) {
1306
1307 // first validate the request.
1308 if ( $this->mainwp_validate_request( $request ) ) {
1309
1310 // get parameters.
1311 if ( null != $request['site_id'] ) {
1312
1313 if ( is_numeric( $request['site_id'] ) ) {
1314
1315 $site_id = $request['site_id'];
1316
1317 // get data.
1318 $website = MainWP_DB::instance()->get_website_by_id( $site_id );
1319 $data = json_decode( $website->plugins, 1 );
1320
1321 $response = new \WP_REST_Response( $data );
1322 $response->set_status( 200 );
1323
1324 } else {
1325 // throw invalid data error.
1326 $response = $this->mainwp_invalid_data_error();
1327 }
1328 } else {
1329 // throw missing data error.
1330 $response = $this->mainwp_missing_data_error();
1331 }
1332 } else {
1333 // throw common error.
1334 $response = $this->mainwp_authentication_error();
1335 }
1336
1337 return $response;
1338 }
1339
1340 /**
1341 * Method mainwp_rest_api_site_installed_plugins_count_callback()
1342 *
1343 * Callback function for managing the response to API requests made for the endpoint: site-installed-plugins-count
1344 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/site/site-installed-plugins-count
1345 * API Method: GET
1346 *
1347 * @param array $request The request made in the API call which includes all parameters.
1348 *
1349 * @return object $response An object that contains the return data and status of the API request.
1350 */
1351 public function mainwp_rest_api_site_installed_plugins_count_callback( $request ) {
1352
1353 // first validate the request.
1354 if ( $this->mainwp_validate_request( $request ) ) {
1355
1356 // get parameters.
1357 if ( null != $request['site_id'] ) {
1358
1359 if ( is_numeric( $request['site_id'] ) ) {
1360
1361 $site_id = $request['site_id'];
1362
1363 // get data.
1364 $website = MainWP_DB::instance()->get_website_by_id( $site_id );
1365 $plugins = json_decode( $website->plugins, 1 );
1366 $data = array(
1367 'count' => count( $plugins ),
1368 );
1369
1370 $response = new \WP_REST_Response( $data );
1371 $response->set_status( 200 );
1372
1373 } else {
1374 // throw invalid data error.
1375 $response = $this->mainwp_invalid_data_error();
1376 }
1377 } else {
1378 // throw missing data error.
1379 $response = $this->mainwp_missing_data_error();
1380 }
1381 } else {
1382 // throw common error.
1383 $response = $this->mainwp_authentication_error();
1384 }
1385
1386 return $response;
1387 }
1388
1389 /**
1390 * Method mainwp_rest_api_site_active_plugins_callback()
1391 *
1392 * Callback function for managing the response to API requests made for the endpoint: site-active-plugins
1393 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/site/site-active-plugins
1394 * API Method: GET
1395 *
1396 * @param array $request The request made in the API call which includes all parameters.
1397 *
1398 * @return object $response An object that contains the return data and status of the API request.
1399 */
1400 public function mainwp_rest_api_site_active_plugins_callback( $request ) {
1401
1402 // first validate the request.
1403 if ( $this->mainwp_validate_request( $request ) ) {
1404
1405 // get parameters.
1406 if ( null != $request['site_id'] ) {
1407
1408 if ( is_numeric( $request['site_id'] ) ) {
1409
1410 $site_id = $request['site_id'];
1411
1412 // get data.
1413 $website = MainWP_DB::instance()->get_website_by_id( $site_id );
1414 $plugins = json_decode( $website->plugins, 1 );
1415 $data = MainWP_Utility::get_sub_array_having( $plugins, 'active', 1 );
1416
1417 $response = new \WP_REST_Response( $data );
1418 $response->set_status( 200 );
1419
1420 } else {
1421 // throw invalid data error.
1422 $response = $this->mainwp_invalid_data_error();
1423 }
1424 } else {
1425 // throw missing data error.
1426 $response = $this->mainwp_missing_data_error();
1427 }
1428 } else {
1429 // throw common error.
1430 $response = $this->mainwp_authentication_error();
1431 }
1432
1433 return $response;
1434 }
1435
1436 /**
1437 * Method mainwp_rest_api_site_active_plugins_count_callback()
1438 *
1439 * Callback function for managing the response to API requests made for the endpoint: site-active-plugins-count
1440 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/site/site-active-plugins-count
1441 * API Method: GET
1442 *
1443 * @param array $request The request made in the API call which includes all parameters.
1444 *
1445 * @return object $response An object that contains the return data and status of the API request.
1446 */
1447 public function mainwp_rest_api_site_active_plugins_count_callback( $request ) {
1448
1449 // first validate the request.
1450 if ( $this->mainwp_validate_request( $request ) ) {
1451
1452 // get parameters.
1453 if ( null != $request['site_id'] ) {
1454
1455 if ( is_numeric( $request['site_id'] ) ) {
1456
1457 $site_id = $request['site_id'];
1458
1459 // get data.
1460 $website = MainWP_DB::instance()->get_website_by_id( $site_id );
1461 $plugins = json_decode( $website->plugins, 1 );
1462 $active_plugins = MainWP_Utility::get_sub_array_having( $plugins, 'active', 1 );
1463 $data = array(
1464 'count' => count( $active_plugins ),
1465 );
1466
1467 $response = new \WP_REST_Response( $data );
1468 $response->set_status( 200 );
1469
1470 } else {
1471 // throw invalid data error.
1472 $response = $this->mainwp_invalid_data_error();
1473 }
1474 } else {
1475 // throw missing data error.
1476 $response = $this->mainwp_missing_data_error();
1477 }
1478 } else {
1479 // throw common error.
1480 $response = $this->mainwp_authentication_error();
1481 }
1482
1483 return $response;
1484 }
1485
1486 /**
1487 * Method mainwp_rest_api_site_inactive_plugins_callback()
1488 *
1489 * Callback function for managing the response to API requests made for the endpoint: site-inactive-plugins
1490 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/site/site-inactive-plugins
1491 * API Method: GET
1492 *
1493 * @param array $request The request made in the API call which includes all parameters.
1494 *
1495 * @return object $response An object that contains the return data and status of the API request.
1496 */
1497 public function mainwp_rest_api_site_inactive_plugins_callback( $request ) {
1498
1499 // first validate the request.
1500 if ( $this->mainwp_validate_request( $request ) ) {
1501
1502 // get parameters.
1503 if ( null != $request['site_id'] ) {
1504
1505 if ( is_numeric( $request['site_id'] ) ) {
1506
1507 $site_id = $request['site_id'];
1508
1509 // get data.
1510 $website = MainWP_DB::instance()->get_website_by_id( $site_id );
1511 $plugins = json_decode( $website->plugins, 1 );
1512 $data = MainWP_Utility::get_sub_array_having( $plugins, 'active', 0 );
1513
1514 $response = new \WP_REST_Response( $data );
1515 $response->set_status( 200 );
1516
1517 } else {
1518 // throw invalid data error.
1519 $response = $this->mainwp_invalid_data_error();
1520 }
1521 } else {
1522 // throw missing data error.
1523 $response = $this->mainwp_missing_data_error();
1524 }
1525 } else {
1526 // throw common error.
1527 $response = $this->mainwp_authentication_error();
1528 }
1529
1530 return $response;
1531 }
1532
1533 /**
1534 * Method mainwp_rest_api_site_inactive_plugins_count_callback()
1535 *
1536 * Callback function for managing the response to API requests made for the endpoint: site-inactive-plugins-count
1537 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/site/site-inactive-plugins-count
1538 * API Method: GET
1539 *
1540 * @param array $request The request made in the API call which includes all parameters.
1541 *
1542 * @return object $response An object that contains the return data and status of the API request.
1543 */
1544 public function mainwp_rest_api_site_inactive_plugins_count_callback( $request ) {
1545
1546 // first validate the request.
1547 if ( $this->mainwp_validate_request( $request ) ) {
1548
1549 // get parameters.
1550 if ( null != $request['site_id'] ) {
1551
1552 if ( is_numeric( $request['site_id'] ) ) {
1553
1554 $site_id = $request['site_id'];
1555
1556 // get data.
1557 $website = MainWP_DB::instance()->get_website_by_id( $site_id );
1558 $plugins = json_decode( $website->plugins, 1 );
1559 $inactive_plugins = MainWP_Utility::get_sub_array_having( $plugins, 'active', 0 );
1560 $data = array(
1561 'count' => count( $inactive_plugins ),
1562 );
1563
1564 $response = new \WP_REST_Response( $data );
1565 $response->set_status( 200 );
1566
1567 } else {
1568 // throw invalid data error.
1569 $response = $this->mainwp_invalid_data_error();
1570 }
1571 } else {
1572 // throw missing data error.
1573 $response = $this->mainwp_missing_data_error();
1574 }
1575 } else {
1576 // throw common error.
1577 $response = $this->mainwp_authentication_error();
1578 }
1579
1580 return $response;
1581 }
1582
1583 /**
1584 * Method mainwp_rest_api_site_installed_themes_callback()
1585 *
1586 * Callback function for managing the response to API requests made for the endpoint: site-installed-themes
1587 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/site/site-installed-themes
1588 * API Method: GET
1589 *
1590 * @param array $request The request made in the API call which includes all parameters.
1591 *
1592 * @return object $response An object that contains the return data and status of the API request.
1593 */
1594 public function mainwp_rest_api_site_installed_themes_callback( $request ) {
1595
1596 // first validate the request.
1597 if ( $this->mainwp_validate_request( $request ) ) {
1598
1599 // get parameters.
1600 if ( null != $request['site_id'] ) {
1601
1602 if ( is_numeric( $request['site_id'] ) ) {
1603
1604 $site_id = $request['site_id'];
1605
1606 // get data.
1607 $website = MainWP_DB::instance()->get_website_by_id( $site_id );
1608 $data = json_decode( $website->themes, 1 );
1609
1610 $response = new \WP_REST_Response( $data );
1611 $response->set_status( 200 );
1612
1613 } else {
1614 // throw invalid data error.
1615 $response = $this->mainwp_invalid_data_error();
1616 }
1617 } else {
1618 // throw missing data error.
1619 $response = $this->mainwp_missing_data_error();
1620 }
1621 } else {
1622 // throw common error.
1623 $response = $this->mainwp_authentication_error();
1624 }
1625
1626 return $response;
1627 }
1628
1629 /**
1630 * Method mainwp_rest_api_site_installed_themes_count_callback()
1631 *
1632 * Callback function for managing the response to API requests made for the endpoint: site-installed-themes-count
1633 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/site/site-installed-themes-count
1634 * API Method: GET
1635 *
1636 * @param array $request The request made in the API call which includes all parameters.
1637 *
1638 * @return object $response An object that contains the return data and status of the API request.
1639 */
1640 public function mainwp_rest_api_site_installed_themes_count_callback( $request ) {
1641
1642 // first validate the request.
1643 if ( $this->mainwp_validate_request( $request ) ) {
1644
1645 // get parameters.
1646 if ( null != $request['site_id'] ) {
1647
1648 if ( is_numeric( $request['site_id'] ) ) {
1649
1650 $site_id = $request['site_id'];
1651
1652 // get data.
1653 $website = MainWP_DB::instance()->get_website_by_id( $site_id );
1654 $themes = json_decode( $website->themes, 1 );
1655 $data = array(
1656 'count' => count( $themes ),
1657 );
1658
1659 $response = new \WP_REST_Response( $data );
1660 $response->set_status( 200 );
1661
1662 } else {
1663 // throw invalid data error.
1664 $response = $this->mainwp_invalid_data_error();
1665 }
1666 } else {
1667 // throw missing data error.
1668 $response = $this->mainwp_missing_data_error();
1669 }
1670 } else {
1671 // throw common error.
1672 $response = $this->mainwp_authentication_error();
1673 }
1674
1675 return $response;
1676 }
1677
1678 /**
1679 * Method mainwp_rest_api_site_active_themes_callback()
1680 *
1681 * Callback function for managing the response to API requests made for the endpoint: site-active-themes
1682 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/site/site-active-themes
1683 * API Method: GET
1684 *
1685 * @param array $request The request made in the API call which includes all parameters.
1686 *
1687 * @return object $response An object that contains the return data and status of the API request.
1688 */
1689 public function mainwp_rest_api_site_active_themes_callback( $request ) {
1690
1691 // first validate the request.
1692 if ( $this->mainwp_validate_request( $request ) ) {
1693
1694 // get parameters.
1695 if ( null != $request['site_id'] ) {
1696
1697 if ( is_numeric( $request['site_id'] ) ) {
1698
1699 $site_id = $request['site_id'];
1700
1701 // get data.
1702 $website = MainWP_DB::instance()->get_website_by_id( $site_id );
1703 $themes = json_decode( $website->themes, 1 );
1704 $data = MainWP_Utility::get_sub_array_having( $themes, 'active', 1 );
1705
1706 $response = new \WP_REST_Response( $data );
1707 $response->set_status( 200 );
1708
1709 } else {
1710 // throw invalid data error.
1711 $response = $this->mainwp_invalid_data_error();
1712 }
1713 } else {
1714 // throw missing data error.
1715 $response = $this->mainwp_missing_data_error();
1716 }
1717 } else {
1718 // throw common error.
1719 $response = $this->mainwp_authentication_error();
1720 }
1721
1722 return $response;
1723 }
1724
1725 /**
1726 * Method mainwp_rest_api_site_inactive_themes_callback()
1727 *
1728 * Callback function for managing the response to API requests made for the endpoint: site-inactive-themes
1729 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/site/site-inactive-themes
1730 * API Method: GET
1731 *
1732 * @param array $request The request made in the API call which includes all parameters.
1733 *
1734 * @return object $response An object that contains the return data and status of the API request.
1735 */
1736 public function mainwp_rest_api_site_inactive_themes_callback( $request ) {
1737
1738 // first validate the request.
1739 if ( $this->mainwp_validate_request( $request ) ) {
1740
1741 // get parameters.
1742 if ( null != $request['site_id'] ) {
1743
1744 if ( is_numeric( $request['site_id'] ) ) {
1745
1746 $site_id = $request['site_id'];
1747
1748 // get data.
1749 $website = MainWP_DB::instance()->get_website_by_id( $site_id );
1750 $themes = json_decode( $website->themes, 1 );
1751 $data = MainWP_Utility::get_sub_array_having( $themes, 'active', 0 );
1752
1753 $response = new \WP_REST_Response( $data );
1754 $response->set_status( 200 );
1755
1756 } else {
1757 // throw invalid data error.
1758 $response = $this->mainwp_invalid_data_error();
1759 }
1760 } else {
1761 // throw missing data error.
1762 $response = $this->mainwp_missing_data_error();
1763 }
1764 } else {
1765 // throw common error.
1766 $response = $this->mainwp_authentication_error();
1767 }
1768
1769 return $response;
1770 }
1771
1772 /**
1773 * Method mainwp_rest_api_site_inactive_themes_count_callback()
1774 *
1775 * Callback function for managing the response to API requests made for the endpoint: site-inactive-themes-count
1776 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/site/site-inactive-themes-count
1777 * API Method: GET
1778 *
1779 * @param array $request The request made in the API call which includes all parameters.
1780 *
1781 * @return object $response An object that contains the return data and status of the API request.
1782 */
1783 public function mainwp_rest_api_site_inactive_themes_count_callback( $request ) {
1784
1785 // first validate the request.
1786 if ( $this->mainwp_validate_request( $request ) ) {
1787
1788 // get parameters.
1789 if ( null != $request['site_id'] ) {
1790
1791 if ( is_numeric( $request['site_id'] ) ) {
1792
1793 $site_id = $request['site_id'];
1794
1795 // get data.
1796 $website = MainWP_DB::instance()->get_website_by_id( $site_id );
1797 $themes = json_decode( $website->themes, 1 );
1798 $inactive_themes = MainWP_Utility::get_sub_array_having( $themes, 'active', 0 );
1799 $data = array(
1800 'count' => count( $inactive_themes ),
1801 );
1802
1803 $response = new \WP_REST_Response( $data );
1804 $response->set_status( 200 );
1805
1806 } else {
1807 // throw invalid data error.
1808 $response = $this->mainwp_invalid_data_error();
1809 }
1810 } else {
1811 // throw missing data error.
1812 $response = $this->mainwp_missing_data_error();
1813 }
1814 } else {
1815 // throw common error.
1816 $response = $this->mainwp_authentication_error();
1817 }
1818
1819 return $response;
1820 }
1821
1822 /**
1823 * Method mainwp_rest_api_site_available_updates_callback()
1824 *
1825 * Callback function for managing the response to API requests made for the endpoint: site-available-updates
1826 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/site/site-available-updates
1827 * API Method: GET
1828 *
1829 * @param array $request The request made in the API call which includes all parameters.
1830 *
1831 * @return object $response An object that contains the return data and status of the API request.
1832 */
1833 public function mainwp_rest_api_site_available_updates_callback( $request ) {
1834
1835 // first validate the request.
1836 if ( $this->mainwp_validate_request( $request ) ) {
1837
1838 // get parameters.
1839 if ( null != $request['site_id'] ) {
1840
1841 if ( is_numeric( $request['site_id'] ) ) {
1842
1843 $site_id = $request['site_id'];
1844
1845 // get data.
1846 $website = MainWP_DB::instance()->get_website_by_id( $site_id );
1847 $wp_upgrades = MainWP_DB::instance()->get_website_option( $website, 'wp_upgrades' );
1848 $wp_upgrades = ( '' != $wp_upgrades ) ? json_decode( $wp_upgrades, true ) : array();
1849
1850 $plugin_upgrades = json_decode( $website->plugin_upgrades, true );
1851 $theme_upgrades = json_decode( $website->theme_upgrades, true );
1852 $translation_upgrades = json_decode( $website->translation_upgrades, true );
1853 $data = array(
1854 'wp_core' => $wp_upgrades,
1855 'plugins' => $plugin_upgrades,
1856 'themes' => $theme_upgrades,
1857 'translation' => $translation_upgrades,
1858 );
1859
1860 $response = new \WP_REST_Response( $data );
1861 $response->set_status( 200 );
1862
1863 } else {
1864 // throw invalid data error.
1865 $response = $this->mainwp_invalid_data_error();
1866 }
1867 } else {
1868 // throw missing data error.
1869 $response = $this->mainwp_missing_data_error();
1870 }
1871 } else {
1872 // throw common error.
1873 $response = $this->mainwp_authentication_error();
1874 }
1875
1876 return $response;
1877 }
1878
1879 /**
1880 * Method mainwp_rest_api_site_available_updates_count_callback()
1881 *
1882 * Callback function for managing the response to API requests made for the endpoint: site-available-updates-count
1883 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/site/site-available-updates-count
1884 * API Method: GET
1885 *
1886 * @param array $request The request made in the API call which includes all parameters.
1887 *
1888 * @return object $response An object that contains the return data and status of the API request.
1889 */
1890 public function mainwp_rest_api_site_available_updates_count_callback( $request ) {
1891
1892 // first validate the request.
1893 if ( $this->mainwp_validate_request( $request ) ) {
1894
1895 // get parameters.
1896 if ( null != $request['site_id'] ) {
1897
1898 if ( is_numeric( $request['site_id'] ) ) {
1899
1900 $site_id = $request['site_id'];
1901
1902 // get data.
1903 $website = MainWP_DB::instance()->get_website_by_id( $site_id );
1904 $plugins = json_decode( $website->plugin_upgrades, true );
1905 $themes = json_decode( $website->theme_upgrades, true );
1906 $translations = json_decode( $website->translation_upgrades, true );
1907 $wp = MainWP_DB::instance()->get_website_option( $website, 'wp_upgrades' );
1908 $wp = ( '' != $wp ) ? json_decode( $wp, true ) : array();
1909
1910 if ( count( $wp ) > 0 ) {
1911 $wp = 1;
1912 } else {
1913 $wp = 0;
1914 }
1915 $total = array_merge( $plugins, $themes, $translations );
1916 $data = array(
1917 'total' => count( $total ) + $wp,
1918 'wp' => $wp,
1919 'plugins' => count( $plugins ),
1920 'themes' => count( $themes ),
1921 'translations' => count( $translations ),
1922 );
1923
1924 $response = new \WP_REST_Response( $data );
1925 $response->set_status( 200 );
1926
1927 } else {
1928 // throw invalid data error.
1929 $response = $this->mainwp_invalid_data_error();
1930 }
1931 } else {
1932 // throw missing data error.
1933 $response = $this->mainwp_missing_data_error();
1934 }
1935 } else {
1936 // throw common error.
1937 $response = $this->mainwp_authentication_error();
1938 }
1939
1940 return $response;
1941 }
1942
1943 /**
1944 * Method mainwp_rest_api_site_abandoned_plugins_callback()
1945 *
1946 * Callback function for managing the response to API requests made for the endpoint: site-abandoned-plugins
1947 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/site/site-abandoned-plugins
1948 * API Method: GET
1949 *
1950 * @param array $request The request made in the API call which includes all parameters.
1951 *
1952 * @return object $response An object that contains the return data and status of the API request.
1953 */
1954 public function mainwp_rest_api_site_abandoned_plugins_callback( $request ) {
1955
1956 // first validate the request.
1957 if ( $this->mainwp_validate_request( $request ) ) {
1958
1959 // get parameters.
1960 if ( null != $request['site_id'] ) {
1961
1962 if ( is_numeric( $request['site_id'] ) ) {
1963
1964 $site_id = $request['site_id'];
1965
1966 // get data.
1967 $website = MainWP_DB::instance()->get_website_by_id( $site_id );
1968 $data = MainWP_DB::instance()->get_website_option( $website, 'plugins_outdate_info' );
1969 $data = ( '' != $data ) ? json_decode( $data, true ) : array();
1970
1971 $response = new \WP_REST_Response( $data );
1972 $response->set_status( 200 );
1973
1974 } else {
1975 // throw invalid data error.
1976 $response = $this->mainwp_invalid_data_error();
1977 }
1978 } else {
1979 // throw missing data error.
1980 $response = $this->mainwp_missing_data_error();
1981 }
1982 } else {
1983 // throw common error.
1984 $response = $this->mainwp_authentication_error();
1985 }
1986
1987 return $response;
1988 }
1989
1990 /**
1991 * Method mainwp_rest_api_site_abandoned_plugins_count_callback()
1992 *
1993 * Callback function for managing the response to API requests made for the endpoint: site-abandoned-plugins-count
1994 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/site/site-abandoned-plugins-count
1995 * API Method: GET
1996 *
1997 * @param array $request The request made in the API call which includes all parameters.
1998 *
1999 * @return object $response An object that contains the return data and status of the API request.
2000 */
2001 public function mainwp_rest_api_site_abandoned_plugins_count_callback( $request ) {
2002
2003 // first validate the request.
2004 if ( $this->mainwp_validate_request( $request ) ) {
2005
2006 // get parameters.
2007 if ( null != $request['site_id'] ) {
2008
2009 if ( is_numeric( $request['site_id'] ) ) {
2010
2011 $site_id = $request['site_id'];
2012
2013 // get data.
2014 $website = MainWP_DB::instance()->get_website_by_id( $site_id );
2015 $plugins = MainWP_DB::instance()->get_website_option( $website, 'plugins_outdate_info' );
2016 $plugins = ( '' != $plugins ) ? json_decode( $plugins, true ) : array();
2017
2018 $data = array(
2019 'count' => count( $plugins ),
2020 );
2021
2022 $response = new \WP_REST_Response( $data );
2023 $response->set_status( 200 );
2024
2025 } else {
2026 // throw invalid data error.
2027 $response = $this->mainwp_invalid_data_error();
2028 }
2029 } else {
2030 // throw missing data error.
2031 $response = $this->mainwp_missing_data_error();
2032 }
2033 } else {
2034 // throw common error.
2035 $response = $this->mainwp_authentication_error();
2036 }
2037
2038 return $response;
2039 }
2040
2041 /**
2042 * Method mainwp_rest_api_site_abandoned_themes_callback()
2043 *
2044 * Callback function for managing the response to API requests made for the endpoint: site-abandoned-themes
2045 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/site/site-abandoned-themes
2046 * API Method: GET
2047 *
2048 * @param array $request The request made in the API call which includes all parameters.
2049 *
2050 * @return object $response An object that contains the return data and status of the API request.
2051 */
2052 public function mainwp_rest_api_site_abandoned_themes_callback( $request ) {
2053
2054 // first validate the request.
2055 if ( $this->mainwp_validate_request( $request ) ) {
2056
2057 // get parameters.
2058 if ( null != $request['site_id'] ) {
2059
2060 if ( is_numeric( $request['site_id'] ) ) {
2061
2062 $site_id = $request['site_id'];
2063
2064 // get data.
2065 $website = MainWP_DB::instance()->get_website_by_id( $site_id );
2066 $data = MainWP_DB::instance()->get_website_option( $website, 'themes_outdate_info' );
2067 $data = ( '' != $data ) ? json_decode( $data, true ) : array();
2068
2069 $response = new \WP_REST_Response( $data );
2070 $response->set_status( 200 );
2071
2072 } else {
2073 // throw invalid data error.
2074 $response = $this->mainwp_invalid_data_error();
2075 }
2076 } else {
2077 // throw missing data error.
2078 $response = $this->mainwp_missing_data_error();
2079 }
2080 } else {
2081 // throw common error.
2082 $response = $this->mainwp_authentication_error();
2083 }
2084
2085 return $response;
2086 }
2087
2088 /**
2089 * Method mainwp_rest_api_site_abandoned_themes_count_callback()
2090 *
2091 * Callback function for managing the response to API requests made for the endpoint: site-abandoned-themes-count
2092 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/site/site-abandoned-themes-count
2093 * API Method: GET
2094 *
2095 * @param array $request The request made in the API call which includes all parameters.
2096 *
2097 * @return object $response An object that contains the return data and status of the API request.
2098 */
2099 public function mainwp_rest_api_site_abandoned_themes_count_callback( $request ) {
2100
2101 // first validate the request.
2102 if ( $this->mainwp_validate_request( $request ) ) {
2103
2104 // get parameters.
2105 if ( null != $request['site_id'] ) {
2106
2107 if ( is_numeric( $request['site_id'] ) ) {
2108
2109 $site_id = $request['site_id'];
2110
2111 // get data.
2112 $website = MainWP_DB::instance()->get_website_by_id( $site_id );
2113 $themes = MainWP_DB::instance()->get_website_option( $website, 'themes_outdate_info' );
2114 $themes = ( '' != $themes ) ? json_decode( $themes, true ) : array();
2115
2116 $data = array(
2117 'count' => count( $themes ),
2118 );
2119
2120 $response = new \WP_REST_Response( $data );
2121 $response->set_status( 200 );
2122
2123 } else {
2124 // throw invalid data error.
2125 $response = $this->mainwp_invalid_data_error();
2126 }
2127 } else {
2128 // throw missing data error.
2129 $response = $this->mainwp_missing_data_error();
2130 }
2131 } else {
2132 // throw common error.
2133 $response = $this->mainwp_authentication_error();
2134 }
2135
2136 return $response;
2137 }
2138
2139 /**
2140 * Method mainwp_rest_api_site_http_status_callback()
2141 *
2142 * Callback function for managing the response to API requests made for the endpoint: site-http-status
2143 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/site/site-http-status
2144 * API Method: GET
2145 *
2146 * @param array $request The request made in the API call which includes all parameters.
2147 *
2148 * @return object $response An object that contains the return data and status of the API request.
2149 */
2150 public function mainwp_rest_api_site_http_status_callback( $request ) {
2151
2152 // first validate the request.
2153 if ( $this->mainwp_validate_request( $request ) ) {
2154
2155 // get parameters.
2156 if ( null != $request['site_id'] ) {
2157
2158 if ( is_numeric( $request['site_id'] ) ) {
2159
2160 $site_id = $request['site_id'];
2161
2162 // get data.
2163 $website = MainWP_DB::instance()->get_website_by_id( $site_id );
2164 $data = $website->http_response_code;
2165
2166 $response = new \WP_REST_Response( $data );
2167 $response->set_status( 200 );
2168
2169 } else {
2170 // throw invalid data error.
2171 $response = $this->mainwp_invalid_data_error();
2172 }
2173 } else {
2174 // throw missing data error.
2175 $response = $this->mainwp_missing_data_error();
2176 }
2177 } else {
2178 // throw common error.
2179 $response = $this->mainwp_authentication_error();
2180 }
2181
2182 return $response;
2183 }
2184
2185 /**
2186 * Method mainwp_rest_api_site_health_score_callback()
2187 *
2188 * Callback function for managing the response to API requests made for the endpoint: site-health-score
2189 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/site/site-health-score
2190 * API Method: GET
2191 *
2192 * @param array $request The request made in the API call which includes all parameters.
2193 *
2194 * @return object $response An object that contains the return data and status of the API request.
2195 */
2196 public function mainwp_rest_api_site_health_score_callback( $request ) {
2197
2198 // first validate the request.
2199 if ( $this->mainwp_validate_request( $request ) ) {
2200
2201 // parameters.
2202 if ( null != $request['site_id'] ) {
2203
2204 if ( is_numeric( $request['site_id'] ) ) {
2205
2206 $site_id = $request['site_id'];
2207
2208 // get data.
2209 $website = MainWP_DB::instance()->get_website_by_id( $site_id, false, array( 'health_site_status' ) );
2210 $health_status = isset( $website->health_site_status ) ? json_decode( $website->health_site_status, true ) : array();
2211 $hstatus = MainWP_Utility::get_site_health( $health_status );
2212 $hval = $hstatus['val'];
2213 $critical = $hstatus['critical'];
2214 if ( 80 <= $hval && 0 == $critical ) {
2215 $health_score = 'Good';
2216 } else {
2217 $health_score = 'Should be improved';
2218 }
2219
2220 $data = $health_score;
2221
2222 $response = new \WP_REST_Response( $data );
2223 $response->set_status( 200 );
2224
2225 } else {
2226 // throw invalid data error.
2227 $response = $this->mainwp_invalid_data_error();
2228 }
2229 } else {
2230 // throw missing data error.
2231 $response = $this->mainwp_missing_data_error();
2232 }
2233 } else {
2234 // throw common error.
2235 $response = $this->mainwp_authentication_error();
2236 }
2237
2238 return $response;
2239 }
2240
2241 /**
2242 * Method mainwp_rest_api_site_security_issues_callback()
2243 *
2244 * Callback function for managing the response to API requests made for the endpoint: site-security-issues
2245 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/site/site-security-issues
2246 * API Method: get
2247 *
2248 * @param array $request The request made in the API call which includes all parameters.
2249 *
2250 * @return object $response An object that contains the return data and status of the API request.
2251 */
2252 public function mainwp_rest_api_site_security_issues_callback( $request ) {
2253
2254 // first validate the request.
2255 if ( $this->mainwp_validate_request( $request ) ) {
2256
2257 // get parameters.
2258 if ( null != $request['site_id'] ) {
2259
2260 if ( is_numeric( $request['site_id'] ) ) {
2261
2262 $site_id = $request['site_id'];
2263
2264 // get data.
2265 $website = MainWP_DB::instance()->get_website_by_id( $site_id );
2266 $data = MainWP_Connect::fetch_url_authed( $website, 'security' );
2267
2268 $response = new \WP_REST_Response( $data );
2269 $response->set_status( 200 );
2270
2271 } else {
2272 // throw invalid data error.
2273 $response = $this->mainwp_invalid_data_error();
2274 }
2275 } else {
2276 // throw missing data error.
2277 $response = $this->mainwp_missing_data_error();
2278 }
2279 } else {
2280 // throw common error.
2281 $response = $this->mainwp_authentication_error();
2282 }
2283
2284 return $response;
2285 }
2286
2287 /**
2288 * Method mainwp_rest_api_add_site_callback()
2289 *
2290 * Callback function for managing the response to API requests made for the endpoint: add-site
2291 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/site/add-site
2292 * API Method: POST
2293 *
2294 * @param array $request The request made in the API call which includes all parameters.
2295 *
2296 * @return object $response An object that contains the return data and status of the API request.
2297 */
2298 public function mainwp_rest_api_add_site_callback( $request ) {
2299
2300 // first validate the request.
2301 if ( $this->mainwp_validate_request( $request ) ) {
2302
2303 // get data.
2304 $fields = $request->get_json_params();
2305
2306 $data = MainWP_Manage_Sites_Handler::rest_api_add_site( $fields );
2307
2308 $response = new \WP_REST_Response( $data );
2309 $response->set_status( 200 );
2310
2311 } else {
2312 // throw common error.
2313 $response = $this->mainwp_authentication_error();
2314 }
2315
2316 return $response;
2317 }
2318
2319 /**
2320 * Method mainwp_rest_api_edit_site_callback()
2321 *
2322 * Callback function for managing the response to API requests made for the endpoint: edit-site
2323 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/site/edit-site
2324 * API Method: PUT
2325 *
2326 * @param array $request The request made in the API call which includes all parameters.
2327 *
2328 * @return object $response An object that contains the return data and status of the API request.
2329 */
2330 public function mainwp_rest_api_edit_site_callback( $request ) {
2331
2332 // first validate the request.
2333 if ( $this->mainwp_validate_request( $request ) ) {
2334
2335 // get parameters.
2336 if ( null != $request['site_id'] ) {
2337
2338 if ( is_numeric( $request['site_id'] ) ) {
2339
2340 $site_id = $request['site_id'];
2341 $fields = $request->get_json_params(); // this will get json body and parse it as an associative array which is perfect for what we want.
2342
2343 // get data.
2344 $data = MainWP_DB_Common::instance()->rest_api_update_website( $site_id, $fields );
2345
2346 $response = new \WP_REST_Response( $data );
2347 $response->set_status( 200 );
2348
2349 } else {
2350 // throw invalid data error.
2351 $response = $this->mainwp_invalid_data_error();
2352 }
2353 } else {
2354 // throw missing data error.
2355 $response = $this->mainwp_missing_data_error();
2356 }
2357 } else {
2358 // throw common error.
2359 $response = $this->mainwp_authentication_error();
2360 }
2361
2362 return $response;
2363 }
2364
2365 /**
2366 * Method mainwp_rest_api_sync_site_callback()
2367 *
2368 * Callback function for managing the response to API requests made for the endpoint: sync-site
2369 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/site/sync-site
2370 * API Method: POST
2371 *
2372 * @param array $request The request made in the API call which includes all parameters.
2373 *
2374 * @return object $response An object that contains the return data and status of the API request.
2375 */
2376 public function mainwp_rest_api_sync_site_callback( $request ) {
2377
2378 // first validate the request.
2379 if ( $this->mainwp_validate_request( $request ) ) {
2380 // get parameters.
2381 if ( null != $request['site_id'] ) {
2382
2383 if ( is_numeric( $request['site_id'] ) ) {
2384
2385 $site_id = $request['site_id'];
2386 $website = MainWP_DB::instance()->get_website_by_id( $site_id );
2387 try {
2388 MainWP_Sync::sync_site( $website );
2389 } catch ( \Exception $e ) {
2390 // ok.
2391 }
2392 // do common process response.
2393 $response = $this->mainwp_run_process_success();
2394 } else {
2395 // throw invalid data error.
2396 $response = $this->mainwp_invalid_data_error();
2397 }
2398 } else {
2399 // throw missing data error.
2400 $response = $this->mainwp_missing_data_error();
2401 }
2402 } else {
2403 // throw common error.
2404 $response = $this->mainwp_authentication_error();
2405 }
2406
2407 return $response;
2408 }
2409
2410 /**
2411 * Method mainwp_rest_api_reconnect_site_callback()
2412 *
2413 * Callback function for managing the response to API requests made for the endpoint: reconnect-site
2414 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/sites/reconnect-site
2415 * API Method: POST
2416 *
2417 * @param array $request The request made in the API call which includes all parameters.
2418 *
2419 * @return object $response An object that contains the return data and status of the API request.
2420 */
2421 public function mainwp_rest_api_reconnect_site_callback( $request ) {
2422
2423 // first validate the request.
2424 if ( $this->mainwp_validate_request( $request ) ) {
2425
2426 if ( null != $request['site_id'] ) {
2427
2428 if ( is_numeric( $request['site_id'] ) ) {
2429
2430 $site_id = $request['site_id'];
2431 $website = MainWP_DB::instance()->get_website_by_id( $site_id );
2432 try {
2433 MainWP_Manage_Sites_View::m_reconnect_site( $website );
2434 } catch ( \Exception $e ) {
2435 // ok.
2436 }
2437 // do common process response.
2438 $response = $this->mainwp_run_process_success();
2439
2440 } else {
2441 // throw invalid data error.
2442 $response = $this->mainwp_invalid_data_error();
2443 }
2444 } else {
2445 // throw missing data error.
2446 $response = $this->mainwp_missing_data_error();
2447 }
2448 } else {
2449 // throw common error.
2450 $response = $this->mainwp_authentication_error();
2451 }
2452
2453 return $response;
2454 }
2455
2456 /**
2457 * Method mainwp_rest_api_disconnect_site_callback()
2458 *
2459 * Callback function for managing the response to API requests made for the endpoint: disconnect-site
2460 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/sites/disconnect-site
2461 * API Method: POST
2462 *
2463 * @param array $request The request made in the API call which includes all parameters.
2464 *
2465 * @return object $response An object that contains the return data and status of the API request.
2466 */
2467 public function mainwp_rest_api_disconnect_site_callback( $request ) {
2468
2469 // first validate the request.
2470 if ( $this->mainwp_validate_request( $request ) ) {
2471
2472 if ( null != $request['site_id'] ) {
2473
2474 if ( is_numeric( $request['site_id'] ) ) {
2475
2476 $site_id = $request['site_id'];
2477 $website = MainWP_DB::instance()->get_website_by_id( $site_id );
2478 try {
2479 MainWP_Connect::fetch_url_authed( $website, 'disconnect' );
2480 } catch ( \Exception $e ) {
2481 // ok.
2482 }
2483 // do common process response.
2484 $response = $this->mainwp_run_process_success();
2485
2486 } else {
2487 // throw invalid data error.
2488 $response = $this->mainwp_invalid_data_error();
2489 }
2490 } else {
2491 // throw missing data error.
2492 $response = $this->mainwp_missing_data_error();
2493 }
2494 } else {
2495 // throw common error.
2496 $response = $this->mainwp_authentication_error();
2497 }
2498
2499 return $response;
2500 }
2501
2502 /**
2503 * Method mainwp_rest_api_remove_site_callback()
2504 *
2505 * Callback function for managing the response to API requests made for the endpoint: remove-site
2506 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/site/remove-site
2507 * API Method: DELETE
2508 *
2509 * @param array $request The request made in the API call which includes all parameters.
2510 *
2511 * @return object $response An object that contains the return data and status of the API request.
2512 */
2513 public function mainwp_rest_api_remove_site_callback( $request ) {
2514
2515 // first validate the request.
2516 if ( $this->mainwp_validate_request( $request ) ) {
2517
2518 // get parameters.
2519 if ( null != $request['site_id'] ) {
2520
2521 if ( is_numeric( $request['site_id'] ) ) {
2522
2523 $site_id = $request['site_id'];
2524
2525 MainWP_Manage_Sites_Handler::remove_website( $site_id );
2526
2527 // do common process response.
2528 $response = $this->mainwp_run_process_success();
2529
2530 } else {
2531 // throw invalid data error.
2532 $response = $this->mainwp_invalid_data_error();
2533 }
2534 } else {
2535 // throw missing data error.
2536 $response = $this->mainwp_missing_data_error();
2537 }
2538 } else {
2539 // throw common error.
2540 $response = $this->mainwp_authentication_error();
2541 }
2542
2543 return $response;
2544 }
2545
2546 /**
2547 * Method mainwp_rest_api_site_update_wordpress_callback()
2548 *
2549 * Callback function for managing the response to API requests made for the endpoint: site-update-wordpress
2550 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/site/site-update-wordpress
2551 * API Method: PUT
2552 *
2553 * @param array $request The request made in the API call which includes all parameters.
2554 *
2555 * @return object $response An object that contains the return data and status of the API request.
2556 */
2557 public function mainwp_rest_api_site_update_wordpress_callback( $request ) {
2558
2559 // first validate the request.
2560 if ( $this->mainwp_validate_request( $request ) ) {
2561
2562 // get parameters.
2563 if ( null != $request['site_id'] ) {
2564
2565 if ( is_numeric( $request['site_id'] ) ) {
2566
2567 $site_id = $request['site_id'];
2568
2569 // get data.
2570 $website = MainWP_DB::instance()->get_website_by_id( $site_id );
2571 try {
2572 $data = MainWP_Updates_Handler::upgrade_website( $website );
2573 } catch ( \Exception $e ) {
2574 $data = array( 'error' => MainWP_Error_Helper::get_console_error_message( $e ) );
2575 }
2576 $response = new \WP_REST_Response( $data );
2577 $response->set_status( 200 );
2578
2579 } else {
2580 // throw invalid data error.
2581 $response = $this->mainwp_invalid_data_error();
2582 }
2583 } else {
2584 // throw missing data error.
2585 $response = $this->mainwp_missing_data_error();
2586 }
2587 } else {
2588 // throw common error.
2589 $response = $this->mainwp_authentication_error();
2590 }
2591
2592 return $response;
2593 }
2594
2595 /**
2596 * Method mainwp_rest_api_site_update_plugins_callback()
2597 *
2598 * Callback function for managing the response to API requests made for the endpoint: site-update-plugins
2599 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/site/site-update-plugins
2600 * API Method: PUT
2601 *
2602 * @param array $request The request made in the API call which includes all parameters.
2603 *
2604 * @return object $response An object that contains the return data and status of the API request.
2605 */
2606 public function mainwp_rest_api_site_update_plugins_callback( $request ) {
2607
2608 // first validate the request.
2609 if ( $this->mainwp_validate_request( $request ) ) {
2610
2611 // get parameters.
2612 if ( null != $request['site_id'] ) {
2613
2614 if ( is_numeric( $request['site_id'] ) ) {
2615
2616 $site_id = $request['site_id'];
2617
2618 // get data.
2619 $website = MainWP_DB::instance()->get_website_by_id( $site_id );
2620 $plugin_upgrades = json_decode( $website->plugin_upgrades, true );
2621 $slugs = array();
2622 foreach ( $plugin_upgrades as $slug => $plugin ) {
2623 $slugs[] = $slug;
2624 }
2625 MainWP_Connect::fetch_url_authed(
2626 $website,
2627 'upgradeplugintheme',
2628 array(
2629 'type' => 'plugin',
2630 'list' => urldecode( implode( ',', $slugs ) ),
2631 )
2632 );
2633
2634 // do common process response.
2635 $response = $this->mainwp_run_process_success();
2636
2637 } else {
2638 // throw invalid data error.
2639 $response = $this->mainwp_invalid_data_error();
2640 }
2641 } else {
2642 // throw missing data error.
2643 $response = $this->mainwp_missing_data_error();
2644 }
2645 } else {
2646 // throw common error.
2647 $response = $this->mainwp_authentication_error();
2648 }
2649
2650 return $response;
2651 }
2652
2653 /**
2654 * Method mainwp_rest_api_site_update_themes_callback()
2655 *
2656 * Callback function for managing the response to API requests made for the endpoint: site-update-themes
2657 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/site/site-update-themes
2658 * API Method: PUT
2659 *
2660 * @param array $request The request made in the API call which includes all parameters.
2661 *
2662 * @return object $response An object that contains the return data and status of the API request.
2663 */
2664 public function mainwp_rest_api_site_update_themes_callback( $request ) {
2665
2666 // first validate the request.
2667 if ( $this->mainwp_validate_request( $request ) ) {
2668
2669 // get parameters.
2670 if ( null != $request['site_id'] ) {
2671
2672 if ( is_numeric( $request['site_id'] ) ) {
2673
2674 $site_id = $request['site_id'];
2675
2676 // get data.
2677 $website = MainWP_DB::instance()->get_website_by_id( $site_id );
2678 $theme_upgrades = json_decode( $website->theme_upgrades, true );
2679 $slugs = array();
2680 foreach ( $theme_upgrades as $slug => $theme ) {
2681 $slugs[] = $slug;
2682 }
2683 MainWP_Connect::fetch_url_authed(
2684 $website,
2685 'upgradeplugintheme',
2686 array(
2687 'type' => 'theme',
2688 'list' => urldecode( implode( ',', $slugs ) ),
2689 )
2690 );
2691
2692 // do common process response.
2693 $response = $this->mainwp_run_process_success();
2694
2695 } else {
2696 // throw invalid data error.
2697 $response = $this->mainwp_invalid_data_error();
2698 }
2699 } else {
2700 // throw missing data error.
2701 $response = $this->mainwp_missing_data_error();
2702 }
2703 } else {
2704 // throw common error.
2705 $response = $this->mainwp_authentication_error();
2706 }
2707
2708 return $response;
2709 }
2710
2711 /**
2712 * Method mainwp_rest_api_site_update_translations_callback()
2713 *
2714 * Callback function for managing the response to API requests made for the endpoint: site-update-translations
2715 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/site/site-update-translations
2716 * API Method: PUT
2717 *
2718 * @param array $request The request made in the API call which includes all parameters.
2719 *
2720 * @return object $response An object that contains the return data and status of the API request.
2721 */
2722 public function mainwp_rest_api_site_update_translations_callback( $request ) {
2723
2724 // first validate the request.
2725 if ( $this->mainwp_validate_request( $request ) ) {
2726
2727 // get parameters.
2728 if ( null != $request['site_id'] ) {
2729
2730 if ( is_numeric( $request['site_id'] ) ) {
2731
2732 $site_id = $request['site_id'];
2733
2734 // get data.
2735 $website = MainWP_DB::instance()->get_website_by_id( $site_id );
2736 $translation_upgrades = json_decode( $website->translation_upgrades, true );
2737 $slugs = array();
2738 foreach ( $translation_upgrades as $translation_upgrade ) {
2739 $slugs[] = $translation_upgrade['slug'];
2740 }
2741 MainWP_Connect::fetch_url_authed(
2742 $website,
2743 'upgradetranslation',
2744 array(
2745 'type' => 'translation',
2746 'list' => urldecode( implode( ',', $slugs ) ),
2747 )
2748 );
2749
2750 // do common process response.
2751 $response = $this->mainwp_run_process_success();
2752
2753 } else {
2754 // throw invalid data error.
2755 $response = $this->mainwp_invalid_data_error();
2756 }
2757 } else {
2758 // throw missing data error.
2759 $response = $this->mainwp_missing_data_error();
2760 }
2761 } else {
2762 // throw common error.
2763 $response = $this->mainwp_authentication_error();
2764 }
2765
2766 return $response;
2767 }
2768
2769 /**
2770 * Method mainwp_rest_api_site_update_item_callback()
2771 *
2772 * Callback function for managing the response to API requests made for the endpoint: site-update-item
2773 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/site/site-update-item
2774 * API Method: PUT
2775 *
2776 * @param array $request The request made in the API call which includes all parameters.
2777 *
2778 * @return object $response An object that contains the return data and status of the API request.
2779 */
2780 public function mainwp_rest_api_site_update_item_callback( $request ) {
2781
2782 // first validate the request.
2783 if ( $this->mainwp_validate_request( $request ) ) {
2784
2785 // get parameters.
2786
2787 if ( null != $request['site_id'] && null != $request['type'] && null != $request['slug'] ) {
2788
2789 if ( is_numeric( $request['site_id'] ) ) {
2790
2791 $site_id = $request['site_id'];
2792 $type = $request['type'];
2793 $slug = $request['slug'];
2794
2795 $website = MainWP_DB::instance()->get_website_by_id( $site_id );
2796 MainWP_Connect::fetch_url_authed(
2797 $website,
2798 'upgradeplugintheme',
2799 array(
2800 'type' => $type,
2801 'list' => urldecode( $slug ),
2802 )
2803 );
2804
2805 // do common process response.
2806 $response = $this->mainwp_run_process_success();
2807
2808 } else {
2809 // throw invalid data error.
2810 $response = $this->mainwp_invalid_data_error();
2811 }
2812 } else {
2813 // throw missing data error.
2814 $response = $this->mainwp_missing_data_error();
2815 }
2816 } else {
2817 // throw common error.
2818 $response = $this->mainwp_authentication_error();
2819 }
2820
2821 return $response;
2822 }
2823
2824 /**
2825 * Method mainwp_rest_api_site_manage_plugin_callback()
2826 *
2827 * Callback function for managing the response to API requests made for the endpoint: site-manage-plugin
2828 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/site/site-manage-plugin
2829 * API Method: POST
2830 *
2831 * @param array $request The request made in the API call which includes all parameters.
2832 *
2833 * @return object $response An object that contains the return data and status of the API request.
2834 */
2835 public function mainwp_rest_api_site_manage_plugin_callback( $request ) {
2836
2837 // first validate the request.
2838 if ( $this->mainwp_validate_request( $request ) ) {
2839
2840 // get parameters.
2841
2842 if ( null != $request['site_id'] && null != $request['plugin'] && null != $request['action'] ) {
2843
2844 if ( 'activate' == $request['action'] || 'deactivate' == $request['action'] || 'delete' == $request['action'] ) {
2845
2846 if ( is_numeric( $request['site_id'] ) ) {
2847
2848 $site_id = $request['site_id'];
2849 $plugin = $request['plugin'];
2850 $action = $request['action'];
2851
2852 $website = MainWP_DB::instance()->get_website_by_id( $site_id );
2853
2854 try {
2855 MainWP_Connect::fetch_url_authed(
2856 $website,
2857 'plugin_action',
2858 array(
2859 'action' => $action,
2860 'plugin' => $plugin,
2861 )
2862 );
2863 } catch ( \Exception $e ) {
2864 // ok.
2865 }
2866
2867 // do common process response.
2868 $response = $this->mainwp_run_process_success();
2869
2870 } else {
2871 // throw invalid data error.
2872 $response = $this->mainwp_invalid_data_error();
2873 }
2874 } else {
2875 // throw invalid data error.
2876 $response = $this->mainwp_invalid_data_error();
2877 }
2878 } else {
2879 // throw missing data error.
2880 $response = $this->mainwp_missing_data_error();
2881 }
2882 } else {
2883 // throw common error.
2884 $response = $this->mainwp_authentication_error();
2885 }
2886
2887 return $response;
2888 }
2889
2890 /**
2891 * Method mainwp_rest_api_site_manage_theme_callback()
2892 *
2893 * Callback function for managing the response to API requests made for the endpoint: site-manage-theme
2894 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/site/site-manage-theme
2895 * API Method: POST
2896 *
2897 * @param array $request The request made in the API call which includes all parameters.
2898 *
2899 * @return object $response An object that contains the return data and status of the API request.
2900 */
2901 public function mainwp_rest_api_site_manage_theme_callback( $request ) {
2902
2903 // first validate the request.
2904 if ( $this->mainwp_validate_request( $request ) ) {
2905
2906 // get parameters.
2907
2908 if ( null != $request['site_id'] && null != $request['theme'] && null != $request['action'] ) {
2909
2910 if ( 'activate' == $request['action'] || 'deactivate' == $request['action'] || 'delete' == $request['action'] ) {
2911
2912 if ( is_numeric( $request['site_id'] ) ) {
2913
2914 $site_id = $request['site_id'];
2915 $theme = $request['theme'];
2916 $action = $request['action'];
2917
2918 $website = MainWP_DB::instance()->get_website_by_id( $site_id );
2919
2920 try {
2921 MainWP_Connect::fetch_url_authed(
2922 $website,
2923 'theme_action',
2924 array(
2925 'action' => $action,
2926 'theme' => $theme,
2927 )
2928 );
2929 } catch ( \Exception $e ) {
2930 // ok.
2931 }
2932
2933 // do common process response.
2934 $response = $this->mainwp_run_process_success();
2935
2936 } else {
2937 // throw invalid data error.
2938 $response = $this->mainwp_invalid_data_error();
2939 }
2940 } else {
2941 // throw invalid data error.
2942 $response = $this->mainwp_invalid_data_error();
2943 }
2944 } else {
2945 // throw missing data error.
2946 $response = $this->mainwp_missing_data_error();
2947 }
2948 } else {
2949 // throw common error.
2950 $response = $this->mainwp_authentication_error();
2951 }
2952
2953 return $response;
2954 }
2955
2956 /**
2957 * Method mainwp_rest_api_check_site_http_status_callback()
2958 *
2959 * Callback function for managing the response to API requests made for the endpoint: check-site-http-status
2960 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/site/check-site-http-status
2961 * API Method: POST
2962 *
2963 * @param array $request The request made in the API call which includes all parameters.
2964 *
2965 * @return object $response An object that contains the return data and status of the API request.
2966 */
2967 public function mainwp_rest_api_check_site_http_status_callback( $request ) {
2968
2969 // first validate the request.
2970 if ( $this->mainwp_validate_request( $request ) ) {
2971
2972 // get parameters.
2973 if ( null != $request['site_id'] ) {
2974
2975 if ( is_numeric( $request['site_id'] ) ) {
2976
2977 $site_id = $request['site_id'];
2978
2979 // get data.
2980 $website = MainWP_DB::instance()->get_website_by_id( $site_id );
2981 MainWP_Monitoring_Handler::handle_check_website( $website );
2982
2983 // do common process response.
2984 $response = $this->mainwp_run_process_success();
2985
2986 } else {
2987 // throw invalid data error.
2988 $response = $this->mainwp_invalid_data_error();
2989 }
2990 } else {
2991 // throw missing data error.
2992 $response = $this->mainwp_missing_data_error();
2993 }
2994 } else {
2995 // throw common error.
2996 $response = $this->mainwp_authentication_error();
2997 }
2998
2999 return $response;
3000 }
3001
3002 /**
3003 * Method mainwp_rest_api_available_updates_callback()
3004 *
3005 * Callback function for managing the response to API requests made for the endpoint: available-updates
3006 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/updates/available-updates
3007 * API Method: GET
3008 *
3009 * @param array $request The request made in the API call which includes all parameters.
3010 *
3011 * @return object $response An object that contains the return data and status of the API request.
3012 */
3013 public function mainwp_rest_api_available_updates_callback( $request ) {
3014
3015 // first validate the request.
3016 if ( $this->mainwp_validate_request( $request ) ) {
3017
3018 $all_updates = array();
3019
3020 $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user( true ) );
3021
3022 while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
3023 $wp_upgrades = MainWP_DB::instance()->get_website_option( $website, 'wp_upgrades' );
3024 $wp_upgrades = ( '' != $wp_upgrades ) ? json_decode( $wp_upgrades, true ) : array();
3025
3026 $plugin_upgrades = json_decode( $website->plugin_upgrades, true );
3027 $theme_upgrades = json_decode( $website->theme_upgrades, true );
3028 $translation_upgrades = json_decode( $website->translation_upgrades, true );
3029 $all_updates[ $website->id ] = array(
3030 'wp_core' => $wp_upgrades,
3031 'plugins' => $plugin_upgrades,
3032 'themes' => $theme_upgrades,
3033 'translation' => $translation_upgrades,
3034 'groups' => $website->wpgroups,
3035 );
3036 }
3037 MainWP_DB::free_result( $websites );
3038
3039 // get data.
3040 $data = $all_updates;
3041
3042 $response = new \WP_REST_Response( $data );
3043 $response->set_status( 200 );
3044
3045 } else {
3046 // throw common error.
3047 $response = $this->mainwp_authentication_error();
3048 }
3049
3050 return $response;
3051 }
3052
3053 /**
3054 * Method mainwp_rest_api_ignored_plugins_updates_callback()
3055 *
3056 * Callback function for managing the response to API requests made for the endpoint: ignored-plugins-updates
3057 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/updates/ignored-plugins-updates
3058 * API Method: GET
3059 *
3060 * @param array $request The request made in the API call which includes all parameters.
3061 *
3062 * @return object $response An object that contains the return data and status of the API request.
3063 */
3064 public function mainwp_rest_api_ignored_plugins_updates_callback( $request ) {
3065
3066 // first validate the request.
3067 if ( $this->mainwp_validate_request( $request ) ) {
3068
3069 $userExtension = MainWP_DB_Common::instance()->get_user_extension();
3070
3071 // get data.
3072 $data = json_decode( $userExtension->ignored_plugins, true );
3073
3074 $response = new \WP_REST_Response( $data );
3075 $response->set_status( 200 );
3076
3077 } else {
3078 // throw common error.
3079 $response = $this->mainwp_authentication_error();
3080 }
3081
3082 return $response;
3083 }
3084
3085 /**
3086 * Method mainwp_rest_api_site_ignored_plugins_updates_callback()
3087 *
3088 * Callback function for managing the response to API requests made for the endpoint: site-ignored-plugins-updates
3089 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/updates/site-ignored-plugins-updates
3090 * API Method: GET
3091 *
3092 * @param array $request The request made in the API call which includes all parameters.
3093 *
3094 * @return object $response An object that contains the return data and status of the API request.
3095 */
3096 public function mainwp_rest_api_site_ignored_plugins_updates_callback( $request ) {
3097
3098 // first validate the request.
3099 if ( $this->mainwp_validate_request( $request ) ) {
3100
3101 // get parameters.
3102 if ( null != $request['site_id'] ) {
3103
3104 if ( is_numeric( $request['site_id'] ) ) {
3105
3106 $site_id = $request['site_id'];
3107
3108 // get data.
3109 $website = MainWP_DB::instance()->get_website_by_id( $site_id );
3110 $data = json_decode( $website->ignored_plugins, true );
3111
3112 $response = new \WP_REST_Response( $data );
3113 $response->set_status( 200 );
3114
3115 } else {
3116 // throw invalid data error.
3117 $response = $this->mainwp_invalid_data_error();
3118 }
3119 } else {
3120 // throw missing data error.
3121 $response = $this->mainwp_missing_data_error();
3122 }
3123 } else {
3124 // throw common error.
3125 $response = $this->mainwp_authentication_error();
3126 }
3127
3128 return $response;
3129 }
3130
3131 /**
3132 * Method mainwp_rest_api_ignored_themes_updates_callback()
3133 *
3134 * Callback function for managing the response to API requests made for the endpoint: ignored-themes-updates
3135 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/updates/ignored-themes-updates
3136 * API Method: GET
3137 *
3138 * @param array $request The request made in the API call which includes all parameters.
3139 *
3140 * @return object $response An object that contains the return data and status of the API request.
3141 */
3142 public function mainwp_rest_api_ignored_themes_updates_callback( $request ) {
3143
3144 // first validate the request.
3145 if ( $this->mainwp_validate_request( $request ) ) {
3146
3147 $userExtension = MainWP_DB_Common::instance()->get_user_extension();
3148
3149 // get data.
3150 $data = json_decode( $userExtension->ignored_themes, true );
3151
3152 $response = new \WP_REST_Response( $data );
3153 $response->set_status( 200 );
3154
3155 } else {
3156 // throw common error.
3157 $response = $this->mainwp_authentication_error();
3158 }
3159
3160 return $response;
3161 }
3162
3163 /**
3164 * Method mainwp_rest_api_site_ignored_themes_updates_callback()
3165 *
3166 * Callback function for managing the response to API requests made for the endpoint: site-ignored-themes-updates
3167 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/updates/site-ignored-themes-updates
3168 * API Method: GET
3169 *
3170 * @param array $request The request made in the API call which includes all parameters.
3171 *
3172 * @return object $response An object that contains the return data and status of the API request.
3173 */
3174 public function mainwp_rest_api_site_ignored_themes_updates_callback( $request ) {
3175
3176 // first validate the request.
3177 if ( $this->mainwp_validate_request( $request ) ) {
3178
3179 // get parameters.
3180 if ( null != $request['site_id'] ) {
3181
3182 if ( is_numeric( $request['site_id'] ) ) {
3183
3184 $site_id = $request['site_id'];
3185
3186 // get data.
3187 $website = MainWP_DB::instance()->get_website_by_id( $site_id );
3188 $data = json_decode( $website->ignored_themes, true );
3189
3190 $response = new \WP_REST_Response( $data );
3191 $response->set_status( 200 );
3192
3193 } else {
3194 // throw invalid data error.
3195 $response = $this->mainwp_invalid_data_error();
3196 }
3197 } else {
3198 // throw missing data error.
3199 $response = $this->mainwp_missing_data_error();
3200 }
3201 } else {
3202 // throw common error.
3203 $response = $this->mainwp_authentication_error();
3204 }
3205
3206 return $response;
3207 }
3208
3209 /**
3210 * Method mainwp_rest_api_ignore_updates_callback()
3211 *
3212 * Callback function for managing the response to API requests made for the endpoint: ignore-updates
3213 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/updates/ignore-updates
3214 * API Method: POST
3215 *
3216 * @param array $request The request made in the API call which includes all parameters.
3217 *
3218 * @return object $response An object that contains the return data and status of the API request.
3219 */
3220 public function mainwp_rest_api_ignore_updates_callback( $request ) {
3221
3222 // first validate the request.
3223 if ( $this->mainwp_validate_request( $request ) ) {
3224
3225 // get parameters.
3226 if ( null != $request['type'] && null != $request['slug'] && null != $request['name'] ) {
3227
3228 $type = $request['type'];
3229 $slug = $request['slug'];
3230 $name = $request['name'];
3231
3232 MainWP_Updates_Handler::ignore_plugins_themes( $type, $slug, $name );
3233
3234 // do common process response.
3235 $response = $this->mainwp_run_process_success();
3236
3237 } else {
3238 // throw missing data error.
3239 $response = $this->mainwp_missing_data_error();
3240 }
3241 } else {
3242 // throw common error.
3243 $response = $this->mainwp_authentication_error();
3244 }
3245
3246 return $response;
3247 }
3248
3249 /**
3250 * Method mainwp_rest_api_ignore_update_callback()
3251 *
3252 * Callback function for managing the response to API requests made for the endpoint: ignore-update
3253 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/updates/ignore-update
3254 * API Method: POST
3255 *
3256 * @param array $request The request made in the API call which includes all parameters.
3257 *
3258 * @return object $response An object that contains the return data and status of the API request.
3259 */
3260 public function mainwp_rest_api_ignore_update_callback( $request ) {
3261
3262 // first validate the request.
3263 if ( $this->mainwp_validate_request( $request ) ) {
3264
3265 // get parameters.
3266 if ( null != $request['type'] && null != $request['slug'] && null != $request['name'] && null != $request['site_id'] ) {
3267
3268 $site_id = $request['site_id'];
3269 $type = $request['type'];
3270 $slug = $request['slug'];
3271 $name = $request['name'];
3272
3273 MainWP_Updates_Handler::ignore_plugin_theme( $type, $slug, $name, $site_id );
3274
3275 // do common process response.
3276 $response = $this->mainwp_run_process_success();
3277
3278 } else {
3279 // throw missing data error.
3280 $response = $this->mainwp_missing_data_error();
3281 }
3282 } else {
3283 // throw common error.
3284 $response = $this->mainwp_authentication_error();
3285 }
3286
3287 return $response;
3288 }
3289
3290 /**
3291 * Method mainwp_rest_api_unignore_updates_callback()
3292 *
3293 * Callback function for managing the response to API requests made for the endpoint: unignore-updates
3294 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/updates/unignore-updates
3295 * API Method: POST
3296 *
3297 * @param array $request The request made in the API call which includes all parameters.
3298 *
3299 * @return object $response An object that contains the return data and status of the API request.
3300 */
3301 public function mainwp_rest_api_unignore_updates_callback( $request ) {
3302
3303 // first validate the request.
3304 if ( $this->mainwp_validate_request( $request ) ) {
3305
3306 // get parameters.
3307 if ( null != $request['type'] && null != $request['slug'] ) {
3308
3309 $type = $request['type'];
3310 $slug = $request['slug'];
3311
3312 MainWP_Updates_Handler::unignore_plugins_themes( $type, $slug );
3313
3314 // do common process response.
3315 $response = $this->mainwp_run_process_success();
3316
3317 } else {
3318 // throw missing data error.
3319 $response = $this->mainwp_missing_data_error();
3320 }
3321 } else {
3322 // throw common error.
3323 $response = $this->mainwp_authentication_error();
3324 }
3325
3326 return $response;
3327 }
3328
3329 /**
3330 * Method mainwp_rest_api_unignore_update_callback()
3331 *
3332 * Callback function for managing the response to API requests made for the endpoint: unignore-update
3333 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/updates/unignore-update
3334 * API Method: POST
3335 *
3336 * @param array $request The request made in the API call which includes all parameters.
3337 *
3338 * @return object $response An object that contains the return data and status of the API request.
3339 */
3340 public function mainwp_rest_api_unignore_update_callback( $request ) {
3341
3342 // first validate the request.
3343 if ( $this->mainwp_validate_request( $request ) ) {
3344
3345 // get parameters.
3346 if ( null != $request['type'] && null != $request['slug'] && null != $request['site_id'] ) {
3347
3348 $site_id = $request['site_id'];
3349 $type = $request['type'];
3350 $slug = $request['slug'];
3351
3352 MainWP_Updates_Handler::unignore_plugin_theme( $type, $slug, $site_id );
3353
3354 // do common process response.
3355 $response = $this->mainwp_run_process_success();
3356
3357 } else {
3358 // throw missing data error.
3359 $response = $this->mainwp_missing_data_error();
3360 }
3361 } else {
3362 // throw common error.
3363 $response = $this->mainwp_authentication_error();
3364 }
3365
3366 return $response;
3367 }
3368
3369 /**
3370 * Method mainwp_rest_api_add_client_callback()
3371 *
3372 * Callback function for managing the response to API requests made for the endpoint: add-client
3373 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/client/add-client
3374 * API Method: POST
3375 *
3376 * @param array $request The request made in the API call which includes all parameters.
3377 *
3378 * @return object $response An object that contains the return data and status of the API request.
3379 */
3380 public function mainwp_rest_api_add_client_callback( $request ) {
3381
3382 // first validate the request.
3383 if ( $this->mainwp_validate_request( $request ) ) {
3384
3385 // get data.
3386 $fields = $request->get_json_params();
3387
3388 try {
3389 $data = MainWP_Client_Handler::rest_api_add_client( $fields );
3390 $response = new \WP_REST_Response( $data );
3391 $response->set_status( 200 );
3392 } catch ( Exception $e ) {
3393 $response = $this->mainwp_invalid_data_error( $e->getMessage() );
3394 }
3395 } else {
3396 // throw common error.
3397 $response = $this->mainwp_authentication_error();
3398 }
3399
3400 return $response;
3401 }
3402
3403
3404 /**
3405 * Method mainwp_rest_api_edit_client_callback()
3406 *
3407 * Callback function for managing the response to API requests made for the endpoint: edit-client
3408 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/client/edit-client
3409 * API Method: PUT
3410 *
3411 * @param array $request The request made in the API call which includes all parameters.
3412 *
3413 * @return object $response An object that contains the return data and status of the API request.
3414 */
3415 public function mainwp_rest_api_edit_client_callback( $request ) {
3416
3417 // first validate the request.
3418 if ( $this->mainwp_validate_request( $request ) ) {
3419
3420 // get data.
3421 $fields = $request->get_json_params();
3422
3423 try {
3424 $data = MainWP_Client_Handler::rest_api_add_client( $fields, true );
3425 $response = new \WP_REST_Response( $data );
3426 $response->set_status( 200 );
3427 } catch ( Exception $e ) {
3428 $response = $this->mainwp_invalid_data_error( $e->getMessage() );
3429 }
3430 } else {
3431 // throw common error.
3432 $response = $this->mainwp_authentication_error();
3433 }
3434
3435 return $response;
3436 }
3437
3438 /**
3439 * Method mainwp_rest_api_remove_client_callback()
3440 *
3441 * Callback function for managing the response to API requests made for the endpoint: remove-client
3442 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/client/remove-client
3443 * API Method: GET
3444 *
3445 * @param array $request The request made in the API call which includes all parameters.
3446 *
3447 * @return object $response An object that contains the return data and status of the API request.
3448 */
3449 public function mainwp_rest_api_remove_client_callback( $request ) {
3450
3451 // first validate the request.
3452 if ( $this->mainwp_validate_request( $request ) ) {
3453 $client_id = isset( $request['client_id'] ) ? intval( $request['client_id'] ) : 0;
3454 if ( ! empty( $client_id ) ) {
3455 MainWP_DB_Client::instance()->delete_client( $client_id );
3456 // do common process response.
3457 $response = $this->mainwp_run_process_success();
3458 } else {
3459 $response = $this->mainwp_invalid_data_error();
3460 }
3461 } else {
3462 // throw common error.
3463 $response = $this->mainwp_authentication_error();
3464 }
3465
3466 return $response;
3467 }
3468
3469 /**
3470 * Method mainwp_rest_api_all_clients_callback()
3471 *
3472 * Callback function for managing the response to API requests made for the endpoint: all-clients
3473 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/clients/all-clients
3474 * API Method: GET
3475 *
3476 * @param array $request The request made in the API call which includes all parameters.
3477 *
3478 * @return object $response An object that contains the return data and status of the API request.
3479 */
3480 public function mainwp_rest_api_all_clients_callback( $request ) {
3481
3482 // first validate the request.
3483 if ( $this->mainwp_validate_request( $request ) ) {
3484
3485 $params = array(
3486 'client' => isset( $request['client'] ) ? $request['client'] : '',
3487 'custom_fields' => isset( $request['custom_fields'] ) && $request['custom_fields'] ? true : false,
3488 'with_tags' => isset( $request['with_tags'] ) && $request['with_tags'] ? true : false,
3489 'with_contacts' => isset( $request['with_contacts'] ) && $request['with_contacts'] ? true : false,
3490 );
3491
3492 // get data.
3493 $data = MainWP_DB_Client::instance()->get_wp_clients( $params );
3494
3495 $result = array(
3496 'data' => $data,
3497 );
3498
3499 $response = new \WP_REST_Response( $result );
3500 $response->set_status( 200 );
3501
3502 } else {
3503 // throw common error.
3504 $response = $this->mainwp_authentication_error();
3505 }
3506
3507 return $response;
3508 }
3509
3510 /**
3511 * Method mainwp_rest_api_all_tags_callback()
3512 *
3513 * Callback function for managing the response to API requests made for the endpoint: all-tags
3514 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/tags/all-tags
3515 * API Method: GET
3516 *
3517 * @param array $request The request made in the API call which includes all parameters.
3518 *
3519 * @return object $response An object that contains the return data and status of the API request.
3520 */
3521 public function mainwp_rest_api_all_tags_callback( $request ) {
3522
3523 // first validate the request.
3524 if ( $this->mainwp_validate_request( $request ) ) {
3525
3526 $data = array();
3527
3528 $tag_id = isset( $request['tag_id'] ) ? intval( $request['tag_id'] ) : 0;
3529 $tag_name = isset( $request['tag_name'] ) ? (string) $request['tag_name'] : '';
3530
3531 $groups = MainWP_DB_Common::instance()->get_groups_and_count();
3532
3533 if ( $groups ) {
3534 foreach ( $groups as $group ) {
3535 if ( ! empty( $tag_id ) ) {
3536 if ( $group->id == $tag_id ) {
3537 $data[ $group->id ] = $group->name;
3538 }
3539 } elseif ( ! empty( $tag_name ) ) {
3540 if ( $group->name == $tag_name ) {
3541 $data[ $group->id ] = $group->name;
3542 }
3543 } else {
3544 $data[ $group->id ] = $group->name;
3545 }
3546 }
3547 }
3548
3549 $result = array(
3550 'data' => $data,
3551 );
3552
3553 $response = new \WP_REST_Response( $result );
3554 $response->set_status( 200 );
3555
3556 } else {
3557 // throw common error.
3558 $response = $this->mainwp_authentication_error();
3559 }
3560
3561 return $response;
3562 }
3563
3564
3565 }
3566 // End of class.
3567