1
|
|
|
<?php if (! defined('EVENT_ESPRESSO_VERSION')) { |
2
|
|
|
exit('No direct script access allowed'); |
3
|
|
|
} |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class EE_Cron_Tasks |
9
|
|
|
* |
10
|
|
|
* @package Event Espresso |
11
|
|
|
* @subpackage core |
12
|
|
|
* @author Brent Christensen |
13
|
|
|
*/ |
14
|
|
|
class EE_Cron_Tasks extends EE_Base |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* WordPress doesn't allow duplicate crons within 10 minutes of the original, |
19
|
|
|
* so we'll set our retry time for just over 10 minutes to avoid that |
20
|
|
|
*/ |
21
|
|
|
const reschedule_timeout = 605; |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var EE_Cron_Tasks |
26
|
|
|
*/ |
27
|
|
|
private static $_instance; |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return EE_Cron_Tasks |
32
|
|
|
* @throws \ReflectionException |
33
|
|
|
* @throws \EE_Error |
34
|
|
|
* @throws \InvalidArgumentException |
35
|
|
|
* @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
36
|
|
|
* @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
37
|
|
|
*/ |
38
|
|
|
public static function instance() |
39
|
|
|
{ |
40
|
|
|
if (! self::$_instance instanceof EE_Cron_Tasks) { |
41
|
|
|
self::$_instance = new self(); |
42
|
|
|
} |
43
|
|
|
return self::$_instance; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @access private |
49
|
|
|
* @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
50
|
|
|
* @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
51
|
|
|
* @throws \InvalidArgumentException |
52
|
|
|
* @throws \EE_Error |
53
|
|
|
* @throws \ReflectionException |
54
|
|
|
*/ |
55
|
|
|
private function __construct() |
56
|
|
|
{ |
57
|
|
|
do_action('AHEE_log', __CLASS__, __FUNCTION__); |
58
|
|
|
// verify that WP Cron is enabled |
59
|
|
|
if ( |
60
|
|
|
defined('DISABLE_WP_CRON') |
61
|
|
|
&& DISABLE_WP_CRON |
62
|
|
|
&& is_admin() |
63
|
|
|
&& ! get_option('ee_disabled_wp_cron_check') |
64
|
|
|
) { |
65
|
|
|
/** |
66
|
|
|
* This needs to be delayed until after the config is loaded because EE_Cron_Tasks is constructed before |
67
|
|
|
* config is loaded. |
68
|
|
|
* This is intentionally using a anonymous function so that its not easily de-registered. Client code |
69
|
|
|
* wanting to not have this functionality can just register its own action at a priority after this one to |
70
|
|
|
* reverse any changes. |
71
|
|
|
*/ |
72
|
|
|
add_action( |
73
|
|
|
'AHEE__EE_System__load_core_configuration__complete', |
74
|
|
|
function () |
75
|
|
|
{ |
76
|
|
|
EE_Registry::instance()->NET_CFG->core->do_messages_on_same_request = true; |
77
|
|
|
EE_Registry::instance()->NET_CFG->update_config(true, false); |
78
|
|
|
add_option('ee_disabled_wp_cron_check', 1, '', false); |
79
|
|
|
} |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
// UPDATE TRANSACTION WITH PAYMENT |
83
|
|
|
add_action( |
84
|
|
|
'AHEE__EE_Cron_Tasks__update_transaction_with_payment_2', |
85
|
|
|
array('EE_Cron_Tasks', 'setup_update_for_transaction_with_payment'), |
86
|
|
|
10, |
87
|
|
|
2 |
88
|
|
|
); |
89
|
|
|
// FINALIZE ABANDONED TRANSACTIONS |
90
|
|
|
add_action( |
91
|
|
|
'AHEE__EE_Cron_Tasks__finalize_abandoned_transactions', |
92
|
|
|
array('EE_Cron_Tasks', 'check_for_abandoned_transactions'), |
93
|
|
|
10, |
94
|
|
|
1 |
95
|
|
|
); |
96
|
|
|
// EXPIRED TRANSACTION CHECK |
97
|
|
|
add_action( |
98
|
|
|
'AHEE__EE_Cron_Tasks__expired_transaction_check', |
99
|
|
|
array('EE_Cron_Tasks', 'expired_transaction_check'), |
100
|
|
|
10, |
101
|
|
|
1 |
102
|
|
|
); |
103
|
|
|
// CLEAN OUT JUNK TRANSACTIONS AND RELATED DATA |
104
|
|
|
add_action( |
105
|
|
|
'AHEE__EE_Cron_Tasks__clean_up_junk_transactions', |
106
|
|
|
array('EE_Cron_Tasks', 'clean_out_junk_transactions') |
107
|
|
|
); |
108
|
|
|
// logging |
109
|
|
|
add_action( |
110
|
|
|
'AHEE__EE_System__load_core_configuration__complete', |
111
|
|
|
array('EE_Cron_Tasks', 'log_scheduled_ee_crons') |
112
|
|
|
); |
113
|
|
|
EE_Registry::instance()->load_lib('Messages_Scheduler'); |
114
|
|
|
//clean out old gateway logs |
115
|
|
|
add_action( |
116
|
|
|
'AHEE_EE_Cron_Tasks__clean_out_old_gateway_logs', |
117
|
|
|
array('EE_Cron_Tasks', 'clean_out_old_gateway_logs') |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @access protected |
124
|
|
|
* @return void |
125
|
|
|
*/ |
126
|
|
|
public static function log_scheduled_ee_crons() |
127
|
|
|
{ |
128
|
|
|
$ee_crons = array( |
129
|
|
|
'AHEE__EE_Cron_Tasks__update_transaction_with_payment', |
130
|
|
|
'AHEE__EE_Cron_Tasks__finalize_abandoned_transactions', |
131
|
|
|
'AHEE__EE_Cron_Tasks__clean_up_junk_transactions', |
132
|
|
|
); |
133
|
|
|
$crons = (array)get_option('cron'); |
134
|
|
|
if (! is_array($crons)) { |
135
|
|
|
return; |
136
|
|
|
} |
137
|
|
|
foreach ($crons as $timestamp => $cron) { |
138
|
|
|
foreach ($ee_crons as $ee_cron) { |
139
|
|
|
if (isset($cron[$ee_cron]) && is_array($cron[$ee_cron])) { |
140
|
|
|
do_action('AHEE_log', __CLASS__, __FUNCTION__, $ee_cron, 'scheduled EE cron'); |
141
|
|
|
foreach ($cron[$ee_cron] as $ee_cron_details) { |
142
|
|
|
if (! empty($ee_cron_details['args'])) { |
143
|
|
|
do_action( |
144
|
|
|
'AHEE_log', |
145
|
|
|
__CLASS__, |
146
|
|
|
__FUNCTION__, |
147
|
|
|
print_r($ee_cron_details['args'], true), |
148
|
|
|
"{$ee_cron} args" |
149
|
|
|
); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
|
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* reschedule_cron_for_transactions_if_maintenance_mode |
161
|
|
|
* if Maintenance Mode is active, this will reschedule a cron to run again in 10 minutes |
162
|
|
|
* |
163
|
|
|
* @param string $cron_task |
164
|
|
|
* @param array $TXN_IDs |
165
|
|
|
* @return bool |
166
|
|
|
* @throws \DomainException |
167
|
|
|
*/ |
168
|
|
|
public static function reschedule_cron_for_transactions_if_maintenance_mode($cron_task, array $TXN_IDs) |
169
|
|
|
{ |
170
|
|
|
if (! method_exists('EE_Cron_Tasks', $cron_task)) { |
171
|
|
|
throw new \DomainException( |
172
|
|
|
sprintf( |
173
|
|
|
__('"%1$s" is not valid method on EE_Cron_Tasks.', 'event_espresso'), |
174
|
|
|
$cron_task |
175
|
|
|
) |
176
|
|
|
); |
177
|
|
|
} |
178
|
|
|
// reschedule the cron if we can't hit the db right now |
179
|
|
|
if (! EE_Maintenance_Mode::instance()->models_can_query()) { |
180
|
|
|
foreach ($TXN_IDs as $TXN_ID => $additional_vars) { |
181
|
|
|
// ensure $additional_vars is an array |
182
|
|
|
$additional_vars = is_array($additional_vars) ? $additional_vars : array($additional_vars); |
183
|
|
|
// reset cron job for the TXN |
184
|
|
|
call_user_func_array( |
185
|
|
|
array('EE_Cron_Tasks', $cron_task), |
186
|
|
|
array_merge( |
187
|
|
|
array( |
188
|
|
|
time() + (10 * MINUTE_IN_SECONDS), |
189
|
|
|
$TXN_ID, |
190
|
|
|
), |
191
|
|
|
$additional_vars |
192
|
|
|
) |
193
|
|
|
); |
194
|
|
|
} |
195
|
|
|
return true; |
196
|
|
|
} |
197
|
|
|
return false; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
|
201
|
|
|
|
202
|
|
|
|
203
|
|
|
/**************** UPDATE TRANSACTION WITH PAYMENT ****************/ |
204
|
|
|
/** |
205
|
|
|
* array of TXN IDs and the payment |
206
|
|
|
* |
207
|
|
|
* @var array |
208
|
|
|
*/ |
209
|
|
|
protected static $_update_transactions_with_payment = array(); |
210
|
|
|
|
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* schedule_update_transaction_with_payment |
214
|
|
|
* sets a wp_schedule_single_event() for updating any TXNs that may |
215
|
|
|
* require updating due to recently received payments |
216
|
|
|
* |
217
|
|
|
* @param int $timestamp |
218
|
|
|
* @param int $TXN_ID |
219
|
|
|
* @param int $PAY_ID |
220
|
|
|
*/ |
221
|
|
View Code Duplication |
public static function schedule_update_transaction_with_payment( |
222
|
|
|
$timestamp, |
223
|
|
|
$TXN_ID, |
224
|
|
|
$PAY_ID |
225
|
|
|
) { |
226
|
|
|
do_action('AHEE_log', __CLASS__, __FUNCTION__); |
227
|
|
|
// validate $TXN_ID and $timestamp |
228
|
|
|
$TXN_ID = absint($TXN_ID); |
229
|
|
|
$timestamp = absint($timestamp); |
230
|
|
|
if ($TXN_ID && $timestamp) { |
231
|
|
|
wp_schedule_single_event( |
232
|
|
|
$timestamp, |
233
|
|
|
'AHEE__EE_Cron_Tasks__update_transaction_with_payment_2', |
234
|
|
|
array($TXN_ID, $PAY_ID) |
235
|
|
|
); |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* setup_update_for_transaction_with_payment |
242
|
|
|
* this is the callback for the action hook: |
243
|
|
|
* 'AHEE__EE_Cron_Tasks__update_transaction_with_payment' |
244
|
|
|
* which is setup by EE_Cron_Tasks::schedule_update_transaction_with_payment(). |
245
|
|
|
* The passed TXN_ID and associated payment gets added to an array, and then |
246
|
|
|
* the EE_Cron_Tasks::update_transaction_with_payment() function is hooked into |
247
|
|
|
* 'shutdown' which will actually handle the processing of any |
248
|
|
|
* transactions requiring updating, because doing so now would be too early |
249
|
|
|
* and the required resources may not be available |
250
|
|
|
* |
251
|
|
|
* @param int $TXN_ID |
252
|
|
|
* @param int $PAY_ID |
253
|
|
|
*/ |
254
|
|
View Code Duplication |
public static function setup_update_for_transaction_with_payment($TXN_ID = 0, $PAY_ID = 0) |
255
|
|
|
{ |
256
|
|
|
do_action('AHEE_log', __CLASS__, __FUNCTION__, $TXN_ID, '$TXN_ID'); |
257
|
|
|
if (absint($TXN_ID)) { |
258
|
|
|
self::$_update_transactions_with_payment[$TXN_ID] = $PAY_ID; |
259
|
|
|
add_action( |
260
|
|
|
'shutdown', |
261
|
|
|
array('EE_Cron_Tasks', 'update_transaction_with_payment'), |
262
|
|
|
5 |
263
|
|
|
); |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* update_transaction_with_payment |
270
|
|
|
* loops through the self::$_abandoned_transactions array |
271
|
|
|
* and attempts to finalize any TXNs that have not been completed |
272
|
|
|
* but have had their sessions expired, most likely due to a user not |
273
|
|
|
* returning from an off-site payment gateway |
274
|
|
|
* |
275
|
|
|
* @throws \EE_Error |
276
|
|
|
* @throws \DomainException |
277
|
|
|
* @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
278
|
|
|
* @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
279
|
|
|
* @throws \InvalidArgumentException |
280
|
|
|
* @throws \ReflectionException |
281
|
|
|
*/ |
282
|
|
|
public static function update_transaction_with_payment() |
283
|
|
|
{ |
284
|
|
|
do_action('AHEE_log', __CLASS__, __FUNCTION__); |
285
|
|
|
if ( |
286
|
|
|
// are there any TXNs that need cleaning up ? |
287
|
|
|
empty(self::$_update_transactions_with_payment) |
288
|
|
|
// reschedule the cron if we can't hit the db right now |
289
|
|
|
|| EE_Cron_Tasks::reschedule_cron_for_transactions_if_maintenance_mode( |
290
|
|
|
'schedule_update_transaction_with_payment', |
291
|
|
|
self::$_update_transactions_with_payment |
292
|
|
|
) |
293
|
|
|
) { |
294
|
|
|
return; |
295
|
|
|
} |
296
|
|
|
/** @type EE_Payment_Processor $payment_processor */ |
297
|
|
|
$payment_processor = EE_Registry::instance()->load_core('Payment_Processor'); |
298
|
|
|
// set revisit flag for payment processor |
299
|
|
|
$payment_processor->set_revisit(); |
300
|
|
|
// load EEM_Transaction |
301
|
|
|
EE_Registry::instance()->load_model('Transaction'); |
302
|
|
|
foreach (self::$_update_transactions_with_payment as $TXN_ID => $PAY_ID) { |
303
|
|
|
// reschedule the cron if we can't hit the db right now |
304
|
|
|
if (! EE_Maintenance_Mode::instance()->models_can_query()) { |
305
|
|
|
// reset cron job for updating the TXN |
306
|
|
|
EE_Cron_Tasks::schedule_update_transaction_with_payment( |
307
|
|
|
time() + EE_Cron_Tasks::reschedule_timeout, |
308
|
|
|
$TXN_ID, |
309
|
|
|
$PAY_ID |
310
|
|
|
); |
311
|
|
|
continue; |
312
|
|
|
} |
313
|
|
|
$transaction = EEM_Transaction::instance()->get_one_by_ID($TXN_ID); |
314
|
|
|
$payment = EEM_Payment::instance()->get_one_by_ID($PAY_ID); |
315
|
|
|
// verify transaction |
316
|
|
|
if ($transaction instanceof EE_Transaction && $payment instanceof EE_Payment) { |
317
|
|
|
// now try to update the TXN with any payments |
318
|
|
|
$payment_processor->update_txn_based_on_payment($transaction, $payment, true, true); |
319
|
|
|
} |
320
|
|
|
unset(self::$_update_transactions_with_payment[$TXN_ID]); |
321
|
|
|
} |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
|
325
|
|
|
|
326
|
|
|
/************ END OF UPDATE TRANSACTION WITH PAYMENT ************/ |
327
|
|
|
/***************** FINALIZE ABANDONED TRANSACTIONS *****************/ |
328
|
|
|
/** |
329
|
|
|
* array of TXN IDs |
330
|
|
|
* |
331
|
|
|
* @var array |
332
|
|
|
*/ |
333
|
|
|
protected static $_abandoned_transactions = array(); |
334
|
|
|
|
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* schedule_finalize_abandoned_transactions_check |
338
|
|
|
* sets a wp_schedule_single_event() for finalizing any TXNs that may |
339
|
|
|
* have been abandoned during the registration process |
340
|
|
|
* |
341
|
|
|
* @param int $timestamp |
342
|
|
|
* @param int $TXN_ID |
343
|
|
|
*/ |
344
|
|
View Code Duplication |
public static function schedule_finalize_abandoned_transactions_check( |
345
|
|
|
$timestamp, |
346
|
|
|
$TXN_ID |
347
|
|
|
) { |
348
|
|
|
// validate $TXN_ID and $timestamp |
349
|
|
|
$TXN_ID = absint($TXN_ID); |
350
|
|
|
$timestamp = absint($timestamp); |
351
|
|
|
do_action('AHEE_log', __CLASS__, __FUNCTION__, $TXN_ID, '$TXN_ID'); |
352
|
|
|
if ($TXN_ID && $timestamp) { |
353
|
|
|
wp_schedule_single_event( |
354
|
|
|
$timestamp, |
355
|
|
|
'AHEE__EE_Cron_Tasks__finalize_abandoned_transactions', |
356
|
|
|
array($TXN_ID) |
357
|
|
|
); |
358
|
|
|
} |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* check_for_abandoned_transactions |
364
|
|
|
* this is the callback for the action hook: |
365
|
|
|
* 'AHEE__EE_Cron_Tasks__espresso_finalize_abandoned_transactions' |
366
|
|
|
* which is utilized by wp_schedule_single_event() |
367
|
|
|
* in EE_SPCO_Reg_Step_Payment_Options::_post_payment_processing(). |
368
|
|
|
* The passed TXN_ID gets added to an array, and then the |
369
|
|
|
* espresso_finalize_abandoned_transactions() function is hooked into |
370
|
|
|
* 'AHEE__EE_System__core_loaded_and_ready' which will actually handle the |
371
|
|
|
* processing of any abandoned transactions, because doing so now would be |
372
|
|
|
* too early and the required resources may not be available |
373
|
|
|
* |
374
|
|
|
* @param int $TXN_ID |
375
|
|
|
*/ |
376
|
|
View Code Duplication |
public static function check_for_abandoned_transactions($TXN_ID = 0) |
377
|
|
|
{ |
378
|
|
|
do_action('AHEE_log', __CLASS__, __FUNCTION__, $TXN_ID, '$TXN_ID'); |
379
|
|
|
if (absint($TXN_ID)) { |
380
|
|
|
self::$_abandoned_transactions[] = $TXN_ID; |
381
|
|
|
add_action( |
382
|
|
|
'shutdown', |
383
|
|
|
array('EE_Cron_Tasks', 'finalize_abandoned_transactions'), |
384
|
|
|
5 |
385
|
|
|
); |
386
|
|
|
} |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
|
390
|
|
|
/** |
391
|
|
|
* finalize_abandoned_transactions |
392
|
|
|
* loops through the self::$_abandoned_transactions array |
393
|
|
|
* and attempts to finalize any TXNs that have not been completed |
394
|
|
|
* but have had their sessions expired, most likely due to a user not |
395
|
|
|
* returning from an off-site payment gateway |
396
|
|
|
* |
397
|
|
|
* @throws \EE_Error |
398
|
|
|
* @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
399
|
|
|
* @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
400
|
|
|
* @throws \InvalidArgumentException |
401
|
|
|
* @throws \ReflectionException |
402
|
|
|
* @throws \DomainException |
403
|
|
|
*/ |
404
|
|
|
public static function finalize_abandoned_transactions() |
405
|
|
|
{ |
406
|
|
|
do_action('AHEE_log', __CLASS__, __FUNCTION__); |
407
|
|
|
if ( |
408
|
|
|
// are there any TXNs that need cleaning up ? |
409
|
|
|
empty(self::$_abandoned_transactions) |
410
|
|
|
// reschedule the cron if we can't hit the db right now |
411
|
|
|
|| EE_Cron_Tasks::reschedule_cron_for_transactions_if_maintenance_mode( |
412
|
|
|
'schedule_finalize_abandoned_transactions_check', |
413
|
|
|
self::$_abandoned_transactions |
414
|
|
|
) |
415
|
|
|
) { |
416
|
|
|
return; |
417
|
|
|
} |
418
|
|
|
/** @type EE_Transaction_Processor $transaction_processor */ |
419
|
|
|
$transaction_processor = EE_Registry::instance()->load_class('Transaction_Processor'); |
420
|
|
|
// set revisit flag for txn processor |
421
|
|
|
$transaction_processor->set_revisit(); |
422
|
|
|
/** @type EE_Payment_Processor $payment_processor */ |
423
|
|
|
$payment_processor = EE_Registry::instance()->load_core('Payment_Processor'); |
424
|
|
|
// load EEM_Transaction |
425
|
|
|
EE_Registry::instance()->load_model('Transaction'); |
426
|
|
|
foreach (self::$_abandoned_transactions as $TXN_ID) { |
427
|
|
|
do_action('AHEE_log', __CLASS__, __FUNCTION__, $TXN_ID, '$TXN_ID'); |
428
|
|
|
// reschedule the cron if we can't hit the db right now |
429
|
|
|
if (! EE_Maintenance_Mode::instance()->models_can_query()) { |
430
|
|
|
// reset cron job for finalizing the TXN |
431
|
|
|
EE_Cron_Tasks::schedule_finalize_abandoned_transactions_check( |
432
|
|
|
time() + EE_Cron_Tasks::reschedule_timeout, |
433
|
|
|
$TXN_ID |
434
|
|
|
); |
435
|
|
|
continue; |
436
|
|
|
} |
437
|
|
|
$transaction = EEM_Transaction::instance()->get_one_by_ID($TXN_ID); |
438
|
|
|
// verify transaction |
439
|
|
|
if ($transaction instanceof EE_Transaction) { |
440
|
|
|
// don't finalize the TXN if it has already been completed |
441
|
|
|
if ($transaction->all_reg_steps_completed() === true) { |
442
|
|
|
continue; |
443
|
|
|
} |
444
|
|
|
// let's simulate an IPN here which will trigger any notifications that need to go out |
445
|
|
|
$payment_processor->update_txn_based_on_payment($transaction, $transaction->last_payment(), true, true); |
446
|
|
|
do_action('AHEE__EE_Cron_Tasks__finalize_abandoned_transactions__abandoned_transaction', $transaction); |
447
|
|
|
} |
448
|
|
|
unset(self::$_abandoned_transactions[$TXN_ID]); |
449
|
|
|
} |
450
|
|
|
} |
451
|
|
|
|
452
|
|
|
|
453
|
|
|
|
454
|
|
|
/************* END OF FINALIZE ABANDONED TRANSACTIONS *************/ |
455
|
|
|
/***************** EXPIRED TRANSACTION CHECK *****************/ |
456
|
|
|
/** |
457
|
|
|
* array of TXN IDs |
458
|
|
|
* |
459
|
|
|
* @var array |
460
|
|
|
*/ |
461
|
|
|
protected static $_expired_transactions = array(); |
462
|
|
|
|
463
|
|
|
|
464
|
|
|
|
465
|
|
|
/** |
466
|
|
|
* schedule_expired_transaction_check |
467
|
|
|
* sets a wp_schedule_single_event() for following up on TXNs after their session has expired |
468
|
|
|
* |
469
|
|
|
* @param int $timestamp |
470
|
|
|
* @param int $TXN_ID |
471
|
|
|
*/ |
472
|
|
|
public static function schedule_expired_transaction_check( |
473
|
|
|
$timestamp, |
474
|
|
|
$TXN_ID |
475
|
|
|
) { |
476
|
|
|
// validate $TXN_ID and $timestamp |
477
|
|
|
$TXN_ID = absint($TXN_ID); |
478
|
|
|
$timestamp = absint($timestamp); |
479
|
|
|
if ($TXN_ID && $timestamp) { |
480
|
|
|
wp_schedule_single_event( |
481
|
|
|
$timestamp, |
482
|
|
|
'AHEE__EE_Cron_Tasks__expired_transaction_check', |
483
|
|
|
array($TXN_ID) |
484
|
|
|
); |
485
|
|
|
} |
486
|
|
|
} |
487
|
|
|
|
488
|
|
|
|
489
|
|
|
|
490
|
|
|
/** |
491
|
|
|
* expired_transaction_check |
492
|
|
|
* this is the callback for the action hook: |
493
|
|
|
* 'AHEE__EE_Cron_Tasks__transaction_session_expiration_check' |
494
|
|
|
* which is utilized by wp_schedule_single_event() |
495
|
|
|
* in \EED_Single_Page_Checkout::_initialize_transaction(). |
496
|
|
|
* The passed TXN_ID gets added to an array, and then the |
497
|
|
|
* process_expired_transactions() function is hooked into |
498
|
|
|
* 'AHEE__EE_System__core_loaded_and_ready' which will actually handle the |
499
|
|
|
* processing of any failed transactions, because doing so now would be |
500
|
|
|
* too early and the required resources may not be available |
501
|
|
|
* |
502
|
|
|
* @param int $TXN_ID |
503
|
|
|
*/ |
504
|
|
|
public static function expired_transaction_check($TXN_ID = 0) |
505
|
|
|
{ |
506
|
|
|
if (absint($TXN_ID)) { |
507
|
|
|
self::$_expired_transactions[$TXN_ID] = $TXN_ID; |
508
|
|
|
add_action( |
509
|
|
|
'shutdown', |
510
|
|
|
array('EE_Cron_Tasks', 'process_expired_transactions'), |
511
|
|
|
5 |
512
|
|
|
); |
513
|
|
|
} |
514
|
|
|
} |
515
|
|
|
|
516
|
|
|
|
517
|
|
|
|
518
|
|
|
/** |
519
|
|
|
* process_expired_transactions |
520
|
|
|
* loops through the self::$_expired_transactions array and processes any failed TXNs |
521
|
|
|
* |
522
|
|
|
* @throws \EE_Error |
523
|
|
|
* @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
524
|
|
|
* @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
525
|
|
|
* @throws \InvalidArgumentException |
526
|
|
|
* @throws \ReflectionException |
527
|
|
|
* @throws \DomainException |
528
|
|
|
*/ |
529
|
|
|
public static function process_expired_transactions() |
530
|
|
|
{ |
531
|
|
|
if ( |
532
|
|
|
// are there any TXNs that need cleaning up ? |
533
|
|
|
empty(self::$_expired_transactions) |
534
|
|
|
// reschedule the cron if we can't hit the db right now |
535
|
|
|
|| EE_Cron_Tasks::reschedule_cron_for_transactions_if_maintenance_mode( |
536
|
|
|
'schedule_expired_transaction_check', |
537
|
|
|
self::$_expired_transactions |
538
|
|
|
) |
539
|
|
|
) { |
540
|
|
|
return; |
541
|
|
|
} |
542
|
|
|
/** @type EE_Transaction_Processor $transaction_processor */ |
543
|
|
|
$transaction_processor = EE_Registry::instance()->load_class('Transaction_Processor'); |
544
|
|
|
// set revisit flag for txn processor |
545
|
|
|
$transaction_processor->set_revisit(); |
546
|
|
|
// load EEM_Transaction |
547
|
|
|
EE_Registry::instance()->load_model('Transaction'); |
548
|
|
|
foreach (self::$_expired_transactions as $TXN_ID) { |
549
|
|
|
$transaction = EEM_Transaction::instance()->get_one_by_ID($TXN_ID); |
550
|
|
|
// verify transaction and whether it is failed or not |
551
|
|
|
if ($transaction instanceof EE_Transaction) { |
552
|
|
|
switch ($transaction->status_ID()) { |
553
|
|
|
// Completed TXNs |
554
|
|
|
case EEM_Transaction::complete_status_code : |
555
|
|
|
do_action( |
556
|
|
|
'AHEE__EE_Cron_Tasks__process_expired_transactions__completed_transaction', |
557
|
|
|
$transaction |
558
|
|
|
); |
559
|
|
|
break; |
560
|
|
|
// Overpaid TXNs |
561
|
|
|
case EEM_Transaction::overpaid_status_code : |
562
|
|
|
do_action( |
563
|
|
|
'AHEE__EE_Cron_Tasks__process_expired_transactions__overpaid_transaction', |
564
|
|
|
$transaction |
565
|
|
|
); |
566
|
|
|
break; |
567
|
|
|
// Incomplete TXNs |
568
|
|
|
case EEM_Transaction::incomplete_status_code : |
569
|
|
|
do_action( |
570
|
|
|
'AHEE__EE_Cron_Tasks__process_expired_transactions__incomplete_transaction', |
571
|
|
|
$transaction |
572
|
|
|
); |
573
|
|
|
// todo : merge the finalize_abandoned_transactions cron into this one... |
574
|
|
|
// todo : move business logic into EE_Transaction_Processor for finalizing abandoned transactions |
575
|
|
|
break; |
576
|
|
|
// Failed TXNs |
577
|
|
|
case EEM_Transaction::failed_status_code : |
578
|
|
|
do_action( |
579
|
|
|
'AHEE__EE_Cron_Tasks__process_expired_transactions__failed_transaction', |
580
|
|
|
$transaction |
581
|
|
|
); |
582
|
|
|
// todo : perform garbage collection here and remove clean_out_junk_transactions() |
583
|
|
|
//$registrations = $transaction->registrations(); |
584
|
|
|
//if ( ! empty( $registrations ) ) { |
585
|
|
|
// foreach ( $registrations as $registration ) { |
586
|
|
|
// if ( $registration instanceof EE_Registration ) { |
587
|
|
|
//$delete_registration = true; |
588
|
|
|
//if ( $registration->attendee() instanceof EE_Attendee ) { |
589
|
|
|
// $delete_registration = false; |
590
|
|
|
//} |
591
|
|
|
//if ( $delete_registration ) { |
592
|
|
|
// $registration->delete_permanently(); |
593
|
|
|
// $registration->delete_related_permanently(); |
594
|
|
|
//} |
595
|
|
|
// } |
596
|
|
|
// } |
597
|
|
|
//} |
598
|
|
|
break; |
599
|
|
|
} |
600
|
|
|
} |
601
|
|
|
unset(self::$_expired_transactions[$TXN_ID]); |
602
|
|
|
} |
603
|
|
|
} |
604
|
|
|
|
605
|
|
|
|
606
|
|
|
|
607
|
|
|
/************* END OF EXPIRED TRANSACTION CHECK *************/ |
608
|
|
|
/************* START CLEAN UP BOT TRANSACTIONS **********************/ |
609
|
|
|
|
610
|
|
|
|
611
|
|
|
|
612
|
|
|
/** |
613
|
|
|
* when a transaction is initially made, schedule this check. |
614
|
|
|
* if it has NO REG data by the time it has expired, forget about it |
615
|
|
|
* |
616
|
|
|
* @throws EE_Error |
617
|
|
|
* @throws InvalidArgumentException |
618
|
|
|
* @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
619
|
|
|
* @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
620
|
|
|
*/ |
621
|
|
|
public static function clean_out_junk_transactions() |
622
|
|
|
{ |
623
|
|
|
if (EE_Maintenance_Mode::instance()->models_can_query()) { |
624
|
|
|
EEM_Transaction::instance('')->delete_junk_transactions(); |
625
|
|
|
EEM_Registration::instance('')->delete_registrations_with_no_transaction(); |
626
|
|
|
EEM_Line_Item::instance('')->delete_line_items_with_no_transaction(); |
627
|
|
|
} |
628
|
|
|
} |
629
|
|
|
|
630
|
|
|
|
631
|
|
|
|
632
|
|
|
/** |
633
|
|
|
* Deletes old gateway logs. After about a week we usually don't need them for debugging. But folks can filter that. |
634
|
|
|
* |
635
|
|
|
* @throws \EE_Error |
636
|
|
|
* @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
637
|
|
|
* @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
638
|
|
|
* @throws \InvalidArgumentException |
639
|
|
|
*/ |
640
|
|
|
public static function clean_out_old_gateway_logs() |
641
|
|
|
{ |
642
|
|
|
if (EE_Maintenance_Mode::instance()->models_can_query()) { |
643
|
|
|
$time_diff_for_comparison = apply_filters( |
644
|
|
|
'FHEE__EE_Cron_Tasks__clean_out_old_gateway_logs__time_diff_for_comparison', |
645
|
|
|
'-1 week' |
646
|
|
|
); |
647
|
|
|
EEM_Change_Log::instance()->delete_gateway_logs_older_than(new DateTime($time_diff_for_comparison)); |
648
|
|
|
} |
649
|
|
|
} |
650
|
|
|
|
651
|
|
|
|
652
|
|
|
} |
653
|
|
|
// End of file EE_Cron_Tasks.core.php |
654
|
|
|
// Location: /EE_Cron_Tasks.core.php |
655
|
|
|
|