Passed
Pull Request — master (#414)
by Brian
06:04
created

GetPaid_Daily_Maintenance   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 26
c 1
b 0
f 0
dl 0
loc 78
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A backwards_compat() 0 2 1
A maybe_expire_subscriptions() 0 18 2
A maybe_clear_deprecated_events() 0 6 2
A maybe_create_scheduled_event() 0 5 2
1
<?php
2
/**
3
 * Daily maintenance class.
4
 *
5
 */
6
7
defined( 'ABSPATH' ) || exit;
8
9
/**
10
 * Daily maintenance class.
11
 *
12
 */
13
class GetPaid_Daily_Maintenance {
14
15
    /**
16
	 * Class constructor.
17
	 */
18
    public function __construct(){
19
20
        // Clear deprecated events.
21
        add_action( 'wp', array( $this, 'maybe_clear_deprecated_events' ) );
22
23
        // (Maybe) schedule a cron that runs daily.
24
        add_action( 'wp', array( $this, 'maybe_create_scheduled_event' ) );
25
26
        // Fired everyday at 7 a.m (this might vary for sites with few visitors)
27
        add_action( 'getpaid_daily_maintenance', array( $this, 'backwards_compat' ) );
28
        add_action( 'getpaid_daily_maintenance', array( $this, 'maybe_expire_subscriptions' ) );
29
        add_action( 'getpaid_daily_maintenance', array( $this, 'maybe_send_upcoming_renewal_reminders' ) );
30
        add_action( 'getpaid_daily_maintenance', array( $this, 'maybe_send_overdue_notices' ) );
31
32
    }
33
34
    /**
35
	 * Schedules a cron to run every day at 7 a.m
36
     *
37
	 */
38
    public function maybe_create_scheduled_event() {
39
40
        if ( ! wp_next_scheduled( 'getpaid_daily_maintenance' ) ) {
41
            $timestamp = strtotime( 'tomorrow 07:00:00', current_time( 'timestamp' ) );
42
            wp_schedule_event( $timestamp, 'daily', 'getpaid_daily_maintenance' );
43
        }
44
45
    }
46
47
    /**
48
	 * Clears deprecated events.
49
     *
50
	 */
51
    public function maybe_clear_deprecated_events() {
52
53
        if ( ! get_option( 'wpinv_cleared_old_events' ) ) {
54
            wp_clear_scheduled_hook( 'wpinv_register_schedule_event_twicedaily' );
55
            wp_clear_scheduled_hook( 'wpinv_register_schedule_event_daily' );
56
            update_option( 'wpinv_cleared_old_events', 1 );
57
        }
58
        
59
    }
60
61
    /**
62
	 * Fires the old hook for backwards compatibility.
63
     *
64
	 */
65
    public function backwards_compat() {
66
        do_action( 'wpinv_register_schedule_event_daily' );
67
    }
68
69
    /**
70
	 * Expires expired subscriptions.
71
     *
72
	 */
73
    public function maybe_expire_subscriptions() {
74
75
        // Fetch expired subscriptions (skips those that expire today).
76
        $args  = array(
77
            'number'             => -1,
78
			'count_total'        => false,
79
			'status'             => 'trialling active failing cancelled',
80
            'date_expires_query' => array(
81
                'before'    => 'today',
82
                'inclusive' => true,
83
            ),
84
        );
85
86
        $subscriptions = new GetPaid_Subscriptions_Query( $args );
87
88
        foreach ( $subscriptions as $subscription ) {
89
            $subscription->set_status( 'expired' );
90
            $subscription->save();
91
        }
92
93
    }
94
95
}
96