/var/www/html_us/wp-content/plugins/woocommerce/src/Internal/Admin/Schedulers/OrdersScheduler.php


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
<?php
/**
 * Order syncing related functions and actions.
 */

namespace Automattic\WooCommerce\Internal\Admin\Schedulers;

defined'ABSPATH' ) || exit;

use 
Automattic\WooCommerce\Admin\API\Reports\Coupons\DataStore as CouponsDataStore;
use 
Automattic\WooCommerce\Admin\API\Reports\Orders\Stats\DataStore as OrdersStatsDataStore;
use 
Automattic\WooCommerce\Admin\API\Reports\Products\DataStore as ProductsDataStore;
use 
Automattic\WooCommerce\Admin\API\Reports\Taxes\DataStore as TaxesDataStore;
use 
Automattic\WooCommerce\Admin\API\Reports\Customers\DataStore as CustomersDataStore;
use 
Automattic\WooCommerce\Admin\API\Reports\Cache as ReportsCache;
use 
Automattic\WooCommerce\Admin\Overrides\Order;
use 
Automattic\WooCommerce\Internal\DataStores\Orders\OrdersTableDataStore;
use 
Automattic\WooCommerce\Utilities\OrderUtil;

/**
 * OrdersScheduler Class.
 */
class OrdersScheduler extends ImportScheduler {
    
/**
     * Slug to identify the scheduler.
     *
     * @var string
     */
    
public static $name 'orders';

    
/**
     * Attach order lookup update hooks.
     *
     * @internal
     */
    
public static function init() {
        
// Activate WC_Order extension.
        
\Automattic\WooCommerce\Admin\Overrides\Order::add_filters();
        
\Automattic\WooCommerce\Admin\Overrides\OrderRefund::add_filters();

        
// Order and refund data must be run on these hooks to ensure meta data is set.
        
add_action'woocommerce_update_order', array( __CLASS__'possibly_schedule_import' ) );
        
add_filter'woocommerce_create_order', array( __CLASS__'possibly_schedule_import' ) );
        
add_action'woocommerce_refund_created', array( __CLASS__'possibly_schedule_import' ) );

        
OrdersStatsDataStore::init();
        
CouponsDataStore::init();
        
ProductsDataStore::init();
        
TaxesDataStore::init();

        
parent::init();
    }

    
/**
     * Add customer dependencies.
     *
     * @internal
     * @return array
     */
    
public static function get_dependencies() {
        return array(
            
'import_batch_init' => \Automattic\WooCommerce\Internal\Admin\Schedulers\CustomersScheduler::get_action'import_batch_init' ),
        );
    }

    
/**
     * Get the order/refund IDs and total count that need to be synced.
     *
     * @internal
     * @param int      $limit Number of records to retrieve.
     * @param int      $page  Page number.
     * @param int|bool $days Number of days prior to current date to limit search results.
     * @param bool     $skip_existing Skip already imported orders.
     */
    
public static function get_items$limit 10$page 1$days false$skip_existing false ) {
        if ( 
OrderUtil::custom_orders_table_usage_is_enabled() ) {
            return 
self::get_items_from_orders_table$limit$page$days$skip_existing );
        } else {
            return 
self::get_items_from_posts_table$limit$page$days$skip_existing );
        }
    }

    
/**
     * Helper method to ger order/refund IDS and total count that needs to be synced.
     *
     * @internal
     * @param int      $limit Number of records to retrieve.
     * @param int      $page  Page number.
     * @param int|bool $days Number of days prior to current date to limit search results.
     * @param bool     $skip_existing Skip already imported orders.
     *
     * @return object Total counts.
     */
    
private static function get_items_from_posts_table$limit$page$days$skip_existing ) {
        global 
$wpdb;
        
$where_clause '';
        
$offset       $page ? ( $page ) * $limit 0;

        if ( 
is_int$days ) ) {
            
$days_ago      gmdate'Y-m-d 00:00:00'time() - ( DAY_IN_SECONDS $days ) );
            
$where_clause .= " AND post_date_gmt >= '{$days_ago}'";
        }

        if ( 
$skip_existing ) {
            
$where_clause .= " AND NOT EXISTS (
                SELECT 1 FROM 
{$wpdb->prefix}wc_order_stats
                WHERE 
{$wpdb->prefix}wc_order_stats.order_id = {$wpdb->posts}.ID
            )"
;
        }

        
$count $wpdb->get_var(
            
"SELECT COUNT(*) FROM {$wpdb->posts}
            WHERE post_type IN ( 'shop_order', 'shop_order_refund' )
            AND post_status NOT IN ( 'wc-auto-draft', 'auto-draft', 'trash' )
            
{$where_clause}"
        
); // phpcs:ignore unprepared SQL ok.

        
$order_ids absint$count ) > $wpdb->get_col(
            
$wpdb->prepare(
                
"SELECT ID FROM {$wpdb->posts}
                WHERE post_type IN ( 'shop_order', 'shop_order_refund' )
                AND post_status NOT IN ( 'wc-auto-draft', 'auto-draft', 'trash' )
                
{$where_clause}
                ORDER BY post_date_gmt ASC
                LIMIT %d
                OFFSET %d"
,
                
$limit,
                
$offset
            
)
        ) : array(); 
