diff --git a/includes/api/src/Package.php b/includes/api/src/Package.php index 1f2ba8b8f07..05cda53a193 100644 --- a/includes/api/src/Package.php +++ b/includes/api/src/Package.php @@ -1,5 +1,7 @@ init() */ public static function init() { + wc_deprecated_function( 'Automattic\WooCommerce\RestApi\Server::instance()->init()', '4.5.0' ); \Automattic\WooCommerce\RestApi\Server::instance()->init(); } /** * Return the version of the package. * + * @deprecated since 4.5.0. This tracks WooCommerce version now. * @return string */ public static function get_version() { - return self::VERSION; + wc_deprecated_function( 'WC()->version', '4.5.0' ); + return WC()->version; } /** * Return the path to the package. * + * @deprecated since 4.5.0. Directly call Automattic\WooCommerce\RestApi\Server::get_path() * @return string */ public static function get_path() { - return dirname( __DIR__ ); + wc_deprecated_function( 'Automattic\WooCommerce\RestApi\Server::get_path()', '4.5.0' ); + return \Automattic\WooCommerce\RestApi\Server::get_path(); } } diff --git a/includes/api/src/Server.php b/includes/api/src/Server.php index 1c589c0da75..4eb3384baa3 100644 --- a/includes/api/src/Server.php +++ b/includes/api/src/Server.php @@ -22,7 +22,7 @@ class Server { * * @var array */ - protected $controllers = []; + protected $controllers = array(); /** * Hook into WordPress ready to init the REST API as needed. @@ -51,11 +51,11 @@ class Server { protected function get_rest_namespaces() { return apply_filters( 'woocommerce_rest_api_get_rest_namespaces', - [ + array( 'wc/v1' => $this->get_v1_controllers(), 'wc/v2' => $this->get_v2_controllers(), 'wc/v3' => $this->get_v3_controllers(), - ] + ) ); } @@ -65,7 +65,7 @@ class Server { * @return array */ protected function get_v1_controllers() { - return [ + return array( 'coupons' => 'WC_REST_Coupons_V1_Controller', 'customer-downloads' => 'WC_REST_Customer_Downloads_V1_Controller', 'customers' => 'WC_REST_Customers_V1_Controller', @@ -86,7 +86,7 @@ class Server { 'taxes' => 'WC_REST_Taxes_V1_Controller', 'webhooks' => 'WC_REST_Webhooks_V1_Controller', 'webhook-deliveries' => 'WC_REST_Webhook_Deliveries_V1_Controller', - ]; + ); } /** @@ -95,7 +95,7 @@ class Server { * @return array */ protected function get_v2_controllers() { - return [ + return array( 'coupons' => 'WC_REST_Coupons_V2_Controller', 'customer-downloads' => 'WC_REST_Customer_Downloads_V2_Controller', 'customers' => 'WC_REST_Customers_V2_Controller', @@ -127,7 +127,7 @@ class Server { 'system-status-tools' => 'WC_REST_System_Status_Tools_V2_Controller', 'shipping-methods' => 'WC_REST_Shipping_Methods_V2_Controller', 'payment-gateways' => 'WC_REST_Payment_Gateways_V2_Controller', - ]; + ); } /** @@ -136,7 +136,7 @@ class Server { * @return array */ protected function get_v3_controllers() { - return [ + return array( 'coupons' => 'WC_REST_Coupons_Controller', 'customer-downloads' => 'WC_REST_Customer_Downloads_Controller', 'customers' => 'WC_REST_Customers_Controller', @@ -176,6 +176,15 @@ class Server { 'data-continents' => 'WC_REST_Data_Continents_Controller', 'data-countries' => 'WC_REST_Data_Countries_Controller', 'data-currencies' => 'WC_REST_Data_Currencies_Controller', - ]; + ); + } + + /** + * Return the path to the package. + * + * @return string + */ + public static function get_path() { + return dirname( __DIR__ ); } } diff --git a/includes/class-wc-api.php b/includes/class-wc-api.php index 629e82feec1..5347b5f7499 100644 --- a/includes/class-wc-api.php +++ b/includes/class-wc-api.php @@ -39,6 +39,14 @@ class WC_API extends WC_Legacy_API { if ( ! $this->is_rest_api_loaded() ) { return null; } + if ( method_exists( \Automattic\WooCommerce\RestApi\Server::class, 'get_path' ) ) { + $path = \Automattic\WooCommerce\RestApi\Server::get_path(); + if ( 0 === strpos( $path, __DIR__ ) ) { + // We are loading API from included version. + return WC()->version; + } + } + // We are loading API from external plugin. return \Automattic\WooCommerce\RestApi\Package::get_version(); } @@ -52,7 +60,7 @@ class WC_API extends WC_Legacy_API { if ( ! $this->is_rest_api_loaded() ) { return null; } - return \Automattic\WooCommerce\RestApi\Package::get_path(); + return \Automattic\WooCommerce\RestApi\Server::get_path(); } /** @@ -62,7 +70,7 @@ class WC_API extends WC_Legacy_API { * @return boolean */ protected function is_rest_api_loaded() { - return class_exists( '\Automattic\WooCommerce\RestApi\Package', false ); + return class_exists( '\Automattic\WooCommerce\RestApi\Server', false ); } /** diff --git a/includes/class-woocommerce.php b/includes/class-woocommerce.php index 146376c31f2..9908924d103 100644 --- a/includes/class-woocommerce.php +++ b/includes/class-woocommerce.php @@ -303,7 +303,7 @@ final class WooCommerce { * Load REST API. */ public function load_rest_api() { - \Automattic\WooCommerce\RestApi\Package::init(); + \Automattic\WooCommerce\RestApi\Server::instance()->init(); } /**