Passed
Push — master ( 190ea9...cc57e9 )
by Brian
04:04
created

GetPaid_Subscription_Exporter::export()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 12
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 17
rs 9.8666
1
<?php
2
/**
3
 * Contains the class that exports subscriptions.
4
 *
5
 *
6
 */
7
8
defined( 'ABSPATH' ) || exit;
9
10
/**
11
 * GetPaid_Subscription_Exporter Class.
12
 */
13
class GetPaid_Subscription_Exporter extends GetPaid_Graph_Downloader {
14
15
	/**
16
	 * Retrieves subscription query args.
17
	 * 
18
	 * @param array $args Args to search for.
19
	 * @return array
20
	 */
21
	public function get_subscription_query_args( $args ) {
22
23
		$query_args = array(
24
			'status'      => 'all',
25
			'number'      => -1,
26
			'count_total' => false,
27
			'fields'      => 'all',
28
		);
29
30
		if ( ! empty( $args['status'] ) && in_array( $args['status'], array_keys( getpaid_get_subscription_statuses() ), true ) ) {
31
			$query_args['status'] = wpinv_clean( wpinv_parse_list( $args['status'] ) );
32
		}
33
34
		$date_query = array();
35
		if ( ! empty( $args['to_date'] ) ) {
36
			$date_query['before'] = wpinv_clean( $args['to_date'] );
37
		}
38
39
		if ( ! empty( $args['from_date'] ) ) {
40
			$date_query['after'] = wpinv_clean( $args['from_date'] );
41
		}
42
43
		if ( ! empty( $date_query ) ) {
44
			$date_query['inclusive']          = true;
45
			$query_args['date_created_query'] = array( $date_query );
46
		}
47
48
		return $query_args;
49
	}
50
51
	/**
52
	 * Retrieves subscriptions.
53
	 * 
54
	 * @param array $query_args GetPaid_Subscriptions_Query args.
55
	 * @return WPInv_Subscription[]
56
	 */
57
	public function get_subscriptions( $query_args ) {
58
59
		// Get subscriptions.
60
		$subscriptions = new GetPaid_Subscriptions_Query( $query_args );
61
62
		// Prepare the results.
63
		return $subscriptions->get_results();
64
65
	}
66
67
	/**
68
	 * Handles the actual download.
69
	 *
70
	 */
71
	public function export( $post_type, $args ) {
72
73
		$subscriptions = $this->get_subscriptions( $this->get_subscription_query_args( $args ) );
74
		$stream        = $this->prepare_output();
75
		$headers       = $this->get_export_fields();
76
		$file_type     = $this->prepare_file_type( 'subscriptions' );
77
78
		if ( 'csv' == $file_type ) {
79
			$this->download_csv( $subscriptions, $stream, $headers );
80
		} else if( 'xml' == $file_type ) {
81
			$this->download_xml( $subscriptions, $stream, $headers );
82
		} else {
83
			$this->download_json( $subscriptions, $stream, $headers );
84
		}
85
86
		fclose( $stream );
87
		exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
88
	}
89
90
	/**
91
	 * Prepares a single subscription for download.
92
	 *
93
	 * @param WPInv_Subscription $subscription The subscription to prepare..
94
	 * @param array $fields The fields to stream.
95
	 * @since       1.0.19
96
	 * @return array
97
	 */
98
	public function prepare_row( $subscription, $fields ) {
99
100
		$prepared      = array();
101
		$amount_fields = $this->get_amount_fields();
102
		$invoice       = $subscription->get_parent_payment();
103
104
		foreach ( $fields as $field ) {
105
106
			$value  = '';
107
			$method = "get_$field";
108
109
			if ( 0 === stripos( $field, 'customer' ) || 'currency' === $field ) {
110
111
				if ( method_exists( $invoice, $method ) ) {
112
					$value  = $invoice->$method();
113
				}
114
115
			} else if ( method_exists( $subscription, $method ) ) {
116
				$value  = $subscription->$method();
117
			}
118
119
			if ( in_array( $field, $amount_fields ) ) {
120
				$value  = wpinv_round_amount( wpinv_sanitize_amount( $value ) );
121
			}
122
123
			$prepared[ $field ] = wpinv_clean( $value );
124
125
		}
126
127
		return $prepared;
128
	}
129
130
	/**
131
	 * Retrieves export fields.
132
	 *
133
	 * @since       1.0.19
134
	 * @return array
135
	 */
136
	public function get_export_fields() {
137
138
		$fields = array(
139
			'id',
140
			'currency',
141
			'initial_amount',
142
			'recurring_amount',
143
			'trial_period',
144
			'frequency',
145
			'period',
146
			'bill_times',
147
			'parent_payment_id',
148
			'profile_id',
149
			'product_id',
150
			'status',
151
			'date_created',
152
			'date_expires',
153
154
			'customer_id',
155
			'customer_first_name',
156
			'customer_last_name',
157
			'customer_phone',
158
			'customer_email',
159
			'customer_country',
160
			'customer_city',
161
			'customer_state',
162
			'customer_zip',
163
			'customer_company',
164
			'customer_vat_number',
165
			'customer_address',
166
167
    	);
168
169
		return apply_filters( 'getpaid_subscription_exporter_get_fields', $fields );
170
	}
171
172
	/**
173
	 * Retrieves amount fields.
174
	 *
175
	 * @since       1.0.19
176
	 * @return array
177
	 */
178
	public function get_amount_fields() {
179
180
		$fields = array(
181
			'initial_amount',
182
			'recurring_amount'
183
    	);
184
185
		return apply_filters( 'getpaid_subscription_exporter_get_amount_fields', $fields );
186
	}
187
188
}
189