| 1 |
<?php |
| 2 |
|
| 3 |
// Do not allow the file to be called directly. |
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
/** |
| 9 |
* This class is used to schedule the tasks used by Patchstack. |
| 10 |
*/ |
| 11 |
class P_Cron extends P_Core { |
| 12 |
|
| 13 |
/** |
| 14 |
* Add the actions required for the task scheduler. |
| 15 |
* |
| 16 |
* @param Patchstack $core |
| 17 |
* @return void |
| 18 |
*/ |
| 19 |
public function __construct( $core ) { |
| 20 |
parent::__construct( $core ); |
| 21 |
add_action( 'init', [ $this, 'schedule_events' ] ); |
| 22 |
add_filter( 'cron_schedules', [ $this, 'cron_schedules' ] ); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Define our own custom cron schedules. |
| 27 |
* |
| 28 |
* @param array $schedules |
| 29 |
* @return array |
| 30 |
*/ |
| 31 |
public function cron_schedules( $schedules ) { |
| 32 |
// Define the scheduled tasks. |
| 33 |
$schedules['patchstack_15minute'] = [ |
| 34 |
'interval' => ( 60 * 15 ), |
| 35 |
'display' => esc_attr__( 'Every 15 Minutes' ), |
| 36 |
]; |
| 37 |
|
| 38 |
$schedules['patchstack_hourly'] = [ |
| 39 |
'interval' => ( 60 * 60 ), |
| 40 |
'display' => esc_attr__( 'Every Hour' ), |
| 41 |
]; |
| 42 |
|
| 43 |
$schedules['patchstack_trihourly'] = [ |
| 44 |
'interval' => ( 60 * 60 * 3 ), |
| 45 |
'display' => esc_attr__( 'Every 3 Hours' ), |
| 46 |
]; |
| 47 |
|
| 48 |
$schedules['patchstack_twicedaily'] = [ |
| 49 |
'interval' => ( 60 * 60 * 12 ), |
| 50 |
'display' => esc_attr__( '2 Times Daily' ), |
| 51 |
]; |
| 52 |
|
| 53 |
$schedules['patchstack_daily'] = [ |
| 54 |
'interval' => ( 60 * 60 * 24 ), |
| 55 |
'display' => esc_attr__( '1 Time Daily' ), |
| 56 |
]; |
| 57 |
|
| 58 |
return $schedules; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Initialize our scheduled tasks. |
| 63 |
* |
| 64 |
* @return void |
| 65 |
*/ |
| 66 |
public function schedule_events() { |
| 67 |
// Random time throughout the day to make sure not all sites synchronize at the same time. |
| 68 |
$crons = get_option( 'patchstack_cron_offset', false ); |
| 69 |
if ( ! $crons || ! isset ( $crons['patchstack_daily'] ) ) { |
| 70 |
$crons = [ |
| 71 |
'patchstack_daily' => strtotime( 'today' ) + mt_rand( 0, 86399 ), |
| 72 |
'patchstack_hourly' => strtotime( 'today' ) + mt_rand( 0, 3600 ), |
| 73 |
'patchstack_trihourly' => strtotime( 'today' ) + mt_rand( 0, 9599 ), |
| 74 |
'patchstack_twicedaily' => strtotime( 'today' ) + mt_rand( 0, 43199 ), |
| 75 |
'patchstack_15minute' => strtotime( 'today' ) + mt_rand( 0, 899 ), |
| 76 |
]; |
| 77 |
update_option( 'patchstack_cron_offset', $crons ); |
| 78 |
|
| 79 |
// Clear existing scheduled events so we can use the new timestamps. |
| 80 |
foreach ( [ 'patchstack_send_software_data', 'patchstack_send_hacker_logs', 'patchstack_post_firewall_rules', 'patchstack_post_firewall_htaccess_rules', 'patchstack_post_dynamic_firewall_rules', 'patchstack_update_license_status', 'patchstack_send_event_logs', 'patchstack_update_plugins', 'patchstack_send_ping' ] as $event ) { |
| 81 |
wp_clear_scheduled_hook( $event ); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
// List of the different events. |
| 86 |
$free = [ |
| 87 |
'patchstack_send_software_data' => 'patchstack_twicedaily', |
| 88 |
'patchstack_update_license_status' => 'patchstack_twicedaily', |
| 89 |
'patchstack_send_ping' => 'patchstack_trihourly', |
| 90 |
'patchstack_update_plugins' => 'patchstack_15minute' |
| 91 |
]; |
| 92 |
|
| 93 |
$premium = [ |
| 94 |
'patchstack_send_hacker_logs' => 'patchstack_15minute', |
| 95 |
'patchstack_post_firewall_rules' => 'patchstack_twicedaily', |
| 96 |
'patchstack_post_firewall_htaccess_rules' => 'patchstack_trihourly', |
| 97 |
'patchstack_post_dynamic_firewall_rules' => 'patchstack_hourly', |
| 98 |
'patchstack_send_event_logs' => 'patchstack_15minute' |
| 99 |
]; |
| 100 |
|
| 101 |
// Schedule the events if they are not scheduled yet. |
| 102 |
foreach ( $free as $event => $interval ) { |
| 103 |
if ( ! wp_next_scheduled( $event ) ) { |
| 104 |
wp_schedule_event( $crons[ $interval ], $interval, $event ); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
if ( $this->get_option( 'patchstack_license_free', 0 ) == 0 ) { |
| 109 |
foreach ( $premium as $event => $interval ) { |
| 110 |
if ( ! wp_next_scheduled( $event ) ) { |
| 111 |
wp_schedule_event( $crons[ $interval ], $interval, $event ); |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
|