Completed
Pull Request — master (#639)
by
unknown
19:14
created

Give_Tools_Recount_Income   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 238
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 238
rs 10
wmc 22
lcom 1
cbo 1

9 Methods

Rating   Name   Duplication   Size   Complexity  
B get_data() 0 50 6
B get_percentage_complete() 0 26 4
A set_properties() 0 2 1
A process_step() 0 21 3
A headers() 0 7 3
A export() 0 7 1
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
 * Recount income
4
 *
5
 * This class handles batch processing of recounting income
6
 *
7
 * @subpackage  Admin/Tools/Give_Tools_Recount_Income
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_Recount_Income Class
20
 *
21
 * @since 1.5
22
 */
23
class Give_Tools_Recount_Income 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 = 100;
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
57
		if ( $this->step == 1 ) {
58
			$this->delete_data( 'give_temp_recount_income' );
59
		}
60
61
		$total = get_option( 'give_temp_recount_income', false );
62
63
		if ( false === $total ) {
64
			$total = (float) 0;
65
			$this->store_data( 'give_temp_recount_income', $total );
66
		}
67
68
		$accepted_statuses = apply_filters( 'give_recount_accepted_statuses', array( 'publish', 'revoked' ) );
69
70
		$args = apply_filters( 'give_recount_income_args', array(
71
			'number' => $this->per_step,
72
			'page'   => $this->step,
73
			'status' => $accepted_statuses,
74
			'fields' => 'ids'
75
		) );
76
77
		$payments = give_get_payments( $args );
78
79
		if ( ! empty( $payments ) ) {
80
81
			foreach ( $payments as $payment ) {
82
83
				$total += give_get_payment_amount( $payment );
84
85
			}
86
87
			if ( $total < 0 ) {
88
				$totals = 0;
89
			}
90
91
			$total = round( $total, give_currency_decimal_filter() );
92
93
			$this->store_data( 'give_temp_recount_income', $total );
94
95
			return true;
96
97
		}
98
99
		update_option( 'give_income_total', $total );
100
		set_transient( 'give_income_total', $total, 86400 );
101
102
		return false;
103
104
	}
105
106
	/**
107
	 * Return the calculated completion percentage
108
	 *
109
	 * @since 1.5
110
	 * @return int
111
	 */
112
	public function get_percentage_complete() {
113
114
		$total = $this->get_stored_data( 'give_recount_income_total' );
115
116
		if ( false === $total ) {
117
			$args = apply_filters( 'give_recount_income_total_args', array() );
118
119
			$counts = give_count_payments( $args );
120
			$total  = absint( $counts->publish ) + absint( $counts->revoked );
121
			$total  = apply_filters( 'give_recount_store_income_total', $total );
122
123
			$this->store_data( 'give_recount_income_total', $total );
124
		}
125
126
		$percentage = 100;
127
128
		if ( $total > 0 ) {
129
			$percentage = ( ( $this->per_step * $this->step ) / $total ) * 100;
130
		}
131
132
		if ( $percentage > 100 ) {
133
			$percentage = 100;
134
		}
135
136
		return $percentage;
137
	}
138
139
	/**
140
	 * Set the properties specific to the payments export
141
	 *
142
	 * @since 1.5
143
	 *
144
	 * @param array $request The Form Data passed into the batch processing
145
	 */
146
	public function set_properties( $request ) {
147
	}
148
149
	/**
150
	 * Process a step
151
	 *
152
	 * @since 1.5
153
	 * @return bool
154
	 */
155
	public function process_step() {
156
157
		if ( ! $this->can_export() ) {
158
			wp_die( __( 'You do not have permission to export data.', 'give' ), __( 'Error', 'give' ), array( 'response' => 403 ) );
159
		}
160
161
		$had_data = $this->get_data();
162
163
		if ( $had_data ) {
164
			$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...
165
166
			return true;
167
		} else {
168
			$this->delete_data( 'give_recount_income_total' );
169
			$this->delete_data( 'give_temp_recount_income' );
170
			$this->done    = true;
171
			$this->message = __( 'Give income successfully recounted.', '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...
172
173
			return false;
174
		}
175
	}
176
177
	public function headers() {
178
		ignore_user_abort( true );
179
180
		if ( ! give_is_func_disabled( 'set_time_limit' ) && ! ini_get( 'safe_mode' ) ) {
181
			set_time_limit( 0 );
182
		}
183
	}
184
185
	/**
186
	 * Perform the export
187
	 *
188
	 * @access public
189
	 * @since 1.5
190
	 * @return void
191
	 */
192
	public function export() {
193
194
		// Set headers
195
		$this->headers();
196
197
		give_die();
198
	}
199
200
	/**
201
	 * Given a key, get the information from the Database Directly
202
	 *
203
	 * @since  1.5
204
	 *
205
	 * @param  string $key The option_name
206
	 *
207
	 * @return mixed       Returns the data from the database
208
	 */
209
	private function get_stored_data( $key ) {
210
		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...
211
		$value = $wpdb->get_var( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = '%s'", $key ) );
212
213
		return empty( $value ) ? false : maybe_unserialize( $value );
214
	}
215
216
	/**
217
	 * Give a key, store the value
218
	 *
219
	 * @since  1.5
220
	 *
221
	 * @param  string $key The option_name
222
	 * @param  mixed $value The value to store
223
	 *
224
	 * @return void
225
	 */
226
	private function store_data( $key, $value ) {
227
		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...
228
229
		$value = maybe_serialize( $value );
230
231
		$data = array(
232
			'option_name'  => $key,
233
			'option_value' => $value,
234
			'autoload'     => 'no',
235
		);
236
237
		$formats = array(
238
			'%s',
239
			'%s',
240
			'%s',
241
		);
242
243
		$wpdb->replace( $wpdb->options, $data, $formats );
244
	}
245
246
	/**
247
	 * Delete an option
248
	 *
249
	 * @since  1.5
250
	 *
251
	 * @param  string $key The option_name to delete
252
	 *
253
	 * @return void
254
	 */
255
	private function delete_data( $key ) {
256
		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...
257
		$wpdb->delete( $wpdb->options, array( 'option_name' => $key ) );
258
	}
259
260
}
261