WooCommerce iZettle 1.x Code Reference
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo
  • Hook Reference

Namespaces

  • Onlineforce
    • Woocommerce_Izettle

Classes

  • Onlineforce\Woocommerce_Izettle\Woocommerce_Izettle
  • Onlineforce\Woocommerce_Izettle\Woocommerce_Izettle_Activator
  • Onlineforce\Woocommerce_Izettle\Woocommerce_Izettle_Admin
  • Onlineforce\Woocommerce_Izettle\Woocommerce_Izettle_Admin_Actions
  • Onlineforce\Woocommerce_Izettle\Woocommerce_Izettle_Admin_Ajax
  • Onlineforce\Woocommerce_Izettle\Woocommerce_Izettle_Admin_Meta_Boxes
  • Onlineforce\Woocommerce_Izettle\Woocommerce_Izettle_Admin_Notices
  • Onlineforce\Woocommerce_Izettle\Woocommerce_Izettle_Background_Worker
  • Onlineforce\Woocommerce_Izettle\Woocommerce_Izettle_Cron
  • Onlineforce\Woocommerce_Izettle\Woocommerce_Izettle_Deactivator
  • Onlineforce\Woocommerce_Izettle\Woocommerce_Izettle_I18n
  • Onlineforce\Woocommerce_Izettle\Woocommerce_Izettle_Images
  • Onlineforce\Woocommerce_Izettle\Woocommerce_Izettle_Inventory
  • Onlineforce\Woocommerce_Izettle\Woocommerce_Izettle_Loader
  • Onlineforce\Woocommerce_Izettle\Woocommerce_Izettle_Orders
  • Onlineforce\Woocommerce_Izettle\Woocommerce_Izettle_Products
  • Onlineforce\Woocommerce_Izettle\Woocommerce_Izettle_Settings

Functions

  • Onlineforce\Woocommerce_Izettle\run_woocommerce_izettle
  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
/**
 * The class that handles cron functionality.
 *
 * A class definition that defines functions regarding cron synchronization
 * between WooCommerce and iZettle.
 *
 * @link       https://www.onlineforce.net
 * @since      1.0.0
 *
 * @package    Woocommerce_Izettle
 * @subpackage Woocommerce_Izettle/includes
 */

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;

/**
 * The class that handles cron functionality.
 *
 * A class definition that defines functions regarding cron synchronization
 * between WooCommerce and iZettle.
 *
 * @since      1.0.0
 * @package    Woocommerce_Izettle
 * @subpackage Woocommerce_Izettle/includes
 * @author     Onlineforce Sweden AB <support@onlineforce.net>
 */
class Woocommerce_Izettle_Cron {

    /**
     * Initialize our cron task with the WordPress scheduler.
     */
    public static function init_cron_task() {

        // Abort if task is already scheduled.
        if ( wp_next_scheduled( 'woocommerce_izettle_cron_task' ) ) {
            return;
        }

        // Schedule our task which defaults to hourly.
        wp_schedule_event(
            time(),
            'woocommerce_izettle_cron_interval',
            'woocommerce_izettle_cron_task'
        );

    }

    /**
     * Remove our cron task from the WordPress scheduler.
     */
    public static function remove_cron_task() {

        $timestamp = wp_next_scheduled( 'woocommerce_izettle_cron_task' );
        wp_unschedule_event( $timestamp, 'woocommerce_izettle_cron_task' );

    }

    /**
     * Reschedule our cron task in the WordPress scheduler.
     */
    public static function reschedule_cron_task() {

        self::remove_cron_task();
        self::init_cron_task();
        Woocommerce_Izettle_Admin::plugin_log( 'Rescheduled cron / sync task.' );

    }

    /**
     * Create our own cron interval for the WordPress scheduler.
     *
     * @param      array $intervals  The intervals that we add to.
     *
     * @return     array   Intervals after we have added our own.
     */
    public static function create_cron_interval( $intervals ) {

        // Default minutes to 60.
        $minutes = 60;

        // Check if user has set a custom interval in options
        // and use this instead.
        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, // seconds.
            'display' => "WooCommerce iZettle cron interval ({$hours} hour(s))",
        );

        return (array) $intervals;

    }

    /**
     * Save cron task last run timestamp in database.
     */
    public static function update_cron_lastrun() {

        update_option( 'woocommerce_izettle_cron_lastrun', current_time( 'mysql' ) );

    }

    /**
     * Get cron task last run timestamp from database.
     *
     * @return     string  The time when the task wast last run.
     */
    public static function get_cron_lastrun() {

        return get_option( 'woocommerce_izettle_cron_lastrun' );

    }

    /**
     * Run the cron task.
     */
    public static function run_cron_task() {

        if ( ! get_option( 'izettle_cron_enabled' ) ) {
            return false;
        }

        $last_run = strtotime( self::get_cron_lastrun() );

        // If we have been triggered by ajax, ignore last run.
        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 );

        // Attempt to set time limit in PHP to avoid script timeout.
        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' );

    }

}
WooCommerce iZettle 1.x Code Reference API documentation generated by ApiGen