Completed
Branch BUG/add-limits-to-cron-queries (4083ad)
by
unknown
16:35 queued 08:51
created
core/db_models/EEM_Transaction.model.php 1 patch
Indentation   +452 added lines, -452 removed lines patch added patch discarded remove patch
@@ -16,230 +16,230 @@  discard block
 block discarded – undo
16 16
 class EEM_Transaction extends EEM_Base
17 17
 {
18 18
 
19
-    // private instance of the Transaction object
20
-    protected static $_instance;
21
-
22
-    /**
23
-     * Status ID(STS_ID on esp_status table) to indicate the transaction is complete,
24
-     * but payment is pending. This is the state for transactions where payment is promised
25
-     * from an offline gateway.
26
-     */
27
-    //  const open_status_code = 'TPN';
28
-
29
-    /**
30
-     * Status ID(STS_ID on esp_status table) to indicate the transaction failed,
31
-     * either due to a technical reason (server or computer crash during registration),
32
-     *  or some other reason that prevent the collection of any useful contact information from any of the registrants
33
-     */
34
-    const failed_status_code = 'TFL';
35
-
36
-    /**
37
-     * Status ID(STS_ID on esp_status table) to indicate the transaction was abandoned,
38
-     * either due to a technical reason (server or computer crash during registration),
39
-     * or due to an abandoned cart after registrant chose not to complete the registration process
40
-     * HOWEVER...
41
-     * an abandoned TXN differs from a failed TXN in that it was able to capture contact information for at least one
42
-     * registrant
43
-     */
44
-    const abandoned_status_code = 'TAB';
45
-
46
-    /**
47
-     * Status ID(STS_ID on esp_status table) to indicate an incomplete transaction,
48
-     * meaning that monies are still owing: TXN_paid < TXN_total
49
-     */
50
-    const incomplete_status_code = 'TIN';
51
-
52
-    /**
53
-     * Status ID (STS_ID on esp_status table) to indicate a complete transaction.
54
-     * meaning that NO monies are owing: TXN_paid == TXN_total
55
-     */
56
-    const complete_status_code = 'TCM';
57
-
58
-    /**
59
-     *  Status ID(STS_ID on esp_status table) to indicate the transaction is overpaid.
60
-     *  This is the same as complete, but site admins actually owe clients the moneys!  TXN_paid > TXN_total
61
-     */
62
-    const overpaid_status_code = 'TOP';
63
-
64
-
65
-    /**
66
-     *    private constructor to prevent direct creation
67
-     *
68
-     * @Constructor
69
-     * @access protected
70
-     *
71
-     * @param string $timezone string representing the timezone we want to set for returned Date Time Strings (and any
72
-     *                         incoming timezone data that gets saved). Note this just sends the timezone info to the
73
-     *                         date time model field objects.  Default is NULL (and will be assumed using the set
74
-     *                         timezone in the 'timezone_string' wp option)
75
-     *
76
-     * @return EEM_Transaction
77
-     * @throws \EE_Error
78
-     */
79
-    protected function __construct($timezone)
80
-    {
81
-        $this->singular_item = __('Transaction', 'event_espresso');
82
-        $this->plural_item   = __('Transactions', 'event_espresso');
83
-
84
-        $this->_tables                 = array(
85
-            'TransactionTable' => new EE_Primary_Table('esp_transaction', 'TXN_ID')
86
-        );
87
-        $this->_fields                 = array(
88
-            'TransactionTable' => array(
89
-                'TXN_ID'           => new EE_Primary_Key_Int_Field('TXN_ID', __('Transaction ID', 'event_espresso')),
90
-                'TXN_timestamp'    => new EE_Datetime_Field(
91
-                    'TXN_timestamp',
92
-                    __('date when transaction was created', 'event_espresso'),
93
-                    false,
94
-                    EE_Datetime_Field::now,
95
-                    $timezone
96
-                ),
97
-                'TXN_total'        => new EE_Money_Field(
98
-                    'TXN_total',
99
-                    __('Total value of Transaction', 'event_espresso'),
100
-                    false,
101
-                    0
102
-                ),
103
-                'TXN_paid'         => new EE_Money_Field(
104
-                    'TXN_paid',
105
-                    __('Amount paid towards transaction to date', 'event_espresso'),
106
-                    false,
107
-                    0
108
-                ),
109
-                'STS_ID'           => new EE_Foreign_Key_String_Field(
110
-                    'STS_ID',
111
-                    __('Status ID', 'event_espresso'),
112
-                    false,
113
-                    EEM_Transaction::failed_status_code,
114
-                    'Status'
115
-                ),
116
-                'TXN_session_data' => new EE_Serialized_Text_Field(
117
-                    'TXN_session_data',
118
-                    __('Serialized session data', 'event_espresso'),
119
-                    true,
120
-                    ''
121
-                ),
122
-                'TXN_hash_salt'    => new EE_Plain_Text_Field(
123
-                    'TXN_hash_salt',
124
-                    __('Transaction Hash Salt', 'event_espresso'),
125
-                    true,
126
-                    ''
127
-                ),
128
-                'PMD_ID'           => new EE_Foreign_Key_Int_Field(
129
-                    'PMD_ID',
130
-                    __("Last Used Payment Method", 'event_espresso'),
131
-                    true,
132
-                    null,
133
-                    'Payment_Method'
134
-                ),
135
-                'TXN_reg_steps'    => new EE_Serialized_Text_Field(
136
-                    'TXN_reg_steps',
137
-                    __('Registration Steps', 'event_espresso'),
138
-                    false,
139
-                    array()
140
-                ),
141
-            )
142
-        );
143
-        $this->_model_relations        = array(
144
-            'Registration'   => new EE_Has_Many_Relation(),
145
-            'Payment'        => new EE_Has_Many_Relation(),
146
-            'Status'         => new EE_Belongs_To_Relation(),
147
-            'Line_Item'      => new EE_Has_Many_Relation(false),
148
-            // you can delete a transaction without needing to delete its line items
149
-            'Payment_Method' => new EE_Belongs_To_Relation(),
150
-            'Message'        => new EE_Has_Many_Relation()
151
-        );
152
-        $this->_model_chain_to_wp_user = 'Registration.Event';
153
-        parent::__construct($timezone);
154
-    }
155
-
156
-
157
-    /**
158
-     *    txn_status_array
159
-     * get list of transaction statuses
160
-     *
161
-     * @access public
162
-     * @return array
163
-     */
164
-    public static function txn_status_array()
165
-    {
166
-        return apply_filters(
167
-            'FHEE__EEM_Transaction__txn_status_array',
168
-            array(
169
-                EEM_Transaction::overpaid_status_code,
170
-                EEM_Transaction::complete_status_code,
171
-                EEM_Transaction::incomplete_status_code,
172
-                EEM_Transaction::abandoned_status_code,
173
-                EEM_Transaction::failed_status_code,
174
-            )
175
-        );
176
-    }
177
-
178
-    /**
179
-     *        get the revenue per day  for the Transaction Admin page Reports Tab
180
-     *
181
-     * @access        public
182
-     *
183
-     * @param string $period
184
-     *
185
-     * @return \stdClass[]
186
-     */
187
-    public function get_revenue_per_day_report($period = '-1 month')
188
-    {
189
-        $sql_date = $this->convert_datetime_for_query(
190
-            'TXN_timestamp',
191
-            date('Y-m-d H:i:s', strtotime($period)),
192
-            'Y-m-d H:i:s',
193
-            'UTC'
194
-        );
195
-
196
-        $query_interval = EEH_DTT_Helper::get_sql_query_interval_for_offset($this->get_timezone(), 'TXN_timestamp');
197
-
198
-        return $this->_get_all_wpdb_results(
199
-            array(
200
-                array(
201
-                    'TXN_timestamp' => array('>=', $sql_date)
202
-                ),
203
-                'group_by' => 'txnDate',
204
-                'order_by' => array('TXN_timestamp' => 'ASC')
205
-            ),
206
-            OBJECT,
207
-            array(
208
-                'txnDate' => array('DATE(' . $query_interval . ')', '%s'),
209
-                'revenue' => array('SUM(TransactionTable.TXN_paid)', '%d')
210
-            )
211
-        );
212
-    }
213
-
214
-
215
-    /**
216
-     *        get the revenue per event  for the Transaction Admin page Reports Tab
217
-     *
218
-     * @access        public
219
-     *
220
-     * @param string $period
221
-     *
222
-     * @throws \EE_Error
223
-     * @return mixed
224
-     */
225
-    public function get_revenue_per_event_report($period = '-1 month')
226
-    {
227
-        global $wpdb;
228
-        $transaction_table          = $wpdb->prefix . 'esp_transaction';
229
-        $registration_table         = $wpdb->prefix . 'esp_registration';
230
-        $registration_payment_table = $wpdb->prefix . 'esp_registration_payment';
231
-        $event_table                = $wpdb->posts;
232
-        $payment_table              = $wpdb->prefix . 'esp_payment';
233
-        $sql_date                   = date('Y-m-d H:i:s', strtotime($period));
234
-        $approved_payment_status    = EEM_Payment::status_id_approved;
235
-        $extra_event_on_join        = '';
236
-        // exclude events not authored by user if permissions in effect
237
-        if (! EE_Registry::instance()->CAP->current_user_can('ee_read_others_registrations', 'reg_per_event_report')) {
238
-            $extra_event_on_join = ' AND Event.post_author = ' . get_current_user_id();
239
-        }
240
-
241
-        return $wpdb->get_results(
242
-            "SELECT
19
+	// private instance of the Transaction object
20
+	protected static $_instance;
21
+
22
+	/**
23
+	 * Status ID(STS_ID on esp_status table) to indicate the transaction is complete,
24
+	 * but payment is pending. This is the state for transactions where payment is promised
25
+	 * from an offline gateway.
26
+	 */
27
+	//  const open_status_code = 'TPN';
28
+
29
+	/**
30
+	 * Status ID(STS_ID on esp_status table) to indicate the transaction failed,
31
+	 * either due to a technical reason (server or computer crash during registration),
32
+	 *  or some other reason that prevent the collection of any useful contact information from any of the registrants
33
+	 */
34
+	const failed_status_code = 'TFL';
35
+
36
+	/**
37
+	 * Status ID(STS_ID on esp_status table) to indicate the transaction was abandoned,
38
+	 * either due to a technical reason (server or computer crash during registration),
39
+	 * or due to an abandoned cart after registrant chose not to complete the registration process
40
+	 * HOWEVER...
41
+	 * an abandoned TXN differs from a failed TXN in that it was able to capture contact information for at least one
42
+	 * registrant
43
+	 */
44
+	const abandoned_status_code = 'TAB';
45
+
46
+	/**
47
+	 * Status ID(STS_ID on esp_status table) to indicate an incomplete transaction,
48
+	 * meaning that monies are still owing: TXN_paid < TXN_total
49
+	 */
50
+	const incomplete_status_code = 'TIN';
51
+
52
+	/**
53
+	 * Status ID (STS_ID on esp_status table) to indicate a complete transaction.
54
+	 * meaning that NO monies are owing: TXN_paid == TXN_total
55
+	 */
56
+	const complete_status_code = 'TCM';
57
+
58
+	/**
59
+	 *  Status ID(STS_ID on esp_status table) to indicate the transaction is overpaid.
60
+	 *  This is the same as complete, but site admins actually owe clients the moneys!  TXN_paid > TXN_total
61
+	 */
62
+	const overpaid_status_code = 'TOP';
63
+
64
+
65
+	/**
66
+	 *    private constructor to prevent direct creation
67
+	 *
68
+	 * @Constructor
69
+	 * @access protected
70
+	 *
71
+	 * @param string $timezone string representing the timezone we want to set for returned Date Time Strings (and any
72
+	 *                         incoming timezone data that gets saved). Note this just sends the timezone info to the
73
+	 *                         date time model field objects.  Default is NULL (and will be assumed using the set
74
+	 *                         timezone in the 'timezone_string' wp option)
75
+	 *
76
+	 * @return EEM_Transaction
77
+	 * @throws \EE_Error
78
+	 */
79
+	protected function __construct($timezone)
80
+	{
81
+		$this->singular_item = __('Transaction', 'event_espresso');
82
+		$this->plural_item   = __('Transactions', 'event_espresso');
83
+
84
+		$this->_tables                 = array(
85
+			'TransactionTable' => new EE_Primary_Table('esp_transaction', 'TXN_ID')
86
+		);
87
+		$this->_fields                 = array(
88
+			'TransactionTable' => array(
89
+				'TXN_ID'           => new EE_Primary_Key_Int_Field('TXN_ID', __('Transaction ID', 'event_espresso')),
90
+				'TXN_timestamp'    => new EE_Datetime_Field(
91
+					'TXN_timestamp',
92
+					__('date when transaction was created', 'event_espresso'),
93
+					false,
94
+					EE_Datetime_Field::now,
95
+					$timezone
96
+				),
97
+				'TXN_total'        => new EE_Money_Field(
98
+					'TXN_total',
99
+					__('Total value of Transaction', 'event_espresso'),
100
+					false,
101
+					0
102
+				),
103
+				'TXN_paid'         => new EE_Money_Field(
104
+					'TXN_paid',
105
+					__('Amount paid towards transaction to date', 'event_espresso'),
106
+					false,
107
+					0
108
+				),
109
+				'STS_ID'           => new EE_Foreign_Key_String_Field(
110
+					'STS_ID',
111
+					__('Status ID', 'event_espresso'),
112
+					false,
113
+					EEM_Transaction::failed_status_code,
114
+					'Status'
115
+				),
116
+				'TXN_session_data' => new EE_Serialized_Text_Field(
117
+					'TXN_session_data',
118
+					__('Serialized session data', 'event_espresso'),
119
+					true,
120
+					''
121
+				),
122
+				'TXN_hash_salt'    => new EE_Plain_Text_Field(
123
+					'TXN_hash_salt',
124
+					__('Transaction Hash Salt', 'event_espresso'),
125
+					true,
126
+					''
127
+				),
128
+				'PMD_ID'           => new EE_Foreign_Key_Int_Field(
129
+					'PMD_ID',
130
+					__("Last Used Payment Method", 'event_espresso'),
131
+					true,
132
+					null,
133
+					'Payment_Method'
134
+				),
135
+				'TXN_reg_steps'    => new EE_Serialized_Text_Field(
136
+					'TXN_reg_steps',
137
+					__('Registration Steps', 'event_espresso'),
138
+					false,
139
+					array()
140
+				),
141
+			)
142
+		);
143
+		$this->_model_relations        = array(
144
+			'Registration'   => new EE_Has_Many_Relation(),
145
+			'Payment'        => new EE_Has_Many_Relation(),
146
+			'Status'         => new EE_Belongs_To_Relation(),
147
+			'Line_Item'      => new EE_Has_Many_Relation(false),
148
+			// you can delete a transaction without needing to delete its line items
149
+			'Payment_Method' => new EE_Belongs_To_Relation(),
150
+			'Message'        => new EE_Has_Many_Relation()
151
+		);
152
+		$this->_model_chain_to_wp_user = 'Registration.Event';
153
+		parent::__construct($timezone);
154
+	}
155
+
156
+
157
+	/**
158
+	 *    txn_status_array
159
+	 * get list of transaction statuses
160
+	 *
161
+	 * @access public
162
+	 * @return array
163
+	 */
164
+	public static function txn_status_array()
165
+	{
166
+		return apply_filters(
167
+			'FHEE__EEM_Transaction__txn_status_array',
168
+			array(
169
+				EEM_Transaction::overpaid_status_code,
170
+				EEM_Transaction::complete_status_code,
171
+				EEM_Transaction::incomplete_status_code,
172
+				EEM_Transaction::abandoned_status_code,
173
+				EEM_Transaction::failed_status_code,
174
+			)
175
+		);
176
+	}
177
+
178
+	/**
179
+	 *        get the revenue per day  for the Transaction Admin page Reports Tab
180
+	 *
181
+	 * @access        public
182
+	 *
183
+	 * @param string $period
184
+	 *
185
+	 * @return \stdClass[]
186
+	 */
187
+	public function get_revenue_per_day_report($period = '-1 month')
188
+	{
189
+		$sql_date = $this->convert_datetime_for_query(
190
+			'TXN_timestamp',
191
+			date('Y-m-d H:i:s', strtotime($period)),
192
+			'Y-m-d H:i:s',
193
+			'UTC'
194
+		);
195
+
196
+		$query_interval = EEH_DTT_Helper::get_sql_query_interval_for_offset($this->get_timezone(), 'TXN_timestamp');
197
+
198
+		return $this->_get_all_wpdb_results(
199
+			array(
200
+				array(
201
+					'TXN_timestamp' => array('>=', $sql_date)
202
+				),
203
+				'group_by' => 'txnDate',
204
+				'order_by' => array('TXN_timestamp' => 'ASC')
205
+			),
206
+			OBJECT,
207
+			array(
208
+				'txnDate' => array('DATE(' . $query_interval . ')', '%s'),
209
+				'revenue' => array('SUM(TransactionTable.TXN_paid)', '%d')
210
+			)
211
+		);
212
+	}
213
+
214
+
215
+	/**
216
+	 *        get the revenue per event  for the Transaction Admin page Reports Tab
217
+	 *
218
+	 * @access        public
219
+	 *
220
+	 * @param string $period
221
+	 *
222
+	 * @throws \EE_Error
223
+	 * @return mixed
224
+	 */
225
+	public function get_revenue_per_event_report($period = '-1 month')
226
+	{
227
+		global $wpdb;
228
+		$transaction_table          = $wpdb->prefix . 'esp_transaction';
229
+		$registration_table         = $wpdb->prefix . 'esp_registration';
230
+		$registration_payment_table = $wpdb->prefix . 'esp_registration_payment';
231
+		$event_table                = $wpdb->posts;
232
+		$payment_table              = $wpdb->prefix . 'esp_payment';
233
+		$sql_date                   = date('Y-m-d H:i:s', strtotime($period));
234
+		$approved_payment_status    = EEM_Payment::status_id_approved;
235
+		$extra_event_on_join        = '';
236
+		// exclude events not authored by user if permissions in effect
237
+		if (! EE_Registry::instance()->CAP->current_user_can('ee_read_others_registrations', 'reg_per_event_report')) {
238
+			$extra_event_on_join = ' AND Event.post_author = ' . get_current_user_id();
239
+		}
240
+
241
+		return $wpdb->get_results(
242
+			"SELECT
243 243
 			Transaction_Event.event_name AS event_name,
244 244
 			SUM(Transaction_Event.paid) AS revenue
245 245
 			FROM
@@ -267,233 +267,233 @@  discard block
 block discarded – undo
267 267
 					$extra_event_on_join
268 268
 				) AS Transaction_Event
269 269
 			GROUP BY event_name",
270
-            OBJECT
271
-        );
272
-    }
273
-
274
-
275
-    /**
276
-     * Gets the current transaction given the reg_url_link, or assumes the reg_url_link is in the
277
-     * $_REQUEST global variable. Either way, tries to find the current transaction (through
278
-     * the registration pointed to by reg_url_link), if not returns null
279
-     *
280
-     * @param string $reg_url_link
281
-     *
282
-     * @return EE_Transaction
283
-     */
284
-    public function get_transaction_from_reg_url_link($reg_url_link = '')
285
-    {
286
-        return $this->get_one(array(
287
-            array(
288
-                'Registration.REG_url_link' => ! empty($reg_url_link) ? $reg_url_link : EE_Registry::instance()->REQ->get(
289
-                    'e_reg_url_link',
290
-                    ''
291
-                )
292
-            )
293
-        ));
294
-    }
295
-
296
-
297
-    /**
298
-     * Updates the provided EE_Transaction with all the applicable payments
299
-     * (or fetch the EE_Transaction from its ID)
300
-     *
301
-     * @deprecated
302
-     *
303
-     * @param EE_Transaction|int $transaction_obj_or_id
304
-     * @param boolean            $save_txn whether or not to save the transaction during this function call
305
-     *
306
-     * @return boolean
307
-     * @throws \EE_Error
308
-     */
309
-    public function update_based_on_payments($transaction_obj_or_id, $save_txn = true)
310
-    {
311
-        EE_Error::doing_it_wrong(
312
-            __CLASS__ . '::' . __FUNCTION__,
313
-            sprintf(
314
-                __('This method is deprecated. Please use "%s" instead', 'event_espresso'),
315
-                'EE_Transaction_Processor::update_transaction_and_registrations_after_checkout_or_payment()'
316
-            ),
317
-            '4.6.0'
318
-        );
319
-        /** @type EE_Transaction_Processor $transaction_processor */
320
-        $transaction_processor = EE_Registry::instance()->load_class('Transaction_Processor');
321
-
322
-        return $transaction_processor->update_transaction_and_registrations_after_checkout_or_payment(
323
-            $this->ensure_is_obj($transaction_obj_or_id)
324
-        );
325
-    }
326
-
327
-    /**
328
-     * Deletes "junk" transactions that were probably added by bots. There might be TONS
329
-     * of these, so we are very careful to NOT select (which the models do even when deleting),
330
-     * and so we only use wpdb directly and only do minimal joins.
331
-     * Transactions are considered "junk" if they're failed for longer than a week.
332
-     * Also, there is an extra check for payments related to the transaction, because if a transaction has a payment on
333
-     * it, it's probably not junk (regardless of what status it has).
334
-     * The downside to this approach is that is addons are listening for object deletions
335
-     * on EEM_Base::delete() they won't be notified of this.  However, there is an action that plugins can hook into
336
-     * to catch these types of deletions.
337
-     *
338
-     * @global WPDB $wpdb
339
-     * @return mixed
340
-     */
341
-    public function delete_junk_transactions()
342
-    {
343
-        /** @type WPDB $wpdb */
344
-        global $wpdb;
345
-        $deleted             = false;
346
-        $time_to_leave_alone = apply_filters(
347
-            'FHEE__EEM_Transaction__delete_junk_transactions__time_to_leave_alone',
348
-            WEEK_IN_SECONDS
349
-        );
350
-
351
-
352
-        /**
353
-         * This allows code to filter the query arguments used for retrieving the transaction IDs to delete.
354
-         * Useful for plugins that want to exclude transactions matching certain query parameters.
355
-         * The query parameters should be in the format accepted by the EEM_Base model queries.
356
-         */
357
-        $ids_query = apply_filters(
358
-            'FHEE__EEM_Transaction__delete_junk_transactions__initial_query_args',
359
-            array(
360
-                0 => array(
361
-                    'STS_ID'        => EEM_Transaction::failed_status_code,
362
-                    'Payment.PAY_ID' => array( 'IS NULL' ),
363
-                    'TXN_timestamp' => array('<', time() - $time_to_leave_alone)
364
-                ),
365
-                'order_by' => ['TXN_timestamp' => 'DESC'],
366
-                'limit' => 1000
367
-            ),
368
-            $time_to_leave_alone
369
-        );
370
-
371
-
372
-        /**
373
-         * This filter is for when code needs to filter the list of transaction ids that represent transactions
374
-         * about to be deleted based on some other criteria that isn't easily done via the query args filter.
375
-         */
376
-        $txn_ids = apply_filters(
377
-            'FHEE__EEM_Transaction__delete_junk_transactions__transaction_ids_to_delete',
378
-            EEM_Transaction::instance()->get_col($ids_query, 'TXN_ID'),
379
-            $time_to_leave_alone
380
-        );
381
-        // now that we have the ids to delete
382
-        if (! empty($txn_ids) && is_array($txn_ids)) {
383
-            // first, make sure these TXN's are removed the "ee_locked_transactions" array
384
-            EEM_Transaction::unset_locked_transactions($txn_ids);
385
-
386
-            // Create IDs placeholder.
387
-            $placeholders = array_fill(0, count($txn_ids), '%d');
270
+			OBJECT
271
+		);
272
+	}
273
+
274
+
275
+	/**
276
+	 * Gets the current transaction given the reg_url_link, or assumes the reg_url_link is in the
277
+	 * $_REQUEST global variable. Either way, tries to find the current transaction (through
278
+	 * the registration pointed to by reg_url_link), if not returns null
279
+	 *
280
+	 * @param string $reg_url_link
281
+	 *
282
+	 * @return EE_Transaction
283
+	 */
284
+	public function get_transaction_from_reg_url_link($reg_url_link = '')
285
+	{
286
+		return $this->get_one(array(
287
+			array(
288
+				'Registration.REG_url_link' => ! empty($reg_url_link) ? $reg_url_link : EE_Registry::instance()->REQ->get(
289
+					'e_reg_url_link',
290
+					''
291
+				)
292
+			)
293
+		));
294
+	}
295
+
296
+
297
+	/**
298
+	 * Updates the provided EE_Transaction with all the applicable payments
299
+	 * (or fetch the EE_Transaction from its ID)
300
+	 *
301
+	 * @deprecated
302
+	 *
303
+	 * @param EE_Transaction|int $transaction_obj_or_id
304
+	 * @param boolean            $save_txn whether or not to save the transaction during this function call
305
+	 *
306
+	 * @return boolean
307
+	 * @throws \EE_Error
308
+	 */
309
+	public function update_based_on_payments($transaction_obj_or_id, $save_txn = true)
310
+	{
311
+		EE_Error::doing_it_wrong(
312
+			__CLASS__ . '::' . __FUNCTION__,
313
+			sprintf(
314
+				__('This method is deprecated. Please use "%s" instead', 'event_espresso'),
315
+				'EE_Transaction_Processor::update_transaction_and_registrations_after_checkout_or_payment()'
316
+			),
317
+			'4.6.0'
318
+		);
319
+		/** @type EE_Transaction_Processor $transaction_processor */
320
+		$transaction_processor = EE_Registry::instance()->load_class('Transaction_Processor');
321
+
322
+		return $transaction_processor->update_transaction_and_registrations_after_checkout_or_payment(
323
+			$this->ensure_is_obj($transaction_obj_or_id)
324
+		);
325
+	}
326
+
327
+	/**
328
+	 * Deletes "junk" transactions that were probably added by bots. There might be TONS
329
+	 * of these, so we are very careful to NOT select (which the models do even when deleting),
330
+	 * and so we only use wpdb directly and only do minimal joins.
331
+	 * Transactions are considered "junk" if they're failed for longer than a week.
332
+	 * Also, there is an extra check for payments related to the transaction, because if a transaction has a payment on
333
+	 * it, it's probably not junk (regardless of what status it has).
334
+	 * The downside to this approach is that is addons are listening for object deletions
335
+	 * on EEM_Base::delete() they won't be notified of this.  However, there is an action that plugins can hook into
336
+	 * to catch these types of deletions.
337
+	 *
338
+	 * @global WPDB $wpdb
339
+	 * @return mixed
340
+	 */
341
+	public function delete_junk_transactions()
342
+	{
343
+		/** @type WPDB $wpdb */
344
+		global $wpdb;
345
+		$deleted             = false;
346
+		$time_to_leave_alone = apply_filters(
347
+			'FHEE__EEM_Transaction__delete_junk_transactions__time_to_leave_alone',
348
+			WEEK_IN_SECONDS
349
+		);
350
+
351
+
352
+		/**
353
+		 * This allows code to filter the query arguments used for retrieving the transaction IDs to delete.
354
+		 * Useful for plugins that want to exclude transactions matching certain query parameters.
355
+		 * The query parameters should be in the format accepted by the EEM_Base model queries.
356
+		 */
357
+		$ids_query = apply_filters(
358
+			'FHEE__EEM_Transaction__delete_junk_transactions__initial_query_args',
359
+			array(
360
+				0 => array(
361
+					'STS_ID'        => EEM_Transaction::failed_status_code,
362
+					'Payment.PAY_ID' => array( 'IS NULL' ),
363
+					'TXN_timestamp' => array('<', time() - $time_to_leave_alone)
364
+				),
365
+				'order_by' => ['TXN_timestamp' => 'DESC'],
366
+				'limit' => 1000
367
+			),
368
+			$time_to_leave_alone
369
+		);
370
+
371
+
372
+		/**
373
+		 * This filter is for when code needs to filter the list of transaction ids that represent transactions
374
+		 * about to be deleted based on some other criteria that isn't easily done via the query args filter.
375
+		 */
376
+		$txn_ids = apply_filters(
377
+			'FHEE__EEM_Transaction__delete_junk_transactions__transaction_ids_to_delete',
378
+			EEM_Transaction::instance()->get_col($ids_query, 'TXN_ID'),
379
+			$time_to_leave_alone
380
+		);
381
+		// now that we have the ids to delete
382
+		if (! empty($txn_ids) && is_array($txn_ids)) {
383
+			// first, make sure these TXN's are removed the "ee_locked_transactions" array
384
+			EEM_Transaction::unset_locked_transactions($txn_ids);
385
+
386
+			// Create IDs placeholder.
387
+			$placeholders = array_fill(0, count($txn_ids), '%d');
388 388
             
389
-            // Glue it together to use inside $wpdb->prepare.
390
-            $format = implode(', ', $placeholders);
391
-
392
-            // let's get deletin'...
393
-            // We got the ids from the original query to get them FROM
394
-            // the db (which is sanitized) so no need to prepare them again.
395
-            $query   = $wpdb->prepare("DELETE FROM " . $this->table() . " WHERE TXN_ID IN ( $format )", $txn_ids);
396
-            $deleted = $wpdb->query($query);
397
-        }
398
-        if ($deleted) {
399
-            /**
400
-             * Allows code to do something after the transactions have been deleted.
401
-             */
402
-            do_action('AHEE__EEM_Transaction__delete_junk_transactions__successful_deletion', $txn_ids);
403
-        }
404
-
405
-        return $deleted;
406
-    }
407
-
408
-
409
-    /**
410
-     * @param array $transaction_IDs
411
-     *
412
-     * @return bool
413
-     */
414
-    public static function unset_locked_transactions(array $transaction_IDs)
415
-    {
416
-        $locked_transactions = get_option('ee_locked_transactions', array());
417
-        $update              = false;
418
-        foreach ($transaction_IDs as $TXN_ID) {
419
-            if (isset($locked_transactions[ $TXN_ID ])) {
420
-                unset($locked_transactions[ $TXN_ID ]);
421
-                $update = true;
422
-            }
423
-        }
424
-        if ($update) {
425
-            update_option('ee_locked_transactions', $locked_transactions);
426
-        }
427
-
428
-        return $update;
429
-    }
430
-
431
-
432
-
433
-    /**
434
-     * returns an array of EE_Transaction objects whose timestamp is greater than
435
-     * the current time minus the session lifespan, which defaults to 60 minutes
436
-     *
437
-     * @return EE_Base_Class[]|EE_Transaction[]
438
-     * @throws EE_Error
439
-     * @throws InvalidArgumentException
440
-     * @throws InvalidDataTypeException
441
-     * @throws InvalidInterfaceException
442
-     */
443
-    public function get_transactions_in_progress()
444
-    {
445
-        return $this->_get_transactions_in_progress();
446
-    }
447
-
448
-
449
-
450
-    /**
451
-     * returns an array of EE_Transaction objects whose timestamp is less than
452
-     * the current time minus the session lifespan, which defaults to 60 minutes
453
-     *
454
-     * @return EE_Base_Class[]|EE_Transaction[]
455
-     * @throws EE_Error
456
-     * @throws InvalidArgumentException
457
-     * @throws InvalidDataTypeException
458
-     * @throws InvalidInterfaceException
459
-     */
460
-    public function get_transactions_not_in_progress()
461
-    {
462
-        return $this->_get_transactions_in_progress('<=');
463
-    }
464
-
465
-
466
-
467
-    /**
468
-     * @param string $comparison
469
-     * @return EE_Base_Class[]|EE_Transaction[]
470
-     * @throws EE_Error
471
-     * @throws InvalidArgumentException
472
-     * @throws InvalidDataTypeException
473
-     * @throws InvalidInterfaceException
474
-     */
475
-    private function _get_transactions_in_progress($comparison = '>=')
476
-    {
477
-        $comparison = $comparison === '>=' || $comparison === '<='
478
-            ? $comparison
479
-            : '>=';
480
-        /** @var EventEspresso\core\domain\values\session\SessionLifespan $session_lifespan */
481
-        $session_lifespan = LoaderFactory::getLoader()->getShared(
482
-            'EventEspresso\core\domain\values\session\SessionLifespan'
483
-        );
484
-        return $this->get_all(
485
-            array(
486
-                array(
487
-                    'TXN_timestamp' => array(
488
-                        $comparison,
489
-                        $session_lifespan->expiration()
490
-                    ),
491
-                    'STS_ID' => array(
492
-                        '!=',
493
-                        EEM_Transaction::complete_status_code
494
-                    ),
495
-                )
496
-            )
497
-        );
498
-    }
389
+			// Glue it together to use inside $wpdb->prepare.
390
+			$format = implode(', ', $placeholders);
391
+
392
+			// let's get deletin'...
393
+			// We got the ids from the original query to get them FROM
394
+			// the db (which is sanitized) so no need to prepare them again.
395
+			$query   = $wpdb->prepare("DELETE FROM " . $this->table() . " WHERE TXN_ID IN ( $format )", $txn_ids);
396
+			$deleted = $wpdb->query($query);
397
+		}
398
+		if ($deleted) {
399
+			/**
400
+			 * Allows code to do something after the transactions have been deleted.
401
+			 */
402
+			do_action('AHEE__EEM_Transaction__delete_junk_transactions__successful_deletion', $txn_ids);
403
+		}
404
+
405
+		return $deleted;
406
+	}
407
+
408
+
409
+	/**
410
+	 * @param array $transaction_IDs
411
+	 *
412
+	 * @return bool
413
+	 */
414
+	public static function unset_locked_transactions(array $transaction_IDs)
415
+	{
416
+		$locked_transactions = get_option('ee_locked_transactions', array());
417
+		$update              = false;
418
+		foreach ($transaction_IDs as $TXN_ID) {
419
+			if (isset($locked_transactions[ $TXN_ID ])) {
420
+				unset($locked_transactions[ $TXN_ID ]);
421
+				$update = true;
422
+			}
423
+		}
424
+		if ($update) {
425
+			update_option('ee_locked_transactions', $locked_transactions);
426
+		}
427
+
428
+		return $update;
429
+	}
430
+
431
+
432
+
433
+	/**
434
+	 * returns an array of EE_Transaction objects whose timestamp is greater than
435
+	 * the current time minus the session lifespan, which defaults to 60 minutes
436
+	 *
437
+	 * @return EE_Base_Class[]|EE_Transaction[]
438
+	 * @throws EE_Error
439
+	 * @throws InvalidArgumentException
440
+	 * @throws InvalidDataTypeException
441
+	 * @throws InvalidInterfaceException
442
+	 */
443
+	public function get_transactions_in_progress()
444
+	{
445
+		return $this->_get_transactions_in_progress();
446
+	}
447
+
448
+
449
+
450
+	/**
451
+	 * returns an array of EE_Transaction objects whose timestamp is less than
452
+	 * the current time minus the session lifespan, which defaults to 60 minutes
453
+	 *
454
+	 * @return EE_Base_Class[]|EE_Transaction[]
455
+	 * @throws EE_Error
456
+	 * @throws InvalidArgumentException
457
+	 * @throws InvalidDataTypeException
458
+	 * @throws InvalidInterfaceException
459
+	 */
460
+	public function get_transactions_not_in_progress()
461
+	{
462
+		return $this->_get_transactions_in_progress('<=');
463
+	}
464
+
465
+
466
+
467
+	/**
468
+	 * @param string $comparison
469
+	 * @return EE_Base_Class[]|EE_Transaction[]
470
+	 * @throws EE_Error
471
+	 * @throws InvalidArgumentException
472
+	 * @throws InvalidDataTypeException
473
+	 * @throws InvalidInterfaceException
474
+	 */
475
+	private function _get_transactions_in_progress($comparison = '>=')
476
+	{
477
+		$comparison = $comparison === '>=' || $comparison === '<='
478
+			? $comparison
479
+			: '>=';
480
+		/** @var EventEspresso\core\domain\values\session\SessionLifespan $session_lifespan */
481
+		$session_lifespan = LoaderFactory::getLoader()->getShared(
482
+			'EventEspresso\core\domain\values\session\SessionLifespan'
483
+		);
484
+		return $this->get_all(
485
+			array(
486
+				array(
487
+					'TXN_timestamp' => array(
488
+						$comparison,
489
+						$session_lifespan->expiration()
490
+					),
491
+					'STS_ID' => array(
492
+						'!=',
493
+						EEM_Transaction::complete_status_code
494
+					),
495
+				)
496
+			)
497
+		);
498
+	}
499 499
 }
Please login to merge, or discard this patch.