1 <?php
2 3 4 5 6 7 8 9
10
11 if ( ! defined( 'ABSPATH' ) ) exit;
12
13 if ( class_exists( 'WP_Importer' ) ) {
14 class WC_Tax_Rate_Importer extends WP_Importer {
15
16 var $id;
17 var $file_url;
18 var $import_page;
19 var $delimiter;
20 var $posts = array();
21 var $imported;
22 var $skipped;
23
24 25 26 27 28 29
30 public function __construct() {
31 $this->import_page = 'woocommerce_tax_rate_csv';
32 }
33
34 35 36 37 38
39 function dispatch() {
40 $this->header();
41
42 if ( ! empty( $_POST['delimiter'] ) )
43 $this->delimiter = stripslashes( trim( $_POST['delimiter'] ) );
44
45 if ( ! $this->delimiter )
46 $this->delimiter = ',';
47
48 $step = empty( $_GET['step'] ) ? 0 : (int) $_GET['step'];
49 switch ( $step ) {
50 case 0:
51 $this->greet();
52 break;
53 case 1:
54 check_admin_referer( 'import-upload' );
55 if ( $this->handle_upload() ) {
56
57 if ( $this->id )
58 $file = get_attached_file( $this->id );
59 else
60 $file = ABSPATH . $this->file_url;
61
62 add_filter( 'http_request_timeout', array( $this, 'bump_request_timeout' ) );
63
64 if ( function_exists( 'gc_enable' ) )
65 gc_enable();
66
67 @set_time_limit(0);
68 @ob_flush();
69 @flush();
70
71 $this->import( $file );
72 }
73 break;
74 }
75 $this->footer();
76 }
77
78 79 80 81 82 83 84 85
86 function format_data_from_csv( $data, $enc ) {
87 return ( $enc == 'UTF-8' ) ? $data : utf8_encode( $data );
88 }
89
90 91 92 93 94 95 96
97 function import( $file ) {
98 global $woocommerce, $wpdb;
99
100 $this->imported = $this->skipped = 0;
101
102 if ( ! is_file($file) ) {
103 echo '<p><strong>' . __( 'Sorry, there has been an error.', 'woocommerce' ) . '</strong><br />';
104 echo __( 'The file does not exist, please try again.', 'woocommerce' ) . '</p>';
105 $this->footer();
106 die();
107 }
108
109 $new_rates = array();
110
111 ini_set( 'auto_detect_line_endings', '1' );
112
113 if ( ( $handle = fopen( $file, "r" ) ) !== FALSE ) {
114
115 $header = fgetcsv( $handle, 0, $this->delimiter );
116
117 if ( sizeof( $header ) == 10 ) {
118
119 $loop = 0;
120
121 while ( ( $row = fgetcsv( $handle, 0, $this->delimiter ) ) !== FALSE ) {
122
123 list( $country, $state, $postcode, $city, $rate, $name, $priority, $compound, $shipping, $class ) = $row;
124
125 $country = trim( strtoupper( $country ) );
126 $state = trim( strtoupper( $state ) );
127
128 if ( $country == '*' )
129 $country = '';
130 if ( $state == '*' )
131 $state = '';
132 if ( $class == 'standard' )
133 $class = '';
134
135 $wpdb->insert(
136 $wpdb->prefix . "woocommerce_tax_rates",
137 array(
138 'tax_rate_country' => $country,
139 'tax_rate_state' => $state,
140 'tax_rate' => wc_format_decimal( $rate, 4 ),
141 'tax_rate_name' => trim( $name ),
142 'tax_rate_priority' => absint( $priority ),
143 'tax_rate_compound' => $compound ? 1 : 0,
144 'tax_rate_shipping' => $shipping ? 1 : 0,
145 'tax_rate_order' => $loop,
146 'tax_rate_class' => sanitize_title( $class )
147 )
148 );
149
150 $tax_rate_id = $wpdb->insert_id;
151
152 $postcode = wc_clean( $postcode );
153 $postcodes = explode( ';', $postcode );
154 $postcodes = array_map( 'strtoupper', array_map( 'wc_clean', $postcodes ) );
155 foreach( $postcodes as $postcode ) {
156 if ( ! empty( $postcode ) && $postcode != '*' ) {
157 $wpdb->insert(
158 $wpdb->prefix . "woocommerce_tax_rate_locations",
159 array(
160 'location_code' => $postcode,
161 'tax_rate_id' => $tax_rate_id,
162 'location_type' => 'postcode',
163 )
164 );
165 }
166 }
167
168 $city = wc_clean( $city );
169 $cities = explode( ';', $city );
170 $cities = array_map( 'strtoupper', array_map( 'wc_clean', $cities ) );
171 foreach( $cities as $city ) {
172 if ( ! empty( $city ) && $city != '*' ) {
173 $wpdb->insert(
174 $wpdb->prefix . "woocommerce_tax_rate_locations",
175 array(
176 'location_code' => $city,
177 'tax_rate_id' => $tax_rate_id,
178 'location_type' => 'city',
179 )
180 );
181 }
182 }
183
184 $loop ++;
185 $this->imported++;
186 }
187
188 } else {
189
190 echo '<p><strong>' . __( 'Sorry, there has been an error.', 'woocommerce' ) . '</strong><br />';
191 echo __( 'The CSV is invalid.', 'woocommerce' ) . '</p>';
192 $this->footer();
193 die();
194
195 }
196
197 fclose( $handle );
198 }
199
200
201 echo '<div class="updated settings-error below-h2"><p>
202 '.sprintf( __( 'Import complete - imported <strong>%s</strong> tax rates and skipped <strong>%s</strong>.', 'woocommerce' ), $this->imported, $this->skipped ).'
203 </p></div>';
204
205 $this->import_end();
206 }
207
208 209 210
211 function import_end() {
212 echo '<p>' . __( 'All done!', 'woocommerce' ) . ' <a href="' . admin_url('admin.php?page=wc-settings&tab=tax') . '">' . __( 'View Tax Rates', 'woocommerce' ) . '</a>' . '</p>';
213
214 do_action( 'import_end' );
215 }
216
217 218 219 220 221 222
223 function handle_upload() {
224
225 if ( empty( $_POST['file_url'] ) ) {
226
227 $file = wp_import_handle_upload();
228
229 if ( isset( $file['error'] ) ) {
230 echo '<p><strong>' . __( 'Sorry, there has been an error.', 'woocommerce' ) . '</strong><br />';
231 echo esc_html( $file['error'] ) . '</p>';
232 return false;
233 }
234
235 $this->id = (int) $file['id'];
236
237 } else {
238
239 if ( file_exists( ABSPATH . $_POST['file_url'] ) ) {
240
241 $this->file_url = esc_attr( $_POST['file_url'] );
242
243 } else {
244
245 echo '<p><strong>' . __( 'Sorry, there has been an error.', 'woocommerce' ) . '</strong></p>';
246 return false;
247
248 }
249
250 }
251
252 return true;
253 }
254
255 256 257 258 259 260
261 function header() {
262 echo '<div class="wrap"><div class="icon32 icon32-woocommerce-importer" id="icon-woocommerce"><br></div>';
263 echo '<h2>' . __( 'Import Tax Rates', 'woocommerce' ) . '</h2>';
264 }
265
266 267 268 269 270 271
272 function () {
273 echo '</div>';
274 }
275
276 277 278 279 280 281
282 function greet() {
283
284 echo '<div class="narrow">';
285 echo '<p>' . __( 'Hi there! Upload a CSV file containing tax rates to import the contents into your shop. Choose a .csv file to upload, then click "Upload file and import".', 'woocommerce' ).'</p>';
286
287 echo '<p>' . sprintf( __( 'Tax rates need to be defined with columns in a specific order (10 columns). <a href="%s">Click here to download a sample</a>.', 'woocommerce' ), WC()->plugin_url() . '/dummy-data/sample_tax_rates.csv' ) . '</p>';
288
289 $action = 'admin.php?import=woocommerce_tax_rate_csv&step=1';
290
291 $bytes = apply_filters( 'import_upload_size_limit', wp_max_upload_size() );
292 $size = size_format( $bytes );
293 $upload_dir = wp_upload_dir();
294 if ( ! empty( $upload_dir['error'] ) ) :
295 ?><div class="error"><p><?php _e( 'Before you can upload your import file, you will need to fix the following error:', 'woocommerce' ); ?></p>
296 <p><strong><?php echo $upload_dir['error']; ?></strong></p></div><?php
297 else :
298 ?>
299 <form enctype="multipart/form-data" id="import-upload-form" method="post" action="<?php echo esc_attr(wp_nonce_url($action, 'import-upload')); ?>">
300 <table class="form-table">
301 <tbody>
302 <tr>
303 <th>
304 <label for="upload"><?php _e( 'Choose a file from your computer:', 'woocommerce' ); ?></label>
305 </th>
306 <td>
307 <input type="file" id="upload" name="import" size="25" />
308 <input type="hidden" name="action" value="save" />
309 <input type="hidden" name="max_file_size" value="<?php echo $bytes; ?>" />
310 <small><?php printf( __('Maximum size: %s', 'woocommerce' ), $size ); ?></small>
311 </td>
312 </tr>
313 <tr>
314 <th>
315 <label for="file_url"><?php _e( 'OR enter path to file:', 'woocommerce' ); ?></label>
316 </th>
317 <td>
318 <?php echo ' ' . ABSPATH . ' '; ?><input type="text" id="file_url" name="file_url" size="25" />
319 </td>
320 </tr>
321 <tr>
322 <th><label><?php _e( 'Delimiter', 'woocommerce' ); ?></label><br/></th>
323 <td><input type="text" name="delimiter" placeholder="," size="2" /></td>
324 </tr>
325 </tbody>
326 </table>
327 <p class="submit">
328 <input type="submit" class="button" value="<?php esc_attr_e( 'Upload file and import', 'woocommerce' ); ?>" />
329 </p>
330 </form>
331 <?php
332 endif;
333
334 echo '</div>';
335 }
336
337 338 339 340 341
342 function bump_request_timeout( $val ) {
343 return 60;
344 }
345 }
346 }
347