/**
  * Utility function loop through existing Snapshot items and make sure they are
  * setup in the WP Cron facility. Also, in case there are some left over cron
  * entries a secondary process will loop through the WP Cron entries to make
  * the entries related to Snapshot are valid and current.
  *
  * @since 1.0.2
  * @see
  *
  * @param none
  *
  * @return none
  */
 function snapshot_scheduler()
 {
     $HAVE_SCHEDULED_EVENTS = false;
     // A two-step process.
     // 1. First any items needing to be schduled we make sure they are added.
     if (isset($this->config_data['items']) && count($this->config_data['items'])) {
         $scheds = (array) wp_get_schedules();
         foreach ($this->config_data['items'] as $key_slug => $item) {
             if (isset($item['interval']) && $item['interval'] != "") {
                 if (isset($scheds[$item['interval']])) {
                     $next_timestamp = wp_next_scheduled($this->_settings['backup_cron_hook'], array(intval($key_slug)));
                     if (!$next_timestamp) {
                         //$interval_offset = $scheds[$item['interval']]['interval'];
                         //$offset_timestamp = time() - $interval_offset;
                         //wp_schedule_event($offset_timestamp, $item['interval'], $this->_settings['backup_cron_hook'], array(intval($key_slug)) );
                         wp_schedule_event(time() + Snapshot_Helper_Utility::calculate_interval_offset_time($item['interval'], $item['interval-offset']), $item['interval'], $this->_settings['backup_cron_hook'], array(intval($key_slug)));
                         $HAVE_SCHEDULED_EVENTS = true;
                     }
                 }
             }
         }
     }
     // 2. Go through the WP cron entries. Any snapshot items not matching to existing items or items without proper intervals unschedule.
     $crons = _get_cron_array();
     if ($crons) {
         foreach ($crons as $cron_time => $cron_set) {
             foreach ($cron_set as $cron_callback_function => $cron_item) {
                 if ($cron_callback_function == "snapshot_backup_cron") {
                     foreach ($cron_item as $cron_key => $cron_details) {
                         if (isset($cron_details['args'][0])) {
                             $item_key = intval($cron_details['args'][0]);
                             if (!isset($this->config_data['items'][$item_key])) {
                                 $timestamp = wp_next_scheduled($this->_settings['backup_cron_hook'], array(intval($item_key)));
                                 if ($timestamp) {
                                     wp_unschedule_event($timestamp, $this->_settings['backup_cron_hook'], array(intval($item_key)));
                                 } else {
                                     wp_unschedule_event($cron_time, $this->_settings['backup_cron_hook'], array(intval($item_key)));
                                 }
                             }
                         }
                     }
                 } else {
                     if ($cron_callback_function == $this->_settings['remote_file_cron_hook']) {
                         foreach ($cron_item as $cron_key => $cron_details) {
                             if ($cron_details['schedule'] !== $this->_settings['remote_file_cron_interval']) {
                                 $timestamp = wp_next_scheduled($this->_settings['remote_file_cron_hook']);
                                 wp_unschedule_event($timestamp, $this->_settings['remote_file_cron_hook']);
                             }
                         }
                     }
                 }
             }
         }
     }
     // We only need the remote file cron if we have destinations defined
     if (isset($this->config_data['destinations']) && count($this->config_data['destinations'])) {
         $timestamp = wp_next_scheduled($this->_settings['remote_file_cron_hook']);
         if (!$timestamp) {
             wp_schedule_event(time(), $this->_settings['remote_file_cron_interval'], $this->_settings['remote_file_cron_hook']);
             $HAVE_SCHEDULED_EVENTS = true;
         }
     }
     if ($HAVE_SCHEDULED_EVENTS == true) {
         wp_remote_post(get_option('siteurl') . '/wp-cron.php', array('timeout' => 3, 'blocking' => false, 'sslverify' => false, 'body' => array('nonce' => wp_create_nonce('WPMUDEVSnapshot'), 'type' => 'start'), 'user-agent' => 'WPMUDEVSnapshot'));
     }
 }