1 <?php
  2 /**
  3  * Tax Rates importer - import tax rates and local tax rates into WooCommerce.
  4  *
  5  * @author      WooThemes
  6  * @category    Admin
  7  * @package     WooCommerce/Admin/Importers
  8  * @version     2.0.0
  9  */
 10 
 11 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
 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          * __construct function.
 26          *
 27          * @access public
 28          * @return void
 29          */
 30         public function __construct() {
 31             $this->import_page = 'woocommerce_tax_rate_csv';
 32         }
 33 
 34         /**
 35          * Registered callback function for the WordPress Importer
 36          *
 37          * Manages the three separate stages of the CSV import process
 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          * format_data_from_csv function.
 80          *
 81          * @access public
 82          * @param mixed $data
 83          * @param string $enc
 84          * @return string
 85          */
 86         function format_data_from_csv( $data, $enc ) {
 87             return ( $enc == 'UTF-8' ) ? $data : utf8_encode( $data );
 88         }
 89 
 90         /**
 91          * import function.
 92          *
 93          * @access public
 94          * @param mixed $file
 95          * @return void
 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             // Show Result
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          * Performs post-import cleanup of files and the cache
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          * Handles the CSV upload and initial parsing of the file to prepare for
219          * displaying author import options
220          *
221          * @return bool False if error uploading or invalid file, true otherwise
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          * header function.
257          *
258          * @access public
259          * @return void
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          * footer function.
268          *
269          * @access public
270          * @return void
271          */
272         function footer() {
273             echo '</div>';
274         }
275 
276         /**
277          * greet function.
278          *
279          * @access public
280          * @return void
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          * Added to http_request_timeout filter to force timeout at 60 seconds during import
339          * @param  int $val
340          * @return int 60
341          */
342         function bump_request_timeout( $val ) {
343             return 60;
344         }
345     }
346 }
347 
WooCommerce API documentation generated by ApiGen 2.8.0