PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.2.1
GiveWP – Donation Plugin and Fundraising Platform v2.2.1
4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 2.31.0 2.31.1 2.32.0 2.33.0 2.33.1 2.33.2 2.33.3 2.33.4 2.33.5 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.5.0 2.5.1 2.5.10 2.5.11 2.5.12 2.5.13 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.5.8 2.5.9 2.6.0 2.6.1 2.6.2 2.6.3 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.9.0 2.9.1 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.10.0 3.11.0 3.12.0 3.12.1 3.12.2 3.12.3 3.13.0 3.14.0 3.14.1 3.14.2 3.15.0 3.15.1 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.17.0 3.17.1 3.17.2 3.18.0 3.19.0 3.19.1 3.19.2 3.19.3 3.19.4 3.2.0 3.2.1 3.2.2 3.20.0 3.21.0 3.21.1 3.22.0 3.22.1 3.22.2 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.7.0 3.8.0 3.9.0 4.0.0 4.1.0 4.1.1 4.10.0 4.10.1 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.14.5 4.14.6 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.1 4.7.0 4.7.1 4.8.0 4.8.1 4.9.0 trunk 1.9.0 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.2 2.11.3 2.12.0 2.12.1 2.12.2 2.12.3 2.13.0 2.13.1 2.13.2 2.13.3 2.13.4 2.14.0 2.15.0 2.16.0 2.16.1 2.17.0 2.17.1 2.17.3 2.18.0 2.18.1 2.19.1 2.19.2 2.19.3 2.19.4 2.19.5 2.19.6 2.19.7 2.19.8 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.20.0 2.20.1 2.20.2 2.21.0 2.21.1 2.21.2 2.21.3 2.21.4 2.22.0 2.22.1 2.22.2 2.22.3 2.23.0 2.23.1 2.23.2 2.24.0 2.24.1 2.24.2 2.25.0 2.25.1 2.25.2 2.25.3 2.26.0 2.27.0 2.27.1 2.27.2 2.27.3 2.28.0 2.29.0 2.29.1 2.29.2
give / includes / admin / tools / data / class-give-tools-delete-import-donors.php
give / includes / admin / tools / data Last commit date
class-give-tools-delete-import-donors.php 7 years ago class-give-tools-delete-test-donors.php 7 years ago class-give-tools-delete-test-transactions.php 7 years ago class-give-tools-recount-all-stats.php 8 years ago class-give-tools-recount-donor-stats.php 8 years ago class-give-tools-recount-form-stats.php 8 years ago class-give-tools-recount-income.php 7 years ago class-give-tools-recount-single-donor-stats.php 8 years ago class-give-tools-reset-stats.php 7 years ago tools-actions.php 8 years ago
class-give-tools-delete-import-donors.php
574 lines
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_Import_Donors Class
21 *
22 * @since 1.8.13
23 */
24 class Give_Tools_Import_Donors extends Give_Batch_Export {
25
26 /**
27 * Form Data passed in batch processing.
28 *
29 * @var $request
30 */
31 var $request;
32
33 /**
34 * Used to store form id's that are going to get recount.
35 *
36 * @var $form_key
37 *
38 * @since 1.8.13
39 */
40 var $form_key = 'give_temp_delete_form_ids';
41
42 /**
43 * Used to store donation id's that are going to get deleted.
44 *
45 * @var $donation_key
46 *
47 * @since 1.8.12
48 */
49 var $donation_key = 'give_temp_delete_donation_ids';
50
51 /**
52 * Used to store the step where the step will be. ( 'count', 'donations', 'donors' ).
53 *
54 * @var $step_key
55 *
56 * @since 1.8.12
57 */
58 var $step_key = 'give_temp_delete_step';
59
60 /**
61 * Used to store donors id's that are going to get deleted.
62 *
63 * @var $donor_key
64 *
65 * @since 1.8.12
66 */
67 var $donor_key = 'give_temp_delete_donor_ids';
68
69 /**
70 * Used to store to get the page count in the loop.
71 *
72 * @var $step_on_key
73 *
74 * @since 1.8.12
75 */
76 var $step_on_key = 'give_temp_delete_step_on';
77
78 /**
79 * Contain total number of step.
80 *
81 * @var $total_step
82 *
83 * @since 1.8.12
84 */
85 var $total_step;
86
87 /**
88 * Counting contain total number of step that completed.
89 *
90 * @var $step_completed
91 *
92 * @since 1.8.12
93 */
94 var $step_completed;
95
96 /**
97 * Our export type. Used for export-type specific filters/actions.
98 *
99 * @var $export_type
100 *
101 * @since 1.8.12
102 */
103 public $export_type = '';
104
105 /**
106 * Allows for a non-form batch processing to be run.
107 *
108 * @var $is_void
109 *
110 * @since 1.8.12
111 */
112 public $is_void = true;
113
114 /**
115 * Sets the number of items to pull on each step
116 *
117 * @var $per_step
118 *
119 * @since 1.8.12
120 */
121 public $per_step = 10;
122
123 /**
124 * Set's all the donors id's
125 *
126 * @var $donor_ids
127 *
128 * @since 1.8.12
129 */
130 public $donor_ids = array();
131
132 /**
133 * Give_Tools_Import_Donors constructor.
134 *
135 * @param int $_step Steps.
136 */
137 public function __construct( $_step = 1 ) {
138 parent::__construct( $_step );
139
140 $this->is_writable = true;
141 }
142
143 /**
144 * Get the Export Data
145 *
146 * @access public
147 * @since 1.8.12
148 * @global object $wpdb Used to query the database using the WordPress Database API
149 *
150 * @return void
151 */
152 public function pre_fetch() {
153 $donation_ids = array();
154 $donor_ids = array();
155
156 // Check if the ajax request if running for the first time.
157 if ( 1 === (int) $this->step ) {
158
159 // Delete all the form ids.
160 $this->delete_option( $this->form_key );
161
162 // Delete all the donation ids.
163 $this->delete_option( $this->donation_key );
164
165 // Delete all the donor ids.
166 $this->delete_option( $this->donor_key );
167
168 // Delete all the step and set to 'count' which if the first step in the process of deleting the donors.
169 $this->update_option( $this->step_key, 'count' );
170
171 // Delete tha page count of the step.
172 $this->update_option( $this->step_on_key, '0' );
173 } else {
174
175 // Get the old donors list.
176 $donor_ids = $this->get_option( $this->donor_key );
177
178 // Get the old donation list.
179 $donation_ids = $this->get_option( $this->donation_key );
180 }
181
182 // Get the step and check for it if it's on the first step( 'count' ) or not.
183 $step = (int) $this->get_step();
184 if ( 1 === $step ) {
185 /**
186 * Will add or update the donation and donor data by running wp query.
187 */
188 $this->count( $step, $donation_ids, $donor_ids );
189 }
190 }
191
192 /**
193 * Will Update or Add the donation and donors ids in the with option table for there respected key.
194 *
195 * @param string $step On which the current ajax is running.
196 * @param array $donation_ids Contain the list of all the donation id's that has being add before this.
197 * @param array $donor_ids Contain the list of all the donors id's that has being add before this.
198 */
199 private function count( $step, $donation_ids = array(), $donor_ids = array() ) {
200
201 // Get the Page count by default it's zero.
202 $paged = (int) $this->get_step_page();
203
204 // Increased the page count by one.
205 ++ $paged;
206
207 /**
208 * Filter add to alter the argument before the wp quest run
209 */
210 $args = apply_filters( 'give_tools_reset_stats_total_args', array(
211 'post_type' => 'give_payment',
212 'post_status' => 'any',
213 'posts_per_page' => $this->per_step,
214 'paged' => $paged,
215 'meta_key' => '_give_payment_import',
216 'meta_value_num' => 1,
217 'meta_compare' => '=',
218 ) );
219
220 // Reset the post data.
221 wp_reset_postdata();
222
223 // Getting the new donation.
224 $donation_posts = new WP_Query( $args );
225
226 // The Loop.
227 if ( $donation_posts->have_posts() ) {
228 while ( $donation_posts->have_posts() ) {
229 $add_author = true;
230 $donation_posts->the_post();
231 global $post;
232 // Add the donation id in side the array.
233 $donation_ids[] = $post->ID;
234
235 $donor_id = (int) give_get_meta( $post->ID, '_give_payment_customer_id', true );
236 if ( ! empty( $donor_id ) ) {
237 $donor = new Give_Donor( $donor_id );
238 if ( ! empty( $donor->id ) ) {
239 if ( empty( $donor->user_id ) && ! empty( $donor->payment_ids ) ) {
240 $add_author = false;
241 $count = (int) count( $donor->payment_ids );
242 if ( 1 === $count ) {
243 give_delete_donor_and_related_donation( $donor );
244 } else {
245 $donor->remove_payment( $post->ID );
246 $donor->decrease_donation_count();
247 }
248 }
249 }
250 }
251
252 if ( ! empty( $add_author ) ) {
253 // Add the donor id in side the array.
254 $donor_ids[] = (int) $post->post_author;
255 }
256 }
257 }
258
259 // Get the total number of post found.
260 $total_donation = (int) $donation_posts->found_posts;
261
262 // Maximum number of page can be display.
263 $max_num_pages = (int) $donation_posts->max_num_pages;
264
265 // Check current page is less then max number of page or not.
266 if ( $paged < $max_num_pages ) {
267
268 // Update the current page variable for the next step.
269 $this->update_option( $this->step_on_key, $paged );
270
271 // Calculating percentage.
272 $page_remain = $max_num_pages - $paged;
273 $this->total_step = (int) $max_num_pages + ( $total_donation / $this->per_step ) + ( ( $page_remain * 2 ) * count( $donor_ids ) );
274 $this->step_completed = $paged;
275 } else {
276 $donation_ids_count = count( $donor_ids );
277 $this->update_option( $this->step_key, 'donation' );
278 $this->update_option( $this->step_on_key, '0' );
279 }
280
281 $donor_ids = array_unique( $donor_ids );
282 $this->update_option( $this->donor_key, $donor_ids );
283 $this->update_option( $this->donation_key, $donation_ids );
284
285 wp_reset_postdata();
286 }
287
288 /**
289 * Return the calculated completion percentage.
290 *
291 * @since 1.8.12
292 *
293 * @return int
294 */
295 public function get_percentage_complete() {
296 return ceil( ( 100 * $this->step_completed ) / $this->total_step );
297 }
298
299 /**
300 * Process Steps
301 *
302 * @return bool
303 */
304 public function process_step() {
305
306 if ( ! $this->can_export() ) {
307 wp_die(
308 esc_html__( 'You do not have permission to delete Import transactions.', 'give' ),
309 esc_html__( 'Error', 'give' ),
310 array(
311 'response' => 403,
312 )
313 );
314 }
315
316 $had_data = $this->get_data();
317
318 if ( $had_data ) {
319 $this->done = false;
320
321 return true;
322 } else {
323 update_option( 'give_earnings_total', give_get_total_earnings( true ), false );
324 Give_Cache::delete( Give_Cache::get_key( 'give_estimated_monthly_stats' ) );
325
326 $this->delete_option( $this->donation_key );
327
328 $this->done = true;
329 $this->message = __( 'Imported donor and transactions successfully deleted.', 'give' );
330
331 return false;
332 }
333 }
334
335 /**
336 * Get the Export Data
337 *
338 * @access public
339 * @since 1.8.12
340 * @global object $wpdb Used to query the database using the WordPress Database API
341 *
342 * @return array|bool $data The data for the CSV file
343 */
344 public function get_data() {
345
346 // Get the donation id's.
347 $donation_ids = $this->get_option( $this->donation_key );
348
349 /**
350 * Return false id not Import donation is found.
351 */
352 if ( empty( $donation_ids ) ) {
353 $this->is_empty = true;
354 $this->total_step = 1;
355
356 return false;
357 }
358
359 // Get the current step.
360 $step = (int) $this->get_step();
361
362 // Get the donor ids.
363 $donor_ids = $this->get_option( $this->donor_key );
364
365 // Delete all the imported donations.
366 if ( 2 === $step ) {
367 $pass_to_donor = false;
368 $page = (int) $this->get_step_page();
369 $page ++;
370 $count = count( $donation_ids );
371
372 $this->total_step = ( ( count( $donation_ids ) / $this->per_step ) * 2 ) + count( $donor_ids );
373 $this->step_completed = $page;
374
375 if ( $count > $this->per_step ) {
376
377 $this->update_option( $this->step_on_key, $page );
378 $donation_ids = $this->get_delete_ids( $donation_ids, $page );
379 $current_page = (int) ceil( $count / $this->per_step );
380
381 if ( $page === $current_page ) {
382 $pass_to_donor = true;
383 }
384 } else {
385 $pass_to_donor = true;
386 }
387
388 if ( true === $pass_to_donor ) {
389 $this->update_option( $this->step_key, 'donor' );
390 $this->update_option( $this->step_on_key, '0' );
391 }
392
393 // Get the old form list.
394 $form_ids = (array) $this->get_option( $this->form_key );
395
396 foreach ( $donation_ids as $item ) {
397 $form_ids[] = give_get_meta( $item, '_give_payment_form_id', true );
398
399 // Delete the main payment.
400 give_delete_donation( absint( $item ) );
401 }
402
403 // Update the new form list.
404 $this->update_option( $this->form_key, $form_ids );
405 } // End if().
406
407 // Delete all the donors.
408 if ( 3 === $step ) {
409
410 // Get the old form list.
411 $form_ids = (array) $this->get_option( $this->form_key );
412 if ( ! empty( $form_ids ) ) {
413 $form_ids = array_unique( $form_ids );
414 foreach ( $form_ids as $form_id ) {
415 give_recount_form_income_donation( (int) $form_id );
416 }
417 }
418 // update the new form list.
419 $this->update_option( $this->form_key, array() );
420
421 $page = (int) $this->get_step_page();
422 $count = count( $donor_ids );
423
424 $this->total_step = ( ( count( $donation_ids ) / $this->per_step ) * 2 ) + count( $donor_ids );
425 $this->step_completed = $page + ( count( $donation_ids ) / $this->per_step );
426
427 if ( ! empty( $donor_ids[ $page ] ) ) {
428 $args = apply_filters( 'give_tools_reset_stats_total_args', array(
429 'post_status' => 'any',
430 'posts_per_page' => 1,
431 'author' => $donor_ids[ $page ],
432 ) );
433
434 $donations = array();
435 $payments = new Give_Payments_Query( $args );
436 $payments = $payments->get_payments();
437 if ( empty( $payments ) ) {
438 Give()->donors->delete_by_user_id( $donor_ids[ $page ] );
439
440 /**
441 * If Checked then delete WP user.
442 *
443 * @since 1.8.14
444 */
445 $delete_import_donors = isset( $_REQUEST['delete-import-donors'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['delete-import-donors'] ) ) : '';
446
447 if ( 'on' === (string) $delete_import_donors ) {
448 wp_delete_user( $donor_ids[ $page ] );
449 }
450 } else {
451 foreach ( $payments as $payment ) {
452 $donations[] = $payment->ID;
453 }
454
455 $donor = new Give_Donor( $donor_ids[ $page ], true );
456 $data_to_update = array(
457 'purchase_count' => count( $donations ),
458 'payment_ids' => implode( ',', $donations ),
459 );
460 $donor->update( $data_to_update );
461 }
462 } // End if().
463
464 $page ++;
465 $this->update_option( $this->step_on_key, $page );
466 if ( $count === $page ) {
467 $this->is_empty = false;
468
469 return false;
470 }
471
472 return true;
473 } // End if().
474
475 return true;
476 }
477
478 /**
479 * This function will get list of donation ids ready for deletion.
480 *
481 * @param array $donation_ids List of donation ids.
482 * @param string $page Ajax on Page.
483 *
484 * @return mixed
485 */
486 public function get_delete_ids( $donation_ids, $page ) {
487 $index = $page --;
488 $count = count( $donation_ids );
489 $temp = 0;
490 $current_page = 0;
491 $post_delete = $this->per_step;
492 $page_donation_id = array();
493
494 foreach ( $donation_ids as $item ) {
495 $temp ++;
496 $page_donation_id[ $current_page ][] = $item;
497 if ( $temp === $post_delete ) {
498 $current_page ++;
499 $temp = 0;
500 }
501 }
502
503 return $page_donation_id[ $page ];
504 }
505
506 /**
507 * Given a key, get the information from the Database Directly
508 *
509 * @since 1.8.12
510 *
511 * @param string $key Option Key.
512 * @param bool $default_value True, if default value, else false.
513 *
514 * @return mixed Returns the data from the database
515 */
516 public function get_option( $key, $default_value = false ) {
517 return get_option( $key, $default_value );
518 }
519
520 /**
521 * Give a key, store the value
522 *
523 * @since 1.8.12
524 *
525 * @param string $key Option Key.
526 * @param mixed $value Option Value.
527 *
528 * @return void
529 */
530 public function update_option( $key, $value ) {
531 update_option( $key, $value, false );
532 }
533
534 /**
535 * Delete an option
536 *
537 * @since 1.8.12
538 *
539 * @param string $key Option Key.
540 *
541 * @return void
542 */
543 public function delete_option( $key ) {
544 delete_option( $key );
545 }
546
547 /**
548 * Get the current step in number.
549 *
550 * There are three step to delete the total donor first counting, second deleting donotion and third deleting donors.
551 *
552 * @return int|string
553 */
554 private function get_step() {
555 $step_key = (string) $this->get_option( $this->step_key, false );
556 if ( 'count' === $step_key ) {
557 return 1;
558 } elseif ( 'donation' === $step_key ) {
559 return 2;
560 } elseif ( 'donor' === $step_key ) {
561 return 3;
562 } else {
563 return $step_key;
564 }
565 }
566
567 /**
568 * Get the current $page value in the ajax.
569 */
570 private function get_step_page() {
571 return $this->get_option( $this->step_on_key, false );
572 }
573 }
574