WC_API

The WooCommerce WC API class.

Defined (1)

The class is defined in the following location(s).

/includes/class-wc-api.php  
  1. class WC_API extends WC_Legacy_API { 
  2.  
  3. /** 
  4. * Setup class. 
  5. * @since 2.0 
  6. */ 
  7. public function __construct() { 
  8. parent::__construct(); 
  9.  
  10. // Add query vars. 
  11. add_filter( 'query_vars', array( $this, 'add_query_vars' ), 0 ); 
  12.  
  13. // Register API endpoints. 
  14. add_action( 'init', array( $this, 'add_endpoint' ), 0 ); 
  15.  
  16. // Handle wc-api endpoint requests. 
  17. add_action( 'parse_request', array( $this, 'handle_api_requests' ), 0 ); 
  18.  
  19. // Ensure payment gateways are initialized in time for API requests. 
  20. add_action( 'woocommerce_api_request', array( 'WC_Payment_Gateways', 'instance' ), 0 ); 
  21.  
  22. // WP REST API. 
  23. $this->rest_api_init(); 
  24.  
  25. /** 
  26. * Add new query vars. 
  27. * @since 2.0 
  28. * @param array $vars 
  29. * @return string[] 
  30. */ 
  31. public function add_query_vars( $vars ) { 
  32. $vars = parent::add_query_vars( $vars ); 
  33. $vars[] = 'wc-api'; 
  34. return $vars; 
  35.  
  36. /** 
  37. * WC API for payment gateway IPNs, etc. 
  38. * @since 2.0 
  39. */ 
  40. public static function add_endpoint() { 
  41. parent::add_endpoint(); 
  42. add_rewrite_endpoint( 'wc-api', EP_ALL ); 
  43.  
  44. /** 
  45. * API request - Trigger any API requests. 
  46. * @since 2.0 
  47. * @version 2.4 
  48. */ 
  49. public function handle_api_requests() { 
  50. global $wp; 
  51.  
  52. if ( ! empty( $_GET['wc-api'] ) ) { 
  53. $wp->query_vars['wc-api'] = $_GET['wc-api']; 
  54.  
  55. // wc-api endpoint requests. 
  56. if ( ! empty( $wp->query_vars['wc-api'] ) ) { 
  57.  
  58. // Buffer, we won't want any output here. 
  59. ob_start(); 
  60.  
  61. // No cache headers. 
  62. nocache_headers(); 
  63.  
  64. // Clean the API request. 
  65. $api_request = strtolower( wc_clean( $wp->query_vars['wc-api'] ) ); 
  66.  
  67. // Trigger generic action before request hook. 
  68. do_action( 'woocommerce_api_request', $api_request ); 
  69.  
  70. // Is there actually something hooked into this API request? If not trigger 400 - Bad request. 
  71. status_header( has_action( 'woocommerce_api_' . $api_request ) ? 200 : 400 ); 
  72.  
  73. // Trigger an action which plugins can hook into to fulfill the request. 
  74. do_action( 'woocommerce_api_' . $api_request ); 
  75.  
  76. // Done, clear buffer and exit. 
  77. ob_end_clean(); 
  78. die( '-1' ); 
  79.  
  80. /** 
  81. * Init WP REST API. 
  82. * @since 2.6.0 
  83. */ 
  84. private function rest_api_init() { 
  85. // REST API was included starting WordPress 4.4. 
  86. if ( ! class_exists( 'WP_REST_Server' ) ) { 
  87. return; 
  88.  
  89. $this->rest_api_includes(); 
  90.  
  91. // Init REST API routes. 
  92. add_action( 'rest_api_init', array( $this, 'register_rest_routes' ), 10 ); 
  93.  
  94. /** 
  95. * Include REST API classes. 
  96. * @since 2.6.0 
  97. */ 
  98. private function rest_api_includes() { 
  99. // Exception handler. 
  100. include_once( dirname( __FILE__ ) . '/api/class-wc-rest-exception.php' ); 
  101.  
  102. // Authentication. 
  103. include_once( dirname( __FILE__ ) . '/api/class-wc-rest-authentication.php' ); 
  104.  
  105. // WP-API classes and functions. 
  106. include_once( dirname( __FILE__ ) . '/vendor/wp-rest-functions.php' ); 
  107. if ( ! class_exists( 'WP_REST_Controller' ) ) { 
  108. include_once( dirname( __FILE__ ) . '/vendor/abstract-wp-rest-controller.php' ); 
  109.  
  110. // Abstract controllers. 
  111. include_once( dirname( __FILE__ ) . '/abstracts/abstract-wc-rest-controller.php' ); 
  112. include_once( dirname( __FILE__ ) . '/abstracts/abstract-wc-rest-posts-controller.php' ); 
  113. include_once( dirname( __FILE__ ) . '/abstracts/abstract-wc-rest-crud-controller.php' ); 
  114. include_once( dirname( __FILE__ ) . '/abstracts/abstract-wc-rest-terms-controller.php' ); 
  115. include_once( dirname( __FILE__ ) . '/abstracts/abstract-wc-rest-shipping-zones-controller.php' ); 
  116. include_once( dirname( __FILE__ ) . '/abstracts/abstract-wc-settings-api.php' ); 
  117.  
  118. // REST API v1 controllers. 
  119. include_once( dirname( __FILE__ ) . '/api/v1/class-wc-rest-coupons-controller.php' ); 
  120. include_once( dirname( __FILE__ ) . '/api/v1/class-wc-rest-customer-downloads-controller.php' ); 
  121. include_once( dirname( __FILE__ ) . '/api/v1/class-wc-rest-customers-controller.php' ); 
  122. include_once( dirname( __FILE__ ) . '/api/v1/class-wc-rest-orders-controller.php' ); 
  123. include_once( dirname( __FILE__ ) . '/api/v1/class-wc-rest-order-notes-controller.php' ); 
  124. include_once( dirname( __FILE__ ) . '/api/v1/class-wc-rest-order-refunds-controller.php' ); 
  125. include_once( dirname( __FILE__ ) . '/api/v1/class-wc-rest-product-attribute-terms-controller.php' ); 
  126. include_once( dirname( __FILE__ ) . '/api/v1/class-wc-rest-product-attributes-controller.php' ); 
  127. include_once( dirname( __FILE__ ) . '/api/v1/class-wc-rest-product-categories-controller.php' ); 
  128. include_once( dirname( __FILE__ ) . '/api/v1/class-wc-rest-product-reviews-controller.php' ); 
  129. include_once( dirname( __FILE__ ) . '/api/v1/class-wc-rest-product-shipping-classes-controller.php' ); 
  130. include_once( dirname( __FILE__ ) . '/api/v1/class-wc-rest-product-tags-controller.php' ); 
  131. include_once( dirname( __FILE__ ) . '/api/v1/class-wc-rest-products-controller.php' ); 
  132. include_once( dirname( __FILE__ ) . '/api/v1/class-wc-rest-report-sales-controller.php' ); 
  133. include_once( dirname( __FILE__ ) . '/api/v1/class-wc-rest-report-top-sellers-controller.php' ); 
  134. include_once( dirname( __FILE__ ) . '/api/v1/class-wc-rest-reports-controller.php' ); 
  135. include_once( dirname( __FILE__ ) . '/api/v1/class-wc-rest-tax-classes-controller.php' ); 
  136. include_once( dirname( __FILE__ ) . '/api/v1/class-wc-rest-taxes-controller.php' ); 
  137. include_once( dirname( __FILE__ ) . '/api/v1/class-wc-rest-webhook-deliveries.php' ); 
  138. include_once( dirname( __FILE__ ) . '/api/v1/class-wc-rest-webhooks-controller.php' ); 
  139.  
  140. // Legacy v2 code. 
  141. include_once( dirname( __FILE__ ) . '/api/legacy/class-wc-rest-legacy-coupons-controller.php' ); 
  142. include_once( dirname( __FILE__ ) . '/api/legacy/class-wc-rest-legacy-orders-controller.php' ); 
  143. include_once( dirname( __FILE__ ) . '/api/legacy/class-wc-rest-legacy-products-controller.php' ); 
  144.  
  145. // REST API v2 controllers. 
  146. include_once( dirname( __FILE__ ) . '/api/class-wc-rest-coupons-controller.php' ); 
  147. include_once( dirname( __FILE__ ) . '/api/class-wc-rest-customer-downloads-controller.php' ); 
  148. include_once( dirname( __FILE__ ) . '/api/class-wc-rest-customers-controller.php' ); 
  149. include_once( dirname( __FILE__ ) . '/api/class-wc-rest-orders-controller.php' ); 
  150. include_once( dirname( __FILE__ ) . '/api/class-wc-rest-order-notes-controller.php' ); 
  151. include_once( dirname( __FILE__ ) . '/api/class-wc-rest-order-refunds-controller.php' ); 
  152. include_once( dirname( __FILE__ ) . '/api/class-wc-rest-product-attribute-terms-controller.php' ); 
  153. include_once( dirname( __FILE__ ) . '/api/class-wc-rest-product-attributes-controller.php' ); 
  154. include_once( dirname( __FILE__ ) . '/api/class-wc-rest-product-categories-controller.php' ); 
  155. include_once( dirname( __FILE__ ) . '/api/class-wc-rest-product-reviews-controller.php' ); 
  156. include_once( dirname( __FILE__ ) . '/api/class-wc-rest-product-shipping-classes-controller.php' ); 
  157. include_once( dirname( __FILE__ ) . '/api/class-wc-rest-product-tags-controller.php' ); 
  158. include_once( dirname( __FILE__ ) . '/api/class-wc-rest-products-controller.php' ); 
  159. include_once( dirname( __FILE__ ) . '/api/class-wc-rest-product-variations-controller.php' ); 
  160. include_once( dirname( __FILE__ ) . '/api/class-wc-rest-report-sales-controller.php' ); 
  161. include_once( dirname( __FILE__ ) . '/api/class-wc-rest-report-top-sellers-controller.php' ); 
  162. include_once( dirname( __FILE__ ) . '/api/class-wc-rest-reports-controller.php' ); 
  163. include_once( dirname( __FILE__ ) . '/api/class-wc-rest-settings-controller.php' ); 
  164. include_once( dirname( __FILE__ ) . '/api/class-wc-rest-setting-options-controller.php' ); 
  165. include_once( dirname( __FILE__ ) . '/api/class-wc-rest-shipping-zones-controller.php' ); 
  166. include_once( dirname( __FILE__ ) . '/api/class-wc-rest-shipping-zone-locations-controller.php' ); 
  167. include_once( dirname( __FILE__ ) . '/api/class-wc-rest-shipping-zone-methods-controller.php' ); 
  168. include_once( dirname( __FILE__ ) . '/api/class-wc-rest-tax-classes-controller.php' ); 
  169. include_once( dirname( __FILE__ ) . '/api/class-wc-rest-taxes-controller.php' ); 
  170. include_once( dirname( __FILE__ ) . '/api/class-wc-rest-webhook-deliveries.php' ); 
  171. include_once( dirname( __FILE__ ) . '/api/class-wc-rest-webhooks-controller.php' ); 
  172. include_once( dirname( __FILE__ ) . '/api/class-wc-rest-system-status-controller.php' ); 
  173. include_once( dirname( __FILE__ ) . '/api/class-wc-rest-system-status-tools-controller.php' ); 
  174. include_once( dirname( __FILE__ ) . '/api/class-wc-rest-shipping-methods-controller.php' ); 
  175. include_once( dirname( __FILE__ ) . '/api/class-wc-rest-payment-gateways-controller.php' ); 
  176.  
  177. /** 
  178. * Register REST API routes. 
  179. * @since 2.6.0 
  180. */ 
  181. public function register_rest_routes() { 
  182. // Register settings to the REST API. 
  183. $this->register_wp_admin_settings(); 
  184.  
  185. $controllers = array( 
  186. // v1 controllers. 
  187. 'WC_REST_Coupons_V1_Controller',  
  188. 'WC_REST_Customer_Downloads_V1_Controller',  
  189. 'WC_REST_Customers_V1_Controller',  
  190. 'WC_REST_Order_Notes_V1_Controller',  
  191. 'WC_REST_Order_Refunds_V1_Controller',  
  192. 'WC_REST_Orders_V1_Controller',  
  193. 'WC_REST_Product_Attribute_Terms_V1_Controller',  
  194. 'WC_REST_Product_Attributes_V1_Controller',  
  195. 'WC_REST_Product_Categories_V1_Controller',  
  196. 'WC_REST_Product_Reviews_V1_Controller',  
  197. 'WC_REST_Product_Shipping_Classes_V1_Controller',  
  198. 'WC_REST_Product_Tags_V1_Controller',  
  199. 'WC_REST_Products_V1_Controller',  
  200. 'WC_REST_Report_Sales_V1_Controller',  
  201. 'WC_REST_Report_Top_Sellers_V1_Controller',  
  202. 'WC_REST_Reports_V1_Controller',  
  203. 'WC_REST_Tax_Classes_V1_Controller',  
  204. 'WC_REST_Taxes_V1_Controller',  
  205. 'WC_REST_Webhook_Deliveries_V1_Controller',  
  206. 'WC_REST_Webhooks_V1_Controller',  
  207.  
  208. // v2 controllers. 
  209. 'WC_REST_Coupons_Controller',  
  210. 'WC_REST_Customer_Downloads_Controller',  
  211. 'WC_REST_Customers_Controller',  
  212. 'WC_REST_Order_Notes_Controller',  
  213. 'WC_REST_Order_Refunds_Controller',  
  214. 'WC_REST_Orders_Controller',  
  215. 'WC_REST_Product_Attribute_Terms_Controller',  
  216. 'WC_REST_Product_Attributes_Controller',  
  217. 'WC_REST_Product_Categories_Controller',  
  218. 'WC_REST_Product_Reviews_Controller',  
  219. 'WC_REST_Product_Shipping_Classes_Controller',  
  220. 'WC_REST_Product_Tags_Controller',  
  221. 'WC_REST_Products_Controller',  
  222. 'WC_REST_Product_Variations_Controller',  
  223. 'WC_REST_Report_Sales_Controller',  
  224. 'WC_REST_Report_Top_Sellers_Controller',  
  225. 'WC_REST_Reports_Controller',  
  226. 'WC_REST_Settings_Controller',  
  227. 'WC_REST_Setting_Options_Controller',  
  228. 'WC_REST_Shipping_Zones_Controller',  
  229. 'WC_REST_Shipping_Zone_Locations_Controller',  
  230. 'WC_REST_Shipping_Zone_Methods_Controller',  
  231. 'WC_REST_Tax_Classes_Controller',  
  232. 'WC_REST_Taxes_Controller',  
  233. 'WC_REST_Webhook_Deliveries_Controller',  
  234. 'WC_REST_Webhooks_Controller',  
  235. 'WC_REST_System_Status_Controller',  
  236. 'WC_REST_System_Status_Tools_Controller',  
  237. 'WC_REST_Shipping_Methods_Controller',  
  238. 'WC_REST_Payment_Gateways_Controller',  
  239. ); 
  240.  
  241. foreach ( $controllers as $controller ) { 
  242. $this->$controller = new $controller(); 
  243. $this->$controller->register_routes(); 
  244.  
  245. /** 
  246. * Register WC settings from WP-API to the REST API. 
  247. * @since 3.0.0 
  248. */ 
  249. public function register_wp_admin_settings() { 
  250. $pages = WC_Admin_Settings::get_settings_pages(); 
  251. foreach ( $pages as $page ) { 
  252. new WC_Register_WP_Admin_Settings( $page, 'page' ); 
  253.  
  254. $emails = WC_Emails::instance(); 
  255. foreach ( $emails->get_emails() as $email ) { 
  256. new WC_Register_WP_Admin_Settings( $email, 'email' ); 
  257.