PageRenderTime 61ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/importers/tax-rates-importer.php

https://github.com/CammoKing/woocommerce
PHP | 351 lines | 205 code | 80 blank | 66 comment | 31 complexity | 6830f1418f21d7edca20f94e01ed5383 MD5 | raw file
Possible License(s): GPL-3.0
  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 1.7.0
  9. */
  10. if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
  11. if ( class_exists( 'WP_Importer' ) ) {
  12. class WC_CSV_Tax_Rates_Import extends WP_Importer {
  13. var $id;
  14. var $file_url;
  15. var $import_page;
  16. var $delimiter;
  17. var $posts = array();
  18. var $imported;
  19. var $skipped;
  20. /**
  21. * __construct function.
  22. *
  23. * @access public
  24. * @return void
  25. */
  26. public function __construct() {
  27. $this->import_page = 'woocommerce_tax_rate_csv';
  28. }
  29. /**
  30. * Registered callback function for the WordPress Importer
  31. *
  32. * Manages the three separate stages of the CSV import process
  33. */
  34. function dispatch() {
  35. $this->header();
  36. if ( ! empty( $_POST['delimiter'] ) )
  37. $this->delimiter = stripslashes( trim( $_POST['delimiter'] ) );
  38. if ( ! $this->delimiter )
  39. $this->delimiter = ',';
  40. $step = empty( $_GET['step'] ) ? 0 : (int) $_GET['step'];
  41. switch ( $step ) {
  42. case 0:
  43. $this->greet();
  44. break;
  45. case 1:
  46. check_admin_referer( 'import-upload' );
  47. if ( $this->handle_upload() ) {
  48. if ( $this->id )
  49. $file = get_attached_file( $this->id );
  50. else
  51. $file = ABSPATH . $this->file_url;
  52. add_filter( 'http_request_timeout', array( &$this, 'bump_request_timeout' ) );
  53. if ( function_exists( 'gc_enable' ) )
  54. gc_enable();
  55. @set_time_limit(0);
  56. @ob_flush();
  57. @flush();
  58. $this->import( $file );
  59. }
  60. break;
  61. }
  62. $this->footer();
  63. }
  64. /**
  65. * format_data_from_csv function.
  66. *
  67. * @access public
  68. * @param mixed $data
  69. * @param mixed $enc
  70. * @return void
  71. */
  72. function format_data_from_csv( $data, $enc ) {
  73. return ( $enc == 'UTF-8' ) ? $data : utf8_encode( $data );
  74. }
  75. /**
  76. * import function.
  77. *
  78. * @access public
  79. * @param mixed $file
  80. * @return void
  81. */
  82. function import( $file ) {
  83. global $woocommerce, $wpdb;
  84. $this->imported = $this->skipped = 0;
  85. if ( ! is_file($file) ) {
  86. echo '<p><strong>' . __( 'Sorry, there has been an error.', 'woocommerce' ) . '</strong><br />';
  87. echo __( 'The file does not exist, please try again.', 'woocommerce' ) . '</p>';
  88. $this->footer();
  89. die();
  90. }
  91. $new_rates = array();
  92. if ( ( $handle = fopen( $file, "r" ) ) !== FALSE ) {
  93. $header = fgetcsv( $handle, 0, $this->delimiter );
  94. if ( sizeof( $header ) == 6 ) {
  95. $rates = get_option( 'woocommerce_tax_rates' );
  96. while ( ( $row = fgetcsv( $handle, 0, $this->delimiter ) ) !== FALSE ) {
  97. $rate = array();
  98. list( $rate['countries'], $rate['class'], $rate['label'], $rate['rate'], $rate['compound'], $rate['shipping'] ) = $row;
  99. $parsed_countries = array();
  100. $rate['countries'] = array_map( 'trim', explode( '|', $rate['countries'] ) );
  101. foreach( $rate['countries'] as $country ) {
  102. if ( strstr( $country, ':' ) ) {
  103. list( $country, $state ) = array_map( 'trim', explode( ':', $country ) );
  104. if ( ! isset( $parsed_countries[ $country ] ) )
  105. $parsed_countries[ $country ] = array();
  106. $parsed_countries[ $country ][] = $state;
  107. } else {
  108. if ( ! isset( $parsed_countries[ $country ] ) )
  109. $parsed_countries[ $country ] = array();
  110. $parsed_countries[ $country ][] = '*';
  111. }
  112. }
  113. $rate['countries'] = $parsed_countries;
  114. $rate['shipping'] = $rate['shipping'] ? 'yes' : 'no';
  115. $rate['compound'] = $rate['compound'] ? 'yes' : 'no';
  116. $new_rates[] = $rate;
  117. unset( $rate, $row );
  118. $this->imported++;
  119. }
  120. $rates = array_merge( $rates, $new_rates );
  121. update_option( 'woocommerce_tax_rates', $rates );
  122. } elseif ( sizeof( $header ) == 8 ) {
  123. $rates = get_option( 'woocommerce_local_tax_rates' );
  124. while ( ( $row = fgetcsv( $handle, 0, $this->delimiter ) ) !== FALSE ) {
  125. $rate = array();
  126. list( $rate['country'], $rate['state'], $rate['postcode'], $rate['class'], $rate['label'], $rate['rate'], $rate['compound'], $rate['shipping'] ) = $row;
  127. if ( ! $rate['country'] || ! $rate['postcode'] || ! $rate['rate'] ) {
  128. $this->skipped++;
  129. continue;
  130. }
  131. $rate['state'] = $rate['state'] ? $rate['state'] : '*';
  132. $rate['shipping'] = $rate['shipping'] ? 'yes' : 'no';
  133. $rate['compound'] = $rate['compound'] ? 'yes' : 'no';
  134. $rate['postcode'] = array_map( 'trim', explode( '|', $rate['postcode'] ) );
  135. $new_rates[] = $rate;
  136. unset( $rate, $row );
  137. $this->imported++;
  138. }
  139. $rates = array_merge( $rates, $new_rates );
  140. update_option( 'woocommerce_local_tax_rates', $rates );
  141. } else {
  142. echo '<p><strong>' . __( 'Sorry, there has been an error.', 'woocommerce' ) . '</strong><br />';
  143. echo __( 'The CSV is invalid.', 'woocommerce' ) . '</p>';
  144. $this->footer();
  145. die();
  146. }
  147. fclose( $handle );
  148. }
  149. // Show Result
  150. echo '<div class="updated settings-error below-h2"><p>
  151. '.sprintf( __( 'Import complete - imported <strong>%s</strong> tax rates and skipped <strong>%s</strong>.', 'woocommerce' ), $this->imported, $this->skipped ).'
  152. </p></div>';
  153. $this->import_end();
  154. }
  155. /**
  156. * Performs post-import cleanup of files and the cache
  157. */
  158. function import_end() {
  159. echo '<p>' . __( 'All done!', 'woocommerce' ) . ' <a href="' . admin_url('admin.php?page=woocommerce_settings&tab=tax') . '">' . __( 'View Tax Rates', 'woocommerce' ) . '</a>' . '</p>';
  160. do_action( 'import_end' );
  161. }
  162. /**
  163. * Handles the CSV upload and initial parsing of the file to prepare for
  164. * displaying author import options
  165. *
  166. * @return bool False if error uploading or invalid file, true otherwise
  167. */
  168. function handle_upload() {
  169. if ( empty( $_POST['file_url'] ) ) {
  170. $file = wp_import_handle_upload();
  171. if ( isset( $file['error'] ) ) {
  172. echo '<p><strong>' . __( 'Sorry, there has been an error.', 'woocommerce' ) . '</strong><br />';
  173. echo esc_html( $file['error'] ) . '</p>';
  174. return false;
  175. }
  176. $this->id = (int) $file['id'];
  177. } else {
  178. if ( file_exists( ABSPATH . $_POST['file_url'] ) ) {
  179. $this->file_url = esc_attr( $_POST['file_url'] );
  180. } else {
  181. echo '<p><strong>' . __( 'Sorry, there has been an error.', 'woocommerce' ) . '</strong></p>';
  182. return false;
  183. }
  184. }
  185. return true;
  186. }
  187. /**
  188. * header function.
  189. *
  190. * @access public
  191. * @return void
  192. */
  193. function header() {
  194. echo '<div class="wrap"><div class="icon32" id="icon-woocommerce-importer"><br></div>';
  195. echo '<h2>' . __( 'Import Tax Rates', 'woocommerce' ) . '</h2>';
  196. }
  197. /**
  198. * footer function.
  199. *
  200. * @access public
  201. * @return void
  202. */
  203. function footer() {
  204. echo '</div>';
  205. }
  206. /**
  207. * greet function.
  208. *
  209. * @access public
  210. * @return void
  211. */
  212. function greet() {
  213. global $woocommerce;
  214. echo '<div class="narrow">';
  215. 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>';
  216. echo '<p>' . sprintf( __( 'Tax rates need to be defined with columns in a specific order (6 columns). <a href="%s">Click here to download a sample</a>.', 'woocommerce' ), $woocommerce->plugin_url() . '/admin/importers/samples/sample_tax_rates.csv' ) . '</p>';
  217. echo '<p>' . sprintf( __( 'Local tax rates also need to be defined with columns in a specific order (8 columns). <a href="%s">Click here to download a sample</a>.', 'woocommerce' ), $woocommerce->plugin_url() . '/admin/importers/samples/sample_local_tax_rates.csv' ) . '</p>';
  218. $action = 'admin.php?import=woocommerce_tax_rate_csv&step=1';
  219. $bytes = apply_filters( 'import_upload_size_limit', wp_max_upload_size() );
  220. $size = wp_convert_bytes_to_hr( $bytes );
  221. $upload_dir = wp_upload_dir();
  222. if ( ! empty( $upload_dir['error'] ) ) :
  223. ?><div class="error"><p><?php _e('Before you can upload your import file, you will need to fix the following error:'); ?></p>
  224. <p><strong><?php echo $upload_dir['error']; ?></strong></p></div><?php
  225. else :
  226. ?>
  227. <form enctype="multipart/form-data" id="import-upload-form" method="post" action="<?php echo esc_attr(wp_nonce_url($action, 'import-upload')); ?>">
  228. <table class="form-table">
  229. <tbody>
  230. <tr>
  231. <th>
  232. <label for="upload"><?php _e( 'Choose a file from your computer:' ); ?></label>
  233. </th>
  234. <td>
  235. <input type="file" id="upload" name="import" size="25" />
  236. <input type="hidden" name="action" value="save" />
  237. <input type="hidden" name="max_file_size" value="<?php echo $bytes; ?>" />
  238. <small><?php printf( __('Maximum size: %s' ), $size ); ?></small>
  239. </td>
  240. </tr>
  241. <tr>
  242. <th>
  243. <label for="file_url"><?php _e( 'OR enter path to file:', 'woocommerce' ); ?></label>
  244. </th>
  245. <td>
  246. <?php echo ' ' . ABSPATH . ' '; ?><input type="text" id="file_url" name="file_url" size="25" />
  247. </td>
  248. </tr>
  249. <tr>
  250. <th><label><?php _e( 'Delimiter', 'woocommerce' ); ?></label><br/></th>
  251. <td><input type="text" name="delimiter" placeholder="," size="2" /></td>
  252. </tr>
  253. </tbody>
  254. </table>
  255. <p class="submit">
  256. <input type="submit" class="button" value="<?php esc_attr_e( 'Upload file and import' ); ?>" />
  257. </p>
  258. </form>
  259. <?php
  260. endif;
  261. echo '</div>';
  262. }
  263. /**
  264. * Added to http_request_timeout filter to force timeout at 60 seconds during import
  265. * @return int 60
  266. */
  267. function bump_request_timeout() {
  268. return 60;
  269. }
  270. }
  271. }