| 1 |
<?php |
| 2 |
/** |
| 3 |
* SureForms events scheduler class. |
| 4 |
* |
| 5 |
* @package sureforms. |
| 6 |
* @since 0.0.2 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace SRFM\Inc; |
| 10 |
|
| 11 |
use SRFM\Inc\Traits\Get_Instance; |
| 12 |
|
| 13 |
/** |
| 14 |
* SureForms events scheduler class. |
| 15 |
* |
| 16 |
* @since 0.0.2 |
| 17 |
*/ |
| 18 |
class Events_Scheduler { |
| 19 |
use Get_Instance; |
| 20 |
|
| 21 |
/** |
| 22 |
* Constructor |
| 23 |
* |
| 24 |
* @since 0.0.2 |
| 25 |
*/ |
| 26 |
public function __construct() { |
| 27 |
add_action( 'init', [ $this, 'srfm_schedule_daily_action' ] ); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Schedules a action that runs every 24 hours for SureForms. |
| 32 |
* |
| 33 |
* @hooked - init |
| 34 |
* @uses as_has_scheduled_action() To check if the action is already scheduled. |
| 35 |
* @uses as_schedule_recurring_action() To schedule a recurring action. |
| 36 |
* @link https://actionscheduler.org/api/ |
| 37 |
* @since 0.0.2 |
| 38 |
* @return void |
| 39 |
*/ |
| 40 |
public function srfm_schedule_daily_action() { |
| 41 |
// Check if the action is not scheduled. Then schedule a new action. |
| 42 |
if ( false === as_has_scheduled_action( 'srfm_daily_scheduled_action' ) ) { |
| 43 |
// Shedule a recurring action srfm_daily_scheduled_events that runs every 24 hours. |
| 44 |
as_schedule_recurring_action( strtotime( 'tomorrow' ), DAY_IN_SECONDS, 'srfm_daily_scheduled_action', [], 'sureforms', true ); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Unschedule any action. |
| 50 |
* |
| 51 |
* @param string $hook Event hook name. |
| 52 |
* @since 0.0.2 |
| 53 |
* @return void |
| 54 |
*/ |
| 55 |
public static function unschedule_events( $hook ) { |
| 56 |
as_unschedule_all_actions( $hook ); |
| 57 |
} |
| 58 |
|
| 59 |
} |
| 60 |
|