PluginProbe ʕ •ᴥ•ʔ
Appointment Booking Plugin – LatePoint | Calendar & Scheduling for WordPress / 5.6.10
Appointment Booking Plugin – LatePoint | Calendar & Scheduling for WordPress v5.6.10
5.6.10 5.6.9 5.6.8 5.6.7 5.6.6 5.6.5 5.6.4 5.6.3 5.6.2 5.6.1 5.6.0 5.5.2 5.5.1 5.5.0 5.4.2 trunk 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.1.8 5.1.9 5.1.91 5.1.92 5.1.93 5.1.94 5.2.0 5.2.1 5.2.10 5.2.11 5.2.2 5.2.3 5.2.4 5.2.5 5.2.6 5.2.7 5.2.8 5.2.9 5.3.0 5.3.1 5.3.2 5.4.0 5.4.1
latepoint / lib / controllers / processes_controller.php
latepoint / lib / controllers Last commit date
activities_controller.php 3 months ago auth_controller.php 5 months ago booking_form_settings_controller.php 5 months ago bookings_controller.php 1 month ago calendars_controller.php 5 months ago carts_controller.php 1 month ago controller.php 5 months ago customer_cabinet_controller.php 1 month ago customers_controller.php 1 month ago dashboard_controller.php 3 months ago default_agent_controller.php 5 months ago events_controller.php 5 months ago form_fields_controller.php 1 month ago integrations_controller.php 5 months ago invoices_controller.php 1 month ago manage_booking_by_key_controller.php 5 months ago manage_order_by_key_controller.php 5 months ago notifications_controller.php 5 months ago orders_controller.php 1 month ago paypal_connect_controller.php 3 weeks ago pro_controller.php 1 month ago process_jobs_controller.php 5 months ago processes_controller.php 3 weeks ago razorpay_connect_controller.php 1 month ago search_controller.php 5 months ago services_controller.php 5 months ago settings_controller.php 3 months ago steps_controller.php 1 month ago stripe_connect_controller.php 1 month ago support_topics_controller.php 5 months ago todos_controller.php 5 months ago transactions_controller.php 1 month ago wizard_controller.php 1 month ago
processes_controller.php
439 lines
1 <?php
2 /*
3 * Copyright (c) 2022 LatePoint LLC. All rights reserved.
4 */
5
6 if ( ! defined( 'ABSPATH' ) ) {
7 exit; // Exit if accessed directly.
8 }
9
10
11 if ( ! class_exists( 'OsProcessesController' ) ) :
12
13
14 class OsProcessesController extends OsController {
15
16 function __construct() {
17 parent::__construct();
18
19
20 $this->views_folder = LATEPOINT_VIEWS_ABSPATH . 'processes/';
21 $this->vars['page_header'] = OsMenuHelper::get_menu_items_by_id( 'processes' );
22 $this->vars['pre_page_header'] = OsMenuHelper::get_label_by_id( 'processes' );
23 $this->vars['breadcrumbs'][] = array(
24 'label' => __( 'Workflows', 'latepoint' ),
25 'link' => OsRouterHelper::build_link( OsRouterHelper::build_route_name( 'processes', 'index' ) ),
26 );
27 }
28
29 public function new_form() {
30 $this->vars['process'] = new OsProcessModel();
31 $this->set_layout( 'none' );
32 $this->format_render( __FUNCTION__ );
33 }
34
35 public function reload_event_trigger_conditions() {
36 $event = new \LatePoint\Misc\ProcessEvent( [ 'type' => $this->params['event_type'] ] );
37 $trigger_conditions_form_section_html = OsProcessesHelper::trigger_conditions_html_for_event( $event );
38
39 if ( $this->get_return_format() == 'json' ) {
40 $this->send_json(
41 array(
42 'status' => LATEPOINT_STATUS_SUCCESS,
43 'message' => $trigger_conditions_form_section_html,
44 )
45 );
46 }
47 }
48
49
50 public function available_properties_for_object_code() {
51 $object_code = $this->params['object_code'];
52 $properties_for_select = \LatePoint\Misc\ProcessEvent::get_properties_for_object_code( $object_code, true );
53 $html = '';
54 foreach ( $properties_for_select as $property ) {
55 $html .= '<option value="' . $property['value'] . '">' . $property['label'] . '</option>';
56 }
57 if ( $this->get_return_format() == 'json' ) {
58 $this->send_json(
59 array(
60 'status' => LATEPOINT_STATUS_SUCCESS,
61 'message' => $html,
62 )
63 );
64 }
65 }
66
67
68 public function available_operators_for_trigger_condition_property() {
69 // example format: old_booking__agent_id
70 $property = $this->params['property'];
71 $operators = \LatePoint\Misc\ProcessEvent::trigger_condition_operators_for_property( $property );
72 $html = '';
73 foreach ( $operators as $value => $label ) {
74 $html .= '<option value="' . $value . '">' . $label . '</option>';
75 }
76 if ( $this->get_return_format() == 'json' ) {
77 $this->send_json(
78 array(
79 'status' => LATEPOINT_STATUS_SUCCESS,
80 'message' => $html,
81 )
82 );
83 }
84 }
85
86 public function available_values_for_trigger_condition_property() {
87 $values = [];
88 $property = $this->params['property'];
89 $operator = $this->params['operator'];
90 $trigger_condition_id = $this->params['trigger_condition_id'];
91 $values = OsProcessesHelper::values_for_trigger_condition_property( $property );
92 $message = \LatePoint\Misc\ProcessEvent::value_field_html_for_trigger_condition( $property, '', $trigger_condition_id );
93
94 if ( $this->get_return_format() == 'json' ) {
95 $this->send_json(
96 array(
97 'status' => LATEPOINT_STATUS_SUCCESS,
98 'message' => $message,
99 )
100 );
101 }
102 }
103
104 function destroy() {
105 if ( filter_var( $this->params['id'], FILTER_VALIDATE_INT ) ) {
106 $this->check_nonce( 'destroy_process_' . $this->params['id'] );
107 $process = new OsProcessModel( $this->params['id'] );
108 if ( $process->delete() ) {
109 $status = LATEPOINT_STATUS_SUCCESS;
110 $response_html = __( 'Process Removed', 'latepoint' );
111 } else {
112 $status = LATEPOINT_STATUS_ERROR;
113 $response_html = __( 'Error Removing Workflow', 'latepoint' );
114 }
115 } else {
116 $status = LATEPOINT_STATUS_SUCCESS;
117 $response_html = __( 'Process Removed', 'latepoint' );
118 }
119 if ( $this->get_return_format() == 'json' ) {
120 $this->send_json(
121 array(
122 'status' => $status,
123 'message' => $response_html,
124 )
125 );
126 }
127 }
128
129 function save() {
130 $process_data = $this->params['process'];
131 if ( ! empty( $process_data['id'] ) ) {
132 $this->check_nonce( 'edit_process_' . $process_data['id'] );
133 $process = new OsProcessModel( $process_data['id'] );
134 $new_process = false;
135 } else {
136 $this->check_nonce( 'new_process' );
137 $process = new OsProcessModel();
138 $new_process = true;
139 }
140
141 $process->status = $process_data['status'] ?? 'active';
142 $process->name = $process_data['name'];
143 $process->event_type = $process_data['event']['type'];
144 $actions = [];
145
146
147 // check if conditions are turned ON and exist in params
148 $trigger_conditions = ( isset( $process_data['event']['conditional'] ) && $process_data['event']['conditional'] == LATEPOINT_VALUE_ON && isset( $process_data['event']['trigger_conditions'] ) && ! empty( $process_data['event']['trigger_conditions'] ) ) ? $process_data['event']['trigger_conditions'] : [];
149 if ( isset( $process_data['actions'] ) ) {
150 $actions = OsProcessesHelper::iterate_trigger_conditions( $trigger_conditions, $process_data['actions'] );
151
152 $has_time_offset = ( ( $process_data['event']['has_time_offset'] ?? '' ) == LATEPOINT_VALUE_ON );
153 $time_offset = $process_data['event']['time_offset'] ?? [];
154 if ( $has_time_offset && ! empty( $time_offset['value'] ) && ! empty( $time_offset['unit'] ) ) {
155 $actions[0]['time_offset'] = $time_offset;
156
157 $actions[0]['time_offset']['value'] = absint( $time_offset['value'] );
158 $actions[0]['time_offset']['unit'] = sanitize_text_field( $time_offset['unit'] );
159 $actions[0]['time_offset']['before_after'] = sanitize_text_field( $time_offset['before_after'] ?? 'after' );
160 } else {
161 $actions[0]['time_offset'] = [];
162 }
163 } else {
164 $actions = [];
165 }
166
167 $process->actions_json = wp_json_encode( $actions );
168 $old_process = $process->is_new_record() ? [] : clone $process;
169 if ( $process->save() ) {
170 if ( ! $new_process ) {
171 // remove previously created jobs for this process that hasn't run yet
172 $jobs = new OsProcessJobModel();
173 $jobs->delete_where(
174 [
175 'process_id' => $process->id,
176 'status' => LATEPOINT_JOB_STATUS_SCHEDULED,
177 ]
178 );
179 /**
180 * Process was updated
181 *
182 * @param {OsProcessModel} $process instance of process model that was updated
183 * @param {OsProcessModel} $old_process instance of process model before it was updated
184 *
185 * @since 4.7.0
186 * @hook latepoint_process_updated
187 *
188 */
189 do_action( 'latepoint_process_updated', $process, $old_process );
190 } else {
191 /**
192 * Process was created
193 *
194 * @param {OsProcessModel} $process instance of process model that was created
195 *
196 * @since 4.7.0
197 * @hook latepoint_process_created
198 *
199 */
200 do_action( 'latepoint_process_created', $process );
201 }
202 $process->build_from_json();
203 OsProcessJobsHelper::recreate_jobs_for_existing_records( $process );
204 $message = __( 'Process Saved', 'latepoint' );
205 $status = LATEPOINT_STATUS_SUCCESS;
206 } else {
207 $message = __( 'Error saving process', 'latepoint' );
208 $status = LATEPOINT_STATUS_ERROR;
209 }
210
211 if ( $this->get_return_format() == 'json' ) {
212 $this->send_json(
213 array(
214 'status' => $status,
215 'message' => $message,
216 )
217 );
218 }
219 }
220
221 function new_trigger_condition() {
222 $process_event = new \LatePoint\Misc\ProcessEvent( [ 'type' => $this->params['event_type'] ] );
223 $trigger_condition_html = $process_event->generate_trigger_condition_form_html();
224 if ( $this->get_return_format() == 'json' ) {
225 $this->send_json(
226 array(
227 'status' => LATEPOINT_STATUS_SUCCESS,
228 'message' => $trigger_condition_html,
229 )
230 );
231 }
232 }
233
234 function new_action() {
235 $action = new \LatePoint\Misc\ProcessAction();
236 $process_id = ! empty( $this->params['process_id'] ) ? sanitize_text_field( $this->params['process_id'] ) : '';
237 $response_html = \LatePoint\Misc\ProcessAction::generate_form( $action, $process_id );
238
239 if ( $this->get_return_format() == 'json' ) {
240 $this->send_json(
241 array(
242 'status' => LATEPOINT_STATUS_SUCCESS,
243 'message' => $response_html,
244 )
245 );
246 }
247 }
248
249 function load_action_settings() {
250 $action = new \LatePoint\Misc\ProcessAction(
251 [
252 'type' => $this->params['action_type'],
253 'id' => $this->params['action_id'],
254 ]
255 );
256
257 $template_id = $this->params['template_id'] ?? false;
258 if ( $template_id ) {
259 $action->load_settings_from_template( $template_id );
260 }
261
262 if ( $this->get_return_format() == 'json' ) {
263 $this->send_json(
264 array(
265 'status' => LATEPOINT_STATUS_SUCCESS,
266 'message' => \LatePoint\Misc\ProcessAction::generate_settings_fields( $action ),
267 )
268 );
269 }
270 }
271
272 function index() {
273 $processes = new OsProcessModel();
274 $this->vars['processes'] = $processes->get_results_as_models();
275 $this->format_render( __FUNCTION__ );
276 }
277
278 function action_test_run() {
279 // Verify CSRF nonce
280 $this->check_nonce( 'test_process_action' );
281
282 $action = new \LatePoint\Misc\ProcessAction();
283 $action->set_from_params( $this->params['action'] );
284
285 $available_data_sources = $action->event->get_available_data_sources();
286 foreach ( $available_data_sources as $data_source ) {
287 $action->selected_data_objects[] = [
288 'model' => $data_source['model'],
289 'id' => $this->params['data_source'][ $data_source['name'] ],
290 ];
291 }
292
293 $result = $action->run();
294 if ( $this->get_return_format() == 'json' ) {
295 $this->send_json(
296 [
297 'status' => $result['status'],
298 'message' => $result['message'],
299 ]
300 );
301 }
302 }
303
304 function test_run() {
305 // Verify CSRF nonce - reuse the nonce from the process form
306 if ( ! empty( $this->params['process']['id'] ) ) {
307 $this->check_nonce( 'edit_process_' . $this->params['process']['id'] );
308 } else {
309 $this->check_nonce( 'new_process' );
310 }
311
312 $process = new OsProcessModel();
313 $process->set_from_params( $this->params['process'] );
314
315 $action_ids_to_run = isset( $this->params['action_ids'] ) ? explode( ',', $this->params['action_ids'] ) : [];
316 $data_sources = $this->params['data_source'];
317
318 $selected_data_objects = [];
319 foreach ( $data_sources as $source_id => $data_source_value ) {
320 $object_data = OsProcessesHelper::get_object_data_by_source( $source_id, $data_source_value );
321 if ( ! empty( $object_data ) ) {
322 $selected_data_objects[] = $object_data;
323 }
324 }
325
326 if ( $process->check_if_objects_satisfy_trigger_conditions( $selected_data_objects ) ) {
327 foreach ( $process->actions as $action ) {
328 if ( $action->status != LATEPOINT_STATUS_ACTIVE ) {
329 continue;
330 }
331 if ( ! in_array( $action->id, $action_ids_to_run ) ) {
332 continue;
333 }
334 $action->selected_data_objects = $selected_data_objects;
335 $action->run();
336 }
337 $status = LATEPOINT_STATUS_SUCCESS;
338 $message = __( 'Run complete', 'latepoint' ) . '. <a href="' . OsRouterHelper::build_link( [ 'activities', 'index' ] ) . '" target="_blank">' . __( 'view logs', 'latepoint' ) . '</a>';
339 } else {
340 $status = LATEPOINT_STATUS_ERROR;
341 $message = __( 'Trigger conditions not met', 'latepoint' );
342 }
343
344
345 if ( $this->get_return_format() == 'json' ) {
346 $this->send_json(
347 [
348 'status' => $status,
349 'message' => $message,
350 ]
351 );
352 }
353 }
354
355 function test_preview() {
356 $process = new OsProcessModel();
357 $process->set_from_params( $this->params['process'] );
358
359 $action_settings_html = '';
360 $available_data_sources = $process->event->get_available_data_sources();
361
362
363 foreach ( $available_data_sources as $data_source ) {
364 $action_settings_html .= OsFormHelper::select_field(
365 'data_source[' . $data_source['name'] . ']',
366 $data_source['label'],
367 $data_source['values'],
368 $data_source['values']['0']['value'],
369 [
370 'class' => 'process-test-data-source-selector',
371 'data-route' => OsRouterHelper::build_route_name( 'processes', 'reload_action_test_preview' ),
372 ]
373 );
374 }
375 $this->vars['action_settings_html'] = $action_settings_html;
376 $this->vars['process'] = $process;
377
378 $this->format_render( __FUNCTION__ );
379 }
380
381 function action_test_preview() {
382 $action = new \LatePoint\Misc\ProcessAction();
383 // because this data is part of a bigger process form, we need to extract just the action params
384 $action->set_from_params( reset( $this->params['process']['actions'] ) );
385 $action->event = new \LatePoint\Misc\ProcessEvent( [ 'type' => $this->params['process_event_type'] ] );
386 $action_settings_html = '';
387 $available_data_sources = $action->event->get_available_data_sources();
388 foreach ( $available_data_sources as $data_source ) {
389 $action_settings_html .= OsFormHelper::select_field(
390 'data_source[' . $data_source['name'] . ']',
391 $data_source['label'],
392 $data_source['values'],
393 $data_source['values']['0']['value'],
394 [
395 'class' => 'process-action-test-data-source-selector',
396 'data-route' => OsRouterHelper::build_route_name( 'processes', 'reload_action_test_preview' ),
397 ]
398 );
399 $action->selected_data_objects[] = [
400 'model' => $data_source['model'],
401 'id' => $data_source['values'][0]['value'],
402 ];
403 }
404 $action_settings_html .= OsFormHelper::hidden_field( 'action[type]', $action->type );
405 $action_settings_html .= OsFormHelper::hidden_field( 'action[event][type]', $action->event->type );
406 $action_settings_html .= OsFormHelper::get_hidden_fields_for_array( $action->settings, 'action[settings]' );
407 $preview_html = $action->generate_preview();
408
409 $this->vars['action'] = $action;
410 $this->vars['preview_html'] = $preview_html;
411 $this->vars['action_settings_html'] = $action_settings_html;
412
413 $this->format_render( __FUNCTION__ );
414 }
415
416
417 function reload_action_test_preview() {
418 $action = new \LatePoint\Misc\ProcessAction();
419 $action->set_from_params( $this->params['action'] );
420 $available_data_sources = $action->event->get_available_data_sources();
421 foreach ( $available_data_sources as $data_source ) {
422 $action->selected_data_objects[] = [
423 'model' => $data_source['model'],
424 'id' => $this->params['data_source'][ $data_source['name'] ],
425 ];
426 }
427 if ( $this->get_return_format() == 'json' ) {
428 $this->send_json(
429 [
430 'status' => LATEPOINT_STATUS_SUCCESS,
431 'message' => $action->generate_preview(),
432 ]
433 );
434 }
435 }
436 }
437
438 endif;
439