| 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, GiveWP |
| 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( |
| 211 |
'give_tools_reset_stats_total_args', |
| 212 |
array( |
| 213 |
'post_type' => 'give_payment', |
| 214 |
'post_status' => 'any', |
| 215 |
'posts_per_page' => $this->per_step, |
| 216 |
'paged' => $paged, |
| 217 |
'meta_key' => '_give_payment_import', |
| 218 |
'meta_value_num' => 1, |
| 219 |
'meta_compare' => '=', |
| 220 |
) |
| 221 |
); |
| 222 |
|
| 223 |
// Reset the post data. |
| 224 |
wp_reset_postdata(); |
| 225 |
|
| 226 |
// Getting the new donation. |
| 227 |
$donation_posts = new WP_Query( $args ); |
| 228 |
|
| 229 |
// The Loop. |
| 230 |
if ( $donation_posts->have_posts() ) { |
| 231 |
while ( $donation_posts->have_posts() ) { |
| 232 |
$add_author = true; |
| 233 |
$donation_posts->the_post(); |
| 234 |
global $post; |
| 235 |
// Add the donation id in side the array. |
| 236 |
$donation_ids[] = $post->ID; |
| 237 |
|
| 238 |
$donor_id = (int) give_get_meta( $post->ID, '_give_payment_customer_id', true ); |
| 239 |
if ( ! empty( $donor_id ) ) { |
| 240 |
$donor = new Give_Donor( $donor_id ); |
| 241 |
if ( ! empty( $donor->id ) ) { |
| 242 |
if ( empty( $donor->user_id ) && ! empty( $donor->payment_ids ) ) { |
| 243 |
$add_author = false; |
| 244 |
$count = (int) count( $donor->payment_ids ); |
| 245 |
if ( 1 === $count ) { |
| 246 |
give_delete_donor_and_related_donation( $donor ); |
| 247 |
} else { |
| 248 |
$donor->remove_payment( $post->ID ); |
| 249 |
$donor->decrease_donation_count(); |
| 250 |
} |
| 251 |
} |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
if ( ! empty( $add_author ) ) { |
| 256 |
// Add the donor id in side the array. |
| 257 |
$donor_ids[] = (int) $post->post_author; |
| 258 |
} |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
// Get the total number of post found. |
| 263 |
$total_donation = (int) $donation_posts->found_posts; |
| 264 |
|
| 265 |
// Maximum number of page can be display. |
| 266 |
$max_num_pages = (int) $donation_posts->max_num_pages; |
| 267 |
|
| 268 |
// Check current page is less then max number of page or not. |
| 269 |
if ( $paged < $max_num_pages ) { |
| 270 |
|
| 271 |
// Update the current page variable for the next step. |
| 272 |
$this->update_option( $this->step_on_key, $paged ); |
| 273 |
|
| 274 |
// Calculating percentage. |
| 275 |
$page_remain = $max_num_pages - $paged; |
| 276 |
$this->total_step = (int) $max_num_pages + ( $total_donation / $this->per_step ) + ( ( $page_remain * 2 ) * count( $donor_ids ) ); |
| 277 |
$this->step_completed = $paged; |
| 278 |
} else { |
| 279 |
$donation_ids_count = count( $donor_ids ); |
| 280 |
$this->update_option( $this->step_key, 'donation' ); |
| 281 |
$this->update_option( $this->step_on_key, '0' ); |
| 282 |
} |
| 283 |
|
| 284 |
$donor_ids = array_unique( $donor_ids ); |
| 285 |
$this->update_option( $this->donor_key, $donor_ids ); |
| 286 |
$this->update_option( $this->donation_key, $donation_ids ); |
| 287 |
|
| 288 |
wp_reset_postdata(); |
| 289 |
} |
| 290 |
|
| 291 |
/** |
| 292 |
* Return the calculated completion percentage. |
| 293 |
* |
| 294 |
* @since 1.8.12 |
| 295 |
* |
| 296 |
* @return int |
| 297 |
*/ |
| 298 |
public function get_percentage_complete() { |
| 299 |
return ceil( ( 100 * $this->step_completed ) / $this->total_step ); |
| 300 |
} |
| 301 |
|
| 302 |
/** |
| 303 |
* Process Steps |
| 304 |
* |
| 305 |
* @return bool |
| 306 |
*/ |
| 307 |
public function process_step() { |
| 308 |
|
| 309 |
if ( ! $this->can_export() ) { |
| 310 |
wp_die( |
| 311 |
esc_html__( 'You do not have permission to delete Import transactions.', 'give' ), |
| 312 |
esc_html__( 'Error', 'give' ), |
| 313 |
array( |
| 314 |
'response' => 403, |
| 315 |
) |
| 316 |
); |
| 317 |
} |
| 318 |
|
| 319 |
$had_data = $this->get_data(); |
| 320 |
|
| 321 |
if ( $had_data ) { |
| 322 |
$this->done = false; |
| 323 |
|
| 324 |
return true; |
| 325 |
} else { |
| 326 |
update_option( 'give_earnings_total', give_get_total_earnings( true ), false ); |
| 327 |
Give_Cache::delete( Give_Cache::get_key( 'give_estimated_monthly_stats' ) ); |
| 328 |
|
| 329 |
$this->delete_option( $this->donation_key ); |
| 330 |
|
| 331 |
$this->done = true; |
| 332 |
$this->message = __( 'Imported donor and transactions successfully deleted.', 'give' ); |
| 333 |
|
| 334 |
return false; |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
/** |
| 339 |
* Get the Export Data |
| 340 |
* |
| 341 |
* @access public |
| 342 |
* @since 1.8.12 |
| 343 |
* @global object $wpdb Used to query the database using the WordPress Database API |
| 344 |
* |
| 345 |
* @return array|bool $data The data for the CSV file |
| 346 |
*/ |
| 347 |
public function get_data() { |
| 348 |
|
| 349 |
// Get the donation id's. |
| 350 |
$donation_ids = $this->get_option( $this->donation_key ); |
| 351 |
|
| 352 |
/** |
| 353 |
* Return false id not Import donation is found. |
| 354 |
*/ |
| 355 |
if ( empty( $donation_ids ) ) { |
| 356 |
$this->is_empty = true; |
| 357 |
$this->total_step = 1; |
| 358 |
|
| 359 |
return false; |
| 360 |
} |
| 361 |
|
| 362 |
// Get the current step. |
| 363 |
$step = (int) $this->get_step(); |
| 364 |
|
| 365 |
// Get the donor ids. |
| 366 |
$donor_ids = $this->get_option( $this->donor_key ); |
| 367 |
|
| 368 |
// Delete all the imported donations. |
| 369 |
if ( 2 === $step ) { |
| 370 |
$pass_to_donor = false; |
| 371 |
$page = (int) $this->get_step_page(); |
| 372 |
$page ++; |
| 373 |
$count = count( $donation_ids ); |
| 374 |
|
| 375 |
$this->total_step = ( ( count( $donation_ids ) / $this->per_step ) * 2 ) + count( $donor_ids ); |
| 376 |
$this->step_completed = $page; |
| 377 |
|
| 378 |
if ( $count > $this->per_step ) { |
| 379 |
|
| 380 |
$this->update_option( $this->step_on_key, $page ); |
| 381 |
$donation_ids = $this->get_delete_ids( $donation_ids, $page ); |
| 382 |
$current_page = (int) ceil( $count / $this->per_step ); |
| 383 |
|
| 384 |
if ( $page === $current_page ) { |
| 385 |
$pass_to_donor = true; |
| 386 |
} |
| 387 |
} else { |
| 388 |
$pass_to_donor = true; |
| 389 |
} |
| 390 |
|
| 391 |
if ( true === $pass_to_donor ) { |
| 392 |
$this->update_option( $this->step_key, 'donor' ); |
| 393 |
$this->update_option( $this->step_on_key, '0' ); |
| 394 |
} |
| 395 |
|
| 396 |
// Get the old form list. |
| 397 |
$form_ids = (array) $this->get_option( $this->form_key ); |
| 398 |
|
| 399 |
foreach ( $donation_ids as $item ) { |
| 400 |
$form_ids[] = give_get_meta( $item, '_give_payment_form_id', true ); |
| 401 |
|
| 402 |
// Delete the main payment. |
| 403 |
give_delete_donation( absint( $item ) ); |
| 404 |
} |
| 405 |
|
| 406 |
// Update the new form list. |
| 407 |
$this->update_option( $this->form_key, $form_ids ); |
| 408 |
} // End if(). |
| 409 |
|
| 410 |
// Delete all the donors. |
| 411 |
if ( 3 === $step ) { |
| 412 |
|
| 413 |
// Get the old form list. |
| 414 |
$form_ids = (array) $this->get_option( $this->form_key ); |
| 415 |
if ( ! empty( $form_ids ) ) { |
| 416 |
$form_ids = array_unique( $form_ids ); |
| 417 |
foreach ( $form_ids as $form_id ) { |
| 418 |
give_recount_form_income_donation( (int) $form_id ); |
| 419 |
} |
| 420 |
} |
| 421 |
// update the new form list. |
| 422 |
$this->update_option( $this->form_key, array() ); |
| 423 |
|
| 424 |
$page = (int) $this->get_step_page(); |
| 425 |
$count = count( $donor_ids ); |
| 426 |
|
| 427 |
$this->total_step = ( ( count( $donation_ids ) / $this->per_step ) * 2 ) + count( $donor_ids ); |
| 428 |
$this->step_completed = $page + ( count( $donation_ids ) / $this->per_step ); |
| 429 |
|
| 430 |
if ( ! empty( $donor_ids[ $page ] ) ) { |
| 431 |
$args = apply_filters( |
| 432 |
'give_tools_reset_stats_total_args', |
| 433 |
array( |
| 434 |
'post_status' => 'any', |
| 435 |
'posts_per_page' => 1, |
| 436 |
'author' => $donor_ids[ $page ], |
| 437 |
) |
| 438 |
); |
| 439 |
|
| 440 |
$donations = array(); |
| 441 |
$payments = new Give_Payments_Query( $args ); |
| 442 |
$payments = $payments->get_payments(); |
| 443 |
if ( empty( $payments ) ) { |
| 444 |
Give()->donors->delete_by_user_id( $donor_ids[ $page ] ); |
| 445 |
|
| 446 |
/** |
| 447 |
* If Checked then delete WP user. |
| 448 |
* |
| 449 |
* @since 1.8.14 |
| 450 |
*/ |
| 451 |
$delete_import_donors = isset( $_REQUEST['delete-import-donors'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['delete-import-donors'] ) ) : ''; |
| 452 |
|
| 453 |
if ( 'on' === (string) $delete_import_donors ) { |
| 454 |
wp_delete_user( $donor_ids[ $page ] ); |
| 455 |
} |
| 456 |
} else { |
| 457 |
foreach ( $payments as $payment ) { |
| 458 |
$donations[] = $payment->ID; |
| 459 |
} |
| 460 |
|
| 461 |
$donor = new Give_Donor( $donor_ids[ $page ], true ); |
| 462 |
$data_to_update = array( |
| 463 |
'purchase_count' => count( $donations ), |
| 464 |
'payment_ids' => implode( ',', $donations ), |
| 465 |
); |
| 466 |
$donor->update( $data_to_update ); |
| 467 |
} |
| 468 |
} // End if(). |
| 469 |
|
| 470 |
$page ++; |
| 471 |
$this->update_option( $this->step_on_key, $page ); |
| 472 |
if ( $count === $page ) { |
| 473 |
$this->is_empty = false; |
| 474 |
|
| 475 |
return false; |
| 476 |
} |
| 477 |
|
| 478 |
return true; |
| 479 |
} // End if(). |
| 480 |
|
| 481 |
return true; |
| 482 |
} |
| 483 |
|
| 484 |
/** |
| 485 |
* This function will get list of donation ids ready for deletion. |
| 486 |
* |
| 487 |
* @param array $donation_ids List of donation ids. |
| 488 |
* @param string $page Ajax on Page. |
| 489 |
* |
| 490 |
* @return mixed |
| 491 |
*/ |
| 492 |
public function get_delete_ids( $donation_ids, $page ) { |
| 493 |
$index = $page --; |
| 494 |
$count = count( $donation_ids ); |
| 495 |
$temp = 0; |
| 496 |
$current_page = 0; |
| 497 |
$post_delete = $this->per_step; |
| 498 |
$page_donation_id = array(); |
| 499 |
|
| 500 |
foreach ( $donation_ids as $item ) { |
| 501 |
$temp ++; |
| 502 |
$page_donation_id[ $current_page ][] = $item; |
| 503 |
if ( $temp === $post_delete ) { |
| 504 |
$current_page ++; |
| 505 |
$temp = 0; |
| 506 |
} |
| 507 |
} |
| 508 |
|
| 509 |
return $page_donation_id[ $page ]; |
| 510 |
} |
| 511 |
|
| 512 |
/** |
| 513 |
* Given a key, get the information from the Database Directly |
| 514 |
* |
| 515 |
* @since 1.8.12 |
| 516 |
* |
| 517 |
* @param string $key Option Key. |
| 518 |
* @param bool $default_value True, if default value, else false. |
| 519 |
* |
| 520 |
* @return mixed Returns the data from the database |
| 521 |
*/ |
| 522 |
public function get_option( $key, $default_value = false ) { |
| 523 |
return get_option( $key, $default_value ); |
| 524 |
} |
| 525 |
|
| 526 |
/** |
| 527 |
* Give a key, store the value |
| 528 |
* |
| 529 |
* @since 1.8.12 |
| 530 |
* |
| 531 |
* @param string $key Option Key. |
| 532 |
* @param mixed $value Option Value. |
| 533 |
* |
| 534 |
* @return void |
| 535 |
*/ |
| 536 |
public function update_option( $key, $value ) { |
| 537 |
update_option( $key, $value, false ); |
| 538 |
} |
| 539 |
|
| 540 |
/** |
| 541 |
* Delete an option |
| 542 |
* |
| 543 |
* @since 1.8.12 |
| 544 |
* |
| 545 |
* @param string $key Option Key. |
| 546 |
* |
| 547 |
* @return void |
| 548 |
*/ |
| 549 |
public function delete_option( $key ) { |
| 550 |
delete_option( $key ); |
| 551 |
} |
| 552 |
|
| 553 |
/** |
| 554 |
* Get the current step in number. |
| 555 |
* |
| 556 |
* There are three step to delete the total donor first counting, second deleting donotion and third deleting donors. |
| 557 |
* |
| 558 |
* @return int|string |
| 559 |
*/ |
| 560 |
private function get_step() { |
| 561 |
$step_key = (string) $this->get_option( $this->step_key, false ); |
| 562 |
if ( 'count' === $step_key ) { |
| 563 |
return 1; |
| 564 |
} elseif ( 'donation' === $step_key ) { |
| 565 |
return 2; |
| 566 |
} elseif ( 'donor' === $step_key ) { |
| 567 |
return 3; |
| 568 |
} else { |
| 569 |
return $step_key; |
| 570 |
} |
| 571 |
} |
| 572 |
|
| 573 |
/** |
| 574 |
* Get the current $page value in the ajax. |
| 575 |
*/ |
| 576 |
private function get_step_page() { |
| 577 |
return $this->get_option( $this->step_on_key, false ); |
| 578 |
} |
| 579 |
|
| 580 |
/** |
| 581 |
* Unset the properties specific to the donors export. |
| 582 |
* |
| 583 |
* @since 2.3.0 |
| 584 |
* |
| 585 |
* @param array $request |
| 586 |
* @param Give_Batch_Export $export |
| 587 |
*/ |
| 588 |
public function unset_properties( $request, $export ) { |
| 589 |
if ( $export->done ) { |
| 590 |
// Delete all the form ids. |
| 591 |
$this->delete_option( $this->form_key ); |
| 592 |
|
| 593 |
// Delete all the donation ids. |
| 594 |
$this->delete_option( $this->donation_key ); |
| 595 |
|
| 596 |
// Delete all the donor ids. |
| 597 |
$this->delete_option( $this->donor_key ); |
| 598 |
|
| 599 |
// Delete all the step and set to 'count' which if the first step in the process of deleting the donors. |
| 600 |
$this->delete_option( $this->step_key ); |
| 601 |
|
| 602 |
// Delete tha page count of the step. |
| 603 |
$this->delete_option( $this->step_on_key ); |
| 604 |
} |
| 605 |
} |
| 606 |
} |
| 607 |
|