/**
  * Create the html element used as the UI control for the datepicker button.
  * The href must keep only active filters.
  *
  * @param array           $args         Populated args for the view
  * @param int|string|null $initial_date The datepicker's initially set date
  * @return Ai1ec_Generic_Html_Tag
  */
 public static function create_datepicker_link(array $args, $initial_date = null)
 {
     global $ai1ec_settings, $ai1ec_view_helper;
     $link = Ai1ec_Helper_Factory::create_generic_html_tag('a');
     $date_format_pattern = Ai1ec_Time_Utility::get_date_pattern_by_key($ai1ec_settings->input_date_format);
     if ($initial_date == null) {
         // If exact_date argument was provided, use its value to initialize
         // datepicker.
         if (isset($args['exact_date']) && $args['exact_date'] !== false && $args['exact_date'] !== null) {
             $initial_date = $args['exact_date'];
         } else {
             $initial_date = Ai1ec_Time_Utility::gmt_to_local(Ai1ec_Time_Utility::current_time());
         }
     }
     // Convert initial date to formatted date if required.
     if (Ai1ec_Validation_Utility::is_valid_time_stamp($initial_date)) {
         $initial_date = Ai1ec_Time_Utility::format_date(Ai1ec_Time_Utility::gmt_to_local($initial_date), $ai1ec_settings->input_date_format);
     }
     $link->add_class('ai1ec-minical-trigger btn');
     $link->set_attribute('data-date', $initial_date);
     $link->set_attribute('data-date-format', $date_format_pattern);
     $link->set_attribute('data-date-weekstart', $ai1ec_settings->week_start_day);
     $link->set_attribute_expr($args['data_type']);
     $text = '<img src="' . esc_attr($ai1ec_view_helper->get_theme_img_url('date-icon.png')) . '" class="ai1ec-icon-datepicker" />';
     $link->set_text($text);
     $href_args = array('action' => $args['action'], 'cat_ids' => $args['cat_ids'], 'tag_ids' => $args['tag_ids'], 'exact_date' => "__DATE__");
     $data_href = self::create_href_helper_instance($href_args);
     $link->set_attribute('data-href', $data_href->generate_href());
     $link->set_attribute('href', '#');
     return $link;
 }
 /**
  * Returns a non-associative array of four links for the day view of the
  * calendar:
  *    previous day, and next day.
  * Each element is an associative array containing the link's enabled status
  * ['enabled'], CSS class ['class'], text ['text'] and value to assign to
  * link's href ['href'].
  *
  * @param array $args	Current request arguments
  *
  * @return array      Array of links
  */
 function get_oneday_pagination_links($args)
 {
     $links = array();
     $orig_date = $args['exact_date'];
     $local_date = Ai1ec_Time_Utility::gmt_to_local($args['exact_date']);
     $bits = Ai1ec_Time_Utility::gmgetdate($local_date);
     // ================
     // = Previous day =
     // ================
     $local_date = gmmktime(0, 0, 0, $bits['mon'], $bits['mday'] + $args['oneday_offset'] - 1, $bits['year']);
     $args['exact_date'] = Ai1ec_Time_Utility::local_to_gmt($local_date);
     $href = Ai1ec_View_Factory::create_href_helper_instance($args);
     $links[] = array('enabled' => true, 'class' => 'ai1ec-prev-day', 'text' => '<i class="icon-chevron-left"></i>', 'href' => $href->generate_href());
     // ======================
     // = Minical datepicker =
     // ======================
     $args['exact_date'] = $orig_date;
     $links[] = Ai1ec_View_Factory::create_datepicker_link($args, $args['exact_date']);
     // =============
     // = Next week =
     // =============
     $local_date = gmmktime(0, 0, 0, $bits['mon'], $bits['mday'] + $args['oneday_offset'] + 1, $bits['year']);
     $args['exact_date'] = Ai1ec_Time_Utility::local_to_gmt($local_date);
     $href = Ai1ec_View_Factory::create_href_helper_instance($args);
     $links[] = array('enabled' => true, 'class' => 'ai1ec-next-day', 'text' => '<i class="icon-chevron-right"></i>', 'href' => $href->generate_href());
     return $links;
 }
 /**
  * gmt_to_local function
  *
  * Returns the UNIX timestamp adjusted to the local timezone.
  *
  * @param int $timestamp
  *
  * @return int
  **/
 function gmt_to_local($timestamp)
 {
     return Ai1ec_Time_Utility::gmt_to_local($timestamp);
 }
 /**
  * Return the embedded day view of the calendar, optionally filtered by
  * event categories and tags.
  *
  * @param array $args     associative array with any of these elements:
  *   int oneday_offset  => specifies which day to display relative to the
  *                        current day
  *   array cat_ids     => restrict events returned to the given set of
  *                        event category slugs
  *   array tag_ids     => restrict events returned to the given set of
  *                        event tag names
  *   array post_ids    => restrict events returned to the given set of
  *                        post IDs
  *
  * @return string	        returns string of view output
  */
 function get_oneday_view($args)
 {
     global $ai1ec_view_helper, $ai1ec_events_helper, $ai1ec_calendar_helper, $ai1ec_settings;
     $defaults = array('oneday_offset' => 0, 'cat_ids' => array(), 'tag_ids' => array(), 'post_ids' => array(), 'exact_date' => Ai1ec_Time_Utility::current_time());
     $args = wp_parse_args($args, $defaults);
     // Localize requested date and get components.
     $local_date = Ai1ec_Time_Utility::gmt_to_local($args['exact_date']);
     $bits = Ai1ec_Time_Utility::gmgetdate($local_date);
     // Apply day offset.
     $day_shift = 0 + $args['oneday_offset'];
     // Now align date to start of day (midnight).
     $local_date = gmmktime(0, 0, 0, $bits['mon'], $bits['mday'] + $day_shift, $bits['year']);
     $cell_array = $ai1ec_calendar_helper->get_oneday_cell_array($local_date, array('cat_ids' => $args['cat_ids'], 'tag_ids' => $args['tag_ids'], 'post_ids' => $args['post_ids']));
     // Create pagination links.
     $pagination_links = $ai1ec_calendar_helper->get_oneday_pagination_links($args);
     $pagination_links = $ai1ec_view_helper->get_theme_view('pagination.php', array('links' => $pagination_links, 'data_type' => $args['data_type']));
     $date_format = Ai1ec_Meta::get_option('date_format', 'l, M j, Y');
     $title = Ai1ec_Time_Utility::date_i18n($date_format, $local_date, true);
     $time_format = Ai1ec_Meta::get_option('time_format', 'g a');
     // Calculate today marker's position.
     $now = Ai1ec_Time_Utility::current_time();
     $now = Ai1ec_Time_Utility::gmt_to_local($now);
     $now_text = $ai1ec_events_helper->get_short_time($now, false);
     $now = Ai1ec_Time_Utility::gmgetdate($now);
     $now = $now['hours'] * 60 + $now['minutes'];
     $view_args = array('title' => $title, 'type' => 'oneday', 'cell_array' => $cell_array, 'show_location_in_title' => $ai1ec_settings->show_location_in_title, 'now_top' => $now, 'now_text' => $now_text, 'pagination_links' => $pagination_links, 'post_ids' => join(',', $args['post_ids']), 'time_format' => $time_format, 'done_allday_label' => false, 'done_grid' => false, 'data_type' => $args['data_type'], 'data_type_events' => '');
     if ($ai1ec_settings->ajaxify_events_in_web_widget) {
         $view_args['data_type_events'] = $args['data_type'];
     }
     // Add navigation if requested.
     $view_args['navigation'] = $args['no_navigation'] ? '' : $ai1ec_view_helper->get_theme_view('navigation.php', $view_args);
     return apply_filters('ai1ec_get_oneday_view', $ai1ec_view_helper->get_theme_view('oneday.php', $view_args), $view_args);
 }
 /**
  * This is the CRON function which sends emails
  *
  */
 public function send_notifications_for_events()
 {
     global $ai1ec_settings;
     if (!$ai1ec_settings->enable_user_event_notifications) {
         return false;
     }
     $notifications = Ai1ec_Meta::get_option(self::SAVED_NOTIFICATIONS, array());
     foreach ($notifications as $event_id => $instances) {
         foreach ($instances as $instance_id => $notifications_to_send) {
             try {
                 $event = new Ai1ec_Event($event_id, $instance_id);
             } catch (Ai1ec_Event_Not_Found $excpt) {
                 unset($notifications[$event_id]);
                 continue 2;
             }
             $subject = $ai1ec_settings->user_upcoming_event_mail_subject;
             $message = $ai1ec_settings->user_upcoming_event_mail_body;
             if (true === $this->check_if_notification_should_be_sent_for_event($event)) {
                 $event_url = get_permalink($event->post_id);
                 if (0 !== (int) $instance_id) {
                     $event_url .= $instance_id;
                 }
                 $translations = array('[event_title]' => $event->post->post_title, '[event_start]' => Ai1ec_Time_Utility::date_i18n('D, d M Y H:i', Ai1ec_Time_Utility::gmt_to_local($event->start)), '[event_url]' => $event_url, '[site_title]' => get_bloginfo('name'), '[site_url]' => site_url());
                 foreach ($notifications_to_send as $recipient => $details) {
                     $notification = Ai1ec_Notification_Factory::create_notification_instance(array($recipient), $message, Ai1ec_Notification_Factory::EMAIL_NOTIFICATION, $subject);
                     $notification->set_translations($translations);
                     $notification->send();
                     // unset the notification
                     unset($notifications[$event_id][$instance_id][$recipient]);
                 }
             }
             // after sending the mail, unset the instance id if all
             // mail have been sent
             if (empty($notifications[$event_id][$instance_id])) {
                 unset($notifications[$event_id][$instance_id]);
             }
         }
         if (empty($notifications[$event_id])) {
             unset($notifications[$event_id]);
         }
     }
     return update_option(self::SAVED_NOTIFICATIONS, $notifications);
 }