PluginProbe
weForms – Easy Drag & Drop Contact Form Builder For WordPress / 1.4.1
weForms – Easy Drag & Drop Contact Form Builder For WordPress v1.4.1
1.6.7 1.6.8 1.6.9 1.6.12 1.6.13 1.6.14 1.6.15 1.6.16 1.6.17 1.6.18 1.6.19 1.6.2 1.6.20 1.6.21 1.6.22 1.6.23 1.6.24 1.6.25 1.6.26 1.6.27 1.6.28 1.6.3 1.6.4 1.6.5 1.6.6 All 74 releases
weforms / includes / api / class-weforms-entries-controller.php

class-weforms-entries-controller.php in weForms – Easy Drag & Drop Contact Form Builder For WordPress 1.4.1, at includes/api/class-weforms-entries-controller.php

334 lines 11.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Entry manager class
5 *
6 * @since 1.4.2
7 */
8
9 class Weforms_Entry_Controller extends Weforms_REST_Controller {
10
11 /**
12 * Endpoint namespace
13 *
14 * @var string
15 */
16 protected $namespace = 'weforms/v1';
17
18 /**
19 * Route name
20 *
21 * @var string
22 */
23 protected $rest_base = 'entries';
24
25 /**
26 * Register all routes releated with forms
27 *
28 * @return void
29 */
30 public function register_routes() {
31
32 register_rest_route(
33 $this->namespace,
34 '/'. $this->rest_base . '/(?P<entry_id>[\d]+)',
35 array(
36 array(
37 'methods' => WP_REST_Server::DELETABLE,
38 'callback' => array( $this, 'delete_items' ),
39 'permission_callback' => array( $this, 'delete_item_permissions_check' ),
40 'args' => array(
41 'entry_id' => array(
42 'required' => true,
43 'type' => 'integer',
44 'sanitize_callback' => 'absint',
45 'description' => __( 'Entry id', 'weforms' ),
46 'validate_callback' => array( $this, 'is_entry_exists' ),
47 ),
48 'force' => array(
49 'type' => 'boolean',
50 'default' => false,
51 'description' => __( 'Whether to bypass trash and force deletion.', 'weforms' ),
52 ),
53 )
54 ),
55 )
56 );
57
58 register_rest_route(
59 $this->namespace,
60 '/'. $this->rest_base,
61 array(
62 array(
63 'methods' => WP_REST_Server::DELETABLE,
64 'callback' => array( $this, 'bulk_delete_items' ),
65 'permission_callback' => array( $this, 'delete_item_permissions_check' ),
66 'args' => array(
67 'entry_id' => array(
68 'required' => true,
69 'type' => 'object',
70 'description' => __( 'Entry id', 'weforms' ),
71 'validate_callback' => array( $this, 'is_entry_exists' ),
72 ),
73 'force' => array(
74 'type' => 'boolean',
75 'default' => false,
76 'description' => __( 'Whether to bypass trash and force deletion.', 'weforms' ),
77 ),
78 )
79 ),
80 )
81 );
82
83 register_rest_route(
84 $this->namespace,
85 '/'. $this->rest_base . '/(?P<entry_id>[\d]+)/restore',
86 array(
87 array(
88 'methods' => WP_REST_Server::READABLE,
89 'callback' => array( $this, 'restore_items' ),
90 'permission_callback' => array( $this, 'update_item_permissions_check' ),
91 'args' => array(
92 'entry_id' => array(
93 'required' => true,
94 'type' => 'integer',
95 'sanitize_callback' => 'absint',
96 'description' => __( 'Entry id', 'weforms' ),
97 'validate_callback' => array( $this, 'is_restore_exists' ),
98 )
99 )
100 ),
101 )
102 );
103
104 register_rest_route(
105 $this->namespace,
106 '/'. $this->rest_base . '/restore',
107 array(
108 array(
109 'methods' => WP_REST_Server::READABLE,
110 'callback' => array( $this, 'bulk_restore_items' ),
111 'permission_callback' => array( $this, 'delete_item_permissions_check' ),
112 'args' => array(
113 'entry_id' => array(
114 'required' => true,
115 'type' => 'object',
116 'description' => __( 'Entry id', 'weforms' ),
117 'validate_callback' => array( $this, 'is_restore_exists' ),
118 ),
119 )
120 ),
121 )
122 );
123
124 }
125
126 /**
127 * Restore Items From Trash
128 *
129 * @since 1.4.2
130 *
131 * @param WP_REST_Request $request
132 *
133 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
134 **/
135 public function bulk_restore_items( $request ) {
136 $entry_ids = isset( $request['entry_id'] ) ? array_map( 'absint', $request['entry_id'] ) : array();
137
138 if ( ! $entry_ids ) {
139 return new WP_Error( 'rest_weforms_invalid_entry',__( 'No entry ids provided!','weforms' ),array( 'status' => 404 ) );
140 }
141
142 foreach ( $entry_ids as $entry_id ) {
143 $status = weforms_change_entry_status( $entry_id, 'publish' );
144
145 if( $status ) {
146 $response['message'][$entry_id] = __( 'Entry Restore successfully ', 'weforms' );
147 } else {
148 $response['message'][$entry_id] = __( 'Entry Restore failed ', 'weforms' );
149 }
150 }
151
152 $response = $this->prepare_response_for_collection( $response );
153 $response = rest_ensure_response( $response );
154
155 return $response;
156 }
157
158 /**
159 * Restore Item From Trash
160 *
161 * @since 1.4.2
162 *
163 * @param WP_REST_Request $request
164 *
165 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
166 **/
167 public function restore_items( $request ) {
168 $entry_id = isset( $request['entry_id'] ) ? intval( $request['entry_id'] ) : 0;
169
170 weforms_change_entry_status( $entry_id, 'publish' );
171
172 $response['id'] = $entry_id;
173 $response['message'] = __( 'Entry Restore Successfully successfully ', 'weforms' );
174 $response['data']['status'] = 200;
175
176 $response = $this->prepare_response_for_collection( $response );
177 $response = rest_ensure_response( $response );
178
179 return $response;
180 }
181
182 /**
183 * Bulk Delele Item
184 *
185 * @since 1.4.2
186 *
187 * @param WP_REST_Request $request
188 *
189 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
190 **/
191 public function bulk_delete_items( $request ) {
192 $entry_ids = isset( $request['entry_id'] ) ? array_map( 'absint', $request['entry_id'] ) : array();
193 $force = (bool) $request['force'];
194
195 if ( ! $entry_ids ) {
196 return new WP_Error('rest_weforms_invalid_entry',__( 'No entry ids provided!', 'weforms' ));
197 }
198
199 $response = array();
200
201 foreach ( $entry_ids as $entry_id ) {
202
203 if ( $force ) {
204
205 $status = weforms_delete_entry( $entry_id );
206
207 if( $status ) {
208 $response['message'][$entry_id] = __( 'Entry Deleted successfully ', 'weforms' );
209 } else {
210 $response['message'][$entry_id] = __( 'Entry Not found ', 'weforms' );
211 }
212 } else {
213 $status = weforms_change_entry_status( $entry_id, 'trash' );
214
215 if( $status ) {
216 $response['message'][$entry_id] = __( 'Entry Move To Trash successfully ', 'weforms' );
217 } else {
218 $response['message'][$entry_id] = __( 'Entry Not found ', 'weforms' );
219 }
220 }
221 }
222
223 $response = $this->prepare_response_for_collection( $response );
224 $response = rest_ensure_response( $response );
225
226 return $response;
227 }
228
229 /**
230 * Delele Item
231 *
232 * @since 1.4.2
233 *
234 * @param WP_REST_Request $request
235 *
236 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
237 **/
238 public function delete_items( $request ) {
239 $entry_id = $request['entry_id'];
240 $force = (bool) $request['force'];
241
242 if ( $force ) {
243 weforms_delete_entry( $entry_id );
244
245 $entry_array['id'] = $entry_id;
246 $entry_array['message'] = __( 'Entry Deleted successfully', 'weforms' );
247 $entry_array['data']['status'] = 200;
248 } else {
249 $status = weforms_change_entry_status( $entry_id, 'trash' );
250
251 $entry_array['id'] = $entry_id;
252 $entry_array['message'] = __( 'Entry Move To Trash successfully', 'weforms' );
253 $entry_array['data']['status'] = 200;
254 }
255
256 $response = $this->prepare_response_for_collection( $entry_array, $request );
257 $response = rest_ensure_response( $response );
258
259 return $response;
260 }
261
262
263 /**
264 * Check is entry in trash exist or not
265 *
266 * @since 1.4.2
267 *
268 * @param string $param
269 * @param WP_REST_Request $request
270 * @param string $key
271 *
272 * @return boolean
273 */
274 public function is_restore_exists( $param, $request, $key ) {
275 global $wpdb;
276
277 // if( is_array( $param ) ) {
278 if( is_array( $request['entry_id'] ) ) {
279 $entry_id = implode( ",", $param );
280 $querystr = "
281 SELECT $wpdb->weforms_entries.id
282 FROM $wpdb->weforms_entries
283 WHERE $wpdb->weforms_entries.ID IN ( $entry_id )
284 AND $wpdb->weforms_entries.status = \"trash\"
285 ";
286 } else {
287 // $entry_id = (int) $param;
288 $entry_id = (int) $request['entry_id'];
289 $querystr = "
290 SELECT $wpdb->weforms_entries.id
291 FROM $wpdb->weforms_entries
292 WHERE $wpdb->weforms_entries.ID = $entry_id
293 AND $wpdb->weforms_entries.status = \"trash\"
294 ";
295 }
296
297 $result = $wpdb->get_results( $querystr );
298
299 if ( empty( $result ) ) {
300 return false;
301 } else {
302 return true;
303 }
304 }
305
306 /**
307 * Get the Form entries schema, conforming to JSON Schema
308 *
309 * @since 1.4.2
310 *
311 * @return array
312 */
313 public function get_item_schema() {
314 $schema = array(
315 '$schema' => 'http://json-schema.org/draft-04/schema#',
316 'title' => 'entries',
317 'type' => 'object',
318 'properties' => array(
319 'entry_id' => array(
320 'description' => __( 'Unique identifier for the object', 'weforms' ),
321 'type' => 'integer',
322 'sanitize_callback' => 'absint',
323 'validate_callback' => array( $this, 'is_entry_exists' ),
324 'context' => array( 'embed', 'view', 'edit' ),
325 'required' => true,
326 'readonly' => true,
327 ),
328 ),
329 );
330
331 return $schema;
332 }
333 }
334