class-wc-customer-order-csv-export-handler.php
10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
<?php
/**
* WooCommerce Customer/Order CSV Export
*
* This source file is subject to the GNU General Public License v3.0
* that is bundled with this package in the file license.txt.
* It is also available through the world-wide-web at this URL:
* http://www.gnu.org/licenses/gpl-3.0.html
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@skyverge.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade WooCommerce Customer/Order CSV Export to newer
* versions in the future. If you wish to customize WooCommerce Customer/Order CSV Export for your
* needs please refer to http://docs.woothemes.com/document/ordercustomer-csv-exporter/
*
* @package WC-Customer-Order-CSV-Export/Handler
* @author SkyVerge
* @copyright Copyright (c) 2012-2016, SkyVerge, Inc.
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0
*/
defined( 'ABSPATH' ) or exit;
/**
* Customer/Order CSV Export Handler
*
* Handles export actions/methods
*
* @since 3.0
*/
class WC_Customer_Order_CSV_Export_Handler {
/** @var array order IDs or customer IDs to export */
public $ids;
/** @var string file name for export or download */
public $filename;
/**
* Initializes the export object from an array of valid order/customer IDs and sets the filename
*
* @since 3.0
* @param int|array $ids orders/customer IDs to export / download
* @param string $export_type what is being exported, `orders` or `customers`
* @return \WC_Customer_Order_CSV_Export_Handler
*/
public function __construct( $ids, $export_type = 'orders' ) {
// handle single order/customer exports
if ( ! is_array( $ids ) ) {
$ids = array( $ids );
}
$this->export_type = $export_type;
$this->ids = $ids;
// set file name
$this->filename = $this->replace_filename_variables();
// instantiate writer here & get CSV
$this->generator = new WC_Customer_Order_CSV_Export_Generator( $this->ids );
}
/**
* Exports orders/customers to CSV and downloads via browser
*
* @since 3.0
*/
public function download() {
$this->export_via( 'download' );
}
/**
* Exports test CSV and downloads via browser
*
* @since 3.0
*/
public function test_download() {
$this->test_export_via( 'download' );
}
/**
* Exports orders/customers to CSV and uploads to remote server
*
* @since 3.0
*/
public function upload() {
$this->export_via( 'ftp' );
}
/**
* Exports test CSV and uploads to remote server
*
* @since 3.0
*/
public function test_upload() {
$this->test_export_via( 'ftp' );
}
/**
* Exports orders/customers to CSV and HTTP POSTs to remote server
*
* @since 3.0
*/
public function http_post() {
$this->export_via( 'http_post' );
}
/**
* Exports test CSV and HTTP POSTs to remote server
*
* @since 3.0
*/
public function test_http_post() {
$this->test_export_via( 'http_post' );
}
/**
* Exports orders/customers to CSV and emails admin with CSV as attachment
*
* @since 3.0
*/
public function email() {
$this->export_via( 'email' );
}
/**
* Exports test CSV and emails admin with CSV as attachment
*
* @since 3.0
*/
public function test_email() {
$this->test_export_via( 'email' );
}
/**
* Exports CSV via the given method
*
* @since 3.0
* @param string $method the export method, `download`, `ftp`, `http_post`, `email`
*/
public function export_via( $method ) {
// try to set unlimited script timeout
@set_time_limit( 0 );
try {
// get method (download, FTP, etc)
$export = $this->get_export_method( $method );
if ( ! is_object( $export ) ) {
throw new Exception( sprintf( __( 'Invalid Export Method: %s', 'woocommerce-customer-order-csv-export' ), $method ) );
}
if ( 'orders' == $this->export_type ) {
// mark each order as exported
// this must be done before download, as the download function exits() to prevent additional output from contaminating the CSV file
$this->mark_orders_as_exported( $method );
}
$export->perform_action( $this->filename, 'orders' == $this->export_type ? $this->generator->get_orders_csv() : $this->generator->get_customers_csv() );
} catch ( Exception $e ) {
// log errors
wc_customer_order_csv_export()->log( $e->getMessage() );
}
}
/**
* Exports a test CSV via the given method
*
* @since 3.0
* @param string $method the export method
* @return string 'Success' or error message
*/
public function test_export_via( $method ) {
// try to set unlimited script timeout
@set_time_limit( 0 );
try {
// get method (download, FTP, etc)
$export = $this->get_export_method( $method );
if ( ! is_object( $export ) ) {
throw new Exception( sprintf( __( 'Invalid Export Method: %s', 'woocommerce-customer-order-csv-export' ), $method ) );
}
// simple test CSV
$export->perform_action( 'test.csv',"column_1,column_2,column_3\ntest_1,test_2,test_3" );
return __( 'Test was successful!', 'woocommerce-customer-order-csv-export' );
} catch ( Exception $e ) {
// log errors
wc_customer_order_csv_export()->log( $e->getMessage() );
return sprintf( __( 'Test failed: %s', 'woocommerce-customer-order-csv-export' ), $e->getMessage() );
}
}
/**
* Returns the export method object
*
* @since 3.0
* @param string $method the export method, `download`, `ftp`, `http_post`, or `email`
* @return object the export method
*/
private function get_export_method( $method ) {
// get the export method specified
switch ( $method ) {
case 'download':
return wc_customer_order_csv_export()->load_class( '/includes/export-methods/class-wc-customer-order-csv-export-method-download.php', 'WC_Customer_Order_CSV_Export_Method_Download' );
case 'ftp':
// abstract FTP class
require_once( wc_customer_order_csv_export()->get_plugin_path() . '/includes/export-methods/ftp/abstract-wc-customer-order-csv-export-method-file-transfer.php' );
$security = get_option( 'wc_customer_order_csv_export_ftp_security' );
switch ( $security ) {
// FTP over SSH
case 'sftp' :
return wc_customer_order_csv_export()->load_class( '/includes/export-methods/ftp/class-wc-customer-order-csv-export-method-sftp.php', 'WC_Customer_Order_CSV_Export_Method_SFTP' );
// FTP with Implicit SSL
case 'ftp_ssl' :
return wc_customer_order_csv_export()->load_class( '/includes/export-methods/ftp/class-wc-customer-order-csv-export-method-ftp-implicit-ssl.php', 'WC_Customer_Order_CSV_Export_Method_FTP_Implicit_SSL' );
// FTP with explicit SSL/TLS *or* regular FTP
case 'ftps' :
case 'none' :
return wc_customer_order_csv_export()->load_class( '/includes/export-methods/ftp/class-wc-customer-order-csv-export-method-ftp.php', 'WC_Customer_Order_CSV_Export_Method_FTP' );
}
break;
case 'http_post':
return wc_customer_order_csv_export()->load_class( '/includes/export-methods/class-wc-customer-order-csv-export-method-http-post.php', 'WC_Customer_Order_CSV_Export_Method_HTTP_POST' );
case 'email':
return wc_customer_order_csv_export()->load_class( '/includes/export-methods/class-wc-customer-order-csv-export-method-email.php', 'WC_Customer_Order_CSV_Export_Method_Email' );
default:
/**
* CSV Export Get Export Method
*
* Triggered when getting the export method. This is designed for
* custom methods to hook in and load their class so it can be
* returned and used.
*
* @since 3.4.0
* @param \WC_Customer_Order_CSV_Export_Handler $this, handler instance
*/
do_action( 'wc_customer_order_csv_export_get_export_method', $this );
$class_name = sprintf( 'WC_Customer_Order_CSV_Export_Custom_Method_%s', ucwords( strtolower( $method ) ) );
return class_exists( $class_name ) ? new $class_name : null;
}
}
/**
* Marks orders as exported by setting the `_wc_customer_order_csv_export_is_exported` order meta flag
*
* @since 3.0
* @param string $method the export method, `download`, `ftp`, `http_post`, or `email`
*/
public function mark_orders_as_exported( $method = 'download' ) {
foreach ( $this->ids as $order_id ) {
// add exported flag
update_post_meta( $order_id, '_wc_customer_order_csv_export_is_exported', 1 );
$order = wc_get_order( $order_id );
switch ( $method ) {
// note that order downloads using the AJAX order action are not marked or noted, only bulk order downloads
case 'download':
$message = __( 'downloaded.', 'woocommerce-customer-order-csv-export' );
break;
case 'ftp':
$message = __( 'uploaded to remote server.', 'woocommerce-customer-order-csv-export' );
break;
case 'http_post':
$message = __( 'POSTed to remote server.', 'woocommerce-customer-order-csv-export' );
break;
case 'email':
$message = __( 'emailed.', 'woocommerce-customer-order-csv-export' );
break;
}
/**
* Filter if an order note should be added when an order is successfully exported
*
* @since 3.9.1
* @param bool $add_order_note true if the order note should be added, false otherwise
*/
if ( apply_filters( 'wc_customer_order_csv_export_add_order_note', true ) ) {
$order->add_order_note( sprintf( __( 'Order exported to CSV and successfully %s', 'woocommerce-customer-order-csv-export' ), $message ) );
}
/**
* CSV Order Exported Action.
*
* Fired when an order is automatically exported. Note this includes
* orders exported via the Orders bulk action.
*
* @since 3.1
* @param WC_Order $order order being exported
* @param string $method how the order is exported (ftp, download, etc)
* @param string $message order note message
* @param \WC_Customer_Order_CSV_Export_Handler $this, handler instance
*/
do_action( 'wc_customer_order_csv_export_order_exported', $order, $method, $message, $this );
}
}
/**
* Replaces variables in file name setting (e.g. %%timestamp%% becomes 2013_03_20_16_22_14 )
*
* @since 3.0
* @return string filename with variables replaced
*/
private function replace_filename_variables() {
$pre_replace_filename = get_option( 'orders' == $this->export_type ? 'wc_customer_order_csv_export_order_filename' : 'wc_customer_order_csv_export_customer_filename' );
$variables = array( '%%timestamp%%', '%%order_ids%%' );
$replacement = array( date( 'Y_m_d_H_i_s', current_time( 'timestamp' ) ), implode( '-', $this->ids ) );
$post_replace_filename = str_replace( $variables, $replacement, $pre_replace_filename );
return apply_filters( 'wc_customer_order_csv_export_filename', $post_replace_filename, $pre_replace_filename, $this->ids );
}
} //end \WC_Customer_Order_CSV_Export_Handler class