PluginProbe
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More / 1.0.4
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More v1.0.4
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 1.0.4, at includes/Classes/Export.php

151 lines 5.4 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'], 'better_payment_admin_nonce')) {
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 $all_transactions = $transaction_model->get_transactions();
49
50 $filename = 'better-payment-transactions-' . date('Y-m-d') . '.csv';
51 return $this->transactions_array_to_csv_download($all_transactions, $filename);
52 }
53
54 public function transactions_array_to_csv_download($array, $filename = "export.csv", $delimiter=";") {
55 $bp_helper_obj = new ClassesHelper();
56 $wp_date_format = get_option('date_format');
57 $wp_time_format = get_option('time_format');
58
59 $f = fopen('php://memory', 'w');
60
61 fputcsv($f, $this->export_transactions_heading(), $delimiter);
62
63 foreach ($array as $line) {
64 $line = (array)$line;
65 unset($line['id']);
66 unset($line['order_id']);
67 unset($line['obj_id']);
68 unset($line['refund_info']);
69 unset($line['customer_info']);
70 // unset($line['form_fields_info']);
71 unset($line['referer']);
72
73
74 $line['status'] = esc_html(ucfirst($bp_helper_obj->get_type_by_transaction_status($line['status'], 'v2')));
75 $line['source'] = esc_html( ucfirst( $line['source'] ) );
76 $line['payment_date'] = wp_date($wp_date_format.' '.$wp_time_format, strtotime($line['payment_date']));;
77
78 $line['form_fields_info'] = $this->form_fields_info_formatted($line['form_fields_info']);
79
80 $line['form_fields_info_name'] = isset($line['form_fields_info']['customer_name']) ? $line['form_fields_info']['customer_name'] : '';
81 $line['form_fields_info_email'] = isset($line['form_fields_info']['customer_email']) ? $line['form_fields_info']['customer_email'] : '';
82
83 $line_formatted = array(
84 $line['form_fields_info_name'],
85 $line['form_fields_info_email'],
86 $line['currency'] . ' ' . $line['amount'],
87 $line['transaction_id'],
88 $line['source'],
89 $line['status'],
90 $line['payment_date'],
91 );
92
93 fputcsv($f, $line_formatted, $delimiter);
94 }
95
96 // reset the file pointer to the start of the file
97 fseek($f, 0);
98 // tell the browser it's going to be a csv file
99 header('Content-Type: text/csv');
100 // tell the browser we want to save it instead of displaying it
101 header('Content-Disposition: attachment; filename="'.$filename.'";');
102 // make php send the generated csv lines to the browser
103
104 fpassthru($f);
105 exit;
106 }
107
108 public function export_transactions_heading(){
109 return array(
110 esc_html('name'),
111 esc_html('email'),
112 esc_html('amount'),
113 esc_html('transaction_id'),
114 esc_html('source'),
115 esc_html('status'),
116 esc_html('payment_date'),
117 // 'Order ID',
118 // 'Customer Info',
119 // 'Form Fields Info',
120 // 'Currency',
121 // 'Referer'
122 );
123 }
124
125 public function form_fields_info_formatted($form_fields_info){
126 $bp_form_fields_info = maybe_unserialize($form_fields_info);
127
128 $bp_transaction_customer_name = isset($bp_form_fields_info['primary_first_name']) ? sanitize_text_field($bp_form_fields_info['primary_first_name']) : '';
129 $bp_transaction_customer_name .= ' ';
130 $bp_transaction_customer_name .= isset($bp_form_fields_info['primary_last_name']) ? sanitize_text_field($bp_form_fields_info['primary_last_name']) : '';
131
132 //legacy
133 if( empty($bp_transaction_customer_name) || $bp_transaction_customer_name == ' ' ){
134 $bp_transaction_customer_name = isset($bp_form_fields_info['first_name']) ? sanitize_text_field($bp_form_fields_info['first_name']) : '';
135 $bp_transaction_customer_name .= ' ';
136 $bp_transaction_customer_name .= isset($bp_form_fields_info['last_name']) ? sanitize_text_field($bp_form_fields_info['last_name']) : '';
137 }
138
139 $bp_transaction_customer_email = isset($bp_form_fields_info['primary_email']) ? sanitize_text_field($bp_form_fields_info['primary_email']) : '';
140 //legacy
141 if( empty($bp_transaction_customer_email) ){
142 $bp_transaction_customer_email = isset($bp_form_fields_info['email']) ? sanitize_text_field($bp_form_fields_info['email']) : '';
143 }
144
145 return [
146 'customer_name' => $bp_transaction_customer_name,
147 'customer_email' => $bp_transaction_customer_email
148 ];
149 }
150
151 }