Brak opisu

class-wc-legacy-api.php 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php
  2. /**
  3. * WooCommerce Legacy API. Was deprecated with 2.6.0.
  4. *
  5. * @author WooThemes
  6. * @category API
  7. * @package WooCommerce\RestApi
  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. * Init the legacy API.
  41. */
  42. public function init() {
  43. add_action( 'parse_request', array( $this, 'handle_rest_api_requests' ), 0 );
  44. }
  45. /**
  46. * Add new query vars.
  47. *
  48. * @since 2.0
  49. * @param array $vars Vars.
  50. * @return string[]
  51. */
  52. public function add_query_vars( $vars ) {
  53. $vars[] = 'wc-api-version'; // Deprecated since 2.6.0.
  54. $vars[] = 'wc-api-route'; // Deprecated since 2.6.0.
  55. return $vars;
  56. }
  57. /**
  58. * Add new endpoints.
  59. *
  60. * @since 2.0
  61. */
  62. public static function add_endpoint() {
  63. // REST API, deprecated since 2.6.0.
  64. add_rewrite_rule( '^wc-api/v([1-3]{1})/?$', 'index.php?wc-api-version=$matches[1]&wc-api-route=/', 'top' );
  65. add_rewrite_rule( '^wc-api/v([1-3]{1})(.*)?', 'index.php?wc-api-version=$matches[1]&wc-api-route=$matches[2]', 'top' );
  66. }
  67. /**
  68. * Handle REST API requests.
  69. *
  70. * @since 2.2
  71. * @deprecated 2.6.0
  72. */
  73. public function handle_rest_api_requests() {
  74. global $wp;
  75. if ( ! empty( $_GET['wc-api-version'] ) ) {
  76. $wp->query_vars['wc-api-version'] = $_GET['wc-api-version'];
  77. }
  78. if ( ! empty( $_GET['wc-api-route'] ) ) {
  79. $wp->query_vars['wc-api-route'] = $_GET['wc-api-route'];
  80. }
  81. // REST API request.
  82. if ( ! empty( $wp->query_vars['wc-api-version'] ) && ! empty( $wp->query_vars['wc-api-route'] ) ) {
  83. wc_maybe_define_constant( 'WC_API_REQUEST', true );
  84. wc_maybe_define_constant( 'WC_API_REQUEST_VERSION', absint( $wp->query_vars['wc-api-version'] ) );
  85. // Legacy v1 API request.
  86. if ( 1 === WC_API_REQUEST_VERSION ) {
  87. $this->handle_v1_rest_api_request();
  88. } elseif ( 2 === WC_API_REQUEST_VERSION ) {
  89. $this->handle_v2_rest_api_request();
  90. } else {
  91. $this->includes();
  92. $this->server = new WC_API_Server( $wp->query_vars['wc-api-route'] );
  93. // load API resource classes.
  94. $this->register_resources( $this->server );
  95. // Fire off the request.
  96. $this->server->serve_request();
  97. }
  98. exit;
  99. }
  100. }
  101. /**
  102. * Include required files for REST API request.
  103. *
  104. * @since 2.1
  105. * @deprecated 2.6.0
  106. */
  107. public function includes() {
  108. // API server / response handlers.
  109. include_once( dirname( __FILE__ ) . '/api/v3/class-wc-api-exception.php' );
  110. include_once( dirname( __FILE__ ) . '/api/v3/class-wc-api-server.php' );
  111. include_once( dirname( __FILE__ ) . '/api/v3/interface-wc-api-handler.php' );
  112. include_once( dirname( __FILE__ ) . '/api/v3/class-wc-api-json-handler.php' );
  113. // Authentication.
  114. include_once( dirname( __FILE__ ) . '/api/v3/class-wc-api-authentication.php' );
  115. $this->authentication = new WC_API_Authentication();
  116. include_once( dirname( __FILE__ ) . '/api/v3/class-wc-api-resource.php' );
  117. include_once( dirname( __FILE__ ) . '/api/v3/class-wc-api-coupons.php' );
  118. include_once( dirname( __FILE__ ) . '/api/v3/class-wc-api-customers.php' );
  119. include_once( dirname( __FILE__ ) . '/api/v3/class-wc-api-orders.php' );
  120. include_once( dirname( __FILE__ ) . '/api/v3/class-wc-api-products.php' );
  121. include_once( dirname( __FILE__ ) . '/api/v3/class-wc-api-reports.php' );
  122. include_once( dirname( __FILE__ ) . '/api/v3/class-wc-api-taxes.php' );
  123. include_once( dirname( __FILE__ ) . '/api/v3/class-wc-api-webhooks.php' );
  124. // Allow plugins to load other response handlers or resource classes.
  125. do_action( 'woocommerce_api_loaded' );
  126. }
  127. /**
  128. * Register available API resources.
  129. *
  130. * @since 2.1
  131. * @deprecated 2.6.0
  132. * @param WC_API_Server $server the REST server.
  133. */
  134. public function register_resources( $server ) {
  135. $api_classes = apply_filters( 'woocommerce_api_classes',
  136. array(
  137. 'WC_API_Coupons',
  138. 'WC_API_Customers',
  139. 'WC_API_Orders',
  140. 'WC_API_Products',
  141. 'WC_API_Reports',
  142. 'WC_API_Taxes',
  143. 'WC_API_Webhooks',
  144. )
  145. );
  146. foreach ( $api_classes as $api_class ) {
  147. $this->$api_class = new $api_class( $server );
  148. }
  149. }
  150. /**
  151. * Handle legacy v1 REST API requests.
  152. *
  153. * @since 2.2
  154. * @deprecated 2.6.0
  155. */
  156. private function handle_v1_rest_api_request() {
  157. // Include legacy required files for v1 REST API request.
  158. include_once( dirname( __FILE__ ) . '/api/v1/class-wc-api-server.php' );
  159. include_once( dirname( __FILE__ ) . '/api/v1/interface-wc-api-handler.php' );
  160. include_once( dirname( __FILE__ ) . '/api/v1/class-wc-api-json-handler.php' );
  161. include_once( dirname( __FILE__ ) . '/api/v1/class-wc-api-xml-handler.php' );
  162. include_once( dirname( __FILE__ ) . '/api/v1/class-wc-api-authentication.php' );
  163. $this->authentication = new WC_API_Authentication();
  164. include_once( dirname( __FILE__ ) . '/api/v1/class-wc-api-resource.php' );
  165. include_once( dirname( __FILE__ ) . '/api/v1/class-wc-api-coupons.php' );
  166. include_once( dirname( __FILE__ ) . '/api/v1/class-wc-api-customers.php' );
  167. include_once( dirname( __FILE__ ) . '/api/v1/class-wc-api-orders.php' );
  168. include_once( dirname( __FILE__ ) . '/api/v1/class-wc-api-products.php' );
  169. include_once( dirname( __FILE__ ) . '/api/v1/class-wc-api-reports.php' );
  170. // Allow plugins to load other response handlers or resource classes.
  171. do_action( 'woocommerce_api_loaded' );
  172. $this->server = new WC_API_Server( $GLOBALS['wp']->query_vars['wc-api-route'] );
  173. // Register available resources for legacy v1 REST API request.
  174. $api_classes = apply_filters( 'woocommerce_api_classes',
  175. array(
  176. 'WC_API_Customers',
  177. 'WC_API_Orders',
  178. 'WC_API_Products',
  179. 'WC_API_Coupons',
  180. 'WC_API_Reports',
  181. )
  182. );
  183. foreach ( $api_classes as $api_class ) {
  184. $this->$api_class = new $api_class( $this->server );
  185. }
  186. // Fire off the request.
  187. $this->server->serve_request();
  188. }
  189. /**
  190. * Handle legacy v2 REST API requests.
  191. *
  192. * @since 2.4
  193. * @deprecated 2.6.0
  194. */
  195. private function handle_v2_rest_api_request() {
  196. include_once( dirname( __FILE__ ) . '/api/v2/class-wc-api-exception.php' );
  197. include_once( dirname( __FILE__ ) . '/api/v2/class-wc-api-server.php' );
  198. include_once( dirname( __FILE__ ) . '/api/v2/interface-wc-api-handler.php' );
  199. include_once( dirname( __FILE__ ) . '/api/v2/class-wc-api-json-handler.php' );
  200. include_once( dirname( __FILE__ ) . '/api/v2/class-wc-api-authentication.php' );
  201. $this->authentication = new WC_API_Authentication();
  202. include_once( dirname( __FILE__ ) . '/api/v2/class-wc-api-resource.php' );
  203. include_once( dirname( __FILE__ ) . '/api/v2/class-wc-api-coupons.php' );
  204. include_once( dirname( __FILE__ ) . '/api/v2/class-wc-api-customers.php' );
  205. include_once( dirname( __FILE__ ) . '/api/v2/class-wc-api-orders.php' );
  206. include_once( dirname( __FILE__ ) . '/api/v2/class-wc-api-products.php' );
  207. include_once( dirname( __FILE__ ) . '/api/v2/class-wc-api-reports.php' );
  208. include_once( dirname( __FILE__ ) . '/api/v2/class-wc-api-webhooks.php' );
  209. // allow plugins to load other response handlers or resource classes.
  210. do_action( 'woocommerce_api_loaded' );
  211. $this->server = new WC_API_Server( $GLOBALS['wp']->query_vars['wc-api-route'] );
  212. // Register available resources for legacy v2 REST API request.
  213. $api_classes = apply_filters( 'woocommerce_api_classes',
  214. array(
  215. 'WC_API_Customers',
  216. 'WC_API_Orders',
  217. 'WC_API_Products',
  218. 'WC_API_Coupons',
  219. 'WC_API_Reports',
  220. 'WC_API_Webhooks',
  221. )
  222. );
  223. foreach ( $api_classes as $api_class ) {
  224. $this->$api_class = new $api_class( $this->server );
  225. }
  226. // Fire off the request.
  227. $this->server->serve_request();
  228. }
  229. /**
  230. * Rest API Init.
  231. *
  232. * @deprecated 3.7.0 - REST API clases autoload.
  233. */
  234. public function rest_api_init() {}
  235. /**
  236. * Include REST API classes.
  237. *
  238. * @deprecated 3.7.0 - REST API clases autoload.
  239. */
  240. public function rest_api_includes() {
  241. $this->rest_api_init();
  242. }
  243. /**
  244. * Register REST API routes.
  245. *
  246. * @deprecated 3.7.0
  247. */
  248. public function register_rest_routes() {
  249. wc_deprecated_function( 'WC_Legacy_API::register_rest_routes', '3.7.0', '' );
  250. $this->register_wp_admin_settings();
  251. }
  252. }