Test Failed
Pull Request — master (#2482)
by Devin
05:37
created

Give_Tools_Delete_Test_Transactions::get_data()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 4
nop 0
dl 0
loc 20
rs 9.2
c 0
b 0
f 0
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
		$items = $this->get_stored_data( 'give_temp_delete_test_ids' );
57
58
		if ( ! is_array( $items ) ) {
59
			return false;
60
		}
61
62
		$offset     = ( $this->step - 1 ) * $this->per_step;
63
		$step_items = array_slice( $items, $offset, $this->per_step );
64
65
		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...
66
			foreach ( $step_items as $item ) {
67
				// Delete the main payment.
68
				give_delete_donation( absint( $item['id'] ) );
69
			}
70
			return true;
71
		}
72
73
		return false;
74
	}
75
76
	/**
77
	 * Return the calculated completion percentage
78
	 *
79
	 * @since 1.5
80
	 * @return int
81
	 */
82
	public function get_percentage_complete() {
83
84
		$items = $this->get_stored_data( 'give_temp_delete_test_ids', false );
0 ignored issues
show
Unused Code introduced by
The call to Give_Tools_Delete_Test_T...ions::get_stored_data() has too many arguments starting with false.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
85
		$total = count( $items );
86
87
		$percentage = 100;
88
89
		if ( $total > 0 ) {
90
			$percentage = ( ( $this->per_step * $this->step ) / $total ) * 100;
91
		}
92
93
		if ( $percentage > 100 ) {
94
			$percentage = 100;
95
		}
96
97
		return $percentage;
98
	}
99
100
	/**
101
	 * Set the properties specific to the payments export
102
	 *
103
	 * @since 1.5
104
	 *
105
	 * @param array $request The Form Data passed into the batch processing
106
	 */
107
	public function set_properties( $request ) {
108
	}
109
110
	/**
111
	 * Process a step
112
	 *
113
	 * @since 1.5
114
	 * @return bool
115
	 */
116 View Code Duplication
	public function process_step() {
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...
117
118
		if ( ! $this->can_export() ) {
119
			wp_die( __( 'You do not have permission to delete test transactions.', 'give' ), __( 'Error', 'give' ), array( 'response' => 403 ) );
120
		}
121
122
		$had_data = $this->get_data();
123
124
		if ( $had_data ) {
125
			$this->done = false;
126
127
			return true;
128
		} else {
129
			update_option( 'give_earnings_total', give_get_total_earnings( true ) );
130
			Give_Cache::delete( Give_Cache::get_key( 'give_estimated_monthly_stats' ) );
131
132
			$this->delete_data( 'give_temp_delete_test_ids' );
133
134
			// Reset the sequential order numbers
135
			if ( give_get_option( 'enable_sequential' ) ) {
136
				delete_option( 'give_last_payment_number' );
137
			}
138
139
			$this->done    = true;
140
			$this->message = __( 'Test transactions successfully deleted.', 'give' );
141
142
			return false;
143
		}
144
	}
145
146
	/**
147
	 * Headers
148
	 */
149 View Code Duplication
	public function headers() {
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...
150
		ignore_user_abort( true );
151
152
		if ( ! give_is_func_disabled( 'set_time_limit' ) && ! ini_get( 'safe_mode' ) ) {
153
			set_time_limit( 0 );
154
		}
155
	}
156
157
	/**
158
	 * Perform the export
159
	 *
160
	 * @access public
161
	 * @since 1.5
162
	 * @return void
163
	 */
164
	public function export() {
165
166
		// Set headers
167
		$this->headers();
168
169
		give_die();
170
	}
171
172
	/**
173
	 * Pre Fetch
174
	 */
175
	public function pre_fetch() {
176
177
		if ( $this->step == 1 ) {
0 ignored issues
show
introduced by
Found "== 1". Use Yoda Condition checks, you must
Loading history...
178
			$this->delete_data( 'give_temp_delete_test_ids' );
179
		}
180
181
		$items = get_option( 'give_temp_delete_test_ids', false );
182
183
		if ( false === $items ) {
184
			$items = array();
185
186
			$args = apply_filters( 'give_tools_reset_stats_total_args', array(
187
				'post_status' => 'any',
188
				'number'      => - 1,
189
				'meta_key'    => '_give_payment_mode',
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
190
				'meta_value'  => 'test'
0 ignored issues
show
introduced by
Detected usage of meta_value, possible slow query.
Loading history...
introduced by
Each line in an array declaration must end in a comma
Loading history...
191
			) );
192
193
			$posts    = new Give_Payments_Query( $args );
194
			$payments = $posts->get_payments();
195
196
			/* @var Give_Payment $payment */
197
			foreach ( $payments as $payment ) {
198
				$items[] = array(
199
					'id'   => (int) $payment->ID,
200
					'type' => 'give_payment',
201
				);
202
			}
203
			
204
			// Allow filtering of items to remove with an unassociative array for each item.
205
			// The array contains the unique ID of the item, and a 'type' for you to use in the execution of the get_data method.
206
			$items = apply_filters( 'give_delete_test_items', $items );
207
208
			$this->store_data( 'give_temp_delete_test_ids', $items );
209
		}
210
211
	}
212
213
	/**
214
	 * Given a key, get the information from the Database Directly
215
	 *
216
	 * @since  1.5
217
	 *
218
	 * @param  string $key The option_name
219
	 *
220
	 * @return mixed       Returns the data from the database
221
	 */
222 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...
223
		global $wpdb;
224
		$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...
225
226
		if ( empty( $value ) ) {
227
			return false;
228
		}
229
230
		$maybe_json = json_decode( $value );
231
		if ( ! is_null( $maybe_json ) ) {
232
			$value = json_decode( $value, true );
233
		}
234
235
		return $value;
236
	}
237
238
	/**
239
	 * Give a key, store the value
240
	 *
241
	 * @since  1.5
242
	 *
243
	 * @param  string $key The option_name
244
	 * @param  mixed $value The value to store
245
	 *
246
	 * @return void
247
	 */
248 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...
249
		global $wpdb;
250
251
		$value = is_array( $value ) ? wp_json_encode( $value ) : esc_attr( $value );
252
253
		$data = array(
254
			'option_name'  => $key,
255
			'option_value' => $value,
256
			'autoload'     => 'no',
257
		);
258
259
		$formats = array(
260
			'%s',
261
			'%s',
262
			'%s',
263
		);
264
265
		$wpdb->replace( $wpdb->options, $data, $formats );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
266
	}
267
268
	/**
269
	 * Delete an option
270
	 *
271
	 * @since  1.5
272
	 *
273
	 * @param  string $key The option_name to delete
274
	 *
275
	 * @return void
276
	 */
277
	private function delete_data( $key ) {
278
		global $wpdb;
279
		$wpdb->delete( $wpdb->options, array( 'option_name' => $key ) );
280
	}
281
282
}
283