Completed
Push — master ( 15aa29...17da96 )
by Claudio
18:39 queued 11s
created

abstract-wc-rest-shipping-zones-controller.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * REST API Shipping Zones Controller base
4
 *
5
 * Houses common functionality between Shipping Zones and Locations.
6
 *
7
 * @package  WooCommerce/Abstracts
8
 * @since    3.0.0
9
 */
10
11 1
if ( ! defined( 'ABSPATH' ) ) {
12
	exit;
13
}
14
15
/**
16
 * REST API Shipping Zones base class.
17
 *
18
 * @package WooCommerce/API
19
 * @extends WC_REST_Controller
20
 */
21
abstract class WC_REST_Shipping_Zones_Controller_Base extends WC_REST_Controller {
22
23
	/**
24
	 * Endpoint namespace.
25
	 *
26
	 * @var string
27
	 */
28
	protected $namespace = 'wc/v2';
29
30
	/**
31
	 * Route base.
32
	 *
33
	 * @var string
34
	 */
35
	protected $rest_base = 'shipping/zones';
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
42
	 */
43 34
	protected function get_zone( $zone_id ) {
44 34
		$zone = WC_Shipping_Zones::get_zone_by( 'zone_id', $zone_id );
45
46 34
		if ( false === $zone ) {
47 12
			return new WP_Error( 'woocommerce_rest_shipping_zone_invalid', __( 'Resource does not exist.', 'woocommerce' ), array( 'status' => 404 ) );
48
		}
49
50 22
		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 46 View Code Duplication
	public function get_items_permissions_check( $request ) {
0 ignored issues
show
This method seems to be duplicated in your project.

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.

Loading history...
60 46
		if ( ! wc_shipping_enabled() ) {
61 2
			return new WP_Error( 'rest_no_route', __( 'Shipping is disabled.', 'woocommerce' ), array( 'status' => 404 ) );
62
		}
63
64 44
		if ( ! wc_rest_check_manager_permissions( 'settings', 'read' ) ) {
65 8
			return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
66
		}
67
68 36
		return true;
69
	}
70
71
	/**
72
	 * Check if a given request has access to create Shipping Zones.
73
	 *
74
	 * @param  WP_REST_Request $request Full details about the request.
75
	 * @return WP_Error|boolean
76
	 */
77 18 View Code Duplication
	public function create_item_permissions_check( $request ) {
0 ignored issues
show
This method seems to be duplicated in your project.

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.

Loading history...
78 18
		if ( ! wc_shipping_enabled() ) {
79 2
			return new WP_Error( 'rest_no_route', __( 'Shipping is disabled.', 'woocommerce' ), array( 'status' => 404 ) );
80
		}
81
82 16
		if ( ! wc_rest_check_manager_permissions( 'settings', 'edit' ) ) {
83 6
			return new WP_Error( 'woocommerce_rest_cannot_create', __( 'Sorry, you are not allowed to create resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
84
		}
85
86 10
		return true;
87
	}
88
89
	/**
90
	 * Check whether a given request has permission to edit Shipping Zones.
91
	 *
92
	 * @param  WP_REST_Request $request Full details about the request.
93
	 * @return WP_Error|boolean
94
	 */
95 32 View Code Duplication
	public function update_items_permissions_check( $request ) {
0 ignored issues
show
This method seems to be duplicated in your project.

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.

Loading history...
96 32
		if ( ! wc_shipping_enabled() ) {
97
			return new WP_Error( 'rest_no_route', __( 'Shipping is disabled.', 'woocommerce' ), array( 'status' => 404 ) );
98
		}
99
100 32
		if ( ! wc_rest_check_manager_permissions( 'settings', 'edit' ) ) {
101 2
			return new WP_Error( 'woocommerce_rest_cannot_edit', __( 'Sorry, you are not allowed to edit this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
102
		}
103
104 30
		return true;
105
	}
106
107
	/**
108
	 * Check whether a given request has permission to delete Shipping Zones.
109
	 *
110
	 * @param  WP_REST_Request $request Full details about the request.
111
	 * @return WP_Error|boolean
112
	 */
113 24 View Code Duplication
	public function delete_items_permissions_check( $request ) {
0 ignored issues
show
This method seems to be duplicated in your project.

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.

Loading history...
114 24
		if ( ! wc_shipping_enabled() ) {
115
			return new WP_Error( 'rest_no_route', __( 'Shipping is disabled.', 'woocommerce' ), array( 'status' => 404 ) );
116
		}
117
118 24
		if ( ! wc_rest_check_manager_permissions( 'settings', 'delete' ) ) {
119 2
			return new WP_Error( 'woocommerce_rest_cannot_edit', __( 'Sorry, you are not allowed to delete this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
120
		}
121
122 22
		return true;
123
	}
124
125
}
126