PluginProbe
Booktics – Appointment Booking Calendar for Service Businesses / trunk
Booktics – Appointment Booking Calendar for Service Businesses vtrunk
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 trunk, at vendor/themewinter/email-notification-sdk/src/Flow/FlowAPI.php

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