1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
<?php
namespace Onlineforce\Woocommerce_Izettle;
use \Onlineforce\Woocommerce_Izettle\Woocommerce_Izettle_Admin;
use \Onlineforce\Woocommerce_Izettle\Woocommerce_Izettle_Products;
use \Onlineforce\Izettle_Bindings\Izettle_Products;
use \Onlineforce\Izettle_Bindings\Izettle_Inventory;
class Woocommerce_Izettle_Cron {
public static function init_cron_task() {
if ( wp_next_scheduled( 'woocommerce_izettle_cron_task' ) ) {
return;
}
wp_schedule_event(
time(),
'woocommerce_izettle_cron_interval',
'woocommerce_izettle_cron_task'
);
}
public static function remove_cron_task() {
$timestamp = wp_next_scheduled( 'woocommerce_izettle_cron_task' );
wp_unschedule_event( $timestamp, 'woocommerce_izettle_cron_task' );
}
public static function reschedule_cron_task() {
self::remove_cron_task();
self::init_cron_task();
Woocommerce_Izettle_Admin::plugin_log( 'Rescheduled cron / sync task.' );
}
public static function create_cron_interval( $intervals ) {
$minutes = 60;
if ( null !== get_option( 'izettle_cron_interval' ) ) {
$minutes = (int) get_option( 'izettle_cron_interval' ) * 60;
}
$hours = floor( $minutes * 60 / 3600 );
$intervals['woocommerce_izettle_cron_interval'] = array(
'interval' => $minutes * 60,
'display' => "WooCommerce iZettle cron interval ({$hours} hour(s))",
);
return (array) $intervals;
}
public static function update_cron_lastrun() {
update_option( 'woocommerce_izettle_cron_lastrun', current_time( 'mysql' ) );
}
public static function get_cron_lastrun() {
return get_option( 'woocommerce_izettle_cron_lastrun' );
}
public static function run_cron_task() {
if ( ! get_option( 'izettle_cron_enabled' ) ) {
return false;
}
$last_run = strtotime( self::get_cron_lastrun() );
if ( defined( 'DOING_AJAX' ) ) {
$last_run = 0;
}
$next_run = $last_run + (60 * 60 * get_option( 'izettle_cron_interval' ));
if ( 0 !== $last_run && current_time( 'timestamp' ) < $next_run ) {
Woocommerce_Izettle_Admin::plugin_log( 'Aborting cron since it has been run within the interval.' );
return false;
}
do_action( 'woocommerce_izettle_before_cron' );
Woocommerce_Izettle_Admin::plugin_log( 'Starting cron task.' );
$start_time = microtime( true );
set_time_limit( 30 );
do_action( 'woocommerce_izettle_during_cron' );
$time_elapsed_secs = round( microtime( true ) - $start_time, 2 );
Woocommerce_Izettle_Admin::plugin_log( "Cron task has been run in {$time_elapsed_secs} seconds." );
self::update_cron_lastrun();
do_action( 'woocommerce_izettle_after_cron' );
}
}