Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
50.00% covered (danger)
50.00%
12 / 24
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
WP_REST_Help_Center_Jetpack_Connection_Health
50.00% covered (danger)
50.00%
12 / 24
66.67% covered (warning)
66.67%
2 / 3
10.50
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 register_rest_route
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 get_connection_health
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2/**
3 * WP_REST_Help_Center_Jetpack_Connection_Health file.
4 *
5 * @package automattic/jetpack-help-center
6 */
7
8namespace Automattic\Jetpack\Help_Center;
9
10/**
11 * Class WP_REST_Help_Center_Jetpack_Connection_Health.
12 */
13class WP_REST_Help_Center_Jetpack_Connection_Health extends WP_REST_Help_Center_Controller {
14    /**
15     * WP_REST_Help_Center_Jetpack_Connection_Health constructor.
16     *
17     * @param Wpcom_Request_Client|null $wpcom_request_client WP.com request client.
18     */
19    public function __construct( ?Wpcom_Request_Client $wpcom_request_client = null ) {
20        parent::__construct( $wpcom_request_client );
21        $this->namespace = 'help-center';
22        $this->rest_base = '/jetpack-connection-health';
23    }
24
25    /**
26     * Register available routes.
27     */
28    public function register_rest_route() {
29        register_rest_route(
30            $this->namespace,
31            $this->rest_base,
32            array(
33                'methods'             => \WP_REST_Server::READABLE,
34                'callback'            => array( $this, 'get_connection_health' ),
35                'permission_callback' => '__return_true',
36            )
37        );
38    }
39
40    /**
41     * Return the Jetpack connection health for the current site.
42     */
43    public function get_connection_health() {
44        /*
45         * Atomic sites have the WP.com blog ID stored as a Jetpack option. This code deliberately
46         * doesn't use `Jetpack_Options::get_option` so it works even when Jetpack has not been loaded.
47         */
48        $jetpack_options = get_option( 'jetpack_options' );
49        if ( is_array( $jetpack_options ) && isset( $jetpack_options['id'] ) ) {
50            $site = (int) $jetpack_options['id'];
51        } else {
52            $site = get_current_blog_id();
53        }
54
55        $body = $this->wpcom_request_client->request(
56            'sites/' . $site . '/jetpack-connection-health',
57            '2'
58        );
59
60        if ( is_wp_error( $body ) ) {
61            return $body;
62        }
63
64        $response = json_decode( wp_remote_retrieve_body( $body ) );
65
66        return rest_ensure_response( $response );
67    }
68}