Test Failed
Push — issues/1227 ( 92c1d9 )
by Ravinder
06:04
created

Give_Cron::__delete_async_events()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

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
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
		add_action( 'init', array( self::$instance, '__load_async_events' ) );
72
	}
73
74
75
	/**
76
	 * Load async events
77
	 *
78
	 * @since 1.8.13
79
	 */
80
	public function __load_async_events() {
0 ignored issues
show
Coding Style introduced by
Method name "Give_Cron::__load_async_events" is invalid; only PHP magic methods should be prefixed with a double underscore
Loading history...
81
		$async_events = get_option( 'give_async_events', array() );
82
83
		// Bailout.
84
		if ( empty( $async_events ) ) {
85
			return;
86
		}
87
88
		foreach ( $async_events as $index => $event ) {
89
			// Set cron name.
90
			$cron_name = "give_async_scheduled_events_{$index}";
91
92
			// Setup cron.
93
			wp_schedule_single_event( current_time( 'timestamp', 1 ), $cron_name, $event['params'] );
94
95
			// Add cron action.
96
			add_action( $cron_name, $event['callback'], 10, count( $event['params'] ) );
97
			add_action( $cron_name, array( $this, '__delete_async_events' ), 9999, 0 );
98
		}
99
	}
100
101
	/**
102
	 * Delete async cron info after run
103
	 *
104
	 * @since 1.8.13
105
	 */
106
	public function __delete_async_events() {
0 ignored issues
show
Coding Style introduced by
Method name "Give_Cron::__delete_async_events" is invalid; only PHP magic methods should be prefixed with a double underscore
Loading history...
107
		$async_events    = get_option( 'give_async_events', array() );
108
		$cron_name_parts = explode( '_', current_action() );
109
		$cron_id         = end( $cron_name_parts );
110
111
		if ( ! empty( $async_events[ $cron_id ] ) ) {
112
			unset( $async_events[ $cron_id ] );
113
			update_option( 'give_async_events', $async_events );
114
		}
115
	}
116
117
	/**
118
	 * Registers new cron schedules
119
	 *
120
	 * @since  1.3.2
121
	 * @access public
122
	 *
123
	 * @param  array $schedules An array of non-default cron schedules.
124
	 *
125
	 * @return array            An array of non-default cron schedules.
126
	 */
127
	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...
128
		// Adds once weekly to the existing schedules.
129
		$schedules['weekly'] = array(
130
			'interval' => 604800,
131
			'display'  => esc_html__( 'Once Weekly', 'give' ),
132
		);
133
134
		return $schedules;
135
	}
136
137
	/**
138
	 * Schedules our events
139
	 *
140
	 * @since  1.3.2
141
	 * @access public
142
	 *
143
	 * @return void
144
	 */
145
	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...
146
		$this->weekly_events();
147
		$this->daily_events();
148
	}
149
150
	/**
151
	 * Schedule weekly events
152
	 *
153
	 * @since  1.3.2
154
	 * @access private
155
	 *
156
	 * @return void
157
	 */
158
	private function weekly_events() {
159
		if ( ! wp_next_scheduled( 'give_weekly_scheduled_events' ) ) {
160
			wp_schedule_event( current_time( 'timestamp' ), 'weekly', 'give_weekly_scheduled_events' );
161
		}
162
	}
163
164
	/**
165
	 * Schedule daily events
166
	 *
167
	 * @since  1.3.2
168
	 * @access private
169
	 *
170
	 * @return void
171
	 */
172
	private function daily_events() {
173
		if ( ! wp_next_scheduled( 'give_daily_scheduled_events' ) ) {
174
			wp_schedule_event( current_time( 'timestamp' ), 'daily', 'give_daily_scheduled_events' );
175
		}
176
	}
177
178
	/**
179
	 * get cron job action name
180
	 *
181
	 * @since  1.8.13
182
	 * @access public
183
	 *
184
	 * @param string $type
185
	 *
186
	 * @return string
187
	 */
188
	public static function get_cron_action( $type = 'weekly' ) {
189
		switch ( $type ) {
190
			case 'daily':
191
				$cron_action = 'give_daily_scheduled_events';
192
				break;
193
194
			case 'async':
195
				$cron_action = 'give_async_scheduled_events';
196
				break;
197
198
			default:
199
				$cron_action = 'give_weekly_scheduled_events';
200
				break;
201
		}
202
203
		return $cron_action;
204
	}
205
206
	/**
207
	 * Add action to cron action
208
	 *
209
	 * @since  1.8.13
210
	 * @access private
211
	 *
212
	 * @param        $action
213
	 * @param string $type
214
	 */
215
	private static function add_event( $action, $type = 'weekly' ) {
216
		$cron_event = self::get_cron_action( $type );
217
		add_action( $cron_event, $action );
218
	}
219
220
	/**
221
	 * Add weekly event
222
	 *
223
	 * @since  1.8.13
224
	 * @access public
225
	 *
226
	 * @param $action
227
	 */
228
	public static function add_weekly_event( $action ) {
229
		self::add_event( $action, 'weekly' );
230
	}
231
232
	/**
233
	 * Add daily event
234
	 *
235
	 * @since  1.8.13
236
	 * @access public
237
	 *
238
	 * @param $action
239
	 */
240
	public static function add_daily_event( $action ) {
241
		self::add_event( $action, 'daily' );
242
	}
243
244
	/**
245
	 * Add async event
246
	 *
247
	 * @since  1.8.13
248
	 * @access public
249
	 *
250
	 * @param string $action
251
	 * @param array  $args
252
	 */
253
	public static function add_async_event( $action, $args = array() ) {
254
255
		// Cache async events.
256
		$async_events             = get_option( 'give_async_events', array() );
257
		$async_events[ uniqid() ] = array(
258
			'callback' => $action,
259
			'params'   => $args,
260
		);
261
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
262
263
		update_option( 'give_async_events', $async_events );
264
	}
265
}
266
267
// Initiate class.
268
Give_Cron::get_instance();