PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.0.3
GiveWP – Donation Plugin and Fundraising Platform v2.0.3
4.16.4 4.16.3 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 8 years ago class-give-tools-delete-test-donors.php 8 years ago class-give-tools-delete-test-transactions.php 8 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 8 years ago class-give-tools-recount-single-donor-stats.php 8 years ago class-give-tools-reset-stats.php 8 years ago tools-actions.php 8 years ago
class-give-tools-delete-import-donors.php
523 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 var $request;
27
28 /**
29 * Used to store form id's that are going to get recount.
30 * @var array.
31 * @since 1.8.13
32 */
33 var $form_key = 'give_temp_delete_form_ids';
34
35 /**
36 * Used to store donation id's that are going to get deleted.
37 * @var array.
38 * @since 1.8.12
39 */
40 var $donation_key = 'give_temp_delete_donation_ids';
41
42 /**
43 * Used to store donors id's that are going to get deleted.
44 * @var array.
45 * @since 1.8.12
46 */
47 var $donor_key = 'give_temp_delete_donor_ids';
48
49 /**
50 * Used to store the step where the step will be. ( 'count', 'donations', 'donors' ).
51 * @var array.
52 * @since 1.8.12
53 */
54 var $step_key = 'give_temp_delete_step';
55
56 /**
57 * Used to store to get the page count in the loop.
58 * @var array.
59 * @since 1.8.12
60 */
61 var $step_on_key = 'give_temp_delete_step_on';
62
63 /**
64 * Contain total number of step .
65 * @var array.
66 * @since 1.8.12
67 */
68 var $total_step;
69
70 /**
71 * Counting contain total number of step that completed.
72 * @var array.
73 * @since 1.8.12
74 */
75 var $step_completed;
76
77 /**
78 * Our export type. Used for export-type specific filters/actions
79 * @var string
80 * @since 1.8.12
81 */
82 public $export_type = '';
83
84 /**
85 * Allows for a non-form batch processing to be run.
86 * @since 1.8.12
87 * @var boolean
88 */
89 public $is_void = true;
90
91 /**
92 * Sets the number of items to pull on each step
93 * @since 1.8.12
94 * @var integer
95 */
96 public $per_step = 10;
97
98 /**
99 * Set's all the donors id's
100 * @since 1.8.12
101 * @var array
102 */
103 public $donor_ids = array();
104
105 /**
106 * Constructor.
107 */
108 public function __construct( $_step = 1 ) {
109 parent::__construct( $_step );
110
111 $this->is_writable = true;
112 }
113
114 /**
115 * Get the Export Data
116 *
117 * @access public
118 * @since 1.8.12
119 * @global object $wpdb Used to query the database using the WordPress Database API
120 *
121 * @return array|bool $data The data for the CSV file
122 */
123 public function pre_fetch() {
124 $donation_ids = array();
125 $donor_ids = array();
126
127 // Check if the ajax request if running for the first time.
128 if ( 1 === (int) $this->step ) {
129 // Delete all the form ids.
130 $this->delete_option( $this->form_key );
131 // Delete all the donation ids.
132 $this->delete_option( $this->donation_key );
133 // Delete all the donor ids.
134 $this->delete_option( $this->donor_key );
135
136 // Delete all the step and set to 'count' which if the first step in the process of deleting the donors.
137 $this->update_option( $this->step_key, 'count' );
138
139 // Delete tha page count of the step.
140 $this->update_option( $this->step_on_key, '0' );
141 } else {
142 // Get the old donors list.
143 $donor_ids = $this->get_option( $this->donor_key );
144
145 // Get the old donation list.
146 $donation_ids = $this->get_option( $this->donation_key );
147 }
148
149 // Get the step and check for it if it's on the first step( 'count' ) or not.
150 $step = (int) $this->get_step();
151 if ( 1 === $step ) {
152 /**
153 * Will add or update the donation and donor data by running wp query.
154 */
155 $this->count( $step, $donation_ids, $donor_ids );
156 }
157 }
158
159 /**
160 * Will Update or Add the donation and donors ids in the with option table for there respected key.
161 *
162 * @param string $step On which the current ajax is running.
163 * @param array $donation_ids Contain the list of all the donation id's that has being add before this
164 * @param array $donor_ids Contain the list of all the donors id's that has being add before this
165 */
166 private function count( $step, $donation_ids = array(), $donor_ids = array() ) {
167
168 // Get the Page count by default it's zero.
169 $paged = (int) $this->get_step_page();
170 // Incresed the page count by one.
171 ++ $paged;
172
173 /**
174 * Filter add to alter the argument before the wp quest run
175 */
176 $args = apply_filters( 'give_tools_reset_stats_total_args', array(
177 'post_type' => 'give_payment',
178 'post_status' => 'any',
179 'posts_per_page' => $this->per_step,
180 'paged' => $paged,
181 'meta_key' => '_give_payment_import',
182 'meta_value_num' => 1,
183 'meta_compare' => '=',
184 ) );
185
186 // Reset the post data.
187 wp_reset_postdata();
188 // Getting the new donation.
189 $donation_posts = new WP_Query( $args );
190
191 // The Loop.
192 if ( $donation_posts->have_posts() ) {
193 while ( $donation_posts->have_posts() ) {
194 $add_author = true;
195 $donation_posts->the_post();
196 global $post;
197 // Add the donation id in side the array.
198 $donation_ids[] = $post->ID;
199
200 $donor_id = (int) give_get_meta( $post->ID, '_give_payment_customer_id', true );
201 if ( ! empty( $donor_id ) ) {
202 $donor = new Give_Donor( $donor_id );
203 if ( ! empty( $donor->id ) ) {
204 if ( empty( $donor->user_id ) && ! empty( $donor->payment_ids ) ) {
205 $add_author = false;
206 $count = (int) count( $donor->payment_ids );
207 if ( 1 === $count ) {
208 Give()->donors->delete( $donor->id );
209 } else {
210 $donor->remove_payment( $post->ID );
211 $donor->decrease_donation_count();
212 }
213 }
214 }
215 }
216
217 if ( ! empty( $add_author ) ) {
218 // Add the donor id in side the array.
219 $donor_ids[] = (int) $post->post_author;
220 }
221 }
222 /* Restore original Post Data */
223 }
224
225 // Get the total number of post found.
226 $total_donation = (int) $donation_posts->found_posts;
227
228 // Maximum number of page can be display
229 $max_num_pages = (int) $donation_posts->max_num_pages;
230
231 // Check current page is less then max number of page or not
232 if ( $paged < $max_num_pages ) {
233 // Update the curretn page virable for the next step
234 $this->update_option( $this->step_on_key, $paged );
235
236 // Calculating percentage.
237 $page_remain = $max_num_pages - $paged;
238 $this->total_step = (int) $max_num_pages + ( $total_donation / $this->per_step ) + ( ( $page_remain * 2 ) * count( $donor_ids ) );
239 $this->step_completed = $paged;
240 } else {
241 $donation_ids_count = count( $donor_ids );
242 $this->update_option( $this->step_key, 'donation' );
243 $this->update_option( $this->step_on_key, '0' );
244 }
245
246 $donor_ids = array_unique( $donor_ids );
247 $this->update_option( $this->donor_key, $donor_ids );
248 $this->update_option( $this->donation_key, $donation_ids );
249
250 wp_reset_postdata();
251 }
252
253 /**
254 * Return the calculated completion percentage.
255 *
256 * @since 1.8.12
257 * @return int
258 */
259 public function get_percentage_complete() {
260 return ceil( ( 100 * $this->step_completed ) / $this->total_step );
261 }
262
263 public function process_step() {
264
265 if ( ! $this->can_export() ) {
266 wp_die( __( 'You do not have permission to delete Import transactions.', 'give' ), __( 'Error', 'give' ), array( 'response' => 403 ) );
267 }
268
269 $had_data = $this->get_data();
270
271 if ( $had_data ) {
272 $this->done = false;
273
274 return true;
275 } else {
276 update_option( 'give_earnings_total', give_get_total_earnings( true ) );
277 Give_Cache::delete( Give_Cache::get_key( 'give_estimated_monthly_stats' ) );
278
279 $this->delete_option( $this->donation_key );
280
281 // Reset the sequential order numbers
282 if ( give_get_option( 'enable_sequential' ) ) {
283 delete_option( 'give_last_payment_number' );
284 }
285
286 $this->done = true;
287 $this->message = __( 'Imported donor and transactions successfully deleted.', 'give' );
288
289 return false;
290 }
291 }
292
293 /**
294 * Get the Export Data
295 *
296 * @access public
297 * @since 1.8.12
298 * @global object $wpdb Used to query the database using the WordPress Database API
299 *
300 * @return array|bool $data The data for the CSV file
301 */
302 public function get_data() {
303
304 // Get the donation id's.
305 $donation_ids = $this->get_option( $this->donation_key );
306
307 /**
308 * Return false id not Import donation is found.
309 */
310 if ( empty( $donation_ids ) ) {
311 $this->is_empty = true;
312 $this->total_step = 1;
313
314 return false;
315 }
316
317 // Get the current step.
318 $step = (int) $this->get_step();
319
320 // get teh donor ids.
321 $donor_ids = $this->get_option( $this->donor_key );
322
323 // In step to we delete all the donation in loop.
324 if ( 2 === $step ) {
325 $pass_to_donor = false;
326 $page = (int) $this->get_step_page();
327 $page ++;
328 $count = count( $donation_ids );
329
330 $this->total_step = ( ( count( $donation_ids ) / $this->per_step ) * 2 ) + count( $donor_ids );
331 $this->step_completed = $page;
332
333
334 if ( $count > $this->per_step ) {
335
336 $this->update_option( $this->step_on_key, $page );
337 $donation_ids = $this->get_delete_ids( $donation_ids, $page );
338 $current_page = (int) ceil( $count / $this->per_step );
339
340 if ( $page === $current_page ) {
341 $pass_to_donor = true;
342 }
343 } else {
344 $pass_to_donor = true;
345 }
346
347 if ( true === $pass_to_donor ) {
348 $this->update_option( $this->step_key, 'donor' );
349 $this->update_option( $this->step_on_key, '0' );
350 }
351
352 // Get the old form list.
353 $form_ids = (array) $this->get_option( $this->form_key );
354
355 foreach ( $donation_ids as $item ) {
356 $form_ids[] = give_get_meta( $item, '_give_payment_form_id', true );
357
358 // Delete the main payment.
359 give_delete_donation( absint( $item ) );
360 }
361
362 // update the new form list.
363 $this->update_option( $this->form_key, $form_ids );
364 }
365
366
367 // Here we delete all the donor
368 if ( 3 === $step ) {
369
370 // Get the old form list.
371 $form_ids = (array) $this->get_option( $this->form_key );
372 if ( ! empty( $form_ids ) ) {
373 $form_ids = array_unique( $form_ids );
374 foreach ( $form_ids as $form_id ) {
375 give_recount_form_income_donation( (int) $form_id );
376 }
377 }
378 // update the new form list.
379 $this->update_option( $this->form_key, array() );
380
381 $page = (int) $this->get_step_page();
382 $count = count( $donor_ids );
383
384 $this->total_step = ( ( count( $donation_ids ) / $this->per_step ) * 2 ) + count( $donor_ids );
385 $this->step_completed = $page + ( count( $donation_ids ) / $this->per_step );
386
387 if ( ! empty( $donor_ids[ $page ] ) ) {
388 $args = apply_filters( 'give_tools_reset_stats_total_args', array(
389 'post_status' => 'any',
390 'posts_per_page' => 1,
391 'author' => $donor_ids[ $page ]
392 ) );
393
394 $donations = array();
395 $payments = new Give_Payments_Query( $args );
396 $payments = $payments->get_payments();
397 if ( empty( $payments ) ) {
398 Give()->donors->delete_by_user_id( $donor_ids[ $page ] );
399
400 /**
401 * If Checked then delete WP user.
402 *
403 * @since 1.8.14
404 */
405 if ( 'on' === (string) $_REQUEST['delete-import-donors'] ) {
406 wp_delete_user( $donor_ids[ $page ] );
407 }
408 } else {
409 foreach ( $payments as $payment ) {
410 $donations[] = $payment->ID;
411 }
412
413 $donor = new Give_Donor( $donor_ids[ $page ], true );
414 $data_to_update = array(
415 'purchase_count' => count( $donations ),
416 'payment_ids' => implode( ',', $donations ),
417 );
418 $donor->update( $data_to_update );
419 }
420 }
421
422 $page ++;
423 $this->update_option( $this->step_on_key, $page );
424 if ( $count === $page ) {
425 $this->is_empty = false;
426
427 return false;
428 }
429
430 return true;
431 }
432
433 return true;
434 }
435
436 public function get_delete_ids( $donation_ids, $page ) {
437 $index = $page --;
438 $count = count( $donation_ids );
439 $temp = 0;
440 $current_page = 0;
441 $post_delete = $this->per_step;
442 $page_donation_id = array();
443
444 foreach ( $donation_ids as $item ) {
445 $temp ++;
446 $page_donation_id[ $current_page ][] = $item;
447 if ( $temp === $post_delete ) {
448 $current_page ++;
449 $temp = 0;
450 }
451 }
452
453 return $page_donation_id[ $page ];
454 }
455
456 /**
457 * Given a key, get the information from the Database Directly
458 *
459 * @since 1.8.12
460 *
461 * @param string $key The option_name
462 *
463 * @return mixed Returns the data from the database
464 */
465 public function get_option( $key, $defalut_value = false ) {
466 return get_option( $key, $defalut_value );
467 }
468
469 /**
470 * Give a key, store the value
471 *
472 * @since 1.8.12s
473 *
474 * @param string $key The option_name
475 * @param mixed $value The value to store
476 *
477 * @return void
478 */
479 public function update_option( $key, $value ) {
480 update_option( $key, $value, false );
481 }
482
483 /**
484 * Delete an option
485 *
486 * @since 1.8.12
487 *
488 * @param string $key The option_name to delete
489 *
490 * @return void
491 */
492 public function delete_option( $key ) {
493 delete_option( $key );
494 }
495
496 /**
497 * Get the current step in number.
498 *
499 * There are three step to delete the total donor first counting, second deleting donotion and third deleting donors.
500 *
501 * @return int|string
502 */
503 private function get_step() {
504 $step_key = (string) $this->get_option( $this->step_key, false );
505 if ( 'count' === $step_key ) {
506 return 1;
507 } elseif ( 'donation' === $step_key ) {
508 return 2;
509 } elseif ( 'donor' === $step_key ) {
510 return 3;
511 } else {
512 return $step_key;
513 }
514 }
515
516 /**
517 * Get the current $page value in the ajax.
518 */
519 private function get_step_page() {
520 return $this->get_option( $this->step_on_key, false );
521 }
522 }
523