Test Failed
Pull Request — master (#2551)
by Devin
04:51
created

Give_Tools_Reset_Stats::delete_data()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
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 ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $step_items of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
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
96
			foreach ( $step_ids as $type => $ids ) {
97
98
				if ( empty( $ids ) ) {
99
					continue;
100
				}
101
102
				$ids = implode( ',', $ids );
103
104
				switch ( $type ) {
105
					case 'customers':
106
						$table_name = $wpdb->prefix . 'give_customers';
107
						$meta_table_name = $wpdb->prefix . 'give_customermeta';
108
						$sql[]      = "DELETE FROM $table_name WHERE id IN ($ids)";
109
						$sql[]      = "DELETE FROM $meta_table_name WHERE customer_id IN ($ids)";
110
						break;
111
					case 'forms':
112
						$sql[] = "UPDATE $wpdb->postmeta SET meta_value = 0 WHERE meta_key = '_give_form_sales' AND post_id IN ($ids)";
113
						$sql[] = "UPDATE $wpdb->postmeta SET meta_value = 0.00 WHERE meta_key = '_give_form_earnings' AND post_id IN ($ids)";
114
						break;
115
					case 'other':
116
						$sql[] = "DELETE FROM $wpdb->posts WHERE id IN ($ids)";
117
						$sql[] = "DELETE FROM $wpdb->postmeta WHERE post_id IN ($ids)";
118
						$sql[] = "DELETE FROM $wpdb->comments WHERE comment_post_ID IN ($ids)";
119
						$sql[] = "DELETE FROM $wpdb->commentmeta WHERE comment_id NOT IN (SELECT comment_ID FROM $wpdb->comments)";
120
						break;
121
				}
122
123
				if ( ! in_array( $type, array( 'customers', 'forms', 'other' ) ) ) {
124
					// Allows other types of custom post types to filter on their own post_type
125
					// and add items to the query list, for the IDs found in their post type.
126
					$sql = apply_filters( "give_reset_add_queries_{$type}", $sql, $ids );
127
				}
128
			}
129
130
			if ( ! empty( $sql ) ) {
131
				foreach ( $sql as $query ) {
132
					$wpdb->query( $query );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
133
				}
134
			}
135
136
			return true;
137
138
		}// End if().
139
140
		return false;
141
142
	}
143
144
	/**
145
	 * Return the calculated completion percentage.
146
	 *
147
	 * @since 1.5
148
	 * @return int
149
	 */
150 View Code Duplication
	public function get_percentage_complete() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
151
152
		$items = $this->get_stored_data( 'give_temp_reset_ids' );
153
		$total = count( $items );
154
155
		$percentage = 100;
156
157
		if ( $total > 0 ) {
158
			$percentage = ( ( $this->per_step * $this->step ) / $total ) * 100;
159
		}
160
161
		if ( $percentage > 100 ) {
162
			$percentage = 100;
163
		}
164
165
		return $percentage;
166
	}
167
168
	/**
169
	 * Set the properties specific to the payments export.
170
	 *
171
	 * @since 1.5
172
	 *
173
	 * @param array $request The Form Data passed into the batch processing.
174
	 */
175
	public function set_properties( $request ) {
176
	}
177
178
	/**
179
	 * Process a step
180
	 *
181
	 * @since 1.5
182
	 * @return bool
183
	 */
184
	public function process_step() {
185
186
		if ( ! $this->can_export() ) {
187
			wp_die( esc_html__( 'You do not have permission to reset data.', 'give' ), esc_html__( 'Error', 'give' ), array(
188
				'response' => 403,
189
			) );
190
		}
191
192
		$had_data = $this->get_data();
193
194
		if ( $had_data ) {
195
			$this->done = false;
196
197
			return true;
198
		} else {
199
			update_option( 'give_earnings_total', 0 );
200
			Give_Cache::delete( Give_Cache::get_key( 'give_estimated_monthly_stats' ) );
201
202
			$this->delete_data( 'give_temp_reset_ids' );
203
204
			// Reset the sequential order numbers
205
			if ( give_get_option( 'enable_sequential' ) ) {
206
				delete_option( 'give_last_payment_number' );
207
			}
208
209
			$this->done    = true;
210
			$this->message = esc_html__( 'Donation forms, income, donations counts, and logs successfully reset.', 'give' );
211
212
			return false;
213
		}
214
	}
