class-wc-api.php 11 KB

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