PluginProbe
Order Tracking – WordPress Status Tracking Plugin / 3.2.2
Order Tracking – WordPress Status Tracking Plugin v3.2.2
3.5.4 3.5.3 3.5.2 3.5.1 3.5.0 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.2.5 3.3.0 3.3.1 3.3.10 3.3.11 3.3.12 3.3.13 3.3.14 3.3.15 All 235 releases
order-tracking / includes / Import.class.php

Import.class.php in Order Tracking – WordPress Status Tracking Plugin 3.2.2, at includes/Import.class.php

320 lines 14.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Class to handle importing orders into the plugin
5 */
6
7 if ( !defined( 'ABSPATH' ) )
8 exit;
9
10 if (!class_exists('ComposerAutoloaderInit4618f5c41cf5e27cc7908556f031e4d4')) {require_once EWD_OTP_PLUGIN_DIR . '/lib/PHPSpreadsheet/vendor/autoload.php';}
11 use PhpOffice\PhpSpreadsheet\Spreadsheet;
12 class ewdotpImport {
13
14 public $status;
15 public $message;
16
17 public function __construct() {
18 add_action( 'admin_menu', array($this, 'register_install_screen' ));
19
20 if ( isset( $_POST['ewdotpImport'] ) ) { add_action( 'admin_init', array($this, 'import_orders' )); }
21 }
22
23 public function register_install_screen() {
24 global $ewd_otp_controller;
25
26 add_submenu_page(
27 'ewd-otp-orders',
28 'Import Menu',
29 'Import',
30 $ewd_otp_controller->settings->get_setting( 'access-role' ),
31 'ewd-otp-import',
32 array($this, 'display_import_screen')
33 );
34 }
35
36 public function display_import_screen() {
37 global $ewd_otp_controller;
38
39 $import_permission = $ewd_otp_controller->permissions->check_permission( 'import' );
40 ?>
41 <div class='wrap'>
42 <h2>Import</h2>
43 <?php if ( $import_permission ) { ?>
44 <form method='post' enctype="multipart/form-data">
45
46 <?php wp_nonce_field( 'EWD_OTP_Import', 'EWD_OTP_Import_Nonce' ); ?>
47
48 <p>
49 <label for="ewd_otp_orders_spreadsheet"><?php _e( 'Spreadsheet Containing Orders', 'order-tracking' ) ?></label><br />
50 <input name="ewd_otp_orders_spreadsheet" type="file" value=""/>
51 </p>
52 <input type='submit' name='ewdotpImport' value='Import Orders' class='button button-primary' />
53 </form>
54 <?php } else { ?>
55 <div class='ewd-otp-premium-locked'>
56 <a href="https://www.etoilewebdesign.com/license-payment/?Selected=OTP&Quantity=1&utm_source=otp_import" target="_blank">Upgrade</a> to the premium version to use this feature
57 </div>
58 <?php } ?>
59 </div>
60 <?php }
61
62 public function import_orders() {
63 global $ewd_otp_controller;
64
65 if ( ! current_user_can( 'edit_posts' ) ) { return; }
66
67 if ( ! isset( $_POST['EWD_OTP_Import_Nonce'] ) ) { return; }
68
69 if ( ! wp_verify_nonce( $_POST['EWD_OTP_Import_Nonce'], 'EWD_OTP_Import' ) ) { return; }
70
71 $update = $this->handle_spreadsheet_upload();
72
73 $custom_fields = $ewd_otp_controller->settings->get_order_custom_fields();
74
75 if ( $update['message_type'] != 'Success' ) :
76 $this->status = false;
77 $this->message = $update['message'];
78
79 add_action( 'admin_notices', array( $this, 'display_notice' ) );
80
81 return;
82 endif;
83
84 $excel_url = EWD_OTP_PLUGIN_DIR . '/order-sheets/' . $update['filename'];
85
86 // Build the workbook object out of the uploaded spreadsheet
87 $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load( $excel_url );
88
89 // Create a worksheet object out of the product sheet in the workbook
90 $sheet = $spreadsheet->getActiveSheet();
91
92 $allowable_custom_fields = array();
93 foreach ( $custom_fields as $custom_field ) { $allowable_custom_fields[] = $custom_field->name; }
94 //List of fields that can be accepted via upload
95 $allowed_fields = array( 'Name', 'Number', 'Order Status', 'Location', 'Display', 'Notes Public', 'Notes Private', 'Email', 'Show in Admin Table', 'Sales Rep ID', 'Customer ID' );
96
97 // Get column names
98 $highest_column = $sheet->getHighestColumn();
99 $highest_column_index = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString( $highest_column );
100 for ( $column = 1; $column <= $highest_column_index; $column++ ) {
101
102 if ( trim( $sheet->getCellByColumnAndRow( $column, 1 )->getValue() ) == 'Name' ) { $name_column = $column; }
103 if ( trim( $sheet->getCellByColumnAndRow( $column, 1 )->getValue() ) == 'Number' ) { $number_column = $column; }
104 if ( trim( $sheet->getCellByColumnAndRow( $column, 1 )->getValue() ) == 'Order Status' ) { $status_column = $column; }
105 if ( trim( $sheet->getCellByColumnAndRow( $column, 1 )->getValue() ) == 'Location' ) { $location_column = $column; }
106 if ( trim( $sheet->getCellByColumnAndRow( $column, 1 )->getValue() ) == 'Display' or trim( $sheet->getCellByColumnAndRow( $column, 1 )->getValue() ) == 'Show in Admin Table' ) { $display_column = $column; }
107 if ( trim( $sheet->getCellByColumnAndRow( $column, 1 )->getValue() ) == 'Notes Public' ) { $public_notes_column = $column; }
108 if ( trim( $sheet->getCellByColumnAndRow( $column, 1 )->getValue() ) == 'Notes Private' ) { $private_notes_column = $column; }
109 if ( trim( $sheet->getCellByColumnAndRow( $column, 1 )->getValue() ) == 'Email' ) { $email_column = $column; }
110 if ( trim( $sheet->getCellByColumnAndRow( $column, 1 )->getValue() ) == 'Sales Rep ID' ) { $sales_rep_id_column = $column; }
111 if ( trim( $sheet->getCellByColumnAndRow( $column, 1 )->getValue() ) == 'Customer ID' ) { $customer_id_column = $column; }
112
113 foreach ( $custom_fields as $custom_field ) {
114
115 if ( trim( $sheet->getCellByColumnAndRow( $column, 1 )->getValue() ) == $custom_field->name ) { $custom_field->column = $column; }
116 }
117 }
118
119 $name_column = ! empty( $name_column ) ? $name_column : -1;
120 $number_column = ! empty( $number_column ) ? $number_column : -1;
121 $status_column = ! empty( $status_column ) ? $status_column : -1;
122 $location_column = ! empty( $location_column ) ? $location_column : -1;
123 $display_column = ! empty( $display_column ) ? $display_column : -1;
124 $public_notes_column = ! empty( $public_notes_column ) ? $public_notes_column : -1;
125 $private_notes_column = ! empty( $private_notes_column ) ? $private_notes_column : -1;
126 $email_column = ! empty( $email_column ) ? $email_column : -1;
127 $sales_rep_id_column = ! empty( $sales_rep_id_column ) ? $sales_rep_id_column : -1;
128 $customer_id_column = ! empty( $customer_id_column ) ? $customer_id_column : -1;
129
130 // Put the spreadsheet data into a multi-dimensional array to facilitate processing
131 $highest_row = $sheet->getHighestRow();
132 for ( $row = 2; $row <= $highest_row; $row++ ) {
133 for ( $column = 1; $column <= $highest_column_index; $column++ ) {
134 $data[$row][$column] = $sheet->getCellByColumnAndRow( $column, $row )->getValue();
135 }
136 }
137
138 // Create the query to insert the products one at a time into the database and then run it
139 foreach ( $data as $order_data ) {
140
141 // Save the data into an array, so that an order can be updated based on the order
142 // number if it exists already
143 $order_data_array = array(
144 'custom_fields' => array()
145 );
146
147 foreach ( $order_data as $col_index => $value ) {
148
149 if ( $col_index == $name_column ) { $order_data_array['name'] = sanitize_text_field( $value ); }
150 elseif ( $col_index == $number_column ) { $order_data_array['number'] = sanitize_text_field( $value ); }
151 elseif ( $col_index == $status_column ) { $order_data_array['status'] = $order_data['external_status'] = sanitize_text_field( $value ); }
152 elseif ( $col_index == $location_column ) { $order_data_array['location'] = sanitize_text_field( $value ); }
153 elseif ( $col_index == $display_column ) { $order_data_array['display'] = ( strtolower( $value ) == 'no' ? false : true ); }
154 elseif ( $col_index == $public_notes_column ) { $order_data_array['notes_public'] = sanitize_textarea_field( $value ); }
155 elseif ( $col_index == $private_notes_column ) { $order_data_array['notes_private'] = sanitize_textarea_field( $value ); }
156 elseif ( $col_index == $email_column ) { $order_data_array['email'] = sanitize_email( $value ); }
157 elseif ( $col_index == $sales_rep_id_column ) { $order_data_array['sales_rep'] = intval( $value ); }
158 elseif ( $col_index == $customer_id_column ) { $order_data_array['customer'] = intval( $value ); }
159 else {
160
161 foreach ( $custom_fields as $custom_field ) {
162
163 if ( $col_index == $custom_field->column ) { $order_data_array['custom_fields'][ $custom_field->id ] = sanitize_text_field( $value ); }
164 }
165 }
166 }
167
168 // Create a new order object, and assign the imported values to it
169 $order = new ewdotpOrder();
170
171 $order_status = null;
172
173 if ( ! empty( $order_data_array['number'] ) ) {
174
175 $db_order_data = $ewd_otp_controller->order_manager->get_order_from_tracking_number( $order_data_array['number'] );
176
177 if ( $db_order_data ) {
178
179 $order->load_order( $db_order_data );
180
181 $order_status = $ewd_otp_controller->order_manager->get_order_field( 'Order_Status', $order->id );
182 }
183 }
184
185 if ( empty( $order->id ) ) { $order->display = true; }
186
187 if ( ! empty( $order_data_array['name'] ) ) { $order->name = $order_data_array['name']; }
188 if ( ! empty( $order_data_array['number'] ) ) { $order->number = $order_data_array['number']; }
189 if ( ! empty( $order_data_array['status'] ) ) { $order->status = $order->external_status = $order_data_array['status']; }
190 if ( ! empty( $order_data_array['location'] ) ) { $order->location = $order_data_array['location']; }
191 if ( isset( $order_data_array['display'] ) ) { $order->display_column = $order_data_array['display']; }
192 if ( ! empty( $order_data_array['notes_public'] ) ) { $order->notes_public = $order_data_array['notes_public']; }
193 if ( ! empty( $order_data_array['notes_private'] ) ) { $order->notes_private = $order_data_array['notes_private']; }
194 if ( ! empty( $order_data_array['email'] ) ) { $order->email = $order_data_array['email']; }
195 if ( ! empty( $order_data_array['sales_rep'] ) ) { $order->sales_rep = $order_data_array['sales_rep']; }
196 if ( ! empty( $order_data_array['customer'] ) ) { $order->customer = $order_data_array['customer']; }
197 if ( ! empty( $order_data_array['custom_fields'] ) ) {
198
199 foreach ( $order_data_array['custom_fields'] as $field_id => $field_value ) {
200
201 $order->custom_fields[ $field_id ] = $field_value;
202 }
203 }
204
205 if ( empty( $order->customer ) and ! empty( $order->email ) and $ewd_otp_controller->settings->get_setting( 'allow-assign-orders-to-customers' ) ) {
206
207 $order->customer = $ewd_otp_controller->customer_manager->get_customer_id_from_email( $order->email );
208 }
209
210 if ( empty( $order->id ) ) {
211
212 $order->insert_order();
213
214 $order->insert_order_status();
215
216 do_action( 'ewd_otp_admin_order_inserted', $order );
217 }
218 else {
219
220 $order->update_order();
221
222 if ( ! empty( $order_data_array['status'] ) and $order_data_array['status'] != $order_status ) {
223
224 $order->insert_order_status();
225
226 do_action( 'ewd_otp_status_updated', $order );
227 }
228 }
229 }
230
231 $this->status = true;
232 $this->message = __( 'Orders added successfully.', 'order-tracking' );
233
234 add_action( 'admin_notices', array( $this, 'display_notice' ) );
235 }
236
237 function handle_spreadsheet_upload() {
238 /* Test if there is an error with the uploaded spreadsheet and return that error if there is */
239 if ( ! empty( $_FILES['ewd_otp_orders_spreadsheet']['error'] ) ) {
240
241 switch( $_FILES['ewd_otp_orders_spreadsheet']['error'] ) {
242
243 case '1':
244 $error = __( 'The uploaded file exceeds the upload_max_filesize directive in php.ini', 'order-tracking' );
245 break;
246 case '2':
247 $error = __( 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form', 'order-tracking' );
248 break;
249 case '3':
250 $error = __( 'The uploaded file was only partially uploaded', 'order-tracking' );
251 break;
252 case '4':
253 $error = __( 'No file was uploaded.', 'order-tracking' );
254 break;
255
256 case '6':
257 $error = __( 'Missing a temporary folder', 'order-tracking' );
258 break;
259 case '7':
260 $error = __( 'Failed to write file to disk', 'order-tracking' );
261 break;
262 case '8':
263 $error = __( 'File upload stopped by extension', 'order-tracking' );
264 break;
265 case '999':
266 default:
267 $error = __( 'No error code avaiable', 'order-tracking' );
268 }
269 }
270 /* Make sure that the file exists */
271 elseif ( empty($_FILES['ewd_otp_orders_spreadsheet']['tmp_name']) || $_FILES['ewd_otp_orders_spreadsheet']['tmp_name'] == 'none' ) {
272 $error = __( 'No file was uploaded here..', 'order-tracking' );
273 }
274 /* Move the file and store the URL to pass it onwards*/
275 /* Check that it is a .xls or .xlsx file */
276 if ( ! isset($_FILES['ewd_otp_orders_spreadsheet']['name'] ) or ( ! preg_match("/\.(xls.?)$/", $_FILES['ewd_otp_orders_spreadsheet']['name'] ) and ! preg_match( "/\.(csv.?)$/", $_FILES['ewd_otp_orders_spreadsheet']['name'] ) ) ) {
277 $error = __( 'File must be .csv, .xls or .xlsx', 'order-tracking' );
278 }
279 else {
280 $filename = basename( $_FILES['ewd_otp_orders_spreadsheet']['name'] );
281 $filename = mb_ereg_replace( "([^\w\s\d\-_~,;\[\]\(\).])", '', $filename );
282 $filename = mb_ereg_replace ("([\.]{2,})", '', $filename );
283
284 //for security reason, we force to remove all uploaded file
285 $target_path = EWD_OTP_PLUGIN_DIR . "/order-sheets/";
286
287 $target_path = $target_path . $filename;
288
289 if ( ! move_uploaded_file($_FILES['ewd_otp_orders_spreadsheet']['tmp_name'], $target_path ) ) {
290 $error .= "There was an error uploading the file, please try again!";
291 }
292 else {
293 $excel_file_name = $filename;
294 }
295 }
296
297 /* Pass the data to the appropriate function in Update_Admin_Databases.php to create the products */
298 if ( ! isset( $error ) ) {
299 $update = array( "message_type" => "Success", "filename" => $excel_file_name );
300 }
301 else {
302 $update = array( "message_type" => "Error", "message" => $error );
303 }
304
305 return $update;
306 }
307
308 public function display_notice() {
309
310 if ( $this->status ) {
311
312 echo "<div class='updated'><p>" . esc_html( $this->message ) . "</p></div>";
313 }
314 else {
315
316 echo "<div class='error'><p>" . esc_html( $this->message ) . "</p></div>";
317 }
318 }
319
320 }