1 | <?php |
||
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 ) { |
||
58 | |||
59 | /** |
||
60 | * WC API for payment gateway IPNs, etc. |
||
61 | * @since 2.0 |
||
62 | */ |
||
63 | public static function add_endpoint() { |
||
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() { |
||
107 | |||
108 | /** |
||
109 | * Init WP REST API. |
||
110 | * @since 2.6.0 |
||
111 | */ |
||
112 | private function rest_api_init() { |
||
113 | // REST API was included starting WordPress 4.4. |
||
114 | if ( ! class_exists( 'WP_REST_Server' ) ) { |
||
115 | return; |
||
116 | } |
||
117 | |||
118 | $this->rest_api_includes(); |
||
119 | |||
120 | // Init REST API routes. |
||
121 | add_action( 'rest_api_init', array( $this, 'register_rest_routes' ) ); |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Include REST API classes. |
||
126 | * @since 2.6.0 |
||
127 | */ |
||
128 | private function rest_api_includes() { |
||
129 | // Exception handler. |
||
130 | include_once( dirname( __FILE__ ) . '/api/class-wc-rest-exception.php' ); |
||
131 | |||
132 | // Authentication. |
||
133 | include_once( dirname( __FILE__ ) . '/api/class-wc-rest-authentication.php' ); |
||
134 | |||
135 | // WP-API classes and functions. |
||
136 | include_once( dirname( __FILE__ ) . '/vendor/wp-rest-functions.php' ); |
||
137 | if ( ! class_exists( 'WP_REST_Controller' ) ) { |
||
138 | include_once( dirname( __FILE__ ) . '/vendor/class-wp-rest-controller.php' ); |
||
139 | } |
||
140 | |||
141 | // Abstract controllers. |
||
142 | include_once( dirname( __FILE__ ) . '/abstracts/abstract-wc-rest-controller.php' ); |
||
143 | include_once( dirname( __FILE__ ) . '/abstracts/abstract-wc-rest-posts-controller.php' ); |
||
144 | include_once( dirname( __FILE__ ) . '/abstracts/abstract-wc-rest-shipping-zones-controller.php' ); |
||
145 | include_once( dirname( __FILE__ ) . '/abstracts/abstract-wc-rest-terms-controller.php' ); |
||
146 | include_once( dirname( __FILE__ ) . '/abstracts/abstract-wc-settings-api.php' ); |
||
147 | |||
148 | |||
149 | // REST API controllers. |
||
150 | include_once( dirname( __FILE__ ) . '/api/class-wc-rest-coupons-controller.php' ); |
||
151 | include_once( dirname( __FILE__ ) . '/api/class-wc-rest-customer-downloads-controller.php' ); |
||
152 | include_once( dirname( __FILE__ ) . '/api/class-wc-rest-customers-controller.php' ); |
||
153 | include_once( dirname( __FILE__ ) . '/api/class-wc-rest-order-notes-controller.php' ); |
||
154 | include_once( dirname( __FILE__ ) . '/api/class-wc-rest-order-refunds-controller.php' ); |
||
155 | include_once( dirname( __FILE__ ) . '/api/class-wc-rest-orders-controller.php' ); |
||
156 | include_once( dirname( __FILE__ ) . '/api/class-wc-rest-product-attribute-terms-controller.php' ); |
||
157 | include_once( dirname( __FILE__ ) . '/api/class-wc-rest-product-attributes-controller.php' ); |
||
158 | include_once( dirname( __FILE__ ) . '/api/class-wc-rest-product-categories-controller.php' ); |
||
159 | include_once( dirname( __FILE__ ) . '/api/class-wc-rest-product-reviews-controller.php' ); |
||
160 | include_once( dirname( __FILE__ ) . '/api/class-wc-rest-product-shipping-classes-controller.php' ); |
||
161 | include_once( dirname( __FILE__ ) . '/api/class-wc-rest-product-tags-controller.php' ); |
||
162 | include_once( dirname( __FILE__ ) . '/api/class-wc-rest-products-controller.php' ); |
||
163 | include_once( dirname( __FILE__ ) . '/api/class-wc-rest-report-sales-controller.php' ); |
||
164 | include_once( dirname( __FILE__ ) . '/api/class-wc-rest-report-top-sellers-controller.php' ); |
||
165 | include_once( dirname( __FILE__ ) . '/api/class-wc-rest-reports-controller.php' ); |
||
166 | include_once( dirname( __FILE__ ) . '/api/class-wc-rest-settings-controller.php' ); |
||
167 | include_once( dirname( __FILE__ ) . '/api/class-wc-rest-settings-options-controller.php' ); |
||
168 | include_once( dirname( __FILE__ ) . '/api/class-wc-rest-shipping-zones-controller.php' ); |
||
169 | include_once( dirname( __FILE__ ) . '/api/class-wc-rest-shipping-zone-locations-controller.php' ); |
||
170 | include_once( dirname( __FILE__ ) . '/api/class-wc-rest-shipping-zone-methods-controller.php' ); |
||
171 | include_once( dirname( __FILE__ ) . '/api/class-wc-rest-tax-classes-controller.php' ); |
||
172 | include_once( dirname( __FILE__ ) . '/api/class-wc-rest-taxes-controller.php' ); |
||
173 | include_once( dirname( __FILE__ ) . '/api/class-wc-rest-webhook-deliveries.php' ); |
||
174 | include_once( dirname( __FILE__ ) . '/api/class-wc-rest-webhooks-controller.php' ); |
||
175 | include_once( dirname( __FILE__ ) . '/api/class-wc-rest-system-status-controller.php' ); |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * Register REST API routes. |
||
180 | * @since 2.6.0 |
||
181 | */ |
||
182 | public function register_rest_routes() { |
||
183 | // Register settings to the REST API. |
||
184 | $this->register_wp_admin_settings(); |
||
185 | |||
186 | $controllers = array( |
||
187 | 'WC_REST_Coupons_Controller', |
||
188 | 'WC_REST_Customer_Downloads_Controller', |
||
189 | 'WC_REST_Customers_Controller', |
||
190 | 'WC_REST_Order_Notes_Controller', |
||
191 | 'WC_REST_Order_Refunds_Controller', |
||
192 | 'WC_REST_Orders_Controller', |
||
193 | 'WC_REST_Product_Attribute_Terms_Controller', |
||
194 | 'WC_REST_Product_Attributes_Controller', |
||
195 | 'WC_REST_Product_Categories_Controller', |
||
196 | 'WC_REST_Product_Reviews_Controller', |
||
197 | 'WC_REST_Product_Shipping_Classes_Controller', |
||
198 | 'WC_REST_Product_Tags_Controller', |
||
199 | 'WC_REST_Products_Controller', |
||
200 | 'WC_REST_Report_Sales_Controller', |
||
201 | 'WC_REST_Report_Top_Sellers_Controller', |
||
202 | 'WC_REST_Reports_Controller', |
||
203 | 'WC_REST_Settings_Controller', |
||
204 | 'WC_REST_Settings_Options_Controller', |
||
205 | 'WC_REST_Shipping_Zones_Controller', |
||
206 | 'WC_REST_Shipping_Zone_Locations_Controller', |
||
207 | 'WC_REST_Shipping_Zone_Methods_Controller', |
||
208 | 'WC_REST_Tax_Classes_Controller', |
||
209 | 'WC_REST_Taxes_Controller', |
||
210 | 'WC_REST_Webhook_Deliveries_Controller', |
||
211 | 'WC_REST_Webhooks_Controller', |
||
212 | 'WC_REST_System_Status_Controller', |
||
213 | ); |
||
214 | |||
215 | foreach ( $controllers as $controller ) { |
||
216 | $this->$controller = new $controller(); |
||
217 | $this->$controller->register_routes(); |
||
218 | } |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * Register WC settings from WP-API to the REST API. |
||
223 | * @since 2.7.0 |
||
224 | */ |
||
225 | public function register_wp_admin_settings() { |
||
231 | |||
232 | } |
||
233 |