This repository has been archived by the owner on Dec 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp-api-libraries-base.php
141 lines (132 loc) · 3.68 KB
/
wp-api-libraries-base.php
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
<?php
/**
* Base API Class
*
* @package wp-api-libraries-base
*/
/* Exit if accessed directly. */
if ( ! defined( 'ABSPATH' ) ) { exit; }
/* Check if class exists. */
if ( ! class_exists( 'BasecampAPIBase' ) ) {
/**
* Abstract BasecampAPIBase class.
*
* @abstract
*/
abstract class BasecampAPIBase {
/**
* Arguments.
*
* @var mixed
* @access protected
*/
protected $args;
/**
* Base URI.
*
* @var mixed
* @access protected
*/
protected $base_uri;
/**
* Is Debug.
*
* @var mixed
* @access protected
*/
protected $is_debug;
/**
* __construct function.
*
* @access public
* @param mixed $base_uri Base URI.
* @param mixed $debug Debug.
* @return void
*/
public function __construct( $base_uri, $debug = false ) {
$this->base_uri = $base_uri;
$this->is_debug = $debug;
}
/**
* Build request function: prepares the class for a fetch request.
*
* @access protected
* @param mixed $route Route, URL to be accessed.
* @param array $body (default: array()) Arguments to pass in. If the method is GET, will be passed as query arguments attached to the route. If the method is not get, but the content type as defined in headers is 'application/json', then the body of the request will be set to a json_encode of $args. Otherwise, they will be passed as the body.
* @param string $method (default: 'GET') The method.
* @return The return of the function.
*/
protected function build_request( $route, $body = array(), $method = 'GET' ) {
// Sets headers.
$this->set_headers();
// Sets method.
$this->args['method'] = $method;
// Sets route.
$this->route = $route;
// Merge bodies.
if( isset( $this->args['body'] ) ){
$body = array_merge( $this->args['body'], $body );
}
// If method is get, then there is no body.
if ( 'GET' === $method ) {
$this->route = add_query_arg( array_filter( $body ), $route );
} // Otherwise, if the content type is application/json, then the body needs to be json_encoded.
elseif ( isset( $this->args['headers']['Content-Type'] ) && 'application/json' === $this->args['headers']['Content-Type'] ) {
$this->args['body'] = wp_json_encode( $body );
} // Anything else, let the user take care of it.
else {
$this->args['body'] = $body;
}
return $this;
}
/**
* Fetch.
*
* @access protected
* @return Request Body.
*/
protected function fetch() {
// Make the request.
// pp( $this->base_uri . $this->route, $this->args );
$response = wp_remote_request( $this->base_uri . $this->route, $this->args );
// Retrieve status code and body.
$code = wp_remote_retrieve_response_code( $response );
$body = json_decode( wp_remote_retrieve_body( $response ) );
$this->clear();
if ( ! $this->is_status_ok( $code ) && ! $this->is_debug ) {
return new WP_Error( 'response-error', sprintf( __( 'Status: &d', 'wp-libraries-api-base' ), $code ), $body );
}
return $body;
}
/**
* Function to be overwritten, gets called before call. Should be used to set headers.
*
* @access protected
* @abstract
* @return void
*/
protected function set_headers(){
$this->args = array();
}
/**
* Function to be overwritten, gets called after the request has been made (if status code was ok). Should be used to reset headers.
*
* @access protected
* @abstract
* @return void
*/
protected function clear(){
$this->args = array();
}
/**
* Returns whether status is in [ 200, 300 ).
*
* @access protected
* @param mixed $code Code.
* @return Status Code.
*/
protected function is_status_ok( $code ) {
return ( 200 <= $code && 300 > $code );
}
}
}