Açıklama Yok

Server.php 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * Initialize this version of the REST API.
  4. *
  5. * @package WooCommerce\RestApi
  6. */
  7. namespace Automattic\WooCommerce\RestApi;
  8. defined( 'ABSPATH' ) || exit;
  9. use Automattic\WooCommerce\RestApi\Utilities\SingletonTrait;
  10. /**
  11. * Class responsible for loading the REST API and all REST API namespaces.
  12. */
  13. class Server {
  14. use SingletonTrait;
  15. /**
  16. * REST API namespaces and endpoints.
  17. *
  18. * @var array
  19. */
  20. protected $controllers = array();
  21. /**
  22. * Hook into WordPress ready to init the REST API as needed.
  23. */
  24. public function init() {
  25. add_action( 'rest_api_init', array( $this, 'register_rest_routes' ), 10 );
  26. }
  27. /**
  28. * Register REST API routes.
  29. */
  30. public function register_rest_routes() {
  31. foreach ( $this->get_rest_namespaces() as $namespace => $controllers ) {
  32. foreach ( $controllers as $controller_name => $controller_class ) {
  33. $this->controllers[ $namespace ][ $controller_name ] = new $controller_class();
  34. $this->controllers[ $namespace ][ $controller_name ]->register_routes();
  35. }
  36. }
  37. }
  38. /**
  39. * Get API namespaces - new namespaces should be registered here.
  40. *
  41. * @return array List of Namespaces and Main controller classes.
  42. */
  43. protected function get_rest_namespaces() {
  44. return apply_filters(
  45. 'woocommerce_rest_api_get_rest_namespaces',
  46. array(
  47. 'wc/v1' => $this->get_v1_controllers(),
  48. 'wc/v2' => $this->get_v2_controllers(),
  49. 'wc/v3' => $this->get_v3_controllers(),
  50. )
  51. );
  52. }
  53. /**
  54. * List of controllers in the wc/v1 namespace.
  55. *
  56. * @return array
  57. */
  58. protected function get_v1_controllers() {
  59. return array(
  60. 'coupons' => 'WC_REST_Coupons_V1_Controller',
  61. 'customer-downloads' => 'WC_REST_Customer_Downloads_V1_Controller',
  62. 'customers' => 'WC_REST_Customers_V1_Controller',
  63. 'order-notes' => 'WC_REST_Order_Notes_V1_Controller',
  64. 'order-refunds' => 'WC_REST_Order_Refunds_V1_Controller',
  65. 'orders' => 'WC_REST_Orders_V1_Controller',
  66. 'product-attribute-terms' => 'WC_REST_Product_Attribute_Terms_V1_Controller',
  67. 'product-attributes' => 'WC_REST_Product_Attributes_V1_Controller',
  68. 'product-categories' => 'WC_REST_Product_Categories_V1_Controller',
  69. 'product-reviews' => 'WC_REST_Product_Reviews_V1_Controller',
  70. 'product-shipping-classes' => 'WC_REST_Product_Shipping_Classes_V1_Controller',
  71. 'product-tags' => 'WC_REST_Product_Tags_V1_Controller',
  72. 'products' => 'WC_REST_Products_V1_Controller',
  73. 'reports-sales' => 'WC_REST_Report_Sales_V1_Controller',
  74. 'reports-top-sellers' => 'WC_REST_Report_Top_Sellers_V1_Controller',
  75. 'reports' => 'WC_REST_Reports_V1_Controller',
  76. 'tax-classes' => 'WC_REST_Tax_Classes_V1_Controller',
  77. 'taxes' => 'WC_REST_Taxes_V1_Controller',
  78. 'webhooks' => 'WC_REST_Webhooks_V1_Controller',
  79. 'webhook-deliveries' => 'WC_REST_Webhook_Deliveries_V1_Controller',
  80. );
  81. }
  82. /**
  83. * List of controllers in the wc/v2 namespace.
  84. *
  85. * @return array
  86. */
  87. protected function get_v2_controllers() {
  88. return array(
  89. 'coupons' => 'WC_REST_Coupons_V2_Controller',
  90. 'customer-downloads' => 'WC_REST_Customer_Downloads_V2_Controller',
  91. 'customers' => 'WC_REST_Customers_V2_Controller',
  92. 'network-orders' => 'WC_REST_Network_Orders_V2_Controller',
  93. 'order-notes' => 'WC_REST_Order_Notes_V2_Controller',
  94. 'order-refunds' => 'WC_REST_Order_Refunds_V2_Controller',
  95. 'orders' => 'WC_REST_Orders_V2_Controller',
  96. 'product-attribute-terms' => 'WC_REST_Product_Attribute_Terms_V2_Controller',
  97. 'product-attributes' => 'WC_REST_Product_Attributes_V2_Controller',
  98. 'product-categories' => 'WC_REST_Product_Categories_V2_Controller',
  99. 'product-reviews' => 'WC_REST_Product_Reviews_V2_Controller',
  100. 'product-shipping-classes' => 'WC_REST_Product_Shipping_Classes_V2_Controller',
  101. 'product-tags' => 'WC_REST_Product_Tags_V2_Controller',
  102. 'products' => 'WC_REST_Products_V2_Controller',
  103. 'product-variations' => 'WC_REST_Product_Variations_V2_Controller',
  104. 'reports-sales' => 'WC_REST_Report_Sales_V2_Controller',
  105. 'reports-top-sellers' => 'WC_REST_Report_Top_Sellers_V2_Controller',
  106. 'reports' => 'WC_REST_Reports_V2_Controller',
  107. 'settings' => 'WC_REST_Settings_V2_Controller',
  108. 'settings-options' => 'WC_REST_Setting_Options_V2_Controller',
  109. 'shipping-zones' => 'WC_REST_Shipping_Zones_V2_Controller',
  110. 'shipping-zone-locations' => 'WC_REST_Shipping_Zone_Locations_V2_Controller',
  111. 'shipping-zone-methods' => 'WC_REST_Shipping_Zone_Methods_V2_Controller',
  112. 'tax-classes' => 'WC_REST_Tax_Classes_V2_Controller',
  113. 'taxes' => 'WC_REST_Taxes_V2_Controller',
  114. 'webhooks' => 'WC_REST_Webhooks_V2_Controller',
  115. 'webhook-deliveries' => 'WC_REST_Webhook_Deliveries_V2_Controller',
  116. 'system-status' => 'WC_REST_System_Status_V2_Controller',
  117. 'system-status-tools' => 'WC_REST_System_Status_Tools_V2_Controller',
  118. 'shipping-methods' => 'WC_REST_Shipping_Methods_V2_Controller',
  119. 'payment-gateways' => 'WC_REST_Payment_Gateways_V2_Controller',
  120. );
  121. }
  122. /**
  123. * List of controllers in the wc/v3 namespace.
  124. *
  125. * @return array
  126. */
  127. protected function get_v3_controllers() {
  128. return array(
  129. 'coupons' => 'WC_REST_Coupons_Controller',
  130. 'customer-downloads' => 'WC_REST_Customer_Downloads_Controller',
  131. 'customers' => 'WC_REST_Customers_Controller',
  132. 'network-orders' => 'WC_REST_Network_Orders_Controller',
  133. 'order-notes' => 'WC_REST_Order_Notes_Controller',
  134. 'order-refunds' => 'WC_REST_Order_Refunds_Controller',
  135. 'orders' => 'WC_REST_Orders_Controller',
  136. 'product-attribute-terms' => 'WC_REST_Product_Attribute_Terms_Controller',
  137. 'product-attributes' => 'WC_REST_Product_Attributes_Controller',
  138. 'product-categories' => 'WC_REST_Product_Categories_Controller',
  139. 'product-reviews' => 'WC_REST_Product_Reviews_Controller',
  140. 'product-shipping-classes' => 'WC_REST_Product_Shipping_Classes_Controller',
  141. 'product-tags' => 'WC_REST_Product_Tags_Controller',
  142. 'products' => 'WC_REST_Products_Controller',
  143. 'product-variations' => 'WC_REST_Product_Variations_Controller',
  144. 'reports-sales' => 'WC_REST_Report_Sales_Controller',
  145. 'reports-top-sellers' => 'WC_REST_Report_Top_Sellers_Controller',
  146. 'reports-orders-totals' => 'WC_REST_Report_Orders_Totals_Controller',
  147. 'reports-products-totals' => 'WC_REST_Report_Products_Totals_Controller',
  148. 'reports-customers-totals' => 'WC_REST_Report_Customers_Totals_Controller',
  149. 'reports-coupons-totals' => 'WC_REST_Report_Coupons_Totals_Controller',
  150. 'reports-reviews-totals' => 'WC_REST_Report_Reviews_Totals_Controller',
  151. 'reports' => 'WC_REST_Reports_Controller',
  152. 'settings' => 'WC_REST_Settings_Controller',
  153. 'settings-options' => 'WC_REST_Setting_Options_Controller',
  154. 'shipping-zones' => 'WC_REST_Shipping_Zones_Controller',
  155. 'shipping-zone-locations' => 'WC_REST_Shipping_Zone_Locations_Controller',
  156. 'shipping-zone-methods' => 'WC_REST_Shipping_Zone_Methods_Controller',
  157. 'tax-classes' => 'WC_REST_Tax_Classes_Controller',
  158. 'taxes' => 'WC_REST_Taxes_Controller',
  159. 'webhooks' => 'WC_REST_Webhooks_Controller',
  160. 'system-status' => 'WC_REST_System_Status_Controller',
  161. 'system-status-tools' => 'WC_REST_System_Status_Tools_Controller',
  162. 'shipping-methods' => 'WC_REST_Shipping_Methods_Controller',
  163. 'payment-gateways' => 'WC_REST_Payment_Gateways_Controller',
  164. 'data' => 'WC_REST_Data_Controller',
  165. 'data-continents' => 'WC_REST_Data_Continents_Controller',
  166. 'data-countries' => 'WC_REST_Data_Countries_Controller',
  167. 'data-currencies' => 'WC_REST_Data_Currencies_Controller',
  168. );
  169. }
  170. /**
  171. * Return the path to the package.
  172. *
  173. * @return string
  174. */
  175. public static function get_path() {
  176. return dirname( __DIR__ );
  177. }
  178. }