1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Delete Donors. |
4
|
|
|
* |
5
|
|
|
* This class handles batch processing of deleting donor data. |
6
|
|
|
* |
7
|
|
|
* @subpackage Admin/Tools/Give_Tools_Delete_Donors |
8
|
|
|
* @copyright Copyright (c) 2016, WordImpress |
9
|
|
|
* @license https://opensource.org/licenses/gpl-license GNU Public License |
10
|
|
|
* @since 1.8.12 |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
// Exit if accessed directly. |
14
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
15
|
|
|
exit; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Give_Tools_Delete_Donors Class |
21
|
|
|
* |
22
|
|
|
* @since 1.8.12 |
23
|
|
|
*/ |
24
|
|
|
class Give_Tools_Delete_Donors extends Give_Batch_Export { |
25
|
|
|
|
26
|
|
|
var $request; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Used to store donation id's that are going to get deleted. |
30
|
|
|
* @var string |
31
|
|
|
* @since 1.8.12 |
32
|
|
|
*/ |
33
|
|
|
var $donation_key = 'give_temp_delete_donation_ids'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Used to store donors id's that are going to get deleted. |
37
|
|
|
* @var string |
38
|
|
|
* @since 1.8.12 |
39
|
|
|
*/ |
40
|
|
|
var $donor_key = 'give_temp_delete_donor_ids'; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Used to store the step where the step will be. ( 'count', 'donations', 'donors' ). |
44
|
|
|
* @var string |
45
|
|
|
* @since 1.8.12 |
46
|
|
|
*/ |
47
|
|
|
var $step_key = 'give_temp_delete_step'; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Used to store to get the page count in the loop. |
51
|
|
|
* @var string |
52
|
|
|
* @since 1.8.12 |
53
|
|
|
*/ |
54
|
|
|
var $step_on_key = 'give_temp_delete_step_on'; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Contain total number of step . |
58
|
|
|
* @var string |
59
|
|
|
* @since 1.8.12 |
60
|
|
|
*/ |
61
|
|
|
var $total_step; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Counting contain total number of step that completed. |
65
|
|
|
* @var int |
66
|
|
|
* @since 1.8.12 |
67
|
|
|
*/ |
68
|
|
|
var $step_completed; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Our export type. Used for export-type specific filters/actions |
72
|
|
|
* @var string |
73
|
|
|
* @since 1.8.12 |
74
|
|
|
*/ |
75
|
|
|
public $export_type = ''; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Allows for a non-form batch processing to be run. |
79
|
|
|
* @since 1.8.12 |
80
|
|
|
* @var boolean |
81
|
|
|
*/ |
82
|
|
|
public $is_void = true; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Sets the number of items to pull on each step |
86
|
|
|
* @since 1.8.12 |
87
|
|
|
* @var int |
88
|
|
|
*/ |
89
|
|
|
public $per_step = 10; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Set's all the donors id's |
93
|
|
|
* @since 1.8.12 |
94
|
|
|
* @var array |
95
|
|
|
*/ |
96
|
|
|
public $donor_ids = array(); |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Constructor. |
100
|
|
|
*/ |
101
|
|
|
public function __construct( $_step = 1 ) { |
102
|
|
|
parent::__construct( $_step ); |
103
|
|
|
|
104
|
|
|
$this->is_writable = true; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get the Export Data |
109
|
|
|
* |
110
|
|
|
* @access public |
111
|
|
|
* @since 1.8.12 |
112
|
|
|
* @global object $wpdb Used to query the database using the WordPress Database API |
113
|
|
|
* |
114
|
|
|
* @return array|bool $data The data for the CSV file |
115
|
|
|
*/ |
116
|
|
View Code Duplication |
public function pre_fetch() { |
|
|
|
|
117
|
|
|
$donation_ids = array(); |
118
|
|
|
$donor_ids = array(); |
119
|
|
|
|
120
|
|
|
// Check if the ajax request if running for the first time. |
121
|
|
|
if ( 1 === (int) $this->step ) { |
122
|
|
|
// Delete all the donation ids. |
123
|
|
|
$this->delete_option( $this->donation_key ); |
124
|
|
|
// Delete all the donor ids. |
125
|
|
|
$this->delete_option( $this->donor_key ); |
126
|
|
|
|
127
|
|
|
// Delete all the step and set to 'count' which if the first step in the process of deleting the donors. |
128
|
|
|
$this->update_option( $this->step_key, 'count' ); |
129
|
|
|
|
130
|
|
|
// Delete tha page count of the step. |
131
|
|
|
$this->update_option( $this->step_on_key, '0' ); |
132
|
|
|
} else { |
133
|
|
|
// Get the old donors list. |
134
|
|
|
$donor_ids = $this->get_option( $this->donor_key ); |
135
|
|
|
|
136
|
|
|
// Get the old donation list. |
137
|
|
|
$donation_ids = $this->get_option( $this->donation_key ); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
// Get the step and check for it if it's on the first step( 'count' ) or not. |
141
|
|
|
$step = (int) $this->get_step(); |
142
|
|
|
if ( 1 === $step ) { |
143
|
|
|
/** |
144
|
|
|
* Will add or update the donation and donor data by running wp query. |
145
|
|
|
*/ |
146
|
|
|
$this->count( $step, $donation_ids, $donor_ids ); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Will Update or Add the donation and donors ids in the with option table for there respected key. |
152
|
|
|
* |
153
|
|
|
* @param string $step On which the current ajax is running. |
154
|
|
|
* @param array $donation_ids Contain the list of all the donation id's that has being add before this |
155
|
|
|
* @param array $donor_ids Contain the list of all the donors id's that has being add before this |
156
|
|
|
*/ |
157
|
|
|
private function count( $step, $donation_ids = array(), $donor_ids = array() ) { |
|
|
|
|
158
|
|
|
|
159
|
|
|
// Get the Page count by default it's zero. |
160
|
|
|
$paged = (int) $this->get_step_page(); |
161
|
|
|
// Incresed the page count by one. |
162
|
|
|
++ $paged; |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Filter add to alter the argument before the wp quest run |
166
|
|
|
*/ |
167
|
|
|
$args = apply_filters( 'give_tools_reset_stats_total_args', array( |
168
|
|
|
'post_type' => 'give_payment', |
169
|
|
|
'post_status' => 'any', |
170
|
|
|
'posts_per_page' => $this->per_step, |
171
|
|
|
'paged' => $paged, |
172
|
|
|
// ONLY TEST MODE TRANSACTIONS!!! |
173
|
|
|
'meta_key' => '_give_payment_mode', |
|
|
|
|
174
|
|
|
'meta_value' => 'test', |
|
|
|
|
175
|
|
|
) ); |
176
|
|
|
|
177
|
|
|
// Reset the post data. |
178
|
|
|
wp_reset_postdata(); |
179
|
|
|
// Getting the new donation. |
180
|
|
|
$donation_posts = new WP_Query( $args ); |
181
|
|
|
|
182
|
|
|
// The Loop. |
183
|
|
|
if ( $donation_posts->have_posts() ) { |
184
|
|
|
while ( $donation_posts->have_posts() ) { |
185
|
|
|
$donation_posts->the_post(); |
186
|
|
|
global $post; |
187
|
|
|
// Add the donation id in side the array. |
188
|
|
|
$donation_ids[] = $post->ID; |
189
|
|
|
|
190
|
|
|
// Add the donor id in side the array. |
191
|
|
|
$donor_ids[] = (int) $post->post_author; |
192
|
|
|
} |
193
|
|
|
/* Restore original Post Data */ |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
// Get the total number of post found. |
197
|
|
|
$total_donation = (int) $donation_posts->found_posts; |
198
|
|
|
|
199
|
|
|
// Maximum number of page can be display |
200
|
|
|
$max_num_pages = (int) $donation_posts->max_num_pages; |
201
|
|
|
|
202
|
|
|
// Check current page is less then max number of page or not |
203
|
|
View Code Duplication |
if ( $paged < $max_num_pages ) { |
|
|
|
|
204
|
|
|
// Update the curretn page virable for the next step |
205
|
|
|
$this->update_option( $this->step_on_key, $paged ); |
206
|
|
|
|
207
|
|
|
// Calculating percentage. |
208
|
|
|
$page_remain = $max_num_pages - $paged; |
209
|
|
|
$this->total_step = (int) $max_num_pages + ( $total_donation / $this->per_step ) + ( ( $page_remain * 2 ) * count( $donor_ids ) ); |
|
|
|
|
210
|
|
|
$this->step_completed = $paged; |
211
|
|
|
} else { |
212
|
|
|
$donation_ids_count = count( $donor_ids ); |
|
|
|
|
213
|
|
|
$this->update_option( $this->step_key, 'donation' ); |
214
|
|
|
$this->update_option( $this->step_on_key, '0' ); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
$donor_ids = array_unique( $donor_ids ); |
218
|
|
|
$this->update_option( $this->donor_key, $donor_ids ); |
219
|
|
|
$this->update_option( $this->donation_key, $donation_ids ); |
220
|
|
|
|
221
|
|
|
wp_reset_postdata(); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Return the calculated completion percentage. |
226
|
|
|
* |
227
|
|
|
* @since 1.8.12 |
228
|
|
|
* @return int |
229
|
|
|
*/ |
230
|
|
|
public function get_percentage_complete() { |
231
|
|
|
return ceil( ( 100 * $this->step_completed ) / $this->total_step ); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
View Code Duplication |
public function process_step() { |
|
|
|
|
235
|
|
|
|
236
|
|
|
if ( ! $this->can_export() ) { |
237
|
|
|
wp_die( __( 'You do not have permission to delete test transactions.', 'give' ), __( 'Error', 'give' ), array( 'response' => 403 ) ); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
$had_data = $this->get_data(); |
241
|
|
|
|
242
|
|
|
if ( $had_data ) { |
243
|
|
|
$this->done = false; |
244
|
|
|
|
245
|
|
|
return true; |
246
|
|
|
} else { |
247
|
|
|
update_option( 'give_earnings_total', give_get_total_earnings( true ) ); |
248
|
|
|
Give_Cache::delete( Give_Cache::get_key( 'give_estimated_monthly_stats' ) ); |
249
|
|
|
|
250
|
|
|
$this->delete_option( $this->donation_key ); |
251
|
|
|
|
252
|
|
|
// Reset the sequential order numbers |
253
|
|
|
if ( give_get_option( 'enable_sequential' ) ) { |
254
|
|
|
delete_option( 'give_last_payment_number' ); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
$this->done = true; |
258
|
|
|
$this->message = __( 'Test donor and transactions successfully deleted.', 'give' ); |
259
|
|
|
|
260
|
|
|
return false; |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Get the Export Data |
266
|
|
|
* |
267
|
|
|
* @access public |
268
|
|
|
* @since 1.8.12 |
269
|
|
|
* @global object $wpdb Used to query the database using the WordPress Database API |
270
|
|
|
* |
271
|
|
|
* @return array|bool $data The data for the CSV file |
272
|
|
|
*/ |
273
|
|
|
public function get_data() { |
274
|
|
|
|
275
|
|
|
// Get the donation id's. |
276
|
|
|
$donation_ids = $this->get_option( $this->donation_key ); |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Return false id not test donation is found. |
280
|
|
|
*/ |
281
|
|
|
if ( empty( $donation_ids ) ) { |
282
|
|
|
$this->is_empty = true; |
283
|
|
|
$this->total_step = 1; |
|
|
|
|
284
|
|
|
|
285
|
|
|
return false; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
// Get the current step. |
289
|
|
|
$step = (int) $this->get_step(); |
290
|
|
|
|
291
|
|
|
// get teh donor ids. |
292
|
|
|
$donor_ids = $this->get_option( $this->donor_key ); |
293
|
|
|
|
294
|
|
|
// In step to we delete all the donation in loop. |
295
|
|
|
if ( 2 === $step ) { |
296
|
|
|
$pass_to_donor = false; |
297
|
|
|
$page = (int) $this->get_step_page(); |
298
|
|
|
$page ++; |
299
|
|
|
$count = count( $donation_ids ); |
300
|
|
|
|
301
|
|
|
$this->total_step = ( ( count( $donation_ids ) / $this->per_step ) * 2 ) + count( $donor_ids ); |
|
|
|
|
302
|
|
|
$this->step_completed = $page; |
303
|
|
|
|
|
|
|
|
304
|
|
|
|
305
|
|
View Code Duplication |
if ( $count > $this->per_step ) { |
|
|
|
|
306
|
|
|
|
307
|
|
|
$this->update_option( $this->step_on_key, $page ); |
308
|
|
|
$donation_ids = $this->get_delete_ids( $donation_ids, $page ); |
309
|
|
|
$current_page = (int) ceil( $count / $this->per_step ); |
310
|
|
|
|
311
|
|
|
if ( $page === $current_page ) { |
312
|
|
|
$pass_to_donor = true; |
313
|
|
|
} |
314
|
|
|
} else { |
315
|
|
|
$pass_to_donor = true; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
if ( true === $pass_to_donor ) { |
319
|
|
|
$this->update_option( $this->step_key, 'donor' ); |
320
|
|
|
$this->update_option( $this->step_on_key, '0' ); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
foreach ( $donation_ids as $item ) { |
324
|
|
|
// Delete the main payment. |
325
|
|
|
give_delete_donation( absint( $item ) ); |
326
|
|
|
} |
327
|
|
|
do_action( 'give_delete_log_cache' ); |
328
|
|
|
} |
329
|
|
|
|
|
|
|
|
330
|
|
|
|
331
|
|
|
// Here we delete all the donor |
332
|
|
|
if ( 3 === $step ) { |
333
|
|
|
$page = (int) $this->get_step_page(); |
334
|
|
|
$count = count( $donor_ids ); |
335
|
|
|
|
336
|
|
|
$this->total_step = ( ( count( $donation_ids ) / $this->per_step ) * 2 ) + count( $donor_ids ); |
337
|
|
|
$this->step_completed = $page + ( count( $donation_ids ) / $this->per_step ); |
338
|
|
|
|
339
|
|
|
$args = apply_filters( 'give_tools_reset_stats_total_args', array( |
340
|
|
|
'post_type' => 'give_payment', |
341
|
|
|
'post_status' => 'any', |
342
|
|
|
'posts_per_page' => 1, |
343
|
|
|
'meta_key' => '_give_payment_mode', |
|
|
|
|
344
|
|
|
'meta_value' => 'live', |
|
|
|
|
345
|
|
|
'author' => $donor_ids[ $page ], |
346
|
|
|
) ); |
347
|
|
|
|
348
|
|
|
$donation_posts = get_posts( $args ); |
349
|
|
|
if ( empty( $donation_posts ) ) { |
350
|
|
|
Give()->donors->delete_by_user_id( $donor_ids[ $page ] ); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
$page ++; |
354
|
|
|
$this->update_option( $this->step_on_key, $page ); |
355
|
|
|
if ( $count === $page ) { |
356
|
|
|
$this->is_empty = false; |
357
|
|
|
|
358
|
|
|
return false; |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
return true; |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
return true; |
365
|
|
|
} |
366
|
|
|
|
367
|
|
View Code Duplication |
public function get_delete_ids( $donation_ids, $page ) { |
|
|
|
|
368
|
|
|
$index = $page --; |
|
|
|
|
369
|
|
|
$count = count( $donation_ids ); |
|
|
|
|
370
|
|
|
$temp = 0; |
371
|
|
|
$current_page = 0; |
372
|
|
|
$post_delete = $this->per_step; |
373
|
|
|
$page_donation_id = array(); |
374
|
|
|
|
375
|
|
|
foreach ( $donation_ids as $item ) { |
376
|
|
|
$temp ++; |
377
|
|
|
$page_donation_id[ $current_page ][] = $item; |
378
|
|
|
if ( $temp === $post_delete ) { |
379
|
|
|
$current_page ++; |
380
|
|
|
$temp = 0; |
381
|
|
|
} |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
return $page_donation_id[ $page ]; |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* Given a key, get the information from the Database Directly |
389
|
|
|
* |
390
|
|
|
* @since 1.8.13 |
391
|
|
|
* |
392
|
|
|
* @param string $key The option_name |
393
|
|
|
* |
394
|
|
|
* @return mixed Returns the data from the database |
395
|
|
|
*/ |
396
|
|
|
public function get_option( $key, $defalut_value = false ) { |
397
|
|
|
return get_option( $key, $defalut_value ); |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
/** |
401
|
|
|
* Give a key, store the value |
402
|
|
|
* |
403
|
|
|
* @since 1.8.12s |
404
|
|
|
* |
405
|
|
|
* @param string $key The option_name |
406
|
|
|
* @param mixed $value The value to store |
407
|
|
|
* |
408
|
|
|
* @return void |
409
|
|
|
*/ |
410
|
|
|
public function update_option( $key, $value ) { |
411
|
|
|
update_option( $key, $value, false ); |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
/** |
415
|
|
|
* Delete an option |
416
|
|
|
* |
417
|
|
|
* @since 1.8.12 |
418
|
|
|
* |
419
|
|
|
* @param string $key The option_name to delete |
420
|
|
|
* |
421
|
|
|
* @return void |
422
|
|
|
*/ |
423
|
|
|
public function delete_option( $key ) { |
424
|
|
|
delete_option( $key ); |
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
/** |
428
|
|
|
* Get the current step in number. |
429
|
|
|
* |
430
|
|
|
* There are three step to delete the total donor first counting, second deleting donotion and third deleting donors. |
431
|
|
|
* |
432
|
|
|
* @return int|string |
433
|
|
|
*/ |
434
|
|
View Code Duplication |
private function get_step() { |
|
|
|
|
435
|
|
|
$step_key = (string) $this->get_option( $this->step_key, false ); |
436
|
|
|
if ( 'count' === $step_key ) { |
437
|
|
|
return 1; |
438
|
|
|
} elseif ( 'donation' === $step_key ) { |
439
|
|
|
return 2; |
440
|
|
|
} elseif ( 'donor' === $step_key ) { |
441
|
|
|
return 3; |
442
|
|
|
} else { |
443
|
|
|
return $step_key; |
444
|
|
|
} |
445
|
|
|
} |
446
|
|
|
|
447
|
|
|
/** |
448
|
|
|
* Get the current $page value in the ajax. |
449
|
|
|
*/ |
450
|
|
|
private function get_step_page() { |
451
|
|
|
return $this->get_option( $this->step_on_key, false ); |
452
|
|
|
} |
453
|
|
|
} |
454
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.