| 1 |
<?php |
| 2 |
/** |
| 3 |
* Cron |
| 4 |
* |
| 5 |
* @package Give |
| 6 |
* @subpackage Classes/Give_Cron |
| 7 |
* @copyright Copyright (c) 2016, GiveWP |
| 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 { |
| 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 |
* @return static |
| 50 |
* @since 1.8.13 |
| 51 |
* @access public |
| 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 4.9.0 rename function - PHP 8 compatibility |
| 67 |
* @since 1.8.13 |
| 68 |
*/ |
| 69 |
private function setup() { |
| 70 |
add_filter( 'cron_schedules', array( self::$instance, 'add_schedules' ) ); |
| 71 |
add_action( 'wp', array( self::$instance, 'schedule_events' ) ); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Registers new cron schedules |
| 76 |
* |
| 77 |
* @param array $schedules An array of non-default cron schedules. |
| 78 |
* |
| 79 |
* @return array An array of non-default cron schedules. |
| 80 |
* |
| 81 |
* @since 4.9.0 rename function - PHP 8 compatibility |
| 82 |
* @since 1.3.2 |
| 83 |
* @access public |
| 84 |
*/ |
| 85 |
public function add_schedules( $schedules = array() ) { |
| 86 |
// Adds once weekly to the existing schedules. |
| 87 |
$schedules['weekly'] = array( |
| 88 |
'interval' => 604800, // 7 * 24 * 3600 |
| 89 |
'display' => __( 'Once Weekly', 'give' ), |
| 90 |
); |
| 91 |
|
| 92 |
// Adds once weekly to the existing schedules. |
| 93 |
$schedules['monthly'] = array( |
| 94 |
'interval' => 2592000, // 30 * 24 * 3600 |
| 95 |
'display' => __( 'Once Monthly', 'give' ), |
| 96 |
); |
| 97 |
|
| 98 |
// Adds every third day to the existing schedules. |
| 99 |
$schedules['thricely'] = array( |
| 100 |
'interval' => 259200, // 3 * 24 * 3600 |
| 101 |
'display' => __( 'Every Third Day', 'give' ), |
| 102 |
); |
| 103 |
|
| 104 |
return $schedules; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Schedules our events |
| 109 |
* |
| 110 |
* @return void |
| 111 |
* |
| 112 |
* @since 4.9.0 rename function - PHP 8 compatibility |
| 113 |
* @since 1.3.2 |
| 114 |
* @access public |
| 115 |
*/ |
| 116 |
public function schedule_events() { |
| 117 |
$this->monthly_events(); |
| 118 |
$this->weekly_events(); |
| 119 |
$this->daily_events(); |
| 120 |
$this->thricely_events(); |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Schedule monthly events |
| 125 |
* |
| 126 |
* @return void |
| 127 |
* @since 2.5.0 |
| 128 |
* @access private |
| 129 |
*/ |
| 130 |
private function monthly_events() { |
| 131 |
if ( ! wp_next_scheduled( 'give_monthly_scheduled_events' ) ) { |
| 132 |
wp_schedule_event( current_time( 'timestamp' ), 'monthly', 'give_monthly_scheduled_events' ); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Schedule weekly events |
| 138 |
* |
| 139 |
* @return void |
| 140 |
* @since 1.3.2 |
| 141 |
* @access private |
| 142 |
*/ |
| 143 |
private function weekly_events() { |
| 144 |
if ( ! wp_next_scheduled( 'give_weekly_scheduled_events' ) ) { |
| 145 |
wp_schedule_event( current_time( 'timestamp' ), 'weekly', 'give_weekly_scheduled_events' ); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Schedule daily events |
| 151 |
* |
| 152 |
* @return void |
| 153 |
* @since 1.3.2 |
| 154 |
* @access private |
| 155 |
*/ |
| 156 |
private function daily_events() { |
| 157 |
if ( ! wp_next_scheduled( 'give_daily_scheduled_events' ) ) { |
| 158 |
wp_schedule_event( current_time( 'timestamp' ), 'daily', 'give_daily_scheduled_events' ); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Schedule thricely events |
| 164 |
* |
| 165 |
* @return void |
| 166 |
* @since 2.5.11 |
| 167 |
* @access private |
| 168 |
*/ |
| 169 |
private function thricely_events() { |
| 170 |
if ( ! wp_next_scheduled( 'give_thricely_scheduled_events' ) ) { |
| 171 |
wp_schedule_event( current_time( 'timestamp' ), 'thricely', 'give_thricely_scheduled_events' ); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* get cron job action name |
| 177 |
* |
| 178 |
* @param string $type |
| 179 |
* |
| 180 |
* @return string |
| 181 |
* @since 1.8.13 |
| 182 |
* @access public |
| 183 |
*/ |
| 184 |
public static function get_cron_action( $type = 'weekly' ) { |
| 185 |
$cron_action = ''; |
| 186 |
|
| 187 |
switch ( $type ) { |
| 188 |
case 'daily': |
| 189 |
$cron_action = 'give_daily_scheduled_events'; |
| 190 |
break; |
| 191 |
|
| 192 |
case 'thricely': |
| 193 |
$cron_action = 'give_thricely_scheduled_events'; |
| 194 |
break; |
| 195 |
|
| 196 |
case 'monthly': |
| 197 |
$cron_action = 'give_monthly_scheduled_events'; |
| 198 |
break; |
| 199 |
|
| 200 |
case 'weekly': |
| 201 |
$cron_action = 'give_weekly_scheduled_events'; |
| 202 |
break; |
| 203 |
} |
| 204 |
|
| 205 |
return $cron_action; |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Add action to cron action |
| 210 |
* |
| 211 |
* @param string $callback |
| 212 |
* @param string $type |
| 213 |
* |
| 214 |
* @since 1.8.13 |
| 215 |
* @access private |
| 216 |
*/ |
| 217 |
private static function add_event( $callback, $type = 'weekly' ) { |
| 218 |
$cron_event = self::get_cron_action( $type ); |
| 219 |
add_action( $cron_event, $callback ); |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* Add weekly event |
| 224 |
* |
| 225 |
* @param string $callback |
| 226 |
* |
| 227 |
* @since 1.8.13 |
| 228 |
* @access public |
| 229 |
*/ |
| 230 |
public static function add_weekly_event( $callback ) { |
| 231 |
self::add_event( $callback ); |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* Add daily event |
| 236 |
* |
| 237 |
* @param $callback |
| 238 |
* |
| 239 |
* @since 1.8.13 |
| 240 |
* @access public |
| 241 |
*/ |
| 242 |
public static function add_daily_event( $callback ) { |
| 243 |
self::add_event( $callback, 'daily' ); |
| 244 |
} |
| 245 |
|
| 246 |
/** |
| 247 |
* Add thricely event |
| 248 |
* |
| 249 |
* @param $callback |
| 250 |
* |
| 251 |
* @since 2.5.11 |
| 252 |
* @access public |
| 253 |
*/ |
| 254 |
public static function add_thricely_event( $callback ) { |
| 255 |
self::add_event( $callback, 'thricely' ); |
| 256 |
} |
| 257 |
|
| 258 |
/** |
| 259 |
* Add monthly event |
| 260 |
* |
| 261 |
* @param $callback |
| 262 |
* |
| 263 |
* @since 2.5.0 |
| 264 |
* @access public |
| 265 |
*/ |
| 266 |
public static function add_monthly_event( $callback ) { |
| 267 |
self::add_event( $callback, 'monthly' ); |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
// Initiate class. |
| 272 |
Give_Cron::get_instance(); |
| 273 |
|