Completed
Pull Request — master (#859)
by Devin
19:40
created

Give_Batch_Payments_Export   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 185
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A set_properties() 0 5 4
B csv_cols() 0 30 2
F get_data() 0 79 17
B get_percentage_complete() 0 30 4
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     http://opensource.org/licenses/gpl-2.0.php 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', 'give' ),
48
			'address2'  => esc_html__( 'Address (Line 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__( 'Purchase 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
		//echo json_encode($args ); exit;
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% 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...
103
104
		$payments = give_get_payments( $args );
105
106
		if ( $payments ) {
107
108
			foreach ( $payments as $payment ) {
109
				$payment_meta = give_get_payment_meta( $payment->ID );
110
				$user_info    = give_get_payment_meta_user_info( $payment->ID );
111
				$total        = give_get_payment_amount( $payment->ID );
112
				$user_id      = isset( $user_info['id'] ) && $user_info['id'] != - 1 ? $user_info['id'] : $user_info['email'];
113
				$products     = '';
114
				$skus         = '';
115
116
				if ( is_numeric( $user_id ) ) {
117
					$user = get_userdata( $user_id );
118
				} else {
119
					$user = false;
120
				}
121
122
				$data[] = array(
123
					'id'        => $payment->ID,
124
					'seq_id'    => give_get_payment_number( $payment->ID ),
125
					'email'     => $payment_meta['email'],
126
					'first'     => $user_info['first_name'],
127
					'last'      => $user_info['last_name'],
128
					'address1'  => isset( $user_info['address']['line1'] ) ? $user_info['address']['line1'] : '',
129
					'address2'  => isset( $user_info['address']['line2'] ) ? $user_info['address']['line2'] : '',
130
					'city'      => isset( $user_info['address']['city'] ) ? $user_info['address']['city'] : '',
131
					'state'     => isset( $user_info['address']['state'] ) ? $user_info['address']['state'] : '',
132
					'country'   => isset( $user_info['address']['country'] ) ? $user_info['address']['country'] : '',
133
					'zip'       => isset( $user_info['address']['zip'] ) ? $user_info['address']['zip'] : '',
134
					'form_id'   => isset( $payment_meta['form_id'] ) ? $payment_meta['form_id'] : '',
135
					'form_name' => isset( $payment_meta['form_title'] ) ? $payment_meta['form_title'] : '',
136
					'skus'      => $skus,
137
					'amount'    => html_entity_decode( give_format_amount( $total ) ),
138
					'gateway'   => give_get_gateway_admin_label( get_post_meta( $payment->ID, '_give_payment_gateway', true ) ),
139
					'trans_id'  => give_get_payment_transaction_id( $payment->ID ),
140
					'key'       => $payment_meta['key'],
141
					'date'      => $payment->post_date,
142
					'user'      => $user ? $user->display_name : __( 'guest', 'give' ),
143
					'status'    => give_get_payment_status( $payment, true )
144
				);
145
146
			}
147
148
			$data = apply_filters( 'give_export_get_data', $data );
149
			$data = apply_filters( 'give_export_get_data_' . $this->export_type, $data );
150
151
			return $data;
152
153
		}
154
155
		return false;
156
157
	}
158
159
	/**
160
	 * Return the calculated completion percentage.
161
	 *
162
	 * @since 1.5
163
	 * @return int
164
	 */
165
	public function get_percentage_complete() {
166
167
		$status = $this->status;
168
		$args   = array(
169
			'start-date' => date( 'n/d/Y', strtotime( $this->start ) ),
170
			'end-date'   => date( 'n/d/Y', strtotime( $this->end ) ),
171
		);
172
173
		if ( 'any' == $status ) {
174
175
			$total = array_sum( (array) give_count_payments( $args ) );
176
177
		} else {
178
179
			$total = give_count_payments( $args )->$status;
180
181
		}
182
183
		$percentage = 100;
184
185
		if ( $total > 0 ) {
186
			$percentage = ( ( 30 * $this->step ) / $total ) * 100;
187
		}
188
189
		if ( $percentage > 100 ) {
190
			$percentage = 100;
191
		}
192
193
		return $percentage;
194
	}
195
196
	/**
197
	 * Set the properties specific to the payments export.
198
	 *
199
	 * @since 1.5
200
	 *
201
	 * @param array $request The Form Data passed into the batch processing.
202
	 */
203
	public function set_properties( $request ) {
204
		$this->start  = isset( $request['start'] ) ? sanitize_text_field( $request['start'] ) : '';
205
		$this->end    = isset( $request['end'] ) ? sanitize_text_field( $request['end'] ) : '';
206
		$this->status = isset( $request['status'] ) ? sanitize_text_field( $request['status'] ) : 'complete';
207
	}
208
}
209