/**
  * get_instance method
  *
  * Return singletonian instance of self
  *
  * @return Ai1ec_Events_List_Helper Singleton instance of self
  */
 public static function get_instance()
 {
     if (!self::$_instance instanceof Ai1ec_Events_List_Helper) {
         self::$_instance = new Ai1ec_Events_List_Helper();
     }
     return self::$_instance;
 }
 /**
  * save function
  *
  * Save settings to the database.
  *
  * @return void
  **/
 function save()
 {
     update_option('ai1ec_settings', $this);
     update_option('start_of_week', $this->week_start_day);
     update_option('ai1ec_cron_version', Ai1ec_Meta::get_option('ai1ec_cron_version') + 1);
     if ($this->is_timezone_open_for_change()) {
         update_option('timezone_string', $this->timezone);
     }
     return Ai1ec_Events_List_Helper::get_instance()->purge();
 }
Ai1ec_Routing_Factory::set_ai1ec_settings($ai1ec_settings);
// ================================
// = Initialize and setup HELPERS =
// ================================
global $ai1ec_view_helper, $ai1ec_settings_helper, $ai1ec_calendar_helper, $ai1ec_app_helper, $ai1ec_events_helper, $ai1ec_importer_helper, $ai1ec_exporter_helper, $ai1ec_platform_helper, $ai1ec_localization_helper, $ai1ec_importer_plugin_helper, $ai1ec_events_list_helper;
$ai1ec_view_helper = Ai1ec_View_Helper::get_instance();
$ai1ec_settings_helper = Ai1ec_Settings_Helper::get_instance();
$ai1ec_calendar_helper = Ai1ec_Calendar_Helper::get_instance();
$ai1ec_app_helper = Ai1ec_App_Helper::get_instance();
$ai1ec_events_helper = Ai1ec_Events_Helper::get_instance();
$ai1ec_importer_helper = Ai1ec_Importer_Helper::get_instance();
$ai1ec_exporter_helper = Ai1ec_Exporter_Helper::get_instance();
$ai1ec_platform_helper = Ai1ec_Platform_Helper::get_instance();
$ai1ec_localization_helper = Ai1ec_Localization_Helper::get_instance();
$ai1ec_importer_plugin_helper = Ai1ec_Importer_Plugin_Helper::get_instance();
$ai1ec_events_list_helper = Ai1ec_Events_List_Helper::get_instance();
if ('admin-ajax.php' === basename($_SERVER['SCRIPT_NAME']) && isset($_REQUEST['lang'])) {
    $ai1ec_localization_helper->set_language($_REQUEST['lang']);
}
// ====================================
// = Initialize and setup CONTROLLERS =
// ====================================
global $ai1ec_app_controller, $ai1ec_settings_controller, $ai1ec_events_controller, $ai1ec_calendar_controller, $ai1ec_importer_controller, $ai1ec_exporter_controller, $ai1ec_platform_controller, $ai1ec_duplicate_controller, $ai1ec_oauth_controller, $ai1ec_logging_controller;
$ai1ec_settings_controller = Ai1ec_Settings_Controller::get_instance();
$ai1ec_events_controller = Ai1ec_Events_Controller::get_instance();
$ai1ec_calendar_controller = Ai1ec_Calendar_Controller::get_instance();
$ai1ec_importer_controller = Ai1ec_Importer_Controller::get_instance();
$ai1ec_exporter_controller = Ai1ec_Exporter_Controller::get_instance();
$ai1ec_platform_controller = Ai1ec_Platform_Controller::get_instance();
$ai1ec_duplicate_controller = Ai1ec_Duplicate_Controller::get_instance();
$ai1ec_oauth_controller = Ai1ec_Oauth_Controller::get_instance();
 /**
  * create_cache_table_entries function
  *
  * Create a new entry for each day that the event spans.
  *
  * @param array $e Event array
  *
  * @return void
  **/
 public function create_cache_table_entries($e)
 {
     global $ai1ec_events_helper;
     // Decompose start dates into components
     $start_bits = getdate($e['start']);
     // ============================================
     // = Calculate the time for event's first day =
     // ============================================
     // Start time is event's original start time
     $event_start = $e['start'];
     // End time is beginning of next day
     $event_end = mktime(0, 0, 0, $start_bits['mon'], $start_bits['mday'] + 1, $start_bits['year']);
     // Cache first day
     $this->insert_event_in_cache_table(array('post_id' => $e['post_id'], 'start' => $event_start, 'end' => $event_end));
     // ====================================================
     // = Calculate the time for event's intermediate days =
     // ====================================================
     // Start time is previous end time
     $event_start = $event_end;
     // End time one day ahead
     $event_end += 60 * 60 * 24;
     // Cache intermediate days
     while ($event_end < $e['end']) {
         $this->insert_event_in_cache_table(array('post_id' => $e['post_id'], 'start' => $event_start, 'end' => $event_end));
         $event_start = $event_end;
         // Start time is previous end time
         $event_end += 24 * 60 * 60;
         // Increment end time by 1 day
     }
     // ===========================================
     // = Calculate the time for event's last day =
     // ===========================================
     // Start time is already correct (previous end time)
     // End time is event end time
     // Only insert if the last event instance if span is > 0
     $event_end = $e['end'];
     if ($event_end > $event_start) {
         // Cache last day
         $this->insert_event_in_cache_table(array('post_id' => $e['post_id'], 'start' => $event_start, 'end' => $event_end));
     }
     return Ai1ec_Events_List_Helper::get_instance()->clean_post_cache($e['post_id']);
 }
 /**
  * edited_events_categories method
  *
  * A callback method, triggered when `event_categories' are being edited
  *
  * @param int $term_id ID of term (category) being edited
  *
  * @return void Method does not return
  */
 function edited_events_categories($term_id)
 {
     global $wpdb;
     $tag_color_value = '';
     if (isset($_POST['tag-color-value']) && !empty($_POST['tag-color-value'])) {
         $tag_color_value = $_POST['tag-color-value'];
     }
     $table_name = $wpdb->prefix . 'ai1ec_event_category_colors';
     $term = $wpdb->get_row($wpdb->prepare('SELECT term_id, term_color' . ' FROM ' . $table_name . ' WHERE term_id = %d', $term_id));
     if (NULL === $term) {
         // term does not exist, create it
         $wpdb->insert($table_name, array('term_id' => $term_id, 'term_color' => $tag_color_value), array('%d', '%s'));
     } else {
         // term exist, update it
         if (NULL === $tag_color_value) {
             $tag_color_value = $term->term_color;
         }
         $wpdb->update($table_name, array('term_color' => $tag_color_value), array('term_id' => $term_id), array('%s'), array('%d'));
     }
     Ai1ec_Events_List_Helper::get_instance()->purge();
 }