PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 6.2.2
MainWP Dashboard: Self-hosted WordPress Management for Agencies v6.2.2
6.2.2 6.2.1 6.2 6.1.8 6.1.7 6.1.6 6.1.5 6.1.4 6.1.3 6.1.2 6.1.1 6.1 6.0.12 6.0.11 4.6.0.1 5.0 5.0.1 5.0.2 5.0.3 5.0.3.1 5.0.3.2 5.1 5.1.1 5.2 5.2.1 All 155 releases
mainwp / includes / rest-api / controller / version1 / class-mainwp-rest-api-v1.php

class-mainwp-rest-api-v1.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies 6.2.2, at includes/rest-api/controller/version1/class-mainwp-rest-api-v1.php

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