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