diff --git a/README.md b/README.md index 1333ed77b7e..dd792585a9c 100644 --- a/README.md +++ b/README.md @@ -1 +1,34 @@ -TODO +--- +title: 'WooCommerce REST API' +--- + +WooCommerce REST API +=== + +This repository houses the WooCommerce REST API found in the core WooCommerce plugin. + +It can also be used as a standalone plugin (it still requires WooCommerce!), or can be pulled into other projects and feature plugins that need new API features that are not yet in the core release. + +Once stable, changes in woocommerce-rest-api are brought over to [WooCommerce core](https://github.com/woocommerce/woocommerce) when they are ready for release. + +## Endpoint namespaces + +* Current stable WC REST API version: **wc/v3** - **[Docs](https://woocommerce.github.io/woocommerce-rest-api-docs/)** +* WC Blocks REST API version (internal use only): **wc-blocks/v1** +* Older versions: + * **wc/v1** - **[Docs](https://woocommerce.github.io/woocommerce-rest-api-docs/wp-api-v1.html)** + * **wc/v2** - **[Docs](https://woocommerce.github.io/woocommerce-rest-api-docs/wp-api-v2.html)** + +## Contributing + +Please read the [WooCommerce contributor guidelines](https://github.com/woocommerce/woocommerce/blob/master/.github/CONTRIBUTING.md) for more information how you can contribute. + +Endpoints are located in the `includes/` directory. Endpoints currently inherit from the stable version of the endpoint. If you need to change the behavior of an endpoint, you can do so in these classes. + +phpunit tests for the API are located in the `tests/unit-tests/` folder and are also merged and shipped with WooCommerce core. You can use the same helpers/framework files that core uses, or introduce new ones. + +Run tests using `phpunit` in the root of this folder. Code coverage reports can be ran with `phpunit --coverage-html /tmp/coverage`. + +## Translation + +For strings located in API endpoints, use `woocommerce` as your text-domain. These endpoints will at some point be merged back into WooCommerce Core. diff --git a/RestApi.php b/RestApi.php deleted file mode 100644 index e0817eb89d2..00000000000 --- a/RestApi.php +++ /dev/null @@ -1,146 +0,0 @@ -get_file_name_from_class( $class ); - - if ( file_exists( "{$dir}{$file}" ) ) { - include "{$dir}{$file}"; - return; - } - - $file = $this->get_file_name_from_class( $class, 'abstract-' ); - - if ( file_exists( dirname( __FILE__ ) . "/includes/abstracts/{$file}" ) ) { - include dirname( __FILE__ ) . "/includes/abstracts/{$file}"; - return; - } - } - - if ( file_exists( dirname( __FILE__ ) . "/includes/{$class}.php" ) ) { - include dirname( __FILE__ ) . "/includes/{$class}.php"; - return; - } - } - - /** - * Get API namespaces. - */ - protected function get_namespaces() { - return array( - 'wc/v1' => 'WC_Rest_API_V1', - 'wc/v2' => 'WC_Rest_API_V2', - 'wc/v3' => 'WC_Rest_API_V3', - 'wc-blocks/v1' => 'WC_Rest_API_Blocks_V1', - ); - } - - /** - * Register REST API routes. - */ - public function register_rest_routes() { - foreach ( $this->get_namespaces() as $namespace => $classname ) { - $api = new $classname(); - $api->includes(); - - foreach ( $api->get_controllers() as $controller ) { - $this->$controller = new $controller(); - $this->$controller->register_routes(); - } - } - } -} diff --git a/includes/v1/class-wc-rest-api-v1.php b/includes/v1/class-wc-rest-api-v1.php deleted file mode 100644 index b5038186261..00000000000 --- a/includes/v1/class-wc-rest-api-v1.php +++ /dev/null @@ -1,75 +0,0 @@ -WooCommerce dev PHP_CodeSniffer ruleset. - tests/cli/ + tests/ apigen/ - includes/libraries/ - includes/legacy/ - includes/api/legacy/ - includes/api/v1/ - includes/class-wc-geo-ip.php - includes/wc-deprecated-functions.php */node_modules/* */vendor/* - + @@ -32,7 +26,6 @@ - includes/**/abstract-*.php tests/* @@ -40,10 +33,6 @@ tests/ - - tests/e2e-tests/ - - i18n/ diff --git a/readme.txt b/readme.txt deleted file mode 100644 index 1333ed77b7e..00000000000 --- a/readme.txt +++ /dev/null @@ -1 +0,0 @@ -TODO diff --git a/src/class-autoload.php b/src/class-autoload.php new file mode 100644 index 00000000000..8c0c2d1ab72 --- /dev/null +++ b/src/class-autoload.php @@ -0,0 +1,75 @@ +get_file_name_from_class( $class ); + $dir = trailingslashit( dirname( __FILE__ ) ); + + // Non-namespaced files. + if ( stristr( $class, 'WC_REST_' ) ) { + if ( stristr( $class, 'WC_REST_Blocks' ) ) { + $subdir = 'wc-blocks/'; + } elseif ( stristr( $class, '_V1_' ) ) { + $subdir = 'v1/'; + } elseif ( stristr( $class, '_V2_' ) ) { + $subdir = 'v2/'; + } else { + $subdir = 'v3/'; + } + + if ( file_exists( $dir . $subdir . $file ) ) { + include $dir . $subdir . $file; + return; + } + } + + if ( file_exists( $dir . $class . '.php' ) ) { + include $dir . $class . '.php'; + return; + } + + if ( file_exists( $dir . $file ) ) { + include $dir . $file; + return; + } + } +} diff --git a/src/class-server.php b/src/class-server.php new file mode 100644 index 00000000000..da232e7475c --- /dev/null +++ b/src/class-server.php @@ -0,0 +1,184 @@ +init(); + add_action( 'rest_api_init', array( $this, 'register_rest_routes' ), 10 ); + } + + /** + * Register REST API routes. + */ + public function register_rest_routes() { + foreach ( $this->get_namespaces() as $namespace ) { + $this->endpoints[ $namespace ] = array(); + + foreach ( $this->get_controllers( $namespace ) as $name => $class ) { + $this->endpoints[ $namespace ][ $class ] = new $class(); + $this->endpoints[ $namespace ][ $class ]->register_routes(); + } + } + } + + /** + * Get API namespaces - new namespaces should be registered here. + * + * @return array + */ + protected function get_namespaces() { + return array( + 'wc/v1', + 'wc/v2', + 'wc/v3', + 'wc-blocks/v1', + ); + } + + /** + * Get API controllers - new controllers/endpoints should be registered here. + * + * @param string $namespace Namespace to get controllers for. + * @return array + */ + protected function get_controllers( $namespace ) { + switch ( $namespace ) { + case 'wc/v1': + return array( + 'WC_REST_Coupons_V1_Controller', + 'WC_REST_Customer_Downloads_V1_Controller', + 'WC_REST_Customers_V1_Controller', + 'WC_REST_Order_Notes_V1_Controller', + 'WC_REST_Order_Refunds_V1_Controller', + 'WC_REST_Orders_V1_Controller', + 'WC_REST_Product_Attribute_Terms_V1_Controller', + 'WC_REST_Product_Attributes_V1_Controller', + 'WC_REST_Product_Categories_V1_Controller', + 'WC_REST_Product_Reviews_V1_Controller', + 'WC_REST_Product_Shipping_Classes_V1_Controller', + 'WC_REST_Product_Tags_V1_Controller', + 'WC_REST_Products_V1_Controller', + 'WC_REST_Report_Sales_V1_Controller', + 'WC_REST_Report_Top_Sellers_V1_Controller', + 'WC_REST_Reports_V1_Controller', + 'WC_REST_Tax_Classes_V1_Controller', + 'WC_REST_Taxes_V1_Controller', + 'WC_REST_Webhook_Deliveries_V1_Controller', + 'WC_REST_Webhooks_V1_Controller', + ); + case 'wc/v2': + return array( + 'WC_REST_Coupons_V2_Controller', + 'WC_REST_Customer_Downloads_V2_Controller', + 'WC_REST_Customers_V2_Controller', + 'WC_REST_Network_Orders_V2_Controller', + 'WC_REST_Order_Notes_V2_Controller', + 'WC_REST_Order_Refunds_V2_Controller', + 'WC_REST_Orders_V2_Controller', + 'WC_REST_Product_Attribute_Terms_V2_Controller', + 'WC_REST_Product_Attributes_V2_Controller', + 'WC_REST_Product_Categories_V2_Controller', + 'WC_REST_Product_Reviews_V2_Controller', + 'WC_REST_Product_Shipping_Classes_V2_Controller', + 'WC_REST_Product_Tags_V2_Controller', + 'WC_REST_Products_V2_Controller', + 'WC_REST_Product_Variations_V2_Controller', + 'WC_REST_Report_Sales_V2_Controller', + 'WC_REST_Report_Top_Sellers_V2_Controller', + 'WC_REST_Reports_V2_Controller', + 'WC_REST_Settings_V2_Controller', + 'WC_REST_Setting_Options_V2_Controller', + 'WC_REST_Shipping_Zones_V2_Controller', + 'WC_REST_Shipping_Zone_Locations_V2_Controller', + 'WC_REST_Shipping_Zone_Methods_V2_Controller', + 'WC_REST_Tax_Classes_V2_Controller', + 'WC_REST_Taxes_V2_Controller', + 'WC_REST_Webhook_Deliveries_V2_Controller', + 'WC_REST_Webhooks_V2_Controller', + 'WC_REST_System_Status_V2_Controller', + 'WC_REST_System_Status_Tools_V2_Controller', + 'WC_REST_Shipping_Methods_V2_Controller', + 'WC_REST_Payment_Gateways_V2_Controller', + ); + case 'wc/v3': + return array( + 'WC_REST_Coupons_Controller', + 'WC_REST_Customer_Downloads_Controller', + 'WC_REST_Customers_Controller', + 'WC_REST_Network_Orders_Controller', + 'WC_REST_Order_Notes_Controller', + 'WC_REST_Order_Refunds_Controller', + 'WC_REST_Orders_Controller', + 'WC_REST_Product_Attribute_Terms_Controller', + 'WC_REST_Product_Attributes_Controller', + 'WC_REST_Product_Categories_Controller', + 'WC_REST_Product_Reviews_Controller', + 'WC_REST_Product_Shipping_Classes_Controller', + 'WC_REST_Product_Tags_Controller', + 'WC_REST_Products_Controller', + 'WC_REST_Product_Variations_Controller', + 'WC_REST_Report_Sales_Controller', + 'WC_REST_Report_Top_Sellers_Controller', + 'WC_REST_Report_Orders_Totals_Controller', + 'WC_REST_Report_Products_Totals_Controller', + 'WC_REST_Report_Customers_Totals_Controller', + 'WC_REST_Report_Coupons_Totals_Controller', + 'WC_REST_Report_Reviews_Totals_Controller', + 'WC_REST_Reports_Controller', + 'WC_REST_Settings_Controller', + 'WC_REST_Setting_Options_Controller', + 'WC_REST_Shipping_Zones_Controller', + 'WC_REST_Shipping_Zone_Locations_Controller', + 'WC_REST_Shipping_Zone_Methods_Controller', + 'WC_REST_Tax_Classes_Controller', + 'WC_REST_Taxes_Controller', + 'WC_REST_Webhooks_Controller', + 'WC_REST_System_Status_Controller', + 'WC_REST_System_Status_Tools_Controller', + 'WC_REST_Shipping_Methods_Controller', + 'WC_REST_Payment_Gateways_Controller', + 'WC_REST_Data_Controller', + 'WC_REST_Data_Continents_Controller', + 'WC_REST_Data_Countries_Controller', + 'WC_REST_Data_Currencies_Controller', + ); + case 'wc-blocks/v1': + return array( + 'WC_REST_Blocks_Product_Attributes_Controller', + 'WC_REST_Blocks_Product_Attribute_Terms_Controller', + 'WC_REST_Blocks_Product_Categories_Controller', + 'WC_REST_Blocks_Products_Controller', + ); + } + return array(); + } +} diff --git a/src/class-singleton.php b/src/class-singleton.php new file mode 100644 index 00000000000..73a7c4f72c6 --- /dev/null +++ b/src/class-singleton.php @@ -0,0 +1,51 @@ +init(); + function wc_rest_api_1_dot_0_dot_0_dev() { + require_once dirname( __FILE__ ) . '/src/class-server.php'; + \WooCommerce\Rest_Api\Server::instance()->init(); } } @@ -35,7 +35,7 @@ add_action( function() { if ( is_callable( array( wc()->api, 'register' ) ) ) { // @internal When bumping the version remember to update the function names below. - wc()->api->register( '1.1.0', 'wc_rest_api_1_dot_1_dot_0' ); + wc()->api->register( '1.1.0', 'wc_rest_api_1_dot_0_dot_0_dev' ); } } );