Move legacy API code into WC_Legacy_API

@claudiosmweb to keep legacy api separated from the new REST API code.
This commit is contained in:
Mike Jolley 2016-06-07 11:03:16 +01:00
parent ced7a099f1
commit 1185333b56
7 changed files with 304 additions and 280 deletions

View File

@ -9,6 +9,8 @@ filter:
- includes/updates/*
- includes/gateways/simplify-commerce/*
- includes/shipping/legacy-*
- includes/wc-deprecated-functions.php
- includes/class-wc-legacy-api.php
checks:
php:

View File

@ -515,11 +515,6 @@ global $wpdb;
<td class="help"><?php echo wc_help_tip( __( 'Does your site have REST API enabled?', 'woocommerce' ) ); ?></td>
<td><?php echo 'yes' === get_option( 'woocommerce_api_enabled' ) ? '<mark class="yes"><span class="dashicons dashicons-yes"></span></mark>' : '<mark class="no">&ndash;</mark>'; ?></td>
</tr>
<tr>
<td data-export-label="API Version"><?php _e( 'API Version', 'woocommerce' ); ?>:</td>
<td class="help"><?php echo wc_help_tip( __( 'What version of the REST API does your site use?', 'woocommerce' ) ); ?></td>
<td><?php echo esc_html( WC_API::VERSION ); ?></td>
</tr>
</tbody>
</table>
<table class="wc_status_table widefat" cellspacing="0">

View File

@ -14,49 +14,25 @@ if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'WC_API' ) ) :
if ( ! class_exists( 'WC_Legacy_API' ) ) {
include_once( 'class-wc-legacy-api.php' );
}
class WC_API {
/**
* This is the major version for the REST API and takes
* first-order position in endpoint URLs.
*
* @deprecated 2.6.0
* @var string
*/
const VERSION = '3.1.0';
/**
* The REST API server.
*
* @deprecated 2.6.0
* @var WC_API_Server
*/
public $server;
/**
* REST API authentication class instance.
*
* @deprecated 2.6.0
* @var WC_API_Authentication
*/
public $authentication;
class WC_API extends WC_Legacy_API {
/**
* Setup class.
* @since 2.0
*/
public function __construct() {
parent::__construct();
// Add query vars.
add_filter( 'query_vars', array( $this, 'add_query_vars' ), 0 );
// Register API endpoints.
add_action( 'init', array( $this, 'add_endpoint' ), 0 );
// Handle REST API requests.
add_action( 'parse_request', array( $this, 'handle_rest_api_requests' ), 0 );
// Handle wc-api endpoint requests.
add_action( 'parse_request', array( $this, 'handle_api_requests' ), 0 );
@ -75,227 +51,20 @@ class WC_API {
* @return string[]
*/
public function add_query_vars( $vars ) {
$vars = parent::add_query_vars( $vars );
$vars[] = 'wc-api';
$vars[] = 'wc-api-version'; // Deprecated since 2.6.0.
$vars[] = 'wc-api-route'; // Deprecated since 2.6.0.
return $vars;
}
/**
* Add new endpoints.
*
* WC API for payment gateway IPNs, etc.
* @since 2.0
*/
public static function add_endpoint() {
// REST API, deprecated since 2.6.0.
add_rewrite_rule( '^wc-api/v([1-3]{1})/?$', 'index.php?wc-api-version=$matches[1]&wc-api-route=/', 'top' );
add_rewrite_rule( '^wc-api/v([1-3]{1})(.*)?', 'index.php?wc-api-version=$matches[1]&wc-api-route=$matches[2]', 'top' );
// WC API for payment gateway IPNs, etc.
parent::add_endpoint();
add_rewrite_endpoint( 'wc-api', EP_ALL );
}
/**
* Handle REST API requests.
*
* @since 2.2
* @deprecated 2.6.0
*/
public function handle_rest_api_requests() {
global $wp;
if ( ! empty( $_GET['wc-api-version'] ) ) {
$wp->query_vars['wc-api-version'] = $_GET['wc-api-version'];
}
if ( ! empty( $_GET['wc-api-route'] ) ) {
$wp->query_vars['wc-api-route'] = $_GET['wc-api-route'];
}
// REST API request.
if ( ! empty( $wp->query_vars['wc-api-version'] ) && ! empty( $wp->query_vars['wc-api-route'] ) ) {
define( 'WC_API_REQUEST', true );
define( 'WC_API_REQUEST_VERSION', absint( $wp->query_vars['wc-api-version'] ) );
// Legacy v1 API request.
if ( 1 === WC_API_REQUEST_VERSION ) {
$this->handle_v1_rest_api_request();
} else if ( 2 === WC_API_REQUEST_VERSION ) {
$this->handle_v2_rest_api_request();
} else {
$this->includes();
$this->server = new WC_API_Server( $wp->query_vars['wc-api-route'] );
// load API resource classes.
$this->register_resources( $this->server );
// Fire off the request.
$this->server->serve_request();
}
exit;
}
}
/**
* Include required files for REST API request.
*
* @since 2.1
* @deprecated 2.6.0
*/
public function includes() {
// API server / response handlers.
include_once( 'api/legacy/v3/class-wc-api-exception.php' );
include_once( 'api/legacy/v3/class-wc-api-server.php' );
include_once( 'api/legacy/v3/interface-wc-api-handler.php' );
include_once( 'api/legacy/v3/class-wc-api-json-handler.php' );
// Authentication.
include_once( 'api/legacy/v3/class-wc-api-authentication.php' );
$this->authentication = new WC_API_Authentication();
include_once( 'api/legacy/v3/class-wc-api-resource.php' );
include_once( 'api/legacy/v3/class-wc-api-coupons.php' );
include_once( 'api/legacy/v3/class-wc-api-customers.php' );
include_once( 'api/legacy/v3/class-wc-api-orders.php' );
include_once( 'api/legacy/v3/class-wc-api-products.php' );
include_once( 'api/legacy/v3/class-wc-api-reports.php' );
include_once( 'api/legacy/v3/class-wc-api-taxes.php' );
include_once( 'api/legacy/v3/class-wc-api-webhooks.php' );
// Allow plugins to load other response handlers or resource classes.
do_action( 'woocommerce_api_loaded' );
}
/**
* Register available API resources.
*
* @since 2.1
* @deprecated 2.6.0
* @param WC_API_Server $server the REST server
*/
public function register_resources( $server ) {
$api_classes = apply_filters( 'woocommerce_api_classes',
array(
'WC_API_Coupons',
'WC_API_Customers',
'WC_API_Orders',
'WC_API_Products',
'WC_API_Reports',
'WC_API_Taxes',
'WC_API_Webhooks',
)
);
foreach ( $api_classes as $api_class ) {
$this->$api_class = new $api_class( $server );
}
}
/**
* Handle legacy v1 REST API requests.
*
* @since 2.2
* @deprecated 2.6.0
*/
private function handle_v1_rest_api_request() {
// Include legacy required files for v1 REST API request.
include_once( 'api/legacy/v1/class-wc-api-server.php' );
include_once( 'api/legacy/v1/interface-wc-api-handler.php' );
include_once( 'api/legacy/v1/class-wc-api-json-handler.php' );
include_once( 'api/legacy/v1/class-wc-api-xml-handler.php' );
include_once( 'api/legacy/v1/class-wc-api-authentication.php' );
$this->authentication = new WC_API_Authentication();
include_once( 'api/legacy/v1/class-wc-api-resource.php' );
include_once( 'api/legacy/v1/class-wc-api-coupons.php' );
include_once( 'api/legacy/v1/class-wc-api-customers.php' );
include_once( 'api/legacy/v1/class-wc-api-orders.php' );
include_once( 'api/legacy/v1/class-wc-api-products.php' );
include_once( 'api/legacy/v1/class-wc-api-reports.php' );
// Allow plugins to load other response handlers or resource classes.
do_action( 'woocommerce_api_loaded' );
$this->server = new WC_API_Server( $GLOBALS['wp']->query_vars['wc-api-route'] );
// Register available resources for legacy v1 REST API request.
$api_classes = apply_filters( 'woocommerce_api_classes',
array(
'WC_API_Customers',
'WC_API_Orders',
'WC_API_Products',
'WC_API_Coupons',
'WC_API_Reports',
)
);
foreach ( $api_classes as $api_class ) {
$this->$api_class = new $api_class( $this->server );
}
// Fire off the request.
$this->server->serve_request();
}
/**
* Handle legacy v2 REST API requests.
*
* @since 2.4
* @deprecated 2.6.0
*/
private function handle_v2_rest_api_request() {
include_once( 'api/legacy/v2/class-wc-api-exception.php' );
include_once( 'api/legacy/v2/class-wc-api-server.php' );
include_once( 'api/legacy/v2/interface-wc-api-handler.php' );
include_once( 'api/legacy/v2/class-wc-api-json-handler.php' );
include_once( 'api/legacy/v2/class-wc-api-authentication.php' );
$this->authentication = new WC_API_Authentication();
include_once( 'api/legacy/v2/class-wc-api-resource.php' );
include_once( 'api/legacy/v2/class-wc-api-coupons.php' );
include_once( 'api/legacy/v2/class-wc-api-customers.php' );
include_once( 'api/legacy/v2/class-wc-api-orders.php' );
include_once( 'api/legacy/v2/class-wc-api-products.php' );
include_once( 'api/legacy/v2/class-wc-api-reports.php' );
include_once( 'api/legacy/v2/class-wc-api-webhooks.php' );
// allow plugins to load other response handlers or resource classes.
do_action( 'woocommerce_api_loaded' );
$this->server = new WC_API_Server( $GLOBALS['wp']->query_vars['wc-api-route'] );
// Register available resources for legacy v2 REST API request.
$api_classes = apply_filters( 'woocommerce_api_classes',
array(
'WC_API_Customers',
'WC_API_Orders',
'WC_API_Products',
'WC_API_Coupons',
'WC_API_Reports',
'WC_API_Webhooks',
)
);
foreach ( $api_classes as $api_class ) {
$this->$api_class = new $api_class( $this->server );
}
// Fire off the request.
$this->server->serve_request();
}
/**
* API request - Trigger any API requests.
*
@ -338,7 +107,6 @@ class WC_API {
/**
* Init WP REST API.
*
* @since 2.6.0
*/
private function rest_api_init() {
@ -357,7 +125,6 @@ class WC_API {
/**
* Include REST API classes.
*
* @since 2.6.0
*/
private function rest_api_includes() {
@ -403,7 +170,6 @@ class WC_API {
/**
* Register REST API routes.
*
* @since 2.6.0
*/
public function register_rest_routes() {
@ -436,7 +202,3 @@ class WC_API {
}
}
}
endif;
return new WC_API();

View File

@ -0,0 +1,271 @@
<?php
/**
* WooCommerce Legacy API. Was deprecated with 2.6.0.
*
* @author WooThemes
* @category API
* @package WooCommerce/API
* @since 2.6
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class WC_Legacy_API {
/**
* This is the major version for the REST API and takes
* first-order position in endpoint URLs.
*
* @deprecated 2.6.0
* @var string
*/
const VERSION = '3.1.0';
/**
* The REST API server.
*
* @deprecated 2.6.0
* @var WC_API_Server
*/
public $server;
/**
* REST API authentication class instance.
*
* @deprecated 2.6.0
* @var WC_API_Authentication
*/
public $authentication;
/**
* Setup class.
* @since 2.0
*/
public function __construct() {
add_action( 'parse_request', array( $this, 'handle_rest_api_requests' ), 0 );
}
/**
* Add new query vars.
*
* @since 2.0
* @param array $vars
* @return string[]
*/
public function add_query_vars( $vars ) {
$vars[] = 'wc-api-version'; // Deprecated since 2.6.0.
$vars[] = 'wc-api-route'; // Deprecated since 2.6.0.
return $vars;
}
/**
* Add new endpoints.
*
* @since 2.0
*/
public static function add_endpoint() {
// REST API, deprecated since 2.6.0.
add_rewrite_rule( '^wc-api/v([1-3]{1})/?$', 'index.php?wc-api-version=$matches[1]&wc-api-route=/', 'top' );
add_rewrite_rule( '^wc-api/v([1-3]{1})(.*)?', 'index.php?wc-api-version=$matches[1]&wc-api-route=$matches[2]', 'top' );
}
/**
* Handle REST API requests.
*
* @since 2.2
* @deprecated 2.6.0
*/
public function handle_rest_api_requests() {
global $wp;
if ( ! empty( $_GET['wc-api-version'] ) ) {
$wp->query_vars['wc-api-version'] = $_GET['wc-api-version'];
}
if ( ! empty( $_GET['wc-api-route'] ) ) {
$wp->query_vars['wc-api-route'] = $_GET['wc-api-route'];
}
// REST API request.
if ( ! empty( $wp->query_vars['wc-api-version'] ) && ! empty( $wp->query_vars['wc-api-route'] ) ) {
define( 'WC_API_REQUEST', true );
define( 'WC_API_REQUEST_VERSION', absint( $wp->query_vars['wc-api-version'] ) );
// Legacy v1 API request.
if ( 1 === WC_API_REQUEST_VERSION ) {
$this->handle_v1_rest_api_request();
} else if ( 2 === WC_API_REQUEST_VERSION ) {
$this->handle_v2_rest_api_request();
} else {
$this->includes();
$this->server = new WC_API_Server( $wp->query_vars['wc-api-route'] );
// load API resource classes.
$this->register_resources( $this->server );
// Fire off the request.
$this->server->serve_request();
}
exit;
}
}
/**
* Include required files for REST API request.
*
* @since 2.1
* @deprecated 2.6.0
*/
public function includes() {
// API server / response handlers.
include_once( 'api/legacy/v3/class-wc-api-exception.php' );
include_once( 'api/legacy/v3/class-wc-api-server.php' );
include_once( 'api/legacy/v3/interface-wc-api-handler.php' );
include_once( 'api/legacy/v3/class-wc-api-json-handler.php' );
// Authentication.
include_once( 'api/legacy/v3/class-wc-api-authentication.php' );
$this->authentication = new WC_API_Authentication();
include_once( 'api/legacy/v3/class-wc-api-resource.php' );
include_once( 'api/legacy/v3/class-wc-api-coupons.php' );
include_once( 'api/legacy/v3/class-wc-api-customers.php' );
include_once( 'api/legacy/v3/class-wc-api-orders.php' );
include_once( 'api/legacy/v3/class-wc-api-products.php' );
include_once( 'api/legacy/v3/class-wc-api-reports.php' );
include_once( 'api/legacy/v3/class-wc-api-taxes.php' );
include_once( 'api/legacy/v3/class-wc-api-webhooks.php' );
// Allow plugins to load other response handlers or resource classes.
do_action( 'woocommerce_api_loaded' );
}
/**
* Register available API resources.
*
* @since 2.1
* @deprecated 2.6.0
* @param WC_API_Server $server the REST server
*/
public function register_resources( $server ) {
$api_classes = apply_filters( 'woocommerce_api_classes',
array(
'WC_API_Coupons',
'WC_API_Customers',
'WC_API_Orders',
'WC_API_Products',
'WC_API_Reports',
'WC_API_Taxes',
'WC_API_Webhooks',
)
);
foreach ( $api_classes as $api_class ) {
$this->$api_class = new $api_class( $server );
}
}
/**
* Handle legacy v1 REST API requests.
*
* @since 2.2
* @deprecated 2.6.0
*/
private function handle_v1_rest_api_request() {
// Include legacy required files for v1 REST API request.
include_once( 'api/legacy/v1/class-wc-api-server.php' );
include_once( 'api/legacy/v1/interface-wc-api-handler.php' );
include_once( 'api/legacy/v1/class-wc-api-json-handler.php' );
include_once( 'api/legacy/v1/class-wc-api-xml-handler.php' );
include_once( 'api/legacy/v1/class-wc-api-authentication.php' );
$this->authentication = new WC_API_Authentication();
include_once( 'api/legacy/v1/class-wc-api-resource.php' );
include_once( 'api/legacy/v1/class-wc-api-coupons.php' );
include_once( 'api/legacy/v1/class-wc-api-customers.php' );
include_once( 'api/legacy/v1/class-wc-api-orders.php' );
include_once( 'api/legacy/v1/class-wc-api-products.php' );
include_once( 'api/legacy/v1/class-wc-api-reports.php' );
// Allow plugins to load other response handlers or resource classes.
do_action( 'woocommerce_api_loaded' );
$this->server = new WC_API_Server( $GLOBALS['wp']->query_vars['wc-api-route'] );
// Register available resources for legacy v1 REST API request.
$api_classes = apply_filters( 'woocommerce_api_classes',
array(
'WC_API_Customers',
'WC_API_Orders',
'WC_API_Products',
'WC_API_Coupons',
'WC_API_Reports',
)
);
foreach ( $api_classes as $api_class ) {
$this->$api_class = new $api_class( $this->server );
}
// Fire off the request.
$this->server->serve_request();
}
/**
* Handle legacy v2 REST API requests.
*
* @since 2.4
* @deprecated 2.6.0
*/
private function handle_v2_rest_api_request() {
include_once( 'api/legacy/v2/class-wc-api-exception.php' );
include_once( 'api/legacy/v2/class-wc-api-server.php' );
include_once( 'api/legacy/v2/interface-wc-api-handler.php' );
include_once( 'api/legacy/v2/class-wc-api-json-handler.php' );
include_once( 'api/legacy/v2/class-wc-api-authentication.php' );
$this->authentication = new WC_API_Authentication();
include_once( 'api/legacy/v2/class-wc-api-resource.php' );
include_once( 'api/legacy/v2/class-wc-api-coupons.php' );
include_once( 'api/legacy/v2/class-wc-api-customers.php' );
include_once( 'api/legacy/v2/class-wc-api-orders.php' );
include_once( 'api/legacy/v2/class-wc-api-products.php' );
include_once( 'api/legacy/v2/class-wc-api-reports.php' );
include_once( 'api/legacy/v2/class-wc-api-webhooks.php' );
// allow plugins to load other response handlers or resource classes.
do_action( 'woocommerce_api_loaded' );
$this->server = new WC_API_Server( $GLOBALS['wp']->query_vars['wc-api-route'] );
// Register available resources for legacy v2 REST API request.
$api_classes = apply_filters( 'woocommerce_api_classes',
array(
'WC_API_Customers',
'WC_API_Orders',
'WC_API_Products',
'WC_API_Coupons',
'WC_API_Reports',
'WC_API_Webhooks',
)
);
foreach ( $api_classes as $api_class ) {
$this->$api_class = new $api_class( $this->server );
}
// Fire off the request.
$this->server->serve_request();
}
}

View File

@ -13,8 +13,6 @@ if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'WC_Query' ) ) :
/**
* WC_Query Class.
*/
@ -698,7 +696,3 @@ class WC_Query {
_deprecated_function( 'layered_nav_query', '2.6', '' );
}
}
endif;
return new WC_Query();

View File

@ -742,9 +742,7 @@ function wc_setcookie( $name, $value, $expire = 0, $secure = false ) {
* @return string the URL.
*/
function get_woocommerce_api_url( $path ) {
$_version = substr( WC_API::VERSION, 0, 1 );
$version = defined( 'WC_API_REQUEST_VERSION' ) ? WC_API_REQUEST_VERSION : $_version;
$version = defined( 'WC_API_REQUEST_VERSION' ) ? WC_API_REQUEST_VERSION : substr( WC_API::VERSION, 0, 1 );
$url = get_home_url( null, "wc-api/v{$version}/", is_ssl() ? 'https' : 'http' );

View File

@ -251,31 +251,33 @@ final class WooCommerce {
include_once( 'includes/class-wc-tracker.php' );
}
$this->query = include( 'includes/class-wc-query.php' ); // The main query class
$this->api = include( 'includes/class-wc-api.php' ); // API Class
include_once( 'includes/class-wc-auth.php' ); // Auth Class
include_once( 'includes/class-wc-post-types.php' ); // Registers post types
include_once( 'includes/class-wc-query.php' ); // The main query class
include_once( 'includes/class-wc-api.php' ); // API Class
include_once( 'includes/class-wc-auth.php' ); // Auth Class
include_once( 'includes/class-wc-post-types.php' ); // Registers post types
include_once( 'includes/abstracts/abstract-wc-data.php' ); // WC_Data for CRUD
include_once( 'includes/abstracts/abstract-wc-payment-token.php' ); // Payment Tokens
include_once( 'includes/abstracts/abstract-wc-product.php' ); // Products
include_once( 'includes/abstracts/abstract-wc-order.php' ); // Orders
include_once( 'includes/abstracts/abstract-wc-settings-api.php' ); // Settings API (for gateways, shipping, and integrations)
include_once( 'includes/abstracts/abstract-wc-shipping-method.php' ); // A Shipping method
include_once( 'includes/abstracts/abstract-wc-payment-gateway.php' ); // A Payment gateway
include_once( 'includes/abstracts/abstract-wc-integration.php' ); // An integration with a service
include_once( 'includes/class-wc-product-factory.php' ); // Product factory
include_once( 'includes/class-wc-payment-tokens.php' ); // Payment tokens controller
include_once( 'includes/gateways/class-wc-payment-gateway-cc.php' ); // CC Payment Gateway
include_once( 'includes/abstracts/abstract-wc-payment-token.php' ); // Payment Tokens
include_once( 'includes/abstracts/abstract-wc-product.php' ); // Products
include_once( 'includes/abstracts/abstract-wc-order.php' ); // Orders
include_once( 'includes/abstracts/abstract-wc-settings-api.php' ); // Settings API (for gateways, shipping, and integrations)
include_once( 'includes/abstracts/abstract-wc-shipping-method.php' ); // A Shipping method
include_once( 'includes/abstracts/abstract-wc-payment-gateway.php' ); // A Payment gateway
include_once( 'includes/abstracts/abstract-wc-integration.php' ); // An integration with a service
include_once( 'includes/class-wc-product-factory.php' ); // Product factory
include_once( 'includes/class-wc-payment-tokens.php' ); // Payment tokens controller
include_once( 'includes/gateways/class-wc-payment-gateway-cc.php' ); // CC Payment Gateway
include_once( 'includes/gateways/class-wc-payment-gateway-echeck.php' ); // eCheck Payment Gateway
include_once( 'includes/class-wc-countries.php' ); // Defines countries and states
include_once( 'includes/class-wc-integrations.php' ); // Loads integrations
include_once( 'includes/class-wc-cache-helper.php' ); // Cache Helper
include_once( 'includes/class-wc-https.php' ); // https Helper
include_once( 'includes/class-wc-countries.php' ); // Defines countries and states
include_once( 'includes/class-wc-integrations.php' ); // Loads integrations
include_once( 'includes/class-wc-cache-helper.php' ); // Cache Helper
include_once( 'includes/class-wc-https.php' ); // https Helper
if ( defined( 'WP_CLI' ) && WP_CLI ) {
include_once( 'includes/class-wc-cli.php' );
}
$this->query = new WC_Query();
$this->api = new WC_API();
}
/**