PluginProbe
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More / 2.4.0
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More v2.4.0
2.4.1 2.4.0 2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.2 2.2.1 2.2.0 2.1.2 2.1.1 trunk 0.0.1 0.0.2 0.0.3 0.0.4 0.0.5 0.0.6 0.0.7 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 All 68 releases
better-payment / includes / Classes / Export.php

Export.php in Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More 2.4.0, at includes/Classes/Export.php

226 lines 8.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Better_Payment\Lite\Classes;
4
5 use Better_Payment\Lite\Classes\Helper as ClassesHelper;
6 use Better_Payment\Lite\Controller;
7 use Better_Payment\Lite\Models\TransactionModel;
8 use Better_Payment\Lite\Traits\Helper;
9
10 /**
11 * Exit if accessed directly
12 */
13 if (!defined('ABSPATH')) {
14 exit;
15 }
16
17 /**
18 * Export handler class
19 *
20 * @since 0.0.4
21 */
22 class Export extends Controller{
23
24 use Helper;
25 /**
26 * Class constructor
27 *
28 * @since 0.0.4
29 */
30 public function __construct() {
31
32 }
33
34 public function export_transactions() {
35 $response = [];
36
37 if (!wp_verify_nonce($_REQUEST['nonce'], 'wp_rest')) {
38 $response['message'] = __("Access Denied!", 'better-payment');
39 wp_send_json_error($response);
40 }
41
42 if (!current_user_can('manage_options')) {
43 $response['message'] = __("Access Denied!", 'better-payment');
44 wp_send_json_error($response);
45 }
46
47 $transaction_model = new TransactionModel();
48
49 // Check if this is a filtered export
50 $is_filtered = isset($_REQUEST['filtered']) && $_REQUEST['filtered'] === '1';
51
52 if ($is_filtered) {
53 // Build filter arguments
54 $filter_args = [];
55
56 if (!empty($_REQUEST['search_text'])) {
57 $filter_args['search_text'] = sanitize_text_field($_REQUEST['search_text']);
58 }
59
60 if (!empty($_REQUEST['payment_date_from'])) {
61 $filter_args['payment_date_from'] = sanitize_text_field($_REQUEST['payment_date_from']);
62 }
63
64 if (!empty($_REQUEST['payment_date_to'])) {
65 $filter_args['payment_date_to'] = sanitize_text_field($_REQUEST['payment_date_to']);
66 }
67
68 if (!empty($_REQUEST['source'])) {
69 $filter_args['source'] = sanitize_text_field($_REQUEST['source']);
70 }
71
72 if (!empty($_REQUEST['currency'])) {
73 $filter_args['currency'] = sanitize_text_field($_REQUEST['currency']);
74 }
75
76 if (!empty($_REQUEST['status'])) {
77 $filter_args['status'] = sanitize_text_field($_REQUEST['status']);
78 }
79
80 $filter_args['per_page'] = '999999999';
81 // The admin list's v2 tab buckets, so an export of a tab holds exactly the rows that tab
82 // lists. The v1 path matched `completed` against the raw status `completed` only.
83 $all_transactions = $transaction_model->get_transactions($filter_args, 'v2');
84 $filename = 'better-payment-transactions-filtered-' . date('Y-m-d') . '.csv';
85 } else {
86 $all_transactions = $transaction_model->get_transactions();
87 $filename = 'better-payment-transactions-' . date('Y-m-d') . '.csv';
88 }
89
90 return $this->transactions_array_to_csv_download($all_transactions, $filename);
91 }
92
93 public function transactions_array_to_csv_download($array, $filename = "export.csv", $delimiter=";") {
94 $wp_date_format = get_option('date_format');
95 $wp_time_format = get_option('time_format');
96
97 $f = fopen('php://memory', 'w');
98
99 fputcsv($f, $this->export_transactions_heading(), $delimiter);
100
101 foreach ($array as $line) {
102 $line = (array)$line;
103 unset($line['id']);
104 unset($line['order_id']);
105 unset($line['obj_id']);
106 unset($line['refund_info']);
107 unset($line['customer_info']);
108 // unset($line['form_fields_info']);
109 unset($line['referer']);
110
111
112 // From the stored row (form_fields_info still serialized): an unconfirmed pre-2.4.0 free
113 // checkout exports as Incomplete (Helper::transaction_status_type()).
114 $line['status'] = esc_html( ClassesHelper::transaction_status_label( $line ) );
115 $line['source'] = esc_html( ucfirst( $line['source'] ) );
116 $line['payment_date'] = wp_date($wp_date_format.' '.$wp_time_format, strtotime($line['payment_date']));;
117
118 // 'One Time' unless Pro labels the row (better_payment/transaction/payment_type).
119 $line['payment_type'] = ClassesHelper::payment_type_label( maybe_unserialize( $line['form_fields_info'] ) );
120
121 $line['form_fields_info'] = $this->form_fields_info_formatted($line['form_fields_info']);
122
123 $line['form_fields_info_name'] = isset($line['form_fields_info']['customer_name']) ? $line['form_fields_info']['customer_name'] : '';
124 $line['form_fields_info_email'] = isset($line['form_fields_info']['customer_email']) ? $line['form_fields_info']['customer_email'] : '';
125
126 unset($line['form_fields_info']['customer_name']);
127 unset($line['form_fields_info']['customer_email']);
128
129 $line['form_fields_info'] = array_filter($line['form_fields_info'], function($value) {
130 return !empty($value);
131 });
132
133 $line_formatted = array(
134 $line['form_fields_info_name'],
135 $line['form_fields_info_email'],
136 $line['currency'] . ' ' . $line['amount'],
137 $line['payment_type'],
138 $line['transaction_id'],
139 $line['source'],
140 $line['status'],
141 $line['payment_date'],
142 ! empty( $line['form_fields_info'] ) ? maybe_serialize( $line['form_fields_info'] ) : '',
143 );
144
145 fputcsv($f, $line_formatted, $delimiter);
146 }
147
148 // reset the file pointer to the start of the file
149 fseek($f, 0);
150 // tell the browser it's going to be a csv file
151 header('Content-Type: text/csv');
152 // tell the browser we want to save it instead of displaying it
153 header('Content-Disposition: attachment; filename="'.$filename.'";');
154 // make php send the generated csv lines to the browser
155
156 fpassthru($f);
157 exit;
158 }
159
160 public function export_transactions_heading(){
161 return array(
162 esc_html('name'),
163 esc_html('email'),
164 esc_html('amount'),
165 esc_html('payment_type'),
166 esc_html('transaction_id'),
167 esc_html('source'),
168 esc_html('status'),
169 esc_html('payment_date'),
170 esc_html('form_fields_info'),
171 // 'Order ID',
172 // 'Customer Info',
173 // 'Form Fields Info',
174 // 'Currency',
175 // 'Referer'
176 );
177 }
178
179 /**
180 * The fields a transaction contributes to the CSV: customer name and email, plus
181 * whatever `better_payment/export/form_fields_info` adds.
182 *
183 * Pro adds the subscription keys its recurring and split checkouts store. Lite's
184 * copy of that (ProSubscriptionTransactionData::export_fields()) serves only an
185 * active Pro that does not (<= 1.4.3); a free install exports name and email.
186 *
187 * @param mixed $form_fields_info Serialized or unserialized form_fields_info.
188 * @return array
189 */
190 public function form_fields_info_formatted($form_fields_info){
191 $bp_form_fields_info = maybe_unserialize($form_fields_info);
192
193 $bp_transaction_customer_name = isset($bp_form_fields_info['primary_first_name']) ? sanitize_text_field($bp_form_fields_info['primary_first_name']) : '';
194 $bp_transaction_customer_name .= ' ';
195 $bp_transaction_customer_name .= isset($bp_form_fields_info['primary_last_name']) ? sanitize_text_field($bp_form_fields_info['primary_last_name']) : '';
196
197 //legacy
198 if( empty($bp_transaction_customer_name) || $bp_transaction_customer_name == ' ' ){
199 $bp_transaction_customer_name = isset($bp_form_fields_info['first_name']) ? sanitize_text_field($bp_form_fields_info['first_name']) : '';
200 $bp_transaction_customer_name .= ' ';
201 $bp_transaction_customer_name .= isset($bp_form_fields_info['last_name']) ? sanitize_text_field($bp_form_fields_info['last_name']) : '';
202 }
203
204 $bp_transaction_customer_email = isset($bp_form_fields_info['primary_email']) ? sanitize_text_field($bp_form_fields_info['primary_email']) : '';
205 //legacy
206 if( empty($bp_transaction_customer_email) ){
207 $bp_transaction_customer_email = isset($bp_form_fields_info['email']) ? sanitize_text_field($bp_form_fields_info['email']) : '';
208 }
209
210 $formatted = [
211 'customer_name' => $bp_transaction_customer_name,
212 'customer_email' => $bp_transaction_customer_email,
213 ];
214
215 $raw_fields = is_array( $bp_form_fields_info ) ? $bp_form_fields_info : [];
216
217 if ( ProSubscriptionTransactionData::lite_copy_applies() ) {
218 $formatted = ProSubscriptionTransactionData::export_fields( $formatted, $raw_fields );
219 }
220
221 $filtered = apply_filters( 'better_payment/export/form_fields_info', $formatted, $raw_fields );
222
223 return is_array( $filtered ) ? $filtered : $formatted;
224 }
225
226 }