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-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
|
|
|
|
|
|
|
// handle REST/legacy API request
|
|
|
|
add_action( 'parse_request', array( $this, 'handle_api_requests'), 0 );
|
|
|
|
|
|
|
|
// TODO: should this be done via this filter or in wp-json-server class?
|
|
|
|
add_filter( 'json_endpoints', array( $this, 'remove_users_endpoint' ), 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';
|
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
|
|
|
|
add_rewrite_rule( '^wc-api\/v1/?$', 'index.php?wc-api-route=/', 'top' );
|
|
|
|
add_rewrite_rule( '^wc-api\/v1(.*)?', 'index.php?wc-api-route=$matches[1]', 'top' );
|
|
|
|
|
|
|
|
// legacy API for payment gateway IPNs
|
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
|
|
|
/**
|
2013-11-04 00:29:45 +00:00
|
|
|
* API request - Trigger any API requests
|
2013-02-13 12:38:15 +00:00
|
|
|
*
|
|
|
|
* @access public
|
2013-11-04 00:29:45 +00:00
|
|
|
* @since 2.0
|
2013-02-13 12:38:15 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2013-11-04 00:29:45 +00:00
|
|
|
public function handle_api_requests() {
|
2013-02-13 12:38:15 +00:00
|
|
|
global $wp;
|
|
|
|
|
|
|
|
if ( ! empty( $_GET['wc-api'] ) )
|
|
|
|
$wp->query_vars['wc-api'] = $_GET['wc-api'];
|
|
|
|
|
2013-11-04 00:29:45 +00:00
|
|
|
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'] ) ) {
|
|
|
|
|
|
|
|
// load required files
|
|
|
|
$this->includes();
|
|
|
|
|
|
|
|
define('XMLRPC_REQUEST', true);
|
|
|
|
|
|
|
|
define('JSON_REQUEST', true);
|
|
|
|
|
|
|
|
// TODO: should these filters/actions be renamed?
|
|
|
|
$wp_json_server_class = apply_filters('wp_json_server_class', 'WP_JSON_Server');
|
|
|
|
|
|
|
|
$this->server = new $wp_json_server_class;
|
|
|
|
|
|
|
|
do_action('wp_json_server_before_serve', $this->server );
|
|
|
|
|
|
|
|
$this->register_resources( $this->server );
|
|
|
|
|
|
|
|
// Fire off the request
|
|
|
|
$this->server->serve_request( $wp->query_vars['wc-api-route'] );
|
|
|
|
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
// legacy API requests
|
2013-02-13 12:38:15 +00:00
|
|
|
if ( ! empty( $wp->query_vars['wc-api'] ) ) {
|
2013-11-04 00:29:45 +00:00
|
|
|
|
2013-02-13 12:38:15 +00:00
|
|
|
// 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
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Include required files for REST API request
|
|
|
|
*
|
|
|
|
* @since 2.1
|
|
|
|
*/
|
|
|
|
private function includes() {
|
|
|
|
|
|
|
|
// TODO: are all these required?
|
|
|
|
include_once( ABSPATH . WPINC . '/class-IXR.php' );
|
|
|
|
include_once( ABSPATH . WPINC . '/class-wp-xmlrpc-server.php' );
|
|
|
|
|
|
|
|
include_once( 'libraries/wp-api/class-wp-json-responsehandler.php' );
|
|
|
|
include_once( 'libraries/wp-api/class-wp-json-server.php' );
|
|
|
|
|
|
|
|
include_once( 'api/class-wc-api-authentication.php' );
|
|
|
|
$this->authentication = new WC_API_Authentication();
|
|
|
|
|
|
|
|
include_once( 'api/class-wc-api-base.php' );
|
|
|
|
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' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register API resources available
|
|
|
|
*
|
|
|
|
* @since 2.1
|
|
|
|
* @param object $server the REST server
|
|
|
|
*/
|
|
|
|
public function register_resources( $server ) {
|
|
|
|
|
|
|
|
$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( $server );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the users endpoints added by the JSON server
|
|
|
|
*
|
|
|
|
* @since 2.1
|
|
|
|
* @param $endpoints
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function remove_users_endpoint( $endpoints ) {
|
|
|
|
|
|
|
|
foreach ( $endpoints as $path => $endpoint ) {
|
|
|
|
|
|
|
|
if ( ! strncmp( $path, '/user', 5 ) )
|
|
|
|
unset( $endpoints[ $path ] );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $endpoints;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|