Completed
Push — release/1.8.11 ( a764cd )
by Ravinder
893:07 queued 876:14
created

Give_Tools_Recount_Donor_Stats   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 219
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 219
rs 10
c 0
b 0
f 0
wmc 20
lcom 1
cbo 4

6 Methods

Rating   Name   Duplication   Size   Complexity  
C get_data() 0 89 9
A get_percentage_complete() 0 23 3
A set_properties() 0 2 1
A process_step() 0 21 3
A headers() 0 7 3
A export() 0 7 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 22 and the first side effect is on line 14.

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 all donor stats.
4
 *
5
 * This class handles batch processing of recounting all donor stats.
6
 *
7
 * @subpackage  Admin/Tools/Give_Tools_Recount_Donor_Stats
8
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
9
 * @since       1.5
10
 */
11
12
// Exit if accessed directly.
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
/**
18
 * Give_Tools_Recount_Donor_Stats Class
19
 *
20
 * @since 1.5
21
 */
22
class Give_Tools_Recount_Donor_Stats extends Give_Batch_Export {
23
24
	/**
25
	 * Our export type. Used for export-type specific filters/actions.
26
	 *
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
	 *
35
	 * @since  1.5
36
	 * @var boolean
37
	 */
38
	public $is_void = true;
39
40
	/**
41
	 * Sets the number of items to pull on each step
42
	 *
43
	 * @since  1.5
44
	 * @var integer
45
	 */
46
	public $per_step = 5;
47
48
	/**
49
	 * Get the Export Data
50
	 *
51
	 * @access public
52
	 * @since 1.5
53
	 *
54
	 * @return array $data The data for the CSV file
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
55
	 */
56
	public function get_data() {
57
58
		$args = array(
59
			'number'  => $this->per_step,
60
			'offset'  => $this->per_step * ( $this->step - 1 ),
61
			'orderby' => 'id',
62
			'order'   => 'DESC',
63
		);
64
65
		$donors = Give()->donors->get_donors( $args );
66
67
		if ( $donors ) {
68
69
			$allowed_payment_status = apply_filters( 'give_recount_donors_donation_statuses', give_get_payment_status_keys() );
70
71
			foreach ( $donors as $donor ) {
72
73
				$attached_payment_ids = explode( ',', $donor->payment_ids );
74
75
				$attached_args = array(
76
					'post__in' => $attached_payment_ids,
77
					'number'   => - 1,
78
					'status'   => $allowed_payment_status,
79
				);
80
81
				$attached_payments = (array) give_get_payments( $attached_args );
82
83
				$unattached_args = array(
84
					'post__not_in' => $attached_payment_ids,
85
					'number'       => - 1,
86
					'status'       => $allowed_payment_status,
87
					'meta_query'   => array(
88
						array(
89
							'key'     => '_give_payment_user_email',
90
							'value'   => $donor->email,
91
							'compare' => '=',
92
						),
93
					),
94
				);
95
96
				$unattached_payments = give_get_payments( $unattached_args );
97
98
				$payments = array_merge( $attached_payments, $unattached_payments );
99
100
				$purchase_value = 0.00;
101
				$purchase_count = 0;
102
				$payment_ids    = array();
103
104
				if ( $payments ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $payments 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...
105
106
					foreach ( $payments as $payment ) {
107
108
						$should_process_payment = 'publish' == $payment->post_status ? true : false;
109
						$should_process_payment = apply_filters( 'give_donor_recount_should_process_donation', $should_process_payment, $payment );
110
111
						if ( true === $should_process_payment ) {
112
113
							if ( apply_filters( 'give_donor_recount_should_increase_value', true, $payment ) ) {
114
								$purchase_value += give_get_payment_amount( $payment->ID );
115
							}
116
117
							if ( apply_filters( 'give_donor_recount_should_increase_count', true, $payment ) ) {
118
								$purchase_count ++;
119
							}
120
						}
121
122
						$payment_ids[] = $payment->ID;
123
					}
124
				}
125
126
				$payment_ids = implode( ',', $payment_ids );
127
128
				$donor_update_data = array(
129
					'purchase_count' => $purchase_count,
130
					'purchase_value' => $purchase_value,
131
					'payment_ids'    => $payment_ids,
132
				);
133
134
				$donor_instance = new Give_Donor( $donor->id );
135
				$donor_instance->update( $donor_update_data );
136
137
			}// End foreach().
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
138
139
			return true;
140
		}// End if().
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
141
142
		return false;
143
144
	}
145
146
	/**
147
	 * Return the calculated completion percentage
148
	 *
149
	 * @since 1.5
150
	 * @return int
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|double?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
151
	 */
152
	public function get_percentage_complete() {
153
154
		$args = array(
155
			'number'  => - 1,
156
			'orderby' => 'id',
157
			'order'   => 'DESC',
158
		);
159
160
		$donors = Give()->donors->get_donors( $args );
161
		$total     = count( $donors );
162
163
		$percentage = 100;
164
165
		if ( $total > 0 ) {
166
			$percentage = ( ( $this->per_step * $this->step ) / $total ) * 100;
167
		}
168
169
		if ( $percentage > 100 ) {
170
			$percentage = 100;
171
		}
172
173
		return $percentage;
174
	}
175
176
	/**
177
	 * Set the properties specific to the payments export
178
	 *
179
	 * @since 1.5
180
	 *
181
	 * @param array $request The Form Data passed into the batch processing
182
	 */
183
	public function set_properties( $request ) {
184
	}
185
186
	/**
187
	 * Process a step
188
	 *
189
	 * @since 1.5
190
	 * @return bool
191
	 */
192
	public function process_step() {
193
194
		if ( ! $this->can_export() ) {
195
			wp_die( esc_html__( 'You do not have permission to recount stats.', 'give' ), esc_html__( 'Error', 'give' ), array(
196
				'response' => 403,
197
			) );
198
		}
199
200
		$had_data = $this->get_data();
201
202
		if ( $had_data ) {
203
			$this->done = false;
204
205
			return true;
206
		} else {
207
			$this->done    = true;
208
			$this->message = esc_html__( 'Donor stats have been 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...
209
210
			return false;
211
		}
212
	}
213
214
	/**
215
	 * Headers
216
	 */
217
	public function headers() {
218
		ignore_user_abort( true );
219
220
		if ( ! give_is_func_disabled( 'set_time_limit' ) && ! ini_get( 'safe_mode' ) ) {
221
			set_time_limit( 0 );
222
		}
223
	}
224
225
	/**
226
	 * Perform the export
227
	 *
228
	 * @access public
229
	 * @since 1.5
230
	 * @return void
231
	 */
232
	public function export() {
233
234
		// Set headers
235
		$this->headers();
236
237
		give_die();
238
	}
239
240
}
241