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