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

namespace AutomateWoo\Triggers;

use 
AutomateWoo\Trigger;
use 
AutomateWoo\Async_Events;
use 
AutomateWoo\Logger;
use 
AutomateWoo\Temporary_Data;
use 
AutomateWoo\Fields\BookingStatus;
use 
AutomateWoo\Proxies\BookingsInterface;
use 
AutomateWoo\Triggers\Utilities\BookingsGroup;
use 
AutomateWoo\Async_Events\BookingStatusChanged as BookingStatusChangedEvent;
use 
AutomateWoo\Triggers\Utilities\BookingDataLayer;

/**
 * @class BookingStatusChanged
 *
 * @since 5.3.0
 */
class BookingStatusChanged extends Trigger {

    use 
BookingsGroup;
    use 
BookingDataLayer;

    
/**
     * @var BookingsInterface Proxy for functionality from WooCommerce Bookings extension.
     */
    
protected $bookings_proxy;

    
/**
     * Async events required by the trigger.
     *
     * @var array|string
     */
    
protected $required_async_events BookingStatusChangedEvent::NAME;

    
/**
     * Constructor
     *
     * @param BookingsInterface $bookings_proxy Bookings proxy class.
     */
    
public function __constructBookingsInterface $bookings_proxy ) {
        
$this->supplied_data_items $this->get_supplied_data_items_for_booking();

        
parent::__construct();

        
$this->bookings_proxy $bookings_proxy;
    }

    
/**
     * Declare our UI metadata.
     */
    
public function load_admin_details() {
        
$this->title       __'Booking Status Changed''automatewoo' );
        
$this->description __(
            
'This trigger fires when a booking status changes. Notice a valid customer is needed to trigger this job.',
            
'automatewoo'
        
);
    }

    
/**
     * Declare our trigger options.
     */
    
public function load_fields() {
        
$from = ( new BookingStatus() )
            ->
set_title__'Status changes from''automatewoo' ) )
            ->
set_name'booking_status_from' )
            ->
set_description__'Select valid previous booking status values to trigger this workflow. Leave blank to allow any previous status. ''automatewoo' ) )
            ->
set_multiple();
        
$this->add_field$from );

        
$to = ( new BookingStatus() )
            ->
set_title__'Status changes to''automatewoo' ) )
            ->
set_name'booking_status_to' )
            ->
set_description__'Select which booking status values will trigger this workflow. Leave blank to allow all.''automatewoo' ) )
            ->
set_multiple();
        
$this->add_field$to );

        
$this->add_field_validate_queued_order_status();
    }

    
/**
     * Register handlers to drive triggers from internal AW async event hook.
     */
    
public function register_hooks() {
        
$async_event Async_Events::getBookingStatusChangedEvent::NAME );
        if ( 
$async_event ) {
            
add_action$async_event->get_hook_name(), [ $this'handle_status_changed' ], 10);
        }
    }

    
/**
     * @param int    $booking_id
     * @param string $old_status
     * @param string $new_status
     */
    
public function handle_status_changedint $booking_idstring $old_statusstring $new_status ) {
        try {
            
$booking    $this->bookings_proxy->get_booking$booking_id );
            
$data_layer $this->generate_booking_data_layer$booking );
        } catch ( 
\Exception $e ) {
            
Logger::notice'bookings'$e->getMessage() );
            return;
        }

        
// Freeze booking status values so we have the trigger-time value when running async.
        
Temporary_Data::set'booking_trigger_from_status'$booking_id$old_status );
        
Temporary_Data::set'booking_trigger_to_status'$booking_id$new_status );

        
$this->maybe_run$data_layer );
    }

    
/**
     * @param \AutomateWoo\Workflow $workflow
     *
     * @return bool
     */
    
public function validate_workflow$workflow ) {
        
$booking             $workflow->data_layer()->get_booking();
        
$allowed_from_statii $workflow->get_trigger_option'booking_status_from' );
        
$allowed_to_statii   $workflow->get_trigger_option'booking_status_to' );

        if ( ! 
$booking ) {
            return 
false;
        }

        
// Defrost saved status data from when trigger fired.
        
$from_status Temporary_Data::get'booking_trigger_from_status'$booking->get_id() );
        
$to_status   Temporary_Data::get'booking_trigger_to_status'$booking->get_id() );

        if ( ! 
$this->validate_status_field$allowed_from_statii$from_status ) ) {
            return 
false;
        }

        if ( ! 
$this->validate_status_field$allowed_to_statii$to_status ) ) {
            return 
false;
        }

        return 
true;
    }

    
/**
     * Ensures 'to' status has not changed while sitting in queue in case
     * `validate_order_status_before_queued_run` is checked in Trigger the UI.
     *
     * @param \AutomateWoo\Workflow $workflow The workflow to validate
     * @return bool True if it's valid
     */
    
public function validate_before_queued_event$workflow ) {

        if ( ! 
$workflow ) {
            return 
false;
        }

        if ( 
$workflow->get_trigger_option'validate_order_status_before_queued_run' ) ) {
            
$status_to $workflow->get_trigger_option'booking_status_to' );
            
$booking   $workflow->data_layer()->get_booking();

            if ( ! 
$this->validate_status_field$status_to$booking->get_status() ) ) {
                return 
false;
            }
        }

        return 
true;
    }
}