215
216
	/**
217
	 * Headers
218
	 */
219
	public function headers() {
220
		give_ignore_user_abort();
221
	}
222
223
	/**
224
	 * Perform the export
225
	 *
226
	 * @access public
227
	 * @since  1.5
228
	 * @return void
229
	 */
230
	public function export() {
231
232
		// Set headers
233
		$this->headers();
234
235
		give_die();
236
	}
237
238
	/**
239
	 * Pre Fetch
240
	 */
241
	public function pre_fetch() {
242
243
		if ( $this->step == 1 ) {
0 ignored issues
show
introduced by
Found "== 1". Use Yoda Condition checks, you must
Loading history...
244
			$this->delete_data( 'give_temp_reset_ids' );
245
		}
246
247
		$items = get_option( 'give_temp_reset_ids', false );
248
249
		if ( false === $items ) {
250
			$items = array();
251
252
			$give_types_for_reset = array( 'give_forms', 'give_payment' );
253
			$give_types_for_reset = apply_filters( 'give_reset_store_post_types', $give_types_for_reset );
254
255
			$args = apply_filters( 'give_tools_reset_stats_total_args', array(
256
				'post_type'      => $give_types_for_reset,
257
				'post_status'    => 'any',
258
				'posts_per_page' => - 1,
259
			) );
260
261
			$posts = get_posts( $args );
262
			foreach ( $posts as $post ) {
263
				$items[] = array(
264
					'id'   => (int) $post->ID,
265
					'type' => $post->post_type,
266
				);
267
			}
268
269
			$donor_args = array(
270
				'number' => - 1,
271
			);
272
			$donors     = Give()->donors->get_donors( $donor_args );
273
			foreach ( $donors as $donor ) {
0 ignored issues
show
Bug introduced by
The expression $donors of type array|object|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
274
				$items[] = array(
275
					'id'   => (int) $donor->id,
276
					'type' => 'customer',
277
				);
278
			}
279
280
			// Allow filtering of items to remove with an unassociative array for each item
281
			// The array contains the unique ID of the item, and a 'type' for you to use in the execution of the get_data method
282
			$items = apply_filters( 'give_reset_items', $items );
283
284
			$this->store_data( 'give_temp_reset_ids', $items );
285
		}// End if().
286
287
	}
288
289
	/**
290
	 * Given a key, get the information from the Database Directly.
291
	 *
292
	 * @since  1.5
293
	 *
294
	 * @param  string $key The option_name
295
	 *
296
	 * @return mixed       Returns the data from the database.
297
	 */
298 View Code Duplication
	private function get_stored_data( $key ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
299
		global $wpdb;
300
		$value = $wpdb->get_var( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = '%s'", $key ) );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
301
302
		if ( empty( $value ) ) {
303
			return false;
304
		}
305
306
		$maybe_json = json_decode( $value );
307
		if ( ! is_null( $maybe_json ) ) {
308
			$value = json_decode( $value, true );
309
		}
310
311
		return $value;
312
	}
313
314
	/**
315
	 * Give a key, store the value.
316
	 *
317
	 * @since  1.5
318
	 *
319
	 * @param  string $key   The option_name.
320
	 * @param  mixed  $value The value to store.
321
	 *
322
	 * @return void
323
	 */
324 View Code Duplication
	private function store_data( $key, $value ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
325
		global $wpdb;
326
327
		$value = is_array( $value ) ? wp_json_encode( $value ) : esc_attr( $value );
328
329
		$data = array(
330
			'option_name'  => $key,
331
			'option_value' => $value,
332
			'autoload'     => 'no',
333
		);
334
335
		$formats = array(
336
			'%s',
337
			'%s',
338
			'%s',
339
		);
340
341
		$wpdb->replace( $wpdb->options, $data, $formats );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
342
	}
343
344
	/**
345
	 * Delete an option
346
	 *
347
	 * @since  1.5
348
	 *
349
	 * @param  string $key The option_name to delete
350
	 *
351
	 * @return void
352
	 */
353
	private function delete_data( $key ) {
354
		global $wpdb;
355
		$wpdb->delete( $wpdb->options, array(
356
			'option_name' => $key,
357
		) );
358
	}
359
360
}