PluginProbe
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More / 2.2.2
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More v2.2.2
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 1.0.5 1.0.6 All 66 releases
better-payment / includes / Classes / Import.php

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

185 lines 8.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\Admin\DB;
6 use Better_Payment\Lite\Controller;
7 use Better_Payment\Lite\Models\TransactionModel;
8 use Better_Payment\Lite\Traits\Helper;
9 use Exception;
10 use League\Csv\Reader;
11 use League\Csv\Statement;
12
13 /**
14 * Exit if accessed directly
15 */
16 if (!defined('ABSPATH')) {
17 exit;
18 }
19
20 /**
21 * Export handler class
22 *
23 * @since 0.0.4
24 */
25 class Import extends Controller{
26
27 use Helper;
28 /**
29 * Class constructor
30 *
31 * @since 0.0.4
32 */
33 public function __construct() {
34
35 }
36
37 public function import_transactions() {
38 $message = __('Invalid File!', 'better-payment');
39
40 $invalid_nonce = ! wp_verify_nonce( $_REQUEST['nonce'], 'better_payment_transaction_import_nonce' );
41 $invalid_access = ! current_user_can('manage_options') || ! isset( $_FILES['better-payment-transaction-import-input'] );
42
43 if ( $invalid_nonce || $invalid_access ) {
44 set_transient('better_payment_import_transactions_error', esc_html( $message ), 30);
45 wp_safe_redirect( $_SERVER['HTTP_REFERER'] );
46 exit;
47 }
48
49 $file = $_FILES['better-payment-transaction-import-input'];
50
51 try {
52 $this->validate_csv_file($file);
53 $csv = Reader::createFromPath($file['tmp_name'], 'r');
54 $csv->setHeaderOffset(0);
55
56 // Header validation
57 $csvHeader = $csv->getHeader();
58 if ( isset( $csvHeader[0] ) && false !== strpos( $csvHeader[0], ';' ) ) {
59 $csv->setDelimiter(';');
60 $csvHeader = $csv->getHeader();
61 }
62
63 if ( ! is_array( $csvHeader ) || ! count( $csvHeader) ){
64 throw new Exception( $message );
65 }
66
67 $csvHeader = array_map('sanitize_text_field', $csvHeader);
68
69 $exportObj = new Export();
70 $allowedHeaders = $exportObj->export_transactions_heading();
71
72 if ( count( $allowedHeaders ) !== count( $csvHeader ) ) {
73 throw new Exception( $message );
74 }
75
76 // Fetch rows
77 $stmt = Statement::create()->offset(0)->limit(3000);
78 $records = $stmt->process($csv);
79
80 $data = array();
81
82 foreach ( $records as $record ) {
83 $better_form_fields = [
84 'primary_first_name' => sanitize_text_field( $record['name'] ),
85 'email' => sanitize_email( $record['email'] ),
86 'source' => sanitize_text_field( $record['source'] ),
87 'is_imported' => intval(1),
88 ];
89
90 if ( ! empty( $record['payment_type'] ) && strtolower($record['payment_type']) === 'subscription' ) {
91 $better_form_fields_selected = isset( $record['form_fields_info'] ) ? maybe_unserialize( $record['form_fields_info'] ) : [];
92
93 $better_form_fields['subscription_id'] = isset( $better_form_fields_selected['subscription_id'] ) ? sanitize_text_field( $better_form_fields_selected['subscription_id'] ) : '';
94 $better_form_fields['subscription_customer_id'] = isset( $better_form_fields_selected['subscription_customer_id'] ) ? sanitize_text_field( $better_form_fields_selected['subscription_customer_id'] ) : '';
95 $better_form_fields['subscription_plan_id'] = isset( $better_form_fields_selected['subscription_plan_id'] ) ? sanitize_text_field( $better_form_fields_selected['subscription_plan_id'] ) : '';
96 $better_form_fields['subscription_interval'] = isset( $better_form_fields_selected['subscription_interval'] ) ? sanitize_text_field( $better_form_fields_selected['subscription_interval'] ) : '';
97 $better_form_fields['subscription_current_period_start'] = isset( $better_form_fields_selected['subscription_current_period_start'] ) ? sanitize_text_field( $better_form_fields_selected['subscription_current_period_start'] ) : '';
98 $better_form_fields['subscription_current_period_end'] = isset( $better_form_fields_selected['subscription_current_period_end'] ) ? sanitize_text_field( $better_form_fields_selected['subscription_current_period_end'] ) : '';
99 $better_form_fields['subscription_status'] = isset( $better_form_fields_selected['subscription_status'] ) ? sanitize_text_field( $better_form_fields_selected['subscription_status'] ) : '';
100 $better_form_fields['subscription_created_date'] = isset( $better_form_fields_selected['subscription_created_date'] ) ? sanitize_text_field( $better_form_fields_selected['subscription_created_date'] ) : '';
101 $better_form_fields['is_payment_split_payment'] = isset( $better_form_fields_selected['is_payment_split_payment'] ) ? sanitize_text_field( $better_form_fields_selected['is_payment_split_payment'] ) : '';
102 }
103
104 $amount_currency_array = explode(' ', $record['amount']);
105 $currency = is_array($amount_currency_array) && count($amount_currency_array) > 1 ? $amount_currency_array[0] : esc_html('USD');
106 $amount = is_array($amount_currency_array) && count($amount_currency_array) > 1 ? $amount_currency_array[1] : $record['amount'];
107
108 $data[] = array(
109 'email' => sanitize_email( $record['email'] ),
110 'currency' => sanitize_text_field( $currency ),
111 'amount' => floatval( $amount ),
112 // 'payment_type' => sanitize_text_field( $record['payment_type'] ),
113 'transaction_id' => sanitize_text_field( $record['transaction_id'] ),
114 'source' => sanitize_text_field( $record['source'] ),
115 'status' => sanitize_text_field( $record['status'] ),
116 'payment_date' => date( 'Y-m-d H:i:s', strtotime( sanitize_text_field( $record['payment_date'] ) ) ),
117 'form_fields_info' => maybe_serialize( $better_form_fields ),
118 );
119 }
120
121 if ( empty( $data ) ) {
122 throw new Exception( __('No valid records found in the CSV file!', 'better-payment') );
123 }
124
125 $table = DB::get_table_name();
126 $rowsInserted = $this->bulk_insert_transactions( $table, $data );
127 if ( $rowsInserted !== false && $rowsInserted > 0 ) {
128 set_transient('better_payment_import_transactions_success', esc_html__( $rowsInserted . ' rows inserted successfully!', 'better-payment' ), 30);
129 } else {
130 throw new Exception( __('Failed to insert records into database!', 'better-payment') );
131 }
132 } catch (\Exception $exception) {
133 set_transient('better_payment_import_transactions_error', esc_html( $exception->getMessage() ), 30);
134 wp_safe_redirect( $_SERVER['HTTP_REFERER'] );
135 exit;
136 }
137
138 wp_safe_redirect( $_SERVER['HTTP_REFERER'] );
139 exit;
140 }
141
142 public function validate_csv_file( $file ){
143 $message = __('Invalid File!', 'better-payment');
144
145 if ( empty( $file ) ){
146 throw new Exception( $message );
147 }
148
149 $extension = pathinfo( $file['name'], PATHINFO_EXTENSION );
150 $allowed_extensions = ['csv'];
151
152 if ( empty( $extension ) || ! in_array( $extension, $allowed_extensions ) ) {
153 throw new Exception( $message );
154 }
155
156 return true;
157 }
158
159 public function bulk_insert_transactions($table, $rows) {
160 global $wpdb;
161
162 $headers = array_keys($rows[0]);
163 asort($headers);
164 $columnList = '`' . implode('`, `', $headers) . '`';
165
166 $sql = "INSERT INTO `$table` ($columnList) VALUES\n";
167 $placeholders = array();
168 $data = array();
169
170 foreach ($rows as $row) {
171 ksort($row);
172 $rowPlaceholders = array();
173 foreach ($row as $value) {
174 $data[] = $value;
175 $rowPlaceholders[] = is_numeric($value) ? '%d' : '%s';
176 }
177 $placeholders[] = '(' . implode(', ', $rowPlaceholders) . ')';
178 }
179
180 $sql .= implode(",\n", $placeholders);
181
182 $rowsInserted = $wpdb->query($wpdb->prepare($sql, $data));
183 return $rowsInserted;
184 }
185 }