|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
/** |
|
3
|
|
|
* Recount all donor stats. |
|
4
|
|
|
* |
|
5
|
|
|
* This class handles batch processing of recounting all donor stats. |
|
6
|
|
|
* |
|
7
|
|
|
* @subpackage Admin/Tools/Give_Tools_Recount_Donor_Stats |
|
8
|
|
|
* @license https://opensource.org/licenses/gpl-license GNU Public License |
|
9
|
|
|
* @since 1.5 |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
// Exit if accessed directly. |
|
13
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
|
14
|
|
|
exit; |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Give_Tools_Recount_Donor_Stats Class |
|
19
|
|
|
* |
|
20
|
|
|
* @since 1.5 |
|
21
|
|
|
*/ |
|
22
|
|
|
class Give_Tools_Recount_Donor_Stats extends Give_Batch_Export { |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Our export type. Used for export-type specific filters/actions. |
|
26
|
|
|
* |
|
27
|
|
|
* @var string |
|
28
|
|
|
* @since 1.5 |
|
29
|
|
|
*/ |
|
30
|
|
|
public $export_type = ''; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Allows for a non-form batch processing to be run. |
|
34
|
|
|
* |
|
35
|
|
|
* @since 1.5 |
|
36
|
|
|
* @var boolean |
|
37
|
|
|
*/ |
|
38
|
|
|
public $is_void = true; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Sets the number of items to pull on each step |
|
42
|
|
|
* |
|
43
|
|
|
* @since 1.5 |
|
44
|
|
|
* @var integer |
|
45
|
|
|
*/ |
|
46
|
|
|
public $per_step = 5; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Get the Export Data |
|
50
|
|
|
* |
|
51
|
|
|
* @access public |
|
52
|
|
|
* @since 1.5 |
|
53
|
|
|
* |
|
54
|
|
|
* @return array $data The data for the CSV file |
|
|
|
|
|
|
55
|
|
|
*/ |
|
56
|
|
|
public function get_data() { |
|
57
|
|
|
|
|
58
|
|
|
$args = array( |
|
59
|
|
|
'number' => $this->per_step, |
|
60
|
|
|
'offset' => $this->per_step * ( $this->step - 1 ), |
|
61
|
|
|
'orderby' => 'id', |
|
62
|
|
|
'order' => 'DESC', |
|
63
|
|
|
); |
|
64
|
|
|
|
|
65
|
|
|
$donors = Give()->donors->get_donors( $args ); |
|
66
|
|
|
|
|
67
|
|
|
if ( $donors ) { |
|
68
|
|
|
|
|
69
|
|
|
$allowed_payment_status = apply_filters( 'give_recount_donors_donation_statuses', give_get_payment_status_keys() ); |
|
70
|
|
|
|
|
71
|
|
|
foreach ( $donors as $donor ) { |
|
72
|
|
|
|
|
73
|
|
|
$attached_payment_ids = explode( ',', $donor->payment_ids ); |
|
74
|
|
|
|
|
75
|
|
|
$attached_args = array( |
|
76
|
|
|
'post__in' => $attached_payment_ids, |
|
77
|
|
|
'number' => - 1, |
|
78
|
|
|
'status' => $allowed_payment_status, |
|
79
|
|
|
); |
|
80
|
|
|
|
|
81
|
|
|
$attached_payments = (array) give_get_payments( $attached_args ); |
|
82
|
|
|
|
|
83
|
|
|
$unattached_args = array( |
|
84
|
|
|
'post__not_in' => $attached_payment_ids, |
|
85
|
|
|
'number' => - 1, |
|
86
|
|
|
'status' => $allowed_payment_status, |
|
87
|
|
|
'meta_query' => array( |
|
88
|
|
|
array( |
|
89
|
|
|
'key' => '_give_payment_user_email', |
|
90
|
|
|
'value' => $donor->email, |
|
91
|
|
|
'compare' => '=', |
|
92
|
|
|
), |
|
93
|
|
|
), |
|
94
|
|
|
); |
|
95
|
|
|
|
|
96
|
|
|
$unattached_payments = give_get_payments( $unattached_args ); |
|
97
|
|
|
|
|
98
|
|
|
$payments = array_merge( $attached_payments, $unattached_payments ); |
|
99
|
|
|
|
|
100
|
|
|
$purchase_value = 0.00; |
|
101
|
|
|
$purchase_count = 0; |
|
102
|
|
|
$payment_ids = array(); |
|
103
|
|
|
|
|
104
|
|
|
if ( $payments ) { |
|
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
foreach ( $payments as $payment ) { |
|
107
|
|
|
|
|
108
|
|
|
$should_process_payment = 'publish' == $payment->post_status ? true : false; |
|
109
|
|
|
$should_process_payment = apply_filters( 'give_donor_recount_should_process_donation', $should_process_payment, $payment ); |
|
110
|
|
|
|
|
111
|
|
|
if ( true === $should_process_payment ) { |
|
112
|
|
|
|
|
113
|
|
|
if ( apply_filters( 'give_donor_recount_should_increase_value', true, $payment ) ) { |
|
114
|
|
|
$purchase_value += give_get_payment_amount( $payment->ID ); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
if ( apply_filters( 'give_donor_recount_should_increase_count', true, $payment ) ) { |
|
118
|
|
|
$purchase_count ++; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
$payment_ids[] = $payment->ID; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
$payment_ids = implode( ',', $payment_ids ); |
|
127
|
|
|
|
|
128
|
|
|
$donor_update_data = array( |
|
129
|
|
|
'purchase_count' => $purchase_count, |
|
130
|
|
|
'purchase_value' => $purchase_value, |
|
131
|
|
|
'payment_ids' => $payment_ids, |
|
132
|
|
|
); |
|
133
|
|
|
|
|
134
|
|
|
$donor_instance = new Give_Donor( $donor->id ); |
|
135
|
|
|
$donor_instance->update( $donor_update_data ); |
|
136
|
|
|
|
|
137
|
|
|
}// End foreach(). |
|
|
|
|
|
|
138
|
|
|
|
|
139
|
|
|
return true; |
|
140
|
|
|
}// End if(). |
|
|
|
|
|
|
141
|
|
|
|
|
142
|
|
|
return false; |
|
143
|
|
|
|
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Return the calculated completion percentage |
|
148
|
|
|
* |
|
149
|
|
|
* @since 1.5 |
|
150
|
|
|
* @return int |
|
|
|
|
|
|
151
|
|
|
*/ |
|
152
|
|
|
public function get_percentage_complete() { |
|
153
|
|
|
|
|
154
|
|
|
$args = array( |
|
155
|
|
|
'number' => - 1, |
|
156
|
|
|
'orderby' => 'id', |
|
157
|
|
|
'order' => 'DESC', |
|
158
|
|
|
); |
|
159
|
|
|
|
|
160
|
|
|
$donors = Give()->donors->get_donors( $args ); |
|
161
|
|
|
$total = count( $donors ); |
|
162
|
|
|
|
|
163
|
|
|
$percentage = 100; |
|
164
|
|
|
|
|
165
|
|
|
if ( $total > 0 ) { |
|
166
|
|
|
$percentage = ( ( $this->per_step * $this->step ) / $total ) * 100; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
if ( $percentage > 100 ) { |
|
170
|
|
|
$percentage = 100; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
return $percentage; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Set the properties specific to the payments export |
|
178
|
|
|
* |
|
179
|
|
|
* @since 1.5 |
|
180
|
|
|
* |
|
181
|
|
|
* @param array $request The Form Data passed into the batch processing |
|
182
|
|
|
*/ |
|
183
|
|
|
public function set_properties( $request ) { |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Process a step |
|
188
|
|
|
* |
|
189
|
|
|
* @since 1.5 |
|
190
|
|
|
* @return bool |
|
191
|
|
|
*/ |
|
192
|
|
|
public function process_step() { |
|
193
|
|
|
|
|
194
|
|
|
if ( ! $this->can_export() ) { |
|
195
|
|
|
wp_die( esc_html__( 'You do not have permission to recount stats.', 'give' ), esc_html__( 'Error', 'give' ), array( |
|
196
|
|
|
'response' => 403, |
|
197
|
|
|
) ); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
$had_data = $this->get_data(); |
|
201
|
|
|
|
|
202
|
|
|
if ( $had_data ) { |
|
203
|
|
|
$this->done = false; |
|
204
|
|
|
|
|
205
|
|
|
return true; |
|
206
|
|
|
} else { |
|
207
|
|
|
$this->done = true; |
|
208
|
|
|
$this->message = esc_html__( 'Donor stats have been successfully recounted.', 'give' ); |
|
|
|
|
|
|
209
|
|
|
|
|
210
|
|
|
return false; |
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* Headers |
|
216
|
|
|
*/ |
|
217
|
|
|
public function headers() { |
|
218
|
|
|
ignore_user_abort( true ); |
|
219
|
|
|
|
|
220
|
|
|
if ( ! give_is_func_disabled( 'set_time_limit' ) && ! ini_get( 'safe_mode' ) ) { |
|
221
|
|
|
set_time_limit( 0 ); |
|
222
|
|
|
} |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* Perform the export |
|
227
|
|
|
* |
|
228
|
|
|
* @access public |
|
229
|
|
|
* @since 1.5 |
|
230
|
|
|
* @return void |
|
231
|
|
|
*/ |
|
232
|
|
|
public function export() { |
|
233
|
|
|
|
|
234
|
|
|
// Set headers |
|
235
|
|
|
$this->headers(); |
|
236
|
|
|
|
|
237
|
|
|
give_die(); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
} |
|
241
|
|
|
|
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.