Test Failed
Pull Request — master (#2668)
by Devin
07:48
created

Give_Cron::__delete_async_events()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 0
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 0
cp 0
crap 6

1 Method

Rating   Name   Duplication   Size   Complexity  
A Give_Cron::weekly_events() 0 5 2
1
<?php
2
/**
3
 * Cron
4
 *
5
 * @package     Give
6
 * @subpackage  Classes/Give_Cron
7
 * @copyright   Copyright (c) 2016, WordImpress
8
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
9
 * @since       1.3.2
10
 */
11
12
// Exit if accessed directly.
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
18
/**
19
 * Give_Cron Class
20
 *
21
 * This class handles scheduled events.
22
 *
23
 * @since 1.3.2
24
 */
25
class Give_Cron {
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
26
27
	/**
28
	 * Instance.
29
	 *
30
	 * @since  1.8.13
31
	 * @access private
32
	 * @var
33
	 */
34
	private static $instance;
35
36
	/**
37
	 * Singleton pattern.
38
	 *
39
	 * @since  1.8.13
40
	 * @access private
41
	 */
42
	private function __construct() {
43
	}
44
45 3
46
	/**
47 3
	 * Get instance.
48 3
	 *
49 3
	 * @since  1.8.13
50 3
	 * @access public
51
	 * @return static
52 3
	 */
53
	public static function get_instance() {
54
		if ( null === static::$instance ) {
55
			self::$instance = new static();
56
			self::$instance->setup();
57
		}
58
59
		return self::$instance;
60
	}
61
62 3
63 3
	/**
64 3
	 * Setup
65 3
	 *
66
	 * @since 1.8.13
67
	 */
68
	private function setup() {
69
		add_filter( 'cron_schedules', array( self::$instance, '__add_schedules' ) );
70
		add_action( 'wp', array( self::$instance, '__schedule_events' ) );
71
	}
72
73
	/**
74 3
	 * Registers new cron schedules
75 3
	 *
76 3
	 * @since  1.3.2
77 3
	 * @access public
78 3
	 *
79
	 * @param  array $schedules An array of non-default cron schedules.
80
	 *
81
	 * @return array            An array of non-default cron schedules.
82
	 */
83
	public function __add_schedules( $schedules = array() ) {
0 ignored issues
show
Coding Style introduced by
Method name "Give_Cron::__add_schedules" is invalid; only PHP magic methods should be prefixed with a double underscore
Loading history...
84
		// Adds once weekly to the existing schedules.
85
		$schedules['weekly'] = array(
86
			'interval' => 604800,
87 3
			'display'  => __( 'Once Weekly', 'give' ),
88 3
		);
89 3
90 3
		return $schedules;
91 3
	}
92
93
	/**
94
	 * Schedules our events
95
	 *
96
	 * @since  1.3.2
97
	 * @access public
98
	 *
99
	 * @return void
100
	 */
101
	public function __schedule_events() {
0 ignored issues
show
Coding Style introduced by
Method name "Give_Cron::__schedule_events" is invalid; only PHP magic methods should be prefixed with a double underscore
Loading history...
102
		$this->weekly_events();
103
		$this->daily_events();
104
	}
105
106
	/**
107
	 * Schedule weekly events
108
	 *
109
	 * @since  1.3.2
110
	 * @access private
111
	 *
112
	 * @return void
113
	 */
114
	private function weekly_events() {
115
		if ( ! wp_next_scheduled( 'give_weekly_scheduled_events' ) ) {
116
			wp_schedule_event( current_time( 'timestamp' ), 'weekly', 'give_weekly_scheduled_events' );
117
		}
118
	}
119
120
	/**
121
	 * Schedule daily events
122
	 *
123
	 * @since  1.3.2
124
	 * @access private
125
	 *
126
	 * @return void
127
	 */
128
	private function daily_events() {
129
		if ( ! wp_next_scheduled( 'give_daily_scheduled_events' ) ) {
130
			wp_schedule_event( current_time( 'timestamp' ), 'daily', 'give_daily_scheduled_events' );
131
		}
132
	}
133
134
	/**
135
	 * get cron job action name
136
	 *
137
	 * @since  1.8.13
138
	 * @access public
139
	 *
140
	 * @param string $type
141
	 *
142
	 * @return string
143
	 */
144
	public static function get_cron_action( $type = 'weekly' ) {
145
		switch ( $type ) {
146
			case 'daily':
147
				$cron_action = 'give_daily_scheduled_events';
148
				break;
149
150
			default:
151
				$cron_action = 'give_weekly_scheduled_events';
152
				break;
153
		}
154
155
		return $cron_action;
156
	}
157
158
	/**
159
	 * Add action to cron action
160
	 *
161
	 * @since  1.8.13
162
	 * @access private
163
	 *
164
	 * @param        $action
165
	 * @param string $type
166
	 */
167
	private static function add_event( $action, $type = 'weekly' ) {
168
		$cron_event = self::get_cron_action( $type );
169
		add_action( $cron_event, $action );
170
	}
171
172
	/**
173
	 * Add weekly event
174
	 *
175
	 * @since  1.8.13
176
	 * @access public
177
	 *
178
	 * @param $action
179
	 */
180
	public static function add_weekly_event( $action ) {
181
		self::add_event( $action, 'weekly' );
182
	}
183
184
	/**
185
	 * Add daily event
186
	 *
187
	 * @since  1.8.13
188
	 * @access public
189
	 *
190
	 * @param $action
191
	 */
192
	public static function add_daily_event( $action ) {
193
		self::add_event( $action, 'daily' );
194
	}
195
}
196
197
// Initiate class.
198
Give_Cron::get_instance();