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 1
/
wp-api-libraries-base.php
100 lines (74 loc) · 2.62 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
<?php
/**
* Base API Class
*/
/* Exit if accessed directly. */
if( !defined( 'ABSPATH' ) ) { exit; }
/* Check if class exists. */
if( !class_exists( 'WpCMLibrariesBase' ) ) {
/* Define abstract class. */
abstract class WpCMLibrariesBase{
/* */
protected $args;
protected $base_uri;
public function __construct( $base_uri ){
$this->base_uri = $base_uri;
}
/**
* Build request function: prepares the class for a fetch request.
*
*
* @param string $route URL to be accessed.
* @param array $args 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 [type] 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;
// 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
else if( '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;
}
protected function fetch(){
// Make the request.
$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 ) ) {
return new WP_Error( 'response-error', sprintf( __( 'Status: %d', 'wp-postmark-api' ), $code ), $body );
}
return $body;
}
/**
* Function to be overwritten, gets called before call. Should be used to set headers.
*/
abstract protected function set_headers();
/**
* Function to be overwritten, gets called after the request has been made (if status code was ok). Should be used to reset headers.
*/
abstract protected function clear();
/**
* Returns whether status is in [ 200, 300 ).
*/
protected function is_status_ok( $code ){
return ( 200 <= $code && 300 > $code );
}
}
}