Test Failed
Push — issues/1971 ( 0bed62 )
by Ravinder
05:01
created

data/class-give-tools-delete-test-transactions.php (5 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Delete Test Transactions
4
 *
5
 * This class handles batch processing of deleting test transactions
6
 *
7
 * @subpackage  Admin/Tools/Give_Tools_Delete_Test_Transactions
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_Delete_Test_Transactions Class
20
 *
21
 * @since 1.5
22
 */
23
class Give_Tools_Delete_Test_Transactions extends Give_Batch_Export {
24
25
	/**
26
	 * Our export type. Used for export-type specific filters/actions
27
	 * @var string
28
	 * @since 1.5
29
	 */
30
	public $export_type = '';
31
32
	/**
33
	 * Allows for a non-form batch processing to be run.
34
	 * @since  1.5
35
	 * @var boolean
36
	 */
37
	public $is_void = true;
38
39
	/**
40
	 * Sets the number of items to pull on each step
41
	 * @since  1.5
42
	 * @var integer
43
	 */
44
	public $per_step = 30;
45
46
	/**
47
	 * Get the Export Data
48
	 *
49
	 * @access public
50
	 * @since 1.5
51
	 * @global object $wpdb Used to query the database using the WordPress Database API
52
	 *
53
	 * @return array|bool $data The data for the CSV file
54
	 */
55
	public function get_data() {
56
		global $wpdb;
57
58
		$items = $this->get_stored_data( 'give_temp_delete_test_ids' );
59
60
		if ( ! is_array( $items ) ) {
61
			return false;
62
		}
63
64
		$offset     = ( $this->step - 1 ) * $this->per_step;
65
		$step_items = array_slice( $items, $offset, $this->per_step );
66
67
		if ( $step_items ) {
68
69
			$step_ids = array(
70
				'other' => array(),
71
			);
72
73
			foreach ( $step_items as $item ) {
74
75
				$step_ids['other'][] = $item['id'];
76
77
			}
78
79
			$sql = array();
80
81
			foreach ( $step_ids as $type => $ids ) {
82
83
				if ( empty( $ids ) ) {
84
					continue;
85
				}
86
87
				$parent_query = '';
0 ignored issues
show
$parent_query is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
88
89
				switch ( $type ) {
90
					case 'other':
91
92
						$temp_ids = implode( ',', $ids );
93
94
						// Get all the test logs of the donations ids.
95
						$parent_query = "SELECT DISTINCT post_id as id FROM $wpdb->postmeta WHERE meta_key = '_give_log_payment_id' AND meta_value IN ( $temp_ids )";
96
						$parent_ids = $wpdb->get_results( $parent_query, 'ARRAY_A' );
0 ignored issues
show
Usage of a direct database call is discouraged.
Loading history...
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
97
98
						// List of all test logs.
99
						if ( $parent_ids ) {
100
							foreach ( $parent_ids as $parent_id ) {
101
								// Adding all the test log in post ids that are going to get deleted.
102
								$ids[] = $parent_id['id'];
103
							}
104
						}
105
						$ids = implode( ',', $ids );
106
107
						$sql[] = "DELETE FROM $wpdb->posts WHERE id IN ($ids)";
108
						$sql[] = "DELETE FROM $wpdb->postmeta WHERE post_id IN ($ids)";
109
						$sql[] = "DELETE FROM $wpdb->comments WHERE comment_post_ID IN ($ids)";
110
						$sql[] = "DELETE FROM $wpdb->commentmeta WHERE comment_id NOT IN (SELECT comment_ID FROM $wpdb->comments)";
111
						break;
112
				}
113
114
			}
115
116
			if ( ! empty( $sql ) ) {
117
				foreach ( $sql as $query ) {
118
					$wpdb->query( $query );
0 ignored issues
show
Usage of a direct database call is discouraged.
Loading history...
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
119
				}
120
				do_action( 'give_delete_log_cache' );
121
			}
122
			return true;
123
		}
124
		return false;
125
	}
126
127
	/**
128
	 * Return the calculated completion percentage
129
	 *
130
	 * @since 1.5
131
	 * @return int
132
	 */
133
	public function get_percentage_complete() {
134
135
		$items = $this->get_stored_data( 'give_temp_delete_test_ids', false );
136
		$total = count( $items );
137
138
		$percentage = 100;
139
140
		if ( $total > 0 ) {
141
			$percentage = ( ( $this->per_step * $this->step ) / $total ) * 100;
142
		}
143
144
		if ( $percentage > 100 ) {
145
			$percentage = 100;
146
		}
147
148
		return $percentage;
149
	}
150
151
	/**
152
	 * Set the properties specific to the payments export
153
	 *
154
	 * @since 1.5
155
	 *
156
	 * @param array $request The Form Data passed into the batch processing
157
	 */
158
	public function set_properties( $request ) {
159
	}
160
161
	/**
162
	 * Process a step
163
	 *
164
	 * @since 1.5
165
	 * @return bool
166
	 */
167 View Code Duplication
	public function process_step() {
168
169
		if ( ! $this->can_export() ) {
170
			wp_die( __( 'You do not have permission to delete test transactions.', 'give' ), __( 'Error', 'give' ), array( 'response' => 403 ) );
171
		}
172
173
		$had_data = $this->get_data();
174
175
		if ( $had_data ) {
176
			$this->done = false;
177
178
			return true;
179
		} else {
180
			update_option( 'give_earnings_total', give_get_total_earnings( true ) );
181
			Give_Cache::delete( Give_Cache::get_key('give_estimated_monthly_stats' ) );
182
183
			$this->delete_data( 'give_temp_delete_test_ids' );
184
185
			// Reset the sequential order numbers
186
			if ( give_get_option( 'enable_sequential' ) ) {
187
				delete_option( 'give_last_payment_number' );
188
			}
189
190
			$this->done    = true;
191
			$this->message = __( 'Test transactions successfully deleted.', 'give' );
192
193
			return false;
194
		}
195
	}
196
197
	/**
198
	 * Headers
199
	 */
200 View Code Duplication
	public function headers() {
201
		ignore_user_abort( true );
202
203
		if ( ! give_is_func_disabled( 'set_time_limit' ) && ! ini_get( 'safe_mode' ) ) {
204
			set_time_limit( 0 );
205
		}
206
	}
207
208
	/**
209
	 * Perform the export
210
	 *
211
	 * @access public
212
	 * @since 1.5
213
	 * @return void
214
	 */
215
	public function export() {
216
217
		// Set headers
218
		$this->headers();
219
220
		give_die();
221
	}
222
223
	/**
224
	 * Pre Fetch
225
	 */
226
	public function pre_fetch() {
227
228
		if ( $this->step == 1 ) {
229
			$this->delete_data( 'give_temp_delete_test_ids' );
230
		}
231
232
		$items = get_option( 'give_temp_delete_test_ids', false );
233
234
		if ( false === $items ) {
235
			$items = array();
236
237
			$args = apply_filters( 'give_tools_reset_stats_total_args', array(
238
				'post_type'      => 'give_payment',
239
				'post_status'    => 'any',
240
				'posts_per_page' => - 1,
241
				// ONLY TEST MODE TRANSACTIONS!!!
242
				'meta_key'   => '_give_payment_mode',
243
				'meta_value' => 'test'
244
			) );
245
246
			$posts = get_posts( $args );
247 View Code Duplication
			foreach ( $posts as $post ) {
248
				$items[] = array(
249
					'id'   => (int) $post->ID,
250
					'type' => $post->post_type,
251
				);
252
			}
253
254
			// Allow filtering of items to remove with an unassociative array for each item.
255
			// The array contains the unique ID of the item, and a 'type' for you to use in the execution of the get_data method.
256
			$items = apply_filters( 'give_delete_test_items', $items );
257
258
			$this->store_data( 'give_temp_delete_test_ids', $items );
259
		}
260
261
	}
262
263
	/**
264
	 * Given a key, get the information from the Database Directly
265
	 *
266
	 * @since  1.5
267
	 *
268
	 * @param  string $key The option_name
269
	 *
270
	 * @return mixed       Returns the data from the database
271
	 */
272
	private function get_stored_data( $key ) {
273
		global $wpdb;
274
		$value = $wpdb->get_var( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = '%s'", $key ) );
275
276
		return empty( $value ) ? false : maybe_unserialize( $value );
277
	}
278
279
	/**
280
	 * Give a key, store the value
281
	 *
282
	 * @since  1.5
283
	 *
284
	 * @param  string $key The option_name
285
	 * @param  mixed $value The value to store
286
	 *
287
	 * @return void
288
	 */
289 View Code Duplication
	private function store_data( $key, $value ) {
290
		global $wpdb;
291
292
		$value = maybe_serialize( $value );
293
294
		$data = array(
295
			'option_name'  => $key,
296
			'option_value' => $value,
297
			'autoload'     => 'no',
298
		);
299
300
		$formats = array(
301
			'%s',
302
			'%s',
303
			'%s',
304
		);
305
306
		$wpdb->replace( $wpdb->options, $data, $formats );
307
	}
308
309
	/**
310
	 * Delete an option
311
	 *
312
	 * @since  1.5
313
	 *
314
	 * @param  string $key The option_name to delete
315
	 *
316
	 * @return void
317
	 */
318
	private function delete_data( $key ) {
319
		global $wpdb;
320
		$wpdb->delete( $wpdb->options, array( 'option_name' => $key ) );
321
	}
322
323
}
324