Passed
Push — master ( 6176aa...f7c939 )
by Mike
03:08
created

AbstractController::get_items_permissions_check()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * REST Controller
4
 *
5
 * It's required to follow "Controller Classes" guide before extending this class:
6
 * <https://developer.wordpress.org/rest-api/extending-the-rest-api/controller-classes/>
7
 *
8
 * @class   \WC_REST_Controller
9
 * @see     https://developer.wordpress.org/rest-api/extending-the-rest-api/controller-classes/
10
 * @package WooCommerce/RestApi
11
 */
12
13
namespace WooCommerce\RestApi\Controllers\Version4;
14
15
defined( 'ABSPATH' ) || exit;
16
17
use \WP_REST_Controller;
18
use \WooCommerce\RestApi\Controllers\Version4\Utilities\Permissions;
19
use \WooCommerce\RestApi\Controllers\Version4\Utilities\BatchTrait;
20
21
/**
22
 * Abstract Rest Controller Class
23
 *
24
 * @package WooCommerce/RestApi
25
 * @extends  WP_REST_Controller
26
 * @version  2.6.0
27
 */
28
abstract class AbstractController extends WP_REST_Controller {
29
	use BatchTrait;
30
31
	/**
32
	 * Endpoint namespace.
33
	 *
34
	 * @var string
35
	 */
36
	protected $namespace = 'wc/v4';
37
38
	/**
39
	 * Route base.
40
	 *
41
	 * @var string
42
	 */
43
	protected $rest_base = '';
44
45
	/**
46
	 * Permission to check.
47
	 *
48
	 * @var string
49
	 */
50
	protected $resource_type = '';
51
52
	/**
53
	 * Register route for items requests.
54
	 *
55
	 * @param array $methods Supported methods. read, create.
56
	 */
57
	protected function register_items_route( $methods = [ 'read', 'create' ] ) {
58
		$routes           = [];
59
		$routes['schema'] = [ $this, 'get_public_item_schema' ];
60
61
		if ( in_array( 'read', $methods, true ) ) {
62
			$routes[] = array(
63
				'methods'             => \WP_REST_Server::READABLE,
64
				'callback'            => array( $this, 'get_items' ),
65
				'permission_callback' => array( $this, 'get_items_permissions_check' ),
66
				'args'                => $this->get_collection_params(),
67
			);
68
		}
69
70
		if ( in_array( 'create', $methods, true ) ) {
71
			$routes[] = array(
72
				'methods'             => \WP_REST_Server::CREATABLE,
73
				'callback'            => array( $this, 'create_item' ),
74
				'permission_callback' => array( $this, 'create_item_permissions_check' ),
75
				'args'                => $this->get_endpoint_args_for_item_schema( \WP_REST_Server::CREATABLE ),
76
			);
77
		}
78
79
		register_rest_route(
80
			$this->namespace,
81
			'/' . $this->rest_base,
82
			$routes,
83
			true
84
		);
85
	}
86
87
	/**
88
	 * Register route for item create/get/delete/update requests.
89
	 *
90
	 * @param array $methods Supported methods. read, create.
91
	 */
92
	protected function register_item_route( $methods = [ 'read', 'edit', 'delete' ] ) {
93
		$routes           = [];
94
		$routes['schema'] = [ $this, 'get_public_item_schema' ];
95
		$routes['args']   = [
96
			'id' => [
97
				'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
98
				'type'        => 'integer',
99
			],
100
		];
101
102
		if ( in_array( 'read', $methods, true ) ) {
103
			$routes[] = array(
104
				'methods'             => \WP_REST_Server::READABLE,
105
				'callback'            => array( $this, 'get_item' ),
106
				'permission_callback' => array( $this, 'get_item_permissions_check' ),
107
				'args'                => array(
108
					'context' => $this->get_context_param(
109
						array(
110
							'default' => 'view',
111
						)
112
					),
113
				),
114
			);
115
		}
116
117
		if ( in_array( 'edit', $methods, true ) ) {
118
			$routes[] = array(
119
				'methods'             => \WP_REST_Server::EDITABLE,
120
				'callback'            => array( $this, 'update_item' ),
121
				'permission_callback' => array( $this, 'update_item_permissions_check' ),
122
				'args'                => $this->get_endpoint_args_for_item_schema( \WP_REST_Server::EDITABLE ),
123
			);
124
		}
125
126
		if ( in_array( 'delete', $methods, true ) ) {
127
			$routes[] = array(
128
				'methods'             => \WP_REST_Server::DELETABLE,
129
				'callback'            => array( $this, 'delete_item' ),
130
				'permission_callback' => array( $this, 'delete_item_permissions_check' ),
131
				'args'                => array(
132
					'force' => array(
133
						'default'     => false,
134
						'description' => __( 'Whether to bypass trash and force deletion.', 'woocommerce' ),
135
						'type'        => 'boolean',
136
					),
137
				),
138
			);
139
		}
140
141
		register_rest_route(
142
			$this->namespace,
143
			'/' . $this->rest_base . '/(?P<id>[\d]+)',
144
			$routes,
145
			true
146
		);
147
	}
148
149
	/**
150
	 * Add the schema from additional fields to an schema array.
151
	 *
152
	 * @param array $schema Schema array.
153
	 * @return array
154
	 */
155
	protected function add_additional_fields_schema( $schema ) {
156
		$schema               = parent::add_additional_fields_schema( $schema );
157
		$object_type          = $schema['title'];
158
		$schema['properties'] = apply_filters( 'woocommerce_rest_' . $object_type . '_schema', $schema['properties'] );
159
		return $schema;
160
	}
161
162
	/**
163
	 * Check whether a given request has permission to read webhooks.
164
	 *
165
	 * @param  \WP_REST_Request $request Full details about the request.
166
	 * @return \WP_Error|boolean
167
	 */
168
	public function get_items_permissions_check( $request ) {
169
		if ( ! Permissions::check_resource( $this->resource_type, 'read' ) ) {
170
			return new \WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
171
		}
172
		return true;
173
	}
174
175
	/**
176
	 * Check if a given request has access create webhooks.
177
	 *
178
	 * @param  \WP_REST_Request $request Full details about the request.
179
	 *
180
	 * @return bool|\WP_Error
181
	 */
182
	public function create_item_permissions_check( $request ) {
183
		if ( ! Permissions::check_resource( $this->resource_type, 'create' ) ) {
184
			return new \WP_Error( 'woocommerce_rest_cannot_create', __( 'Sorry, you are not allowed to create resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
185
		}
186
		return true;
187
	}
188
189
	/**
190
	 * Check if a given request has access to read a webhook.
191
	 *
192
	 * @param  \WP_REST_Request $request Full details about the request.
193
	 * @return \WP_Error|boolean
194
	 */
195
	public function get_item_permissions_check( $request ) {
196
		$id = $request->get_param( 'id' );
197
198
		if ( 0 !== $id && ! Permissions::check_resource( $this->resource_type, 'read', $id ) ) {
199
			return new \WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot view this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
200
		}
201
		return true;
202
	}
203
204
	/**
205
	 * Check if a given request has access update a webhook.
206
	 *
207
	 * @param  \WP_REST_Request $request Full details about the request.
208
	 *
209
	 * @return bool|\WP_Error
210
	 */
211
	public function update_item_permissions_check( $request ) {
212
		$id = $request->get_param( 'id' );
213
214
		if ( 0 !== $id && ! Permissions::check_resource( $this->resource_type, 'edit', $id ) ) {
215
			return new \WP_Error( 'woocommerce_rest_cannot_edit', __( 'Sorry, you are not allowed to edit this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
216
		}
217
		return true;
218
	}
219
220
	/**
221
	 * Check if a given request has access delete a webhook.
222
	 *
223
	 * @param  \WP_REST_Request $request Full details about the request.
224
	 *
225
	 * @return bool|\WP_Error
226
	 */
227
	public function delete_item_permissions_check( $request ) {
228
		$id = $request->get_param( 'id' );
229
230
		if ( 0 !== $id && ! Permissions::check_resource( $this->resource_type, 'delete', $id ) ) {
231
			return new \WP_Error( 'woocommerce_rest_cannot_delete', __( 'Sorry, you are not allowed to delete this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
232
		}
233
		return true;
234
	}
235
236
	/**
237
	 * Check if a given request has access batch create, update and delete items.
238
	 *
239
	 * @param  \WP_REST_Request $request Full details about the request.
240
	 *
241
	 * @return bool|\WP_Error
242
	 */
243
	public function batch_items_permissions_check( $request ) {
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

243
	public function batch_items_permissions_check( /** @scrutinizer ignore-unused */ $request ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
244
		if ( ! Permissions::check_resource( $this->resource_type, 'batch' ) ) {
245
			return new \WP_Error( 'woocommerce_rest_cannot_batch', __( 'Sorry, you are not allowed to batch manipulate this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
246
		}
247
		return true;
248
	}
249
}
250