-
Notifications
You must be signed in to change notification settings - Fork 341
Expand file tree
/
Copy pathHealth_Checks.php
More file actions
167 lines (150 loc) · 3.99 KB
/
Health_Checks.php
File metadata and controls
167 lines (150 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
/**
* Class Google\Site_Kit\Core\Util\Health_Checks
*
* @package Google\Site_Kit\Core\Util
* @copyright 2021 Google LLC
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @link https://sitekit.withgoogle.com
*/
namespace Google\Site_Kit\Core\Util;
use Exception;
use Google\Site_Kit\Core\Authentication\Authentication;
use Google\Site_Kit\Core\Authentication\Google_Proxy;
use Google\Site_Kit\Core\Permissions\Permissions;
use Google\Site_Kit\Core\REST_API\REST_Route;
use Google\Site_Kit_Dependencies\Google\Service\SearchConsole as Google_Service_SearchConsole;
use Google\Site_Kit_Dependencies\Google_Service_Exception;
use WP_REST_Server;
/**
* Class for performing health checks.
*
* @since 1.14.0
* @access private
* @ignore
*/
class Health_Checks {
/**
* Authentication instance.
*
* @var Authentication
*/
protected $authentication;
/**
* Google_Proxy instance.
*
* @var Google_Proxy
*/
protected $google_proxy;
/**
* Constructor.
*
* @param Authentication $authentication Authentication instance.
*/
public function __construct( Authentication $authentication ) {
$this->authentication = $authentication;
$this->google_proxy = $authentication->get_google_proxy();
}
/**
* Registers functionality through WordPress hooks.
*
* @since 1.14.0
*/
public function register() {
add_filter(
'googlesitekit_rest_routes',
function ( $rest_routes ) {
$health_check_routes = $this->get_rest_routes();
return array_merge( $rest_routes, $health_check_routes );
}
);
}
/**
* Gets all health check REST routes.
*
* @since 1.14.0
*
* @return REST_Route[] List of REST_Route objects.
*/
private function get_rest_routes() {
return array(
new REST_Route(
'core/site/data/health-checks',
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => function () {
$checks = array(
'googleAPI' => $this->check_google_api(),
'skService' => $this->check_service_connectivity(),
);
return compact( 'checks' );
},
'permission_callback' => function () {
return current_user_can( Permissions::VIEW_SHARED_DASHBOARD ) || current_user_can( Permissions::SETUP );
},
),
)
),
);
}
/**
* Checks connection to Google APIs.
*
* @since 1.14.0
*
* @return array Results data.
*/
private function check_google_api() {
$client = $this->authentication->get_oauth_client()->get_client();
$restore_defer = $client->withDefer( false );
$error_msg = '';
// Make a request to the Search API.
// This request is bound to fail but this is okay as long as the error response comes
// from a Google API endpoint (Google_Service_exception). The test is only intended
// to check that the server is capable of connecting to the Google API (at all)
// regardless of valid authentication, which will likely be missing here.
try {
( new Google_Service_SearchConsole( $client ) )->sites->listSites();
$pass = true;
} catch ( Google_Service_Exception $e ) {
if ( ! empty( $e->getErrors() ) ) {
$pass = true;
} else {
$pass = false;
$error_msg = $e->getMessage();
}
} catch ( Exception $e ) {
$pass = false;
$error_msg = $e->getMessage();
}
$restore_defer();
return array(
'pass' => $pass,
'errorMsg' => $error_msg,
);
}
/**
* Checks connection to Site Kit service.
*
* @since 1.85.0
*
* @return array Results data.
*/
private function check_service_connectivity() {
$service_url = $this->google_proxy->url();
$response = wp_remote_head( $service_url );
if ( is_wp_error( $response ) ) {
return array(
'pass' => false,
'errorMsg' => $response->get_error_message(),
);
}
$status_code = wp_remote_retrieve_response_code( $response );
$pass = is_int( $status_code ) && $status_code < 400;
return array(
'pass' => $pass,
'errorMsg' => $pass ? '' : 'connection_fail',
);
}
}