wpg_order_export

The WooCommerce Simply Order Export wpg order export class.

Defined (1)

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

/classes/class-order-export.php  
  1. class wpg_order_export { 
  2.  
  3. /** 
  4. * Bootstraps the class and hooks required actions & filters. 
  5. */ 
  6. public function __construct() { 
  7.  
  8. add_filter( 'woocommerce_settings_tabs_array', array( $this, 'add_settings_tab' ), 50 ); 
  9. add_action( 'woocommerce_settings_tabs_order_export', array($this, 'settings_tab') ); 
  10. add_action( 'woocommerce_update_options_order_export', array($this, 'update_settings') ); 
  11. add_action( 'woocommerce_admin_field_short_desc', array($this, 'short_desc_field') ); 
  12. add_action( 'woocommerce_admin_field_advanced_options', array($this, 'advanced_options') ); 
  13. add_action( 'admin_enqueue_scripts', array($this, 'scripts') ); 
  14. add_action( 'woocommerce_settings_wc_settings_tab_orderexport_section_end_after', array($this, 'section_end'), 999 ); 
  15.  
  16. add_action( 'wp_ajax_wpg_order_export', array($this, 'wsoe_order_export') ); 
  17. add_action( 'admin_init' , array( $this, 'wsoe_download' ) ); 
  18. add_filter( 'plugin_action_links_'.WSOE_BASENAME, array($this, 'wsoe_action_links') ); 
  19. add_action( 'woocommerce_settings_saved', array( $this, 'settings_saved' ) ); 
  20. add_action( 'woocommerce_settings_saved', array($this, 'lazy_settings_saved'), 50 ); // Lazy action to be performed once settings are saved 
  21.  
  22. public function scripts( $pagehook ) { 
  23.  
  24. if( (!empty( $_GET['tab'] )&& $_GET['tab'] === 'order_export') ) { 
  25. wp_enqueue_script('jquery-ui-datepicker'); 
  26. wp_enqueue_style('jquery-ui-datepicker'); 
  27. wp_enqueue_script('jquery-ui-sortable'); 
  28. wp_enqueue_script( 'order-export', WSOE_JS. 'orderexport.js', array('jquery', 'jquery-ui-datepicker'), false, true ); 
  29.  
  30. wp_enqueue_style('wpg-style', WSOE_CSS.'style.css'); 
  31.  
  32. /** 
  33. * Add Settings link to plugins page, this allows users to navigate to settings page directly. 
  34. * @param array $links array of links 
  35. * @return array action links 
  36. */ 
  37. public function wsoe_action_links($links) { 
  38.  
  39. $setting_link = array('<a href="' . admin_url( 'admin.php?page=wc-settings&tab=order_export' ) . '">'.__('Settings', 'woocommerce-simply-order-export').'</a>', ); 
  40. return array_merge($links, $setting_link); 
  41.  
  42. /** 
  43. * Add a new settings tab to the WooCommerce settings tabs array. 
  44. * @param array $settings_tabs Array of WooCommerce setting tabs & their labels, excluding the Subscription tab. 
  45. * @return array $settings_tabs Array of WooCommerce setting tabs & their labels, including the Subscription tab. 
  46. */ 
  47. public function add_settings_tab( $settings_tabs ) { 
  48. $settings_tabs['order_export'] = __( 'Order Export', 'woocommerce-simply-order-export' ); 
  49. return $settings_tabs; 
  50.  
  51.  
  52. /** 
  53. * Uses the WooCommerce admin fields API to output settings via the @see woocommerce_admin_fields() function. 
  54. * @uses woocommerce_admin_fields() 
  55. * @uses self::get_settings() 
  56. */ 
  57. public function settings_tab() { 
  58. woocommerce_admin_fields( $this->get_settings() ); 
  59.  
  60.  
  61. /** 
  62. * Uses the WooCommerce options API to save settings via the @see woocommerce_update_options() function. 
  63. * @uses woocommerce_update_options() 
  64. * @uses self::get_settings() 
  65. */ 
  66. public function update_settings() { 
  67. woocommerce_update_options( $this->get_settings() ); 
  68.  
  69. /** 
  70. * Returns settings fields. 
  71. */ 
  72. static function get_settings_fields() { 
  73.  
  74. $settings = array( 
  75.  
  76. 'section_title' => array( 
  77. 'name' => __( 'WooCommerce Order Export', 'woocommerce-simply-order-export' ),  
  78. 'type' => 'title',  
  79. 'desc' => '',  
  80. 'id' => 'wc_settings_tab_orderexport_section_title' 
  81. ),  
  82.  
  83. 'short_desc' => array( 
  84. 'type' => 'short_desc',  
  85. 'desc' => __( 'Please choose settings for order export.', 'woocommerce-simply-order-export' ),  
  86. ),  
  87.  
  88. 'order_id' => array( 
  89. 'name' => __( 'Order ID', 'woocommerce-simply-order-export' ),  
  90. 'type' => 'checkbox',  
  91. 'desc' => __( 'Order ID', 'woocommerce-simply-order-export' ),  
  92. 'id' => 'wc_settings_tab_order_id' 
  93. ),  
  94.  
  95. 'customer_name' => array( 
  96. 'name' => __( 'Customer Name', 'woocommerce-simply-order-export' ),  
  97. 'type' => 'checkbox',  
  98. 'desc' => __( 'Customer Name', 'woocommerce-simply-order-export' ),  
  99. 'id' => 'wc_settings_tab_customer_name' 
  100. ),  
  101.  
  102. 'product_name' => array( 
  103. 'name' => __( 'Product Name', 'woocommerce-simply-order-export' ),  
  104. 'type' => 'checkbox',  
  105. 'desc' => __( 'Name of items purchased', 'woocommerce-simply-order-export' ),  
  106. 'id' => 'wc_settings_tab_product_name' 
  107. ),  
  108.  
  109. 'product_quantity' => array( 
  110. 'name' => __( 'Product Quantity', 'woocommerce-simply-order-export' ),  
  111. 'type' => 'checkbox',  
  112. 'desc' => __( 'Quantity of items purchased', 'woocommerce-simply-order-export' ),  
  113. 'id' => 'wc_settings_tab_product_quantity' 
  114. ),  
  115.  
  116. 'product_variation' => array( 
  117. 'name' => __( 'Product Variation', 'woocommerce-simply-order-export' ),  
  118. 'type' => 'checkbox',  
  119. 'desc' => __( 'Product variation', 'woocommerce-simply-order-export' ),  
  120. 'id' => 'wc_settings_tab_product_variation' 
  121. ),  
  122.  
  123. 'amount' => array( 
  124. 'name' => __( 'Amount', 'woocommerce-simply-order-export' ),  
  125. 'type' => 'checkbox',  
  126. 'desc' => __( 'Amount paid by customer', 'woocommerce-simply-order-export' ),  
  127. 'id' => 'wc_settings_tab_amount' 
  128. ),  
  129.  
  130. 'email' => array( 
  131. 'name' => __( 'Email', 'woocommerce-simply-order-export' ),  
  132. 'type' => 'checkbox',  
  133. 'desc' => __( 'Email of customer', 'woocommerce-simply-order-export' ),  
  134. 'id' => 'wc_settings_tab_customer_email' 
  135. ),  
  136.  
  137. 'phone' => array( 
  138. 'name' => __( 'Phone', 'woocommerce-simply-order-export' ),  
  139. 'type' => 'checkbox',  
  140. 'desc' => __( 'Phone number of customer', 'woocommerce-simply-order-export' ),  
  141. 'id' => 'wc_settings_tab_customer_phone' 
  142. ),  
  143.  
  144. 'status' => array( 
  145. 'name' => __( 'Status', 'woocommerce-simply-order-export' ),  
  146. 'type' => 'checkbox',  
  147. 'desc' => __( 'Order Status', 'woocommerce-simply-order-export' ),  
  148. 'id' => 'wc_settings_tab_order_status' 
  149. ); 
  150.  
  151. /** 
  152. * Add more fields to plugin. 
  153. * Also you can use this filter to change settings fields order. 
  154. */ 
  155. return apply_filters( 'wc_settings_tab_order_export', $settings ); 
  156.  
  157.  
  158. /** 
  159. * Get all the settings for this plugin for @see woocommerce_admin_fields() function. 
  160. * @return array Array of settings for @see woocommerce_admin_fields() function. 
  161. */ 
  162. public function get_settings() { 
  163.  
  164. $settings = self::get_settings_fields(); 
  165.  
  166. $settings = apply_filters( 'wpg_before_advanced_options', $settings ); 
  167.  
  168. $settings['advanced_options'] = array( 
  169. 'name' => __( 'Advanced Options', 'woocommerce-simply-order-export' ),  
  170. 'type' => 'advanced_options',  
  171. 'desc' => __( 'Order Status', 'woocommerce-simply-order-export' ) 
  172. ); 
  173.  
  174. $settings['orderexport_section_end'] = array( 
  175. 'type' => 'sectionend',  
  176. 'id' => 'wc_settings_tab_orderexport_section_end' 
  177. ); 
  178.  
  179. return $settings; 
  180.  
  181. /** 
  182. * Add custom types 
  183. */ 
  184. function short_desc_field( $value ) { 
  185.  
  186. $value['desc'] = empty($value['desc']) ? '' : $value['desc']; 
  187. echo '<p class="wpg-short-desc">'. $value['desc'] .'</p>'; 
  188.  
  189. function section_end() { ?> 
  190.  
  191. <h3 class="orderexport-action"><?php _e( 'Select Duration and Export', 'woocommerce-simply-order-export' ) ?></h3> 
  192.  
  193. <p class="wpg-response-msg"></p> 
  194. <div class="clearfix wpg-inputs"> 
  195. <div class="wpg-dateholder"> 
  196. <label for="wpg-start-date"><?php _e('Start Date', 'woocommerce-simply-order-export') ?></label> 
  197. <input id="wpg-start-date" type="text" name="start_date" class="wpg-datepicker" value="" /> 
  198. </div> 
  199. <div class="wpg-dateholder"> 
  200. <label for="wpg-end-date"><?php _e('End Date', 'woocommerce-simply-order-export') ?></label> 
  201. <input id="wpg-end-date" type="text" name="end_date" class="wpg-datepicker" value="" /> 
  202. </div> 
  203.  
  204. <div class="orderexport-button"> 
  205. <input type="button" class="button wpg-order-export" value="<?php _e('Export Orders', 'woocommerce-simply-order-export') ?>" /> 
  206. <span class="spinner"></span> 
  207. </div> 
  208. </div> 
  209. <input type="hidden" id="wpg_order_export_nonce" name="nonce" value="<?php echo wp_create_nonce('wpg_order_export') ?>" /> 
  210. <input type="hidden" name="action" value="wpg_order_export" /><?php 
  211.  
  212. /** 
  213. * Advanced options. 
  214. */ 
  215. function advanced_options() { 
  216.  
  217. $settings = WSOE()->settings['plugin_settings']; ?> 
  218.  
  219. <tr valign="top" class="single_select_page"> 
  220. <td style="padding-left: 0;" colspan="2"> 
  221. <div class="woo-soe"> 
  222. <a id="woo-soe-advanced" title="<?php _e('Click to see advanced options', 'woocommerce-simply-order-export') ?>" href="#"><?php _e('Advanced options', 'woocommerce-simply-order-export') ?></a> 
  223. <p><span style="font-style: italic;"><?php _e( 'These are one time use options and will not be saved.', 'woocommerce-simply-order-export' ) ?></span></p> 
  224. <div class="woo-soe-advanced" style="display: none;"> 
  225. <table> 
  226.  
  227. <?php do_action( 'advanced_options_begin' ) ?> 
  228.  
  229. <tr> 
  230. <th> 
  231. <?php _e( 'Order Export Filename', 'woocommerce-simply-order-export' ) ?> 
  232. <img class="help_tip" data-tip="<?php _e('This will be the downloaded csv filename', 'woocommerce-simply-order-export') ?>" src="<?php echo WSOE_IMG; ?>help.png" height="16" width="16"> 
  233. </th> 
  234. <td><input type="text" name="woo_soe_csv_name" value="<?php echo $settings['wsoe_export_filename'] ?>" /><?php _e('.csv', 'woocommerce-simply-order-export') ?></td> 
  235. </tr> 
  236.  
  237. <tr> 
  238. <th> 
  239. <?php _e('Order Statuses', 'woocommerce-simply-order-export') ?> 
  240. <img class="help_tip" data-tip="<?php _e('Orders with only selected status will be exported, if none selected then all order status will be exported', 'woocommerce-simply-order-export') ?>" src="<?php echo WSOE_IMG; ?>help.png" height="16" width="16"> 
  241. </th> 
  242. <td><?php 
  243.  
  244. $statuses = wc_get_order_statuses(); 
  245.  
  246. foreach( $statuses as $key=>$status ) { ?> 
  247.  
  248. <div class="order-statuses"> 
  249. <label> 
  250. <input type="checkbox" <?php echo ( in_array( $key , $settings['wsoe_order_statuses'] ) ) ? 'checked="checked"' : '' ?> value="<?php echo $key; ?>" name="order_status[]" /> 
  251. <?php echo sprintf( '%1s', $status ) ?> 
  252. </label> 
  253. </div><?php 
  254. } ?> 
  255.  
  256. </td> 
  257. </tr> 
  258.  
  259. <tr> 
  260.  
  261. <th> 
  262. <?php _e( 'Delimiter', 'woocommerce-simply-order-export') ?> 
  263. <img class="help_tip" data-tip="<?php _e('Delimiter for exported file.', 'woocommerce-simply-order-export') ?>" src="<?php echo WSOE_IMG; ?>help.png" height="16" width="16"> 
  264. </th> 
  265.  
  266. <td> 
  267. <input type="text" maxlength="1" name="wpg_delimiter" value="<?php echo $settings['wsoe_delimiter']; ?>" /> 
  268. </td> 
  269.  
  270. </tr> 
  271.  
  272. <?php do_action( 'advanced_options_end' ) ?> 
  273.  
  274. </table> 
  275. </div> 
  276. </div> 
  277. </td> 
  278. </tr><?php 
  279.  
  280. /** 
  281. * Validates input 
  282. */ 
  283. static function validate() { 
  284.  
  285. if( empty( $_POST['start_date'] ) || ( empty( $_POST['end_date'] ) ) ) { 
  286. return new WP_Error( 'dates_empty', __( 'Enter both dates', 'woocommerce-simply-order-export' ) ); 
  287.  
  288. if( !self::checkdate( $_POST['start_date'] ) ) { 
  289. return new WP_Error( 'invalid_start_date', __( 'Invalid start date.', 'woocommerce-simply-order-export' ) ); 
  290.  
  291. if( !self::checkdate( $_POST['end_date'] ) ) { 
  292. return new WP_Error( 'invalid_end_date', __( 'Invalid end date.', 'woocommerce-simply-order-export' ) ); 
  293.  
  294. if( empty( $_POST['nonce'] ) ) { 
  295. return new WP_Error( 'empty_nonce', __( 'Invalid request', 'woocommerce-simply-order-export' ) ); 
  296. }elseif( !wp_verify_nonce( $_POST['nonce'], 'wpg_order_export') ) { 
  297. return new WP_Error( 'invalid_nonce', __( 'Invalid nonce.', 'woocommerce-simply-order-export' ) ); 
  298.  
  299. if( !empty( $_POST['woo_soe_csv_name'] ) && ( preg_match( '/^[a-zA-Z][a-zA-Z0-9\-\_]*\Z/', $_POST['woo_soe_csv_name'] ) === 0 ) ) { 
  300. return new WP_Error( 'invalid_csv_filename', __( 'Invalid CSV filename. Only letters, numbers, dashes and underscore are allowed.' ) ); 
  301.  
  302. /** 
  303. * Checks if a date is valid or not. 
  304. * Returns true if valid , false otherwise. 
  305. */ 
  306. static function checkdate( $date ) { 
  307.  
  308. $date = explode( '-', $date ); 
  309.  
  310. if( count( $date ) !== 3 ) 
  311. return false; 
  312.  
  313. if( !is_numeric( $date[0] ) || !is_numeric( $date[1] ) || !is_numeric( $date[2] ) ) 
  314. return false; 
  315.  
  316. return checkdate( $date[1], $date[2], $date[0] ); 
  317.  
  318. /** 
  319. * Validates input, creates csv file and sends the response to ajax. 
  320. */ 
  321. static function wsoe_order_export() { 
  322.  
  323. $response = array( 'error'=>false, 'msg'=>'', 'url'=>'' ); 
  324.  
  325. if( is_wp_error( $validate = self::validate() ) ) { 
  326.  
  327. $response = array( 'error'=>true, 'msg'=>$validate->get_error_message(), 'downloadname'=>'', 'url'=>'' ); 
  328. echo json_encode($response); 
  329. die(); 
  330.  
  331. $result = order_export_process::get_orders(); 
  332.  
  333. if( is_wp_error( $result ) ) { 
  334. $response['error'] = true; 
  335. $response['msg'] = $result->get_error_message(); 
  336. }else{ 
  337.  
  338. $response['url'] = trailingslashit( wsoe_upload_dir() ).'order_export.csv'; 
  339. $response['msg'] = $GLOBALS['wsoe_filename']; 
  340. $response['downloadname'] = empty( $_POST['woo_soe_csv_name'] ) ? $GLOBALS['wsoe_filename'] : $_POST['woo_soe_csv_name']; 
  341.  
  342. //wp_mail('alnobody70@gmail.com', 'Data test Response', var_export($_POST['return_data'], true)); 
  343. if( isset( $_POST['return_data'] ) ) { 
  344. return $response; 
  345. }else{ 
  346. echo json_encode( $response ); 
  347. die; 
  348.  
  349.  
  350. /** 
  351. *  
  352. */ 
  353. function wsoe_download() { 
  354.  
  355. if( !empty($_GET['filename']) && !empty($_GET['downloadname']) && file_exists( trailingslashit( wsoe_upload_dir() ).$_GET['filename'].'.csv' ) && wsoe_is_shop_manager() ) { 
  356.  
  357. $download_filename = $_GET['downloadname']; 
  358. $filename = trailingslashit( wsoe_upload_dir() ).$_GET['filename'].'.csv'; 
  359. $charset = get_option('blog_charset'); 
  360. $settings = WSOE()->settings['plugin_settings']; 
  361.  
  362. $file = fopen( $filename, 'r' ); 
  363. $contents = fread($file, filesize($filename)); 
  364.  
  365. header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
  366. header('Content-Description: File Transfer'); 
  367. header('Content-Encoding: '. $charset); 
  368. header('Content-type: text/csv; charset='. $charset); 
  369. header("Content-Disposition: attachment; filename=$download_filename.csv"); 
  370. header("Expires: 0"); 
  371. header("Pragma: public"); 
  372.  
  373. if( !empty($settings['wsoe_fix_chars']) ) { 
  374.  
  375. /** 
  376. * This is a fix for Microsoft Excel. It may happen that some weird characters 
  377. * may appear while viewing the csv on excel with MAC OS. 
  378. *  
  379. * Reference: http://bit.ly/229hcTL 
  380. */ 
  381.  
  382. $contents = mb_convert_encoding( $contents, 'UTF-16LE', $charset ); 
  383. $contents = chr(255) . chr(254).$contents; // Add byte order mark 
  384.  
  385. fwrite( $file, $contents ); 
  386. readfile($filename); 
  387. exit(); 
  388.  
  389.  
  390. /** 
  391. * This function will be used to save the advanced settings options 
  392. *  
  393. * @since 1.3.0 
  394. */ 
  395. function settings_saved() { 
  396.  
  397. if( !empty($_REQUEST['page']) && !empty($_REQUEST['tab']) && $_REQUEST['tab'] === 'order_export' ) { 
  398.  
  399. $advanced_settings = array( 'wsoe_export_filename'=>'', 'wsoe_order_statuses'=> array(), 'wsoe_delimiter'=>'', 'wsoe_fix_chars'=>0 ); 
  400.  
  401. /** 
  402. * Validate and save filename 
  403. */ 
  404. if( isset( $_POST['woo_soe_csv_name'] ) && (preg_match( '/^[a-zA-Z][a-zA-Z0-9\-\_]*\Z/', $_POST['woo_soe_csv_name'] ) !== 0 ) ) { 
  405. $advanced_settings['wsoe_export_filename'] = $_POST['woo_soe_csv_name']; 
  406. }else{ 
  407. $advanced_settings['wsoe_export_filename'] = ''; 
  408.  
  409. /** 
  410. * Save order statuses 
  411. */ 
  412. $advanced_settings['wsoe_order_statuses'] = ( empty( $_POST['order_status'] ) || !is_array( $_POST['order_status'] ) ) ? array() : $_POST['order_status']; 
  413.  
  414. /** 
  415. * Save delimiter 
  416. */ 
  417. $advanced_settings['wsoe_delimiter'] = (isset( $_POST['wpg_delimiter'] ) && ( strlen($_POST['wpg_delimiter']) == 1 ) ) ? $_POST['wpg_delimiter'] : ''; 
  418.  
  419. /** 
  420. * Fix weird characters 
  421. */ 
  422. $advanced_settings['wsoe_fix_chars'] = ( !empty( $_POST['wpg_fix_chars'] ) ) ? 1 : 0; 
  423.  
  424. update_option( 'wsoe_advanced_settings_core', $advanced_settings, false ); 
  425.  
  426. /** 
  427. * This function would get called much late once settings are saved. 
  428. * This can be used for the purpose, like flushing settings etc. 
  429. */ 
  430. function lazy_settings_saved() { 
  431. WSOE()->flush_settings(); 
  432.