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.11 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 / models / process_job_model.php
latepoint / lib / models Last commit date
activity_model.php 1 year ago agent_meta_model.php 1 year ago agent_model.php 1 year ago booking_meta_model.php 1 year ago booking_model.php 1 year ago bundle_meta_model.php 1 year ago bundle_model.php 1 year ago cart_item_model.php 1 year ago cart_meta_model.php 1 year ago cart_model.php 1 year ago connector_model.php 1 year ago customer_meta_model.php 1 year ago customer_model.php 11 months ago invoice_model.php 1 year ago join_bundles_services_model.php 1 year ago location_category_model.php 11 months ago location_model.php 1 year ago meta_model.php 1 year ago model.php 11 months ago off_period_model.php 1 year ago order_intent_meta_model.php 1 year ago order_intent_model.php 11 months ago order_item_model.php 1 year ago order_meta_model.php 1 year ago order_model.php 1 year ago otp_model.php 11 months ago payment_request_model.php 1 year ago process_job_model.php 6 months ago process_model.php 1 year ago recurrence_model.php 1 year ago service_category_model.php 11 months ago service_meta_model.php 1 year ago service_model.php 11 months ago session_model.php 1 year ago settings_model.php 1 year ago step_settings_model.php 1 year ago transaction_intent_model.php 1 year ago transaction_model.php 1 year ago transaction_refund_model.php 1 year ago work_period_model.php 1 year ago
process_job_model.php
272 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) return $action;
58 }
59 return null;
60 }
61
62 public function get_actions($force_refresh = false){
63 if(!isset($this->actions) || $force_refresh){
64 $this->actions = [];
65 $settings = json_decode($this->settings, true);
66 if($settings){
67 foreach($settings['action_data'] as $action_id => $action_data){
68 $action = new \LatePoint\Misc\ProcessAction();
69 $action->id = $action_id;
70 $action->type = $settings['action_info'][$action_id]['type'];
71 $action->prepared_data_for_run = $action_data;
72 $this->actions[] = $action;
73 }
74 }
75 }
76 return $this->actions;
77 }
78
79 public function get_actions_summary(){
80 $actions_html = '';
81
82 $process_actions = $this->get_actions();
83 $run_result = '';
84 if( ! empty($this->run_result) ) {
85 $run_result = json_decode($this->run_result, true);
86 }
87 if(empty($process_actions) && empty($run_result)) return __('No Actions', 'latepoint');
88
89 foreach($process_actions as $action){
90 if(isset($run_result['ran_actions_info'][$action->id])){
91 $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>';
92 $action_name = '<span>'.\LatePoint\Misc\ProcessAction::get_action_name_for_type($run_result['ran_actions_info'][$action->id]['type']).'</span>'.$status_icon.'</span>';
93 }else{
94 $action_name = '<span>'.\LatePoint\Misc\ProcessAction::get_action_name_for_type($action->type).'</span><i class="latepoint-icon latepoint-icon-bell"></i></span>';
95 }
96 $action_icon = '';
97 switch($action->type){
98 case 'send_email':
99 $action_icon = '<i class="latepoint-icon latepoint-icon-mail"></i>';
100 break;
101 case 'send_sms':
102 $action_icon = '<i class="latepoint-icon latepoint-icon-message-circle"></i>';
103 break;
104 case 'trigger_webhook':
105 $action_icon = '<i class="latepoint-icon latepoint-icon-globe"></i>';
106 break;
107 }
108
109 $actions_html.= '<span
110 data-os-action="'.OsRouterHelper::build_route_name('process_jobs', 'preview_job_action').'"
111 data-os-output-target="side-panel"
112 data-os-after-call="latepoint_init_json_view"
113 data-os-params="'.OsUtilHelper::build_os_params(['job_id' => $this->id, 'action_id' => $action->id]).'"
114 data-os-lightbox-classes="width-800"
115 class="action-run-info-pill">'.$action_icon.$action_name.'</span>';
116 }
117
118 return $actions_html;
119 }
120
121 protected function get_process() {
122 if ($this->process_id) {
123 if (!isset($this->process) || (isset($this->process) && ($this->process->id != $this->process_id))) {
124 $this->process = new OsProcessModel($this->process_id);
125 $this->process->build_from_json();
126 }
127 } else {
128 $this->process = new OsProcessModel();
129 }
130 return $this->process;
131 }
132
133 /**
134 * @param $action_ids_to_run IDs of actions that need to be run, all will run by default
135 * @return $this
136 */
137 public function run(array $action_ids_to_run = []){
138 $actions = $this->get_actions();
139 if($actions){
140 $settings = json_decode($this->settings, true);
141 $error_messages = [];
142 // 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
143 $previous_run_result = empty($this->run_result) ? [] : json_decode($this->run_result, true);
144 $ran_actions_info = (isset($previous_run_result['ran_actions_info']) && $action_ids_to_run) ? $previous_run_result['ran_actions_info'] : [];
145 foreach($actions as $action){
146 // skip if specific action IDs are passed and action in this loop is not one of the requested to be run
147 if($action_ids_to_run && !in_array($action->id, $action_ids_to_run)) continue;
148 if(!isset($settings['action_data'][$action->id])){
149 $error_messages[] = $action->id.': '.__('Process action have been modified since the job was created.', 'latepoint');
150 continue;
151 }
152 $action->prepared_data_for_run = $settings['action_data'][$action->id];
153 $result = $action->run(false);
154 $ran_actions_info[$action->id] = [
155 'type' => $action->type,
156 'id' => $action->id,
157 'run_status' => $result['status'],
158 'run_datetime_utc' => OsTimeHelper::now_datetime_utc_in_db_format(),
159 'run_message' => $result['message']];
160 if($result['status'] != LATEPOINT_STATUS_SUCCESS){
161 $error_messages[] = $action->id.': '.$result['message'];
162 }
163 }
164 if(empty($error_messages)){
165 $this->status = LATEPOINT_JOB_STATUS_COMPLETED;
166 $message = $action_ids_to_run ? __('Selected actions ran successfully.', 'latepoint').' ['.implode(',', $action_ids_to_run).']' : __('The job ran successfully.', 'latepoint');
167 $this->run_result = wp_json_encode(['status' => LATEPOINT_STATUS_SUCCESS, 'run_datetime_utc' => OsTimeHelper::now_datetime_utc_in_db_format(), 'message' => $message, 'ran_actions_info' => $ran_actions_info]);
168 }else{
169 $this->status = LATEPOINT_JOB_STATUS_ERROR;
170 $this->run_result = wp_json_encode(['status' => LATEPOINT_STATUS_ERROR, 'run_datetime_utc' => OsTimeHelper::now_datetime_utc_in_db_format(), 'message' => implode(', ', $error_messages), 'ran_actions_info' => $ran_actions_info]);
171 }
172 }else{
173 $this->run_result = wp_json_encode(['status' => LATEPOINT_STATUS_SUCCESS, 'run_datetime_utc' => OsTimeHelper::now_datetime_utc_in_db_format(), 'message' => __('Job process has no actions to run', 'latepoint')]);
174 $this->status = LATEPOINT_JOB_STATUS_COMPLETED;
175 }
176 $activity = new OsActivityModel();
177
178 $event_type = $this->get_original_process_attribute('event_type');
179 switch($event_type){
180 case 'order_created':
181 case 'order_updated':
182 $order = new OsOrderModel($this->object_id);
183 $activity->customer_id = $order->customer_id;
184 $activity->order_id = $order->id;
185 break;
186 case 'booking_created':
187 case 'booking_updated':
188 case 'booking_start':
189 case 'booking_end':
190 $booking = new OsBookingModel($this->object_id);
191 $activity->booking_id = $this->object_id;
192 $activity->customer_id = $booking->customer_id;
193 $activity->agent_id = $booking->agent_id;
194 $activity->service_id = $booking->service_id;
195 $activity->location_id = $booking->location_id;
196 $activity->order_id = $booking->order->id;
197 break;
198 case 'customer_created':
199 $activity->customer_id = $this->object_id;
200 break;
201 case 'transaction_created':
202 $transaction = new OsTransactionModel($this->object_id);
203 $activity->order_id = $transaction->order_id;
204 break;
205 case 'payment_request_created':
206 $payment_request = new OsPaymentRequestModel($this->object_id);
207 $activity->order_id = $payment_request->order_id;
208 break;
209 }
210
211 $this->save();
212
213 $activity->code = 'process_job_run';
214
215 if(OsAuthHelper::get_highest_current_user_type()){
216 $activity->initiated_by = OsAuthHelper::get_highest_current_user_type();
217 $activity->initiated_by_id = OsAuthHelper::get_highest_current_user_id();
218 }
219 $activity->description = wp_json_encode(['job_id' => $this->id, 'processed_datetime' => OsTimeHelper::now_datetime_in_db_format(), 'run_result' => $this->run_result, 'status' => LATEPOINT_STATUS_SUCCESS]);
220
221 /**
222 * Activity that is created when a process job is run
223 *
224 * @since 5.1.0
225 * @hook latepoint_job_run_activity
226 *
227 * @param {OsActivityModel} $activity Activity model
228 * @param {OsProcessJobModel} $process_job Process job model
229 *
230 * @returns {OsActivityModel} Filtered activity model
231 */
232 $activity = apply_filters('latepoint_job_run_activity', $activity, $this, $event_type);
233 $activity->save();
234 return $this;
235 }
236
237 protected function params_to_save($role = 'admin'){
238 $params_to_save = [
239 'id',
240 'process_id',
241 'object_id',
242 'object_model_type',
243 'to_run_after_utc',
244 'status',
245 'process_info',
246 'settings',
247 'run_result'
248 ];
249 return $params_to_save;
250 }
251
252 protected function allowed_params($role = 'admin'){
253 $allowed_params = [
254 'id',
255 'process_id',
256 'object_id',
257 'object_model_type',
258 'to_run_after_utc',
259 'status',
260 'process_info',
261 'settings',
262 'run_result'
263 ];
264 return $allowed_params;
265 }
266
267
268 protected function properties_to_validate(){
269 $validations = [];
270 return $validations;
271 }
272 }