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-test-donors.php
451 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_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_key' => '_give_payment_mode', |
| 174 | 'meta_value' => 'test', |
| 175 | ) ); |
| 176 | |
| 177 | // Reset the post data. |
| 178 | wp_reset_postdata(); |
| 179 | // Getting the new donation. |
| 180 | $donation_posts = new WP_Query( $args ); |
| 181 | |
| 182 | // The Loop. |
| 183 | if ( $donation_posts->have_posts() ) { |
| 184 | while ( $donation_posts->have_posts() ) { |
| 185 | $donation_posts->the_post(); |
| 186 | global $post; |
| 187 | // Add the donation id in side the array. |
| 188 | $donation_ids[] = $post->ID; |
| 189 | |
| 190 | // Add the donor id in side the array. |
| 191 | $donor_ids[] = (int) $post->post_author; |
| 192 | } |
| 193 | /* Restore original Post Data */ |
| 194 | } |
| 195 | |
| 196 | // Get the total number of post found. |
| 197 | $total_donation = (int) $donation_posts->found_posts; |
| 198 | |
| 199 | // Maximum number of page can be display |
| 200 | $max_num_pages = (int) $donation_posts->max_num_pages; |
| 201 | |
| 202 | // Check current page is less then max number of page or not |
| 203 | if ( $paged < $max_num_pages ) { |
| 204 | // Update the curretn page virable for the next step |
| 205 | $this->update_option( $this->step_on_key, $paged ); |
| 206 | |
| 207 | // Calculating percentage. |
| 208 | $page_remain = $max_num_pages - $paged; |
| 209 | $this->total_step = (int) $max_num_pages + ( $total_donation / $this->per_step ) + ( ( $page_remain * 2 ) * count( $donor_ids ) ); |
| 210 | $this->step_completed = $paged; |
| 211 | } else { |
| 212 | $donation_ids_count = count( $donor_ids ); |
| 213 | $this->update_option( $this->step_key, 'donation' ); |
| 214 | $this->update_option( $this->step_on_key, '0' ); |
| 215 | } |
| 216 | |
| 217 | $donor_ids = array_unique( $donor_ids ); |
| 218 | $this->update_option( $this->donor_key, $donor_ids ); |
| 219 | $this->update_option( $this->donation_key, $donation_ids ); |
| 220 | |
| 221 | wp_reset_postdata(); |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Return the calculated completion percentage. |
| 226 | * |
| 227 | * @since 1.8.12 |
| 228 | * @return int |
| 229 | */ |
| 230 | public function get_percentage_complete() { |
| 231 | return ceil( ( 100 * $this->step_completed ) / $this->total_step ); |
| 232 | } |
| 233 | |
| 234 | public function process_step() { |
| 235 | |
| 236 | if ( ! $this->can_export() ) { |
| 237 | wp_die( |
| 238 | esc_html__( 'You do not have permission to delete test transactions.', 'give' ), |
| 239 | esc_html__( 'Error', 'give' ), |
| 240 | array( 'response' => 403 ) |
| 241 | ); |
| 242 | } |
| 243 | |
| 244 | $had_data = $this->get_data(); |
| 245 | |
| 246 | if ( $had_data ) { |
| 247 | $this->done = false; |
| 248 | |
| 249 | return true; |
| 250 | } else { |
| 251 | update_option( 'give_earnings_total', give_get_total_earnings( true ) ); |
| 252 | Give_Cache::delete( Give_Cache::get_key( 'give_estimated_monthly_stats' ) ); |
| 253 | |
| 254 | $this->delete_option( $this->donation_key ); |
| 255 | |
| 256 | $this->done = true; |
| 257 | $this->message = __( 'Test donor and transactions successfully deleted.', 'give' ); |
| 258 | |
| 259 | return false; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Get the Export Data |
| 265 | * |
| 266 | * @access public |
| 267 | * @since 1.8.12 |
| 268 | * @global object $wpdb Used to query the database using the WordPress Database API |
| 269 | * |
| 270 | * @return array|bool $data The data for the CSV file |
| 271 | */ |
| 272 | public function get_data() { |
| 273 | |
| 274 | // Get the donation id's. |
| 275 | $donation_ids = $this->get_option( $this->donation_key ); |
| 276 | |
| 277 | /** |
| 278 | * Return false id not test donation is found. |
| 279 | */ |
| 280 | if ( empty( $donation_ids ) ) { |
| 281 | $this->is_empty = true; |
| 282 | $this->total_step = 1; |
| 283 | |
| 284 | return false; |
| 285 | } |
| 286 | |
| 287 | // Get the current step. |
| 288 | $step = (int) $this->get_step(); |
| 289 | |
| 290 | // get teh donor ids. |
| 291 | $donor_ids = $this->get_option( $this->donor_key ); |
| 292 | |
| 293 | // In step to we delete all the donation in loop. |
| 294 | if ( 2 === $step ) { |
| 295 | $pass_to_donor = false; |
| 296 | $page = (int) $this->get_step_page(); |
| 297 | $page ++; |
| 298 | $count = count( $donation_ids ); |
| 299 | |
| 300 | $this->total_step = ( ( count( $donation_ids ) / $this->per_step ) * 2 ) + count( $donor_ids ); |
| 301 | $this->step_completed = $page; |
| 302 | |
| 303 | if ( $count > $this->per_step ) { |
| 304 | |
| 305 | $this->update_option( $this->step_on_key, $page ); |
| 306 | $donation_ids = $this->get_delete_ids( $donation_ids, $page ); |
| 307 | $current_page = (int) ceil( $count / $this->per_step ); |
| 308 | |
| 309 | if ( $page === $current_page ) { |
| 310 | $pass_to_donor = true; |
| 311 | } |
| 312 | } else { |
| 313 | $pass_to_donor = true; |
| 314 | } |
| 315 | |
| 316 | if ( true === $pass_to_donor ) { |
| 317 | $this->update_option( $this->step_key, 'donor' ); |
| 318 | $this->update_option( $this->step_on_key, '0' ); |
| 319 | } |
| 320 | |
| 321 | foreach ( $donation_ids as $item ) { |
| 322 | // Delete the main payment. |
| 323 | give_delete_donation( absint( $item ) ); |
| 324 | } |
| 325 | do_action( 'give_delete_log_cache' ); |
| 326 | } |
| 327 | |
| 328 | // Here we delete all the donor |
| 329 | if ( 3 === $step ) { |
| 330 | $page = (int) $this->get_step_page(); |
| 331 | $count = count( $donor_ids ); |
| 332 | |
| 333 | $this->total_step = ( ( count( $donation_ids ) / $this->per_step ) * 2 ) + count( $donor_ids ); |
| 334 | $this->step_completed = $page + ( count( $donation_ids ) / $this->per_step ); |
| 335 | |
| 336 | $args = apply_filters( 'give_tools_reset_stats_total_args', array( |
| 337 | 'post_type' => 'give_payment', |
| 338 | 'post_status' => 'any', |
| 339 | 'posts_per_page' => 1, |
| 340 | 'meta_key' => '_give_payment_mode', |
| 341 | 'meta_value' => 'live', |
| 342 | 'author' => $donor_ids[ $page ], |
| 343 | ) ); |
| 344 | |
| 345 | $donation_posts = get_posts( $args ); |
| 346 | if ( empty( $donation_posts ) ) { |
| 347 | Give()->donors->delete_by_user_id( $donor_ids[ $page ] ); |
| 348 | } |
| 349 | |
| 350 | $page ++; |
| 351 | $this->update_option( $this->step_on_key, $page ); |
| 352 | if ( $count === $page ) { |
| 353 | $this->is_empty = false; |
| 354 | |
| 355 | return false; |
| 356 | } |
| 357 | |
| 358 | return true; |
| 359 | } |
| 360 | |
| 361 | return true; |
| 362 | } |
| 363 | |
| 364 | public function get_delete_ids( $donation_ids, $page ) { |
| 365 | $index = $page --; |
| 366 | $count = count( $donation_ids ); |
| 367 | $temp = 0; |
| 368 | $current_page = 0; |
| 369 | $post_delete = $this->per_step; |
| 370 | $page_donation_id = array(); |
| 371 | |
| 372 | foreach ( $donation_ids as $item ) { |
| 373 | $temp ++; |
| 374 | $page_donation_id[ $current_page ][] = $item; |
| 375 | if ( $temp === $post_delete ) { |
| 376 | $current_page ++; |
| 377 | $temp = 0; |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | return $page_donation_id[ $page ]; |
| 382 | } |
| 383 | |
| 384 | /** |
| 385 | * Given a key, get the information from the Database Directly |
| 386 | * |
| 387 | * @since 1.8.13 |
| 388 | * |
| 389 | * @param string $key The option_name |
| 390 | * |
| 391 | * @return mixed Returns the data from the database |
| 392 | */ |
| 393 | public function get_option( $key, $defalut_value = false ) { |
| 394 | return get_option( $key, $defalut_value ); |
| 395 | } |
| 396 | |
| 397 | /** |
| 398 | * Give a key, store the value |
| 399 | * |
| 400 | * @since 1.8.12s |
| 401 | * |
| 402 | * @param string $key The option_name |
| 403 | * @param mixed $value The value to store |
| 404 | * |
| 405 | * @return void |
| 406 | */ |
| 407 | public function update_option( $key, $value ) { |
| 408 | update_option( $key, $value, false ); |
| 409 | } |
| 410 | |
| 411 | /** |
| 412 | * Delete an option |
| 413 | * |
| 414 | * @since 1.8.12 |
| 415 | * |
| 416 | * @param string $key The option_name to delete |
| 417 | * |
| 418 | * @return void |
| 419 | */ |
| 420 | public function delete_option( $key ) { |
| 421 | delete_option( $key ); |
| 422 | } |
| 423 | |
| 424 | /** |
| 425 | * Get the current step in number. |
| 426 | * |
| 427 | * There are three step to delete the total donor first counting, second deleting donotion and third deleting donors. |
| 428 | * |
| 429 | * @return int|string |
| 430 | */ |
| 431 | private function get_step() { |
| 432 | $step_key = (string) $this->get_option( $this->step_key, false ); |
| 433 | if ( 'count' === $step_key ) { |
| 434 | return 1; |
| 435 | } elseif ( 'donation' === $step_key ) { |
| 436 | return 2; |
| 437 | } elseif ( 'donor' === $step_key ) { |
| 438 | return 3; |
| 439 | } else { |
| 440 | return $step_key; |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | /** |
| 445 | * Get the current $page value in the ajax. |
| 446 | */ |
| 447 | private function get_step_page() { |
| 448 | return $this->get_option( $this->step_on_key, false ); |
| 449 | } |
| 450 | } |
| 451 |