/var/www/html_uk/wp-content/plugins/automatewoo/includes/Subscription_Workflow_Helper.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
293
294
295
296
<?php

namespace AutomateWoo;

defined'ABSPATH' ) || exit;

/**
 * Class Subscription_Workflow_Helper
 *
 * @since 4.5
 *
 * @package AutomateWoo
 */
class Subscription_Workflow_Helper {

    
/**
     * Get the subscription group name.
     *
     * @return string
     */
    
public static function get_group_name() {
        return 
__'Subscriptions''automatewoo' );
    }

    
/**
     * Get the subscription products field.
     *
     * @return Fields\Product
     */
    
public static function get_products_field() {
        
$field = new Fields\Product();
        
$field->set_name'subscription_products' );
        
$field->set_title__'Subscription products''automatewoo' ) );
        
$field->set_description__'Select products here to make this workflow only run on subscriptions with matching products. Leave blank to run for all products.''automatewoo' ) );
        
$field->set_multipletrue );
        
$field->set_allow_variationstrue );

        return 
$field;
    }

    
/**
     * Get the 'active subscriptions only' field.
     *
     * @return Fields\Checkbox
     */
    
public static function get_active_subscriptions_only_field() {
        
$field = new Fields\Checkbox();
        
$field->set_name'active_only' );
        
$field->set_title__'Active subscriptions only''automatewoo' ) );
        
$field->set_description__'Enable to ensure the subscription is still active when the workflow runs. This is useful if the workflow is not run immediately.''automatewoo' ) );
        
$field->default_to_checked true;

        return 
$field;
    }

    
/**
     * Validate the subscription products field for a workflow.
     *
     * @param Workflow $workflow
     *
     * @return bool
     */
    
public static function validate_products_field$workflow ) {
        
$subscription          $workflow->data_layer()->get_subscription();
        
$subscription_products $workflow->get_trigger_option'subscription_products' );

        
// there's no product restriction
        
if ( empty( $subscription_products ) ) {
            return 
true;
        }

        
$included_product_ids = [];

        foreach ( 
$subscription->get_items() as $item ) {
            
$included_product_ids[] = $item->get_product_id();
            
$included_product_ids[] = $item->get_variation_id();
        }

        return (bool) 
array_intersect$included_product_ids$subscription_products );
    }


    
/**
     * Validate the 'active subscriptions only' field.
     *
     * @param Workflow $workflow
     *
     * @return bool
     */
    
public static function validate_active_subscriptions_only_field$workflow ) {
        
$subscription $workflow->data_layer()->get_subscription();

        if ( 
$workflow->get_trigger_option'active_only' ) ) {
            if ( ! 
$subscription->has_status'active' ) ) {
                return 
false;
            }
        }

        return 
true;
    }

    
/**
     * Trigger for subscription.
     *
     * @param Trigger              $trigger
     * @param int|\WC_Subscription $subscription
     */
    
public static function trigger_for_subscription$trigger$subscription ) {
        
$subscription wcs_get_subscription$subscription );

        if ( ! 
$subscription ) {
            return;
        }

        
$trigger->maybe_run(
            [
                
'subscription' => $subscription,
                
'customer'     => Customer_Factory::get_by_user_id$subscription->get_user_id() ),
            ]
        );
    }

    
/**
     * Trigger for each line item in a subscription.
     *
     * @param Trigger              $trigger
     * @param int|\WC_Subscription $subscription
     */
    
public static function trigger_for_each_subscription_line_item$trigger$subscription ) {
        
$subscription wcs_get_subscription$subscription );

        if ( ! 
$subscription ) {
            return;
        }

        
$customer Customer_Factory::get_by_user_id$subscription->get_user_id() );

        foreach ( 
$subscription->get_items() as $order_item_id => $order_item ) {
            
$trigger->maybe_run(
                [
                    
'subscription'      => $subscription,
                    
'customer'          => $customer,
                    
'product'           => $order_item->get_product(),
                    
'subscription_item' => $order_item,
                ]
            );
        }
    }

    
