Package init method

This commit is contained in:
Mike Jolley 2019-06-21 17:28:46 +01:00
parent e3dc79481d
commit d31f6f70a2
3 changed files with 26 additions and 21 deletions

View File

@ -1,8 +1,8 @@
<?php
/**
* WooCommerce API class loader.
* WC-API endpoint handler.
*
* This handles APIs in WooCommerce. These include:
* This handles API related functionality in WooCommerce.
* - wc-api endpoint - Commonly used by Payment gateways for callbacks.
* - Legacy REST API - Deprecated in 2.6.0. @see class-wc-legacy-api.php
* - WP REST API - The main REST API in WooCommerce which is built on top of the WP REST API.
@ -24,24 +24,11 @@ class WC_API extends WC_Legacy_API {
public function init() {
parent::init();
add_action( 'init', array( $this, 'add_endpoint' ), 0 );
add_action( 'init', array( $this, 'rest_api_init' ) );
add_filter( 'query_vars', array( $this, 'add_query_vars' ), 0 );
add_action( 'parse_request', array( $this, 'handle_api_requests' ), 0 );
add_action( 'rest_api_init', array( $this, 'register_wp_admin_settings' ) );
}
/**
* Call the Server class from the Rest API package.
*
* @see \Automattic\WooCommerce\RestApi\Server
*/
public function rest_api_init() {
if ( $this->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 ) {

View File

@ -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();

View File

@ -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.
*