woocsv_import_admin

The Woocommerce CSV Import woocsv import admin class.

Defined (1)

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

/admin/class-woocsv-import-admin.php  
  1. class woocsv_import_admin 
  2.  
  3. public function __construct() { 
  4. //add menu's and css 
  5. add_action('admin_menu', array($this, 'menu')); 
  6.  
  7. //add ajax methode 
  8. //delete header 
  9. add_action( 'wp_ajax_delete_header', array( $this, 'delete_header' ) ); 
  10.  
  11. //move header up 
  12. //@todo 
  13. //add_action( 'wp_ajax_header_up', array( $this, 'header_up' ) ); 
  14.  
  15. //move header down 
  16. //@todo 
  17. //add_action( 'wp_ajax_header_down', array( $this, 'header_down' ) );  
  18.  
  19. public function header_down() { 
  20.  
  21.  
  22. public function header_up() { 
  23.  
  24.  
  25. public function delete_header() { 
  26. global $woocsv_import; 
  27.  
  28. if ( isset ( $_POST['header_name'] ) ) { 
  29. unset ( $woocsv_import->headers[ $_POST[ 'header_name' ] ] ); 
  30. update_option( 'woocsv_headers' , $woocsv_import->headers );  
  31. wp_die( $_POST['header_name'] ); 
  32.  
  33. wp_die( 0 ); 
  34.  
  35.  
  36. public function menu() 
  37. global $woocsv_import; 
  38.  
  39. //====================================== 
  40. //role support  
  41. //====================================== 
  42. $user = wp_get_current_user(); 
  43. $current_role = $user->roles; 
  44. $allowed_roles = $woocsv_import->get_roles(); 
  45.  
  46. if (is_admin() && ( count( array_intersect($current_role, $allowed_roles)) > 0 || current_user_can( 'manage_options' ) ) ) { 
  47.  
  48. //main page 
  49. $page = add_menu_page('CSV Import', __('CSV Import', 'woocommerce-csvimport') , current($current_role), 'woocsv_import', array($this, 'import'), 'dashicons-randomize',  
  50. '58.15011976');  
  51.  
  52. //settings page 
  53. add_submenu_page( 'woocsv_import', 'Settings', __('Settings', 'woocommerce-csvimport'), current($current_role), 'woocsv-settings', array($this, 'settings')); 
  54.  
  55. //add-ons 
  56. add_submenu_page( 'woocsv_import', 'Docs', __('Docs', 'woocommerce-csvimport'), current($current_role), 'woocsv-docs', array($this, 'docs')); 
  57.  
  58. //documentation 
  59. add_submenu_page( 'woocsv_import', 'Add-ons', '<span style="color:#ef6c00">'.__('Add-ons', 'woocommerce-csvimport').'</span>', current($current_role), 'woocsv-addons', array($this, 'addons')); 
  60.  
  61. //js and css 
  62. add_action( 'admin_print_scripts-' .$page, array( $this, 'enqueue_scripts' ) ); 
  63. add_action( 'admin_print_styles', array( $this, 'enqueue_styles' ) );  
  64.  
  65. public function docs () { 
  66. include dirname( __FILE__ ) . '/partials/woocsv-import-admin-docs.php'; 
  67. }  
  68.  
  69. public function addons () { 
  70. include dirname( __FILE__ ) . '/partials/woocsv-import-admin-addons.php'; 
  71.  
  72. public function import () { 
  73. global $woocsv_import; 
  74.  
  75. /** ! HEADER */ 
  76. if ( isset ($_FILES['csvfile']) && $_FILES['csvfile']['error'] != 0 ) { 
  77.  
  78. $fileErrorMessage = ($_FILES['csvfile']['error'])?:'unknown'; 
  79. echo '<div class="error"><p>'; 
  80. echo sprintf( __('Something went wrong during the file upload, error message: %s', 'woocommerce-csvimport'), UploadErrorMessages::convertErrorToMessage($_FILES[ 'csvfile'][ 'error']) ); 
  81. echo '</p></div>'; 
  82.  
  83. //save header 
  84. if ( isset( $_POST['action'] ) &&  
  85. $_POST['action'] === 'save_header_preview' &&  
  86. check_admin_referer('save_header_preview', 'save_header_preview')  
  87. ) { 
  88. $this->save_header(); 
  89. include dirname( __FILE__ ) . '/partials/woocsv-import-admin-header.php'; 
  90. return;  
  91.  
  92. //preview the header 
  93. if ( isset( $_POST['action'] ) && 
  94. $_POST['action'] === 'start_header_preview' && 
  95. @is_uploaded_file( $_FILES['csvfile']['tmp_name'] ) && 
  96. check_admin_referer( 'upload_header_file' , 'upload_header_file' )  
  97. ) { 
  98. include dirname( __FILE__ ) . '/partials/woocsv-import-admin-header-preview.php'; 
  99. return; 
  100.  
  101. //header page 
  102. if ( ( 
  103. isset( $_GET['page'], $_GET['tab']) &&  
  104. $_GET['page'] == 'woocsv_import' &&  
  105. $_GET['tab'] == 'headers' 
  106. ) ||  
  107. empty($woocsv_import->headers)  
  108. ) { 
  109. include dirname( __FILE__ ) . '/partials/woocsv-import-admin-header.php'; 
  110. return; 
  111.  
  112. /** ! IMPORT */ 
  113. //preview the import 
  114. if ( isset( $_POST['action'] ) && 
  115. $_POST['action'] === 'start_import_preview' && 
  116. @is_uploaded_file( $_FILES['csvfile']['tmp_name'] ) && 
  117. check_admin_referer( 'upload_import_file' , 'upload_import_file' )  
  118. ) { 
  119. // @since 3.1.0 
  120. //@todo create batch allready! 
  121.  
  122. //set the active header 
  123. $header = $woocsv_import->headers[$_POST['header_name']]; 
  124. $header_name = $_POST['header_name']; 
  125. update_option('woocsv_header', $woocsv_import->headers[$_POST['header_name']] ); 
  126. $woocsv_import->header = $header; 
  127.  
  128. include dirname( __FILE__ ) . '/partials/woocsv-import-admin-import-preview.php'; 
  129. return; 
  130.  
  131. //we are creating a batch 
  132. if ( isset( $_POST['action'] ) && 
  133. $_POST['action'] === 'create_batch' && 
  134. check_admin_referer( 'create_batch' , 'create_batch' ) 
  135. ) { 
  136.  
  137. //create a new batch 
  138. $new_batch_code = woocsv_batches::create(); 
  139.  
  140. if ($new_batch_code) { 
  141. $data = array( 
  142. 'filename' => $_POST['filename'],  
  143. 'row' => 0,  
  144. 'header_name' => $_POST['header_name'],  
  145. 'speperator' => $_POST['seperator'],  
  146. 'total_rows' => $_POST['total_rows'],  
  147. ); 
  148. woocsv_batches::update($new_batch_code, $data); 
  149.  
  150.  
  151. //else just load the regular import screen 
  152. include dirname( __FILE__ ) . '/partials/woocsv-import-admin-import.php';  
  153.  
  154. public static function save_header() 
  155. global $woocsv_import; 
  156. $headerOrder = ''; 
  157. foreach ($_POST as $key=>$value) { 
  158. if (preg_match("/fields_[0-9]/", $key, $matches)) { 
  159. $headerOrder[] = $value; 
  160. $headers = $woocsv_import->headers; 
  161. $headers[$_POST['header_name']] = $headerOrder; 
  162. update_option('woocsv_headers', $headers); 
  163. $woocsv_import->headers = $headers; 
  164.  
  165.  
  166. public function settings () { 
  167. include dirname( __FILE__ ) . '/partials/woocsv-import-admin-settings.php'; 
  168.  
  169. public function enqueue_scripts() 
  170.  
  171. //====================================== 
  172. // register the scripts 
  173. //====================================== 
  174. wp_enqueue_script('jquery'); 
  175. wp_register_script( 'woocsv-script', plugin_dir_url( __FILE__ ). '/js/woocsv-import-admin.js' ); 
  176.  
  177.  
  178. //====================================== 
  179. // localize javascript 
  180. //====================================== 
  181. $strings = array ( 
  182. 'error' => __( 'Something went wrong. We could not make a connection with the server.', 'woocommerce-csvimport' ),  
  183. 'done' => __( 'Done', 'woocommerce-csvimport' ),  
  184. 'start' => __( 'Starting', 'woocommerce-csvimport'),  
  185. ); 
  186. wp_localize_script( 'woocsv-script', 'strings', $strings ); 
  187.  
  188.  
  189. //====================================== 
  190. // enqueue the javascript 
  191. //====================================== 
  192. wp_enqueue_script( 'woocsv-script' ); 
  193.  
  194. public function enqueue_styles() 
  195. wp_register_style( 'woocsv-css', plugin_dir_url( __FILE__ ).'/css/woocsv-import-admin.css' ); 
  196. wp_enqueue_style( 'woocsv-css' ); 
  197.