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
6 years ago
class-give-tools-recount-income.php
6 years ago
class-give-tools-recount-single-donor-stats.php
6 years ago
class-give-tools-reset-stats.php
6 years ago
tools-actions.php
7 years ago
class-give-tools-delete-donations.php
349 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Delete Donations between a date range. |
| 4 | * |
| 5 | * This class handles batch processing of deleting donations between a given range. |
| 6 | * |
| 7 | * @package Admin/Tools |
| 8 | * @subpackage Admin/Tools/Give_Tools_Delete_Donations |
| 9 | * @copyright Copyright (c) 2016, GiveWP |
| 10 | * @license https://opensource.org/licenses/gpl-license GNU Public License |
| 11 | * @since 2.3.0 |
| 12 | */ |
| 13 | |
| 14 | // Exit if accessed directly. |
| 15 | if ( ! defined( 'ABSPATH' ) ) { |
| 16 | exit; |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Give_Tools_Delete_Donations Class |
| 21 | * |
| 22 | * @since 2.3.0 |
| 23 | */ |
| 24 | class Give_Tools_Delete_Donations extends Give_Batch_Export { |
| 25 | |
| 26 | /** |
| 27 | * Our export type. Used for export-type specific filters/actions. |
| 28 | * |
| 29 | * @var string |
| 30 | * @since 2.3.0 |
| 31 | */ |
| 32 | public $export_type = ''; |
| 33 | |
| 34 | /** |
| 35 | * Allows for a non-form batch processing to be run. |
| 36 | * |
| 37 | * @since 2.3.0 |
| 38 | * @var boolean |
| 39 | */ |
| 40 | public $is_void = true; |
| 41 | |
| 42 | /** |
| 43 | * Sets the number of items to pull on each step. |
| 44 | * |
| 45 | * @since 2.3.0 |
| 46 | * @var integer |
| 47 | */ |
| 48 | public $per_step = 30; |
| 49 | |
| 50 | /** |
| 51 | * Set the start date of donation. |
| 52 | * |
| 53 | * @since 2.3.0 |
| 54 | * @var string |
| 55 | */ |
| 56 | public $start_date = ''; |
| 57 | |
| 58 | /** |
| 59 | * Set the end date of donation. |
| 60 | * |
| 61 | * @since 2.3.0 |
| 62 | * @var string |
| 63 | */ |
| 64 | public $end_date = ''; |
| 65 | |
| 66 | /** |
| 67 | * Constructor. |
| 68 | * |
| 69 | * @param number $_step Step ID of the currenct batch. |
| 70 | */ |
| 71 | public function __construct( $_step = 1 ) { |
| 72 | parent::__construct( $_step ); |
| 73 | |
| 74 | $this->is_writable = true; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Get the Export Data |
| 79 | * |
| 80 | * @access public |
| 81 | * @since 2.3.0 |
| 82 | * @global object $wpdb Used to query the database using the WordPress Database API |
| 83 | * |
| 84 | * @return array|bool $data The data for the CSV file |
| 85 | */ |
| 86 | public function get_data() { |
| 87 | $items = $this->get_stored_data( 'give_temp_delete_donation_ids' ); |
| 88 | |
| 89 | if ( ! is_array( $items ) ) { |
| 90 | return false; |
| 91 | } |
| 92 | |
| 93 | $offset = ( $this->step - 1 ) * $this->per_step; |
| 94 | $step_items = array_slice( $items, $offset, $this->per_step ); |
| 95 | |
| 96 | if ( $step_items ) { |
| 97 | foreach ( $step_items as $item ) { |
| 98 | // Delete the main payment. |
| 99 | give_delete_donation( absint( $item['id'] ) ); |
| 100 | } |
| 101 | return true; |
| 102 | } |
| 103 | |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Return the calculated completion percentage |
| 109 | * |
| 110 | * @since 2.3.0 |
| 111 | * @return int |
| 112 | */ |
| 113 | public function get_percentage_complete() { |
| 114 | |
| 115 | $items = $this->get_stored_data( 'give_temp_delete_donation_ids' ); |
| 116 | |
| 117 | if ( ! is_array( $items ) ) { |
| 118 | return 100; |
| 119 | } |
| 120 | |
| 121 | $total = count( $items ); |
| 122 | |
| 123 | $percentage = 100; |
| 124 | |
| 125 | if ( $total > 0 ) { |
| 126 | $percentage = ( ( $this->per_step * $this->step ) / $total ) * 100; |
| 127 | } |
| 128 | |
| 129 | if ( $percentage > 100 ) { |
| 130 | $percentage = 100; |
| 131 | } |
| 132 | |
| 133 | return $percentage; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Set the properties specific to the payments export |
| 138 | * |
| 139 | * @since 2.3.0 |
| 140 | * |
| 141 | * @param array $request The Form Data passed into the batch processing. |
| 142 | */ |
| 143 | public function set_properties( $request ) { |
| 144 | $this->start_date = isset( $request['delete_donations_start_date'] ) ? sanitize_text_field( $request['delete_donations_start_date'] ) : false; |
| 145 | $this->end_date = isset( $request['delete_donations_end_date'] ) ? sanitize_text_field( $request['delete_donations_end_date'] ) : false; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Process a step |
| 150 | * |
| 151 | * @since 2.3.0 |
| 152 | * @return bool |
| 153 | */ |
| 154 | public function process_step() { |
| 155 | if ( ! $this->can_export() ) { |
| 156 | wp_die( esc_html__( 'You do not have permission to delete donations.', 'give' ), esc_html__( 'Error', 'give' ), array( 'response' => 403 ) ); |
| 157 | } |
| 158 | |
| 159 | $had_data = $this->get_data(); |
| 160 | |
| 161 | if ( $had_data ) { |
| 162 | $this->done = false; |
| 163 | |
| 164 | return true; |
| 165 | } else { |
| 166 | update_option( 'give_earnings_total', give_get_total_earnings( true ), false ); |
| 167 | Give_Cache::delete( Give_Cache::get_key( 'give_estimated_monthly_stats' ) ); |
| 168 | |
| 169 | $this->delete_data( 'give_temp_delete_donation_ids' ); |
| 170 | |
| 171 | $this->done = true; |
| 172 | |
| 173 | $donation_count = get_option( 'give_temp_delete_donation_count', 0 ); |
| 174 | |
| 175 | $this->message = sprintf( '%1$s %2$s', $donation_count, _n( 'donation successfully deleted.', 'donations successfully deleted.', $donation_count, 'give' ) ); |
| 176 | |
| 177 | delete_option( 'give_temp_delete_donation_count' ); |
| 178 | |
| 179 | return false; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Headers |
| 185 | */ |
| 186 | public function headers() { |
| 187 | give_ignore_user_abort(); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Perform the export |
| 192 | * |
| 193 | * @access public |
| 194 | * @since 2.3.0 |
| 195 | * @return void |
| 196 | */ |
| 197 | public function export() { |
| 198 | |
| 199 | // Set headers. |
| 200 | $this->headers(); |
| 201 | |
| 202 | give_die(); |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Pre Fetch |
| 207 | */ |
| 208 | public function pre_fetch() { |
| 209 | |
| 210 | if ( '1' === $this->step ) { |
| 211 | $this->delete_data( 'give_temp_delete_donation_ids' ); |
| 212 | } |
| 213 | |
| 214 | $items = get_option( 'give_temp_delete_donation_ids', false ); |
| 215 | |
| 216 | if ( false === $items ) { |
| 217 | $items = array(); |
| 218 | |
| 219 | /** |
| 220 | * This filter can be used to modify the args supplied to |
| 221 | * Give_Payments_Query. |
| 222 | * |
| 223 | * @since 2.3.0 |
| 224 | */ |
| 225 | $args = apply_filters( |
| 226 | 'give_tools_delete_donations_only_args', |
| 227 | array( |
| 228 | 'post_status' => 'any', |
| 229 | 'number' => - 1, |
| 230 | 'start_date' => $this->start_date, |
| 231 | 'end_date' => $this->end_date, |
| 232 | ) |
| 233 | ); |
| 234 | |
| 235 | $posts = new Give_Payments_Query( $args ); |
| 236 | |
| 237 | $payments = $posts->get_payments(); |
| 238 | |
| 239 | if ( ! empty( $payments ) ) { |
| 240 | foreach ( $payments as $payment ) { |
| 241 | $items[] = array( |
| 242 | 'id' => (int) $payment->ID, |
| 243 | ); |
| 244 | } |
| 245 | |
| 246 | // Allow filtering of items to remove with an unassociative array for each item. |
| 247 | // The array contains the unique ID of the item, and a 'type' for you to use in the execution of the get_data method. |
| 248 | $items = apply_filters( 'give_delete_donation_items', $items ); |
| 249 | |
| 250 | $this->store_data( 'give_temp_delete_donation_ids', $items ); |
| 251 | |
| 252 | $donation_count = get_option( 'give_temp_delete_donation_count', 0 ); |
| 253 | |
| 254 | if ( 0 === $donation_count ) { |
| 255 | add_option( 'give_temp_delete_donation_count', count( $items ) ); |
| 256 | } else { |
| 257 | $donation_count += (int) $donation_count; |
| 258 | update_option( 'give_temp_delete_donation_count', $donation_count ); |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Given a key, get the information from the Database Directly |
| 266 | * |
| 267 | * @since 2.3.0 |
| 268 | * |
| 269 | * @param string $key The option_name. |
| 270 | * |
| 271 | * @return mixed Returns the data from the database |
| 272 | */ |
| 273 | private function get_stored_data( $key ) { |
| 274 | global $wpdb; |
| 275 | $value = $wpdb->get_var( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s", $key ) ); |
| 276 | |
| 277 | if ( empty( $value ) ) { |
| 278 | return false; |
| 279 | } |
| 280 | |
| 281 | $maybe_json = json_decode( $value ); |
| 282 | if ( ! is_null( $maybe_json ) ) { |
| 283 | $value = json_decode( $value, true ); |
| 284 | } |
| 285 | |
| 286 | return $value; |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * Give a key, store the value. |
| 291 | * |
| 292 | * @since 2.3.0 |
| 293 | * |
| 294 | * @param string $key The option_name. |
| 295 | * @param mixed $value The value to store. |
| 296 | * |
| 297 | * @return void |
| 298 | */ |
| 299 | private function store_data( $key, $value ) { |
| 300 | global $wpdb; |
| 301 | |
| 302 | $value = is_array( $value ) ? wp_json_encode( $value ) : esc_attr( $value ); |
| 303 | |
| 304 | $data = array( |
| 305 | 'option_name' => $key, |
| 306 | 'option_value' => $value, |
| 307 | 'autoload' => 'no', |
| 308 | ); |
| 309 | |
| 310 | $formats = array( |
| 311 | '%s', |
| 312 | '%s', |
| 313 | '%s', |
| 314 | ); |
| 315 | |
| 316 | $wpdb->replace( $wpdb->options, $data, $formats ); |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * Delete an option |
| 321 | * |
| 322 | * @since 2.3.0 |
| 323 | * |
| 324 | * @param string $key The option_name to delete. |
| 325 | * |
| 326 | * @return void |
| 327 | */ |
| 328 | private function delete_data( $key ) { |
| 329 | global $wpdb; |
| 330 | $wpdb->delete( $wpdb->options, array( 'option_name' => $key ) ); |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Unset the properties specific to the donors export. |
| 335 | * |
| 336 | * @since 2.3.0 |
| 337 | * |
| 338 | * @param array $request Form's REQUEST array. |
| 339 | * @param Give_Batch_Export $export Export object. |
| 340 | */ |
| 341 | public function unset_properties( $request, $export ) { |
| 342 | if ( $export->done ) { |
| 343 | |
| 344 | // Delete all the donation ids. |
| 345 | $this->delete_data( 'give_temp_delete_donation_ids' ); |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 |