Server.php
291 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Initialize this version of the REST API. |
| 4 | * |
| 5 | * @package WooCommerce\RestApi |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\WooCommerce\RestApi; |
| 9 | |
| 10 | defined( 'ABSPATH' ) || exit; |
| 11 | |
| 12 | use Automattic\WooCommerce\Proxies\LegacyProxy; |
| 13 | use Automattic\WooCommerce\RestApi\Utilities\SingletonTrait; |
| 14 | use Automattic\WooCommerce\Admin\Features\Features; |
| 15 | use Automattic\WooCommerce\Internal\RestApi\Routes\V4\OrderNotes\Controller as OrderNotesController; |
| 16 | use Automattic\WooCommerce\Internal\RestApi\Routes\V4\ShippingZones\Controller as ShippingZonesController; |
| 17 | use Automattic\WooCommerce\Internal\RestApi\Routes\V4\ShippingZoneMethod\Controller as ShippingZoneMethodController; |
| 18 | use Automattic\WooCommerce\Internal\RestApi\Routes\V4\Orders\Controller as OrdersController; |
| 19 | use Automattic\WooCommerce\Internal\RestApi\Routes\V4\Refunds\Controller as RefundsController; |
| 20 | use Automattic\WooCommerce\Internal\RestApi\Routes\V4\Settings\Products\Controller as SettingsProductsController; |
| 21 | use Automattic\WooCommerce\Internal\RestApi\Routes\V4\Products\Controller as ProductsController; |
| 22 | use Automattic\WooCommerce\Internal\RestApi\Routes\V4\Settings\PaymentGateways\Controller as PaymentGatewaysController; |
| 23 | use Automattic\WooCommerce\Internal\RestApi\Routes\V4\Settings\OfflinePaymentMethods\Controller as OfflinePaymentMethodsController; |
| 24 | use Automattic\WooCommerce\Internal\RestApi\Routes\V4\Customers\Controller as CustomersController; |
| 25 | use Automattic\WooCommerce\Internal\RestApi\Routes\V4\Settings\General\Controller as GeneralSettingsController; |
| 26 | use Automattic\WooCommerce\Internal\RestApi\Routes\V4\Settings\Email\Controller as EmailSettingsController; |
| 27 | use Automattic\WooCommerce\Internal\RestApi\Routes\V4\Settings\Tax\Controller as TaxSettingsController; |
| 28 | use Automattic\WooCommerce\Internal\RestApi\Routes\V4\Settings\Emails\Controller as EmailsSettingsController; |
| 29 | use Automattic\WooCommerce\Internal\RestApi\Routes\V4\Fulfillments\Controller as FulfillmentsController; |
| 30 | use Automattic\WooCommerce\Internal\RestApi\Routes\V4\Settings\Account\Controller as AccountSettingsController; |
| 31 | |
| 32 | /** |
| 33 | * Class responsible for loading the REST API and all REST API namespaces. |
| 34 | */ |
| 35 | class Server { |
| 36 | use SingletonTrait; |
| 37 | |
| 38 | /** |
| 39 | * REST API namespaces and endpoints. |
| 40 | * |
| 41 | * @var array |
| 42 | */ |
| 43 | protected $controllers = array(); |
| 44 | |
| 45 | /** |
| 46 | * Hook into WordPress ready to init the REST API as needed. |
| 47 | */ |
| 48 | public function init() { // phpcs:ignore WooCommerce.Functions.InternalInjectionMethod -- Not an injection method. |
| 49 | add_action( 'rest_api_init', array( $this, 'register_rest_routes' ), 10 ); |
| 50 | |
| 51 | \WC_REST_System_Status_V2_Controller::register_cache_clean(); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Register REST API routes. |
| 56 | */ |
| 57 | public function register_rest_routes() { |
| 58 | $container = wc_get_container(); |
| 59 | $legacy_proxy = $container->get( LegacyProxy::class ); |
| 60 | foreach ( $this->get_rest_namespaces() as $namespace => $controllers ) { |
| 61 | foreach ( $controllers as $controller_name => $controller_class ) { |
| 62 | $this->controllers[ $namespace ][ $controller_name ] = $container->has( $controller_class ) ? |
| 63 | $container->get( $controller_class ) : |
| 64 | $legacy_proxy->get_instance_of( $controller_class ); |
| 65 | $this->controllers[ $namespace ][ $controller_name ]->register_routes(); |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Get API namespaces - new namespaces should be registered here. |
| 72 | * |
| 73 | * @return array List of Namespaces and Main controller classes. |
| 74 | */ |
| 75 | protected function get_rest_namespaces() { |
| 76 | $namespaces = array( |
| 77 | 'wc/v1' => wc_rest_should_load_namespace( 'wc/v1' ) ? $this->get_v1_controllers() : array(), |
| 78 | 'wc/v2' => wc_rest_should_load_namespace( 'wc/v2' ) ? $this->get_v2_controllers() : array(), |
| 79 | 'wc/v3' => wc_rest_should_load_namespace( 'wc/v3' ) ? $this->get_v3_controllers() : array(), |
| 80 | 'wc-telemetry' => wc_rest_should_load_namespace( 'wc-telemetry' ) ? $this->get_telemetry_controllers() : array(), |
| 81 | ); |
| 82 | |
| 83 | if ( wc_rest_should_load_namespace( 'wc/v4' ) && Features::is_enabled( 'rest-api-v4' ) ) { |
| 84 | $namespaces['wc/v4'] = $this->get_v4_controllers(); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Filter the list of REST API controllers to load. |
| 89 | * |
| 90 | * @since 4.5.0 |
| 91 | * @param array $controllers List of $namespace => $controllers to load. |
| 92 | */ |
| 93 | return apply_filters( 'woocommerce_rest_api_get_rest_namespaces', $namespaces ); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * List of controllers in the wc/v1 namespace. |
| 98 | * |
| 99 | * @return array |
| 100 | */ |
| 101 | protected function get_v1_controllers() { |
| 102 | return array( |
| 103 | 'coupons' => 'WC_REST_Coupons_V1_Controller', |
| 104 | 'customer-downloads' => 'WC_REST_Customer_Downloads_V1_Controller', |
| 105 | 'customers' => 'WC_REST_Customers_V1_Controller', |
| 106 | 'order-notes' => 'WC_REST_Order_Notes_V1_Controller', |
| 107 | 'order-refunds' => 'WC_REST_Order_Refunds_V1_Controller', |
| 108 | 'orders' => 'WC_REST_Orders_V1_Controller', |
| 109 | 'product-attribute-terms' => 'WC_REST_Product_Attribute_Terms_V1_Controller', |
| 110 | 'product-attributes' => 'WC_REST_Product_Attributes_V1_Controller', |
| 111 | 'product-categories' => 'WC_REST_Product_Categories_V1_Controller', |
| 112 | 'product-reviews' => 'WC_REST_Product_Reviews_V1_Controller', |
| 113 | 'product-shipping-classes' => 'WC_REST_Product_Shipping_Classes_V1_Controller', |
| 114 | 'product-tags' => 'WC_REST_Product_Tags_V1_Controller', |
| 115 | 'products' => 'WC_REST_Products_V1_Controller', |
| 116 | 'reports-sales' => 'WC_REST_Report_Sales_V1_Controller', |
| 117 | 'reports-top-sellers' => 'WC_REST_Report_Top_Sellers_V1_Controller', |
| 118 | 'reports' => 'WC_REST_Reports_V1_Controller', |
| 119 | 'tax-classes' => 'WC_REST_Tax_Classes_V1_Controller', |
| 120 | 'taxes' => 'WC_REST_Taxes_V1_Controller', |
| 121 | 'webhooks' => 'WC_REST_Webhooks_V1_Controller', |
| 122 | 'webhook-deliveries' => 'WC_REST_Webhook_Deliveries_V1_Controller', |
| 123 | ); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * List of controllers in the wc/v2 namespace. |
| 128 | * |
| 129 | * @return array |
| 130 | */ |
| 131 | protected function get_v2_controllers() { |
| 132 | return array( |
| 133 | 'coupons' => 'WC_REST_Coupons_V2_Controller', |
| 134 | 'customer-downloads' => 'WC_REST_Customer_Downloads_V2_Controller', |
| 135 | 'customers' => 'WC_REST_Customers_V2_Controller', |
| 136 | 'network-orders' => 'WC_REST_Network_Orders_V2_Controller', |
| 137 | 'order-notes' => 'WC_REST_Order_Notes_V2_Controller', |
| 138 | 'order-refunds' => 'WC_REST_Order_Refunds_V2_Controller', |
| 139 | 'orders' => 'WC_REST_Orders_V2_Controller', |
| 140 | 'product-attribute-terms' => 'WC_REST_Product_Attribute_Terms_V2_Controller', |
| 141 | 'product-attributes' => 'WC_REST_Product_Attributes_V2_Controller', |
| 142 | 'product-categories' => 'WC_REST_Product_Categories_V2_Controller', |
| 143 | 'product-reviews' => 'WC_REST_Product_Reviews_V2_Controller', |
| 144 | 'product-shipping-classes' => 'WC_REST_Product_Shipping_Classes_V2_Controller', |
| 145 | 'product-tags' => 'WC_REST_Product_Tags_V2_Controller', |
| 146 | 'products' => 'WC_REST_Products_V2_Controller', |
| 147 | 'product-variations' => 'WC_REST_Product_Variations_V2_Controller', |
| 148 | 'reports-sales' => 'WC_REST_Report_Sales_V2_Controller', |
| 149 | 'reports-top-sellers' => 'WC_REST_Report_Top_Sellers_V2_Controller', |
| 150 | 'reports' => 'WC_REST_Reports_V2_Controller', |
| 151 | 'settings' => 'WC_REST_Settings_V2_Controller', |
| 152 | 'settings-options' => 'WC_REST_Setting_Options_V2_Controller', |
| 153 | 'shipping-zones' => 'WC_REST_Shipping_Zones_V2_Controller', |
| 154 | 'shipping-zone-locations' => 'WC_REST_Shipping_Zone_Locations_V2_Controller', |
| 155 | 'shipping-zone-methods' => 'WC_REST_Shipping_Zone_Methods_V2_Controller', |
| 156 | 'tax-classes' => 'WC_REST_Tax_Classes_V2_Controller', |
| 157 | 'taxes' => 'WC_REST_Taxes_V2_Controller', |
| 158 | 'webhooks' => 'WC_REST_Webhooks_V2_Controller', |
| 159 | 'webhook-deliveries' => 'WC_REST_Webhook_Deliveries_V2_Controller', |
| 160 | 'system-status' => 'WC_REST_System_Status_V2_Controller', |
| 161 | 'system-status-tools' => 'WC_REST_System_Status_Tools_V2_Controller', |
| 162 | 'shipping-methods' => 'WC_REST_Shipping_Methods_V2_Controller', |
| 163 | 'payment-gateways' => 'WC_REST_Payment_Gateways_V2_Controller', |
| 164 | ); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * List of controllers in the wc/v3 namespace. |
| 169 | * |
| 170 | * @return array |
| 171 | */ |
| 172 | protected function get_v3_controllers() { |
| 173 | $controllers = array( |
| 174 | 'coupons' => 'WC_REST_Coupons_Controller', |
| 175 | 'customer-downloads' => 'WC_REST_Customer_Downloads_Controller', |
| 176 | 'customers' => 'WC_REST_Customers_Controller', |
| 177 | 'layout-templates' => 'WC_REST_Layout_Templates_Controller', |
| 178 | 'network-orders' => 'WC_REST_Network_Orders_Controller', |
| 179 | 'order-notes' => 'WC_REST_Order_Notes_Controller', |
| 180 | 'order-refunds' => 'WC_REST_Order_Refunds_Controller', |
| 181 | 'orders' => 'WC_REST_Orders_Controller', |
| 182 | 'product-attribute-terms' => 'WC_REST_Product_Attribute_Terms_Controller', |
| 183 | 'product-attributes' => 'WC_REST_Product_Attributes_Controller', |
| 184 | 'product-categories' => 'WC_REST_Product_Categories_Controller', |
| 185 | 'product-custom-fields' => 'WC_REST_Product_Custom_Fields_Controller', |
| 186 | 'product-reviews' => 'WC_REST_Product_Reviews_Controller', |
| 187 | 'product-shipping-classes' => 'WC_REST_Product_Shipping_Classes_Controller', |
| 188 | 'product-tags' => 'WC_REST_Product_Tags_Controller', |
| 189 | 'products' => 'WC_REST_Products_Controller', |
| 190 | 'product-variations' => 'WC_REST_Product_Variations_Controller', |
| 191 | 'refunds' => 'WC_REST_Refunds_Controller', |
| 192 | 'reports-sales' => 'WC_REST_Report_Sales_Controller', |
| 193 | 'reports-top-sellers' => 'WC_REST_Report_Top_Sellers_Controller', |
| 194 | 'reports-orders-totals' => 'WC_REST_Report_Orders_Totals_Controller', |
| 195 | 'reports-products-totals' => 'WC_REST_Report_Products_Totals_Controller', |
| 196 | 'reports-customers-totals' => 'WC_REST_Report_Customers_Totals_Controller', |
| 197 | 'reports-coupons-totals' => 'WC_REST_Report_Coupons_Totals_Controller', |
| 198 | 'reports-reviews-totals' => 'WC_REST_Report_Reviews_Totals_Controller', |
| 199 | 'reports' => 'WC_REST_Reports_Controller', |
| 200 | 'settings' => 'WC_REST_Settings_Controller', |
| 201 | 'settings-options' => 'WC_REST_Setting_Options_Controller', |
| 202 | 'shipping-zones' => 'WC_REST_Shipping_Zones_Controller', |
| 203 | 'shipping-zone-locations' => 'WC_REST_Shipping_Zone_Locations_Controller', |
| 204 | 'shipping-zone-methods' => 'WC_REST_Shipping_Zone_Methods_Controller', |
| 205 | 'tax-classes' => 'WC_REST_Tax_Classes_Controller', |
| 206 | 'taxes' => 'WC_REST_Taxes_Controller', |
| 207 | 'variations' => 'WC_REST_Variations_Controller', |
| 208 | 'webhooks' => 'WC_REST_Webhooks_Controller', |
| 209 | 'system-status' => 'WC_REST_System_Status_Controller', |
| 210 | 'system-status-tools' => 'WC_REST_System_Status_Tools_Controller', |
| 211 | 'shipping-methods' => 'WC_REST_Shipping_Methods_Controller', |
| 212 | 'payment-gateways' => 'WC_REST_Payment_Gateways_Controller', |
| 213 | 'data' => 'WC_REST_Data_Controller', |
| 214 | 'data-continents' => 'WC_REST_Data_Continents_Controller', |
| 215 | 'data-countries' => 'WC_REST_Data_Countries_Controller', |
| 216 | 'data-currencies' => 'WC_REST_Data_Currencies_Controller', |
| 217 | 'paypal-standard' => 'WC_REST_Paypal_Standard_Controller', |
| 218 | 'paypal-webhooks' => 'WC_REST_Paypal_Webhooks_Controller', |
| 219 | 'paypal-buttons' => 'WC_REST_Paypal_Buttons_Controller', |
| 220 | ); |
| 221 | |
| 222 | if ( Features::is_enabled( 'products-catalog-api' ) ) { |
| 223 | $controllers['products-catalog'] = 'WC_REST_Products_Catalog_Controller'; |
| 224 | } |
| 225 | |
| 226 | return $controllers; |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * List of controllers in the wc/v4 namespace. |
| 231 | * |
| 232 | * @return array |
| 233 | */ |
| 234 | protected function get_v4_controllers() { |
| 235 | return array( |
| 236 | 'fulfillments' => FulfillmentsController::class, |
| 237 | 'products' => ProductsController::class, |
| 238 | 'customers' => CustomersController::class, |
| 239 | 'order-notes' => OrderNotesController::class, |
| 240 | 'shipping-zones' => ShippingZonesController::class, |
| 241 | 'shipping-zone-method' => ShippingZoneMethodController::class, |
| 242 | 'orders' => OrdersController::class, |
| 243 | 'refunds' => RefundsController::class, |
| 244 | 'offline-payment-methods' => OfflinePaymentMethodsController::class, |
| 245 | 'settings-general' => GeneralSettingsController::class, |
| 246 | 'settings-email' => EmailSettingsController::class, |
| 247 | 'settings-emails' => EmailsSettingsController::class, |
| 248 | 'settings-products' => SettingsProductsController::class, |
| 249 | 'settings-payment-gateways' => PaymentGatewaysController::class, |
| 250 | 'settings-tax' => TaxSettingsController::class, |
| 251 | 'settings-account' => AccountSettingsController::class, |
| 252 | // This is a wrapper that redirects V4 settings requests to the V3 settings controller. |
| 253 | 'settings' => 'WC_REST_Settings_V4_Controller', |
| 254 | ); |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Get instance of a V4 controller. |
| 259 | * |
| 260 | * @param string $identifier Controller identifier. |
| 261 | * @param string $route Route class name. |
| 262 | * @return object The instance of the controller. |
| 263 | */ |
| 264 | protected function get_v4_controller( $identifier, $route ) { |
| 265 | if ( isset( $this->controllers['wc/v4'][ $identifier ] ) ) { |
| 266 | return $this->controllers['wc/v4'][ $identifier ]; |
| 267 | } |
| 268 | return new $route(); |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * List of controllers in the telemetry namespace. |
| 273 | * |
| 274 | * @return array |
| 275 | */ |
| 276 | protected function get_telemetry_controllers() { |
| 277 | return array( |
| 278 | 'tracker' => 'WC_REST_Telemetry_Controller', |
| 279 | ); |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Return the path to the package. |
| 284 | * |
| 285 | * @return string |
| 286 | */ |
| 287 | public static function get_path() { |
| 288 | return dirname( __DIR__ ); |
| 289 | } |
| 290 | } |
| 291 |