WC_Tax_Rate_Importer

Tax Rates importer - import tax rates and local tax rates into WooCommerce.

Defined (1)

The class is defined in the following location(s).

/includes/admin/importers/class-wc-tax-rate-importer.php  
  1. class WC_Tax_Rate_Importer extends WP_Importer { 
  2.  
  3. /** 
  4. * The current file id. 
  5. * @var int 
  6. */ 
  7. public $id; 
  8.  
  9. /** 
  10. * The current file url. 
  11. * @var string 
  12. */ 
  13. public $file_url; 
  14.  
  15. /** 
  16. * The current import page. 
  17. * @var string 
  18. */ 
  19. public $import_page; 
  20.  
  21. /** 
  22. * The current delimiter. 
  23. * @var string 
  24. */ 
  25. public $delimiter; 
  26.  
  27. /** 
  28. * Constructor. 
  29. */ 
  30. public function __construct() { 
  31. $this->import_page = 'woocommerce_tax_rate_csv'; 
  32. $this->delimiter = empty( $_POST['delimiter'] ) ? ', ' : (string) wc_clean( $_POST['delimiter'] ); 
  33.  
  34. /** 
  35. * Registered callback function for the WordPress Importer. 
  36. * Manages the three separate stages of the CSV import process. 
  37. */ 
  38. public function dispatch() { 
  39.  
  40. $this->header(); 
  41.  
  42. $step = empty( $_GET['step'] ) ? 0 : (int) $_GET['step']; 
  43.  
  44. switch ( $step ) { 
  45.  
  46. case 0: 
  47. $this->greet(); 
  48. break; 
  49.  
  50. case 1: 
  51. check_admin_referer( 'import-upload' ); 
  52.  
  53. if ( $this->handle_upload() ) { 
  54.  
  55. if ( $this->id ) { 
  56. $file = get_attached_file( $this->id ); 
  57. } else { 
  58. $file = ABSPATH . $this->file_url; 
  59.  
  60. add_filter( 'http_request_timeout', array( $this, 'bump_request_timeout' ) ); 
  61.  
  62. $this->import( $file ); 
  63. break; 
  64.  
  65. $this->footer(); 
  66.  
  67. /** 
  68. * Import is starting. 
  69. */ 
  70. private function import_start() { 
  71. if ( function_exists( 'gc_enable' ) ) { 
  72. gc_enable(); 
  73. wc_set_time_limit( 0 ); 
  74. @ob_flush(); 
  75. @flush(); 
  76. @ini_set( 'auto_detect_line_endings', '1' ); 
  77.  
  78. /** 
  79. * UTF-8 encode the data if `$enc` value isn't UTF-8. 
  80. * @param mixed $data 
  81. * @param string $enc 
  82. * @return string 
  83. */ 
  84. public function format_data_from_csv( $data, $enc ) { 
  85. return ( 'UTF-8' === $enc ) ? $data : utf8_encode( $data ); 
  86.  
  87. /** 
  88. * Import the file if it exists and is valid. 
  89. * @param mixed $file 
  90. */ 
  91. public function import( $file ) { 
  92. if ( ! is_file( $file ) ) { 
  93. $this->import_error( __( 'The file does not exist, please try again.', 'woocommerce' ) ); 
  94.  
  95. $this->import_start(); 
  96.  
  97. $loop = 0; 
  98.  
  99. if ( ( $handle = fopen( $file, "r" ) ) !== false ) { 
  100.  
  101. $header = fgetcsv( $handle, 0, $this->delimiter ); 
  102.  
  103. if ( 10 === sizeof( $header ) ) { 
  104.  
  105. while ( ( $row = fgetcsv( $handle, 0, $this->delimiter ) ) !== false ) { 
  106.  
  107. list( $country, $state, $postcode, $city, $rate, $name, $priority, $compound, $shipping, $class ) = $row; 
  108.  
  109. $tax_rate = array( 
  110. 'tax_rate_country' => $country,  
  111. 'tax_rate_state' => $state,  
  112. 'tax_rate' => $rate,  
  113. 'tax_rate_name' => $name,  
  114. 'tax_rate_priority' => $priority,  
  115. 'tax_rate_compound' => $compound ? 1 : 0,  
  116. 'tax_rate_shipping' => $shipping ? 1 : 0,  
  117. 'tax_rate_order' => $loop ++,  
  118. 'tax_rate_class' => $class,  
  119. ); 
  120.  
  121. $tax_rate_id = WC_Tax::_insert_tax_rate( $tax_rate ); 
  122. WC_Tax::_update_tax_rate_postcodes( $tax_rate_id, wc_clean( $postcode ) ); 
  123. WC_Tax::_update_tax_rate_cities( $tax_rate_id, wc_clean( $city ) ); 
  124. } else { 
  125. $this->import_error( __( 'The CSV is invalid.', 'woocommerce' ) ); 
  126.  
  127. fclose( $handle ); 
  128.  
  129. // Show Result 
  130. echo '<div class="updated settings-error"><p>'; 
  131. /** translators: %s: tax rates count */ 
  132. printf( 
  133. __( 'Import complete - imported %s tax rates.', 'woocommerce' ),  
  134. '<strong>' . $loop . '</strong>' 
  135. ); 
  136. echo '</p></div>'; 
  137.  
  138. $this->import_end(); 
  139.  
  140. /** 
  141. * Performs post-import cleanup of files and the cache. 
  142. */ 
  143. public function import_end() { 
  144. echo '<p>' . __( 'All done!', 'woocommerce' ) . ' <a href="' . admin_url( 'admin.php?page=wc-settings&tab=tax' ) . '">' . __( 'View tax rates', 'woocommerce' ) . '</a>' . '</p>'; 
  145.  
  146. do_action( 'import_end' ); 
  147.  
  148. /** 
  149. * Handles the CSV upload and initial parsing of the file to prepare for. 
  150. * displaying author import options. 
  151. * @return bool False if error uploading or invalid file, true otherwise 
  152. */ 
  153. public function handle_upload() { 
  154. if ( empty( $_POST['file_url'] ) ) { 
  155.  
  156. $file = wp_import_handle_upload(); 
  157.  
  158. if ( isset( $file['error'] ) ) { 
  159. $this->import_error( $file['error'] ); 
  160.  
  161. $this->id = absint( $file['id'] ); 
  162.  
  163. } elseif ( file_exists( ABSPATH . $_POST['file_url'] ) ) { 
  164. $this->file_url = esc_attr( $_POST['file_url'] ); 
  165. } else { 
  166. $this->import_error(); 
  167.  
  168. return true; 
  169.  
  170. /** 
  171. * Output header html. 
  172. */ 
  173. public function header() { 
  174. echo '<div class="wrap">'; 
  175. echo '<h1>' . __( 'Import tax rates', 'woocommerce' ) . '</h1>'; 
  176.  
  177. /** 
  178. * Output footer html. 
  179. */ 
  180. public function footer() { 
  181. echo '</div>'; 
  182.  
  183. /** 
  184. * Output information about the uploading process. 
  185. */ 
  186. public function greet() { 
  187.  
  188. echo '<div class="narrow">'; 
  189. 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>'; 
  190.  
  191. 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>'; 
  192.  
  193. $action = 'admin.php?import=woocommerce_tax_rate_csv&step=1'; 
  194.  
  195. $bytes = apply_filters( 'import_upload_size_limit', wp_max_upload_size() ); 
  196. $size = size_format( $bytes ); 
  197. $upload_dir = wp_upload_dir(); 
  198. if ( ! empty( $upload_dir['error'] ) ) : 
  199. ?><div class="error"><p><?php _e( 'Before you can upload your import file, you will need to fix the following error:', 'woocommerce' ); ?></p> 
  200. <p><strong><?php echo $upload_dir['error']; ?></strong></p></div><?php 
  201. else : 
  202. ?> 
  203. <form enctype="multipart/form-data" id="import-upload-form" method="post" action="<?php echo esc_attr( wp_nonce_url( $action, 'import-upload' ) ); ?>"> 
  204. <table class="form-table"> 
  205. <tbody> 
  206. <tr> 
  207. <th> 
  208. <label for="upload"><?php _e( 'Choose a file from your computer:', 'woocommerce' ); ?></label> 
  209. </th> 
  210. <td> 
  211. <input type="file" id="upload" name="import" size="25" /> 
  212. <input type="hidden" name="action" value="save" /> 
  213. <input type="hidden" name="max_file_size" value="<?php echo $bytes; ?>" /> 
  214. <small><?php 
  215. /** translators: %s: maximum upload size */ 
  216. printf( 
  217. __( 'Maximum size: %s', 'woocommerce' ),  
  218. $size 
  219. ); 
  220. ?></small> 
  221. </td> 
  222. </tr> 
  223. <tr> 
  224. <th> 
  225. <label for="file_url"><?php _e( 'OR enter path to file:', 'woocommerce' ); ?></label> 
  226. </th> 
  227. <td> 
  228. <?php echo ' ' . ABSPATH . ' '; ?><input type="text" id="file_url" name="file_url" size="25" /> 
  229. </td> 
  230. </tr> 
  231. <tr> 
  232. <th><label><?php _e( 'Delimiter', 'woocommerce' ); ?></label><br/></th> 
  233. <td><input type="text" name="delimiter" placeholder=", " size="2" /></td> 
  234. </tr> 
  235. </tbody> 
  236. </table> 
  237. <p class="submit"> 
  238. <input type="submit" class="button" value="<?php esc_attr_e( 'Upload file and import', 'woocommerce' ); ?>" /> 
  239. </p> 
  240. </form> 
  241. <?php 
  242. endif; 
  243.  
  244. echo '</div>'; 
  245.  
  246. /** 
  247. * Show import error and quit. 
  248. * @param string $message 
  249. */ 
  250. private function import_error( $message = '' ) { 
  251. echo '<p><strong>' . __( 'Sorry, there has been an error.', 'woocommerce' ) . '</strong><br />'; 
  252. if ( $message ) { 
  253. echo esc_html( $message ); 
  254. echo '</p>'; 
  255. $this->footer(); 
  256. die(); 
  257.  
  258. /** 
  259. * Added to http_request_timeout filter to force timeout at 60 seconds during import. 
  260. * @param int $val 
  261. * @return int 60 
  262. */ 
  263. public function bump_request_timeout( $val ) { 
  264. return 60;