Passed
Push — master ( 46e064...13ee52 )
by Brian
04:28
created

GetPaid_Daily_Maintenance   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 27
c 1
b 0
f 0
dl 0
loc 89
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A backwards_compat() 0 2 1
A maybe_expire_subscriptions() 0 19 3
A maybe_clear_deprecated_events() 0 6 2
A maybe_create_scheduled_event() 0 5 2
A log_cron_run() 0 2 1
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, 'log_cron_run' ) );
28
		add_action( 'getpaid_daily_maintenance', array( $this, 'backwards_compat' ) );
29
		add_action( 'getpaid_daily_maintenance', array( $this, 'maybe_expire_subscriptions' ) );
30
31
	}
32
33
	/**
34
	 * Schedules a cron to run every day at 7 a.m
35
	 *
36
	 */
37
	public function maybe_create_scheduled_event() {
38
39
		if ( ! wp_next_scheduled( 'getpaid_daily_maintenance' ) ) {
40
			$timestamp = strtotime( 'tomorrow 07:00:00', current_time( 'timestamp' ) );
41
			wp_schedule_event( $timestamp, 'daily', 'getpaid_daily_maintenance' );
42
		}
43
44
	}
45
46
	/**
47
	 * Clears deprecated events.
48
	 *
49
	 */
50
	public function maybe_clear_deprecated_events() {
51
52
		if ( ! get_option( 'wpinv_cleared_old_events' ) ) {
53
			wp_clear_scheduled_hook( 'wpinv_register_schedule_event_twicedaily' );
54
			wp_clear_scheduled_hook( 'wpinv_register_schedule_event_daily' );
55
			update_option( 'wpinv_cleared_old_events', 1 );
56
		}
57
58
	}
59
60
	/**
61
	 * Fires the old hook for backwards compatibility.
62
	 *
63
	 */
64
	public function backwards_compat() {
65
		do_action( 'wpinv_register_schedule_event_daily' );
66
	}
67
68
	/**
69
	 * Expires expired subscriptions.
70
	 *
71
	 */
72
	public function maybe_expire_subscriptions() {
73
74
		// Fetch expired subscriptions (skips those that expire today).
75
		$args  = array(
76
			'number'             => -1,
77
			'count_total'        => false,
78
			'status'             => 'trialling active failing cancelled',
79
			'date_expires_query' => array(
80
				'before'    => 'today',
81
				'inclusive' => false,
82
			),
83
		);
84
85
		$subscriptions = new GetPaid_Subscriptions_Query( $args );
86
87
		foreach ( $subscriptions as $subscription ) {
88
			if ( apply_filters( 'getpaid_daily_maintenance_should_expire_subscription', true, $subscription ) ) {
89
				$subscription->set_status( 'expired' );
90
				$subscription->save();
91
			}
92
		}
93
94
	}
95
96
	/**
97
	 * Logs cron runs.
98
	 *
99
	 */
100
	public function log_cron_run() {
101
		wpinv_error_log( "GetPaid Daily Cron" );
102
	}
103
104
}
105