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. global $wp_version; 
  86.  
  87. // REST API was included starting WordPress 4.4. 
  88. if ( version_compare( $wp_version, 4.4, '<' ) ) { 
  89. return; 
  90.  
  91. $this->rest_api_includes(); 
  92.  
  93. // Init REST API routes. 
  94. add_action( 'rest_api_init', array( $this, 'register_rest_routes' ) ); 
  95.  
  96. /** 
  97. * Include REST API classes. 
  98. * @since 2.6.0 
  99. */ 
  100. private function rest_api_includes() { 
  101. // Exception handler. 
  102. include_once( 'api/class-wc-rest-exception.php' ); 
  103.  
  104. // Authentication. 
  105. include_once( 'api/class-wc-rest-authentication.php' ); 
  106.  
  107. // WP-API classes and functions. 
  108. include_once( 'vendor/wp-rest-functions.php' ); 
  109. if ( ! class_exists( 'WP_REST_Controller' ) ) { 
  110. include_once( 'vendor/class-wp-rest-controller.php' ); 
  111.  
  112. // Abstract controllers. 
  113. include_once( 'abstracts/abstract-wc-rest-controller.php' ); 
  114. include_once( 'abstracts/abstract-wc-rest-posts-controller.php' ); 
  115. include_once( 'abstracts/abstract-wc-rest-terms-controller.php' ); 
  116.  
  117. // REST API controllers. 
  118. include_once( 'api/class-wc-rest-coupons-controller.php' ); 
  119. include_once( 'api/class-wc-rest-customer-downloads-controller.php' ); 
  120. include_once( 'api/class-wc-rest-customers-controller.php' ); 
  121. include_once( 'api/class-wc-rest-order-notes-controller.php' ); 
  122. include_once( 'api/class-wc-rest-order-refunds-controller.php' ); 
  123. include_once( 'api/class-wc-rest-orders-controller.php' ); 
  124. include_once( 'api/class-wc-rest-product-attribute-terms-controller.php' ); 
  125. include_once( 'api/class-wc-rest-product-attributes-controller.php' ); 
  126. include_once( 'api/class-wc-rest-product-categories-controller.php' ); 
  127. include_once( 'api/class-wc-rest-product-reviews-controller.php' ); 
  128. include_once( 'api/class-wc-rest-product-shipping-classes-controller.php' ); 
  129. include_once( 'api/class-wc-rest-product-tags-controller.php' ); 
  130. include_once( 'api/class-wc-rest-products-controller.php' ); 
  131. include_once( 'api/class-wc-rest-report-sales-controller.php' ); 
  132. include_once( 'api/class-wc-rest-report-top-sellers-controller.php' ); 
  133. include_once( 'api/class-wc-rest-reports-controller.php' ); 
  134. include_once( 'api/class-wc-rest-tax-classes-controller.php' ); 
  135. include_once( 'api/class-wc-rest-taxes-controller.php' ); 
  136. include_once( 'api/class-wc-rest-webhook-deliveries.php' ); 
  137. include_once( 'api/class-wc-rest-webhooks-controller.php' ); 
  138.  
  139. /** 
  140. * Register REST API routes. 
  141. * @since 2.6.0 
  142. */ 
  143. public function register_rest_routes() { 
  144. $controllers = array( 
  145. 'WC_REST_Coupons_Controller',  
  146. 'WC_REST_Customer_Downloads_Controller',  
  147. 'WC_REST_Customers_Controller',  
  148. 'WC_REST_Order_Notes_Controller',  
  149. 'WC_REST_Order_Refunds_Controller',  
  150. 'WC_REST_Orders_Controller',  
  151. 'WC_REST_Product_Attribute_Terms_Controller',  
  152. 'WC_REST_Product_Attributes_Controller',  
  153. 'WC_REST_Product_Categories_Controller',  
  154. 'WC_REST_Product_Reviews_Controller',  
  155. 'WC_REST_Product_Shipping_Classes_Controller',  
  156. 'WC_REST_Product_Tags_Controller',  
  157. 'WC_REST_Products_Controller',  
  158. 'WC_REST_Report_Sales_Controller',  
  159. 'WC_REST_Report_Top_Sellers_Controller',  
  160. 'WC_REST_Reports_Controller',  
  161. 'WC_REST_Tax_Classes_Controller',  
  162. 'WC_REST_Taxes_Controller',  
  163. 'WC_REST_Webhook_Deliveries_Controller',  
  164. 'WC_REST_Webhooks_Controller',  
  165. ); 
  166.  
  167. foreach ( $controllers as $controller ) { 
  168. $this->$controller = new $controller(); 
  169. $this->$controller->register_routes();