PluginProbe
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More / 1.0.3
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More v1.0.3
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 / Import.php

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

164 lines 5.7 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] ) && str_contains( $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 $amount_currency_array = explode(' ', $record['amount']);
91 $currency = is_array($amount_currency_array) && count($amount_currency_array) > 1 ? $amount_currency_array[0] : esc_html('USD');
92 $amount = is_array($amount_currency_array) && count($amount_currency_array) > 1 ? $amount_currency_array[1] : $record['amount'];
93
94 $data[] = array(
95 'email' => sanitize_email( $record['email'] ),
96 'currency' => sanitize_text_field( $currency ),
97 'amount' => floatval( $amount ),
98 'transaction_id' => sanitize_text_field( $record['transaction_id'] ),
99 'source' => sanitize_text_field( $record['source'] ),
100 'status' => sanitize_text_field( $record['status'] ),
101 'payment_date' => date( 'Y-m-d H:i:s', strtotime( sanitize_text_field( $record['payment_date'] ) ) ),
102 'form_fields_info' => maybe_serialize( $better_form_fields ),
103 );
104 }
105
106 $table = DB::get_table_name();
107 $rowsInserted = $this->bulk_insert_transactions( $table, $data );
108 if ( $rowsInserted) {
109 set_transient('better_payment_import_transactions_success', esc_html__( $rowsInserted . ' rows inserted successfully!', 'better-payment' ), 30);
110 }
111 } catch (\Exception $exception) {
112 set_transient('better_payment_import_transactions_error', esc_html( $exception->getMessage() ), 30);
113 wp_safe_redirect( $_SERVER['HTTP_REFERER'] );
114 exit;
115 }
116
117 wp_safe_redirect( $_SERVER['HTTP_REFERER'] );
118 exit;
119 }
120
121 public function validate_csv_file( $file ){
122 $message = __('Invalid File!', 'better-payment');
123
124 if ( empty( $file ) ){
125 throw new Exception( $message );
126 }
127
128 $extension = pathinfo( $file['name'], PATHINFO_EXTENSION );
129 $allowed_extensions = ['csv'];
130
131 if ( empty( $extension ) || ! in_array( $extension, $allowed_extensions ) ) {
132 throw new Exception( $message );
133 }
134
135 return true;
136 }
137
138 public function bulk_insert_transactions($table, $rows) {
139 global $wpdb;
140
141 $headers = array_keys($rows[0]);
142 asort($headers);
143 $columnList = '`' . implode('`, `', $headers) . '`';
144
145 $sql = "INSERT INTO `$table` ($columnList) VALUES\n";
146 $placeholders = array();
147 $data = array();
148
149 foreach ($rows as $row) {
150 ksort($row);
151 $rowPlaceholders = array();
152 foreach ($row as $key => $value) {
153 $data[] = $value;
154 $rowPlaceholders[] = is_numeric($value) ? '%d' : '%s';
155 }
156 $placeholders[] = '(' . implode(', ', $rowPlaceholders) . ')';
157 }
158
159 $sql .= implode(",\n", $placeholders);
160
161 $rowsInserted = $wpdb->query($wpdb->prepare($sql, $data));
162 return $rowsInserted;
163 }
164 }