PluginProbe
Datafeedr API / 1.4.2
Datafeedr API v1.4.2
1.4.2 1.0.125 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.2 1.0.20 1.0.21 1.0.22 1.0.23 1.0.24 1.0.25 1.0.26 1.0.27 1.0.28 1.0.29 1.0.3 1.0.30 1.0.31 1.0.32 1.0.33 All 181 releases
datafeedr-api / classes / class-datafeedr-cron.php

class-datafeedr-cron.php in Datafeedr API 1.4.2, at classes/class-datafeedr-cron.php

161 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Exit if accessed directly
5 */
6 if ( ! defined( 'ABSPATH' ) ) {
7 exit;
8 }
9
10 if ( ! class_exists( 'Dfrapi_Cron' ) ) {
11
12 class Dfrapi_Cron {
13
14 /** @var string $hook A name for this cron. */
15 public $hook;
16
17 /** @var int $interval How often to run this cron in seconds. */
18 public $interval;
19
20 /** @var Closure|string|null $callback Optional. Anonymous function, function name or null to override with your own handle() method. */
21 public $callback;
22
23 /** @var array $args Optional. An array of arguments to pass into the callback. */
24 public $args;
25
26 /** @var string $recurrence How often the event should subsequently recur. See wp_get_schedules(). */
27 public $recurrence;
28
29 /**
30 * Dfrapi_Cron constructor.
31 *
32 * @param string $hook
33 * @param integer $interval
34 * @param null $callback
35 * @param array $args
36 */
37 private function __construct( $hook, $interval, $callback = null, $args = [] ) {
38 $this->hook = trim( $hook );
39 $this->interval = absint( $interval );
40 $this->callback = $callback;
41 $this->args = $args;
42 $this->set_recurrence();
43 $this->schedule_event();
44
45 add_filter( 'cron_schedules', [ $this, 'add_schedule' ] );
46 add_action( $this->hook, [ $this, 'handle' ] );
47 }
48
49 /**
50 * Return instance of class.
51 *
52 * @param string $hook
53 * @param integer $interval
54 * @param null $callback
55 * @param array $args
56 *
57 * @return static
58 */
59 public static function init( $hook, $interval, $callback = null, $args = [] ) {
60 return new static( $hook, $interval, $callback, $args );
61 }
62
63 /**
64 * Handles the execution.
65 *
66 * IMPORTANT: If you are extending the Dfrapi_Cron class using inheritance, then you MUST
67 * include a handle() method in your child class.
68 */
69 public function handle() {
70 if ( is_callable( $this->callback ) ) {
71 call_user_func_array( $this->callback, $this->args );
72 }
73 }
74
75 /**
76 * Schedule the event if it's not already scheduled.
77 */
78 public function schedule_event() {
79 if ( ! wp_next_scheduled( $this->hook, $this->args ) ) {
80 wp_schedule_event( time(), $this->recurrence, $this->hook, $this->args );
81 }
82 }
83
84 /**
85 * Adds the new recurrence to the array of $schedules.
86 *
87 * @param array $schedules
88 *
89 * @return array
90 */
91 public function add_schedule( $schedules ) {
92 if ( in_array( $this->recurrence, $this->default_wp_recurrences() ) ) {
93 return $schedules;
94 }
95
96 $schedules[ $this->recurrence ] = [
97 'interval' => $this->interval,
98 'display' => __( 'Every ' . $this->interval . ' seconds', 'datafeedr-api' ),
99 ];
100
101 return $schedules;
102 }
103
104 /**
105 * Sets the recurrence for this cron job. This will either return a default
106 * WordPress recurrence such as 'hourly' or 'twicedaily' or create its own
107 * recurrence such as 'dfrapi_every_7200_seconds' or 'dfrapi_every_43200_seconds'.
108 */
109 private function set_recurrence() {
110 foreach ( $this->default_wp_schedules() as $recurrence => $schedule ) {
111 if ( $this->interval === absint( $schedule['interval'] ) ) {
112 $this->recurrence = $recurrence;
113
114 return;
115 }
116 }
117
118 $this->recurrence = 'dfrapi_every_' . $this->interval . '_seconds';
119 }
120
121 /**
122 * Returns an array of default WordPress schedules. As of 2021-01-14, those are:
123 *
124 * Array (
125 * [hourly] => Array (
126 * [interval] => 3600
127 * [display] => Once Hourly
128 * )
129 * [twicedaily] => Array (
130 * [interval] => 43200
131 * [display] => Twice Daily
132 * )
133 * [daily] => Array (
134 * [interval] => 86400
135 * [display] => Once Daily
136 * )
137 * [weekly] => Array (
138 * [interval] => 604800
139 * [display] => Once Weekly
140 * )
141 * )
142 *
143 * @return array
144 */
145 private function default_wp_schedules() {
146 return array_filter( wp_get_schedules(), function ( $schedule ) {
147 return in_array( $schedule, $this->default_wp_recurrences() );
148 }, ARRAY_FILTER_USE_KEY );
149 }
150
151 /**
152 * Returns an array of default WordPress recurrences as listed in the wp_get_schedules() function.
153 *
154 * @return string[]
155 */
156 private function default_wp_recurrences() {
157 return [ 'hourly', 'twicedaily', 'daily', 'weekly' ];
158 }
159 }
160 }
161