2013-02-13 12:38:15 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2015-11-03 13:53:50 +00:00
|
|
|
* WooCommerce API
|
2013-02-20 17:14:46 +00:00
|
|
|
*
|
2015-11-03 13:31:20 +00:00
|
|
|
* Handles WC-API endpoint requests.
|
2013-02-20 17:14:46 +00:00
|
|
|
*
|
2015-04-27 19:50:07 +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
|
|
|
|
2014-09-20 18:42:10 +00:00
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
2015-01-28 17:50:58 +00:00
|
|
|
exit;
|
2014-09-20 18:42:10 +00:00
|
|
|
}
|
2013-11-04 00:29:45 +00:00
|
|
|
|
2015-01-28 17:50:58 +00:00
|
|
|
if ( ! class_exists( 'WC_API' ) ) :
|
|
|
|
|
2013-02-13 12:38:15 +00:00
|
|
|
class WC_API {
|
|
|
|
|
2016-01-06 14:35:35 +00:00
|
|
|
/**
|
|
|
|
* This is the major version for the REST API and takes
|
2015-11-03 13:31:20 +00:00
|
|
|
* first-order position in endpoint URLs.
|
2016-01-06 14:35:35 +00:00
|
|
|
*
|
2016-02-16 20:59:18 +00:00
|
|
|
* @deprecated 2.6.0
|
2016-01-06 14:35:35 +00:00
|
|
|
* @var string
|
2013-11-06 06:56:38 +00:00
|
|
|
*/
|
2015-12-07 16:54:14 +00:00
|
|
|
const VERSION = '3.1.0';
|
2013-11-06 06:56:38 +00:00
|
|
|
|
2016-01-06 14:35:35 +00:00
|
|
|
/**
|
|
|
|
* The REST API server.
|
|
|
|
*
|
2016-02-16 20:22:12 +00:00
|
|
|
* @deprecated 2.6.0
|
2016-02-16 20:59:18 +00:00
|
|
|
* @var WC_API_Server
|
2016-01-06 14:35:35 +00:00
|
|
|
*/
|
2013-11-06 06:56:38 +00:00
|
|
|
public $server;
|
|
|
|
|
2016-01-06 14:35:35 +00:00
|
|
|
/**
|
|
|
|
* REST API authentication class instance.
|
|
|
|
*
|
2016-02-16 20:22:12 +00:00
|
|
|
* @deprecated 2.6.0
|
2016-02-16 20:59:18 +00:00
|
|
|
* @var WC_API_Authentication
|
2016-01-06 14:35:35 +00:00
|
|
|
*/
|
2014-07-12 21:51:08 +00:00
|
|
|
public $authentication;
|
|
|
|
|
2013-02-13 12:38:15 +00:00
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Setup class.
|
2013-02-13 12:38:15 +00:00
|
|
|
*
|
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() {
|
2016-02-16 20:59:18 +00:00
|
|
|
// Add query vars.
|
2015-04-27 20:49:35 +00:00
|
|
|
add_filter( 'query_vars', array( $this, 'add_query_vars' ), 0 );
|
2013-11-04 00:29:45 +00:00
|
|
|
|
2016-02-16 20:59:18 +00:00
|
|
|
// Register API endpoints.
|
2015-04-27 20:49:35 +00:00
|
|
|
add_action( 'init', array( $this, 'add_endpoint' ), 0 );
|
2013-11-04 00:29:45 +00:00
|
|
|
|
2016-02-16 20:59:18 +00:00
|
|
|
// Handle REST API requests.
|
2015-04-27 20:49:35 +00:00
|
|
|
add_action( 'parse_request', array( $this, 'handle_rest_api_requests' ), 0 );
|
2014-07-12 21:51:08 +00:00
|
|
|
|
2016-02-16 20:59:18 +00:00
|
|
|
// Handle wc-api endpoint requests.
|
2014-07-12 21:51:08 +00:00
|
|
|
add_action( 'parse_request', array( $this, 'handle_api_requests' ), 0 );
|
2015-06-05 13:36:45 +00:00
|
|
|
|
2016-02-16 20:59:18 +00:00
|
|
|
// Ensure payment gateways are initialized in time for API requests.
|
2015-06-05 13:36:45 +00:00
|
|
|
add_action( 'woocommerce_api_request', array( 'WC_Payment_Gateways', 'instance' ), 0 );
|
2016-02-16 20:59:18 +00:00
|
|
|
|
|
|
|
// WP REST API.
|
2016-02-17 19:40:41 +00:00
|
|
|
$this->rest_api_init();
|
2013-02-13 12:38:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-27 19:50:07 +00:00
|
|
|
* Add new query vars.
|
2013-02-13 12:38:15 +00:00
|
|
|
*
|
2013-11-04 00:29:45 +00:00
|
|
|
* @since 2.0
|
2016-01-06 14:35:35 +00:00
|
|
|
* @param array $vars
|
2014-09-07 23:37:55 +00:00
|
|
|
* @return string[]
|
2013-02-13 12:38:15 +00:00
|
|
|
*/
|
|
|
|
public function add_query_vars( $vars ) {
|
|
|
|
$vars[] = 'wc-api';
|
2016-02-16 20:22:12 +00:00
|
|
|
$vars[] = 'wc-api-version'; // Deprecated since 2.6.0.
|
|
|
|
$vars[] = 'wc-api-route'; // Deprecated since 2.6.0.
|
|
|
|
|
2013-02-13 12:38:15 +00:00
|
|
|
return $vars;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-27 19:50:07 +00:00
|
|
|
* Add new endpoints.
|
2013-02-13 12:38:15 +00:00
|
|
|
*
|
2013-11-04 00:29:45 +00:00
|
|
|
* @since 2.0
|
2013-02-13 12:38:15 +00:00
|
|
|
*/
|
2015-03-20 14:20:12 +00:00
|
|
|
public static function add_endpoint() {
|
2013-11-04 00:29:45 +00:00
|
|
|
|
2016-02-16 20:22:12 +00:00
|
|
|
// REST API, deprecated since 2.6.0.
|
2015-06-17 16:50:52 +00:00
|
|
|
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' );
|
2013-11-04 00:29:45 +00:00
|
|
|
|
2016-02-17 19:40:41 +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
|
|
|
/**
|
2015-11-03 13:31:20 +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
|
2016-02-16 20:22:12 +00:00
|
|
|
* @deprecated 2.6.0
|
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
|
|
|
|
2016-02-17 19:40:41 +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
|
|
|
|
2016-02-17 19:40:41 +00:00
|
|
|
// Legacy v1 API request.
|
2014-07-12 21:51:08 +00:00
|
|
|
if ( 1 === WC_API_REQUEST_VERSION ) {
|
|
|
|
$this->handle_v1_rest_api_request();
|
2015-06-17 16:50:52 +00:00
|
|
|
} else if ( 2 === WC_API_REQUEST_VERSION ) {
|
|
|
|
$this->handle_v2_rest_api_request();
|
2014-07-12 21:51:08 +00:00
|
|
|
} else {
|
|
|
|
$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
|
|
|
|
2016-02-17 19:40:41 +00:00
|
|
|
// load API resource classes.
|
2014-07-12 21:51:08 +00:00
|
|
|
$this->register_resources( $this->server );
|
2013-02-13 12:38:15 +00:00
|
|
|
|
2016-02-17 19:40:41 +00:00
|
|
|
// Fire off the request.
|
2014-07-12 21:51:08 +00:00
|
|
|
$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
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Include required files for REST API request.
|
2013-11-04 00:29:45 +00:00
|
|
|
*
|
|
|
|
* @since 2.1
|
2016-02-16 20:22:12 +00:00
|
|
|
* @deprecated 2.6.0
|
2013-11-04 00:29:45 +00:00
|
|
|
*/
|
2014-07-30 20:22:58 +00:00
|
|
|
public function includes() {
|
2013-11-04 00:29:45 +00:00
|
|
|
|
2016-02-17 19:40:41 +00:00
|
|
|
// API server / response handlers.
|
2016-02-16 20:22:12 +00:00
|
|
|
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' );
|
2013-11-04 00:29:45 +00:00
|
|
|
|
2016-02-17 19:40:41 +00:00
|
|
|
// Authentication.
|
2016-02-16 20:22:12 +00:00
|
|
|
include_once( 'api/legacy/v3/class-wc-api-authentication.php' );
|
2013-11-04 00:29:45 +00:00
|
|
|
$this->authentication = new WC_API_Authentication();
|
|
|
|
|
2016-02-16 20:22:12 +00:00
|
|
|
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' );
|
2013-11-06 06:56:11 +00:00
|
|
|
|
2016-02-17 19:40:41 +00:00
|
|
|
// Allow plugins to load other response handlers or resource classes.
|
2013-11-23 18:27:47 +00:00
|
|
|
do_action( 'woocommerce_api_loaded' );
|
2013-11-04 00:29:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Register available API resources.
|
2013-11-04 00:29:45 +00:00
|
|
|
*
|
|
|
|
* @since 2.1
|
2016-02-16 20:22:12 +00:00
|
|
|
* @deprecated 2.6.0
|
2014-09-07 23:37:55 +00:00
|
|
|
* @param WC_API_Server $server the REST server
|
2013-11-04 00:29:45 +00:00
|
|
|
*/
|
|
|
|
public function register_resources( $server ) {
|
|
|
|
|
2013-11-23 18:27:47 +00:00
|
|
|
$api_classes = apply_filters( 'woocommerce_api_classes',
|
|
|
|
array(
|
2015-10-07 19:37:53 +00:00
|
|
|
'WC_API_Coupons',
|
2013-11-23 18:27:47 +00:00
|
|
|
'WC_API_Customers',
|
|
|
|
'WC_API_Orders',
|
|
|
|
'WC_API_Products',
|
|
|
|
'WC_API_Reports',
|
2015-10-07 19:37:53 +00:00
|
|
|
'WC_API_Taxes',
|
2014-07-30 20:22:58 +00:00
|
|
|
'WC_API_Webhooks',
|
2013-11-23 18:27:47 +00:00
|
|
|
)
|
|
|
|
);
|
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
|
|
|
|
|
|
|
/**
|
2015-06-17 16:50:52 +00:00
|
|
|
* Handle legacy v1 REST API requests.
|
2014-07-12 21:51:08 +00:00
|
|
|
*
|
|
|
|
* @since 2.2
|
2016-02-16 20:22:12 +00:00
|
|
|
* @deprecated 2.6.0
|
2014-07-12 21:51:08 +00:00
|
|
|
*/
|
|
|
|
private function handle_v1_rest_api_request() {
|
|
|
|
|
2016-02-17 19:40:41 +00:00
|
|
|
// Include legacy required files for v1 REST API request.
|
2016-02-16 20:22:12 +00:00
|
|
|
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' );
|
2014-07-12 21:51:08 +00:00
|
|
|
|
2016-02-16 20:22:12 +00:00
|
|
|
include_once( 'api/legacy/v1/class-wc-api-authentication.php' );
|
2014-07-12 21:51:08 +00:00
|
|
|
$this->authentication = new WC_API_Authentication();
|
|
|
|
|
2016-02-16 20:22:12 +00:00
|
|
|
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' );
|
2014-07-12 21:51:08 +00:00
|
|
|
|
2016-02-17 19:40:41 +00:00
|
|
|
// Allow plugins to load other response handlers or resource classes.
|
2014-07-12 21:51:08 +00:00
|
|
|
do_action( 'woocommerce_api_loaded' );
|
|
|
|
|
|
|
|
$this->server = new WC_API_Server( $GLOBALS['wp']->query_vars['wc-api-route'] );
|
|
|
|
|
2016-02-17 19:40:41 +00:00
|
|
|
// Register available resources for legacy v1 REST API request.
|
2014-07-12 21:51:08 +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',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach ( $api_classes as $api_class ) {
|
|
|
|
$this->$api_class = new $api_class( $this->server );
|
|
|
|
}
|
|
|
|
|
2016-02-17 19:40:41 +00:00
|
|
|
// Fire off the request.
|
2014-07-12 21:51:08 +00:00
|
|
|
$this->server->serve_request();
|
|
|
|
}
|
|
|
|
|
2015-06-17 16:50:52 +00:00
|
|
|
/**
|
|
|
|
* Handle legacy v2 REST API requests.
|
|
|
|
*
|
|
|
|
* @since 2.4
|
2016-02-16 20:22:12 +00:00
|
|
|
* @deprecated 2.6.0
|
2015-06-17 16:50:52 +00:00
|
|
|
*/
|
|
|
|
private function handle_v2_rest_api_request() {
|
2016-02-16 20:22:12 +00:00
|
|
|
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' );
|
2015-06-17 16:50:52 +00:00
|
|
|
|
2016-02-16 20:22:12 +00:00
|
|
|
include_once( 'api/legacy/v2/class-wc-api-authentication.php' );
|
2015-06-17 16:50:52 +00:00
|
|
|
$this->authentication = new WC_API_Authentication();
|
|
|
|
|
2016-02-16 20:22:12 +00:00
|
|
|
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' );
|
2015-06-17 16:50:52 +00:00
|
|
|
|
2016-02-17 19:40:41 +00:00
|
|
|
// allow plugins to load other response handlers or resource classes.
|
2015-06-17 16:50:52 +00:00
|
|
|
do_action( 'woocommerce_api_loaded' );
|
|
|
|
|
|
|
|
$this->server = new WC_API_Server( $GLOBALS['wp']->query_vars['wc-api-route'] );
|
|
|
|
|
2016-02-17 19:40:41 +00:00
|
|
|
// Register available resources for legacy v2 REST API request.
|
2015-06-17 16:50:52 +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',
|
|
|
|
'WC_API_Webhooks',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach ( $api_classes as $api_class ) {
|
|
|
|
$this->$api_class = new $api_class( $this->server );
|
|
|
|
}
|
|
|
|
|
2016-02-17 19:40:41 +00:00
|
|
|
// Fire off the request.
|
2015-06-17 16:50:52 +00:00
|
|
|
$this->server->serve_request();
|
|
|
|
}
|
|
|
|
|
2014-07-12 21:51:08 +00:00
|
|
|
/**
|
2015-06-05 13:36:45 +00:00
|
|
|
* API request - Trigger any API requests.
|
2014-07-12 21:51:08 +00:00
|
|
|
*
|
2016-02-16 20:22:12 +00:00
|
|
|
* @since 2.0
|
|
|
|
* @version 2.4
|
2014-07-12 21:51:08 +00:00
|
|
|
*/
|
|
|
|
public function handle_api_requests() {
|
|
|
|
global $wp;
|
|
|
|
|
|
|
|
if ( ! empty( $_GET['wc-api'] ) ) {
|
|
|
|
$wp->query_vars['wc-api'] = $_GET['wc-api'];
|
|
|
|
}
|
|
|
|
|
2016-02-17 19:40:41 +00:00
|
|
|
// wc-api endpoint requests.
|
2014-07-12 21:51:08 +00:00
|
|
|
if ( ! empty( $wp->query_vars['wc-api'] ) ) {
|
|
|
|
|
2016-02-17 19:40:41 +00:00
|
|
|
// Buffer, we won't want any output here.
|
2014-07-12 21:51:08 +00:00
|
|
|
ob_start();
|
|
|
|
|
2016-02-17 19:40:41 +00:00
|
|
|
// No cache headers.
|
2015-06-05 13:36:45 +00:00
|
|
|
nocache_headers();
|
2014-07-12 21:51:08 +00:00
|
|
|
|
2016-02-17 19:40:41 +00:00
|
|
|
// Clean the API request.
|
2015-06-05 13:36:45 +00:00
|
|
|
$api_request = strtolower( wc_clean( $wp->query_vars['wc-api'] ) );
|
|
|
|
|
2016-02-17 19:40:41 +00:00
|
|
|
// Trigger generic action before request hook.
|
2015-06-05 13:36:45 +00:00
|
|
|
do_action( 'woocommerce_api_request', $api_request );
|
|
|
|
|
2016-02-17 19:40:41 +00:00
|
|
|
// Is there actually something hooked into this API request? If not trigger 400 - Bad request.
|
2015-06-05 13:36:45 +00:00
|
|
|
status_header( has_action( 'woocommerce_api_' . $api_request ) ? 200 : 400 );
|
2014-07-12 21:51:08 +00:00
|
|
|
|
2016-02-17 19:40:41 +00:00
|
|
|
// Trigger an action which plugins can hook into to fulfill the request.
|
2015-06-05 13:36:45 +00:00
|
|
|
do_action( 'woocommerce_api_' . $api_request );
|
2014-07-12 21:51:08 +00:00
|
|
|
|
2016-02-17 19:40:41 +00:00
|
|
|
// Done, clear buffer and exit.
|
2014-07-12 21:51:08 +00:00
|
|
|
ob_end_clean();
|
2016-02-17 19:40:41 +00:00
|
|
|
die( '-1' );
|
2014-07-12 21:51:08 +00:00
|
|
|
}
|
|
|
|
}
|
2016-02-16 20:59:18 +00:00
|
|
|
|
2016-02-17 19:40:41 +00:00
|
|
|
/**
|
|
|
|
* Init WP REST API.
|
|
|
|
*
|
|
|
|
* @since 2.6.0
|
|
|
|
*/
|
|
|
|
private function rest_api_init() {
|
|
|
|
global $wp_version;
|
|
|
|
|
|
|
|
// REST API was included starting WordPress 4.4.
|
|
|
|
if ( version_compare( $wp_version, 4.4, '<' ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->rest_api_includes();
|
|
|
|
|
|
|
|
// Init REST API routes.
|
|
|
|
add_action( 'rest_api_init', array( $this, 'register_rest_routes' ) );
|
|
|
|
}
|
|
|
|
|
2016-02-16 20:59:18 +00:00
|
|
|
/**
|
|
|
|
* Include REST API classes.
|
|
|
|
*
|
|
|
|
* @since 2.6.0
|
|
|
|
*/
|
2016-02-17 19:40:41 +00:00
|
|
|
private function rest_api_includes() {
|
2016-03-28 12:22:44 +00:00
|
|
|
// Exception handler.
|
|
|
|
if ( ! class_exists( 'WC_API_Exception' ) ) {
|
|
|
|
include_once( 'api/legacy/v3/class-wc-api-exception.php' );
|
|
|
|
}
|
|
|
|
include_once( 'api/class-wc-rest-exception.php' );
|
|
|
|
|
2016-03-18 09:53:09 +00:00
|
|
|
// Authentication.
|
2016-03-28 12:25:50 +00:00
|
|
|
include_once( 'api/class-wc-rest-authentication.php' );
|
2016-03-18 09:53:09 +00:00
|
|
|
|
2016-02-23 15:15:25 +00:00
|
|
|
// WP-API classes and functions.
|
|
|
|
include_once( 'vendor/wp-api-functions.php' );
|
2016-02-16 20:59:18 +00:00
|
|
|
if ( ! class_exists( 'WP_REST_Controller' ) ) {
|
|
|
|
include_once( 'vendor/class-wp-rest-controller.php' );
|
|
|
|
}
|
|
|
|
|
2016-02-22 19:43:52 +00:00
|
|
|
// Abstract controllers.
|
|
|
|
include_once( 'abstracts/abstract-wc-rest-posts-controller.php' );
|
|
|
|
include_once( 'abstracts/abstract-wc-rest-terms-controller.php' );
|
|
|
|
|
|
|
|
// REST API controllers.
|
2016-03-28 12:25:50 +00:00
|
|
|
include_once( 'api/class-wc-rest-coupons-controller.php' );
|
|
|
|
include_once( 'api/class-wc-rest-customers-controller.php' );
|
|
|
|
include_once( 'api/class-wc-rest-order-notes-controller.php' );
|
|
|
|
include_once( 'api/class-wc-rest-order-refunds-controller.php' );
|
|
|
|
include_once( 'api/class-wc-rest-orders-controller.php' );
|
|
|
|
include_once( 'api/class-wc-rest-product-attribute-terms-controller.php' );
|
|
|
|
include_once( 'api/class-wc-rest-product-attributes-controller.php' );
|
|
|
|
include_once( 'api/class-wc-rest-product-categories-controller.php' );
|
|
|
|
include_once( 'api/class-wc-rest-product-shipping-classes-controller.php' );
|
|
|
|
include_once( 'api/class-wc-rest-product-tags-controller.php' );
|
|
|
|
include_once( 'api/class-wc-rest-products-controller.php' );
|
|
|
|
include_once( 'api/class-wc-rest-report-sales-controller.php' );
|
|
|
|
include_once( 'api/class-wc-rest-report-top-sellers-controller.php' );
|
|
|
|
include_once( 'api/class-wc-rest-reports-controller.php' );
|
|
|
|
include_once( 'api/class-wc-rest-tax-classes-controller.php' );
|
|
|
|
include_once( 'api/class-wc-rest-taxes-controller.php' );
|
|
|
|
include_once( 'api/class-wc-rest-webhook-deliveries.php' );
|
|
|
|
include_once( 'api/class-wc-rest-webhooks-controller.php' );
|
2016-02-16 20:59:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register REST API routes.
|
|
|
|
*
|
|
|
|
* @since 2.6.0
|
|
|
|
*/
|
|
|
|
public function register_rest_routes() {
|
2016-02-17 19:29:09 +00:00
|
|
|
$controllers = array(
|
|
|
|
'WC_REST_Coupons_Controller',
|
|
|
|
'WC_REST_Customers_Controller',
|
|
|
|
'WC_REST_Order_Notes_Controller',
|
|
|
|
'WC_REST_Order_Refunds_Controller',
|
|
|
|
'WC_REST_Orders_Controller',
|
|
|
|
'WC_REST_Product_Attribute_Terms_Controller',
|
|
|
|
'WC_REST_Product_Attributes_Controller',
|
|
|
|
'WC_REST_Product_Categories_Controller',
|
|
|
|
'WC_REST_Product_Shipping_Classes_Controller',
|
|
|
|
'WC_REST_Product_Tags_Controller',
|
|
|
|
'WC_REST_Products_Controller',
|
2016-03-10 02:19:36 +00:00
|
|
|
'WC_REST_Report_Sales_Controller',
|
2016-03-10 02:51:25 +00:00
|
|
|
'WC_REST_Report_Top_Sellers_Controller',
|
2016-02-17 19:29:09 +00:00
|
|
|
'WC_REST_Reports_Controller',
|
|
|
|
'WC_REST_Tax_Classes_Controller',
|
|
|
|
'WC_REST_Taxes_Controller',
|
2016-03-15 20:54:06 +00:00
|
|
|
'WC_REST_Webhook_Deliveries_Controller',
|
2016-03-15 19:56:55 +00:00
|
|
|
'WC_REST_Webhooks_Controller',
|
2016-02-17 19:29:09 +00:00
|
|
|
);
|
2016-02-16 20:59:18 +00:00
|
|
|
|
|
|
|
foreach ( $controllers as $controller ) {
|
2016-02-17 19:40:41 +00:00
|
|
|
$this->$controller = new $controller();
|
2016-02-16 20:59:18 +00:00
|
|
|
$this->$controller->register_routes();
|
|
|
|
}
|
|
|
|
}
|
2013-11-04 00:29:45 +00:00
|
|
|
}
|
2015-01-28 17:50:58 +00:00
|
|
|
|
|
|
|
endif;
|
|
|
|
|
|
|
|
return new WC_API();
|