/var/www/html_uk/wp-content/plugins/automatewoo/includes/Async_Events/Order_Created.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
<?php

namespace AutomateWoo\Async_Events;

use 
AutomateWoo\Clean;
use 
AutomateWoo\Subscription_Workflow_Helper;
use 
WC_Order;
use 
WP_Post;

defined'ABSPATH' ) || exit;

/**
 * Class Order_Created
 *
 * @since 4.8.0
 */
class Order_Created extends Abstract_Async_Event {

    use 
UniqueEventsForRequestHelper;

    const 
MAYBE_ORDER_CREATED_HOOK 'automatewoo/async/maybe_order_created';
    const 
ORDER_CREATED_META_KEY   '_automatewoo_order_created';

    
/**
     * Init the event.
     */
    
public function init() {
        
add_action'woocommerce_new_order', [ $this'enqueue_maybe_order_created_async_event' ], 100);
        
add_action'woocommerce_order_status_changed', [ $this'handle_order_status_changed' ], 50);
        
add_actionself::MAYBE_ORDER_CREATED_HOOK, [ $this'maybe_do_order_created_action' ] );
    }

    
/**
     * Handle order status change.
     *
     * @param int      $order_id
     * @param string   $old_status
     * @param string   $new_status
     * @param WC_Order $order
     *
     * @since 5.5.23
     */
    
public function handle_order_status_changedint $order_idstring $old_statusstring $new_statusWC_Order $order ) {
        
$draft_statuses aw_get_draft_order_statuses();

        
// ensure that the old status IS a draft status and the new status IS NOT a draft status
        
if ( in_array$old_status$draft_statusestrue ) && ! in_array$new_status$draft_statusestrue ) ) {
            
$this->enqueue_maybe_order_created_async_event$order_id );
        }
    }

    
/**
     * Handle post status transition.
     *
     * @param string  $new_status
     * @param string  $old_status
     * @param WP_Post $post
     *
     * @deprecated use \AutomateWoo\Async_Events\Order_Created::handle_order_status_changed()
     */
    
public function handle_transition_post_statusstring $new_statusstring $old_statusWP_Post $post ) {
        if ( 
$post->post_type !== 'shop_order' ) {
            return;
        }

        
wc_deprecated_function__METHOD__'5.6.0''\AutomateWoo\Async_Events\Order_Created::handle_order_status_changed' );
        
$this->handle_order_status_changed$post->ID$old_status$new_statuswc_get_order$post->ID ) );
    }

    
/**
     * An order was created.
     *
     * @param int|string $order_id
     * @param WC_Order   $order
     */
    
public function enqueue_maybe_order_created_async_event$order_id$order null ) {
        
$order_id Clean::id$order_id );
        
$order    $order $order wc_get_order$order_id );
        if ( ! 
$order || Subscription_Workflow_Helper::is_subscription$order_id ) ) {
            return;
        }

        
// Creating a draft order triggers woocommerce_new_order, but we don't want it to trigger this workflow.
        
if ( in_array$order->get_status(), aw_get_draft_order_statuses(), true ) ) {
            return;
        }
        
// Due to the variety of order created hooks, protect against adding multiple events for the same order_id
        
if ( $this->check_item_is_unique_for_event$order_id ) ) {
            return;
        }

        
$this->record_event_added_for_item$order_id );

        
// Enqueue the async action on shutdown to ensure the event doesn't happen before the order is fully created
        
$this->action_scheduler->enqueue_async_action_on_shutdownself::MAYBE_ORDER_CREATED_HOOK, [ $order_id ] );
    }

    
/**
     * Handles async order created event.
     *
     * Prevents duplicate events from running with a meta check.
     *
     * @param int $order_id
     */
    
public function maybe_do_order_created_actionint $order_id ) {
        
$order wc_get_orderClean::id$order_id ) );
        if ( ! 
$order || $order->get_metaself::ORDER_CREATED_META_KEY ) || Subscription_Workflow_Helper::is_subscription$order_id ) ) {
            return;
        }

        
$order->update_meta_dataself::ORDER_CREATED_META_KEYtrue );
        
$order->save();

        
// do real async order created action
        
do_action$this->get_hook_name(), $order_id );
    }
}