class-wc-legacy-api.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. /**
  3. * WooCommerce Legacy API. Was deprecated with 2.6.0.
  4. *
  5. * @author WooThemes
  6. * @category API
  7. * @package WooCommerce/API
  8. * @since 2.6
  9. */
  10. if ( ! defined( 'ABSPATH' ) ) {
  11. exit;
  12. }
  13. /**
  14. * Legacy API.
  15. */
  16. class WC_Legacy_API {
  17. /**
  18. * This is the major version for the REST API and takes
  19. * first-order position in endpoint URLs.
  20. *
  21. * @deprecated 2.6.0
  22. * @var string
  23. */
  24. const VERSION = '3.1.0';
  25. /**
  26. * The REST API server.
  27. *
  28. * @deprecated 2.6.0
  29. * @var WC_API_Server
  30. */
  31. public $server;
  32. /**
  33. * REST API authentication class instance.
  34. *
  35. * @deprecated 2.6.0
  36. * @var WC_API_Authentication
  37. */
  38. public $authentication;
  39. /**
  40. * Setup class.
  41. *
  42. * @since 2.0
  43. */
  44. public function __construct() {
  45. add_action( 'parse_request', array( $this, 'handle_rest_api_requests' ), 0 );
  46. }
  47. /**
  48. * Add new query vars.
  49. *
  50. * @since 2.0
  51. * @param array $vars Vars.
  52. * @return string[]
  53. */
  54. public function add_query_vars( $vars ) {
  55. $vars[] = 'wc-api-version'; // Deprecated since 2.6.0.
  56. $vars[] = 'wc-api-route'; // Deprecated since 2.6.0.
  57. return $vars;
  58. }
  59. /**
  60. * Add new endpoints.
  61. *
  62. * @since 2.0
  63. */
  64. public static function add_endpoint() {
  65. // REST API, deprecated since 2.6.0.
  66. add_rewrite_rule( '^wc-api/v([1-3]{1})/?$', 'index.php?wc-api-version=$matches[1]&wc-api-route=/', 'top' );
  67. add_rewrite_rule( '^wc-api/v([1-3]{1})(.*)?', 'index.php?wc-api-version=$matches[1]&wc-api-route=$matches[2]', 'top' );
  68. }
  69. /**
  70. * Handle REST API requests.
  71. *
  72. * @since 2.2
  73. * @deprecated 2.6.0
  74. */
  75. public function handle_rest_api_requests() {
  76. global $wp;
  77. if ( ! empty( $_GET['wc-api-version'] ) ) {
  78. $wp->query_vars['wc-api-version'] = $_GET['wc-api-version'];
  79. }
  80. if ( ! empty( $_GET['wc-api-route'] ) ) {
  81. $wp->query_vars['wc-api-route'] = $_GET['wc-api-route'];
  82. }
  83. // REST API request.
  84. if ( ! empty( $wp->query_vars['wc-api-version'] ) && ! empty( $wp->query_vars['wc-api-route'] ) ) {
  85. wc_maybe_define_constant( 'WC_API_REQUEST', true );
  86. wc_maybe_define_constant( 'WC_API_REQUEST_VERSION', absint( $wp->query_vars['wc-api-version'] ) );
  87. // Legacy v1 API request.
  88. if ( 1 === WC_API_REQUEST_VERSION ) {
  89. $this->handle_v1_rest_api_request();
  90. } elseif ( 2 === WC_API_REQUEST_VERSION ) {
  91. $this->handle_v2_rest_api_request();
  92. } else {
  93. $this->includes();
  94. $this->server = new WC_API_Server( $wp->query_vars['wc-api-route'] );
  95. // load API resource classes.
  96. $this->register_resources( $this->server );
  97. // Fire off the request.
  98. $this->server->serve_request();
  99. }
  100. exit;
  101. }
  102. }
  103. /**
  104. * Include required files for REST API request.
  105. *
  106. * @since 2.1
  107. * @deprecated 2.6.0
  108. */
  109. public function includes() {
  110. // API server / response handlers.
  111. include_once( dirname( __FILE__ ) . '/../api/legacy/v3/class-wc-api-exception.php' );
  112. include_once( dirname( __FILE__ ) . '/../api/legacy/v3/class-wc-api-server.php' );
  113. include_once( dirname( __FILE__ ) . '/../api/legacy/v3/interface-wc-api-handler.php' );
  114. include_once( dirname( __FILE__ ) . '/../api/legacy/v3/class-wc-api-json-handler.php' );
  115. // Authentication.
  116. include_once( dirname( __FILE__ ) . '/../api/legacy/v3/class-wc-api-authentication.php' );
  117. $this->authentication = new WC_API_Authentication();
  118. include_once( dirname( __FILE__ ) . '/../api/legacy/v3/class-wc-api-resource.php' );
  119. include_once( dirname( __FILE__ ) . '/../api/legacy/v3/class-wc-api-coupons.php' );
  120. include_once( dirname( __FILE__ ) . '/../api/legacy/v3/class-wc-api-customers.php' );
  121. include_once( dirname( __FILE__ ) . '/../api/legacy/v3/class-wc-api-orders.php' );
  122. include_once( dirname( __FILE__ ) . '/../api/legacy/v3/class-wc-api-products.php' );
  123. include_once( dirname( __FILE__ ) . '/../api/legacy/v3/class-wc-api-reports.php' );
  124. include_once( dirname( __FILE__ ) . '/../api/legacy/v3/class-wc-api-taxes.php' );
  125. include_once( dirname( __FILE__ ) . '/../api/legacy/v3/class-wc-api-webhooks.php' );
  126. // Allow plugins to load other response handlers or resource classes.
  127. do_action( 'woocommerce_api_loaded' );
  128. }
  129. /**
  130. * Register available API resources.
  131. *
  132. * @since 2.1
  133. * @deprecated 2.6.0
  134. * @param WC_API_Server $server the REST server.
  135. */
  136. public function register_resources( $server ) {
  137. $api_classes = apply_filters( 'woocommerce_api_classes',
  138. array(
  139. 'WC_API_Coupons',
  140. 'WC_API_Customers',
  141. 'WC_API_Orders',
  142. 'WC_API_Products',
  143. 'WC_API_Reports',
  144. 'WC_API_Taxes',
  145. 'WC_API_Webhooks',
  146. )
  147. );
  148. foreach ( $api_classes as $api_class ) {
  149. $this->$api_class = new $api_class( $server );
  150. }
  151. }
  152. /**
  153. * Handle legacy v1 REST API requests.
  154. *
  155. * @since 2.2
  156. * @deprecated 2.6.0
  157. */
  158. private function handle_v1_rest_api_request() {
  159. // Include legacy required files for v1 REST API request.
  160. include_once( dirname( __FILE__ ) . '/../api/legacy/v1/class-wc-api-server.php' );
  161. include_once( dirname( __FILE__ ) . '/../api/legacy/v1/interface-wc-api-handler.php' );
  162. include_once( dirname( __FILE__ ) . '/../api/legacy/v1/class-wc-api-json-handler.php' );
  163. include_once( dirname( __FILE__ ) . '/../api/legacy/v1/class-wc-api-xml-handler.php' );
  164. include_once( dirname( __FILE__ ) . '/../api/legacy/v1/class-wc-api-authentication.php' );
  165. $this->authentication = new WC_API_Authentication();
  166. include_once( dirname( __FILE__ ) . '/../api/legacy/v1/class-wc-api-resource.php' );
  167. include_once( dirname( __FILE__ ) . '/../api/legacy/v1/class-wc-api-coupons.php' );
  168. include_once( dirname( __FILE__ ) . '/../api/legacy/v1/class-wc-api-customers.php' );
  169. include_once( dirname( __FILE__ ) . '/../api/legacy/v1/class-wc-api-orders.php' );
  170. include_once( dirname( __FILE__ ) . '/../api/legacy/v1/class-wc-api-products.php' );
  171. include_once( dirname( __FILE__ ) . '/../api/legacy/v1/class-wc-api-reports.php' );
  172. // Allow plugins to load other response handlers or resource classes.
  173. do_action( 'woocommerce_api_loaded' );
  174. $this->server = new WC_API_Server( $GLOBALS['wp']->query_vars['wc-api-route'] );
  175. // Register available resources for legacy v1 REST API request.
  176. $api_classes = apply_filters( 'woocommerce_api_classes',
  177. array(
  178. 'WC_API_Customers',
  179. 'WC_API_Orders',
  180. 'WC_API_Products',
  181. 'WC_API_Coupons',
  182. 'WC_API_Reports',
  183. )
  184. );
  185. foreach ( $api_classes as $api_class ) {
  186. $this->$api_class = new $api_class( $this->server );
  187. }
  188. // Fire off the request.
  189. $this->server->serve_request();
  190. }
  191. /**
  192. * Handle legacy v2 REST API requests.
  193. *
  194. * @since 2.4
  195. * @deprecated 2.6.0
  196. */
  197. private function handle_v2_rest_api_request() {
  198. include_once( dirname( __FILE__ ) . '/../api/legacy/v2/class-wc-api-exception.php' );
  199. include_once( dirname( __FILE__ ) . '/../api/legacy/v2/class-wc-api-server.php' );
  200. include_once( dirname( __FILE__ ) . '/../api/legacy/v2/interface-wc-api-handler.php' );
  201. include_once( dirname( __FILE__ ) . '/../api/legacy/v2/class-wc-api-json-handler.php' );
  202. include_once( dirname( __FILE__ ) . '/../api/legacy/v2/class-wc-api-authentication.php' );
  203. $this->authentication = new WC_API_Authentication();
  204. include_once( dirname( __FILE__ ) . '/../api/legacy/v2/class-wc-api-resource.php' );
  205. include_once( dirname( __FILE__ ) . '/../api/legacy/v2/class-wc-api-coupons.php' );
  206. include_once( dirname( __FILE__ ) . '/../api/legacy/v2/class-wc-api-customers.php' );
  207. include_once( dirname( __FILE__ ) . '/../api/legacy/v2/class-wc-api-orders.php' );
  208. include_once( dirname( __FILE__ ) . '/../api/legacy/v2/class-wc-api-products.php' );
  209. include_once( dirname( __FILE__ ) . '/../api/legacy/v2/class-wc-api-reports.php' );
  210. include_once( dirname( __FILE__ ) . '/../api/legacy/v2/class-wc-api-webhooks.php' );
  211. // allow plugins to load other response handlers or resource classes.
  212. do_action( 'woocommerce_api_loaded' );
  213. $this->server = new WC_API_Server( $GLOBALS['wp']->query_vars['wc-api-route'] );
  214. // Register available resources for legacy v2 REST API request.
  215. $api_classes = apply_filters( 'woocommerce_api_classes',
  216. array(
  217. 'WC_API_Customers',
  218. 'WC_API_Orders',
  219. 'WC_API_Products',
  220. 'WC_API_Coupons',
  221. 'WC_API_Reports',
  222. 'WC_API_Webhooks',
  223. )
  224. );
  225. foreach ( $api_classes as $api_class ) {
  226. $this->$api_class = new $api_class( $this->server );
  227. }
  228. // Fire off the request.
  229. $this->server->serve_request();
  230. }
  231. }