Completed
Pull Request — master (#1412)
by Ravinder
17:25
created

Give_Batch_Payments_Export::set_properties()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 6
nc 32
nop 1
dl 0
loc 7
rs 8.8571
c 0
b 0
f 0
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 24 and the first side effect is on line 16.

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
 * Payments Export Class.
4
 *
5
 * This class handles payment export in batches.
6
 *
7
 * @package     Give
8
 * @subpackage  Admin/Reports
9
 * @copyright   Copyright (c) 2016, WordImpress
10
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
11
 * @since       1.5
12
 */
13
14
// Exit if accessed directly.
15
if ( ! defined( 'ABSPATH' ) ) {
16
	exit;
17
}
18
19
/**
20
 * Give_Batch_Payments_Export Class
21
 *
22
 * @since 1.5
23
 */
24
class Give_Batch_Payments_Export extends Give_Batch_Export {
25
26
	/**
27
	 * Our export type. Used for export-type specific filters/actions.
28
	 * @var string
29
	 * @since 1.5
30
	 */
31
	public $export_type = 'payments';
32
33
	/**
34
	 * Set the CSV columns.
35
	 *
36
	 * @access public
37
	 * @since 1.5
38
	 * @return array $cols All the columns.
39
	 */
40
	public function csv_cols() {
41
		$cols = array(
42
			'id'        => esc_html__( 'ID', 'give' ), // unaltered payment ID (use for querying).
43
			'seq_id'    => esc_html__( 'Payment Number', 'give' ), // sequential payment ID.
44
			'email'     => esc_html__( 'Email', 'give' ),
45
			'first'     => esc_html__( 'First Name', 'give' ),
46
			'last'      => esc_html__( 'Last Name', 'give' ),
47
			'address1'  => esc_html__( 'Address 1', 'give' ),
48
			'address2'  => esc_html__( 'Address 2', 'give' ),
49
			'city'      => esc_html__( 'City', 'give' ),
50
			'state'     => esc_html__( 'State', 'give' ),
51
			'country'   => esc_html__( 'Country', 'give' ),
52
			'zip'       => esc_html__( 'Zip / Postal Code', 'give' ),
53
			'form_id'   => esc_html__( 'Form ID', 'give' ),
54
			'form_name' => esc_html__( 'Form Name', 'give' ),
55
			'amount'    => esc_html__( 'Amount', 'give' ) . ' (' . html_entity_decode( give_currency_filter( '' ) ) . ')',
56
			'gateway'   => esc_html__( 'Payment Method', 'give' ),
57
			'trans_id'  => esc_html__( 'Transaction ID', 'give' ),
58
			'key'       => esc_html__( 'Key', 'give' ),
59
			'date'      => esc_html__( 'Date', 'give' ),
60
			'user'      => esc_html__( 'User', 'give' ),
61
			'status'    => esc_html__( 'Status', 'give' )
62
		);
63
64
		if ( ! give_get_option( 'enable_sequential' ) ) {
65
			unset( $cols['seq_id'] );
66
		}
67
68
		return $cols;
69
	}
70
71
	/**
72
	 * Get the Export Data.
73
	 *
74
	 * @access public
75
	 * @since  1.5
76
	 * @global object $wpdb Used to query the database using the WordPress database API.
77
	 * @return array $data The data for the CSV file.
78
	 */
79
	public function get_data() {
80
		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...
81
82
		$data = array();
83
84
		$args = array(
85
			'number' => 30,
86
			'page'   => $this->step,
87
			'status' => $this->status
88
		);
89
90
		if ( ! empty( $this->start ) || ! empty( $this->end ) ) {
91
92
			$args['date_query'] = array(
93
				array(
94
					'after'     => date( 'Y-n-d 00:00:00', strtotime( $this->start ) ),
95
					'before'    => date( 'Y-n-d 23:59:59', strtotime( $this->end ) ),
96
					'inclusive' => true
97
				)
98
			);
99
100
		}
101
102
		// Add category or tag to payment query if any.
103
		if ( ! empty( $this->categories ) || ! empty( $this->tags ) ) {
104
			$form_args = array(
105
				'post_type'      => 'give_forms',
106
				'post_status'    => 'publish',
107
				'posts_per_page' => - 1,
108
				'fields'         => 'ids',
109
				'tax_query'      => array(
110
					'relation' => 'AND',
111
				),
112
			);
113
114
115
			if ( ! empty( $this->categories ) ) {
116
				$form_args['tax_query'][] = array(
117
					'taxonomy' => 'give_forms_category',
118
					'terms'    => $this->categories,
0 ignored issues
show
Bug introduced by
The property categories 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...
119
				);
120
			}
121
122
			if ( ! empty( $this->tags ) ) {
123
				$form_args['tax_query'][] = array(
124
					'taxonomy' => 'give_forms_tag',
125
					'terms'    => $this->tags,
0 ignored issues
show
Bug introduced by
The property tags 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...
126
				);
127
			}
128
129
			$forms = new WP_Query( $form_args );
130
131
			if ( empty( $forms->posts ) ) {
132
				return array();
133
			}
134
135
			$args['give_forms'] = $forms->posts;
136
		}
137
138
		$payments = give_get_payments( $args );
139
140
		if ( $payments ) {
141
142
			foreach ( $payments as $payment ) {
143
				$payment_meta = give_get_payment_meta( $payment->ID );
144
				$user_info    = give_get_payment_meta_user_info( $payment->ID );
145
				$total        = give_get_payment_amount( $payment->ID );
146
				$user_id      = isset( $user_info['id'] ) && $user_info['id'] != - 1 ? $user_info['id'] : $user_info['email'];
147
				$products     = '';
148
				$skus         = '';
149
150
				if ( is_numeric( $user_id ) ) {
151
					$user = get_userdata( $user_id );
152
				} else {
153
					$user = false;
154
				}
155
156
				$data[] = array(
157
					'id'        => $payment->ID,
158
					'seq_id'    => give_get_payment_number( $payment->ID ),
159
					'email'     => $payment_meta['email'],
160
					'first'     => $user_info['first_name'],
161
					'last'      => $user_info['last_name'],
162
					'address1'  => isset( $user_info['address']['line1'] ) ? $user_info['address']['line1'] : '',
163
					'address2'  => isset( $user_info['address']['line2'] ) ? $user_info['address']['line2'] : '',
164
					'city'      => isset( $user_info['address']['city'] ) ? $user_info['address']['city'] : '',
165
					'state'     => isset( $user_info['address']['state'] ) ? $user_info['address']['state'] : '',
166
					'country'   => isset( $user_info['address']['country'] ) ? $user_info['address']['country'] : '',
167
					'zip'       => isset( $user_info['address']['zip'] ) ? $user_info['address']['zip'] : '',
168
					'form_id'   => isset( $payment_meta['form_id'] ) ? $payment_meta['form_id'] : '',
169
					'form_name' => isset( $payment_meta['form_title'] ) ? $payment_meta['form_title'] : '',
170
					'skus'      => $skus,
171
					'amount'    => html_entity_decode( give_format_amount( $total ) ),
172
					'gateway'   => give_get_gateway_admin_label( get_post_meta( $payment->ID, '_give_payment_gateway', true ) ),
173
					'trans_id'  => give_get_payment_transaction_id( $payment->ID ),
174
					'key'       => $payment_meta['key'],
175
					'date'      => $payment->post_date,
176
					'user'      => $user ? $user->display_name : __( 'guest', 'give' ),
177
					'status'    => give_get_payment_status( $payment, true )
178
				);
179
180
			}
181
182
			$data = apply_filters( 'give_export_get_data', $data );
183
			$data = apply_filters( "give_export_get_data_{$this->export_type}", $data );
184
185
			return $data;
186
187
		}
188
189
		return array();
190
191
	}
192
193
	/**
194
	 * Return the calculated completion percentage.
195
	 *
196
	 * @since 1.5
197
	 * @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...
198
	 */
199
	public function get_percentage_complete() {
200
201
		$status = $this->status;
202
		$args   = array(
203
			'start-date' => date( 'n/d/Y', strtotime( $this->start ) ),
204
			'end-date'   => date( 'n/d/Y', strtotime( $this->end ) ),
205
		);
206
207
		if ( 'any' == $status ) {
208
209
			$total = array_sum( (array) give_count_payments( $args ) );
210
211
		} else {
212
213
			$total = give_count_payments( $args )->$status;
214
215
		}
216
217
		$percentage = 100;
218
219
		if ( $total > 0 ) {
220
			$percentage = ( ( 30 * $this->step ) / $total ) * 100;
221
		}
222
223
		if ( $percentage > 100 ) {
224
			$percentage = 100;
225
		}
226
227
		return $percentage;
228
	}
229
230
	/**
231
	 * Set the properties specific to the payments export.
232
	 *
233
	 * @since 1.5
234
	 *
235
	 * @param array $request The Form Data passed into the batch processing.
236
	 */
237
	public function set_properties( $request ) {
238
		$this->start      = isset( $request['start'] ) ? sanitize_text_field( $request['start'] ) : '';
239
		$this->end        = isset( $request['end'] ) ? sanitize_text_field( $request['end'] ) : '';
240
		$this->status     = isset( $request['status'] ) ? sanitize_text_field( $request['status'] ) : 'complete';
241
		$this->categories = isset( $request['give_forms_categories'] ) ? give_clean( $request['give_forms_categories'] ) : array();
242
		$this->tags       = isset( $request['give_forms_tags'] ) ? give_clean( $request['give_forms_tags'] ) : array();
243
	}
244
}
245