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