Completed
Pull Request — master (#664)
by Devin
19:01
created

Give_Tools_Delete_Test_Transactions   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 289
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 289
rs 10
wmc 29
lcom 1
cbo 1

10 Methods

Rating   Name   Duplication   Size   Complexity  
C get_data() 0 58 9
A get_percentage_complete() 0 17 3
A set_properties() 0 2 1
B process_step() 0 30 4
A headers() 0 7 3
A export() 0 7 1
B pre_fetch() 0 36 4
A get_stored_data() 0 6 2
A store_data() 0 19 1
A delete_data() 0 4 1
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 23 and the first side effect is on line 15.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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     http://opensource.org/licenses/gpl-2.0.php 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
52
	 *   Database API
53
	 * @return array $data The data for the CSV file
54
	 */
55
	public function get_data() {
56
		global $wpdb;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
57
58
		$items = $this->get_stored_data( 'give_temp_reset_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 ) {
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...
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
				$ids = implode( ',', $ids );
88
89
				switch ( $type ) {
90
					case 'other':
91
						$sql[] = "DELETE FROM $wpdb->posts WHERE id IN ($ids)";
92
						$sql[] = "DELETE FROM $wpdb->postmeta WHERE post_id IN ($ids)";
93
						$sql[] = "DELETE FROM $wpdb->comments WHERE comment_post_ID IN ($ids)";
94
						$sql[] = "DELETE FROM $wpdb->commentmeta WHERE comment_id NOT IN (SELECT comment_ID FROM $wpdb->comments)";
95
						break;
96
				}
97
98
			}
99
100
			if ( ! empty( $sql ) ) {
101
				foreach ( $sql as $query ) {
102
					$wpdb->query( $query );
103
				}
104
			}
105
106
			return true;
107
108
		}
109
110
		return false;
111
112
	}
113
114
	/**
115
	 * Return the calculated completion percentage
116
	 *
117
	 * @since 1.5
118
	 * @return int
119
	 */
120
	public function get_percentage_complete() {
121
122
		$items = $this->get_stored_data( 'give_temp_reset_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...
123
		$total = count( $items );
124
125
		$percentage = 100;
126
127
		if ( $total > 0 ) {
128
			$percentage = ( ( $this->per_step * $this->step ) / $total ) * 100;
129
		}
130
131
		if ( $percentage > 100 ) {
132
			$percentage = 100;
133
		}
134
135
		return $percentage;
136
	}
137
138
	/**
139
	 * Set the properties specific to the payments export
140
	 *
141
	 * @since 1.5
142
	 *
143
	 * @param array $request The Form Data passed into the batch processing
144
	 */
145
	public function set_properties( $request ) {
146
	}
147
148
	/**
149
	 * Process a step
150
	 *
151
	 * @since 1.5
152
	 * @return bool
153
	 */
154
	public function process_step() {
155
156
		if ( ! $this->can_export() ) {
157
			wp_die( __( 'You do not have permission to delete test transactions.', 'give' ), __( 'Error', 'give' ), array( 'response' => 403 ) );
158
		}
159
160
		$had_data = $this->get_data();
161
162
		if ( $had_data ) {
163
			$this->done = false;
0 ignored issues
show
Bug introduced by
The property done does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
164
165
			return true;
166
		} else {
167
			update_option( 'give_earnings_total', 0 );
168
			delete_transient( 'give_earnings_total' );
169
			delete_transient( 'give_estimated_monthly_stats' . true );
170
			delete_transient( 'give_estimated_monthly_stats' . false );
171
			$this->delete_data( 'give_temp_reset_ids' );
172
173
			// Reset the sequential order numbers
174
			if ( give_get_option( 'enable_sequential' ) ) {
175
				delete_option( 'give_last_payment_number' );
176
			}
177
178
			$this->done    = true;
179
			$this->message = __( 'Test transactions successfully deleted.', 'give' );
0 ignored issues
show
Bug introduced by
The property message does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
180
181
			return false;
182
		}
183
	}
184
185
	/**
186
	 * Headers
187
	 */
188
	public function headers() {
189
		ignore_user_abort( true );
190
191
		if ( ! give_is_func_disabled( 'set_time_limit' ) && ! ini_get( 'safe_mode' ) ) {
192
			set_time_limit( 0 );
193
		}
194
	}
195
196
	/**
197
	 * Perform the export
198
	 *
199
	 * @access public
200
	 * @since 1.5
201
	 * @return void
202
	 */
203
	public function export() {
204
205
		// Set headers
206
		$this->headers();
207
208
		give_die();
209
	}
210
211
	/**
212
	 * Pre Fetch
213
	 */
214
	public function pre_fetch() {
215
216
		if ( $this->step == 1 ) {
217
			$this->delete_data( 'give_temp_reset_ids' );
218
		}
219
220
		$items = get_option( 'give_temp_reset_ids', false );
221
222
		if ( false === $items ) {
223
			$items = array();
224
225
			$args = apply_filters( 'give_tools_reset_stats_total_args', array(
226
				'post_type'      => 'give_payment',
227
				'post_status'    => 'any',
228
				'posts_per_page' => - 1,
229
				//ONLY TEST MODE TRANSACTIONS!!!
230
				'meta_key'   => '_give_payment_mode',
231
				'meta_value' => 'test'
232
			) );
233
234
			$posts = get_posts( $args );
235
			foreach ( $posts as $post ) {
236
				$items[] = array(
237
					'id'   => (int) $post->ID,
238
					'type' => $post->post_type,
239
				);
240
			}
241
242
			// Allow filtering of items to remove with an unassociative array for each item
243
			// The array contains the unique ID of the item, and a 'type' for you to use in the execution of the get_data method
244
			$items = apply_filters( 'give_reset_store_items', $items );
245
246
			$this->store_data( 'give_temp_reset_ids', $items );
247
		}
248
249
	}
250
251
	/**
252
	 * Given a key, get the information from the Database Directly
253
	 *
254
	 * @since  1.5
255
	 *
256
	 * @param  string $key The option_name
257
	 *
258
	 * @return mixed       Returns the data from the database
259
	 */
260
	private function get_stored_data( $key ) {
261
		global $wpdb;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
262
		$value = $wpdb->get_var( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = '%s'", $key ) );
263
264
		return empty( $value ) ? false : maybe_unserialize( $value );
265
	}
266
267
	/**
268
	 * Give a key, store the value
269
	 *
270
	 * @since  1.5
271
	 *
272
	 * @param  string $key The option_name
273
	 * @param  mixed $value The value to store
274
	 *
275
	 * @return void
276
	 */
277
	private function store_data( $key, $value ) {
278
		global $wpdb;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
279
280
		$value = maybe_serialize( $value );
281
282
		$data = array(
283
			'option_name'  => $key,
284
			'option_value' => $value,
285
			'autoload'     => 'no',
286
		);
287
288
		$formats = array(
289
			'%s',
290
			'%s',
291
			'%s',
292
		);
293
294
		$wpdb->replace( $wpdb->options, $data, $formats );
295
	}
296
297
	/**
298
	 * Delete an option
299
	 *
300
	 * @since  1.5
301
	 *
302
	 * @param  string $key The option_name to delete
303
	 *
304
	 * @return void
305
	 */
306
	private function delete_data( $key ) {
307
		global $wpdb;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
308
		$wpdb->delete( $wpdb->options, array( 'option_name' => $key ) );
309
	}
310
311
}
312