REST API v2

This commit is contained in:
Claudio Sanches 2017-02-09 15:06:13 -02:00
parent 962b60c8fe
commit 4352484ed7
32 changed files with 135 additions and 161 deletions

View File

@ -1,127 +0,0 @@
<?php
/**
* REST API Shipping Zones Controller base
*
* Houses common functionality between Shipping Zones and Locations.
*
* @author WooThemes
* @category API
* @package WooCommerce/API
* @since 2.7.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* REST API Shipping Zones base class.
*
* @package WooCommerce/API
* @extends WC_REST_Controller
*/
abstract class WC_REST_Shipping_Zones_Controller_Base extends WC_REST_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc/v1';
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'shipping/zones';
/**
* Retrieve a Shipping Zone by it's ID.
*
* @param int $zone_id Shipping Zone ID.
* @return WC_Shipping_Zone|WP_Error
*/
protected function get_zone( $zone_id ) {
$zone = WC_Shipping_Zones::get_zone_by( 'zone_id', $zone_id );
if ( false === $zone ) {
return new WP_Error( 'woocommerce_rest_shipping_zone_invalid', __( 'Resource does not exist.', 'woocommerce' ), array( 'status' => 404 ) );
}
return $zone;
}
/**
* Check whether a given request has permission to read Shipping Zones.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function get_items_permissions_check( $request ) {
if ( ! wc_shipping_enabled() ) {
return new WP_Error( 'rest_no_route', __( 'Shipping is disabled.', 'woocommerce' ), array( 'status' => 404 ) );
}
if ( ! wc_rest_check_manager_permissions( 'settings', 'read' ) ) {
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
/**
* Check if a given request has access to create Shipping Zones.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function create_item_permissions_check( $request ) {
if ( ! wc_shipping_enabled() ) {
return new WP_Error( 'rest_no_route', __( 'Shipping is disabled.', 'woocommerce' ), array( 'status' => 404 ) );
}
if ( ! wc_rest_check_manager_permissions( 'settings', 'edit' ) ) {
return new WP_Error( 'woocommerce_rest_cannot_create', __( 'Sorry, you are not allowed to create resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
/**
* Check whether a given request has permission to edit Shipping Zones.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function update_items_permissions_check( $request ) {
if ( ! wc_shipping_enabled() ) {
return new WP_Error( 'rest_no_route', __( 'Shipping is disabled.', 'woocommerce' ), array( 'status' => 404 ) );
}
if ( ! wc_rest_check_manager_permissions( 'settings', 'edit' ) ) {
return new WP_Error( 'woocommerce_rest_cannot_edit', __( 'Sorry, you are not allowed to edit this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
/**
* Check whether a given request has permission to delete Shipping Zones.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function delete_items_permissions_check( $request ) {
if ( ! wc_shipping_enabled() ) {
return new WP_Error( 'rest_no_route', __( 'Shipping is disabled.', 'woocommerce' ), array( 'status' => 404 ) );
}
if ( ! wc_rest_check_manager_permissions( 'settings', 'delete' ) ) {
return new WP_Error( 'woocommerce_rest_cannot_edit', __( 'Sorry, you are not allowed to delete this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
}

View File

@ -27,7 +27,7 @@ class WC_REST_Coupons_Controller extends WC_REST_Posts_Controller {
*
* @var string
*/
protected $namespace = 'wc/v1';
protected $namespace = 'wc/v2';
/**
* Route base.

View File

@ -27,7 +27,7 @@ class WC_REST_Customer_Downloads_Controller extends WC_REST_Controller {
*
* @var string
*/
protected $namespace = 'wc/v1';
protected $namespace = 'wc/v2';
/**
* Route base.

View File

@ -27,7 +27,7 @@ class WC_REST_Customers_Controller extends WC_REST_Controller {
*
* @var string
*/
protected $namespace = 'wc/v1';
protected $namespace = 'wc/v2';
/**
* Route base.

View File

@ -27,7 +27,7 @@ class WC_REST_Order_Notes_Controller extends WC_REST_Controller {
*
* @var string
*/
protected $namespace = 'wc/v1';
protected $namespace = 'wc/v2';
/**
* Route base.

View File

@ -29,7 +29,7 @@ class WC_REST_Order_Refunds_Controller extends WC_REST_Orders_Controller {
*
* @var string
*/
protected $namespace = 'wc/v1';
protected $namespace = 'wc/v2';
/**
* Route base.

View File

@ -27,7 +27,7 @@ class WC_REST_Orders_Controller extends WC_REST_Posts_Controller {
*
* @var string
*/
protected $namespace = 'wc/v1';
protected $namespace = 'wc/v2';
/**
* Route base.

View File

@ -25,7 +25,7 @@ class WC_REST_Payment_Gateways_Controller extends WC_REST_Controller {
*
* @var string
*/
protected $namespace = 'wc/v1';
protected $namespace = 'wc/v2';
/**
* Route base.

View File

@ -27,7 +27,7 @@ class WC_REST_Product_Attribute_Terms_Controller extends WC_REST_Terms_Controlle
*
* @var string
*/
protected $namespace = 'wc/v1';
protected $namespace = 'wc/v2';
/**
* Route base.

View File

@ -27,7 +27,7 @@ class WC_REST_Product_Attributes_Controller extends WC_REST_Controller {
*
* @var string
*/
protected $namespace = 'wc/v1';
protected $namespace = 'wc/v2';
/**
* Route base.

View File

@ -27,7 +27,7 @@ class WC_REST_Product_Categories_Controller extends WC_REST_Terms_Controller {
*
* @var string
*/
protected $namespace = 'wc/v1';
protected $namespace = 'wc/v2';
/**
* Route base.

View File

@ -27,7 +27,7 @@ class WC_REST_Product_Reviews_Controller extends WC_REST_Controller {
*
* @var string
*/
protected $namespace = 'wc/v1';
protected $namespace = 'wc/v2';
/**
* Route base.

View File

@ -27,7 +27,7 @@ class WC_REST_Product_Shipping_Classes_Controller extends WC_REST_Terms_Controll
*
* @var string
*/
protected $namespace = 'wc/v1';
protected $namespace = 'wc/v2';
/**
* Route base.

View File

@ -27,7 +27,7 @@ class WC_REST_Product_Tags_Controller extends WC_REST_Terms_Controller {
*
* @var string
*/
protected $namespace = 'wc/v1';
protected $namespace = 'wc/v2';
/**
* Route base.

View File

@ -27,7 +27,7 @@ class WC_REST_Product_Variations_Controller extends WC_REST_Products_Controller
*
* @var string
*/
protected $namespace = 'wc/v1';
protected $namespace = 'wc/v2';
/**
* Route base.

View File

@ -27,7 +27,7 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
*
* @var string
*/
protected $namespace = 'wc/v1';
protected $namespace = 'wc/v2';
/**
* Route base.

View File

@ -27,7 +27,7 @@ class WC_REST_Report_Sales_Controller extends WC_REST_Controller {
*
* @var string
*/
protected $namespace = 'wc/v1';
protected $namespace = 'wc/v2';
/**
* Route base.

View File

@ -27,7 +27,7 @@ class WC_REST_Report_Top_Sellers_Controller extends WC_REST_Report_Sales_Control
*
* @var string
*/
protected $namespace = 'wc/v1';
protected $namespace = 'wc/v2';
/**
* Route base.

View File

@ -27,7 +27,7 @@ class WC_REST_Reports_Controller extends WC_REST_Controller {
*
* @var string
*/
protected $namespace = 'wc/v1';
protected $namespace = 'wc/v2';
/**
* Route base.

View File

@ -18,7 +18,7 @@ class WC_REST_Settings_Controller extends WC_REST_Controller {
/**
* WP REST API namespace/version.
*/
protected $namespace = 'wc/v1';
protected $namespace = 'wc/v2';
/**
* Route base.

View File

@ -18,7 +18,7 @@ class WC_REST_Settings_Options_Controller extends WC_REST_Controller {
/**
* WP REST API namespace/version.
*/
protected $namespace = 'wc/v1';
protected $namespace = 'wc/v2';
/**
* Route base.

View File

@ -25,7 +25,7 @@ class WC_REST_Shipping_Methods_Controller extends WC_REST_Controller {
*
* @var string
*/
protected $namespace = 'wc/v1';
protected $namespace = 'wc/v2';
/**
* Route base.

View File

@ -18,9 +18,9 @@ if ( ! defined( 'ABSPATH' ) ) {
* REST API Shipping Zone Locations class.
*
* @package WooCommerce/API
* @extends WC_REST_Shipping_Zones_Controller_Base
* @extends WC_REST_Shipping_Zones_Controller
*/
class WC_REST_Shipping_Zone_Locations_Controller extends WC_REST_Shipping_Zones_Controller_Base {
class WC_REST_Shipping_Zone_Locations_Controller extends WC_REST_Shipping_Zones_Controller {
/**
* Register the routes for Shipping Zone Locations.

View File

@ -18,9 +18,9 @@ if ( ! defined( 'ABSPATH' ) ) {
* REST API Shipping Zone Methods class.
*
* @package WooCommerce/API
* @extends WC_REST_Shipping_Zones_Controller_Base
* @extends WC_REST_Shipping_Zones_Controller
*/
class WC_REST_Shipping_Zone_Methods_Controller extends WC_REST_Shipping_Zones_Controller_Base {
class WC_REST_Shipping_Zone_Methods_Controller extends WC_REST_Shipping_Zones_Controller {
/**
* Register the routes for Shipping Zone Methods.

View File

@ -18,9 +18,23 @@ if ( ! defined( 'ABSPATH' ) ) {
* REST API Shipping Zones class.
*
* @package WooCommerce/API
* @extends WC_REST_Shipping_Zones_Controller_Base
* @extends WC_REST_Controller
*/
class WC_REST_Shipping_Zones_Controller extends WC_REST_Shipping_Zones_Controller_Base {
class WC_REST_Shipping_Zones_Controller extends WC_REST_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc/v2';
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'shipping/zones';
/**
* Register the routes for Shipping Zones.
@ -75,6 +89,94 @@ class WC_REST_Shipping_Zones_Controller extends WC_REST_Shipping_Zones_Controlle
) );
}
/**
* Retrieve a Shipping Zone by it's ID.
*
* @param int $zone_id Shipping Zone ID.
* @return WC_Shipping_Zone|WP_Error
*/
protected function get_zone( $zone_id ) {
$zone = WC_Shipping_Zones::get_zone_by( 'zone_id', $zone_id );
if ( false === $zone ) {
return new WP_Error( 'woocommerce_rest_shipping_zone_invalid', __( 'Resource does not exist.', 'woocommerce' ), array( 'status' => 404 ) );
}
return $zone;
}
/**
* Check whether a given request has permission to read Shipping Zones.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function get_items_permissions_check( $request ) {
if ( ! wc_shipping_enabled() ) {
return new WP_Error( 'rest_no_route', __( 'Shipping is disabled.', 'woocommerce' ), array( 'status' => 404 ) );
}
if ( ! wc_rest_check_manager_permissions( 'settings', 'read' ) ) {
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
/**
* Check if a given request has access to create Shipping Zones.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function create_item_permissions_check( $request ) {
if ( ! wc_shipping_enabled() ) {
return new WP_Error( 'rest_no_route', __( 'Shipping is disabled.', 'woocommerce' ), array( 'status' => 404 ) );
}
if ( ! wc_rest_check_manager_permissions( 'settings', 'edit' ) ) {
return new WP_Error( 'woocommerce_rest_cannot_create', __( 'Sorry, you are not allowed to create resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
/**
* Check whether a given request has permission to edit Shipping Zones.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function update_items_permissions_check( $request ) {
if ( ! wc_shipping_enabled() ) {
return new WP_Error( 'rest_no_route', __( 'Shipping is disabled.', 'woocommerce' ), array( 'status' => 404 ) );
}
if ( ! wc_rest_check_manager_permissions( 'settings', 'edit' ) ) {
return new WP_Error( 'woocommerce_rest_cannot_edit', __( 'Sorry, you are not allowed to edit this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
/**
* Check whether a given request has permission to delete Shipping Zones.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function delete_items_permissions_check( $request ) {
if ( ! wc_shipping_enabled() ) {
return new WP_Error( 'rest_no_route', __( 'Shipping is disabled.', 'woocommerce' ), array( 'status' => 404 ) );
}
if ( ! wc_rest_check_manager_permissions( 'settings', 'delete' ) ) {
return new WP_Error( 'woocommerce_rest_cannot_edit', __( 'Sorry, you are not allowed to delete this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
/**
* Get a single Shipping Zone.
*

View File

@ -25,7 +25,7 @@ class WC_REST_System_Status_Controller extends WC_REST_Controller {
*
* @var string
*/
protected $namespace = 'wc/v1';
protected $namespace = 'wc/v2';
/**
* Route base.

View File

@ -25,7 +25,7 @@ class WC_REST_System_Status_Tools_Controller extends WC_REST_Controller {
*
* @var string
*/
protected $namespace = 'wc/v1';
protected $namespace = 'wc/v2';
/**
* Route base.

View File

@ -27,7 +27,7 @@ class WC_REST_Tax_Classes_Controller extends WC_REST_Controller {
*
* @var string
*/
protected $namespace = 'wc/v1';
protected $namespace = 'wc/v2';
/**
* Route base.

View File

@ -27,7 +27,7 @@ class WC_REST_Taxes_Controller extends WC_REST_Controller {
*
* @var string
*/
protected $namespace = 'wc/v1';
protected $namespace = 'wc/v2';
/**
* Route base.

View File

@ -27,7 +27,7 @@ class WC_REST_Webhook_Deliveries_Controller extends WC_REST_Controller {
*
* @var string
*/
protected $namespace = 'wc/v1';
protected $namespace = 'wc/v2';
/**
* Route base.

View File

@ -27,7 +27,7 @@ class WC_REST_Webhooks_Controller extends WC_REST_Posts_Controller {
*
* @var string
*/
protected $namespace = 'wc/v1';
protected $namespace = 'wc/v2';
/**
* Route base.

View File

@ -141,7 +141,6 @@ class WC_API extends WC_Legacy_API {
// Abstract controllers.
include_once( dirname( __FILE__ ) . '/abstracts/abstract-wc-rest-controller.php' );
include_once( dirname( __FILE__ ) . '/abstracts/abstract-wc-rest-posts-controller.php' );
include_once( dirname( __FILE__ ) . '/abstracts/abstract-wc-rest-shipping-zones-controller.php' );
include_once( dirname( __FILE__ ) . '/abstracts/abstract-wc-rest-terms-controller.php' );
include_once( dirname( __FILE__ ) . '/abstracts/abstract-wc-settings-api.php' );