Skip to content

Commit

Permalink
Move legacy API code into WC_Legacy_API
Browse files Browse the repository at this point in the history
@claudiosmweb to keep legacy api separated from the new REST API code.
  • Loading branch information
mikejolley committed Jun 7, 2016
1 parent ced7a09 commit 1185333
Show file tree
Hide file tree
Showing 7 changed files with 304 additions and 280 deletions.
2 changes: 2 additions & 0 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 0 additions & 5 deletions includes/admin/views/html-admin-page-status-report.php
Original file line number Diff line number Diff line change
Expand Up @@ -515,11 +515,6 @@
<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">
Expand Down
256 changes: 9 additions & 247 deletions includes/class-wc-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,49 +14,25 @@
exit;
}

if ( ! class_exists( 'WC_API' ) ) :

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;
if ( ! class_exists( 'WC_Legacy_API' ) ) {
include_once( 'class-wc-legacy-api.php' );
}

/**
* 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 );

Expand All @@ -75,227 +51,20 @@ public function __construct() {
* @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.
*
Expand Down Expand Up @@ -338,7 +107,6 @@ public function handle_api_requests() {

/**
* Init WP REST API.
*
* @since 2.6.0
*/
private function rest_api_init() {
Expand All @@ -357,7 +125,6 @@ private function rest_api_init() {

/**
* Include REST API classes.
*
* @since 2.6.0
*/
private function rest_api_includes() {
Expand Down Expand Up @@ -403,7 +170,6 @@ private function rest_api_includes() {

/**
* Register REST API routes.
*
* @since 2.6.0
*/
public function register_rest_routes() {
Expand Down Expand Up @@ -436,7 +202,3 @@ public function register_rest_routes() {
}
}
}

endif;

return new WC_API();
Loading

0 comments on commit 1185333

Please sign in to comment.