// phpcs:ignore unprepared SQL ok.

        
return (object) array(
            
'total' => absint$count ),
            
'ids'   => $order_ids,
        );
    }

    
/**
     * Helper method to ger order/refund IDS and total count that needs to be synced from HPOS.
     *
     * @internal
     * @param int      $limit Number of records to retrieve.
     * @param int      $page  Page number.
     * @param int|bool $days Number of days prior to current date to limit search results.
     * @param bool     $skip_existing Skip already imported orders.
     *
     * @return object Total counts.
     */
    
private static function get_items_from_orders_table$limit$page$days$skip_existing ) {
        global 
$wpdb;
        
$where_clause '';
        
$offset       $page ? ( $page ) * $limit 0;
        
$order_table  OrdersTableDataStore::get_orders_table_name();

        if ( 
is_int$days ) ) {
            
$days_ago      gmdate'Y-m-d 00:00:00'time() - ( DAY_IN_SECONDS $days ) );
            
$where_clause .= " AND orders.date_created_gmt >= '{$days_ago}'";
        }

        if ( 
$skip_existing ) {
            
$where_clause .= "AND NOT EXiSTS (
                    SELECT 1 FROM 
{$wpdb->prefix}wc_order_stats
                    WHERE 
{$wpdb->prefix}wc_order_stats.order_id = orders.id
                    )
                "
;
        }

        
$count $wpdb->get_var(
            
"
SELECT COUNT(*) FROM 
{$order_table} AS orders
WHERE type in ( 'shop_order', 'shop_order_refund' )
AND status NOT IN ( 'wc-auto-draft', 'trash', 'auto-draft' )
{$where_clause}
"
        
); // phpcs:ignore unprepared SQL ok.

        
$order_ids absint$count ) > $wpdb->get_col(
            
$wpdb->prepare(
                
"SELECT id FROM {$order_table} AS orders
                WHERE type IN ( 'shop_order', 'shop_order_refund' )
                AND status NOT IN ( 'wc-auto-draft', 'auto-draft', 'trash' )
                
{$where_clause}
                ORDER BY date_created_gmt ASC
                LIMIT %d
                OFFSET %d"
,
                
$limit,
                
$offset
            
)
        ) : array(); 
// phpcs:ignore unprepared SQL ok.

        
return (object) array(
            
'total' => absint$count ),
            
'ids'   => $order_ids,
        );
    }

    
/**
     * Get total number of rows imported.
     *
     * @internal
     */
    
public static function get_total_imported() {
        global 
$wpdb;
        return 
$wpdb->get_var"SELECT COUNT(*) FROM {$wpdb->prefix}wc_order_stats" );
    }

    
/**
     * Schedule this import if the post is an order or refund.
     *
     * @param int $order_id Post ID.
     *
     * @internal
     * @returns int The order id
     */
    
public static function possibly_schedule_import$order_id ) {
        if ( ! 
OrderUtil::is_order$order_id, array( 'shop_order' ) ) && 'woocommerce_refund_created' !== current_filter() ) {
            return 
$order_id;
        }

        
self::schedule_action'import', array( $order_id ) );
        return 
$order_id;
    }

    
/**
     * Imports a single order or refund to update lookup tables for.
     * If an error is encountered in one of the updates, a retry action is scheduled.
     *
     * @internal
     * @param int $order_id Order or refund ID.
     * @return void
     */
    
public static function import$order_id ) {
        
$order wc_get_order$order_id );

        
// If the order isn't found for some reason, skip the sync.
        
if ( ! $order ) {
            return;
        }

        
$type $order->get_type();

        
// If the order isn't the right type, skip sync.
        
if ( 'shop_order' !== $type && 'shop_order_refund' !== $type ) {
            return;
        }

        
// If the order has no id or date created, skip sync.
        
if ( ! $order->get_id() || ! $order->get_date_created() ) {
            return;
        }

        
$results = array(
            
OrdersStatsDataStore::sync_order$order_id ),
            
ProductsDataStore::sync_order_products$order_id ),
            
CouponsDataStore::sync_order_coupons$order_id ),
            
TaxesDataStore::sync_order_taxes$order_id ),
            
CustomersDataStore::sync_order_customer$order_id ),
        );

        if ( 
'shop_order' === $type ) {
            
$order_refunds $order->get_refunds();

            foreach ( 
$order_refunds as $refund ) {
                
OrdersStatsDataStore::sync_order$refund->get_id() );
            }
        }

        
ReportsCache::invalidate();
    }

    
/**
     * Delete a batch of orders.
     *
     * @internal
     * @param int $batch_size Number of items to delete.
     * @return void
     */
    
public static function delete$batch_size ) {
        global 
$wpdb;

        
$order_ids $wpdb->get_col(
            
$wpdb->prepare(
                
"SELECT order_id FROM {$wpdb->prefix}wc_order_stats ORDER BY order_id ASC LIMIT %d",
                
$batch_size
            
)
        );

        foreach ( 
$order_ids as $order_id ) {
            
OrdersStatsDataStore::delete_order$order_id );
        }
    }
}