/**
  * Send an e-mail to the admin and another to the user if form passes
  * validation.
  */
 public function add_ics_feed_frontend()
 {
     global $wpdb;
     $table_name = $wpdb->prefix . 'ai1ec_event_feeds';
     $check = $this->validate_form();
     $check['nonce'] = wp_nonce_field('ai1ec_submit_ics_form', AI1EC_POST_TYPE, true, false);
     if (true === $check['success']) {
         $ai1ec_settings = Ai1ec_Settings::get_instance();
         // Strip slashes if ridiculous PHP setting magic_quotes_gpc is enabled.
         if (get_magic_quotes_gpc()) {
             foreach ($_POST as &$param) {
                 $param = stripslashes($param);
             }
         }
         $translations = $this->get_translations();
         $notification_for_admin = Ai1ec_Notification_Factory::create_notification_instance(array(get_option('admin_email')), $ai1ec_settings->admin_mail_body, Ai1ec_Notification_Factory::EMAIL_NOTIFICATION, $ai1ec_settings->admin_mail_subject);
         $notification_for_user = Ai1ec_Notification_Factory::create_notification_instance(array($_POST['ai1ec_submitter_email']), $ai1ec_settings->user_mail_body, Ai1ec_Notification_Factory::EMAIL_NOTIFICATION, $ai1ec_settings->user_mail_subject);
         $notification_for_admin->set_translations($translations);
         $notification_for_admin->send();
         $notification_for_user->set_translations($translations);
         $notification_for_user->send();
     }
     echo json_encode($check);
     exit;
 }
 /**
  * Handle AJAX request for submission of front-end create event form.
  *
  * @return null
  */
 public function submit_front_end_create_event_form()
 {
     global $ai1ec_view_helper, $ai1ec_calendar_helper, $ai1ec_events_helper;
     $ai1ec_settings = Ai1ec_Settings::get_instance();
     $error = false;
     $html = '';
     $default_error_msg = __('There was an error creating your event.', AI1EC_PLUGIN_NAME) . ' ' . __('Please try again or contact the site administrator for help.', AI1EC_PLUGIN_NAME);
     $valid = $this->validate_front_end_create_event_form($message);
     // If valid submission, proceed with event creation.
     if ($valid) {
         // Determine post publish status.
         if (current_user_can('publish_ai1ec_events')) {
             $post_status = 'publish';
         } else {
             if (current_user_can('edit_ai1ec_events')) {
                 $post_status = 'pending';
             } else {
                 if ($ai1ec_settings->allow_anonymous_submissions) {
                     $post_status = 'pending';
                 }
             }
         }
         // Strip slashes if ridiculous PHP setting magic_quotes_gpc is enabled.
         foreach ($_POST as $param_name => $param) {
             if ('ai1ec' === substr($param_name, 0, 5) && is_scalar($param)) {
                 $_POST[$param_name] = stripslashes($param);
             }
         }
         // Build post array from submitted data.
         $post = array('post_type' => AI1EC_POST_TYPE, 'post_author' => get_current_user_id(), 'post_title' => $_POST['post_title'], 'post_content' => $_POST['post_content'], 'post_status' => $post_status);
         // Copy posted event data to new empty event object.
         $event = new Ai1ec_Event();
         $event->post = $post;
         $event->categories = isset($_POST['ai1ec_categories']) ? implode(',', $_POST['ai1ec_categories']) : '';
         $event->tags = isset($_POST['ai1ec_tags']) ? $_POST['ai1ec_tags'] : '';
         $event->allday = isset($_POST['ai1ec_all_day_event']) ? (bool) $_POST['ai1ec_all_day_event'] : 0;
         $event->instant_event = isset($_POST['ai1ec_instant_event']) ? (bool) $_POST['ai1ec_instant_event'] : 0;
         $event->start = isset($_POST['ai1ec_start_time']) ? $_POST['ai1ec_start_time'] : '';
         if ($event->instant_event) {
             $event->end = $event->start + 1800;
         } else {
             $event->end = isset($_POST['ai1ec_end_time']) ? $_POST['ai1ec_end_time'] : '';
         }
         $event->address = isset($_POST['ai1ec_address']) ? $_POST['ai1ec_address'] : '';
         $event->show_map = isset($_POST['ai1ec_google_map']) ? (bool) $_POST['ai1ec_google_map'] : 0;
         $scalar_field_list = array('ai1ec_venue' => FILTER_SANITIZE_STRING, 'ai1ec_cost' => FILTER_SANITIZE_STRING, 'ai1ec_is_free' => FILTER_SANITIZE_NUMBER_INT, 'ai1ec_ticket_url' => FILTER_VALIDATE_URL, 'ai1ec_contact_name' => FILTER_SANITIZE_STRING, 'ai1ec_contact_phone' => FILTER_SANITIZE_STRING, 'ai1ec_contact_email' => FILTER_VALIDATE_EMAIL, 'ai1ec_contact_url' => FILTER_VALIDATE_URL);
         foreach ($scalar_field_list as $scalar_field => $field_filter) {
             $scalar_value = filter_input(INPUT_POST, $scalar_field, $field_filter);
             if (!empty($scalar_value)) {
                 $use_name = substr($scalar_field, 6);
                 $event->{$use_name} = $scalar_value;
             }
         }
         // Save the event to the database.
         try {
             $event->save();
             $ai1ec_events_helper->cache_event($event);
             // Check if uploads are enabled and there is an uploaded file.
             if ((is_user_logged_in() || $ai1ec_settings->allow_anonymous_submissions && $ai1ec_settings->allow_anonymous_uploads) && !empty($_FILES['ai1ec_image']['name'])) {
                 require_once ABSPATH . 'wp-admin/includes/image.php';
                 require_once ABSPATH . 'wp-admin/includes/file.php';
                 require_once ABSPATH . 'wp-admin/includes/media.php';
                 $attach_id = media_handle_upload('ai1ec_image', $event->post_id);
                 if (is_int($attach_id)) {
                     update_post_meta($event->post_id, '_thumbnail_id', $attach_id);
                 }
             }
             // Send the mail
             $admin_notification = Ai1ec_Notification_Factory::create_notification_instance(array(get_option('admin_email')), $ai1ec_settings->admin_add_new_event_mail_body, Ai1ec_Notification_Factory::EMAIL_NOTIFICATION, $ai1ec_settings->admin_add_new_event_mail_subject);
             $edit_url = 'post.php?post=' . $event->post_id . '&action=edit';
             $translations = array('[event_title]' => $_POST['post_title'], '[site_title]' => get_bloginfo('name'), '[site_url]' => site_url(), '[event_admin_url]' => admin_url($edit_url));
             $admin_notification->set_translations($translations);
             $sent = $admin_notification->send();
             if (current_user_can('publish_ai1ec_events')) {
                 $message = sprintf(__('Thank you for your submission. Your event <em>%s</em> was published successfully.', AI1EC_PLUGIN_NAME), $post['post_title']);
                 $link_text = __('View Your Event', AI1EC_PLUGIN_NAME);
                 $link_url = get_permalink($event->post_id);
             } else {
                 $message = sprintf(__('Thank you for your submission. Your event <em>%s</em> will be reviewed and published once approved.', AI1EC_PLUGIN_NAME), $post['post_title']);
                 $link_text = __('Back to Calendar', AI1EC_PLUGIN_NAME);
                 $link_url = $ai1ec_calendar_helper->get_calendar_url();
             }
         } catch (Exception $e) {
             trigger_error(sprintf(__('There was an error during event creation: %s', AI1EC_PLUGIN_NAME), $e->getMessage()), E_USER_WARNING);
             $error = true;
             $message = $default_error_msg;
         }
         $args = array('message_type' => $error ? 'error' : 'success', 'message' => $message, 'link_text' => $link_text, 'link_url' => $link_url);
         $html = $ai1ec_view_helper->get_theme_view('create-event-message.php', $args);
     } else {
         $error = true;
     }
     $response = array('error' => $error, 'message' => $message, 'html' => $html);
     $ai1ec_view_helper->xml_response($response);
 }
 /**
  * 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);
 }