PluginProbe ʕ •ᴥ•ʔ
LatePoint – Calendar Booking Plugin for Appointments and Events / 5.5.2
LatePoint – Calendar Booking Plugin for Appointments and Events v5.5.2
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 / models / process_job_model.php
latepoint / lib / models Last commit date
activity_model.php 3 months ago agent_meta_model.php 3 months ago agent_model.php 3 months ago booking_meta_model.php 3 months ago booking_model.php 3 months ago bundle_meta_model.php 3 months ago bundle_model.php 3 months ago cart_item_model.php 3 months ago cart_meta_model.php 3 months ago cart_model.php 3 months ago connector_model.php 3 months ago customer_meta_model.php 3 months ago customer_model.php 1 month ago invoice_model.php 3 months ago join_bundles_services_model.php 3 months ago location_category_model.php 3 months ago location_model.php 3 months ago meta_model.php 3 months ago model.php 3 months ago off_period_model.php 3 months ago order_intent_meta_model.php 3 months ago order_intent_model.php 3 months ago order_item_model.php 3 months ago order_meta_model.php 3 months ago order_model.php 3 months ago otp_model.php 3 months ago payment_request_model.php 3 months ago process_job_model.php 3 months ago process_model.php 3 months ago recurrence_model.php 3 months ago service_category_model.php 3 months ago service_meta_model.php 3 months ago service_model.php 3 months ago session_model.php 3 months ago settings_model.php 3 months ago step_settings_model.php 3 months ago transaction_intent_model.php 3 months ago transaction_model.php 3 months ago transaction_refund_model.php 3 months ago work_period_model.php 3 months ago
process_job_model.php
312 lines
1 <?php
2 /*
3 * Copyright (c) 2022 LatePoint LLC. All rights reserved.
4 */
5
6 class OsProcessJobModel extends OsModel {
7 var $id,
8 $process_id,
9 $object_id,
10 $object_model_type,
11 $to_run_after_utc,
12 $status = LATEPOINT_JOB_STATUS_SCHEDULED,
13 $settings,
14 $run_result,
15 $process_info,
16 $updated_at,
17 $created_at;
18
19 function __construct( $id = false ) {
20 parent::__construct();
21 $this->table_name = LATEPOINT_TABLE_PROCESS_JOBS;
22
23 if ( $id ) {
24 $this->load_by_id( $id );
25 }
26 }
27
28 public function get_link_to_object() {
29 $href = '#';
30 $attrs = '';
31 switch ( $this->process->event_type ) {
32 case 'order_updated':
33 case 'order_created':
34 $attrs = OsOrdersHelper::quick_order_btn_html( $this->object_id );
35 break;
36 case 'customer_created':
37 $attrs = OsCustomerHelper::quick_customer_btn_html( $this->object_id );
38 break;
39 case 'booking_start':
40 case 'booking_end':
41 case 'booking_created':
42 case 'booking_updated':
43 $attrs = OsBookingHelper::quick_booking_btn_html( $this->object_id );
44 break;
45 }
46 $link = '<a href="' . esc_url( $href ) . '" ' . $attrs . '>' . esc_html( $this->object_id ) . '</a>';
47 return $link;
48 }
49
50 public function get_original_process_attribute( $attribute ) {
51 $process_info = json_decode( $this->process_info, true );
52 return $process_info[ $attribute ] ?? __( 'n/a', 'latepoint' );
53 }
54
55 public function get_action_by_id_from_settings( $action_id ) {
56 foreach ( $this->actions as $action ) {
57 if ( $action->id == $action_id ) {
58 return $action;
59 }
60 }
61 return null;
62 }
63
64 public function get_actions( $force_refresh = false ) {
65 if ( ! isset( $this->actions ) || $force_refresh ) {
66 $this->actions = [];
67 $settings = json_decode( $this->settings, true );
68 if ( $settings ) {
69 foreach ( $settings['action_data'] as $action_id => $action_data ) {
70 $action = new \LatePoint\Misc\ProcessAction();
71 $action->id = $action_id;
72 $action->type = $settings['action_info'][ $action_id ]['type'];
73 $action->prepared_data_for_run = $action_data;
74 $this->actions[] = $action;
75 }
76 }
77 }
78 return $this->actions;
79 }
80
81 public function get_actions_summary() {
82 $actions_html = '';
83
84 $process_actions = $this->get_actions();
85 $run_result = '';
86 if ( ! empty( $this->run_result ) ) {
87 $run_result = json_decode( $this->run_result, true );
88 }
89 if ( empty( $process_actions ) && empty( $run_result ) ) {
90 return __( 'No Actions', 'latepoint' );
91 }
92
93 foreach ( $process_actions as $action ) {
94 if ( isset( $run_result['ran_actions_info'][ $action->id ] ) ) {
95 $status_icon = $run_result['ran_actions_info'][ $action->id ]['run_status'] == LATEPOINT_STATUS_SUCCESS ? '<i style="color: #78ad06;" class="latepoint-icon latepoint-icon-checkmark"></i>' : '<i style="color: #ad0606;" class="latepoint-icon latepoint-icon-x-square"></i>';
96 $action_name = '<span>' . \LatePoint\Misc\ProcessAction::get_action_name_for_type( $run_result['ran_actions_info'][ $action->id ]['type'] ) . '</span>' . $status_icon . '</span>';
97 } else {
98 $action_name = '<span>' . \LatePoint\Misc\ProcessAction::get_action_name_for_type( $action->type ) . '</span><i class="latepoint-icon latepoint-icon-bell"></i></span>';
99 }
100 $action_icon = '';
101 switch ( $action->type ) {
102 case 'send_email':
103 $action_icon = '<i class="latepoint-icon latepoint-icon-mail"></i>';
104 break;
105 case 'send_sms':
106 $action_icon = '<i class="latepoint-icon latepoint-icon-message-circle"></i>';
107 break;
108 case 'trigger_webhook':
109 $action_icon = '<i class="latepoint-icon latepoint-icon-globe"></i>';
110 break;
111 }
112
113 $actions_html .= '<span
114 data-os-action="' . OsRouterHelper::build_route_name( 'process_jobs', 'preview_job_action' ) . '"
115 data-os-output-target="side-panel"
116 data-os-after-call="latepoint_init_json_view"
117 data-os-params="' . OsUtilHelper::build_os_params(
118 [
119 'job_id' => $this->id,
120 'action_id' => $action->id,
121 ]
122 ) . '"
123 data-os-lightbox-classes="width-800"
124 class="action-run-info-pill">' . $action_icon . $action_name . '</span>';
125 }
126
127 return $actions_html;
128 }
129
130 protected function get_process() {
131 if ( $this->process_id ) {
132 if ( ! isset( $this->process ) || ( isset( $this->process ) && ( $this->process->id != $this->process_id ) ) ) {
133 $this->process = new OsProcessModel( $this->process_id );
134 $this->process->build_from_json();
135 }
136 } else {
137 $this->process = new OsProcessModel();
138 }
139 return $this->process;
140 }
141
142 /**
143 * @param $action_ids_to_run IDs of actions that need to be run, all will run by default
144 * @return $this
145 */
146 public function run( array $action_ids_to_run = [] ) {
147 $actions = $this->get_actions();
148 if ( $actions ) {
149 $settings = json_decode( $this->settings, true );
150 $error_messages = [];
151 // try to load from previous run, if it ever existed and if select action IDs are passed, otherwise overwrite. This is done to not override actions that ran before with partial actions that ran now
152 $previous_run_result = empty( $this->run_result ) ? [] : json_decode( $this->run_result, true );
153 $ran_actions_info = ( isset( $previous_run_result['ran_actions_info'] ) && $action_ids_to_run ) ? $previous_run_result['ran_actions_info'] : [];
154 foreach ( $actions as $action ) {
155 // skip if specific action IDs are passed and action in this loop is not one of the requested to be run
156 if ( $action_ids_to_run && ! in_array( $action->id, $action_ids_to_run ) ) {
157 continue;
158 }
159 if ( ! isset( $settings['action_data'][ $action->id ] ) ) {
160 $error_messages[] = $action->id . ': ' . __( 'Process action have been modified since the job was created.', 'latepoint' );
161 continue;
162 }
163 $action->prepared_data_for_run = $settings['action_data'][ $action->id ];
164 $result = $action->run( false );
165 $ran_actions_info[ $action->id ] = [
166 'type' => $action->type,
167 'id' => $action->id,
168 'run_status' => $result['status'],
169 'run_datetime_utc' => OsTimeHelper::now_datetime_utc_in_db_format(),
170 'run_message' => $result['message'],
171 ];
172 if ( $result['status'] != LATEPOINT_STATUS_SUCCESS ) {
173 $error_messages[] = $action->id . ': ' . $result['message'];
174 }
175 }
176 if ( empty( $error_messages ) ) {
177 $this->status = LATEPOINT_JOB_STATUS_COMPLETED;
178 $message = $action_ids_to_run ? __( 'Selected actions ran successfully.', 'latepoint' ) . ' [' . implode( ',', $action_ids_to_run ) . ']' : __( 'The job ran successfully.', 'latepoint' );
179 $this->run_result = wp_json_encode(
180 [
181 'status' => LATEPOINT_STATUS_SUCCESS,
182 'run_datetime_utc' => OsTimeHelper::now_datetime_utc_in_db_format(),
183 'message' => $message,
184 'ran_actions_info' => $ran_actions_info,
185 ]
186 );
187 } else {
188 $this->status = LATEPOINT_JOB_STATUS_ERROR;
189 $this->run_result = wp_json_encode(
190 [
191 'status' => LATEPOINT_STATUS_ERROR,
192 'run_datetime_utc' => OsTimeHelper::now_datetime_utc_in_db_format(),
193 'message' => implode( ', ', $error_messages ),
194 'ran_actions_info' => $ran_actions_info,
195 ]
196 );
197 }
198 } else {
199 $this->run_result = wp_json_encode(
200 [
201 'status' => LATEPOINT_STATUS_SUCCESS,
202 'run_datetime_utc' => OsTimeHelper::now_datetime_utc_in_db_format(),
203 'message' => __( 'Job process has no actions to run', 'latepoint' ),
204 ]
205 );
206 $this->status = LATEPOINT_JOB_STATUS_COMPLETED;
207 }
208 $activity = new OsActivityModel();
209
210 $event_type = $this->get_original_process_attribute( 'event_type' );
211 switch ( $event_type ) {
212 case 'order_created':
213 case 'order_updated':
214 $order = new OsOrderModel( $this->object_id );
215 $activity->customer_id = $order->customer_id;
216 $activity->order_id = $order->id;
217 break;
218 case 'booking_created':
219 case 'booking_updated':
220 case 'booking_start':
221 case 'booking_end':
222 $booking = new OsBookingModel( $this->object_id );
223 $activity->booking_id = $this->object_id;
224 $activity->customer_id = $booking->customer_id;
225 $activity->agent_id = $booking->agent_id;
226 $activity->service_id = $booking->service_id;
227 $activity->location_id = $booking->location_id;
228 $activity->order_id = $booking->order->id;
229 break;
230 case 'customer_created':
231 $activity->customer_id = $this->object_id;
232 break;
233 case 'transaction_created':
234 $transaction = new OsTransactionModel( $this->object_id );
235 $activity->order_id = $transaction->order_id;
236 break;
237 case 'payment_request_created':
238 $payment_request = new OsPaymentRequestModel( $this->object_id );
239 $activity->order_id = $payment_request->order_id;
240 break;
241 }
242
243 $this->save();
244
245 $activity->code = 'process_job_run';
246
247 if ( OsAuthHelper::get_highest_current_user_type() ) {
248 $activity->initiated_by = OsAuthHelper::get_highest_current_user_type();
249 $activity->initiated_by_id = OsAuthHelper::get_highest_current_user_id();
250 }
251 $activity->description = wp_json_encode(
252 [
253 'job_id' => $this->id,
254 'processed_datetime' => OsTimeHelper::now_datetime_in_db_format(),
255 'run_result' => $this->run_result,
256 'status' => LATEPOINT_STATUS_SUCCESS,
257 ]
258 );
259
260 /**
261 * Activity that is created when a process job is run
262 *
263 * @since 5.1.0
264 * @hook latepoint_job_run_activity
265 *
266 * @param {OsActivityModel} $activity Activity model
267 * @param {OsProcessJobModel} $process_job Process job model
268 *
269 * @returns {OsActivityModel} Filtered activity model
270 */
271 $activity = apply_filters( 'latepoint_job_run_activity', $activity, $this, $event_type );
272 $activity->save();
273 return $this;
274 }
275
276 protected function params_to_save( $role = 'admin' ) {
277 $params_to_save = [
278 'id',
279 'process_id',
280 'object_id',
281 'object_model_type',
282 'to_run_after_utc',
283 'status',
284 'process_info',
285 'settings',
286 'run_result',
287 ];
288 return $params_to_save;
289 }
290
291 protected function allowed_params( $role = 'admin' ) {
292 $allowed_params = [
293 'id',
294 'process_id',
295 'object_id',
296 'object_model_type',
297 'to_run_after_utc',
298 'status',
299 'process_info',
300 'settings',
301 'run_result',
302 ];
303 return $allowed_params;
304 }
305
306
307 protected function properties_to_validate() {
308 $validations = [];
309 return $validations;
310 }
311 }
312