|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* WooCommerce API |
|
4
|
|
|
* |
|
5
|
|
|
* Handles WC-API endpoint requests. |
|
6
|
|
|
* |
|
7
|
|
|
* @author WooThemes |
|
8
|
|
|
* @category API |
|
9
|
|
|
* @package WooCommerce/API |
|
10
|
|
|
* @since 2.0 |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
|
14
|
|
|
exit; |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
if ( ! class_exists( 'WC_Legacy_API' ) ) { |
|
18
|
|
|
include_once( 'class-wc-legacy-api.php' ); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
class WC_API extends WC_Legacy_API { |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Setup class. |
|
25
|
|
|
* @since 2.0 |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __construct() { |
|
28
|
|
|
parent::__construct(); |
|
29
|
|
|
|
|
30
|
|
|
// Add query vars. |
|
31
|
|
|
add_filter( 'query_vars', array( $this, 'add_query_vars' ), 0 ); |
|
32
|
|
|
|
|
33
|
|
|
// Register API endpoints. |
|
34
|
|
|
add_action( 'init', array( $this, 'add_endpoint' ), 0 ); |
|
35
|
|
|
|
|
36
|
|
|
// Handle wc-api endpoint requests. |
|
37
|
|
|
add_action( 'parse_request', array( $this, 'handle_api_requests' ), 0 ); |
|
38
|
|
|
|
|
39
|
|
|
// Ensure payment gateways are initialized in time for API requests. |
|
40
|
|
|
add_action( 'woocommerce_api_request', array( 'WC_Payment_Gateways', 'instance' ), 0 ); |
|
41
|
|
|
|
|
42
|
|
|
// WP REST API. |
|
43
|
|
|
$this->rest_api_init(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Add new query vars. |
|
48
|
|
|
* |
|
49
|
|
|
* @since 2.0 |
|
50
|
|
|
* @param array $vars |
|
51
|
|
|
* @return string[] |
|
52
|
|
|
*/ |
|
53
|
|
|
public function add_query_vars( $vars ) { |
|
54
|
|
|
$vars = parent::add_query_vars( $vars ); |
|
55
|
|
|
$vars[] = 'wc-api'; |
|
56
|
|
|
return $vars; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* WC API for payment gateway IPNs, etc. |
|
61
|
|
|
* @since 2.0 |
|
62
|
|
|
*/ |
|
63
|
|
|
public static function add_endpoint() { |
|
64
|
|
|
parent::add_endpoint(); |
|
65
|
|
|
add_rewrite_endpoint( 'wc-api', EP_ALL ); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* API request - Trigger any API requests. |
|
70
|
|
|
* |
|
71
|
|
|
* @since 2.0 |
|
72
|
|
|
* @version 2.4 |
|
73
|
|
|
*/ |
|
74
|
|
|
public function handle_api_requests() { |
|
75
|
|
|
global $wp; |
|
76
|
|
|
|
|
77
|
|
|
if ( ! empty( $_GET['wc-api'] ) ) { |
|
78
|
|
|
$wp->query_vars['wc-api'] = $_GET['wc-api']; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
// wc-api endpoint requests. |
|
82
|
|
|
if ( ! empty( $wp->query_vars['wc-api'] ) ) { |
|
83
|
|
|
|
|
84
|
|
|
// Buffer, we won't want any output here. |
|
85
|
|
|
ob_start(); |
|
86
|
|
|
|
|
87
|
|
|
// No cache headers. |
|
88
|
|
|
nocache_headers(); |
|
89
|
|
|
|
|
90
|
|
|
// Clean the API request. |
|
91
|
|
|
$api_request = strtolower( wc_clean( $wp->query_vars['wc-api'] ) ); |
|
92
|
|
|
|
|
93
|
|
|
// Trigger generic action before request hook. |
|
94
|
|
|
do_action( 'woocommerce_api_request', $api_request ); |
|
95
|
|
|
|
|
96
|
|
|
// Is there actually something hooked into this API request? If not trigger 400 - Bad request. |
|
97
|
|
|
status_header( has_action( 'woocommerce_api_' . $api_request ) ? 200 : 400 ); |
|
98
|
|
|
|
|
99
|
|
|
// Trigger an action which plugins can hook into to fulfill the request. |
|
100
|
|
|
do_action( 'woocommerce_api_' . $api_request ); |
|
101
|
|
|
|
|
102
|
|
|
// Done, clear buffer and exit. |
|
103
|
|
|
ob_end_clean(); |
|
104
|
|
|
die( '-1' ); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Init WP REST API. |
|
110
|
|
|
* @since 2.6.0 |
|
111
|
|
|
*/ |
|
112
|
|
|
private function rest_api_init() { |
|
113
|
|
|
global $wp_version; |
|
114
|
|
|
|
|
115
|
|
|
// REST API was included starting WordPress 4.4. |
|
116
|
|
|
if ( version_compare( $wp_version, 4.4, '<' ) ) { |
|
117
|
|
|
return; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
$this->rest_api_includes(); |
|
121
|
|
|
|
|
122
|
|
|
// Init REST API routes. |
|
123
|
|
|
add_action( 'rest_api_init', array( $this, 'register_rest_routes' ) ); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Include REST API classes. |
|
128
|
|
|
* @since 2.6.0 |
|
129
|
|
|
*/ |
|
130
|
|
|
private function rest_api_includes() { |
|
131
|
|
|
// Exception handler. |
|
132
|
|
|
include_once( 'api/class-wc-rest-exception.php' ); |
|
133
|
|
|
|
|
134
|
|
|
// Authentication. |
|
135
|
|
|
include_once( 'api/class-wc-rest-authentication.php' ); |
|
136
|
|
|
|
|
137
|
|
|
// WP-API classes and functions. |
|
138
|
|
|
include_once( 'vendor/wp-rest-functions.php' ); |
|
139
|
|
|
if ( ! class_exists( 'WP_REST_Controller' ) ) { |
|
140
|
|
|
include_once( 'vendor/class-wp-rest-controller.php' ); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
// Abstract controllers. |
|
144
|
|
|
include_once( 'abstracts/abstract-wc-rest-controller.php' ); |
|
145
|
|
|
include_once( 'abstracts/abstract-wc-rest-posts-controller.php' ); |
|
146
|
|
|
include_once( 'abstracts/abstract-wc-rest-terms-controller.php' ); |
|
147
|
|
|
|
|
148
|
|
|
// REST API controllers. |
|
149
|
|
|
include_once( 'api/class-wc-rest-coupons-controller.php' ); |
|
150
|
|
|
include_once( 'api/class-wc-rest-customer-downloads-controller.php' ); |
|
151
|
|
|
include_once( 'api/class-wc-rest-customers-controller.php' ); |
|
152
|
|
|
include_once( 'api/class-wc-rest-order-notes-controller.php' ); |
|
153
|
|
|
include_once( 'api/class-wc-rest-order-refunds-controller.php' ); |
|
154
|
|
|
include_once( 'api/class-wc-rest-orders-controller.php' ); |
|
155
|
|
|
include_once( 'api/class-wc-rest-product-attribute-terms-controller.php' ); |
|
156
|
|
|
include_once( 'api/class-wc-rest-product-attributes-controller.php' ); |
|
157
|
|
|
include_once( 'api/class-wc-rest-product-categories-controller.php' ); |
|
158
|
|
|
include_once( 'api/class-wc-rest-product-reviews-controller.php' ); |
|
159
|
|
|
include_once( 'api/class-wc-rest-product-shipping-classes-controller.php' ); |
|
160
|
|
|
include_once( 'api/class-wc-rest-product-tags-controller.php' ); |
|
161
|
|
|
include_once( 'api/class-wc-rest-products-controller.php' ); |
|
162
|
|
|
include_once( 'api/class-wc-rest-report-sales-controller.php' ); |
|
163
|
|
|
include_once( 'api/class-wc-rest-report-top-sellers-controller.php' ); |
|
164
|
|
|
include_once( 'api/class-wc-rest-reports-controller.php' ); |
|
165
|
|
|
include_once( 'api/class-wc-rest-tax-classes-controller.php' ); |
|
166
|
|
|
include_once( 'api/class-wc-rest-taxes-controller.php' ); |
|
167
|
|
|
include_once( 'api/class-wc-rest-webhook-deliveries.php' ); |
|
168
|
|
|
include_once( 'api/class-wc-rest-webhooks-controller.php' ); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Register REST API routes. |
|
173
|
|
|
* @since 2.6.0 |
|
174
|
|
|
*/ |
|
175
|
|
|
public function register_rest_routes() { |
|
176
|
|
|
$controllers = array( |
|
177
|
|
|
'WC_REST_Coupons_Controller', |
|
178
|
|
|
'WC_REST_Customer_Downloads_Controller', |
|
179
|
|
|
'WC_REST_Customers_Controller', |
|
180
|
|
|
'WC_REST_Order_Notes_Controller', |
|
181
|
|
|
'WC_REST_Order_Refunds_Controller', |
|
182
|
|
|
'WC_REST_Orders_Controller', |
|
183
|
|
|
'WC_REST_Product_Attribute_Terms_Controller', |
|
184
|
|
|
'WC_REST_Product_Attributes_Controller', |
|
185
|
|
|
'WC_REST_Product_Categories_Controller', |
|
186
|
|
|
'WC_REST_Product_Reviews_Controller', |
|
187
|
|
|
'WC_REST_Product_Shipping_Classes_Controller', |
|
188
|
|
|
'WC_REST_Product_Tags_Controller', |
|
189
|
|
|
'WC_REST_Products_Controller', |
|
190
|
|
|
'WC_REST_Report_Sales_Controller', |
|
191
|
|
|
'WC_REST_Report_Top_Sellers_Controller', |
|
192
|
|
|
'WC_REST_Reports_Controller', |
|
193
|
|
|
'WC_REST_Tax_Classes_Controller', |
|
194
|
|
|
'WC_REST_Taxes_Controller', |
|
195
|
|
|
'WC_REST_Webhook_Deliveries_Controller', |
|
196
|
|
|
'WC_REST_Webhooks_Controller', |
|
197
|
|
|
); |
|
198
|
|
|
|
|
199
|
|
|
foreach ( $controllers as $controller ) { |
|
200
|
|
|
$this->$controller = new $controller(); |
|
201
|
|
|
$this->$controller->register_routes(); |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|