activities_helper.php
3 months ago
agent_helper.php
3 months ago
analytics_helper.php
4 months ago
auth_helper.php
3 months ago
blocks_helper.php
3 months ago
booking_helper.php
3 months ago
bricks_helper.php
3 months ago
bundles_helper.php
3 months ago
calendar_helper.php
3 months ago
carts_helper.php
3 months ago
connector_helper.php
3 months ago
csv_helper.php
3 months ago
customer_helper.php
3 months ago
customer_import_helper.php
3 months ago
database_helper.php
3 months ago
debug_helper.php
3 months ago
defaults_helper.php
3 months ago
elementor_helper.php
3 months ago
email_helper.php
3 months ago
encrypt_helper.php
3 months ago
events_helper.php
3 months ago
form_helper.php
3 months ago
icalendar_helper.php
3 months ago
image_helper.php
3 months ago
invoices_helper.php
3 months ago
license_helper.php
3 months ago
location_helper.php
3 months ago
marketing_systems_helper.php
3 months ago
meeting_systems_helper.php
3 months ago
menu_helper.php
3 months ago
meta_helper.php
3 months ago
migrations_helper.php
3 months ago
money_helper.php
3 months ago
notifications_helper.php
3 months ago
nps_survey_helper.php
3 months ago
order_intent_helper.php
3 months ago
orders_helper.php
3 months ago
otp_helper.php
3 months ago
pages_helper.php
3 months ago
params_helper.php
3 months ago
payments_helper.php
3 months ago
price_breakdown_helper.php
3 months ago
process_jobs_helper.php
3 months ago
processes_helper.php
3 months ago
replacer_helper.php
3 months ago
resource_helper.php
3 months ago
roles_helper.php
3 months ago
router_helper.php
3 months ago
service_helper.php
3 months ago
sessions_helper.php
3 months ago
settings_helper.php
3 months ago
short_links_systems_helper.php
3 months ago
shortcodes_helper.php
3 months ago
sms_helper.php
3 months ago
steps_helper.php
3 months ago
stripe_connect_helper.php
3 months ago
styles_helper.php
3 months ago
support_topics_helper.php
3 months ago
time_helper.php
3 months ago
timeline_helper.php
3 months ago
transaction_helper.php
3 months ago
transaction_intent_helper.php
3 months ago
util_helper.php
3 months ago
version_specific_updates_helper.php
3 months ago
whatsapp_helper.php
3 months ago
work_periods_helper.php
3 months ago
wp_datetime.php
3 months ago
wp_user_helper.php
3 months ago
processes_helper.php
339 lines
| 1 | <?php |
| 2 | /* |
| 3 | * Copyright (c) 2022 LatePoint LLC. All rights reserved. |
| 4 | */ |
| 5 | |
| 6 | class OsProcessesHelper { |
| 7 | |
| 8 | |
| 9 | public static function check_if_process_exists( OsProcessModel $process ): bool { |
| 10 | $existing_process = new OsProcessModel(); |
| 11 | $existing_process = $existing_process->select( 'id' )->where( |
| 12 | [ |
| 13 | 'event_type' => $process->event_type, |
| 14 | 'name' => $process->name, |
| 15 | ] |
| 16 | )->set_limit( 1 )->get_results(); |
| 17 | return ! empty( $existing_process ); |
| 18 | } |
| 19 | |
| 20 | public static function get_object_data_by_source( string $source, string $value, bool $include_model = true ): array { |
| 21 | $object_data = []; |
| 22 | switch ( $source ) { |
| 23 | case 'booking_id': |
| 24 | $object_data = [ |
| 25 | 'model' => 'booking', |
| 26 | 'id' => $value, |
| 27 | ]; |
| 28 | if ( $include_model ) { |
| 29 | $model = new OsBookingModel( $value ); |
| 30 | $object_data['model_ready'] = $model; |
| 31 | } |
| 32 | break; |
| 33 | case 'order_id': |
| 34 | $object_data = [ |
| 35 | 'model' => 'order', |
| 36 | 'id' => $value, |
| 37 | ]; |
| 38 | if ( $include_model ) { |
| 39 | $model = new OsOrderModel( $value ); |
| 40 | $object_data['model_ready'] = $model; |
| 41 | } |
| 42 | break; |
| 43 | case 'payment_request_id': |
| 44 | $object_data = [ |
| 45 | 'model' => 'payment_request', |
| 46 | 'id' => $value, |
| 47 | ]; |
| 48 | if ( $include_model ) { |
| 49 | $model = new OsPaymentRequestModel( $value ); |
| 50 | $object_data['model_ready'] = $model; |
| 51 | } |
| 52 | break; |
| 53 | } |
| 54 | /** |
| 55 | * Get the list of booking statuses that are enabled for synchronization with Google Calendar |
| 56 | * |
| 57 | * @since 5.1.0 |
| 58 | * @hook latepoint_get_process_object_by_source |
| 59 | * |
| 60 | * @param {array} $object_data Object info |
| 61 | * @param {string} $source Source type |
| 62 | * @param {string} $value Source value |
| 63 | * @param {bool} $include_model Should include model into object data or not |
| 64 | * |
| 65 | * @returns {array} Filtered object data |
| 66 | */ |
| 67 | $object_data = apply_filters( 'latepoint_get_process_object_by_source', $object_data, $source, $value, $include_model ); |
| 68 | return $object_data; |
| 69 | } |
| 70 | |
| 71 | public static function processes_list_for_select() { |
| 72 | $processes = new OsProcessModel(); |
| 73 | $processes = $processes->get_results_as_models(); |
| 74 | $processes_list = []; |
| 75 | foreach ( $processes as $process ) { |
| 76 | $processes_list[] = [ |
| 77 | 'value' => $process->id, |
| 78 | 'label' => $process->name, |
| 79 | ]; |
| 80 | } |
| 81 | return $processes_list; |
| 82 | } |
| 83 | |
| 84 | public static function extract_trigger_conditions_from_groups( $groups ) { |
| 85 | $trigger_conditions = []; |
| 86 | if ( $groups ) { |
| 87 | foreach ( $groups as $group ) { |
| 88 | if ( $group['type'] == 'group' && ! empty( $group['trigger_condition'] ) ) { |
| 89 | $trigger_condition = [ 'id' => \LatePoint\Misc\ProcessEvent::generate_trigger_condition_id() ]; |
| 90 | $trigger_condition['property'] = $group['trigger_condition']['property']; |
| 91 | foreach ( $group['items'] as $item ) { |
| 92 | if ( $item['type'] == 'trigger_condition_branch' ) { |
| 93 | $trigger_condition['operator'] = $item['settings']['operator']; |
| 94 | $trigger_condition['value'] = $item['settings']['value']; |
| 95 | } |
| 96 | $trigger_conditions[] = $trigger_condition; |
| 97 | |
| 98 | if ( $item['items'] ) { |
| 99 | $temp = self::extract_trigger_conditions_from_groups( $item['items'] ); |
| 100 | if ( ! empty( $temp ) ) { |
| 101 | $trigger_conditions = array_merge( $trigger_conditions, $temp ); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | return $trigger_conditions; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * @param \LatePoint\Misc\ProcessEvent $event |
| 113 | * @return string |
| 114 | * |
| 115 | * Generate conditions form for an event |
| 116 | * |
| 117 | */ |
| 118 | public static function trigger_conditions_html_for_event( \LatePoint\Misc\ProcessEvent $event ): string { |
| 119 | if ( empty( \LatePoint\Misc\ProcessEvent::get_available_trigger_condition_objects_for_event_type( $event->type ) ) ) { |
| 120 | return ''; |
| 121 | } |
| 122 | $temp_id = 'pe_' . \OsUtilHelper::random_text( 'alnum', 6 ); |
| 123 | $conditions_html = '<div class="pe-conditions" id="pe-conditions-for-' . esc_attr( $temp_id ) . '" style="' . ( ( ! empty( $event->trigger_conditions ) ) ? 'display: block;' : 'display: none;' ) . '"> |
| 124 | <div class="pe-conditions-heading">' . __( 'Trigger only if:', 'latepoint' ) . '</div> |
| 125 | ' . $event->trigger_conditions_form_html() . ' |
| 126 | </div>'; |
| 127 | $conditions_html = apply_filters( 'latepoint_process_conditions_html', $conditions_html, $event, $temp_id ); |
| 128 | $html = '<div class="sub-section-row"> |
| 129 | <div class="sub-section-label"> |
| 130 | <h3>' . __( 'Conditional', 'latepoint' ) . '</h3> |
| 131 | </div> |
| 132 | <div class="sub-section-content"> |
| 133 | ' . OsFormHelper::toggler_field( 'process[event][conditional]', __( 'Trigger only when specific conditions are met', 'latepoint' ), ! empty( $event->trigger_conditions ), 'pe-conditions-for-' . $temp_id ) . ' |
| 134 | ' . $conditions_html . ' |
| 135 | </div> |
| 136 | </div>'; |
| 137 | return $html; |
| 138 | } |
| 139 | |
| 140 | public static function time_offset_html_for_event( \LatePoint\Misc\ProcessEvent $event ): string { |
| 141 | $temp_id = 'pe_' . \OsUtilHelper::random_text( 'alnum', 6 ); |
| 142 | |
| 143 | $time_offset_settings_html = OsUtilHelper::pro_feature_block(); |
| 144 | $time_offset_settings_html = apply_filters( 'latepoint_event_time_offset_settings_html', $time_offset_settings_html, $event ); |
| 145 | |
| 146 | $html = '<div class="sub-section-row"> |
| 147 | <div class="sub-section-label"> |
| 148 | <h3>' . __( 'Time offset', 'latepoint' ) . '</h3> |
| 149 | </div> |
| 150 | <div class="sub-section-content"> |
| 151 | ' . OsFormHelper::toggler_field( 'process[event][has_time_offset]', __( 'Execute actions with a time offset', 'latepoint' ), ! empty( $event->time_offset ), 'pe-conditions-for-' . $temp_id ) . ' |
| 152 | <div class="pe-conditions" id="pe-conditions-for-' . esc_attr( $temp_id ) . '" style="' . ( ( ! empty( $event->time_offset ) ) ? 'display: block;' : 'display: none;' ) . '"> |
| 153 | ' . $time_offset_settings_html . ' |
| 154 | </div> |
| 155 | </div> |
| 156 | </div>'; |
| 157 | return $html; |
| 158 | } |
| 159 | |
| 160 | public static function extract_actions_from_groups( $groups ) { |
| 161 | $actions = []; |
| 162 | if ( $groups ) { |
| 163 | foreach ( $groups as $group ) { |
| 164 | if ( $group['type'] == 'action' ) { |
| 165 | $action_data = $group['settings']; |
| 166 | $actions[] = new \LatePoint\Misc\ProcessAction( |
| 167 | [ |
| 168 | 'type' => $action_data['type'], |
| 169 | 'id' => $group['id'], |
| 170 | 'status' => $action_data['status'] ?? LATEPOINT_STATUS_ACTIVE, |
| 171 | 'settings' => $action_data['settings'], |
| 172 | ] |
| 173 | ); |
| 174 | } |
| 175 | if ( ! empty( $group['items'] ) ) { |
| 176 | $temp = self::extract_actions_from_groups( $group['items'] ); |
| 177 | if ( ! empty( $temp ) ) { |
| 178 | $actions = array_merge( $actions, $temp ); |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | return $actions; |
| 184 | } |
| 185 | |
| 186 | public static function iterate_trigger_conditions( $trigger_conditions, $actions ) { |
| 187 | $trigger_condition = reset( $trigger_conditions ); |
| 188 | if ( ! empty( $trigger_conditions ) ) { |
| 189 | $group = [ |
| 190 | [ |
| 191 | 'type' => 'group', |
| 192 | 'trigger_condition' => [ |
| 193 | 'property' => $trigger_condition['property'], |
| 194 | ], |
| 195 | 'items' => [ |
| 196 | [ |
| 197 | 'type' => 'trigger_condition_branch', |
| 198 | 'settings' => [ |
| 199 | 'operator' => $trigger_condition['operator'], |
| 200 | 'value' => $trigger_condition['value'], |
| 201 | ], |
| 202 | 'items' => self::iterate_trigger_conditions( array_slice( $trigger_conditions, 1 ), $actions ), |
| 203 | ], |
| 204 | ], |
| 205 | ], |
| 206 | ]; |
| 207 | } else { |
| 208 | // no more conditions, do actions |
| 209 | $action_items = []; |
| 210 | if ( ! empty( $actions ) ) { |
| 211 | foreach ( $actions as $action_id => $action ) { |
| 212 | $action_items[] = [ |
| 213 | 'type' => 'action', |
| 214 | 'id' => $action_id, |
| 215 | 'settings' => [ |
| 216 | 'status' => $action['status'] ?? LATEPOINT_STATUS_ACTIVE, |
| 217 | 'type' => $action['type'], |
| 218 | 'settings' => $action['settings'] ?? [], |
| 219 | ], |
| 220 | ]; |
| 221 | } |
| 222 | } |
| 223 | $group = [ |
| 224 | [ |
| 225 | 'type' => 'group', |
| 226 | 'trigger_condition' => false, |
| 227 | 'items' => $action_items, |
| 228 | ], |
| 229 | ]; |
| 230 | } |
| 231 | return $group; |
| 232 | } |
| 233 | |
| 234 | public static function values_for_trigger_condition_property( $property ) { |
| 235 | $values = []; |
| 236 | $property_data = explode( '__', $property ); |
| 237 | $property_object = $property_data[0]; |
| 238 | $property_attribute = $property_data[1]; |
| 239 | switch ( $property_object ) { |
| 240 | case 'order': |
| 241 | case 'old_order': |
| 242 | switch ( $property_attribute ) { |
| 243 | case 'fulfillment_status': |
| 244 | $fulfillment_statuses = OsOrdersHelper::get_fulfillment_statuses_list(); |
| 245 | foreach ( $fulfillment_statuses as $fulfillment_status_key => $fulfillment_status ) { |
| 246 | $values[] = [ |
| 247 | 'value' => $fulfillment_status_key, |
| 248 | 'label' => $fulfillment_status, |
| 249 | ]; |
| 250 | } |
| 251 | break; |
| 252 | case 'payment_status': |
| 253 | $payment_statuses = OsOrdersHelper::get_order_payment_statuses_list(); |
| 254 | foreach ( $payment_statuses as $payment_status_key => $payment_status ) { |
| 255 | $values[] = [ |
| 256 | 'value' => $payment_status_key, |
| 257 | 'label' => $payment_status, |
| 258 | ]; |
| 259 | } |
| 260 | break; |
| 261 | case 'status': |
| 262 | $statuses = OsOrdersHelper::get_order_statuses_list(); |
| 263 | foreach ( $statuses as $status_key => $status ) { |
| 264 | $values[] = [ |
| 265 | 'value' => $status_key, |
| 266 | 'label' => $status, |
| 267 | ]; |
| 268 | } |
| 269 | break; |
| 270 | } |
| 271 | break; |
| 272 | case 'booking': |
| 273 | case 'old_booking': |
| 274 | switch ( $property_attribute ) { |
| 275 | case 'payment_status': |
| 276 | $payment_statuses = OsOrdersHelper::get_order_payment_statuses_list(); |
| 277 | foreach ( $payment_statuses as $payment_status_key => $payment_status ) { |
| 278 | $values[] = [ |
| 279 | 'value' => $payment_status_key, |
| 280 | 'label' => $payment_status, |
| 281 | ]; |
| 282 | } |
| 283 | break; |
| 284 | case 'status': |
| 285 | $statuses = OsBookingHelper::get_statuses_list(); |
| 286 | foreach ( $statuses as $status_key => $status ) { |
| 287 | $values[] = [ |
| 288 | 'value' => $status_key, |
| 289 | 'label' => $status, |
| 290 | ]; |
| 291 | } |
| 292 | break; |
| 293 | case 'agent_id': |
| 294 | $values = OsFormHelper::model_options_for_multi_select( 'agent' ); |
| 295 | break; |
| 296 | case 'service_id': |
| 297 | $values = OsFormHelper::model_options_for_multi_select( 'service' ); |
| 298 | break; |
| 299 | } |
| 300 | break; |
| 301 | case 'customer': |
| 302 | break; |
| 303 | case 'transaction': |
| 304 | switch ( $property_attribute ) { |
| 305 | case 'payment_method': |
| 306 | $values = OsPaymentsHelper::get_all_payment_methods_for_select(); |
| 307 | break; |
| 308 | case 'payment_portion': |
| 309 | $values = OsPaymentsHelper::get_payment_portions_list(); |
| 310 | break; |
| 311 | case 'kind': |
| 312 | $values = OsPaymentsHelper::get_list_of_transaction_kinds(); |
| 313 | break; |
| 314 | case 'status': |
| 315 | $values = OsPaymentsHelper::get_transaction_statuses_list(); |
| 316 | break; |
| 317 | } |
| 318 | break; |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * Returns an array of operators available for a selected condition property |
| 323 | * |
| 324 | * @since 4.7.0 |
| 325 | * @hook latepoint_process_event_trigger_condition_properties |
| 326 | * |
| 327 | * @param {array} $values Array of operators |
| 328 | * @param {string} $property Property in a format of object_code__object_property (e.g. old_booking__agent_id) |
| 329 | * @param {string} $property_object Object name |
| 330 | * @param {string} $property_attribute Property of an object |
| 331 | * |
| 332 | * @returns {array} The array of available operators |
| 333 | * |
| 334 | */ |
| 335 | $values = apply_filters( 'latepoint_available_values_for_process_event_trigger_condition_property', $values, $property, $property_object, $property_attribute ); |
| 336 | return $values; |
| 337 | } |
| 338 | } |
| 339 |