1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* REST API Shipping Zones Controller base |
4
|
|
|
* |
5
|
|
|
* Houses common functionality between Shipping Zones and Locations. |
6
|
|
|
* |
7
|
|
|
* @author WooThemes |
8
|
|
|
* @category API |
9
|
|
|
* @package WooCommerce/API |
10
|
|
|
* @since 2.7.0 |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
14
|
|
|
exit; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* REST API Shipping Zones base class. |
19
|
|
|
* |
20
|
|
|
* @package WooCommerce/API |
21
|
|
|
* @extends WC_REST_Controller |
22
|
|
|
*/ |
23
|
|
|
abstract class WC_REST_Shipping_Zones_Controller_Base extends WC_REST_Controller { |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Endpoint namespace. |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $namespace = 'wc/v1'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Route base. |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $rest_base = 'shipping/zones'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Retrieve a Shipping Zone by it's ID. |
41
|
|
|
* |
42
|
|
|
* @param int $zone_id Shipping Zone ID. |
43
|
|
|
* @return WC_Shipping_Zone|WP_Error |
44
|
|
|
*/ |
45
|
|
|
protected function get_zone( $zone_id ) { |
46
|
|
|
$zone = WC_Shipping_Zones::get_zone_by( 'zone_id', $zone_id ); |
47
|
|
|
|
48
|
|
|
if ( false === $zone ) { |
49
|
|
|
return new WP_Error( 'woocommerce_rest_shipping_zone_invalid', __( "Resource doesn't exist.", 'woocommerce' ), array( 'status' => 404 ) ); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $zone; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Check whether a given request has permission to read Shipping Zones. |
57
|
|
|
* |
58
|
|
|
* @param WP_REST_Request $request Full details about the request. |
59
|
|
|
* @return WP_Error|boolean |
60
|
|
|
*/ |
61
|
|
View Code Duplication |
public function get_items_permissions_check( $request ) { |
|
|
|
|
62
|
|
|
if ( ! wc_shipping_enabled() ) { |
63
|
|
|
return new WP_Error( 'rest_no_route', __( 'Shipping is disabled.' ), array( 'status' => 404 ) ); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if ( ! wc_rest_check_manager_permissions( 'settings', 'read' ) ) { |
67
|
|
|
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return true; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Check whether a given request has permission to edit Shipping Zones. |
75
|
|
|
* |
76
|
|
|
* @param WP_REST_Request $request Full details about the request. |
77
|
|
|
* @return WP_Error|boolean |
78
|
|
|
*/ |
79
|
|
View Code Duplication |
public function update_items_permissions_check( $request ) { |
|
|
|
|
80
|
|
|
if ( ! wc_shipping_enabled() ) { |
81
|
|
|
return new WP_Error( 'rest_no_route', __( 'Shipping is disabled.' ), array( 'status' => 404 ) ); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if ( ! wc_rest_check_manager_permissions( 'settings', 'edit' ) ) { |
85
|
|
|
return new WP_Error( 'woocommerce_rest_cannot_update', __( 'Sorry, you cannot update resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return true; |
89
|
|
|
} |
90
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.