Object_Sync_Sf_Queue::schedule_recurring()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 2
rs 10
cc 1
nc 1
nop 5
1
<?php
2
/**
3
 * Manage the task queue.
4
 *
5
 * @class   Object_Sync_Sf_Queue
6
 * @package Object_Sync_Salesforce
7
 */
8
9
defined( 'ABSPATH' ) || exit;
10
11
/**
12
 * Object_Sync_Sf_Queue class.
13
 */
14
class Object_Sync_Sf_Queue {
15
16
	/**
17
	 * Current version of the plugin
18
	 *
19
	 * @var string
20
	 */
21
	public $version;
22
23
	/**
24
	 * The main plugin file
25
	 *
26
	 * @var string
27
	 */
28
	public $file;
29
30
	/**
31
	 * Global object of `$wpdb`, the WordPress database
32
	 *
33
	 * @var object
34
	 */
35
	public $wpdb;
36
37
	/**
38
	 * The plugin's slug so we can include it when necessary
39
	 *
40
	 * @var string
41
	 */
42
	public $slug;
43
44
	/**
45
	 * The plugin's prefix when saving options to the database
46
	 *
47
	 * @var string
48
	 */
49
	public $option_prefix;
50
51
	/**
52
	 * Array of what classes in the plugin can be scheduled to occur with `wp_cron` events
53
	 *
54
	 * @var array
55
	 */
56
	public $schedulable_classes;
57
58
	/**
59
	 * Constructor for queue class
60
	 */
61
	public function __construct() {
62
		$this->version       = object_sync_for_salesforce()->version;
63
		$this->file          = object_sync_for_salesforce()->file;
64
		$this->wpdb          = object_sync_for_salesforce()->wpdb;
65
		$this->slug          = object_sync_for_salesforce()->slug;
66
		$this->option_prefix = object_sync_for_salesforce()->option_prefix;
67
68
		$this->schedulable_classes = object_sync_for_salesforce()->schedulable_classes;
69
70
		$this->add_actions();
71
	}
72
73
	/**
74
	 * Add actions
75
	 */
76
	private function add_actions() {
77
		add_filter( 'action_scheduler_queue_runner_batch_size', array( $this, 'action_scheduler_batch_size' ) );
0 ignored issues
show
Bug introduced by
The function add_filter was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

77
		/** @scrutinizer ignore-call */ 
78
  add_filter( 'action_scheduler_queue_runner_batch_size', array( $this, 'action_scheduler_batch_size' ) );
Loading history...
78
		add_filter( 'action_scheduler_queue_runner_concurrent_batches', array( $this, 'action_scheduler_concurrent_batches' ) );
79
	}
80
81
	/**
82
	 * Set the batch size.
83
	 *
84
	 * @param int $batch_size how big the batch is.
85
	 * @return int $batch_size
86
	 */
87
	public function action_scheduler_batch_size( $batch_size ) {
0 ignored issues
show
Unused Code introduced by
The parameter $batch_size is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

87
	public function action_scheduler_batch_size( /** @scrutinizer ignore-unused */ $batch_size ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
88
		// default for this library is 20 so that is where we start.
89
		$batch_size = filter_var( get_option( $this->option_prefix . 'action_scheduler_batch_size', 20 ), FILTER_VALIDATE_INT );
0 ignored issues
show
Bug introduced by
The function get_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

89
		$batch_size = filter_var( /** @scrutinizer ignore-call */ get_option( $this->option_prefix . 'action_scheduler_batch_size', 20 ), FILTER_VALIDATE_INT );
Loading history...
90
		return $batch_size;
91
	}
92
93
	/**
94
	 * Set the number of concurrent batches that can run.
95
	 *
96
	 * @param int $concurrent_batches how many batches can run at once.
97
	 * @return int $concurrent_batches
98
	 */
99
	public function action_scheduler_concurrent_batches( $concurrent_batches ) {
0 ignored issues
show
Unused Code introduced by
The parameter $concurrent_batches is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

99
	public function action_scheduler_concurrent_batches( /** @scrutinizer ignore-unused */ $concurrent_batches ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
100
		// default for this library is 5 so that is where we start.
101
		$concurrent_batches = filter_var( get_option( $this->option_prefix . 'action_scheduler_concurrent_batches', 5 ), FILTER_VALIDATE_INT );
0 ignored issues
show
Bug introduced by
The function get_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

101
		$concurrent_batches = filter_var( /** @scrutinizer ignore-call */ get_option( $this->option_prefix . 'action_scheduler_concurrent_batches', 5 ), FILTER_VALIDATE_INT );
Loading history...
102
		return $concurrent_batches;
103
	}
104
105
	/**
106
	 * Get all the schedules with their frequencies, sorted
107
	 *
108
	 * @param string $unit The unit of time.
109
	 * @param string $sort Which direction to sort.
110
	 * @return array $this->schedulable_classes
111
	 */
112
	public function get_frequencies( $unit = 'seconds', $sort = 'asc' ) {
0 ignored issues
show
Unused Code introduced by
The parameter $unit is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

112
	public function get_frequencies( /** @scrutinizer ignore-unused */ $unit = 'seconds', $sort = 'asc' ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
113
114
		foreach ( $this->schedulable_classes as $key => $schedule ) {
115
			$this->schedulable_classes[ $key ]['frequency'] = $this->get_frequency( $key, 'seconds' );
116
		}
117
118
		if ( 'asc' === $sort ) {
119
			uasort(
120
				$this->schedulable_classes,
121
				function ( $a, $b ) {
122
					// we want zero values at the top of an ascending sort.
123
					if ( 0 === $a['frequency'] ) {
124
						return 1;
125
					}
126
					if ( 0 === $b['frequency'] ) {
127
						return -1;
128
					}
129
					return $a['frequency'] - $b['frequency'];
130
				}
131
			);
132
		} else {
133
			uasort(
134
				$this->schedulable_classes,
135
				function( $a, $b ) {
136
					return $b['frequency'] - $a['frequency'];
137
				}
138
			);
139
		}
140
141
		return $this->schedulable_classes;
142
143
	}
144
145
	/**
146
	 * Get a single schedule item's frequency
147
	 *
148
	 * @param string $name The name of the schedule.
149
	 * @param string $unit The unit of time.
150
	 * @return int How often it runs in that unit of time
151
	 */
152
	public function get_frequency( $name, $unit ) {
153
		$schedule_number = filter_var( get_option( $this->option_prefix . $name . '_schedule_number', '' ), FILTER_VALIDATE_INT );
0 ignored issues
show
Bug introduced by
The function get_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

153
		$schedule_number = filter_var( /** @scrutinizer ignore-call */ get_option( $this->option_prefix . $name . '_schedule_number', '' ), FILTER_VALIDATE_INT );
Loading history...
154
		$schedule_unit   = get_option( $this->option_prefix . $name . '_schedule_unit', '' );
155
156
		switch ( $schedule_unit ) {
157
			case 'minutes':
158
				$seconds = 60;
159
				$minutes = 1;
160
				break;
161
			case 'hours':
162
				$seconds = 3600;
163
				$minutes = 60;
164
				break;
165
			case 'days':
166
				$seconds = 86400;
167
				$minutes = 1440;
168
				break;
169
			default:
170
				$seconds = 0;
171
				$minutes = 0;
172
		}
173
174
		$total = $$unit * $schedule_number;
175
176
		return $total;
177
	}
178
179
	/**
180
	 * Enqueue an action to run one time, as soon as possible
181
	 *
182
	 * @param string $hook The hook to trigger.
183
	 * @param array  $args Arguments to pass when the hook triggers.
184
	 * @param string $group The group to assign this job to.
185
	 * @return string The action ID.
186
	 */
187
	public function add( $hook, $args = array(), $group = '' ) {
188
		return $this->schedule_single( time(), $hook, $args, $group );
189
	}
190
191
	/**
192
	 * Schedule an action to run once at some time in the future
193
	 *
194
	 * @param int    $timestamp When the job will run.
195
	 * @param string $hook The hook to trigger.
196
	 * @param array  $args Arguments to pass when the hook triggers.
197
	 * @param string $group The group to assign this job to.
198
	 * @return string The action ID.
199
	 */
200
	public function schedule_single( $timestamp, $hook, $args = array(), $group = '' ) {
201
		return as_schedule_single_action( $timestamp, $hook, $args, $group );
202
	}
203
204
	/**
205
	 * Schedule a recurring action
206
	 *
207
	 * @param int    $timestamp When the first instance of the job will run.
208
	 * @param int    $interval_in_seconds How long to wait between runs.
209
	 * @param string $hook The hook to trigger.
210
	 * @param array  $args Arguments to pass when the hook triggers.
211
	 * @param string $group The group to assign this job to.
212
	 * @return string The action ID.
213
	 */
214
	public function schedule_recurring( $timestamp, $interval_in_seconds, $hook, $args = array(), $group = '' ) {
215
		return as_schedule_recurring_action( $timestamp, $interval_in_seconds, $hook, $args, $group );
216
	}
217
218
	/**
219
	 * Schedule an action that recurs on a cron-like schedule.
220
	 *
221
	 * @param int    $timestamp The schedule will start on or after this time.
222
	 * @param string $cron_schedule A cron-link schedule string.
223
	 * @see http://en.wikipedia.org/wiki/Cron
224
	 *   *    *    *    *    *    *
225
	 *   ┬    ┬    ┬    ┬    ┬    ┬
226
	 *   |    |    |    |    |    |
227
	 *   |    |    |    |    |    + year [optional]
228
	 *   |    |    |    |    +----- day of week (0 - 7) (Sunday=0 or 7)
229
	 *   |    |    |    +---------- month (1 - 12)
230
	 *   |    |    +--------------- day of month (1 - 31)
231
	 *   |    +-------------------- hour (0 - 23)
232
	 *   +------------------------- min (0 - 59)
233
	 * @param string $hook The hook to trigger.
234
	 * @param array  $args Arguments to pass when the hook triggers.
235
	 * @param string $group The group to assign this job to.
236
	 * @return string The action ID
237
	 */
238
	public function schedule_cron( $timestamp, $cron_schedule, $hook, $args = array(), $group = '' ) {
239
		return as_schedule_cron_action( $timestamp, $cron_schedule, $hook, $args, $group );
240
	}
241
242
	/**
243
	 * Dequeue all actions with a matching hook (and optionally matching args and group) so they are not run.
244
	 *
245
	 * Any recurring actions with a matching hook will also be cancelled, not just the next scheduled action.
246
	 *
247
	 * Technically, one action in a recurring or Cron action is scheduled at any one point in time. The next
248
	 * in the sequence is scheduled after the previous one is run, so only the next scheduled action needs to
249
	 * be cancelled/dequeued to stop the sequence.
250
	 *
251
	 * @param string $hook The hook that the job will trigger.
252
	 * @param array  $args Args that would have been passed to the job.
253
	 * @param string $group Group name.
254
	 */
255
	public function cancel( $hook, $args = array(), $group = '' ) {
256
		as_unschedule_action( $hook, $args, $group );
257
	}
258
259
	/**
260
	 * Get the date and time for the next scheduled occurence of an action with a given hook
261
	 * (an optionally that matches certain args and group), if any.
262
	 *
263
	 * @param string $hook Hook name.
264
	 * @param array  $args Arguments.
265
	 * @param string $group Group name.
266
	 * @return timestamp|null The date and time for the next occurrence, or null if there is no pending, scheduled action for the given hook.
0 ignored issues
show
Bug introduced by
The type timestamp was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
267
	 */
268
	public function get_next( $hook, $args = null, $group = '' ) {
269
270
		$next_timestamp = as_next_scheduled_action( $hook, $args, $group );
271
272
		if ( false !== $next_timestamp ) {
273
			return $next_timestamp;
274
		}
275
276
		return null;
277
	}
278
279
	/**
280
	 * Find scheduled actions
281
	 *
282
	 * @param array  $args Possible arguments, with their default values:
283
	 *        'hook' => '' - the name of the action that will be triggered
284
	 *        'args' => null - the args array that will be passed with the action
285
	 *        'date' => null - the scheduled date of the action. Expects a DateTime object, a unix timestamp, or a string that can parsed with strtotime(). Used in UTC timezone.
286
	 *        'date_compare' => '<=' - operator for testing "date". accepted values are '!=', '>', '>=', '<', '<=', '='
287
	 *        'modified' => null - the date the action was last updated. Expects a DateTime object, a unix timestamp, or a string that can parsed with strtotime(). Used in UTC timezone.
288
	 *        'modified_compare' => '<=' - operator for testing "modified". accepted values are '!=', '>', '>=', '<', '<=', '='
289
	 *        'group' => '' - the group the action belongs to
290
	 *        'status' => '' - ActionScheduler_Store::STATUS_COMPLETE or ActionScheduler_Store::STATUS_PENDING
291
	 *        'claimed' => null - TRUE to find claimed actions, FALSE to find unclaimed actions, a string to find a specific claim ID
292
	 *        'per_page' => 5 - Number of results to return
293
	 *        'offset' => 0
294
	 *        'orderby' => 'date' - accepted values are 'hook', 'group', 'modified', or 'date'
295
	 *        'order' => 'ASC'.
296
	 *
297
	 * @param string $return_format OBJECT, ARRAY_A, or ids.
298
	 * @return array
299
	 */
300
	public function search( $args = array(), $return_format = OBJECT ) {
0 ignored issues
show
Bug introduced by
The constant OBJECT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
301
		return as_get_scheduled_actions( $args, $return_format );
302
	}
303
}
304