PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.1.0
GiveWP – Donation Plugin and Fundraising Platform v2.1.0
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
522 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(
267 esc_html__( 'You do not have permission to delete Import transactions.', 'give' ),
268 esc_html__( 'Error', 'give' ),
269 array( 'response' => 403 )
270 );
271 }
272
273 $had_data = $this->get_data();
274
275 if ( $had_data ) {
276 $this->done = false;
277
278 return true;
279 } else {
280 update_option( 'give_earnings_total', give_get_total_earnings( true ) );
281 Give_Cache::delete( Give_Cache::get_key( 'give_estimated_monthly_stats' ) );
282
283 $this->delete_option( $this->donation_key );
284
285 $this->done = true;
286 $this->message = __( 'Imported donor and transactions successfully deleted.', 'give' );
287
288 return false;
289 }
290 }
291
292 /**
293 * Get the Export Data
294 *
295 * @access public
296 * @since 1.8.12
297 * @global object $wpdb Used to query the database using the WordPress Database API
298 *
299 * @return array|bool $data The data for the CSV file
300 */
301 public function get_data() {
302
303 // Get the donation id's.
304 $donation_ids = $this->get_option( $this->donation_key );
305
306 /**
307 * Return false id not Import donation is found.
308 */
309 if ( empty( $donation_ids ) ) {
310 $this->is_empty = true;
311 $this->total_step = 1;
312
313 return false;
314 }
315
316 // Get the current step.
317 $step = (int) $this->get_step();
318
319 // get teh donor ids.
320 $donor_ids = $this->get_option( $this->donor_key );
321
322 // In step to we delete all the donation in loop.
323 if ( 2 === $step ) {
324 $pass_to_donor = false;
325 $page = (int) $this->get_step_page();
326 $page ++;
327 $count = count( $donation_ids );
328
329 $this->total_step = ( ( count( $donation_ids ) / $this->per_step ) * 2 ) + count( $donor_ids );
330 $this->step_completed = $page;
331
332 if ( $count > $this->per_step ) {
333
334 $this->update_option( $this->step_on_key, $page );
335 $donation_ids = $this->get_delete_ids( $donation_ids, $page );
336 $current_page = (int) ceil( $count / $this->per_step );
337
338 if ( $page === $current_page ) {
339 $pass_to_donor = true;
340 }
341 } else {
342 $pass_to_donor = true;
343 }
344
345 if ( true === $pass_to_donor ) {
346 $this->update_option( $this->step_key, 'donor' );
347 $this->update_option( $this->step_on_key, '0' );
348 }
349
350 // Get the old form list.
351 $form_ids = (array) $this->get_option( $this->form_key );
352
353 foreach ( $donation_ids as $item ) {
354 $form_ids[] = give_get_meta( $item, '_give_payment_form_id', true );
355
356 // Delete the main payment.
357 give_delete_donation( absint( $item ) );
358 }
359
360 // update the new form list.
361 $this->update_option( $this->form_key, $form_ids );
362 }
363
364 // Here we delete all the donor
365 if ( 3 === $step ) {
366
367 // Get the old form list.
368 $form_ids = (array) $this->get_option( $this->form_key );
369 if ( ! empty( $form_ids ) ) {
370 $form_ids = array_unique( $form_ids );
371 foreach ( $form_ids as $form_id ) {
372 give_recount_form_income_donation( (int) $form_id );
373 }
374 }
375 // update the new form list.
376 $this->update_option( $this->form_key, array() );
377
378 $page = (int) $this->get_step_page();
379 $count = count( $donor_ids );
380
381 $this->total_step = ( ( count( $donation_ids ) / $this->per_step ) * 2 ) + count( $donor_ids );
382 $this->step_completed = $page + ( count( $donation_ids ) / $this->per_step );
383
384 if ( ! empty( $donor_ids[ $page ] ) ) {
385 $args = apply_filters( 'give_tools_reset_stats_total_args', array(
386 'post_status' => 'any',
387 'posts_per_page' => 1,
388 'author' => $donor_ids[ $page ],
389 ) );
390
391 $donations = array();
392 $payments = new Give_Payments_Query( $args );
393 $payments = $payments->get_payments();
394 if ( empty( $payments ) ) {
395 Give()->donors->delete_by_user_id( $donor_ids[ $page ] );
396
397 /**
398 * If Checked then delete WP user.
399 *
400 * @since 1.8.14
401 */
402 $delete_import_donors = isset( $_REQUEST['delete-import-donors'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['delete-import-donors'] ) ) : '';
403
404 if ( 'on' === (string) $delete_import_donors ) {
405 wp_delete_user( $donor_ids[ $page ] );
406 }
407 } else {
408 foreach ( $payments as $payment ) {
409 $donations[] = $payment->ID;
410 }
411
412 $donor = new Give_Donor( $donor_ids[ $page ], true );
413 $data_to_update = array(
414 'purchase_count' => count( $donations ),
415 'payment_ids' => implode( ',', $donations ),
416 );
417 $donor->update( $data_to_update );
418 }
419 }
420
421 $page ++;
422 $this->update_option( $this->step_on_key, $page );
423 if ( $count === $page ) {
424 $this->is_empty = false;
425
426 return false;
427 }
428
429 return true;
430 }
431
432 return true;
433 }
434
435 public function get_delete_ids( $donation_ids, $page ) {
436 $index = $page --;
437 $count = count( $donation_ids );
438 $temp = 0;
439 $current_page = 0;
440 $post_delete = $this->per_step;
441 $page_donation_id = array();
442
443 foreach ( $donation_ids as $item ) {
444 $temp ++;
445 $page_donation_id[ $current_page ][] = $item;
446 if ( $temp === $post_delete ) {
447 $current_page ++;
448 $temp = 0;
449 }
450 }
451
452 return $page_donation_id[ $page ];
453 }
454
455 /**
456 * Given a key, get the information from the Database Directly
457 *
458 * @since 1.8.12
459 *
460 * @param string $key The option_name
461 *
462 * @return mixed Returns the data from the database
463 */
464 public function get_option( $key, $defalut_value = false ) {
465 return get_option( $key, $defalut_value );
466 }
467
468 /**
469 * Give a key, store the value
470 *
471 * @since 1.8.12s
472 *
473 * @param string $key The option_name
474 * @param mixed $value The value to store
475 *
476 * @return void
477 */
478 public function update_option( $key, $value ) {
479 update_option( $key, $value, false );
480 }
481
482 /**
483 * Delete an option
484 *
485 * @since 1.8.12
486 *
487 * @param string $key The option_name to delete
488 *
489 * @return void
490 */
491 public function delete_option( $key ) {
492 delete_option( $key );
493 }
494
495 /**
496 * Get the current step in number.
497 *
498 * There are three step to delete the total donor first counting, second deleting donotion and third deleting donors.
499 *
500 * @return int|string
501 */
502 private function get_step() {
503 $step_key = (string) $this->get_option( $this->step_key, false );
504 if ( 'count' === $step_key ) {
505 return 1;
506 } elseif ( 'donation' === $step_key ) {
507 return 2;
508 } elseif ( 'donor' === $step_key ) {
509 return 3;
510 } else {
511 return $step_key;
512 }
513 }
514
515 /**
516 * Get the current $page value in the ajax.
517 */
518 private function get_step_page() {
519 return $this->get_option( $this->step_on_key, false );
520 }
521 }
522