diff --git a/includes/class-wc-api.php b/includes/class-wc-api.php index 1bd0c4a6129..eb33cbf35f9 100644 --- a/includes/class-wc-api.php +++ b/includes/class-wc-api.php @@ -1,8 +1,8 @@ is_rest_api_loaded() || version_compare( PHP_VERSION, '5.6.0', '<' ) ) { - return; - } - \Automattic\WooCommerce\RestApi\Server::instance()->init(); - } - /** * Get the version of the REST API package being ran. * @@ -82,7 +69,7 @@ class WC_API extends WC_Legacy_API { */ public function get_endpoint_data( $endpoint, $params = array() ) { if ( ! $this->is_rest_api_loaded() ) { - $this->rest_api_init(); + return new WP_Error( 'rest_api_unavailable', __( 'The Rest API is unavailable.', 'woocommerce' ) ); } $request = new \WP_REST_Request( 'GET', $endpoint ); if ( $params ) { diff --git a/includes/class-woocommerce.php b/includes/class-woocommerce.php index f45b3636ae8..09f2df8c1c1 100644 --- a/includes/class-woocommerce.php +++ b/includes/class-woocommerce.php @@ -180,6 +180,7 @@ final class WooCommerce { register_shutdown_function( array( $this, 'log_errors' ) ); add_action( 'plugins_loaded', array( $this, 'on_plugins_loaded' ), -1 ); + add_action( 'plugins_loaded', array( $this, 'init_packages' ) ); add_action( 'after_setup_theme', array( $this, 'setup_environment' ) ); add_action( 'after_setup_theme', array( $this, 'include_template_functions' ), 11 ); add_action( 'init', array( $this, 'init' ), 0 ); @@ -191,6 +192,21 @@ final class WooCommerce { add_action( 'deactivated_plugin', array( $this, 'deactivated_plugin' ) ); } + /** + * Load any WooCommerce packages. + * + * Most packages will have their own init routines, so this is used early once plugins are loaded. + * + * @since 3.7.0 + */ + public function init_packages() { + if ( version_compare( PHP_VERSION, '5.6.0', '<' ) ) { + return; + } + \Automattic\WooCommerce\Blocks\Library::instance()->init(); + \Automattic\WooCommerce\RestApi\Server::instance()->init(); + } + /** * Ensures fatal errors are logged so they can be picked up in the status report. * @@ -540,11 +556,6 @@ final class WooCommerce { $this->deprecated_hook_handlers['actions'] = new WC_Deprecated_Action_Hooks(); $this->deprecated_hook_handlers['filters'] = new WC_Deprecated_Filter_Hooks(); - // Init any packages. - if ( version_compare( PHP_VERSION, '5.6.0', '>=' ) ) { - \Automattic\WooCommerce\Blocks\Library::instance()->init(); - } - // Classes/actions loaded for the frontend and for ajax requests. if ( $this->is_request( 'frontend' ) ) { wc_load_cart(); diff --git a/includes/legacy/class-wc-legacy-api.php b/includes/legacy/class-wc-legacy-api.php index c10cccb3bf2..f99eae731d5 100644 --- a/includes/legacy/class-wc-legacy-api.php +++ b/includes/legacy/class-wc-legacy-api.php @@ -271,6 +271,13 @@ class WC_Legacy_API { $this->server->serve_request(); } + /** + * Rest API Init. + * + * @deprecated since 3.7.0 - REST API clases autoload. + */ + public function rest_api_init() {} + /** * Include REST API classes. *