Completed
Push — master ( 644cf7...b0963d )
by Mike
19s
created

WC_API::rest_api_includes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 49
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 9
Bugs 0 Features 1
Metric Value
cc 2
eloc 37
nc 2
nop 0
dl 0
loc 49
rs 9.2258
c 9
b 0
f 1
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( dirname( __FILE__ ) . '/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
		// 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() {
226
		$pages = WC_Admin_Settings::get_settings_pages();
227
		foreach ( $pages as $page ) {
228
			new WC_Register_WP_Admin_Settings( $page );
229
		}
230
	}
231
232
}
233