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

namespace AutomateWoo;

use 
AutomateWoo\Exceptions\InvalidArgument;

if ( ! 
defined'ABSPATH' ) ) {
    exit;
}

/**
 * @class Trigger_Subscription_Before_End
 */
class Trigger_Subscription_Before_End extends Trigger_Subscription_Before_Renewal {

    
/**
     * Method to set the action's admin props.
     *
     * Admin props include: title, group and description.
     */
    
public function load_admin_details() {
        
$this->title        __'Subscription Before End''automatewoo' );
        
$this->description  __"This trigger runs once per day for any subscriptions that are due to expire/end on the workflow's target date. For example, if set to run 7 days before end, it would look for subscriptions that are due to end on the date exactly 7 days from now."'automatewoo' );
        
$this->description .= ' ' $this->get_description_text_workflow_not_immediate();

        
$this->group Subscription_Workflow_Helper::get_group_name();
    }

    
/**
     * Load fields.
     */
    
public function load_fields() {

        
$days_before = ( new Fields\Positive_Number() )
            ->
set_name'days_before' )
            ->
set_title__'Days before end''automatewoo' ) )
            ->
set_required();

        
$this->add_field$days_before );
        
$this->add_field$this->get_field_time_of_day() );
        
$this->add_fieldSubscription_Workflow_Helper::get_products_field() );
    }

    
/**
     * Get subscriptions that match the workflow's date params.
     *
     * @param Workflow $workflow
     * @param int      $offset
     * @param int      $limit
     *
     * @return int[] Array of subscription IDs.
     *
     * @throws InvalidArgument If workflow 'days before' option is not valid.
     */
    
protected function get_subscriptions_for_workflowWorkflow $workflowint $offsetint $limit ) {
        
$days_before_end = (int) $workflow->get_trigger_option'days_before' );
        
$this->validate_positive_integer$days_before_end );

        
$date = ( new DateTime() )->add( new \DateInterval"P{$days_before_end}D" ) );

        return 
$this->query_subscriptions_for_day(
            
$date,
            
'_schedule_end',
            [ 
'wc-active''wc-pending-cancel' ],
            
$offset,
            
$limit
        
);
    }

    
/**
     * Validate before a queued workflow event.
     *
     * Ensures that the subscription is either active or pending cancellation.
     *
     * @param Workflow $workflow
     *
     * @return bool
     */
    
public function validate_before_queued_event$workflow ) {
        
$subscription $workflow->data_layer()->get_subscription();

        if ( ! 
$subscription || ! $subscription->has_status( [ 'active''pending-cancel' ] ) ) {
            return 
false;
        }

        return 
true;
    }
}