| 1 |
<?php |
| 2 |
/** |
| 3 |
* Declare class Config |
| 4 |
* |
| 5 |
* @package Config |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace LassoLite\Classes; |
| 9 |
|
| 10 |
use LassoLite\Classes\Processes\Import_All; |
| 11 |
|
| 12 |
/** |
| 13 |
* Config |
| 14 |
*/ |
| 15 |
class Cron { |
| 16 |
|
| 17 |
const CRONS = array( |
| 18 |
'lasso_lite_tracking_support_status' => 'daily', |
| 19 |
'lasso_lite_import_all' => 'lasso_lite_15_minutes', |
| 20 |
); |
| 21 |
|
| 22 |
/** |
| 23 |
* Cron constructor. |
| 24 |
*/ |
| 25 |
public function __construct() { |
| 26 |
add_filter( 'cron_schedules', array( $this, 'add_lasso_cron' ) ); |
| 27 |
add_action( 'lasso_lite_tracking_support_status', array( $this, 'lasso_lite_tracking_support_status' ) ); |
| 28 |
add_action( 'lasso_lite_import_all', array( $this, 'lasso_import_all' ) ); |
| 29 |
|
| 30 |
$this->lasso_create_schedule_hook(); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Add a custom cron to WordPress |
| 35 |
* |
| 36 |
* @param array $schedules An array of non-default cron schedules. Default empty. |
| 37 |
*/ |
| 38 |
public function add_lasso_cron( $schedules ) { |
| 39 |
$schedules['lasso_lite_15_minutes'] = array( |
| 40 |
'interval' => 15 * MINUTE_IN_SECONDS, // ? 15 minutes in seconds |
| 41 |
'display' => __( '15 minutes' ), |
| 42 |
); |
| 43 |
|
| 44 |
return $schedules; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Create hook for the new cron |
| 49 |
*/ |
| 50 |
public function lasso_create_schedule_hook() { |
| 51 |
$crons = self::CRONS; |
| 52 |
$crons_array = _get_cron_array(); |
| 53 |
$events = array(); |
| 54 |
foreach ( $crons_array as $time => $cron ) { |
| 55 |
foreach ( $cron as $hook => $dings ) { |
| 56 |
if ( strpos( $hook, 'lasso_lite_' ) === false ) { |
| 57 |
continue; |
| 58 |
} |
| 59 |
|
| 60 |
foreach ( $dings as $sig => $data ) { |
| 61 |
$interval = $data['interval'] ?? HOUR_IN_SECONDS; |
| 62 |
|
| 63 |
// ? get the cron that is less than the existing one |
| 64 |
if ( isset( $events[ $hook ] ) && $interval >= $events[ $hook ]->interval ) { |
| 65 |
continue; |
| 66 |
} |
| 67 |
|
| 68 |
$events[ $hook ] = (object) array( |
| 69 |
'hook' => $hook, |
| 70 |
'time' => $time, // ? UTC |
| 71 |
'schedule' => $data['schedule'], |
| 72 |
'interval' => $interval, |
| 73 |
); |
| 74 |
|
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
foreach ( $crons as $cron_name => $interval ) { |
| 80 |
if ( ! wp_next_scheduled( $cron_name ) ) { |
| 81 |
wp_schedule_event( time(), $interval, $cron_name ); |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Tracking support status |
| 88 |
*/ |
| 89 |
public function lasso_lite_tracking_support_status() { |
| 90 |
$settings = Setting::get_settings(); |
| 91 |
if ( boolval( $settings[ Enum::SUPPORT_ENABLED ] ) ) { |
| 92 |
Setting::save_support( false ); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Import all |
| 98 |
*/ |
| 99 |
public function lasso_import_all() { |
| 100 |
$allow_import_all = get_option( Import_All::OPTION, '0' ); |
| 101 |
if ( 1 === intval( $allow_import_all ) ) { |
| 102 |
$lasso_import_all = new Import_All(); |
| 103 |
$lasso_import_all->import(); |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|