PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 3.14.2
GiveWP – Donation Plugin and Fundraising Platform v3.14.2
4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 2.31.0 2.31.1 2.32.0 2.33.0 2.33.1 2.33.2 2.33.3 2.33.4 2.33.5 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.5.0 2.5.1 2.5.10 2.5.11 2.5.12 2.5.13 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.5.8 2.5.9 2.6.0 2.6.1 2.6.2 2.6.3 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.9.0 2.9.1 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.10.0 3.11.0 3.12.0 3.12.1 3.12.2 3.12.3 3.13.0 3.14.0 3.14.1 3.14.2 3.15.0 3.15.1 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.17.0 3.17.1 3.17.2 3.18.0 3.19.0 3.19.1 3.19.2 3.19.3 3.19.4 3.2.0 3.2.1 3.2.2 3.20.0 3.21.0 3.21.1 3.22.0 3.22.1 3.22.2 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.7.0 3.8.0 3.9.0 4.0.0 4.1.0 4.1.1 4.10.0 4.10.1 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.14.5 4.14.6 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.1 4.7.0 4.7.1 4.8.0 4.8.1 4.9.0 trunk 1.9.0 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.2 2.11.3 2.12.0 2.12.1 2.12.2 2.12.3 2.13.0 2.13.1 2.13.2 2.13.3 2.13.4 2.14.0 2.15.0 2.16.0 2.16.1 2.17.0 2.17.1 2.17.3 2.18.0 2.18.1 2.19.1 2.19.2 2.19.3 2.19.4 2.19.5 2.19.6 2.19.7 2.19.8 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.20.0 2.20.1 2.20.2 2.21.0 2.21.1 2.21.2 2.21.3 2.21.4 2.22.0 2.22.1 2.22.2 2.22.3 2.23.0 2.23.1 2.23.2 2.24.0 2.24.1 2.24.2 2.25.0 2.25.1 2.25.2 2.25.3 2.26.0 2.27.0 2.27.1 2.27.2 2.27.3 2.28.0 2.29.0 2.29.1 2.29.2
give / includes / admin / tools / data / class-give-tools-delete-donations.php
give / includes / admin / tools / data Last commit date
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-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