implement separate handlers for v1/v2 REST API requests

This commit is contained in:
Max Rice 2014-07-12 17:51:08 -04:00
parent 54c019dd62
commit a4cb198cb9
1 changed files with 123 additions and 45 deletions

View File

@ -17,11 +17,14 @@ class WC_API {
/** This is the major version for the REST API and takes
* first-order position in endpoint URLs
*/
const VERSION = 1;
const VERSION = 2;
/** @var WC_API_Server the REST API server */
public $server;
/** @var WC_API_Authentication REST API authentication class instance */
public $authentication;
/**
* Setup class
*
@ -37,8 +40,11 @@ class WC_API {
// register API endpoints
add_action( 'init', array( $this, 'add_endpoint'), 0 );
// handle REST/legacy API request
add_action( 'parse_request', array( $this, 'handle_api_requests'), 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 );
}
/**
@ -51,6 +57,7 @@ class WC_API {
*/
public function add_query_vars( $vars ) {
$vars[] = 'wc-api';
$vars[] = 'wc-api-version';
$vars[] = 'wc-api-route';
return $vars;
}
@ -65,72 +72,58 @@ class WC_API {
public function add_endpoint() {
// REST API
add_rewrite_rule( '^wc-api\/v' . self::VERSION . '/?$', 'index.php?wc-api-route=/', 'top' );
add_rewrite_rule( '^wc-api\/v' . self::VERSION .'(.*)?', 'index.php?wc-api-route=$matches[1]', 'top' );
add_rewrite_rule( '^wc-api/v([1-2]{1})/?$', 'index.php?wc-api-version=$matches[1]&wc-api-route=/', 'top' );
add_rewrite_rule( '^wc-api/v([1-2]{1})(.*)?', 'index.php?wc-api-version=$matches[1]&wc-api-route=$matches[2]', 'top' );
// legacy API for payment gateway IPNs
// WC API for payment gateway IPNs, etc
add_rewrite_endpoint( 'wc-api', EP_ALL );
}
/**
* API request - Trigger any API requests
* Handle REST API requests
*
* @access public
* @since 2.0
* @return void
* @since 2.2
*/
public function handle_api_requests() {
public function handle_rest_api_requests() {
global $wp;
if ( ! empty( $_GET['wc-api'] ) )
$wp->query_vars['wc-api'] = $_GET['wc-api'];
if ( ! empty( $_GET['wc-api-version'] ) ) {
$wp->query_vars['wc-api-version'] = $_GET['wc-api-version'];
}
if ( ! empty( $_GET['wc-api-route'] ) )
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-route'] ) ) {
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'] ) );
// load required files
$this->includes();
// legacy v1 API request
if ( 1 === WC_API_REQUEST_VERSION ) {
$this->server = new WC_API_Server( $wp->query_vars['wc-api-route'] );
$this->handle_v1_rest_api_request();
// load API resource classes
$this->register_resources( $this->server );
} else {
// Fire off the request
$this->server->serve_request();
$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;
}
// legacy API requests
if ( ! empty( $wp->query_vars['wc-api'] ) ) {
// Buffer, we won't want any output here
ob_start();
// Get API trigger
$api = strtolower( esc_attr( $wp->query_vars['wc-api'] ) );
// Load class if exists
if ( class_exists( $api ) )
$api_class = new $api();
// Trigger actions
do_action( 'woocommerce_api_' . $api );
// Done, clear buffer and exit
ob_end_clean();
die('1');
}
}
/**
* Include required files for REST API request
*
@ -142,7 +135,6 @@ class WC_API {
include_once( 'api/class-wc-api-server.php' );
include_once( 'api/interface-wc-api-handler.php' );
include_once( 'api/class-wc-api-json-handler.php' );
include_once( 'api/class-wc-api-xml-handler.php' );
// authentication
include_once( 'api/class-wc-api-authentication.php' );
@ -182,4 +174,90 @@ class WC_API {
}
}
/**
* Handle legacy v1 REST API requests. Note this and the associated
* classes in the v1 folder should be removed when the API version is bumped
* to v3
*
* @since 2.2
*/
private function handle_v1_rest_api_request() {
// include legacy required files for v1 REST API request
include_once( 'api/v1/class-wc-api-server.php' );
include_once( 'api/v1/interface-wc-api-handler.php' );
include_once( 'api/v1/class-wc-api-json-handler.php' );
include_once( 'api/v1/class-wc-api-xml-handler.php' );
include_once( 'api/v1/class-wc-api-authentication.php' );
$this->authentication = new WC_API_Authentication();
include_once( 'api/v1/class-wc-api-resource.php' );
include_once( 'api/v1/class-wc-api-orders.php' );
include_once( 'api/v1/class-wc-api-products.php' );
include_once( 'api/v1/class-wc-api-coupons.php' );
include_once( 'api/v1/class-wc-api-customers.php' );
include_once( 'api/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();
}
/**
* API request - Trigger any API requests
*
* @access public
* @since 2.0
* @return void
*/
public function handle_api_requests() {
global $wp;
if ( ! empty( $_GET['wc-api'] ) ) {
$wp->query_vars['wc-api'] = $_GET['wc-api'];
}
// wc-api endpoint requests
if ( ! empty( $wp->query_vars['wc-api'] ) ) {
// Buffer, we won't want any output here
ob_start();
// Get API trigger
$api = strtolower( esc_attr( $wp->query_vars['wc-api'] ) );
// Load class if exists
if ( class_exists( $api ) )
$api_class = new $api();
// Trigger actions
do_action( 'woocommerce_api_' . $api );
// Done, clear buffer and exit
ob_end_clean();
die('1');
}
}
}