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

Give_Batch_Customers_Export   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 153
rs 10
wmc 17
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A csv_cols() 0 12 1
B get_data() 0 69 6
B get_percentage_complete() 0 23 4
B set_properties() 0 6 6
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
 * Batch Customers Export Class
4
 *
5
 * This class handles customer export
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_Customers_Export Class
21
 *
22
 * @since 1.5
23
 */
24
class Give_Batch_Customers_Export extends Give_Batch_Export {
25
26
	/**
27
	 * Our export type. Used for export-type specific filters/actions
28
	 *
29
	 * @var string
30
	 * @since 1.5
31
	 */
32
	public $export_type = 'customers';
33
34
	/**
35
	 * Set the CSV columns
36
	 *
37
	 * @access public
38
	 * @since 1.5
39
	 * @return array $cols All the columns
40
	 */
41
	public function csv_cols() {
42
43
		$cols = array(
44
			'id'        => __( 'ID', 'give' ),
45
			'name'      => __( 'Name', 'give' ),
46
			'email'     => __( 'Email', 'give' ),
47
			'purchases' => __( 'Number of Donations', 'give' ),
48
			'amount'    => __( 'Donor Value', 'give' )
49
		);
50
51
		return $cols;
52
	}
53
54
	/**
55
	 * Get the Export Data
56
	 *
57
	 * @access public
58
	 * @since 1.5
59
	 *   Database API
60
	 * @global object $give_logs EDD Logs Object
61
	 * @return array $data The data for the CSV file
62
	 */
63
	public function get_data() {
64
65
		$data = array();
66
67
		if ( ! empty( $this->form ) ) {
68
69
			// Export customers of a specific product
70
			global $give_logs;
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...
71
72
			$args = array(
73
				'post_parent'    => absint( $this->form ),
74
				'log_type'       => 'sale',
75
				'posts_per_page' => 30,
76
				'paged'          => $this->step
77
			);
78
79
			if ( null !== $this->price_id ) {
80
				$args['meta_query'] = array(
81
					array(
82
						'key'   => '_give_log_price_id',
83
						'value' => (int) $this->price_id
84
					)
85
				);
86
			}
87
88
			$logs = $give_logs->get_connected_logs( $args );
89
90
			if ( $logs ) {
91
				foreach ( $logs as $log ) {
92
93
					$payment_id  = get_post_meta( $log->ID, '_give_log_payment_id', true );
94
					$customer_id = give_get_payment_customer_id( $payment_id );
95
					$customer    = new Give_Customer( $customer_id );
96
97
					$data[] = array(
98
						'id'        => $customer->id,
99
						'name'      => $customer->name,
100
						'email'     => $customer->email,
101
						'purchases' => $customer->purchase_count,
102
						'amount'    => give_format_amount( $customer->purchase_value ),
103
					);
104
				}
105
			}
106
107
		} else {
108
109
			// Export all customers
110
			$offset    = 30 * ( $this->step - 1 );
111
			$customers = Give()->customers->get_customers( array( 'number' => 30, 'offset' => $offset ) );
112
113
			$i = 0;
114
115
			foreach ( $customers as $customer ) {
116
117
				$data[ $i ]['id']        = $customer->id;
118
				$data[ $i ]['name']      = $customer->name;
119
				$data[ $i ]['email']     = $customer->email;
120
				$data[ $i ]['purchases'] = $customer->purchase_count;
121
				$data[ $i ]['amount']    = give_format_amount( $customer->purchase_value );
122
123
				$i ++;
124
			}
125
		}
126
127
		$data = apply_filters( 'give_export_get_data', $data );
128
		$data = apply_filters( 'give_export_get_data_' . $this->export_type, $data );
129
130
		return $data;
131
	}
132
133
	/**
134
	 * Return the calculated completion percentage
135
	 *
136
	 * @since 1.5
137
	 * @return int
138
	 */
139
	public function get_percentage_complete() {
140
141
		$percentage = 0;
142
143
		// We can't count the number when getting them for a specific form
144
		if ( empty( $this->form ) ) {
145
146
			$total = Give()->customers->count();
147
148
			if ( $total > 0 ) {
149
150
				$percentage = ( ( 30 * $this->step ) / $total ) * 100;
151
152
			}
153
154
		}
155
156
		if ( $percentage > 100 ) {
157
			$percentage = 100;
158
		}
159
160
		return $percentage;
161
	}
162
163
	/**
164
	 * Set the properties specific to the Customers export
165
	 *
166
	 * @since 1.5
167
	 *
168
	 * @param array $request The Form Data passed into the batch processing
169
	 */
170
	public function set_properties( $request ) {
171
		$this->start    = isset( $request['start'] ) ? sanitize_text_field( $request['start'] ) : '';
172
		$this->end      = isset( $request['end'] ) ? sanitize_text_field( $request['end'] ) : '';
173
		$this->form = isset( $request['form'] ) ? absint( $request['form'] ) : null;
174
		$this->price_id = ! empty( $request['give_price_option'] ) && 0 !== $request['give_price_option'] ? absint( $request['give_price_option'] ) : null;
175
	}
176
}
177