2013-02-13 12:38:15 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2013-02-20 17:14:46 +00:00
|
|
|
* WooCommerce API
|
|
|
|
*
|
2013-11-04 00:29:45 +00:00
|
|
|
* Handles WC-API endpoint requests
|
2013-02-20 17:14:46 +00:00
|
|
|
*
|
2013-11-04 00:29:45 +00:00
|
|
|
* @author WooThemes
|
|
|
|
* @category API
|
|
|
|
* @package WooCommerce/API
|
|
|
|
* @since 2.0
|
2013-02-13 12:38:15 +00:00
|
|
|
*/
|
2013-11-04 00:29:45 +00:00
|
|
|
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
|
|
|
|
|
2013-02-13 12:38:15 +00:00
|
|
|
class WC_API {
|
|
|
|
|
2013-11-06 06:56:38 +00:00
|
|
|
/** This is the major version for the REST API and takes
|
|
|
|
* first-order position in endpoint URLs
|
|
|
|
*/
|
2014-07-12 21:51:08 +00:00
|
|
|
const VERSION = 2;
|
2013-11-06 06:56:38 +00:00
|
|
|
|
|
|
|
/** @var WC_API_Server the REST API server */
|
|
|
|
public $server;
|
|
|
|
|
2014-07-12 21:51:08 +00:00
|
|
|
/** @var WC_API_Authentication REST API authentication class instance */
|
|
|
|
public $authentication;
|
|
|
|
|
2013-02-13 12:38:15 +00:00
|
|
|
/**
|
2013-11-04 00:29:45 +00:00
|
|
|
* Setup class
|
2013-02-13 12:38:15 +00:00
|
|
|
*
|
|
|
|
* @access public
|
2013-11-04 00:29:45 +00:00
|
|
|
* @since 2.0
|
|
|
|
* @return WC_API
|
2013-02-13 12:38:15 +00:00
|
|
|
*/
|
|
|
|
public function __construct() {
|
2013-11-04 00:29:45 +00:00
|
|
|
|
|
|
|
// add query vars
|
2013-02-13 12:38:15 +00:00
|
|
|
add_filter( 'query_vars', array( $this, 'add_query_vars'), 0 );
|
2013-11-04 00:29:45 +00:00
|
|
|
|
|
|
|
// register API endpoints
|
2013-02-13 12:38:15 +00:00
|
|
|
add_action( 'init', array( $this, 'add_endpoint'), 0 );
|
2013-11-04 00:29:45 +00:00
|
|
|
|
2014-07-12 21:51:08 +00:00
|
|
|
// 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 );
|
2013-02-13 12:38:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* add_query_vars function.
|
|
|
|
*
|
|
|
|
* @access public
|
2013-11-04 00:29:45 +00:00
|
|
|
* @since 2.0
|
|
|
|
* @param $vars
|
|
|
|
* @return array
|
2013-02-13 12:38:15 +00:00
|
|
|
*/
|
|
|
|
public function add_query_vars( $vars ) {
|
|
|
|
$vars[] = 'wc-api';
|
2014-07-12 21:51:08 +00:00
|
|
|
$vars[] = 'wc-api-version';
|
2013-11-04 00:29:45 +00:00
|
|
|
$vars[] = 'wc-api-route';
|
2013-02-13 12:38:15 +00:00
|
|
|
return $vars;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* add_endpoint function.
|
|
|
|
*
|
|
|
|
* @access public
|
2013-11-04 00:29:45 +00:00
|
|
|
* @since 2.0
|
2013-02-13 12:38:15 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function add_endpoint() {
|
2013-11-04 00:29:45 +00:00
|
|
|
|
|
|
|
// REST API
|
2014-07-12 21:51:08 +00:00
|
|
|
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' );
|
2013-11-04 00:29:45 +00:00
|
|
|
|
2014-07-12 21:51:08 +00:00
|
|
|
// WC API for payment gateway IPNs, etc
|
2013-02-13 12:38:15 +00:00
|
|
|
add_rewrite_endpoint( 'wc-api', EP_ALL );
|
|
|
|
}
|
|
|
|
|
2013-11-04 00:29:45 +00:00
|
|
|
|
2013-02-13 12:38:15 +00:00
|
|
|
/**
|
2014-07-12 21:51:08 +00:00
|
|
|
* Handle REST API requests
|
2013-02-13 12:38:15 +00:00
|
|
|
*
|
2014-07-12 21:51:08 +00:00
|
|
|
* @since 2.2
|
2013-02-13 12:38:15 +00:00
|
|
|
*/
|
2014-07-12 21:51:08 +00:00
|
|
|
public function handle_rest_api_requests() {
|
2013-02-13 12:38:15 +00:00
|
|
|
global $wp;
|
|
|
|
|
2014-07-12 21:51:08 +00:00
|
|
|
if ( ! empty( $_GET['wc-api-version'] ) ) {
|
|
|
|
$wp->query_vars['wc-api-version'] = $_GET['wc-api-version'];
|
|
|
|
}
|
2013-02-13 12:38:15 +00:00
|
|
|
|
2014-07-12 21:51:08 +00:00
|
|
|
if ( ! empty( $_GET['wc-api-route'] ) ) {
|
2013-11-04 00:29:45 +00:00
|
|
|
$wp->query_vars['wc-api-route'] = $_GET['wc-api-route'];
|
2014-07-12 21:51:08 +00:00
|
|
|
}
|
2013-11-04 00:29:45 +00:00
|
|
|
|
|
|
|
// REST API request
|
2014-07-12 21:51:08 +00:00
|
|
|
if ( ! empty( $wp->query_vars['wc-api-version'] ) && ! empty( $wp->query_vars['wc-api-route'] ) ) {
|
2013-11-04 00:29:45 +00:00
|
|
|
|
2013-11-06 06:56:11 +00:00
|
|
|
define( 'WC_API_REQUEST', true );
|
2014-07-12 21:51:08 +00:00
|
|
|
define( 'WC_API_REQUEST_VERSION', absint( $wp->query_vars['wc-api-version'] ) );
|
2013-11-06 06:56:11 +00:00
|
|
|
|
2014-07-12 21:51:08 +00:00
|
|
|
// legacy v1 API request
|
|
|
|
if ( 1 === WC_API_REQUEST_VERSION ) {
|
2013-11-04 00:29:45 +00:00
|
|
|
|
2014-07-12 21:51:08 +00:00
|
|
|
$this->handle_v1_rest_api_request();
|
2013-11-04 00:29:45 +00:00
|
|
|
|
2014-07-12 21:51:08 +00:00
|
|
|
} else {
|
2013-11-04 00:29:45 +00:00
|
|
|
|
2014-07-12 21:51:08 +00:00
|
|
|
$this->includes();
|
2013-11-04 00:29:45 +00:00
|
|
|
|
2014-07-12 21:51:08 +00:00
|
|
|
$this->server = new WC_API_Server( $wp->query_vars['wc-api-route'] );
|
2013-11-04 00:29:45 +00:00
|
|
|
|
2014-07-12 21:51:08 +00:00
|
|
|
// load API resource classes
|
|
|
|
$this->register_resources( $this->server );
|
2013-02-13 12:38:15 +00:00
|
|
|
|
2014-07-12 21:51:08 +00:00
|
|
|
// Fire off the request
|
|
|
|
$this->server->serve_request();
|
|
|
|
}
|
2013-02-13 12:38:15 +00:00
|
|
|
|
2014-07-12 21:51:08 +00:00
|
|
|
exit;
|
2013-02-13 12:38:15 +00:00
|
|
|
}
|
|
|
|
}
|
2013-11-04 00:29:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Include required files for REST API request
|
|
|
|
*
|
|
|
|
* @since 2.1
|
|
|
|
*/
|
|
|
|
private function includes() {
|
|
|
|
|
2013-11-23 18:27:47 +00:00
|
|
|
// API server / response handlers
|
2014-07-28 03:20:48 +00:00
|
|
|
include_once( 'api/class-wc-api-exception.php' );
|
2013-11-06 06:56:11 +00:00
|
|
|
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' );
|
2013-11-04 00:29:45 +00:00
|
|
|
|
2013-11-23 18:27:47 +00:00
|
|
|
// authentication
|
2013-11-04 00:29:45 +00:00
|
|
|
include_once( 'api/class-wc-api-authentication.php' );
|
|
|
|
$this->authentication = new WC_API_Authentication();
|
|
|
|
|
2013-11-09 21:20:23 +00:00
|
|
|
include_once( 'api/class-wc-api-resource.php' );
|
2013-11-04 00:29:45 +00:00
|
|
|
include_once( 'api/class-wc-api-orders.php' );
|
|
|
|
include_once( 'api/class-wc-api-products.php' );
|
|
|
|
include_once( 'api/class-wc-api-coupons.php' );
|
|
|
|
include_once( 'api/class-wc-api-customers.php' );
|
|
|
|
include_once( 'api/class-wc-api-reports.php' );
|
2013-11-06 06:56:11 +00:00
|
|
|
|
2013-11-23 18:27:47 +00:00
|
|
|
// allow plugins to load other response handlers or resource classes
|
|
|
|
do_action( 'woocommerce_api_loaded' );
|
2013-11-04 00:29:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-11-23 18:27:47 +00:00
|
|
|
* Register available API resources
|
2013-11-04 00:29:45 +00:00
|
|
|
*
|
|
|
|
* @since 2.1
|
|
|
|
* @param object $server the REST server
|
|
|
|
*/
|
|
|
|
public function register_resources( $server ) {
|
|
|
|
|
2013-11-23 18:27:47 +00:00
|
|
|
$api_classes = apply_filters( 'woocommerce_api_classes',
|
|
|
|
array(
|
|
|
|
'WC_API_Customers',
|
|
|
|
'WC_API_Orders',
|
|
|
|
'WC_API_Products',
|
|
|
|
'WC_API_Coupons',
|
|
|
|
'WC_API_Reports',
|
|
|
|
)
|
|
|
|
);
|
2013-11-04 00:29:45 +00:00
|
|
|
|
|
|
|
foreach ( $api_classes as $api_class ) {
|
|
|
|
$this->$api_class = new $api_class( $server );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-12 21:51:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-04 00:29:45 +00:00
|
|
|
}
|