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