1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/** |
3
|
|
|
* Payment Actions |
4
|
|
|
* |
5
|
|
|
* @package Give |
6
|
|
|
* @subpackage Payments |
7
|
|
|
* @copyright Copyright (c) 2016, WordImpress |
8
|
|
|
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
9
|
|
|
* @since 1.0 |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
// Exit if accessed directly |
13
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
14
|
|
|
exit; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Complete a purchase aka donation |
19
|
|
|
* |
20
|
|
|
* Performs all necessary actions to complete a purchase. |
21
|
|
|
* Triggered by the give_update_payment_status() function. |
22
|
|
|
* |
23
|
|
|
* @since 1.0 |
24
|
|
|
* |
25
|
|
|
* @param int $payment_id The ID number of the payment. |
26
|
|
|
* @param string $new_status The status of the payment, probably "publish". |
27
|
|
|
* @param string $old_status The status of the payment prior to being marked as "complete", probably "pending". |
28
|
|
|
* |
29
|
|
|
* @return void |
30
|
|
|
*/ |
31
|
|
|
function give_complete_purchase( $payment_id, $new_status, $old_status ) { |
32
|
|
|
|
33
|
|
|
// Make sure that payments are only completed once |
34
|
42 |
|
if ( $old_status == 'publish' || $old_status == 'complete' ) { |
35
|
6 |
|
return; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
// Make sure the payment completion is only processed when new status is complete |
39
|
42 |
|
if ( $new_status != 'publish' && $new_status != 'complete' ) { |
40
|
1 |
|
return; |
41
|
|
|
} |
42
|
|
|
|
43
|
42 |
|
$payment = new Give_Payment( $payment_id ); |
44
|
|
|
|
45
|
42 |
|
$creation_date = get_post_field( 'post_date', $payment_id, 'raw' ); |
46
|
42 |
|
$payment_meta = $payment->payment_meta; |
|
|
|
|
47
|
42 |
|
$completed_date = $payment->completed_date; |
|
|
|
|
48
|
42 |
|
$user_info = $payment->user_info; |
|
|
|
|
49
|
42 |
|
$customer_id = $payment->customer_id; |
|
|
|
|
50
|
42 |
|
$amount = $payment->total; |
|
|
|
|
51
|
42 |
|
$price_id = $payment->price_id; |
|
|
|
|
52
|
42 |
|
$form_id = $payment->form_id; |
|
|
|
|
53
|
|
|
|
54
|
42 |
|
do_action( 'give_pre_complete_purchase', $payment_id ); |
55
|
|
|
|
56
|
|
|
// Ensure these actions only run once, ever |
57
|
42 |
|
if ( empty( $completed_date ) ) { |
58
|
|
|
|
59
|
42 |
|
give_record_sale_in_log( $form_id, $payment_id, $price_id, $creation_date ); |
60
|
42 |
|
do_action( 'give_complete_form_donation', $form_id, $payment_id, $payment_meta ); |
61
|
|
|
|
62
|
42 |
|
} |
63
|
|
|
|
64
|
|
|
// Increase the earnings for this form ID |
65
|
42 |
|
give_increase_earnings( $form_id, $amount ); |
66
|
42 |
|
give_increase_purchase_count( $form_id ); |
67
|
|
|
|
68
|
|
|
// Clear the total earnings cache |
69
|
42 |
|
delete_transient( 'give_earnings_total' ); |
70
|
|
|
// Clear the This Month earnings (this_monththis_month is NOT a typo) |
71
|
42 |
|
delete_transient( md5( 'give_earnings_this_monththis_month' ) ); |
72
|
42 |
|
delete_transient( md5( 'give_earnings_todaytoday' ) ); |
73
|
|
|
|
74
|
|
|
// Increase the donor's purchase stats |
75
|
42 |
|
$customer = new Give_Customer( $customer_id ); |
76
|
42 |
|
$customer->increase_purchase_count(); |
77
|
42 |
|
$customer->increase_value( $amount ); |
78
|
|
|
|
79
|
42 |
|
give_increase_total_earnings( $amount ); |
80
|
|
|
|
81
|
|
|
// Ensure this action only runs once ever |
82
|
42 |
|
if ( empty( $completed_date ) ) { |
83
|
|
|
|
84
|
|
|
// Save the completed date |
85
|
42 |
|
$payment->completed_date = current_time( 'mysql' ); |
|
|
|
|
86
|
42 |
|
$payment->save(); |
87
|
42 |
|
|
88
|
42 |
|
/** |
89
|
|
|
* Fires after a purchase/donation successfully complete. |
90
|
42 |
|
* |
91
|
|
|
* @since 1.6 |
92
|
|
|
* |
93
|
|
|
* @param int $payment_id The ID of the payment. |
94
|
|
|
*/ |
95
|
|
|
do_action( 'give_complete_purchase', $payment_id ); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
add_action( 'give_update_payment_status', 'give_complete_purchase', 100, 3 ); |
101
|
|
|
|
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Record payment status change |
105
|
|
|
* |
106
|
|
|
* @since 1.0 |
107
|
|
|
* |
108
|
|
|
* @param int $payment_id The ID number of the payment. |
109
|
42 |
|
* @param string $new_status The status of the payment, probably "publish". |
110
|
42 |
|
* @param string $old_status The status of the payment prior to being marked as "complete", probably "pending". |
111
|
42 |
|
* |
112
|
|
|
* @return void |
113
|
42 |
|
*/ |
114
|
|
|
function give_record_status_change( $payment_id, $new_status, $old_status ) { |
115
|
42 |
|
|
116
|
42 |
|
// Get the list of statuses so that status in the payment note can be translated |
117
|
|
|
$stati = give_get_payment_statuses(); |
118
|
42 |
|
$old_status = isset( $stati[ $old_status ] ) ? $stati[ $old_status ] : $old_status; |
119
|
|
|
$new_status = isset( $stati[ $new_status ] ) ? $stati[ $new_status ] : $new_status; |
120
|
42 |
|
|
121
|
42 |
|
$status_change = sprintf( |
122
|
|
|
/* translators: 1: old status 2: new status */ |
123
|
|
|
esc_html__( 'Status changed from %1$s to %2$s.', 'give' ), |
124
|
|
|
$old_status, |
125
|
|
|
$new_status |
126
|
|
|
); |
127
|
|
|
|
128
|
|
|
give_insert_payment_note( $payment_id, $status_change ); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
add_action( 'give_update_payment_status', 'give_record_status_change', 100, 3 ); |
132
|
|
|
|
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Clear User History Cache |
136
|
|
|
* |
137
|
|
|
* Flushes the current user's purchase history transient when a payment status |
138
|
42 |
|
* is updated. |
139
|
|
|
* |
140
|
42 |
|
* @since 1.0 |
141
|
41 |
|
* |
142
|
41 |
|
* @param int $payment_id The ID number of the payment. |
143
|
|
|
* @param string $new_status The status of the payment, probably "publish". |
144
|
42 |
|
* @param string $old_status The status of the payment prior to being marked as "complete", probably "pending". |
145
|
|
|
* |
146
|
|
|
* @return void |
147
|
|
|
*/ |
148
|
|
|
function give_clear_user_history_cache( $payment_id, $new_status, $old_status ) { |
|
|
|
|
149
|
|
|
|
150
|
|
|
$payment = new Give_Payment( $payment_id ); |
151
|
|
|
|
152
|
|
|
if ( ! empty( $payment->user_id ) ) { |
|
|
|
|
153
|
|
|
delete_transient( 'give_user_' . $payment->user_id . '_purchases' ); |
|
|
|
|
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
add_action( 'give_update_payment_status', 'give_clear_user_history_cache', 10, 3 ); |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Update Old Payments Totals |
162
|
|
|
* |
163
|
|
|
* Updates all old payments, prior to 1.2, with new meta for the total purchase amount. |
164
|
|
|
* |
165
|
|
|
* It's done to query payments by their totals. |
166
|
|
|
* |
167
|
|
|
* @since 1.0 |
168
|
|
|
* |
169
|
|
|
* @param array $data Arguments passed. |
170
|
|
|
* |
171
|
|
|
* @return void |
172
|
|
|
*/ |
173
|
|
|
function give_update_old_payments_with_totals( $data ) { |
174
|
|
|
if ( ! wp_verify_nonce( $data['_wpnonce'], 'give_upgrade_payments_nonce' ) ) { |
175
|
|
|
return; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
if ( get_option( 'give_payment_totals_upgraded' ) ) { |
179
|
|
|
return; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
$payments = give_get_payments( array( |
183
|
|
|
'offset' => 0, |
184
|
|
|
'number' => - 1, |
185
|
|
|
'mode' => 'all' |
186
|
|
|
) ); |
187
|
|
|
|
188
|
|
|
if ( $payments ) { |
189
|
|
|
foreach ( $payments as $payment ) { |
190
|
|
|
|
191
|
|
|
$payment = new Give_Payment( $payment->ID ); |
192
|
|
|
$meta = $payment->get_meta(); |
193
|
|
|
|
194
|
|
|
$payment->total = $meta['amount']; |
|
|
|
|
195
|
|
|
$payment->save(); |
196
|
|
|
|
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
add_option( 'give_payment_totals_upgraded', 1 ); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
add_action( 'give_upgrade_payments', 'give_update_old_payments_with_totals' ); |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Mark Abandoned Donations |
207
|
|
|
* |
208
|
|
|
* Updates over a week-old 'pending' donations to 'abandoned' status. |
209
|
|
|
* |
210
|
|
|
* @since 1.0 |
211
|
|
|
* |
212
|
|
|
* @return void |
213
|
|
|
*/ |
214
|
|
|
function give_mark_abandoned_donations() { |
215
|
|
|
$args = array( |
216
|
|
|
'status' => 'pending', |
217
|
|
|
'number' => - 1, |
218
|
|
|
'output' => 'give_payments', |
219
|
|
|
); |
220
|
|
|
|
221
|
|
|
add_filter( 'posts_where', 'give_filter_where_older_than_week' ); |
222
|
|
|
|
223
|
|
|
$payments = give_get_payments( $args ); |
224
|
|
|
|
225
|
|
|
remove_filter( 'posts_where', 'give_filter_where_older_than_week' ); |
226
|
|
|
|
227
|
|
|
if ( $payments ) { |
228
|
|
|
/** |
229
|
|
|
* Filter payment gateways: Used to set payment gateways which can be skip while transferring pending payment to abandon. |
230
|
|
|
* |
231
|
|
|
* @since 1.6 |
232
|
|
|
* |
233
|
|
|
* @param array $skip_payment_gateways Array of payment gateways |
234
|
|
|
*/ |
235
|
|
|
$skip_payment_gateways = apply_filters( 'give_mark_abandoned_donation_gateways', array( 'offline' ) ); |
236
|
|
|
|
237
|
|
|
foreach ( $payments as $payment ) { |
238
|
|
|
$gateway = give_get_payment_gateway( $payment ); |
239
|
|
|
|
240
|
|
|
// Skip payment gateways. |
241
|
|
|
if ( in_array( $gateway, $skip_payment_gateways ) ) { |
242
|
|
|
continue; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
$payment->status = 'abandoned'; |
246
|
|
|
$payment->save(); |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
add_action( 'give_weekly_scheduled_events', 'give_mark_abandoned_donations' ); |
252
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.