forked from AfterShip/aftership-apps-woocommerce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-aftership-api.php
182 lines (143 loc) · 3.66 KB
/
class-aftership-api.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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
/**
* AfterShip API
*
* Handles AfterShip-API endpoint requests
*
* @author AfterShip
* @category API
* @package AfterShip
* @since 1.0
*/
if (!defined('ABSPATH')) exit; // Exit if accessed directly
class AfterShip_API
{
/** This is the major version for the REST API and takes
* first-order position in endpoint URLs
*/
const VERSION = 1;
/** @var WC_API_Server the REST API server */
public $server;
/**
* Setup class
*
* @access public
* @since 2.0
* @return WC_API
*/
public function __construct()
{
// add query vars
add_filter('query_vars', array($this, 'add_query_vars'), 0);
// register API endpoints
add_action('init', array($this, 'add_endpoint'), 0);
// handle REST/legacy API request
add_action('parse_request', array($this, 'handle_api_requests'), 0);
}
/**
* add_query_vars function.
*
* @access public
* @since 2.0
* @param $vars
* @return array
*/
public function add_query_vars($vars)
{
$vars[] = 'aftership-api';
$vars[] = 'aftership-api-route';
return $vars;
}
/**
* add_endpoint function.
*
* @access public
* @since 2.0
* @return void
*/
public function add_endpoint()
{
// REST API
add_rewrite_rule('^aftership-api\/v' . self::VERSION . '/?$', 'index.php?aftership-api-route=/', 'top');
add_rewrite_rule('^aftership-api\/v' . self::VERSION . '(.*)?', 'index.php?aftership-api-route=$matches[1]', 'top');
// legacy API for payment gateway IPNs
add_rewrite_endpoint('aftership-api', EP_ALL);
}
/**
* API request - Trigger any API requests
*
* @access public
* @since 2.0
* @return void
*/
public function handle_api_requests()
{
global $wp;
if (!empty($_GET['aftership-api']))
$wp->query_vars['aftership-api'] = $_GET['aftership-api'];
if (!empty($_GET['aftership-api-route']))
$wp->query_vars['aftership-api-route'] = $_GET['aftership-api-route'];
// REST API request
if (!empty($wp->query_vars['aftership-api-route'])) {
define('AFTERSHIP_API_REQUEST', true);
// load required files
$this->includes();
$this->server = new AfterShip_API_Server($wp->query_vars['aftership-api-route']);
// load API resource classes
$this->register_resources($this->server);
// Fire off the request
$this->server->serve_request();
exit;
}
// legacy API requests
if (!empty($wp->query_vars['aftership-api'])) {
// Buffer, we won't want any output here
ob_start();
// Get API trigger
$api = strtolower(esc_attr($wp->query_vars['aftership-api']));
// Load class if exists
if (class_exists($api))
$api_class = new $api();
// Trigger actions
do_action('woocommerce_api_' . $api);
// Done, clear buffer and exit
ob_end_clean();
die('1');
}
}
/**
* Include required files for REST API request
*
* @since 2.1
*/
private function includes()
{
// API server / response handlers
include_once('api/class-aftership-api-server.php');
include_once('api/interface-aftership-api-handler.php');
include_once('api/class-aftership-api-json-handler.php');
// authentication
include_once('api/class-aftership-api-authentication.php');
$this->authentication = new AfterShip_API_Authentication();
include_once('api/class-aftership-api-resource.php');
// self api
include_once('api/class-aftership-api-orders.php');
}
/**
* Register available API resources
*
* @since 2.1
* @param object $server the REST server
*/
public function register_resources($server)
{
$api_classes = apply_filters('aftership_api_classes',
array(
'AfterShip_API_Orders',
)
);
foreach ($api_classes as $api_class) {
$this->$api_class = new $api_class($server);
}
}
}