PluginProbe ʕ •ᴥ•ʔ
Appointment Booking Plugin – LatePoint | Calendar & Scheduling for WordPress / 5.2.10
Appointment Booking Plugin – LatePoint | Calendar & Scheduling for WordPress v5.2.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 4 months ago auth_controller.php 4 months ago booking_form_settings_controller.php 5 months ago bookings_controller.php 1 year ago calendars_controller.php 9 months ago carts_controller.php 1 year ago controller.php 9 months ago customer_cabinet_controller.php 4 months ago customers_controller.php 4 months ago dashboard_controller.php 9 months ago default_agent_controller.php 1 year ago events_controller.php 1 year ago form_fields_controller.php 9 months ago integrations_controller.php 9 months ago invoices_controller.php 4 months ago manage_booking_by_key_controller.php 4 months ago manage_order_by_key_controller.php 1 year ago notifications_controller.php 1 year ago orders_controller.php 4 months ago pro_controller.php 1 year ago process_jobs_controller.php 4 months ago processes_controller.php 4 months ago search_controller.php 1 year ago services_controller.php 9 months ago settings_controller.php 4 months ago steps_controller.php 9 months ago stripe_connect_controller.php 9 months ago support_topics_controller.php 1 year ago todos_controller.php 1 year ago transactions_controller.php 4 months ago wizard_controller.php 4 months ago
processes_controller.php
338 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( 'label' => __( 'Workflows', 'latepoint' ), 'link' => OsRouterHelper::build_link( OsRouterHelper::build_route_name( 'processes', 'index' ) ) );
24 }
25
26 public function new_form() {
27 $this->vars['process'] = new OsProcessModel();
28 $this->set_layout( 'none' );
29 $this->format_render( __FUNCTION__ );
30 }
31
32 public function reload_event_trigger_conditions() {
33 $event = new \LatePoint\Misc\ProcessEvent( [ 'type' => $this->params['event_type'] ] );
34 $trigger_conditions_form_section_html = OsProcessesHelper::trigger_conditions_html_for_event( $event );
35
36 if ( $this->get_return_format() == 'json' ) {
37 $this->send_json( array( 'status' => LATEPOINT_STATUS_SUCCESS, 'message' => $trigger_conditions_form_section_html ) );
38 }
39 }
40
41
42 public function available_properties_for_object_code() {
43 $object_code = $this->params['object_code'];
44 $properties_for_select = \LatePoint\Misc\ProcessEvent::get_properties_for_object_code( $object_code, true );
45 $html = '';
46 foreach ( $properties_for_select as $property ) {
47 $html .= '<option value="' . $property['value'] . '">' . $property['label'] . '</option>';
48 }
49 if ( $this->get_return_format() == 'json' ) {
50 $this->send_json( array( 'status' => LATEPOINT_STATUS_SUCCESS, 'message' => $html ) );
51 }
52 }
53
54
55 public function available_operators_for_trigger_condition_property() {
56 // example format: old_booking__agent_id
57 $property = $this->params['property'];
58 $operators = \LatePoint\Misc\ProcessEvent::trigger_condition_operators_for_property( $property );
59 $html = '';
60 foreach ( $operators as $value => $label ) {
61 $html .= '<option value="' . $value . '">' . $label . '</option>';
62 }
63 if ( $this->get_return_format() == 'json' ) {
64 $this->send_json( array( 'status' => LATEPOINT_STATUS_SUCCESS, 'message' => $html ) );
65 }
66 }
67
68 public function available_values_for_trigger_condition_property() {
69 $values = [];
70 $property = $this->params['property'];
71 $operator = $this->params['operator'];
72 $trigger_condition_id = $this->params['trigger_condition_id'];
73 $values = OsProcessesHelper::values_for_trigger_condition_property( $property );
74 if ( $this->get_return_format() == 'json' ) {
75 $this->send_json( array( 'status' => LATEPOINT_STATUS_SUCCESS,
76 'message' => OsFormHelper::multi_select_field( 'process[event][trigger_conditions][' . $trigger_condition_id . '][value]', false, $values, false, [] )
77 ) );
78 }
79 }
80
81 function destroy() {
82 if ( filter_var( $this->params['id'], FILTER_VALIDATE_INT ) ) {
83 $this->check_nonce( 'destroy_process_' . $this->params['id'] );
84 $process = new OsProcessModel( $this->params['id'] );
85 if ( $process->delete() ) {
86 $status = LATEPOINT_STATUS_SUCCESS;
87 $response_html = __( 'Process Removed', 'latepoint' );
88 } else {
89 $status = LATEPOINT_STATUS_ERROR;
90 $response_html = __( 'Error Removing Workflow', 'latepoint' );
91 }
92 } else {
93 $status = LATEPOINT_STATUS_SUCCESS;
94 $response_html = __( 'Process Removed', 'latepoint' );
95 }
96 if ( $this->get_return_format() == 'json' ) {
97 $this->send_json( array( 'status' => $status, 'message' => $response_html ) );
98 }
99 }
100
101 function save() {
102 $process_data = $this->params['process'];
103 if ( ! empty( $process_data['id'] ) ) {
104 $this->check_nonce( 'edit_process_' . $process_data['id'] );
105 $process = new OsProcessModel( $process_data['id'] );
106 $new_process = false;
107 } else {
108 $this->check_nonce( 'new_process' );
109 $process = new OsProcessModel();
110 $new_process = true;
111 }
112
113 $process->status = $process_data['status'] ?? 'active';
114 $process->name = $process_data['name'];
115 $process->event_type = $process_data['event']['type'];
116 $actions = [];
117
118
119 // check if conditions are turned ON and exist in params
120 $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'] : [];
121 if ( isset( $process_data['actions'] ) ) {
122 $actions = OsProcessesHelper::iterate_trigger_conditions( $trigger_conditions, $process_data['actions'] );
123
124 if ( $process_data['event']['has_time_offset'] == LATEPOINT_VALUE_ON ) {
125 $actions[0]['time_offset'] = $process_data['event']['time_offset'];
126 } else {
127 $actions[0]['time_offset'] = [];
128 }
129 } else {
130 $actions = [];
131 }
132
133 $process->actions_json = wp_json_encode( $actions );
134 $old_process = $process->is_new_record() ? [] : clone $process;
135 if ( $process->save() ) {
136 if ( ! $new_process ) {
137 // remove previously created jobs for this process that hasn't run yet
138 $jobs = new OsProcessJobModel();
139 $jobs->delete_where( [ 'process_id' => $process->id, 'status' => LATEPOINT_JOB_STATUS_SCHEDULED ] );
140 /**
141 * Process was updated
142 *
143 * @param {OsProcessModel} $process instance of process model that was updated
144 * @param {OsProcessModel} $old_process instance of process model before it was updated
145 *
146 * @since 4.7.0
147 * @hook latepoint_process_updated
148 *
149 */
150 do_action( 'latepoint_process_updated', $process, $old_process );
151 } else {
152 /**
153 * Process was created
154 *
155 * @param {OsProcessModel} $process instance of process model that was created
156 *
157 * @since 4.7.0
158 * @hook latepoint_process_created
159 *
160 */
161 do_action( 'latepoint_process_created', $process );
162 }
163 $process->build_from_json();
164 OsProcessJobsHelper::recreate_jobs_for_existing_records( $process );
165 $message = __( 'Process Saved', 'latepoint' );
166 $status = LATEPOINT_STATUS_SUCCESS;
167 } else {
168 $message = __( 'Error saving process', 'latepoint' );
169 $status = LATEPOINT_STATUS_ERROR;
170 }
171
172 if ( $this->get_return_format() == 'json' ) {
173 $this->send_json( array( 'status' => $status, 'message' => $message ) );
174 }
175 }
176
177 function new_trigger_condition() {
178 $process_event = new \LatePoint\Misc\ProcessEvent( [ 'type' => $this->params['event_type'] ] );
179 $trigger_condition_html = $process_event->generate_trigger_condition_form_html();
180 if ( $this->get_return_format() == 'json' ) {
181 $this->send_json( array( 'status' => LATEPOINT_STATUS_SUCCESS, 'message' => $trigger_condition_html ) );
182 }
183 }
184
185 function new_action() {
186 $action = new \LatePoint\Misc\ProcessAction();
187 $process_id = !empty($this->params['process_id']) ? sanitize_text_field($this->params['process_id']) : '';
188 $response_html = \LatePoint\Misc\ProcessAction::generate_form( $action, $process_id );
189
190 if ( $this->get_return_format() == 'json' ) {
191 $this->send_json( array( 'status' => LATEPOINT_STATUS_SUCCESS, 'message' => $response_html ) );
192 }
193 }
194
195 function load_action_settings() {
196 $action = new \LatePoint\Misc\ProcessAction( [ 'type' => $this->params['action_type'], 'id' => $this->params['action_id'] ] );
197
198 $template_id = $this->params['template_id'] ?? false;
199 if ( $template_id ) {
200 $action->load_settings_from_template( $template_id );
201 }
202
203 if ( $this->get_return_format() == 'json' ) {
204 $this->send_json( array( 'status' => LATEPOINT_STATUS_SUCCESS, 'message' => \LatePoint\Misc\ProcessAction::generate_settings_fields( $action ) ) );
205 }
206 }
207
208 function index() {
209 $processes = new OsProcessModel();
210 $this->vars['processes'] = $processes->get_results_as_models();
211 $this->format_render( __FUNCTION__ );
212 }
213
214 function action_test_run() {
215 // Verify CSRF nonce
216 $this->check_nonce( 'test_process_action' );
217
218 $action = new \LatePoint\Misc\ProcessAction();
219 $action->set_from_params( $this->params['action'] );
220
221 $available_data_sources = $action->event->get_available_data_sources();
222 foreach ( $available_data_sources as $data_source ) {
223 $action->selected_data_objects[] = [ 'model' => $data_source['model'], 'id' => $this->params['data_source'][ $data_source['name'] ] ];
224 }
225
226 $result = $action->run();
227 if ( $this->get_return_format() == 'json' ) {
228 $this->send_json( [ 'status' => $result['status'], 'message' => $result['message'] ] );
229 }
230 }
231
232 function test_run() {
233 // Verify CSRF nonce - reuse the nonce from the process form
234 if ( ! empty( $this->params['process']['id'] ) ) {
235 $this->check_nonce( 'edit_process_' . $this->params['process']['id'] );
236 } else {
237 $this->check_nonce( 'new_process' );
238 }
239
240 $process = new OsProcessModel();
241 $process->set_from_params( $this->params['process'] );
242
243 $action_ids_to_run = isset( $this->params['action_ids'] ) ? explode( ',', $this->params['action_ids'] ) : [];
244 $data_sources = $this->params['data_source'];
245
246 $selected_data_objects = [];
247 foreach ( $data_sources as $source_id => $data_source_value ) {
248 $object_data = OsProcessesHelper::get_object_data_by_source( $source_id, $data_source_value );
249 if(!empty($object_data)){
250 $selected_data_objects[] = $object_data;
251 }
252 }
253
254 if ( $process->check_if_objects_satisfy_trigger_conditions( $selected_data_objects ) ) {
255 foreach ( $process->actions as $action ) {
256 if ( $action->status != LATEPOINT_STATUS_ACTIVE ) {
257 continue;
258 }
259 if ( ! in_array( $action->id, $action_ids_to_run ) ) {
260 continue;
261 }
262 $action->selected_data_objects = $selected_data_objects;
263 $action->run();
264 }
265 $status = LATEPOINT_STATUS_SUCCESS;
266 $message = __( 'Run complete', 'latepoint' ) . '. <a href="' . OsRouterHelper::build_link( [ 'activities', 'index' ] ) . '" target="_blank">' . __( 'view logs', 'latepoint' ) . '</a>';
267 } else {
268 $status = LATEPOINT_STATUS_ERROR;
269 $message = __( 'Trigger conditions not met', 'latepoint' );
270 }
271
272
273 if ( $this->get_return_format() == 'json' ) {
274 $this->send_json( [ 'status' => $status, 'message' => $message ] );
275 }
276 }
277
278 function test_preview() {
279 $process = new OsProcessModel();
280 $process->set_from_params( $this->params['process'] );
281
282 $action_settings_html = '';
283 $available_data_sources = $process->event->get_available_data_sources();
284
285
286 foreach ( $available_data_sources as $data_source ) {
287 $action_settings_html .= OsFormHelper::select_field( 'data_source[' . $data_source['name'] . ']', $data_source['label'], $data_source['values'], $data_source['values']['0']['value'], [ 'class' => 'process-test-data-source-selector',
288 'data-route' => OsRouterHelper::build_route_name( 'processes', 'reload_action_test_preview' )
289 ] );
290 }
291 $this->vars['action_settings_html'] = $action_settings_html;
292 $this->vars['process'] = $process;
293
294 $this->format_render( __FUNCTION__ );
295 }
296
297 function action_test_preview() {
298 $action = new \LatePoint\Misc\ProcessAction();
299 // because this data is part of a bigger process form, we need to extract just the action params
300 $action->set_from_params( reset( $this->params['process']['actions'] ) );
301 $action->event = new \LatePoint\Misc\ProcessEvent( [ 'type' => $this->params['process_event_type'] ] );
302 $action_settings_html = '';
303 $available_data_sources = $action->event->get_available_data_sources();
304 foreach ( $available_data_sources as $data_source ) {
305 $action_settings_html .= OsFormHelper::select_field( 'data_source[' . $data_source['name'] . ']', $data_source['label'], $data_source['values'], $data_source['values']['0']['value'], [
306 'class' => 'process-action-test-data-source-selector',
307 'data-route' => OsRouterHelper::build_route_name( 'processes', 'reload_action_test_preview' )
308 ] );
309 $action->selected_data_objects[] = [ 'model' => $data_source['model'], 'id' => $data_source['values'][0]['value'] ];
310 }
311 $action_settings_html .= OsFormHelper::hidden_field( 'action[type]', $action->type );
312 $action_settings_html .= OsFormHelper::hidden_field( 'action[event][type]', $action->event->type );
313 $action_settings_html.= OsFormHelper::get_hidden_fields_for_array($action->settings, 'action[settings]');
314 $preview_html = $action->generate_preview();
315
316 $this->vars['action'] = $action;
317 $this->vars['preview_html'] = $preview_html;
318 $this->vars['action_settings_html'] = $action_settings_html;
319
320 $this->format_render( __FUNCTION__ );
321 }
322
323
324 function reload_action_test_preview() {
325 $action = new \LatePoint\Misc\ProcessAction();
326 $action->set_from_params( $this->params['action'] );
327 $available_data_sources = $action->event->get_available_data_sources();
328 foreach ( $available_data_sources as $data_source ) {
329 $action->selected_data_objects[] = [ 'model' => $data_source['model'], 'id' => $this->params['data_source'][ $data_source['name'] ] ];
330 }
331 if ( $this->get_return_format() == 'json' ) {
332 $this->send_json( [ 'status' => LATEPOINT_STATUS_SUCCESS, 'message' => $action->generate_preview() ] );
333 }
334 }
335
336 }
337
338 endif;