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