| 1 |
<?php |
| 2 |
/** |
| 3 |
* @version 1.0 |
| 4 |
* @package Booking Calendar |
| 5 |
* @subpackage CRON |
| 6 |
* @category Execure Recurent Actions |
| 7 |
* |
| 8 |
* @author wpdevelop |
| 9 |
* @link https://wpbookingcalendar.com/ |
| 10 |
* @email info@wpbookingcalendar.com |
| 11 |
* |
| 12 |
* @modified 2014.09.02 |
| 13 |
* @since 5.2.0 |
| 14 |
*/ |
| 15 |
|
| 16 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 17 |
|
| 18 |
|
| 19 |
/** |
| 20 |
* Usage example: |
| 21 |
|
| 22 |
WPBC()->cron->update( 'wpbc_import_gcal' // Name of Cron task |
| 23 |
, array( |
| 24 |
'action' => array( 'wpbc_silent_import_all_events' ) // {REQUIRED} Name of Action Hook and possible parameters. Before you need to add this action: add_bk_action('wpbc_silent_import_all_events' , 'wpbc_silent_import_all_events' ); |
| 25 |
, 'start_time' => time() // Start time of Execution. Default = Now |
| 26 |
, 'recurrence' => 2 // Set time interval in 'time_dimension' (hours | minutes | days ) for repeat of execution. Default = 24 |
| 27 |
, 'time_dimension' => 'h' // 'h' - hours, 'm' - minutes, 'd' - days |
| 28 |
, 'priority' => 10 // Priority of actions execution. Default = 10. Lower priority - execution firstly. |
| 29 |
) |
| 30 |
); |
| 31 |
|
| 32 |
// D E L E T E specific Cron |
| 33 |
WPBC()->cron->delete( 'wpbc_import_gcal' ); // Name of Cron task |
| 34 |
|
| 35 |
*/ |
| 36 |
class WPBC_Cron { |
| 37 |
|
| 38 |
private $actions; |
| 39 |
|
| 40 |
|
| 41 |
function __construct() { |
| 42 |
|
| 43 |
$this->actions = array(); |
| 44 |
|
| 45 |
add_action( 'init', array( $this, 'load' ), 9 ); |
| 46 |
add_bk_action( 'wpbc_other_versions_deactivation', array( &$this, 'deactivate' ) ); |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
public function load(){ |
| 51 |
|
| 52 |
$booking_cron = get_bk_option('booking_cron'); |
| 53 |
|
| 54 |
if ( $booking_cron === false ) { |
| 55 |
$booking_cron = array(); |
| 56 |
} else { |
| 57 |
if ( is_serialized( $booking_cron ) ) { |
| 58 |
$booking_cron = unserialize( $booking_cron ); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
$this->actions = $booking_cron; |
| 63 |
|
| 64 |
if ( ! empty( $this->actions ) ) { |
| 65 |
$this->check(); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
public function check(){ |
| 71 |
|
| 72 |
// Sort by priority of execution |
| 73 |
$priority = array(); |
| 74 |
|
| 75 |
foreach ($this->actions as $action_name => $action) { |
| 76 |
|
| 77 |
if ( ! isset( $priority[ $action['priority'] ] ) ) { |
| 78 |
$priority[ $action['priority'] ] = array(); |
| 79 |
} |
| 80 |
|
| 81 |
$priority[ $action['priority'] ][ $action_name ] = $action; |
| 82 |
} |
| 83 |
|
| 84 |
ksort( $priority ); |
| 85 |
|
| 86 |
// check for execution based on priority |
| 87 |
foreach ( $priority as $actions_list ) { |
| 88 |
|
| 89 |
foreach ( $actions_list as $action_name => $action ) { |
| 90 |
|
| 91 |
//1. Check for start time |
| 92 |
if ( $action['start_time'] > time() ) { |
| 93 |
continue; |
| 94 |
} |
| 95 |
|
| 96 |
if ( $this->get_task__time_in_seconds__of_next_execution__from_now( $action ) > 0 ) { |
| 97 |
continue; // Time is not yet |
| 98 |
} |
| 99 |
|
| 100 |
// We need to run the TASK ! |
| 101 |
|
| 102 |
// Update last_execution time to current time, becase we will run task |
| 103 |
$action['last_execution'] = time(); |
| 104 |
|
| 105 |
// Save changes to CRON |
| 106 |
$this->update( $action_name, $action ); |
| 107 |
|
| 108 |
// Execute |
| 109 |
$this->action( $action_name ); |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
/** |
| 116 |
* Add Paramters |
| 117 |
* |
| 118 |
* @param $action_name |
| 119 |
* @param $action_params |
| 120 |
* |
| 121 |
* @return bool |
| 122 |
* |
| 123 |
* Example: |
| 124 |
* $wpbc_cron->add('wpbc_import', array( |
| 125 |
'action' => array( 'wpbc_silent_import_all_events' ) // Action and parameters |
| 126 |
, 'start_time' => time() // Now |
| 127 |
, 'recurrence' => 1 // each one hour |
| 128 |
)); |
| 129 |
* $wpbc_cron->add('wpbc_import', array( |
| 130 |
'action' => array( 'wpbc_silent_import_all_events' ) // Action and parameters |
| 131 |
, 'start_time' => time() // Now |
| 132 |
, 'recurrence' => 10 // each 10 minutes hour |
| 133 |
, 'time_dimension' => 'm' |
| 134 |
)); |
| 135 |
|
| 136 |
*/ |
| 137 |
public function add($action_name, $action_params) { |
| 138 |
|
| 139 |
if ( ! isset( $this->actions[ $action_name ] ) ) { |
| 140 |
|
| 141 |
if ( ! isset( $action_params['action'] ) ) { |
| 142 |
return false; |
| 143 |
} |
| 144 |
|
| 145 |
$defaults = array( |
| 146 |
'start_time' => ( time() - 1 ), // GMT time, when to start this action - Now |
| 147 |
'recurrence' => 24, // Each 24 hours |
| 148 |
'time_dimension' => 'h', // 'h' - hours, 'm' - minutes, 'd' - days |
| 149 |
'priority' => 10, // Set priority of actions execution at the same time |
| 150 |
'last_execution' => time() // Set last time execution |
| 151 |
); |
| 152 |
$args = wp_parse_args( $action_params, $defaults ); |
| 153 |
|
| 154 |
$this->actions[ $action_name ] = $args; |
| 155 |
|
| 156 |
|
| 157 |
// Update to DB |
| 158 |
$booking_cron = get_bk_option( 'booking_cron' ); |
| 159 |
|
| 160 |
if ( $booking_cron === false ) { |
| 161 |
$booking_cron = array(); |
| 162 |
} else { |
| 163 |
if ( is_serialized( $booking_cron ) ) { |
| 164 |
$booking_cron = unserialize( $booking_cron ); |
| 165 |
} |
| 166 |
} |
| 167 |
$booking_cron[ $action_name ] = $args; |
| 168 |
|
| 169 |
update_bk_option( 'booking_cron', $booking_cron ); |
| 170 |
|
| 171 |
return true; |
| 172 |
} else { |
| 173 |
return false; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
/** |
| 179 |
* Update CRON task |
| 180 |
* |
| 181 |
* @param $action_name |
| 182 |
* @param $action_params |
| 183 |
* |
| 184 |
* @return true|void |
| 185 |
*/ |
| 186 |
public function update( $action_name, $action_params ) { |
| 187 |
|
| 188 |
if ( isset( $this->actions[$action_name] ) ) { |
| 189 |
|
| 190 |
$args = wp_parse_args( $action_params, $this->actions[$action_name] ); |
| 191 |
|
| 192 |
$this->actions[$action_name] = $args; |
| 193 |
|
| 194 |
// Update to DB |
| 195 |
$booking_cron = get_bk_option('booking_cron'); |
| 196 |
|
| 197 |
if ( $booking_cron === false ) |
| 198 |
$booking_cron = array(); |
| 199 |
else { |
| 200 |
if ( is_serialized( $booking_cron ) ) { |
| 201 |
$booking_cron = unserialize( $booking_cron ); |
| 202 |
} |
| 203 |
} |
| 204 |
$booking_cron[ $action_name] = $args; |
| 205 |
|
| 206 |
update_bk_option( 'booking_cron' , $booking_cron ); |
| 207 |
|
| 208 |
return true; |
| 209 |
|
| 210 |
} else { |
| 211 |
$this->add( $action_name, $action_params ); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
public function delete($action_name) { |
| 217 |
|
| 218 |
if ( isset( $this->actions[ $action_name ] ) ) { |
| 219 |
unset( $this->actions[ $action_name ] ); |
| 220 |
} |
| 221 |
|
| 222 |
// Update to DB |
| 223 |
$booking_cron = get_bk_option( 'booking_cron' ); |
| 224 |
|
| 225 |
if ( $booking_cron === false ) { |
| 226 |
$booking_cron = array(); |
| 227 |
} else { |
| 228 |
if ( is_serialized( $booking_cron ) ) { |
| 229 |
$booking_cron = unserialize( $booking_cron ); |
| 230 |
} |
| 231 |
if ( isset( $booking_cron[ $action_name ] ) ) { |
| 232 |
unset( $booking_cron[ $action_name ] ); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
//////////////////////////////////////////////////////////////////// |
| 237 |
|
| 238 |
update_bk_option( 'booking_cron', $booking_cron ); |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
private function action($action_name) { |
| 243 |
|
| 244 |
// Description: |
| 245 |
// |
| 246 |
// This type of execution action add posibility to use not only name of action |
| 247 |
// |
| 248 |
// 'action' => array( 'wpbc_silent_import_all_events' ) // Action and parameters |
| 249 |
// |
| 250 |
// but also parameters |
| 251 |
// |
| 252 |
// 'action' => array( 'wpbc_silent_import_all_events', 2, 'test' ) // Action and parameters |
| 253 |
// |
| 254 |
if ( isset( $this->actions[ $action_name ] ) ) { |
| 255 |
call_user_func_array( 'make_bk_action', $this->actions[ $action_name ]['action'] ); |
| 256 |
} |
| 257 |
|
| 258 |
// Simple Action execution without parameters. |
| 259 |
//make_bk_action( $this->actions[$action_name]['action'][0] ); |
| 260 |
|
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
// Deactivation of the plugin - Delete this option. |
| 265 |
public function deactivate(){ |
| 266 |
delete_bk_option( 'booking_cron' ); |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
/** |
| 272 |
* Get info about time execution |
| 273 |
* |
| 274 |
* @return array [ |
| 275 |
* 'wpbc_import_gcal' => [ |
| 276 |
* 'interval_in_seconds' => 600 |
| 277 |
* 'last_time__run__local' => '2023-09-29 11:35:04' |
| 278 |
* 'next_time__run__local' => '2023-09-29 11:35:04' |
| 279 |
* 'next_run_after_seconds' => 354 |
| 280 |
* ] |
| 281 |
* , ... |
| 282 |
* ] |
| 283 |
* |
| 284 |
* Example: |
| 285 |
* WPBC()->cron->get_active_tasks_info() |
| 286 |
*/ |
| 287 |
public function get_active_tasks_info(){ |
| 288 |
|
| 289 |
$active_tasks = array(); |
| 290 |
|
| 291 |
foreach ( $this->actions as $task_name => $task_params_arr ) { |
| 292 |
$active_tasks[ $task_name ] = array(); |
| 293 |
//$active_tasks[ $task_name ]['priority'] = $task_params_arr['priority']; |
| 294 |
$active_tasks[ $task_name ]['interval_in_seconds'] = $this->get_task__execution_interval_in_seconds( $task_params_arr ); |
| 295 |
|
| 296 |
$last_time__run__timestamp = $task_params_arr['last_execution']; |
| 297 |
$next_time__run__timestamp = $this->get_task__timestamp__of_next_execution( $task_params_arr ); |
| 298 |
//active_tasks[ $task_name ]['last_time__run__gmt'] = gmdate( 'Y-m-d H:i:s', $active_tasks[ $task_name ]['last_time__run__timestamp'] ); |
| 299 |
//$active_tasks[ $task_name ]['next_time__run__gmt'] = gmdate( 'Y-m-d H:i:s', $active_tasks[ $task_name ]['next_time__run__timestamp'] ); |
| 300 |
|
| 301 |
$active_tasks[ $task_name ]['last_time__run__local'] = wp_date( 'Y-m-d H:i:s', $last_time__run__timestamp ); |
| 302 |
$active_tasks[ $task_name ]['next_time__run__local'] = wp_date( 'Y-m-d H:i:s', $next_time__run__timestamp ); |
| 303 |
|
| 304 |
$time_in_seconds__of_next_execution = $this->get_task__time_in_seconds__of_next_execution__from_now( $task_params_arr ); |
| 305 |
$active_tasks[ $task_name ]['next_run_after_seconds'] = ( $time_in_seconds__of_next_execution < 0 ) ? __( 'Now', 'booking' ) . ' ( ' . $time_in_seconds__of_next_execution . ')' : $time_in_seconds__of_next_execution; |
| 306 |
} |
| 307 |
|
| 308 |
return $active_tasks; |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
/** |
| 313 |
* Get execution interval of this task in seconds, e.g. -> 600 (10 minutes) |
| 314 |
* |
| 315 |
* @param $action = array( |
| 316 |
* 'time_dimension' => m |
| 317 |
* 'recurrence' => 10 |
| 318 |
* ) |
| 319 |
* |
| 320 |
* @return float|int 600 (= 10 * 60) |
| 321 |
*/ |
| 322 |
public function get_task__execution_interval_in_seconds( $action ) { |
| 323 |
|
| 324 |
$number_of_seconds = 60 * 60; // Hour by default |
| 325 |
|
| 326 |
if ( ! empty( $action['time_dimension'] ) ) { |
| 327 |
|
| 328 |
switch ( $action['time_dimension'] ) { |
| 329 |
case 'd': |
| 330 |
$number_of_seconds = 24 * 60 * 60; // Days |
| 331 |
break; |
| 332 |
case 'h': |
| 333 |
$number_of_seconds = 60 * 60; // Hours |
| 334 |
break; |
| 335 |
case 'm': |
| 336 |
$number_of_seconds = 60; // Minutes |
| 337 |
break; |
| 338 |
default: |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
$execution_interval_in_seconds = intval( $action['recurrence'] ) * $number_of_seconds; // number of hours | m | d // FixIn: 8.4.5.2. |
| 343 |
|
| 344 |
return $execution_interval_in_seconds; |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
/** |
| 349 |
* Get time (timestamp - time in secodns since 1970) of next execution e.g.: 1695984751 |
| 350 |
* |
| 351 |
* @param $action = array( |
| 352 |
* 'time_dimension' => m |
| 353 |
* 'recurrence' => 10 |
| 354 |
* 'last_execution' => 1695984151 <- timestamp |
| 355 |
* ) |
| 356 |
* |
| 357 |
* @return float|int 1695989151 <- timestamp |
| 358 |
*/ |
| 359 |
public function get_task__timestamp__of_next_execution( $action ){ |
| 360 |
|
| 361 |
$execution_interval_in_seconds = $this->get_task__execution_interval_in_seconds( $action ); |
| 362 |
|
| 363 |
$next_time_execution = intval( $action['last_execution'] ) + $execution_interval_in_seconds; |
| 364 |
|
| 365 |
return $next_time_execution; |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
/** |
| 370 |
* Get time in seconds until next execution. If this value negative, then task must be executed already e.g. 354 |
| 371 |
* |
| 372 |
* @param $action = array( |
| 373 |
* 'time_dimension' => m |
| 374 |
* 'recurrence' => 10 |
| 375 |
* 'last_execution' => 1695984151 <- timestamp |
| 376 |
* ) |
| 377 |
* |
| 378 |
* @return float|int 1695989151 <- timestamp |
| 379 |
*/ |
| 380 |
public function get_task__time_in_seconds__of_next_execution__from_now( $action ){ |
| 381 |
|
| 382 |
$next_time_execution = $this->get_task__timestamp__of_next_execution( $action ); |
| 383 |
|
| 384 |
$time_in_seconds = $next_time_execution - time(); |
| 385 |
|
| 386 |
return $time_in_seconds; |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
/** |
| 391 |
* Get readable time of last task run ( empty string '' if no task) |
| 392 |
* |
| 393 |
* @param string $action_name task name |
| 394 |
* |
| 395 |
* @return string |
| 396 |
* |
| 397 |
* Example: $readable_time_of_next_run = WPBC()->cron->get_readable_time_of_last_run( 'wpbc_import_gcal' ); |
| 398 |
* Output: '2023-09-29 16:27:10' |
| 399 |
*/ |
| 400 |
public function get_readable_time_of_last_run( $action_name ) { |
| 401 |
|
| 402 |
if ( empty( $this->actions[ $action_name ] ) ) { |
| 403 |
return ''; |
| 404 |
} |
| 405 |
|
| 406 |
$cron_info = ''; |
| 407 |
|
| 408 |
$active_tasks_arr = $this->get_active_tasks_info(); |
| 409 |
|
| 410 |
if ( ! empty( $active_tasks_arr[ $action_name ] ) ) { |
| 411 |
$cron_info = $active_tasks_arr[ $action_name ]['last_time__run__local']; |
| 412 |
} |
| 413 |
|
| 414 |
return $cron_info; |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
/** |
| 419 |
* Get readable time of next task run ( empty string '' if no task) |
| 420 |
* |
| 421 |
* @param string $action_name task name |
| 422 |
* @param $after_seconds_prefix_text 'text betweeen '2023-09-29' and '4 minutes 24 seconds' - default '-' |
| 423 |
* |
| 424 |
* @return string |
| 425 |
* |
| 426 |
* Example: $readable_time_of_next_run = WPBC()->cron->get_readable_time_of_next_run( 'wpbc_import_gcal' ); |
| 427 |
* Output: '2023-09-29 16:27:10 - 4 minutes 24 seconds' |
| 428 |
*/ |
| 429 |
public function get_readable_time_of_next_run( $action_name , $after_seconds_prefix_text = '-') { |
| 430 |
|
| 431 |
if ( empty( $this->actions[ $action_name ] ) ) { |
| 432 |
return ''; |
| 433 |
} |
| 434 |
|
| 435 |
$cron_info = ''; |
| 436 |
|
| 437 |
$active_tasks_arr = $this->get_active_tasks_info(); |
| 438 |
|
| 439 |
if ( ! empty( $active_tasks_arr[ $action_name ] ) ) { |
| 440 |
$cron_info .= $active_tasks_arr[ $action_name ]['next_time__run__local']; |
| 441 |
$cron_info .= ' ' . $after_seconds_prefix_text . ' ' . wpbc_get_readable_time_interval( $active_tasks_arr[ $action_name ]['next_run_after_seconds'] ); |
| 442 |
} |
| 443 |
|
| 444 |
return $cron_info; |
| 445 |
} |
| 446 |
|
| 447 |
} |
| 448 |
|