Cron   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 35
dl 0
loc 89
rs 10
c 0
b 0
f 0
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A schedule_task() 0 28 6
A cron_schedules() 0 33 3
A __construct() 0 4 1
1
<?php
2
namespace NirjharLo\WP_Plugin_Framework\Lib;
3
4
if ( ! defined( 'ABSPATH' ) ) exit;
5
6
/**
7
 * Add Cron schedules and cron task callback
8
 *
9
 * @author     Nirjhar Lo
10
 * @package    wp-plugin-framework
11
 */
12
if ( ! class_exists( 'Cron' ) ) {
13
14
	final class Cron {
15
16
17
		/**
18
		 * Add corn to wp scheduler
19
		 *
20
		 * @return Void
21
		 */
22
		public function __construct() {
23
24
			//Add cron schedules
25
			add_filter('cron_schedules', array( $this, 'cron_schedules' ) );
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

25
			/** @scrutinizer ignore-call */ 
26
   add_filter('cron_schedules', array( $this, 'cron_schedules' ) );
Loading history...
26
		}
27
28
29
		/**
30
		 * Set up the schedules
31
		 *
32
		 * @return Void
33
		 */
34
		public function cron_schedules( $schedules ) {
35
36
			$prefix = 'prefix_'; // Avoid conflict with other crons. Example Reference: cron_30_mins
37
38
			// Example schedule options
39
			$schedule_options = array(
40
						'24_hrs' => array(
41
								'display' => '24 hours',
42
								'interval' => 86400
43
								),
44
						'48_hrs' => array(
45
								'display' => '48 hours',
46
								'interval' => 172800
47
								),
48
						'72_hrs' => array(
49
								'display' => '72 hours',
50
								'interval' => 259200
51
								)
52
						);
53
54
			/* Add each custom schedule into the cron job system. */
55
			foreach($schedule_options as $schedule_key => $schedule){
56
57
				if(!isset($schedules[$prefix.$schedule_key])) {
58
59
					$schedules[$prefix.$schedule_key] = array(
60
									'interval' => $schedule['interval'],
61
									'display' => __( 'Every '.$schedule['display'] )
0 ignored issues
show
Bug introduced by
The function __ 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

61
									'display' => /** @scrutinizer ignore-call */ __( 'Every '.$schedule['display'] )
Loading history...
62
									);
63
				}
64
			}
65
66
			return $schedules;
67
		}
68
69
70
		/**
71
		 * Schedule the task based on added schedules
72
		 *
73
		 * @return Bool
74
		 */
75
		public function schedule_task($task) {
76
77
			if( ! $task ) {
78
				return false;
79
			}
80
81
			$required_keys = array(
82
						'timestamp',
83
						'recurrence',
84
						'hook'
85
					);
86
			$missing_keys = array();
87
			foreach( $required_keys as $key ){
88
				if( ! array_key_exists( $key, $task ) ) {
89
					$missing_keys[] = $key;
90
				}
91
			}
92
93
			if( ! empty( $missing_keys ) ){
94
				return false;
95
			}
96
97
			if( wp_next_scheduled( $task['hook'] ) ){
0 ignored issues
show
Bug introduced by
The function wp_next_scheduled 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

97
			if( /** @scrutinizer ignore-call */ wp_next_scheduled( $task['hook'] ) ){
Loading history...
98
				wp_clear_scheduled_hook($task['hook']);
0 ignored issues
show
Bug introduced by
The function wp_clear_scheduled_hook 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

98
				/** @scrutinizer ignore-call */ 
99
    wp_clear_scheduled_hook($task['hook']);
Loading history...
99
			}
100
101
			wp_schedule_event($task['timestamp'], $task['recurrence'], $task['hook']);
0 ignored issues
show
Bug introduced by
The function wp_schedule_event 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
			/** @scrutinizer ignore-call */ 
102
   wp_schedule_event($task['timestamp'], $task['recurrence'], $task['hook']);
Loading history...
102
			return true;
103
		}
104
	}
105
} ?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
106