PluginProbe
Booktics – Appointment Booking Calendar for Service Businesses / 1.0.3
Booktics – Appointment Booking Calendar for Service Businesses v1.0.3
1.0.29 1.0.28 1.0.27 1.0.26 1.0.25 1.0.24 1.0.23 1.0.22 1.0.21 1.0.20 1.0.19 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.2 1.0.3 All 31 releases
booktics / vendor / themewinter / email-notification-sdk / src / Flow / FlowAPI.php

FlowAPI.php in Booktics – Appointment Booking Calendar for Service Businesses 1.0.3, at vendor/themewinter/email-notification-sdk/src/Flow/FlowAPI.php

484 lines 14.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace ENS\Flow;
3
4 use ENS\Config;
5 use WP_HTTP_Response;
6 use WP_REST_Controller;
7
8 /**
9 * Class FlowAPI
10 *
11 * @package ENS\Flow
12 *
13 * @since 1.0.0
14 */
15 class FlowAPI extends WP_REST_Controller {
16
17 /**
18 * Namespace
19 *
20 * @since 1.0.0
21 *
22 * @var string
23 */
24 protected $namespace;
25
26 /**
27 * Rest base
28 *
29 * @since 1.0.0
30 *
31 * @var string
32 */
33 protected $rest_base = 'notification-flow';
34
35 /**
36 * FlowAPI constructor.
37 *
38 * @since 1.0.0
39 */
40 public function init() {
41 $plugin_slug = Config::get( 'plugin_slug' );
42 $this->namespace = $plugin_slug . '/v1';
43
44 add_action( 'rest_api_init', [$this, 'register_routes'] );
45 }
46
47 /**
48 * Register API endpoints routes.
49 *
50 * @since 1.0.0
51 *
52 * @return void
53 */
54 public function register_routes() {
55 register_rest_route(
56 $this->namespace,
57 $this->rest_base,
58 [
59 [ // Create
60 'methods' => \WP_REST_Server::CREATABLE,
61 'callback' => [$this, 'create_item'],
62 'permission_callback' => function () {
63 // return current_user_can( 'manage_options' );
64 return true;
65 },
66 ],
67 [ // Bulk delete
68 'methods' => \WP_REST_Server::DELETABLE,
69 'callback' => [$this, 'bulk_delete'],
70 'permission_callback' => function () {
71 // return current_user_can( 'manage_options' );
72 return true;
73 },
74 ],
75 [ // show list
76 'methods' => \WP_REST_Server::READABLE,
77 'callback' => [$this, 'get_items'],
78 'permission_callback' => function () {
79 // return current_user_can( 'manage_options' );
80 return true;
81 },
82 ],
83 ]
84 );
85
86 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<flow_id>[\d]+)', [
87 [ // Get single flow
88 'methods' => \WP_REST_Server::READABLE,
89 'callback' => [$this, 'get_item'],
90 'permission_callback' => function () {
91 // return current_user_can( 'manage_options' );
92 return true;
93 },
94 ],
95 [ // Update single flow
96 'methods' => \WP_REST_Server::EDITABLE,
97 'callback' => [$this, 'update_item'],
98 'permission_callback' => function () {
99 // return current_user_can( 'manage_options' );
100 return true;
101 },
102 ],
103 [ // Delete single flow
104 'methods' => \WP_REST_Server::DELETABLE,
105 'callback' => [$this, 'delete_item'],
106 'permission_callback' => function () {
107 // return current_user_can( 'manage_options' );
108 return true;
109 },
110 ],
111 ] );
112
113 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<flow_id>[\d]+)' . '/clone', [
114 [ // Clone flow
115 'methods' => \WP_REST_Server::CREATABLE,
116 'callback' => [$this, 'clone_item'],
117 'permission_callback' => function () {
118 // return current_user_can( 'manage_options' );
119 return true;
120 },
121 ],
122 ] );
123 }
124
125 /**
126 * Create flow
127 *
128 * @since 1.0.0
129 *
130 * @param WP_Rest_Request $request
131 *
132 * @return JSON Created flow data
133 */
134 public function create_item( $request ) {
135 $response = ens_verify_nonce( $request->get_header( 'x_wp_nonce' ) );
136 if ( $response instanceof WP_HTTP_Response ) {
137 return $response;
138 }
139
140 return $this->save_flow( $request );
141 }
142
143 /**
144 * Update flow
145 *
146 * @since 1.0.0
147 *
148 * @param WP_Rest_Request $request
149 *
150 * @return JSON Updated flow data
151 */
152 public function update_item( $request ) {
153 $response = ens_verify_nonce( $request->get_header( 'x_wp_nonce' ) );
154 if ( $response instanceof WP_HTTP_Response ) {
155 return $response;
156 }
157
158 $flow_id = (int) $request['flow_id'];
159 $flow = new Flow( $flow_id );
160
161 if ( !$flow->is_flow() ) {
162
163 $response = [
164 'success' => 0,
165 'status_code' => 422,
166 'message' => __( 'Invalid flow id.', Config::get( 'text_domain' ) ),
167 'data' => [],
168 ];
169
170 return new WP_HTTP_Response( $response, 422 );
171 }
172 return $this->save_flow( $request, $flow_id );
173 }
174
175 /**
176 * Get flow
177 *
178 * @since 1.0.0
179 *
180 * @param WP_Rest_Request $request
181 *
182 * @return JSON Flow data
183 */
184 public function get_item( $request ) {
185 $response = ens_verify_nonce( $request->get_header( 'x_wp_nonce' ) );
186 if ( $response instanceof WP_HTTP_Response ) {
187 return $response;
188 }
189
190 $flow_id = (int) $request['flow_id'];
191 $flow = new Flow( $flow_id );
192
193 if ( !$flow->is_flow() ) {
194
195 $data = [
196 'success' => 0,
197 'status_code' => 422,
198 'message' => __( 'Invalid flow id.', Config::get( 'text_domain' ) ),
199 'data' => [],
200 ];
201
202 return new WP_HTTP_Response( $data, 422 );
203 }
204
205 $response = [
206 'success' => 1,
207 'status_code' => 200,
208 'message' => __( 'Successfully retrieved flow', Config::get( 'text_domain' ) ),
209 'data' => $this->prepare_item( $flow_id ),
210 ];
211
212 return rest_ensure_response( $response, 200 );
213 }
214
215 /**
216 * Get all flows
217 *
218 * @since 1.0.0
219 *
220 * @param WP_Rest_Request $request
221 *
222 * @return JSON
223 */
224 public function get_items( $request ) {
225 $response = ens_verify_nonce( $request->get_header( 'x_wp_nonce' ) );
226 if ( $response instanceof WP_HTTP_Response ) {
227 return $response;
228 }
229
230 $per_page = !empty( $request['per_page'] ) ? intval( $request['per_page'] ) : -1;
231 $paged = !empty( $request['paged'] ) ? intval( $request['paged'] ) : -1;
232 $trigger = !empty( $request['trigger'] ) ? sanitize_text_field( $request['trigger'] ) : '';
233 $status = !empty( $request['status'] ) ? sanitize_text_field( $request['status'] ) : 'any';
234 $search_key = !empty( $request['search_key'] ) ? sanitize_text_field( $request['search_key'] ) : '';
235
236 $flows = Flow::all( [
237 'posts_per_page' => $per_page,
238 'paged' => $paged,
239 'trigger' => $trigger,
240 'post_status' => $status,
241 'search_key' => $search_key,
242 ] );
243
244 $items = [];
245
246 foreach ( $flows['items'] as $item ) {
247 $single_flow = $this->prepare_item( $item->ID );
248 $items[] = $single_flow;
249 }
250
251 $status_count = Flow::total_flows_group_by_status();
252
253 $data = [
254 'success' => 1,
255 'status_code' => 200,
256 'message' => __( 'Showing filtered flow list.', Config::get( 'text_domain' ) ),
257 'data' => [
258 'total' => $flows['total'],
259 'status_count' => $status_count,
260 'items' => $items,
261 ],
262 ];
263
264 return rest_ensure_response( $data, 200 );
265 }
266
267 /**
268 * Delete flow
269 *
270 * @since 1.0.0
271 *
272 * @param WP_Rest_Request $request
273 *
274 * @return JSON
275 */
276 public function delete_item( $request ) {
277 $response = ens_verify_nonce( $request->get_header( 'x_wp_nonce' ) );
278 if ( $response instanceof WP_HTTP_Response ) {
279 return $response;
280 }
281
282 $flow_id = (int) $request['flow_id'];
283 $flow = new Flow( $flow_id );
284
285 if ( !$flow->is_flow() ) {
286
287 $data = [
288 'success' => 0,
289 'status_code' => 422,
290 'message' => __( 'Invalid flow id.', Config::get( 'text_domain' ) ),
291 'data' => [],
292 ];
293
294 return new WP_HTTP_Response( $data, 422 );
295 }
296
297 $flow->delete();
298
299 $response = [
300 'success' => 1,
301 'status_code' => 200,
302 'message' => __( 'Successfully deleted flow', Config::get( 'text_domain' ) ),
303 'data' => [],
304 ];
305
306 return rest_ensure_response( $response, 200 );
307 }
308
309 /**
310 * Bulk delete flows
311 *
312 * @since 1.0.0
313 *
314 * @param WP_Rest_Request $request
315 *
316 * @return JSON
317 */
318 public function bulk_delete( $request ) {
319 $response = ens_verify_nonce( $request->get_header( 'x_wp_nonce' ) );
320 if ( $response instanceof WP_HTTP_Response ) {
321 return $response;
322 }
323
324 $flow_ids = json_decode( $request->get_body(), true );
325
326 foreach ( $flow_ids as $flow_id ) {
327 $flow = new Flow( $flow_id );
328
329 if ( !$flow->is_flow() ) {
330 $data = [
331 'success' => 0,
332 'status_code' => 422,
333 'message' => __( 'Invalid flow id.', Config::get( 'text_domain' ) ),
334 'data' => [],
335 ];
336
337 return new WP_HTTP_Response( $data, 422 );
338 }
339
340 $flow->delete();
341 }
342
343 return rest_ensure_response( [
344 'success' => 1,
345 'status_code' => 200,
346 'message' => __( 'Successfully deleted all flows', Config::get( 'text_domain' ) ),
347 'data' => [
348 'items' => $flow_ids,
349 ],
350 ], 200 );
351 }
352
353 /**
354 * Clone flow
355 *
356 * @since 1.0.0
357 *
358 * @param Object $request
359 *
360 * @return JSON
361 */
362 public function clone_item( $request ) {
363 $response = ens_verify_nonce( $request->get_header( 'x_wp_nonce' ) );
364 if ( $response instanceof WP_HTTP_Response ) {
365 return $response;
366 }
367
368 $flow_id = (int) $request['flow_id'];
369 $flow = new Flow( $flow_id );
370
371 if ( !$flow->is_flow() ) {
372 return new WP_HTTP_Response(
373 [
374 'success' => 0,
375 'status_code' => 422,
376 'message' => __( 'Invalid flow id.', Config::get( 'text_domain' ) ),
377 'data' => [],
378 ],
379 422
380 );
381 }
382
383 $flow->clone();
384
385 $item = $this->prepare_item( $flow );
386
387 $response = [
388 'success' => 1,
389 'status_code' => 200,
390 'message' => __( 'Successfully cloned requested flow', Config::get( 'text_domain' ) ),
391 'data' => $item,
392 ];
393
394 return rest_ensure_response( $response, 200 );
395 }
396
397 /**
398 * Save flow
399 *
400 * @since 1.0.0
401 *
402 * @param WP_Rest_Request $request
403 * @param int $id
404 *
405 * @return WP_REST_Response
406 */
407 public function save_flow( $request, $id = 0 ) {
408 $flow = new Flow( $id );
409 $request_body = $request->get_body();
410 $data = !is_null( $request_body ) ? json_decode( $request_body, true ) : [];
411
412 // Check if the input data is empty
413 if ( empty( $data ) ) {
414 return rest_ensure_response( [
415 'status_code' => 400,
416 'success' => 0,
417 'message' => __( 'No data provided to save the flow', Config::get( 'text_domain' ) ),
418 ], 400 );
419 }
420
421 $name = !empty( $data['name'] ) ? sanitize_text_field( $data['name'] ) : $flow->get_name();
422 $trigger = !empty( $data['trigger'] ) ? sanitize_text_field( $data['trigger'] ) : $flow->get_trigger();
423
424 $flow_config = !empty( $data['flow_config'] ) ? ens_sanitize_recursive( $data['flow_config'] ) : $flow->get_flow_config();
425 $status = ( isset( $data['status'] ) && !empty( $data['status'] ) ) ? sanitize_text_field( strtolower( $data['status'] ) ) : ( $flow->get_status() ? $flow->get_status() : 'draft' );
426
427 $action = $id ? 'updated' : 'created';
428
429 // Check if the input data is empty
430 if ( empty( $name ) || empty( $trigger ) ) {
431 return rest_ensure_response( [
432 'status_code' => 400,
433 'success' => 0,
434 'message' => __( 'Flow name & trigger is required', Config::get( 'text_domain' ) ),
435 ], 400 );
436 }
437
438 // preparing flow data.
439 $flow_data = [
440 'name' => $name,
441 'trigger' => $trigger,
442 'flow_config' => $flow_config,
443 'status' => $status,
444 ];
445
446 $flow->set_props( $flow_data );
447 $flow_id = $flow->save();
448
449 // Prepare response data.
450 $item = $this->prepare_item( $flow_id );
451
452 $response = [
453 'status_code' => 200,
454 'success' => 1,
455 // translators: %s is the name of the action
456 'message' => esc_html( sprintf( __( 'Successfully %s notification flow', Config::get( 'text_domain' ) ), $action ) ),
457 'data' => $item,
458 ];
459
460 return rest_ensure_response( $response, 200 );
461 }
462
463 /**
464 * Prepare item for response
465 *
466 * @since 1.0.0
467 *
468 * @param integer $flow_id
469 *
470 * @return array
471 */
472 public function prepare_item( $flow_id ) {
473 $flow = new Flow( $flow_id );
474 $data = [
475 'id' => $flow->get_id(),
476 'name' => $flow->get_name(),
477 'trigger' => $flow->get_trigger(),
478 'flow_config' => empty( $flow->get_flow_config() ) ? [] : $flow->get_flow_config(),
479 'status' => $flow->get_status(),
480 ];
481
482 return apply_filters( 'ens_flow_prepare_item', $data, $flow_id );
483 }
484 }