woocommerce /
woocommerce-rest-api
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * REST API Shipping Zones Controller base |
||
| 4 | * |
||
| 5 | * Houses common functionality between Shipping Zones and Locations. |
||
| 6 | * |
||
| 7 | * @package Automattic/WooCommerce/RestApi |
||
| 8 | * @since 3.0.0 |
||
| 9 | */ |
||
| 10 | |||
| 11 | namespace Automattic\WooCommerce\RestApi\Controllers\Version4; |
||
| 12 | |||
| 13 | defined( 'ABSPATH' ) || exit; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * REST API Shipping Zones base class. |
||
| 17 | * |
||
| 18 | * @package Automattic/WooCommerce/RestApi |
||
| 19 | * @extends AbstractController |
||
| 20 | */ |
||
| 21 | abstract class AbstractShippingZonesController extends AbstractController { |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Route base. |
||
| 25 | * |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | protected $rest_base = 'shipping/zones'; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Permission to check. |
||
| 32 | * |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | protected $resource_type = 'settings'; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Retrieve a Shipping Zone by it's ID. |
||
| 39 | * |
||
| 40 | * @param int $zone_id Shipping Zone ID. |
||
| 41 | * @return WC_Shipping_Zone|\WP_Error |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 42 | */ |
||
| 43 | protected function get_zone( $zone_id ) { |
||
| 44 | $zone = \WC_Shipping_Zones::get_zone_by( 'zone_id', $zone_id ); |
||
| 45 | |||
| 46 | if ( false === $zone ) { |
||
| 47 | return new \WP_Error( 'woocommerce_rest_shipping_zone_invalid', __( 'Resource does not exist.', 'woocommerce-rest-api' ), array( 'status' => 404 ) ); |
||
| 48 | } |
||
| 49 | |||
| 50 | return $zone; |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Check whether a given request has permission to read Shipping Zones. |
||
| 55 | * |
||
| 56 | * @param \WP_REST_Request $request Full details about the request. |
||
| 57 | * @return \WP_Error|boolean |
||
| 58 | */ |
||
| 59 | public function get_items_permissions_check( $request ) { |
||
| 60 | if ( ! wc_shipping_enabled() ) { |
||
| 61 | return new \WP_Error( 'rest_no_route', __( 'Shipping is disabled.', 'woocommerce-rest-api' ), array( 'status' => 404 ) ); |
||
| 62 | } |
||
| 63 | return parent::get_items_permissions_check( $request ); |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Check if a given request has access to create Shipping Zones. |
||
| 68 | * |
||
| 69 | * @param \WP_REST_Request $request Full details about the request. |
||
| 70 | * @return \WP_Error|boolean |
||
| 71 | */ |
||
| 72 | public function create_item_permissions_check( $request ) { |
||
| 73 | if ( ! wc_shipping_enabled() ) { |
||
| 74 | return new \WP_Error( 'rest_no_route', __( 'Shipping is disabled.', 'woocommerce-rest-api' ), array( 'status' => 404 ) ); |
||
| 75 | } |
||
| 76 | return parent::create_item_permissions_check( $request ); |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Check whether a given request has permission to edit Shipping Zones. |
||
| 81 | * |
||
| 82 | * @param \WP_REST_Request $request Full details about the request. |
||
| 83 | * @return \WP_Error|boolean |
||
| 84 | */ |
||
| 85 | public function update_item_permissions_check( $request ) { |
||
| 86 | if ( ! wc_shipping_enabled() ) { |
||
| 87 | return new \WP_Error( 'rest_no_route', __( 'Shipping is disabled.', 'woocommerce-rest-api' ), array( 'status' => 404 ) ); |
||
| 88 | } |
||
| 89 | return parent::update_item_permissions_check( $request ); |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Check whether a given request has permission to delete Shipping Zones. |
||
| 94 | * |
||
| 95 | * @param \WP_REST_Request $request Full details about the request. |
||
| 96 | * @return \WP_Error|boolean |
||
| 97 | */ |
||
| 98 | public function delete_item_permissions_check( $request ) { |
||
| 99 | if ( ! wc_shipping_enabled() ) { |
||
| 100 | return new \WP_Error( 'rest_no_route', __( 'Shipping is disabled.', 'woocommerce-rest-api' ), array( 'status' => 404 ) ); |
||
| 101 | } |
||
| 102 | return parent::delete_item_permissions_check( $request ); |
||
| 103 | } |
||
| 104 | } |
||
| 105 |