Completed
Push — develop ( e63648...3edd81 )
by Zack
19:54 queued 12:35
created

Route::get_item()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 18 and the first side effect is on line 15.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * @package   GravityView
4
 * @license   GPL2+
5
 * @author    Josh Pollock <[email protected]>
6
 * @link      http://gravityview.co
7
 * @copyright Copyright 2015, Katz Web Services, Inc.
8
 *
9
 * @since 2.0
10
 */
11
namespace GV\REST;
12
13
/** If this file is called directly, abort. */
14 1
if ( ! defined( 'GRAVITYVIEW_DIR' ) ) {
15
	die();
16
}
17
18
abstract class Route extends \WP_REST_Controller {
19
	/**
20
	 * Route Name
21
	 *
22
	 * @since 2.0
23
	 * @access protected
24
	 * @var string
25
	 */
26
	protected $route_name;
27
28
	/**
29
	 * Sub type, forms {$namespace}/route_name/{id}/sub_type type endpoints
30
	 *
31
	 * @since 2.0
32
	 * @access protected
33
	 * @var string
34
	 */
35
	protected $sub_type;
36
37
	/**
38
	 * Register the routes for the objects of the controller.
39
	 */
40 12
	public function register_routes() {
41 12
		$namespace = \GV\REST\Core::get_namespace();
42 12
		$base = $this->get_route_name();
43
44 12
		register_rest_route( $namespace, '/' . $base, array(
45
			array(
46 12
				'methods'         => \WP_REST_Server::READABLE,
47 12
				'callback'        => array( $this, 'get_items' ),
48 12
				'permission_callback' => array( $this, 'get_items_permissions_check' ),
49
				'args'            => array(
50
					'page' => array(
51
						'default' => 1,
52
						'sanitize_callback' => 'absint'
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
53
					),
54
					'limit' => array(
55
						'default' => 10,
56
						'sanitize_callback' => 'absint'
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
57
					),
58
					'post_id' => array(
59
						'default' => null,
60
						'sanitize_callback' => 'absint'
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
61
					)
62
				)
63
			),
64
			array(
65
				'methods'         => \WP_REST_Server::CREATABLE,
66 12
				'callback'        => array( $this, 'create_item' ),
67 12
				'permission_callback' => array( $this, 'create_item_permissions_check' ),
68 12
				'args'            => $this->create_item_args()
69
70
			),
71
		) );
72 12
		register_rest_route( $namespace, '/' . $base . '/(?P<id>[\d]+)', array(
73
			array(
74 12
				'methods'         => \WP_REST_Server::READABLE,
75 12
				'callback'        => array( $this, 'get_item' ),
76 12
				'permission_callback' => array( $this, 'get_item_permissions_check' ),
77
				'args'            => array(
78
					'context'          => array(
79
						'default'      => 'view',
80
					),
81
				),
82
			),
83
			array(
84
				'methods'         => \WP_REST_Server::EDITABLE,
85 12
				'callback'        => array( $this, 'update_item' ),
86 12
				'permission_callback' => array( $this, 'update_item_permissions_check' ),
87 12
				'args'              => $this->update_item_args(),
88
			),
89
			array(
90
				'methods'  => \WP_REST_Server::DELETABLE,
91 12
				'callback' => array( $this, 'delete_item' ),
92 12
				'permission_callback' => array( $this, 'delete_item_permissions_check' ),
93
				'args'     => array(
94
					'force'    => array(
95
						'default'      => false,
96
					),
97
				),
98
			),
99
		) );
100
101 12
		$sub_type = $this->get_sub_type();
102
		
103 12
		$format = '(?:\.(?P<format>html|json|csv))?';
104
105 12
		register_rest_route( $namespace, '/' . $base . '/(?P<id>[\d]+)' . '/' . $sub_type . $format, array(
106
			array(
107 12
				'methods'         => \WP_REST_Server::READABLE,
108 12
				'callback'        => array( $this, 'get_sub_items' ),
109 12
				'permission_callback' => array( $this, 'get_sub_items_permissions_check' ),
110
				'args'            => array(
111
					'page' => array(
112
						'default' => 1,
113
						'sanitize_callback' => 'absint'
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
114
					),
115
					'limit' => array(
116
						'default' => 10,
117
						'sanitize_callback' => 'absint'
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
118
					),
119
					'post_id' => array(
120
						'default' => null,
121
						'sanitize_callback' => 'absint'
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
122
					)
123
				)
124
			),
125
			array(
126
				'methods'         => \WP_REST_Server::CREATABLE,
127 12
				'callback'        => array( $this, 'create_sub_item' ),
128 12
				'permission_callback' => array( $this, 'create_sub_item_permissions_check' ),
129 12
				'args'     => $this->create_sub_item_args()
130
			),
131
		) );
132
133 12
		$format = '(?:\.(?P<format>html|json))?';
134
135 12
		register_rest_route( $namespace, sprintf( '/%s/(?P<id>[\d]+)/%s/(?P<s_id>[\w-]+)%s', $base, $sub_type, $format ) , array(
136
			array(
137 12
				'methods'         => \WP_REST_Server::READABLE,
138 12
				'callback'        => array( $this, 'get_sub_item' ),
139 12
				'permission_callback' => array( $this, 'get_sub_item_permissions_check' ),
140
				'args'            => array(
141
					'context'          => array(
142
						'default'      => 'view',
143
					),
144
				),
145
			),
146
			array(
147
				'methods'         => \WP_REST_Server::EDITABLE,
148 12
				'callback'        => array( $this, 'update_sub_item' ),
149 12
				'permission_callback' => array( $this, 'update_sub_item_permissions_check' ),
150 12
				'args'     => $this->update_sub_item_args()
151
			),
152
			array(
153
				'methods'  => \WP_REST_Server::DELETABLE,
154 12
				'callback' => array( $this, 'delete_sub_item' ),
155 12
				'permission_callback' => array( $this, 'delete_sub_item_permissions_check' ),
156
				'args'     => array(
157
					'force'    => array(
158
						'default'      => false,
159
					),
160
				),
161
			),
162
		) );
163 12
	}
164
165
	/**
166
	 * Get route name
167
	 *
168
	 * MUST SET route_name property in subclass!
169
	 *
170
	 * @since 2.0
171
	 * @access protected
172
	 * @return string
173
	 */
174 12
	protected function get_route_name() {
175 12
		if ( is_string( $this->route_name ) ) {
176 12
			return $this->route_name;
177
		} else {
178
			_doing_it_wrong( __METHOD__, __( 'Must set route name in subclass.', 'gravityview' ), '2.0' );
179
			return '';
180
		}
181
	}
182
183
	/**
184
	 * Get sub_type
185
	 *
186
	 * MUST SET sub_type property in subclass!
187
	 *
188
	 * @since 2.0
189
	 * @access protected
190
	 * @return string
191
	 */
192 12
	protected function get_sub_type() {
193 12
		if ( is_string( $this->sub_type ) ) {
194 12
			return $this->sub_type;
195
		} else {
196
			_doing_it_wrong( __METHOD__, __( 'Must set route sub type in subclass.', 'gravityview' ), '2.0' );
197
			return '';
198
		}
199
	}
200
201
	/**
202
	 * Get a collection of items
203
	 *
204
	 * @param \WP_REST_Request $request Full data about the request.
205
	 * @return \WP_Error|\WP_REST_Response
206
	 */
207
	public function get_items( $request ) {
208
		return $this->not_implemented();
209
	}
210
211
	/**
212
	 * Get one item from the collection
213
	 *
214
	 * @param \WP_REST_Request $request Full data about the request.
215
	 * @return \WP_Error|\WP_REST_Response
216
	 */
217
	public function get_item( $request ) {
218
		return $this->not_implemented();
219
	}
220
221
	/**
222
	 * Create one item from the collection
223
	 *
224
	 * @param \WP_REST_Request $request Full data about the request.
225
	 * @return \WP_REST_Response
226
	 */
227
	public function create_item( $request ) {
228
		return $this->not_implemented();
229
	}
230
231
	/**
232
	 * Update one item from the collection
233
	 *
234
	 * @param \WP_REST_Request $request Full data about the request.
235
	 * @return \WP_REST_Response
236
	 */
237
	public function update_item( $request ) {
238
		return $this->not_implemented();
239
	}
240
241
	/**
242
	 * Delete one item from the collection
243
	 *
244
	 * @param \WP_REST_Request $request Full data about the request.
245
	 * @return \WP_REST_Response
246
	 */
247
	public function delete_item( $request ) {
248
		return $this->not_implemented();
249
	}
250
251
252
	/**
253
	 * Get a collection of items
254
	 *
255
	 * @param \WP_REST_Request $request Full data about the request.
256
	 * @return \WP_Error|\WP_REST_Response
257
	 */
258
	public function get_sub_items( $request ) {
259
		return $this->not_implemented();
260
261
	}
262
263
	/**
264
	 * Get one item from the collection
265
	 *
266
	 * @param \WP_REST_Request $request Full data about the request.
267
	 * @return \WP_Error|\WP_REST_Response
268
	 */
269
	public function get_sub_item( $request ) {
270
		return $this->not_implemented();
271
	}
272
273
	/**
274
	 * Create one item from the collection
275
	 *
276
	 * @param \WP_REST_Request $request Full data about the request.
277
	 * @return \WP_REST_Response
278
	 */
279
	public function create_sub_item( $request ) {
280
		return $this->not_implemented();
281
	}
282
283
	/**
284
	 * Update one item from the collection for sub items
285
	 *
286
	 * @param \WP_REST_Request $request Full data about the request.
287
	 * @return \WP_REST_Response
288
	 */
289
	public function update_sub_item( $request ) {
290
		return $this->not_implemented();
291
	}
292
293
	/**
294
	 * Delete one item from the collection for sub items
295
	 *
296
	 * @param \WP_REST_Request $request Full data about the request.
297
	 * @return \WP_REST_Response
298
	 */
299
	public function delete_sub_item( $request ) {
300
		return $this->not_implemented();
301
	}
302
303
	/**
304
	 * Check if a given request has access to get items
305
	 *
306
	 * @param \WP_REST_Request $request Full data about the request.
307
	 * @return \WP_REST_Response
308
	 */
309
	public function get_items_permissions_check( $request ) {
310
		return $this->not_implemented();
311
	}
312
313
	/**
314
	 * Check if a given request has access to get a specific item
315
	 *
316
	 * @param \WP_REST_Request $request Full data about the request.
317
	 * @return \WP_REST_Response
318
	 */
319
	public function get_item_permissions_check( $request ) {
320
		return $this->not_implemented();
321
	}
322
323
	/**
324
	 * Check if a given request has access to create items
325
	 *
326
	 * @param \WP_REST_Request $request Full data about the request.
327
	 * @return \WP_REST_Response
328
	 */
329
	public function create_item_permissions_check( $request ) {
330
		return $this->not_implemented();
331
	}
332
333
	/**
334
	 * Check if a given request has access to update a specific item
335
	 *
336
	 * @param \WP_REST_Request $request Full data about the request.
337
	 * @return \WP_REST_Response
338
	 */
339
	public function update_item_permissions_check( $request ) {
340
		return $this->not_implemented();
341
	}
342
343
	/**
344
	 * Check if a given request has access to delete a specific item
345
	 *
346
	 * @param \WP_REST_Request $request Full data about the request.
347
	 * @return \WP_REST_Response
348
	 */
349
	public function delete_item_permissions_check( $request ) {
350
		return $this->not_implemented();
351
	}
352
353
	/**
354
	 * Prepare the item for create or update operation
355
	 *
356
	 * @todo ZACK - Use this as genric prepare to save or remove from usage.
357
	 * @param \WP_REST_Request $request Request object
358
	 * @return \WP_REST_Response
359
	 */
360
	protected function prepare_item_for_database( $request ) {
361
		return $this->not_implemented();
362
	}
363
364
	/**
365
	 * Prepare the item for the REST response
366
	 *
367
	 *  @todo ZACK - Use this as generic prepare for response or remove from usage
368
	 *
369
	 * @since 2.0
370
	 * @param mixed $item WordPress representation of the item.
371
	 * @param \WP_REST_Request $request Request object.
372
	 * @return \WP_REST_Response
373
	 */
374
	public function prepare_item_for_response( $item, $request ) {
375
		return $this->not_implemented();
376
	}
377
378
379
	/**
380
	 * Generic response for routes not yet implemented
381
	 *
382
	 * @since 2.0
383
	 * @return \WP_REST_Response
384
	 */
385
	protected function not_implemented(  ) {
386
		$error = new \WP_Error( 'not-implemented-yet', __( 'Endpoint Not Yet Implemented.', 'gravityview' )  );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces before closing bracket; 2 found
Loading history...
387
		return new \WP_REST_Response( $error, 501 );
388
	}
389
390
	/**
391
	 * Fallback if subclass doesn't define routes
392
	 *
393
	 * Returns empty array for args instead of making an error.
394
	 *
395
	 * @since 2.0
396
	 * @param $method
397
	 * @return array
398
	 */
399 12
	public function __call( $method, $args ) {
400 12
		if ( in_array( $method, array(
401 12
			'create_item_args',
402
			'update_item_args',
403
			'create_sub_item_args',
404
			'update_sub_item_args'
0 ignored issues
show
introduced by
Comma required after last value in array declaration
Loading history...
405
		) ) ) {
406 12
			return array();
407
		}
408
	}
409
}
410