class-wc-tax-rate-importer.php 8.5 KB

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