WC_Legacy_API

The WooCommerce WC Legacy API class.

Defined (1)

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

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