woocsv_import

The Woocommerce CSV Import woocsv import class.

Defined (1)

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

/include/class-woocsv-import.php  
  1. class woocsv_import 
  2. public $logger; 
  3.  
  4. public $separator; 
  5.  
  6. public $addons; 
  7.  
  8. public $api_url = 'http://allaerd.org/api/wc-api/check_for_updates'; 
  9.  
  10. public $upload_dir; 
  11.  
  12. public $import_log; 
  13.  
  14. public $options; 
  15.  
  16. public $header; 
  17.  
  18. public $headers = array (); 
  19.  
  20. public $message; 
  21.  
  22. public $version = '3.1.3'; 
  23.  
  24. public $options_default = array ( 
  25. 'woocsv_separator' => ', ',  
  26. 'woocsv_skip_first_line' => 1,  
  27. 'woocsv_blocksize' => 1,  
  28. 'woocsv_merge_products' => 1,  
  29. 'woocsv_add_to_categories' => 1,  
  30. 'woocsv_debug' => 0,  
  31. 'woocsv_match_by' => 'sku',  
  32. 'woocsv_roles' => array ( 'shop_manager' ),  
  33. 'woocsv_match_author_by' => 'login',  
  34. 'woocsv_convert_to_utf8' => 1,  
  35. ); 
  36.  
  37. public $fields = array ( 
  38. 0 => 'sku',  
  39. 1 => 'post_name',  
  40. 2 => 'post_status',  
  41. 3 => 'post_title',  
  42. 4 => 'post_content',  
  43. 5 => 'post_excerpt',  
  44. 6 => 'category',  
  45. 7 => 'tags',  
  46. 8 => 'stock',  
  47. 10 => 'regular_price',  
  48. 11 => 'sale_price',  
  49. 12 => 'weight',  
  50. 13 => 'length',  
  51. 14 => 'width',  
  52. 15 => 'height',  
  53. 17 => 'tax_status',  
  54. 18 => 'tax_class',  
  55. 19 => 'stock_status', // instock, outofstock 
  56. 20 => 'visibility', // visible, catelog, search, hidden 
  57. 21 => 'backorders', // yes, no 
  58. 22 => 'featured', // yes, no 
  59. 23 => 'manage_stock', // yes, no 
  60. 24 => 'featured_image',  
  61. 25 => 'product_gallery',  
  62. 26 => 'shipping_class',  
  63. 27 => 'comment_status', //closed, open 
  64. 28 => 'change_stock', // +1 -1 + 5 -8 
  65. 29 => 'ID',  
  66. 30 => 'ping_status', // open, closed 
  67. 31 => 'menu_order',  
  68. 32 => 'post_author', //user name or nice name of an user 
  69. 33 => 'post_date',  
  70. ); 
  71.  
  72.  
  73. public function __construct () 
  74.  
  75. $this->logger = new \Allaerd\Woocsv\LogToFile(); 
  76.  
  77. //load dependencies 
  78. $this->load_dependenies(); 
  79.  
  80. // activation hook 
  81. register_activation_hook( __FILE__, array ( $this, 'install' ) ); 
  82.  
  83. //load options 
  84. $this->set_options(); 
  85.  
  86. //check install 
  87. $this->check_install(); 
  88.  
  89. //fill header 
  90. $this->set_header(); 
  91.  
  92. //add ajax 
  93. add_action( 'wp_ajax_run_import', array ( $this, 'run_import' ) ); 
  94.  
  95. $this->upload_dir = wp_upload_dir(); 
  96.  
  97. $this->fields = apply_filters('allaerd_importer_fields', $this->fields); 
  98.  
  99. public function load_dependenies () 
  100.  
  101. //admin 
  102. require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-woocsv-import-admin.php'; 
  103. new woocsv_import_admin(); 
  104.  
  105. //settings 
  106. require_once plugin_dir_path( dirname( __FILE__ ) ) . 'include/class-woocsv-import-settings.php'; 
  107. new woocsv_import_settings(); 
  108.  
  109. //main product 
  110. require_once plugin_dir_path( dirname( __FILE__ ) ) . 'include/class-woocsv-product.php'; 
  111.  
  112. public function set_options () 
  113. $options = $this->options_default; 
  114.  
  115. foreach ($options as $key => $value) { 
  116. if (get_option( $key ) !== FALSE) { 
  117. $options[ substr( $key, 7 ) ] = get_option( $key ); 
  118. } else { 
  119. update_option( $key, $value ); 
  120. $this->options = $options; 
  121.  
  122. public function check_install () 
  123. $message = $this->message; 
  124.  
  125. //old way 
  126. if (!get_option( 'woocsv-options' )) { 
  127. update_option( 'woocsv-options', $this->options ); 
  128.  
  129. //new way 
  130. if (!get_option( 'woocsv_options' )) { 
  131. update_option( 'woocsv_options', $this->options ); 
  132.  
  133.  
  134. $upload_dir = wp_upload_dir(); 
  135. $dir = $upload_dir[ 'basedir' ] . '/csvimport/'; 
  136. if (!is_dir( $dir )) { 
  137. @mkdir( $dir ); 
  138.  
  139. if (!is_writable( $upload_dir[ 'basedir' ] . '/csvimport/' )) { 
  140. $message .= __( 'Upload directory is not writable, please check you permissions', 'woocommerce-csvimport' ); 
  141.  
  142. $this->message = $message; 
  143. if ($message) { 
  144. add_action( 'admin_notices', array ( $this, 'show_warning' ) ); 
  145.  
  146.  
  147. public function set_header () 
  148.  
  149. $header = get_option( 'woocsv_header' ); 
  150. if ($header) { 
  151. $this->header = $header; 
  152.  
  153. $headers = get_option( 'woocsv_headers' ); 
  154. if ($headers) { 
  155. $this->headers = $headers; 
  156.  
  157. public function get_roles () 
  158. return get_option( 'woocsv_roles' ); 
  159.  
  160. public function get_match_author_by () 
  161. return get_option( 'woocsv_match_author_by' ); 
  162.  
  163. public function get_match_by () 
  164. return get_option( 'woocsv_match_by' ); 
  165.  
  166. public function get_add_to_categories () 
  167. return get_option( 'woocsv_add_to_categories' ); 
  168.  
  169. public function get_merge_products () 
  170. return get_option( 'woocsv_merge_products' ); 
  171.  
  172. public function get_blocksize () 
  173. return get_option( 'woocsv_blocksize' ); 
  174.  
  175. public function get_separator () 
  176. return get_option( 'woocsv_separator' ); 
  177.  
  178. public function get_convert_to_utf8 () 
  179. return get_option( 'woocsv_convert_to_utf8' ); 
  180.  
  181. public function install () 
  182. $upload_dir = wp_upload_dir(); 
  183. $dir = $upload_dir[ 'basedir' ] . '/csvimport/'; 
  184. @mkdir( $dir ); 
  185.  
  186. //create options 
  187. $this->set_options(); 
  188.  
  189. public function show_warning () 
  190. global $current_screen; 
  191. if ($current_screen->parent_base == 'woocsv_import') { 
  192. echo '<div class="error"><p>' . $this->message . '</p></div>'; 
  193.  
  194. public function handle_file_upload ($from_location, $filename) 
  195. do_action( 'woocsv_before_csv_upload', $filename ); 
  196. $upload_dir = wp_upload_dir(); 
  197. $to_location = $upload_dir[ 'basedir' ] . '/csvimport/' . $filename; 
  198. if (@move_uploaded_file( $from_location, $to_location )) { 
  199. do_action( 'woocsv_after_csv_upload', $filename ); 
  200.  
  201. return $to_location; 
  202. } else { 
  203. return FALSE; 
  204.  
  205. public function run_import () 
  206. global $woocsv_product; 
  207.  
  208. /** 
  209. * Are we starting for the first time, than create a batch and continue, else just pick uo the batch code and start where you left 
  210. */ 
  211. do_action( 'woocsv_start_import' ); 
  212.  
  213. if (empty($_POST[ 'batch_code' ])) { 
  214. //create a new batch 
  215. $batch_code = woocsv_batches::create(); 
  216. if ($batch_code) { 
  217.  
  218. //get max time we have and set the block size 
  219. $max_execution_time = @ini_get( 'max_execution_time' ); 
  220. if ($max_execution_time == 0) { 
  221. $max_execution_time = 30; 
  222.  
  223. $block_size = $this->get_blocksize(); 
  224.  
  225. $data = array ( 
  226. 'filename' => $_POST[ 'filename' ],  
  227. 'row' => 0,  
  228. 'block_size' => $block_size,  
  229. 'header_name' => $_POST[ 'header_name' ],  
  230. 'seperator' => $_POST[ 'seperator' ],  
  231. 'total_rows' => (int)$_POST[ 'total_rows' ],  
  232. 'start_date' => time(),  
  233. 'max_execution_time' => $max_execution_time,  
  234. ); 
  235. woocsv_batches::update( $batch_code, $data ); 
  236. } else { 
  237. //@todo die nice 
  238.  
  239. //and get the batch 
  240. $batch = woocsv_batches::get_batch( $batch_code ); 
  241. } else { 
  242. $batch_code = $_POST[ 'batch_code' ]; 
  243. $batch = woocsv_batches::get_batch( $_POST[ 'batch_code' ] ); 
  244.  
  245. $this->setLoggerFilename( $batch_code ); 
  246.  
  247. //lets check if we are done? 
  248. if ($batch[ 'row' ] >= $batch[ 'total_rows' ]) { 
  249. $batch[ 'end_date' ] = time(); 
  250. $batch[ 'status' ] = 'done'; 
  251. woocsv_batches::update( $batch_code, $batch ); 
  252.  
  253. do_action( 'woocsv_after_import_finished' ); 
  254.  
  255. //@todo DIE NICE 
  256. $this->die_nicer( $batch_code, $batch ); 
  257.  
  258. // do we need to skip the first line? 
  259. if ($this->get_skip_first_line() == 1 && $batch[ 'row' ] == 0) { 
  260. $batch[ 'row' ] = 1; 
  261. $this->logger->log( __( '-- Skipping the first line', 'woocommerce-csvimport' ) ); 
  262.  
  263. //get the from and till 
  264. $from = $batch[ 'row' ]; 
  265. $till = (($batch[ 'row' ] + $batch[ 'block_size' ]) < $batch[ 'total_rows' ]) ? ($batch[ 'row' ] + $batch[ 'block_size' ]) : $batch[ 'total_rows' ]; 
  266.  
  267. //get the lines 
  268. $lines = $this->get_lines_from_file( $batch[ 'filename' ], $from, $till, $batch[ 'seperator' ] ); 
  269.  
  270. //get the header 
  271. $header = $this->get_header_from_name( $batch[ 'header_name' ] ); 
  272.  
  273. $this->toggleCache( TRUE ); 
  274.  
  275. $time_started = microtime( TRUE ); 
  276.  
  277. //loop over the lines and fill, pase and save the lines 
  278. foreach ($lines[ 'lines' ] as $line) { 
  279.  
  280. //reset time ever time around 
  281. @set_time_limit( 0 ); 
  282. //new one and fill in the header and the raw data 
  283.  
  284. $woocsv_product = new woocsv_import_product ( $this->logger ); 
  285.  
  286. $woocsv_product->header = $header; 
  287. $woocsv_product->raw_data = $line; 
  288.  
  289. //fill it, parse it and save it 
  290. $woocsv_product->fill_in_data(); 
  291.  
  292. $this->logger->log( __( '-----> Row', 'woocommerce-csvimport' ) ); 
  293.  
  294. $woocsv_product->parse_data(); 
  295.  
  296. $woocsv_product->save(); 
  297. if ($woocsv_product->log) { 
  298. $this->logger->log( __( $woocsv_product->log, 'woocommerce-csvimport' ) ); 
  299.  
  300.  
  301. //write tot log if debug is on 
  302. if ($this->get_debug() == 0) { 
  303. $this->logger->log( __( '--->debug dump', 'woocommerce-csvimport' ) ); 
  304. $this->logger->log( __( $woocsv_product, 'woocommerce-csvimport' ) ); 
  305. //close log 
  306.  
  307. $this->logger->log( __( '-----> end row', 'woocommerce-csvimport' ) ); 
  308.  
  309. //goto the next row 
  310. $batch[ 'row' ]++; 
  311.  
  312. //delete transionts 
  313. if (function_exists( 'wc_delete_product_transients' )) { 
  314. wc_delete_product_transients( $woocsv_product->body[ 'ID' ] ); 
  315. $time_finished = microtime( TRUE ); 
  316. if (!$this->get_blocksize()) { 
  317.  
  318. $time_factor = ceil( ($batch[ 'max_execution_time' ] - ($time_finished - $time_started)) / $batch[ 'max_execution_time' ] * 100 ); 
  319.  
  320. switch ($time_factor) { 
  321. case $time_factor > 90: 
  322. $block_size = 10; 
  323. break; 
  324. case $time_factor > 50: 
  325. $block_size = 5; 
  326. break; 
  327. case $time_factor > 10: 
  328. $block_size = 1; 
  329. break; 
  330. default: 
  331. $block_size = 0; 
  332.  
  333. $batch[ 'block_size' ] += $block_size; 
  334.  
  335. woocsv_batches::update( $batch_code, $batch ); 
  336.  
  337. $this->die_nicer( $batch_code, $batch ); 
  338.  
  339. public function die_nicer ($batch_code, $batch) 
  340.  
  341. $this->toggleCache(); 
  342.  
  343. //are we done? 
  344. if ($batch[ 'status' ] == 'done') { 
  345. $post_data[ 'done' ] = 1; 
  346. } else { 
  347. $post_data[ 'batch_code' ] = $batch_code; 
  348. $post_data[ 'status' ] = 0; 
  349.  
  350. woocsv_batches::update( $batch_code, $batch ); 
  351.  
  352. //============================= 
  353. // Check if we need to debug 
  354. //============================= 
  355. if ($this->get_debug() == 0) { 
  356. ob_get_clean(); 
  357.  
  358. $post_data[ 'batch' ] = $batch; 
  359.  
  360. echo json_encode( $post_data ); 
  361. unset($post_data); 
  362. wp_die(); 
  363.  
  364. public function get_debug () 
  365. return get_option( 'woocsv_debug' ); 
  366.  
  367. public function get_skip_first_line () 
  368. return get_option( 'woocsv_skip_first_line' ); 
  369.  
  370. public function get_lines_from_file ($filename, $from, $till, $seperator, $get_total_rows = FALSE) 
  371.  
  372. ini_set( "auto_detect_line_endings", TRUE ); 
  373.  
  374. $row = 0; 
  375. $lines = array (); 
  376. $handle = fopen( $filename, "r" ); 
  377.  
  378. while (($line = fgetcsv( $handle, 0, $seperator )) !== FALSE) { 
  379.  
  380. if ($row >= $from && $row <= $till) { 
  381.  
  382. if (is_array( $line ) && $this->get_convert_to_utf8()) { 
  383. foreach ($line as $key => $value) { 
  384.  
  385. //setting 1 
  386. if ($this->get_convert_to_utf8() == 1) { 
  387. $line[ $key ] = utf8_encode( $value ); 
  388.  
  389. //setting 2 
  390. if ($this->get_convert_to_utf8() == 2 && function_exists( 'mb_convert_encoding' )) { 
  391. $line[ $key ] = mb_convert_encoding( $value, 'UTF-8', ' auto' ); 
  392.  
  393.  
  394.  
  395. $lines[] = $line; 
  396.  
  397. if (!$get_total_rows && $row > $till) { 
  398. //return if we do not need the totals 
  399. fclose( $handle ); 
  400.  
  401. return array ( 'lines' => $lines ); 
  402.  
  403. $row++; 
  404.  
  405. return array ( 'lines' => $lines, 'total_rows' => $row ); 
  406.  
  407. //new for schedule and future use! 
  408.  
  409. public function get_header_from_name ($header_name) 
  410. if (isset ($this->headers[ $header_name ])) { 
  411. return $this->headers[ $header_name ]; 
  412. } else { 
  413. return FALSE; 
  414.  
  415. public function setLoggerFilename ($batch_code) 
  416. $this->logger->setFilename( $this->upload_dir[ 'basedir' ] . '/csvimport/' . $batch_code . '.log' ); 
  417.  
  418. public function toggleCache ($status = FALSE) 
  419. if (function_exists( 'wp_suspend_cache_invalidation' )) { 
  420. wp_suspend_cache_invalidation( $status ); 
  421. if (function_exists( 'wp_defer_term_counting ' )) { 
  422. wp_defer_term_counting( $status ); 
  423.