woocsv_batches

Class woocsv_batches.

Defined (1)

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

/include/class-woocsv-batches.php  
  1. class woocsv_batches 
  2.  
  3. /** 
  4. * @var array 
  5. */ 
  6.  
  7. static $batch_data = array ( 
  8. 'row' => 0,  
  9. 'block_size' => 1,  
  10. 'total_rows' => 0,  
  11. 'filename' => '',  
  12. 'status' => 'new',  
  13. 'header_name' => '',  
  14. 'seperator' => ', ',  
  15. 'start_date' => '',  
  16. 'end_date' => '',  
  17. 'creation_date' => '',  
  18. 'schedule_date' => '',  
  19. 'max_execution_time' => '' 
  20. ); 
  21.  
  22. /** 
  23. * @param $batch_code 
  24. * @todo improve error handling when batched or headers can not be found 
  25. */ 
  26. public static function schedule ( $batch_code ) { 
  27. global $woocsv_import; 
  28.  
  29. $batches = get_option ( 'woocsv_batches' ); 
  30. $headers = get_option ( 'woocsv_headers' ); 
  31.  
  32. if ( isset( $batches[$batch_code] ) ) { 
  33. //get batch 
  34. $batch = $batches[$batch_code]; 
  35. } else { 
  36. //delete batch? 
  37. return false; 
  38.  
  39. if ( isset( $headers[$batch['header_name']] ) ) { 
  40. //get header 
  41. $header = $headers[$batch['header_name']]; 
  42. } else { 
  43. //delete header? 
  44. return false; 
  45.  
  46. //set batch status 
  47. $batch['status'] = 'in progress'; 
  48.  
  49. //skip the first row if needed 
  50. if ( $batch['row'] = 0 && $woocsv_import->get_skip_first_line () ) { 
  51. $batch['row'] = 1; 
  52.  
  53. //get a few lines 
  54. $lines = $woocsv_import->get_lines_from_file ( $batch['filename'], $batch['row'], $batch['row'] + 5, $batch['seperator'] ); 
  55.  
  56. //loop them and import 
  57. foreach ( $lines as $line ) { 
  58.  
  59. //create a new product 
  60. $product = new woocsv_product ( $line, $header ); 
  61.  
  62.  
  63. $batches[$batch_code] = $batch; 
  64. update_option ( 'woocsv_batches', $batches ); 
  65.  
  66. /** 
  67. * @param bool $batch_code 
  68. * @return mixed 
  69. */ 
  70.  
  71. static function get_batch ( $batch_code = false ) { 
  72.  
  73. $batches = get_option ( 'woocsv_batches' ); 
  74. if ( isset ( $batches[$batch_code] ) ) { 
  75. $batch = $batches[$batch_code]; 
  76. } else { 
  77. $batch = false; 
  78.  
  79. return $batch; 
  80.  
  81. /** 
  82. * @return bool|string 
  83. */ 
  84.  
  85. static function create () { 
  86.  
  87. //create a unique batch code 
  88. $new_batch_code = self::unique_number (); 
  89.  
  90. //fill in the data 
  91. $new_batch[$new_batch_code] = self::$batch_data; 
  92. $new_batch[$new_batch_code]['creation_date'] = time (); 
  93.  
  94. //get the other batches 
  95. $batches = get_option ( 'woocsv_batches' ); 
  96.  
  97. //and prepend the new one 
  98. if ($batches) { 
  99. $batches = $new_batch + $batches; 
  100. } else { 
  101. $batches = $new_batch; 
  102.  
  103.  
  104.  
  105. //return code if succeeded 
  106. if ( update_option ( 'woocsv_batches', $batches ) ) { 
  107. return $new_batch_code; 
  108. } else { 
  109. return false; 
  110.  
  111.  
  112. /** 
  113. * @return string 
  114. */ 
  115. static function unique_number () { 
  116. return substr ( md5 ( uniqid ( mt_rand (), true ) ), 0, 10 ); 
  117.  
  118.  
  119. /** 
  120. * @param null $batch_code 
  121. * @param $data 
  122. * @return bool 
  123. */ 
  124. static function update ( $batch_code = null, $data ) { 
  125.  
  126. //is the function call correct? 
  127. if ( !$batch_code && !is_array ( $data ) ) { 
  128. return false; 
  129.  
  130. //we can continue! 
  131.  
  132. $batches = get_option ( 'woocsv_batches' ); 
  133.  
  134. if ( isset ( $batches[$batch_code] ) ) { 
  135. $batch = $batches[$batch_code]; 
  136.  
  137. //only update batches keys that are in self data 
  138. foreach ( self::$batch_data as $key => $value ) { 
  139.  
  140. $batch[$key] = ( isset ( $data[$key] ) ) ? $data[$key] : $value; 
  141.  
  142. $batches[$batch_code] = $batch; 
  143. update_option ( 'woocsv_batches', $batches ); 
  144.  
  145. return true; 
  146.  
  147. /** 
  148. * when pressing start in the import screen on a batch, this function is called to schedule the event 
  149. */ 
  150. static function start () { 
  151. $batch_code = $_POST['batch']; 
  152. wp_schedule_single_event ( time (), 'woocsv_schedule_batch', array ( $batch_code ) ); 
  153. wp_die ( 'done' ); 
  154.  
  155. /** 
  156. * ajax function to delete all batches 
  157. */ 
  158.  
  159. static function delete_all () { 
  160. global $woocsv_import; 
  161. update_option ( 'woocsv_batches', array () ); 
  162. wp_die (); 
  163.  
  164. /** 
  165. * ajax function to delete the selected batch 
  166. */ 
  167. static function delete () { 
  168. if ( isset ( $_POST['batch_code'] ) ) { 
  169. $batches = get_option ( 'woocsv_batches' ); 
  170. unset ( $batches[$_POST['batch_code']] ); 
  171. update_option ( 'woocsv_batches', $batches ); 
  172. wp_die ( $_POST['batch_code'] ); 
  173. wp_die ( 0 ); 
  174.