PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.0.1
GiveWP – Donation Plugin and Fundraising Platform v2.0.1
4.16.3 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-reset-stats.php
give / includes / admin / tools / data Last commit date
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-reset-stats.php
362 lines
1 <?php
2 /**
3 * Recount income and stats
4 *
5 * This class handles batch processing of resetting donations and income stats.
6 *
7 * @subpackage Admin/Tools/Give_Tools_Reset_Stats
8 * @copyright Copyright (c) 2016, WordImpress
9 * @license https://opensource.org/licenses/gpl-license GNU Public License
10 * @since 1.5
11 */
12
13 // Exit if accessed directly.
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit;
16 }
17
18 /**
19 * Give_Tools_Reset_Stats Class
20 *
21 * @since 1.5
22 */
23 class Give_Tools_Reset_Stats extends Give_Batch_Export {
24
25 /**
26 * Our export type. Used for export-type specific filters/actions
27 *
28 * @var string
29 * @since 1.5
30 */
31 public $export_type = '';
32
33 /**
34 * Allows for a non-form batch processing to be run.
35 *
36 * @since 1.5
37 * @var boolean
38 */
39 public $is_void = true;
40
41 /**
42 * Sets the number of items to pull on each step
43 *
44 * @since 1.5
45 * @var integer
46 */
47 public $per_step = 30;
48
49 /**
50 * Get the Export Data
51 *
52 * @access public
53 * @since 1.5
54 * @global object $wpdb Used to query the database using the WordPress
55 * Database API
56 * @return bool $data The data for the CSV file
57 */
58 public function get_data() {
59 global $wpdb;
60
61 $items = $this->get_stored_data( 'give_temp_reset_ids' );
62
63 if ( ! is_array( $items ) ) {
64 return false;
65 }
66
67 $offset = ( $this->step - 1 ) * $this->per_step;
68 $step_items = array_slice( $items, $offset, $this->per_step );
69
70 if ( $step_items ) {
71
72 $step_ids = array(
73 'customers' => array(),
74 'forms' => array(),
75 'other' => array(),
76 );
77
78 foreach ( $step_items as $item ) {
79
80 switch ( $item['type'] ) {
81 case 'customer':
82 $step_ids['customers'][] = $item['id'];
83 break;
84 case 'forms':
85 $step_ids['give_forms'][] = $item['id'];
86 break;
87 default:
88 $item_type = apply_filters( 'give_reset_item_type', 'other', $item );
89 $step_ids[ $item_type ][] = $item['id'];
90 break;
91 }
92 }
93
94 $sql = array();
95 $meta_table = __give_v20_bc_table_details('form' );
96
97 foreach ( $step_ids as $type => $ids ) {
98
99 if ( empty( $ids ) ) {
100 continue;
101 }
102
103 $ids = implode( ',', $ids );
104
105 switch ( $type ) {
106 case 'customers':
107 $sql[] = "DELETE FROM $wpdb->donors WHERE id IN ($ids)";
108 $table_name = $wpdb->prefix . 'give_customers';
109 $meta_table_name = $wpdb->prefix . 'give_customermeta';
110 $sql[] = "DELETE FROM $table_name WHERE id IN ($ids)";
111 $sql[] = "DELETE FROM $meta_table_name WHERE customer_id IN ($ids)";
112 break;
113 case 'forms':
114 $sql[] = "UPDATE {$meta_table['name']} SET meta_value = 0 WHERE meta_key = '_give_form_sales' AND {$meta_table['column']['id']} IN ($ids)";
115 $sql[] = "UPDATE {$meta_table['name']} SET meta_value = 0.00 WHERE meta_key = '_give_form_earnings' AND {$meta_table['column']['id']} IN ($ids)";
116 break;
117 case 'other':
118 $sql[] = "DELETE FROM $wpdb->posts WHERE id IN ($ids)";
119 $sql[] = "DELETE FROM $wpdb->postmeta WHERE post_id IN ($ids)";
120 $sql[] = "DELETE FROM $wpdb->comments WHERE comment_post_ID IN ($ids)";
121 $sql[] = "DELETE FROM $wpdb->commentmeta WHERE comment_id NOT IN (SELECT comment_ID FROM $wpdb->comments)";
122 break;
123 }
124
125 if ( ! in_array( $type, array( 'customers', 'forms', 'other' ) ) ) {
126 // Allows other types of custom post types to filter on their own post_type
127 // and add items to the query list, for the IDs found in their post type.
128 $sql = apply_filters( "give_reset_add_queries_{$type}", $sql, $ids );
129 }
130 }
131
132 if ( ! empty( $sql ) ) {
133 foreach ( $sql as $query ) {
134 $wpdb->query( $query );
135 }
136 }
137
138 return true;
139
140 }// End if().
141
142 return false;
143
144 }
145
146 /**
147 * Return the calculated completion percentage.
148 *
149 * @since 1.5
150 * @return int
151 */
152 public function get_percentage_complete() {
153
154 $items = $this->get_stored_data( 'give_temp_reset_ids' );
155 $total = count( $items );
156
157 $percentage = 100;
158
159 if ( $total > 0 ) {
160 $percentage = ( ( $this->per_step * $this->step ) / $total ) * 100;
161 }
162
163 if ( $percentage > 100 ) {
164 $percentage = 100;
165 }
166
167 return $percentage;
168 }
169
170 /**
171 * Set the properties specific to the payments export.
172 *
173 * @since 1.5
174 *
175 * @param array $request The Form Data passed into the batch processing.
176 */
177 public function set_properties( $request ) {
178 }
179
180 /**
181 * Process a step
182 *
183 * @since 1.5
184 * @return bool
185 */
186 public function process_step() {
187
188 if ( ! $this->can_export() ) {
189 wp_die( esc_html__( 'You do not have permission to reset data.', 'give' ), esc_html__( 'Error', 'give' ), array(
190 'response' => 403,
191 ) );
192 }
193
194 $had_data = $this->get_data();
195
196 if ( $had_data ) {
197 $this->done = false;
198
199 return true;
200 } else {
201 update_option( 'give_earnings_total', 0 );
202 Give_Cache::delete( Give_Cache::get_key( 'give_estimated_monthly_stats' ) );
203
204 $this->delete_data( 'give_temp_reset_ids' );
205
206 // Reset the sequential order numbers
207 if ( give_get_option( 'enable_sequential' ) ) {
208 delete_option( 'give_last_payment_number' );
209 }
210
211 $this->done = true;
212 $this->message = esc_html__( 'Donation forms, income, donations counts, and logs successfully reset.', 'give' );
213
214 return false;
215 }
216 }
217
218 /**
219 * Headers
220 */
221 public function headers() {
222 give_ignore_user_abort();
223 }
224
225 /**
226 * Perform the export
227 *
228 * @access public
229 * @since 1.5
230 * @return void
231 */
232 public function export() {
233
234 // Set headers
235 $this->headers();
236
237 give_die();
238 }
239
240 /**
241 * Pre Fetch
242 */
243 public function pre_fetch() {
244
245 if ( $this->step == 1 ) {
246 $this->delete_data( 'give_temp_reset_ids' );
247 }
248
249 $items = get_option( 'give_temp_reset_ids', false );
250
251 if ( false === $items ) {
252 $items = array();
253
254 $give_types_for_reset = array( 'give_forms', 'give_payment' );
255 $give_types_for_reset = apply_filters( 'give_reset_store_post_types', $give_types_for_reset );
256
257 $args = apply_filters( 'give_tools_reset_stats_total_args', array(
258 'post_type' => $give_types_for_reset,
259 'post_status' => 'any',
260 'posts_per_page' => - 1,
261 ) );
262
263 $posts = get_posts( $args );
264 foreach ( $posts as $post ) {
265 $items[] = array(
266 'id' => (int) $post->ID,
267 'type' => $post->post_type,
268 );
269 }
270
271 $donor_args = array(
272 'number' => - 1,
273 );
274 $donors = Give()->donors->get_donors( $donor_args );
275 foreach ( $donors as $donor ) {
276 $items[] = array(
277 'id' => (int) $donor->id,
278 'type' => 'customer',
279 );
280 }
281
282 // Allow filtering of items to remove with an unassociative array for each item
283 // The array contains the unique ID of the item, and a 'type' for you to use in the execution of the get_data method
284 $items = apply_filters( 'give_reset_items', $items );
285
286 $this->store_data( 'give_temp_reset_ids', $items );
287 }// End if().
288
289 }
290
291 /**
292 * Given a key, get the information from the Database Directly.
293 *
294 * @since 1.5
295 *
296 * @param string $key The option_name
297 *
298 * @return mixed Returns the data from the database.
299 */
300 private function get_stored_data( $key ) {
301 global $wpdb;
302 $value = $wpdb->get_var( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = '%s'", $key ) );
303
304 if ( empty( $value ) ) {
305 return false;
306 }
307
308 $maybe_json = json_decode( $value );
309 if ( ! is_null( $maybe_json ) ) {
310 $value = json_decode( $value, true );
311 }
312
313 return $value;
314 }
315
316 /**
317 * Give a key, store the value.
318 *
319 * @since 1.5
320 *
321 * @param string $key The option_name.
322 * @param mixed $value The value to store.
323 *
324 * @return void
325 */
326 private function store_data( $key, $value ) {
327 global $wpdb;
328
329 $value = is_array( $value ) ? wp_json_encode( $value ) : esc_attr( $value );
330
331 $data = array(
332 'option_name' => $key,
333 'option_value' => $value,
334 'autoload' => 'no',
335 );
336
337 $formats = array(
338 '%s',
339 '%s',
340 '%s',
341 );
342
343 $wpdb->replace( $wpdb->options, $data, $formats );
344 }
345
346 /**
347 * Delete an option
348 *
349 * @since 1.5
350 *
351 * @param string $key The option_name to delete
352 *
353 * @return void
354 */
355 private function delete_data( $key ) {
356 global $wpdb;
357 $wpdb->delete( $wpdb->options, array(
358 'option_name' => $key,
359 ) );
360 }
361
362 }