Test Failed
Push — master ( b86487...e5e5a2 )
by Ravinder
07:05
created

Give_Tools_Recount_Donor_Stats::headers()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 7
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 2
nop 0
dl 7
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
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
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(
0 ignored issues
show
introduced by
Detected usage of meta_query, possible slow query.
Loading history...
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().
138
139
			return true;
140
		}// End if().
141
142
		return false;
143
144
	}
145
146
	/**
147
	 * Return the calculated completion percentage
148
	 *
149
	 * @since 1.5
150
	 * @return int
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' );
209
210
			return false;
211
		}
212
	}
213
214
	/**
215
	 * Headers
216
	 */
217 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...
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