PluginProbe
WPBot – ChatBot Conversational Forms / 1.2.0
WPBot – ChatBot Conversational Forms v1.2.0
1.5.0 1.4.8 1.4.7 trunk 0.9.2 0.9.3 0.9.5 0.9.6 0.9.7 0.9.8 0.9.9 1.0.0 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.6 1.1.7 1.1.8 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 All 36 releases
conversational-forms / classes / api / crud.php

crud.php in WPBot – ChatBot Conversational Forms 1.2.0, at classes/api/crud.php

395 lines 10.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Abstract class the all REST API route collections that follow CRUD pattern should extend
5 *
6 * @package Caldera_Forms Modified by QuantumCloud
7 * @author Josh Pollock <Josh@CalderaWP.com>
8 * @license GPL-2.0+
9 * @link
10 * @copyright 2016 CalderaWP LLC
11 */
12 abstract class Qcformbuilder_Forms_API_CRUD implements Qcformbuilder_Forms_API_Route {
13
14 /**
15 * Form object for this response
16 *
17 * @since 1.5.0
18 *
19 * @var Qcformbuilder_Forms_API_Form|Qcformbuilder_Forms_API_Privacy
20 */
21 protected $form;
22
23 /**
24 * Namespace for API
25 *
26 * @since 1.5.0
27 *
28 * @var string
29 */
30 protected $namespace;
31
32 /**
33 * @inheritdoc
34 *
35 * @since 1.4.4
36 */
37 public function add_routes( $namespace ) {
38 $this->namespace = $namespace;
39 register_rest_route( $namespace, $this->non_id_endpoint_url(), array(
40 array(
41 'methods' => \WP_REST_Server::READABLE,
42 'callback' => array( $this, 'get_items' ),
43 'permission_callback' => array( $this, 'get_items_permissions_check' ),
44 'args' => $this->get_items_args(),
45 ),
46 array(
47 'methods' => \WP_REST_Server::CREATABLE,
48 'callback' => array( $this, 'create_item' ),
49 'permission_callback' => array( $this, 'create_item_permissions_check' ),
50 'args' => $this->request_args()
51 ),
52 )
53 );
54 register_rest_route( $namespace, $this->id_endpoint_url(), array(
55 array(
56 'methods' => \WP_REST_Server::READABLE,
57 'callback' => array( $this, 'get_item' ),
58 'permission_callback' => array( $this, 'get_item_permissions_check' ),
59 'args' => $this->get_item_args()
60 ),
61 array(
62 'methods' => \WP_REST_Server::EDITABLE,
63 'callback' => array( $this, 'update_item' ),
64 'permission_callback' => array( $this, 'update_item_permissions_check' ),
65 'args' => $this->request_args( )
66 ),
67 array(
68 'methods' => \WP_REST_Server::DELETABLE,
69 'callback' => array( $this, 'delete_item' ),
70 'permission_callback' => array( $this, 'delete_item_permissions_check' ),
71 'args' => array(
72 'force' => array(
73 'default' => false,
74 'required' => false,
75 ),
76 'all' => array(
77 'default' => false,
78 'required' => false,
79 ),
80 'id' => array(
81 'default' => 0,
82 'sanatization_callback' => 'absint'
83 )
84 ),
85 ),
86 )
87 );
88
89 register_rest_route( $namespace, '/' . $this->route_base(), array(
90 'methods' => 'GET',
91 'callback' => array( $this, 'index' ),
92 ) );
93 }
94
95 /**
96 * Get the allow attributes for get items calls
97 *
98 * @since unknown
99 *
100 * @return array
101 */
102 public function get_item_args(){
103 return array(
104 'context' => array(
105 'default' => 'view',
106 ),
107 'entry_list_only_fields' => array(
108 'required' => false,
109 'default' => false,
110 )
111 );
112 }
113
114 /**
115 * Callback for the index of this collection
116 *
117 * @since 1.4.4
118 *
119 * @param WP_REST_Request $request
120 *
121 * @return Qcformbuilder_Forms_API_Response
122 */
123 public function index( WP_REST_Request $request ){
124 /** @var WP_REST_Server $wp_rest_server */
125 global $wp_rest_server;
126 $routes = $wp_rest_server->get_routes();
127 $endpoints = array();
128 foreach ( $routes as $route => $route_endpoints ){
129 if( false !== strpos( $route, $this->namespace . '/' . $this->route_base() ) ){
130 $endpoints[ $route ] = $route_endpoints;
131 }
132 }
133
134 $data = array(
135 'namespace' => $this->namespace,
136 'routes' => $wp_rest_server->get_data_for_routes( $endpoints, $request['context'] ),
137 );
138
139 return new Qcformbuilder_Forms_API_Response( $data, 200, array() );
140
141 }
142
143 /**
144 * Define query arguments for GET requests
145 *
146 * @since 1.4.4
147 *
148 * @return array
149 */
150 public function request_args(){
151 //must ovveride, should be abstract but PHP5.2
152 _doing_it_wrong( __FUNCTION__, '', '1.5.0' );
153 }
154
155 /**
156 * Define query arguments for POST/PUT requests
157 *
158 * @since 1.8.0
159 *
160 * @return array
161 */
162 public function args_for_create(){
163 return $this->request_args();
164 }
165
166 /**
167 * Check if a given request has access to get items
168 *
169 * @since 1.4.4
170 *
171 * @param WP_REST_Request $request Full data about the request.
172 * @return \WP_Error|bool
173 */
174 public function get_items_permissions_check( WP_REST_Request $request ){
175 //must ovveride, should be abstract but PHP5.2
176 _doing_it_wrong( __FUNCTION__, '', '1.5.0' );
177 }
178
179 /**
180 * Check if a given request has access to get a specific item
181 *
182 * @since 1.4.4
183 *
184 * @param WP_REST_Request $request Full data about the request.
185 * @return \WP_Error|bool
186 */
187 public function get_item_permissions_check( WP_REST_Request $request ) {
188 return $this->get_items_permissions_check( $request );
189 }
190
191 /**
192 * Check if a given request has access to create items
193 *
194 * @since 1.4.4
195 *
196 * @param WP_REST_Request $request Full data about the request.
197 * @return \WP_Error|bool
198 */
199 public function create_item_permissions_check( WP_REST_Request $request ){
200 //must ovveride, should be abstract but PHP5.2
201 _doing_it_wrong( __FUNCTION__, '', '1.5.0' );
202 }
203
204 /**
205 * Check if a given request has access to update a specific item
206 *
207 * @since 1.4.4
208 *
209 * @param WP_REST_Request $request Full data about the request.
210 * @return \WP_Error|bool
211 */
212 public function update_item_permissions_check( WP_REST_Request $request ) {
213 return $this->create_item_permissions_check( $request );
214 }
215
216 /**
217 * Check if a given request has access to delete a specific item
218 *
219 * @since 1.4.4
220 *
221 * @param WP_REST_Request $request Full data about the request.
222 * @return \WP_Error|bool
223 */
224 public function delete_item_permissions_check( WP_REST_Request $request ) {
225 return $this->create_item_permissions_check( $request );
226 }
227
228 /**
229 * Get a collection of items
230 *
231 * @since 1.4.4
232 *
233 * @param WP_REST_Request $request Full data about the request.
234 * @return \WP_Error|\WP_REST_Response
235 */
236 public function get_items( WP_REST_Request $request ) {
237 return $this->not_yet_response();
238 }
239
240 /**
241 * Get one item from the collection
242 *
243 * @since 1.4.4
244 *
245 * @param WP_REST_Request $request Full data about the request.
246 * @return \WP_Error|\WP_REST_Response
247 */
248 public function get_item( WP_REST_Request $request ) {
249 return $this->not_yet_response();
250 }
251
252 /**
253 * Create one item from the collection
254 *
255 * @since 1.4.4
256 *
257 * @param WP_REST_Request $request Full data about the request.
258 * @return Qcformbuilder_Forms_API_Response|Qcformbuilder_Forms_API_Error
259 */
260 public function create_item( WP_REST_Request $request ) {
261 return $this->not_yet_response();
262 }
263
264 /**
265 * Update one item from the collection
266 *
267 * @since 1.4.4
268 *
269 * @param WP_REST_Request $request Full data about the request.
270 * @return Qcformbuilder_Forms_API_Response|Qcformbuilder_Forms_API_Error
271 */
272 public function update_item( WP_REST_Request $request ) {
273 return $this->not_yet_response();
274 }
275
276 /**
277 * Delete one item from the collection
278 *
279 * @since 1.4.4
280 *
281 * @param WP_REST_Request $request Full data about the request.
282 * @return Qcformbuilder_Forms_API_Response|Qcformbuilder_Forms_API_Error
283 */
284 public function delete_item( WP_REST_Request $request ) {
285 return $this->not_yet_response();
286 }
287
288 /**
289 * Return a 501 error for non-existant route
290 *
291 * @since 1.4.4
292 *
293 * @return Qcformbuilder_Forms_API_Response
294 */
295 protected function not_yet_response() {
296 $error = new Qcformbuilder_Forms_API_Error( 'not-implemented-yet', __( 'Route Not Yet Implemented :(', 'qcformbuilder-forms' ) );
297 return new Qcformbuilder_Forms_API_Response( $error, 501, array() );
298 }
299
300 /**
301 * Get class shortname and use as base
302 *
303 * @since 1.4.4
304 *
305 * MUST ovveride in subclass with a hardcoded string.
306 */
307 protected function route_base() {
308 //must ovveride, should be abstract but PHP5.2
309 _doing_it_wrong( __FUNCTION__, '', '1.5.0' );
310 }
311
312 /**
313 * Form the endpoint URL that deos not include item ID
314 *
315 * Used by for get_items() and create_items()
316 *
317 * @since 1.4.4
318 *
319 * @return string
320 */
321 protected function non_id_endpoint_url() {
322 return '/' . $this->route_base();
323
324 }
325
326 /**
327 * Form the endpoint URL that includes item ID
328 *
329 * Used by for get_item() and update_time() and delete_item()
330 *
331 * @since 1.4.4
332 *
333 * @return string
334 */
335 public function id_endpoint_url() {
336 return '/' . $this->route_base() . '/(?P<id>[\w-]+)';
337 }
338
339 /**
340 * @return array
341 */
342 public function get_items_args() {
343 return array(
344 'page' => array(
345 'default' => 1,
346 'sanitize_callback' => 'absint',
347 ),
348 'per_page' => array(
349 'default' => 20,
350 'sanitize_callback' => 'absint',
351 ),
352 'limit' => array(
353 'default' => 10,
354 'sanitize_callback' => 'absint',
355 )
356 );
357 }
358
359 /**
360 * Factory for Qcformbuilder_Forms_API_Form objects
361 *
362 * @since 1.5.0
363 *
364 * @param string $id Form ID
365 * @param WP_REST_Request $request Current REST API request
366 * @param bool $set_prop Optional. Set in $form property of object if true, the default. If false, return.
367 * @param bool $privacy_context Optional. If false, a Qcformbuilder_Forms_API_Privacy is returned. If true, the default, a Qcformbuilder_Forms_API_Form is returned.
368 *
369 * @return Qcformbuilder_Forms_API_Form|Qcformbuilder_Forms_API_Privacy
370 * @throws Exception
371 */
372 protected function form_object_factory( $id, WP_REST_Request $request, $set_prop = true, $privacy_context= false ){
373 $form = Qcformbuilder_Forms_Forms::get_form( $id );
374
375 if( empty( $form ) || ( empty( $form[ 'ID' ] ) && empty( $form[ 'name' ] ) ) ){
376 throw new Exception();
377 }
378
379 if ($privacy_context) {
380 $obj = new Qcformbuilder_Forms_API_Form($form);
381 }else{
382 $obj = new Qcformbuilder_Forms_API_Privacy($form);
383 }
384
385 $obj->set_request( $request );
386 if ( $set_prop ) {
387 $this->form = $obj;
388 } else {
389
390 return $obj;
391 }
392
393 }
394
395 }