class-wc-customer-order-csv-export-cron.php
5.65 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
<?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/Generator
* @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 Cron Class
*
* Adds custom schedule and schedules the export event
*
* @since 3.0
*/
class WC_Customer_Order_CSV_Export_Cron {
/**
* Setup hooks and filters specific to WP-cron functions
*
* @since 3.0
*/
public function __construct() {
// Add custom schedule, e.g. every 10 minutes
add_filter( 'cron_schedules', array( $this, 'add_auto_export_schedule' ) );
// Schedule auto-update events if they don't exist, run in both frontend and backend so events are still scheduled when an admin reactivates the plugin
add_action( 'init', array( $this, 'add_scheduled_export' ) );
// Trigger export + upload of non-exported orders, wp-cron fires this action on the given recurring schedule
add_action( 'wc_customer_order_csv_export_auto_export_orders', array( $this, 'auto_export_orders' ) );
$this->exports_enabled = ( 'disabled' != get_option( 'wc_customer_order_csv_export_auto_export_method' ) );
}
/**
* If automatic exports are enabled, add the custom interval (e.g. every 15 minutes) set on the admin settings page
*
* @since 3.0
* @param array $schedules WP-Cron schedules array
* @return array $schedules now including our custom schedule
*/
public function add_auto_export_schedule( $schedules ) {
if ( $this->exports_enabled ) {
$export_interval = get_option( 'wc_customer_order_csv_export_auto_export_interval' );
if ( $export_interval ) {
$schedules['wc_customer_order_csv_export_auto_export_interval'] = array(
'interval' => (int) $export_interval * 60,
'display' => sprintf( __( 'Every %d minutes', 'woocommerce-customer-order-csv-export' ), (int) $export_interval )
);
}
}
return $schedules;
}
/**
* If automatic exports are enabled, add the event if not already scheduled
*
* This performs a `do_action( 'wc_customer_order_csv_export_auto_export_orders' )` on our custom schedule
*
* @since 3.0
*/
public function add_scheduled_export() {
if ( $this->exports_enabled ) {
// Schedule export
if ( ! wp_next_scheduled( 'wc_customer_order_csv_export_auto_export_orders' ) ) {
$start_time = get_option( 'wc_customer_order_csv_export_auto_export_start_time' );
$curr_time = current_time( 'timestamp' );
if ( $start_time ) {
if ( $curr_time > strtotime( 'today ' . $start_time, $curr_time ) ) {
$start_timestamp = strtotime( 'tomorrow ' . $start_time, $curr_time ) - ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
} else {
$start_timestamp = strtotime( 'today ' . $start_time, $curr_time ) - ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
}
} else {
$export_interval = get_option( 'wc_customer_order_csv_export_auto_export_interval' );
$start_timestamp = strtotime( "now +{$export_interval} minutes" );
}
wp_schedule_event( $start_timestamp, 'wc_customer_order_csv_export_auto_export_interval', 'wc_customer_order_csv_export_auto_export_orders' );
}
}
}
/**
* Exports any non-exported orders to CSV and performs the chosen action (upload, HTTP POST, email)
*
* @since 3.0
*/
public function auto_export_orders() {
$order_statuses = (array) get_option( 'wc_customer_order_csv_export_auto_export_statuses' );
/**
* Query order IDs to export
*
* By default we get un-exported order IDs,
* but this filter can change the query behavior
*
* @since 3.11.2
* @param array $query_args WP_Query args to fetch order IDs to export automatically
* @param array $order_statuses Order statuses to export
*/
$query_args = apply_filters( 'wc_customer_order_csv_export_auto_export_order_query_args', array(
'fields' => 'ids',
'post_type' => 'shop_order',
'post_status' => empty( $order_statuses ) ? 'any' : $order_statuses,
'nopaging' => true,
'meta_key' => '_wc_customer_order_csv_export_is_exported',
'meta_value' => 0
), $order_statuses );
$query = new WP_Query( $query_args );
if ( ! empty( $query->posts ) ) {
// Export the queried orders
$export = new WC_Customer_Order_CSV_Export_Handler( $query->posts );
$export->export_via( get_option( 'wc_customer_order_csv_export_auto_export_method' ) );
}
/**
* Auto-Export Action.
*
* Fired when orders are auto-exported
*
* @since 3.0
* @param array $order_ids order IDs that were exported
*/
do_action( 'wc_customer_order_csv_export_orders_exported', $query->posts );
}
/**
* Clear scheduled events upon deactivation
*
* @since 3.1
*/
public function clear_scheduled_export() {
wp_clear_scheduled_hook( 'wc_customer_order_csv_export_auto_export_orders' );
}
} // end \WC_Customer_Order_CSV_Export_Cron class