class-wpml-rest.php
1.07 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
<?php
/**
* @author OnTheGo Systems
*/
class WPML_Rest {
private $http;
/**
* WPML_Rest constructor.
*
* @param WP_Http $http
*/
public function __construct( WP_Http $http ) {
$this->http = $http;
}
public function is_available() {
return function_exists( 'rest_get_server' );
}
public function is_rest_request() {
return defined( 'REST_REQUEST' ) && REST_REQUEST;
}
public function has_registered_routes() {
return (bool) rest_get_server()->get_routes();
}
public function has_discovered_routes() {
return (bool) $this->get_discovered_routes();
}
private function get_discovered_routes() {
$url = $this->get_discovery_url();
$response = $this->http->get( $url );
$body = json_decode( $response['body'], true );
return array_key_exists( 'routes', $body ) ? $body['routes'] : array();
}
public function get_discovery_url() {
$url_prefix = 'wp-json';
if ( function_exists( 'rest_get_url_prefix' ) ) {
$url_prefix = rest_get_url_prefix();
}
return untrailingslashit( trailingslashit( get_site_url() ) . $url_prefix );
}
}