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

namespace AutomateWoo;

defined'ABSPATH' ) || exit;

/**
 * @class Trigger_Subscription_Status_Changed
 */
class Trigger_Subscription_Status_Changed extends Trigger {

    
/**
     * Sets supplied data for the trigger.
     *
     * @var array
     */
    
public $supplied_data_items = [ 'subscription''customer' ];

    
/**
     * Async events required by the trigger.
     *
     * @since 4.8.0
     * @var array|string
     */
    
protected $required_async_events 'subscription_status_changed';


    
/**
     * Method to set title, group, description and other admin props.
     */
    
public function load_admin_details() {
        
$this->title       __'Subscription Status Changed''automatewoo' );
        
$this->description __'This trigger fires when a subscription status changes with one exception. When an automatic subscription payment is processed the subscription status is changed to "On-hold" and then immediately back to "Active" if the payment is successful. When this happens this trigger deliberately doesn\'t fire because the status was only changed momentarily while the subscription has essentially remained "Active".''automatewoo' );
        
$this->group       Subscription_Workflow_Helper::get_group_name();
    }


    
/**
     * Registers fields used for this trigger.
     */
    
public function load_fields() {

        
$from = ( new Fields\Subscription_Status() )
            ->
set_title__'Status changes from''automatewoo' ) )
            ->
set_name'subscription_status_from' )
            ->
set_description__'Select which subscription status changes will trigger this workflow. Leave blank for any subscription status.''automatewoo' ) )
            ->
set_multiple();

        
$to = clone $from;

        
$to->set_title__'Status changes to''automatewoo' ) )
            ->
set_name'subscription_status_to' );

        
$recheck_status = ( new Fields\Checkbox() )
            ->
set_name'validate_order_status_before_queued_run' )
            ->
set_title__'Recheck status before run''automatewoo' ) )
            ->
set_description__"This is useful for workflows that are not run immediately as it ensures the status of the subscription hasn't changed since initial trigger."'automatewoo' ) )
            ->
set_default_to_checked();

        
$this->add_field$from );
        
$this->add_field$to );
        
$this->add_fieldSubscription_Workflow_Helper::get_products_field() );
        
$this->add_field$recheck_status );
    }


    
/**
     * Register trigger hooks.
     */
    
public function register_hooks() {
        
add_action$this->get_hook_subscription_status_changed(), [ $this'handle_status_changed' ], 10);
    }


    
/**
     * @param int    $subscription_id
     * @param string $new_status
     * @param string $old_status
     */
    
public function handle_status_changed$subscription_id$new_status$old_status ) {
        
// use temp data to store the real status changed, status of sub may have already changed if using async
        
Temporary_Data::set'subscription_old_status'$subscription_id$old_status );
        
Temporary_Data::set'subscription_new_status'$subscription_id$new_status );
        
Subscription_Workflow_Helper::trigger_for_subscription$this$subscription_id );
    }


    
/**
     * @param Workflow $workflow
     *
     * @return bool
     */
    
public function validate_workflow$workflow ) {
        
$subscription $workflow->data_layer()->get_subscription();
        
$status_from  $workflow->get_trigger_option'subscription_status_from' );
        
$status_to    $workflow->get_trigger_option'subscription_status_to' );

        if ( ! 
$subscription ) {
            return 
false;
        }

        
$old_status Temporary_Data::get'subscription_old_status'$subscription->get_id() );
        
$new_status Temporary_Data::get'subscription_new_status'$subscription->get_id() );

        if ( ! 
$this->validate_status_field$status_from$old_status ) ) {
            return 
false;
        }

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

        if ( ! 
Subscription_Workflow_Helper::validate_products_field$workflow ) ) {
            return 
false;
        }

        return 
true;
    }


    
/**
     * Ensures 'to' status has not changed while sitting in queue.
     *
     * @param Workflow $workflow
     *
     * @return bool
     */
    
public function validate_before_queued_event$workflow ) {
        
$subscription $workflow->data_layer()->get_subscription();

        if ( ! 
$subscription ) {
            return 
false;
        }

        
// Option to validate order status
        
if ( $workflow->get_trigger_option'validate_order_status_before_queued_run' ) ) {

            
$status_to $workflow->get_trigger_option'subscription_status_to' );

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

        return 
true;
    }
}