Test Failed
Push — feature/background-processing ( 959cb0...47ce8e )
by Ravinder
04:25
created

Give_Cron::add_async_event()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
46
	/**
47
	 * Get instance.
48
	 *
49
	 * @since  1.8.13
50
	 * @access public
51
	 * @return static
52
	 */
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
63
	/**
64
	 * Setup
65
	 *
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
	 * Registers new cron schedules
75
	 *
76
	 * @since  1.3.2
77
	 * @access public
78
	 *
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
			'display'  => esc_html__( 'Once Weekly', 'give' ),
88
		);
89
90
		// Cron for background process.
91
		$schedules['async'] = array(
92
			'interval' => - 3600,
93
			'display'  => esc_html__( 'Background Process', 'give' ),
94
		);
95
96
		return $schedules;
97
	}
98
99
	/**
100
	 * Schedules our events
101
	 *
102
	 * @since  1.3.2
103
	 * @access public
104
	 *
105
	 * @return void
106
	 */
107
	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...
108
		$this->weekly_events();
109
		$this->daily_events();
110
		$this->async_events();
111
	}
112
113
	/**
114
	 * Schedule weekly events
115
	 *
116
	 * @since  1.3.2
117
	 * @access private
118
	 *
119
	 * @return void
120
	 */
121
	private function weekly_events() {
122
		if ( ! wp_next_scheduled( 'give_weekly_scheduled_events' ) ) {
123
			wp_schedule_event( current_time( 'timestamp' ), 'weekly', 'give_weekly_scheduled_events' );
124
		}
125
	}
126
127
	/**
128
	 * Schedule daily events
129
	 *
130
	 * @since  1.3.2
131
	 * @access private
132
	 *
133
	 * @return void
134
	 */
135
	private function daily_events() {
136
		if ( ! wp_next_scheduled( 'give_daily_scheduled_events' ) ) {
137
			wp_schedule_event( current_time( 'timestamp' ), 'daily', 'give_daily_scheduled_events' );
138
		}
139
	}
140
141
	/**
142
	 * Schedule async events
143
	 *
144
	 * @since  1.8.13
145
	 * @access private
146
	 *
147
	 * @return void
148
	 */
149
	private function async_events() {
150
		if ( ! wp_next_scheduled( 'give_async_scheduled_events' ) ) {
151
			wp_schedule_event( current_time( 'timestamp' ), 'async', 'give_async_scheduled_events' );
152
		}
153
	}
154
155
	/**
156
	 * get cron job action name
157
	 *
158
	 * @since  1.8.13
159
	 * @access public
160
	 *
161
	 * @param string $type
162
	 *
163
	 * @return string
164
	 */
165
	public static function get_cron_action( $type = 'weekly' ) {
166
		switch ( $type ) {
167
			case 'daily':
168
				$cron_action = 'give_daily_scheduled_events';
169
				break;
170
171
			case 'async':
172
				$cron_action = 'give_async_scheduled_events';
173
				break;
174
175
			default:
176
				$cron_action = 'give_weekly_scheduled_events';
177
				break;
178
		}
179
180
		return $cron_action;
181
	}
182
183
	/**
184
	 * Add action to cron action
185
	 *
186
	 * @since  1.8.13
187
	 * @access private
188
	 *
189
	 * @param        $action
190
	 * @param string $type
191
	 */
192
	private static function add_event( $action, $type = 'weekly' ) {
193
		$cron_event = self::get_cron_action( $type );
194
		add_action( $cron_event, $action );
195
	}
196
197
	/**
198
	 * Add weekly event
199
	 *
200
	 * @since  1.8.13
201
	 * @access public
202
	 *
203
	 * @param $action
204
	 */
205
	public static function add_weekly_event( $action ) {
206
		self::add_event( $action, 'weekly' );
207
	}
208
209
	/**
210
	 * Add daily event
211
	 *
212
	 * @since  1.8.13
213
	 * @access public
214
	 *
215
	 * @param $action
216
	 */
217
	public static function add_daily_event( $action ) {
218
		self::add_event( $action, 'daily' );
219
	}
220
221
	/**
222
	 * Add async event
223
	 *
224
	 * @since  1.8.13
225
	 * @access public
226
	 *
227
	 * @param $action
228
	 */
229
	public static function add_async_event( $action ) {
230
		self::add_event( $action, 'async' );
231
	}
232
}
233
234
// Initiate class.
235
Give_Cron::get_instance();