PageRenderTime 29ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/class-wc-api.php

https://gitlab.com/CarlCDavid/woocommerce
PHP | 268 lines | 127 code | 57 blank | 84 comment | 11 complexity | 729232a50cb30dccc171be0cbd826cfd MD5 | raw file
  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. if ( ! defined( 'ABSPATH' ) ) {
  13. exit;
  14. }
  15. if ( ! class_exists( 'WC_API' ) ) :
  16. class WC_API {
  17. /** This is the major version for the REST API and takes
  18. * first-order position in endpoint URLs
  19. */
  20. const VERSION = '2.1.0';
  21. /** @var WC_API_Server the REST API server */
  22. public $server;
  23. /** @var WC_API_Authentication REST API authentication class instance */
  24. public $authentication;
  25. /**
  26. * Setup class
  27. *
  28. * @since 2.0
  29. * @return WC_API
  30. */
  31. public function __construct() {
  32. // add query vars
  33. add_filter( 'query_vars', array( $this, 'add_query_vars' ), 0 );
  34. // register API endpoints
  35. add_action( 'init', array( $this, 'add_endpoint' ), 0 );
  36. // handle REST API requests
  37. add_action( 'parse_request', array( $this, 'handle_rest_api_requests' ), 0 );
  38. // handle wc-api endpoint requests
  39. add_action( 'parse_request', array( $this, 'handle_api_requests' ), 0 );
  40. }
  41. /**
  42. * Add new query vars.
  43. *
  44. * @since 2.0
  45. * @param $vars
  46. * @return string[]
  47. */
  48. public function add_query_vars( $vars ) {
  49. $vars[] = 'wc-api';
  50. $vars[] = 'wc-api-version';
  51. $vars[] = 'wc-api-route';
  52. return $vars;
  53. }
  54. /**
  55. * Add new endpoints.
  56. *
  57. * @since 2.0
  58. */
  59. public static function add_endpoint() {
  60. // REST API
  61. add_rewrite_rule( '^wc-api/v([1-2]{1})/?$', 'index.php?wc-api-version=$matches[1]&wc-api-route=/', 'top' );
  62. add_rewrite_rule( '^wc-api/v([1-2]{1})(.*)?', 'index.php?wc-api-version=$matches[1]&wc-api-route=$matches[2]', 'top' );
  63. // WC API for payment gateway IPNs, etc
  64. add_rewrite_endpoint( 'wc-api', EP_ALL );
  65. }
  66. /**
  67. * Handle REST API requests
  68. *
  69. * @since 2.2
  70. */
  71. public function handle_rest_api_requests() {
  72. global $wp;
  73. if ( ! empty( $_GET['wc-api-version'] ) ) {
  74. $wp->query_vars['wc-api-version'] = $_GET['wc-api-version'];
  75. }
  76. if ( ! empty( $_GET['wc-api-route'] ) ) {
  77. $wp->query_vars['wc-api-route'] = $_GET['wc-api-route'];
  78. }
  79. // REST API request
  80. if ( ! empty( $wp->query_vars['wc-api-version'] ) && ! empty( $wp->query_vars['wc-api-route'] ) ) {
  81. define( 'WC_API_REQUEST', true );
  82. define( 'WC_API_REQUEST_VERSION', absint( $wp->query_vars['wc-api-version'] ) );
  83. // legacy v1 API request
  84. if ( 1 === WC_API_REQUEST_VERSION ) {
  85. $this->handle_v1_rest_api_request();
  86. } else {
  87. $this->includes();
  88. $this->server = new WC_API_Server( $wp->query_vars['wc-api-route'] );
  89. // load API resource classes
  90. $this->register_resources( $this->server );
  91. // Fire off the request
  92. $this->server->serve_request();
  93. }
  94. exit;
  95. }
  96. }
  97. /**
  98. * Include required files for REST API request
  99. *
  100. * @since 2.1
  101. */
  102. public function includes() {
  103. // API server / response handlers
  104. include_once( 'api/class-wc-api-exception.php' );
  105. include_once( 'api/class-wc-api-server.php' );
  106. include_once( 'api/interface-wc-api-handler.php' );
  107. include_once( 'api/class-wc-api-json-handler.php' );
  108. // authentication
  109. include_once( 'api/class-wc-api-authentication.php' );
  110. $this->authentication = new WC_API_Authentication();
  111. include_once( 'api/class-wc-api-resource.php' );
  112. include_once( 'api/class-wc-api-orders.php' );
  113. include_once( 'api/class-wc-api-products.php' );
  114. include_once( 'api/class-wc-api-coupons.php' );
  115. include_once( 'api/class-wc-api-customers.php' );
  116. include_once( 'api/class-wc-api-reports.php' );
  117. include_once( 'api/class-wc-api-webhooks.php' );
  118. // allow plugins to load other response handlers or resource classes
  119. do_action( 'woocommerce_api_loaded' );
  120. }
  121. /**
  122. * Register available API resources
  123. *
  124. * @since 2.1
  125. * @param WC_API_Server $server the REST server
  126. */
  127. public function register_resources( $server ) {
  128. $api_classes = apply_filters( 'woocommerce_api_classes',
  129. array(
  130. 'WC_API_Customers',
  131. 'WC_API_Orders',
  132. 'WC_API_Products',
  133. 'WC_API_Coupons',
  134. 'WC_API_Reports',
  135. 'WC_API_Webhooks',
  136. )
  137. );
  138. foreach ( $api_classes as $api_class ) {
  139. $this->$api_class = new $api_class( $server );
  140. }
  141. }
  142. /**
  143. * Handle legacy v1 REST API requests. Note this and the associated
  144. * classes in the v1 folder should be removed when the API version is bumped
  145. * to v3
  146. *
  147. * @since 2.2
  148. */
  149. private function handle_v1_rest_api_request() {
  150. // include legacy required files for v1 REST API request
  151. include_once( 'api/v1/class-wc-api-server.php' );
  152. include_once( 'api/v1/interface-wc-api-handler.php' );
  153. include_once( 'api/v1/class-wc-api-json-handler.php' );
  154. include_once( 'api/v1/class-wc-api-xml-handler.php' );
  155. include_once( 'api/v1/class-wc-api-authentication.php' );
  156. $this->authentication = new WC_API_Authentication();
  157. include_once( 'api/v1/class-wc-api-resource.php' );
  158. include_once( 'api/v1/class-wc-api-orders.php' );
  159. include_once( 'api/v1/class-wc-api-products.php' );
  160. include_once( 'api/v1/class-wc-api-coupons.php' );
  161. include_once( 'api/v1/class-wc-api-customers.php' );
  162. include_once( 'api/v1/class-wc-api-reports.php' );
  163. // allow plugins to load other response handlers or resource classes
  164. do_action( 'woocommerce_api_loaded' );
  165. $this->server = new WC_API_Server( $GLOBALS['wp']->query_vars['wc-api-route'] );
  166. // Register available resources for legacy v1 REST API request
  167. $api_classes = apply_filters( 'woocommerce_api_classes',
  168. array(
  169. 'WC_API_Customers',
  170. 'WC_API_Orders',
  171. 'WC_API_Products',
  172. 'WC_API_Coupons',
  173. 'WC_API_Reports',
  174. )
  175. );
  176. foreach ( $api_classes as $api_class ) {
  177. $this->$api_class = new $api_class( $this->server );
  178. }
  179. // Fire off the request
  180. $this->server->serve_request();
  181. }
  182. /**
  183. * API request - Trigger any API requests
  184. *
  185. * @since 2.0
  186. */
  187. public function handle_api_requests() {
  188. global $wp;
  189. if ( ! empty( $_GET['wc-api'] ) ) {
  190. $wp->query_vars['wc-api'] = $_GET['wc-api'];
  191. }
  192. // wc-api endpoint requests
  193. if ( ! empty( $wp->query_vars['wc-api'] ) ) {
  194. // Buffer, we won't want any output here
  195. ob_start();
  196. // Get API trigger
  197. $api = strtolower( esc_attr( $wp->query_vars['wc-api'] ) );
  198. // Load class if exists
  199. if ( class_exists( $api ) ) {
  200. new $api();
  201. }
  202. // Trigger actions
  203. do_action( 'woocommerce_api_' . $api );
  204. // Done, clear buffer and exit
  205. ob_end_clean();
  206. die('1');
  207. }
  208. }
  209. }
  210. endif;
  211. return new WC_API();