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

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