/**
     * Trigger for an order only if it's a subscription order.
     *
     * @since 4.8.0
     *
     * @param Trigger       $trigger
     * @param \WC_Order|int $order
     */
    
public static function trigger_for_subscription_order$trigger$order ) {
        
$order wc_get_order$order );
        if ( ! 
$order ) {
            return;
        }

        
$subscription self::get_subscription_for_order$order );
        if ( ! 
$subscription ) {
            return;
        }

        
$trigger->maybe_run(
            [
                
'order'        => $order,
                
'customer'     => Customer_Factory::get_by_order$order ),
                
'subscription' => $subscription,
            ]
        );
    }

    
/**
     * Get subscription statuses.
     *
     * Excludes the 'wc-switched' status.
     *
     * @since 4.5.0
     *
     * @return array
     */
    
public static function get_subscription_statuses() {
        
$statuses wcs_get_subscription_statuses();
        unset( 
$statuses['wc-switched'] );
        return 
$statuses;
    }

    
/**
     * Get the order's first related subscription.
     *
     * Orders can technically have multiple subscriptions this method returns only the one.
     *
     * @param \WC_Order    $order
     * @param string|array $order_type
     *
     * @return \WC_Subscription|false
     *
     * @since 4.8.0
     */
    
protected static function get_subscription_for_order$order$order_type 'any' ) {
        if ( ! 
wcs_order_contains_subscription$order$order_type ) ) {
            return 
false;
        }

        return 
currentwcs_get_subscriptions_for_order$order, [ 'order_type' => $order_type ] ) );
    }

    
/**
     * Get description for all subscription order triggers.
     *
     * @return string
     *
     * @since 4.8.0
     */
    
public static function get_subscription_order_trigger_description() {
        return 
__'If the order has multiple subscriptions, only the first subscription will be used in the workflow. Subscription parent orders can have multiple subscriptions if the order contains products with different billing periods.''automatewoo' );
    }

    
/**
     * Get list of subscription order types.
     *
     * @return array
     *
     * @since 4.8.0
     */
    
public static function get_subscription_order_types() {
        return [
            
'parent'      => __'Parent''automatewoo' ),
            
'renewal'     => __'Renewal''automatewoo' ),
            
'resubscribe' => __'Resubscribe''automatewoo' ),
            
'switch'      => __'Switch''automatewoo' ),
        ];
    }

    
/**
     * Get subscription order types field.
     *
     * @return Fields\Select
     *
     * @since 4.8.0
     */
    
public static function get_subscription_order_types_field() {
        
$field = new Fields\Select();
        
$field->set_name'order_types' );
        
$field->set_title__'Subscription order types''automatewoo' ) );
        
$field->set_optionsself::get_subscription_order_types() );
        
$field->set_placeholder__'[Any]''automatewoo' ) );
        
$field->set_multipletrue );
        
$field->set_description__'Select which subscription order types this workflow will run for.''automatewoo' ) );
        return 
$field;
    }

    
/**
     * Validate the subscription order types field for a workflow.
     *
     * @param Workflow $workflow
     *
     * @return bool
     *
     * @since 4.8.0
     */
    
public static function validate_subscription_order_types_field$workflow ) {
        
$valid_order_types $workflow->get_trigger_option'order_types' );
        
$order             $workflow->data_layer()->get_order();

        if ( ! 
$order ) {
            return 
false;
        }

        if ( ! 
$valid_order_types ) {
            
$valid_order_types 'any';
        }

        return 
wcs_order_contains_subscription$order$valid_order_types );
    }

    
/**
     * Check if an order is a Subscription
     *
     * @param int $order_id The order ID
     * @return bool True if the order is a subscription
     * @since 5.6.7
     */
    
public static function is_subscriptionint $order_id ) {
        if ( 
Integrations::is_subscriptions_active() && wcs_is_subscription$order_id ) ) {
            return 
true;
        }

        return 
false;
    }
}