public static function save_post($post_id, $post = false)
 {
     global $wpdb, $EM_Event, $EM_Location, $EM_Notices, $EM_SAVING_EVENT, $EM_EVENT_SAVE_POST;
     if (!empty($EM_SAVING_EVENT)) {
         return;
     }
     //never proceed with this if using EM_Event::save();
     if (isset($_GET['preview_id']) && isset($_GET['preview_nonce']) && wp_verify_nonce($_GET['preview_nonce'], 'post_preview_' . $post_id)) {
         return;
     }
     //don't proceed with saving when previewing, may cause issues
     $post_type = get_post_type($post_id);
     $is_post_type = $post_type == EM_POST_TYPE_EVENT || $post_type == 'event-recurring';
     $saving_status = !in_array(get_post_status($post_id), array('trash', 'auto-draft')) && !defined('DOING_AUTOSAVE');
     $EM_EVENT_SAVE_POST = true;
     //first filter for save_post in EM for events
     if (!defined('UNTRASHING_' . $post_id) && $is_post_type && $saving_status) {
         $EM_Event = new EM_Event($post_id, 'post_id');
         //grab event, via post info, reset the $EM_Event variable
         $EM_Event->post_type = $post_type;
         if (!empty($_REQUEST['_emnonce']) && wp_verify_nonce($_REQUEST['_emnonce'], 'edit_event')) {
             //this is only run if we know form data was submitted, hence the nonce
             $get_meta = $EM_Event->get_post_meta();
             $validate_meta = $EM_Event->validate_meta();
             //Handle Errors by making post draft
             do_action('em_event_save_pre', $EM_Event);
             //technically, the event is saved... but the meta isn't. wp doesn't give an pre-intervention action for this (or does it?)
             //if we execute a location save here, we will screw up the current save_post $wp_filter pointer executed in do_action()
             //therefore, we save the current pointer position (priority) and set it back after saving the location further down
             global $wp_filter, $wp_current_filter;
             $wp_filter_priority = key($wp_filter['save_post']);
             $tag = end($wp_current_filter);
             //save the event meta, whether validated or not and which includes saving a location
             $save_meta = $EM_Event->save_meta();
             //reset save_post pointer in $wp_filter to its original position
             reset($wp_filter[$tag]);
             do {
                 if (key($wp_filter[$tag]) == $wp_filter_priority) {
                     break;
                 }
             } while (next($wp_filter[$tag]) !== false);
             //save categories in case of default category
             if (get_option('dbem_categories_enabled')) {
                 $EM_Event->get_categories()->save();
             }
             //continue whether all went well or not
             if (!$get_meta || !$validate_meta || !$save_meta) {
                 //failed somewhere, set to draft, don't publish
                 $EM_Event->set_status(null, true);
                 if ($EM_Event->is_recurring()) {
                     $EM_Notices->add_error('<strong>' . __('Your event details are incorrect and recurrences cannot be created, please correct these errors first:', 'events-manager') . '</strong>', true);
                     //Always seems to redirect, so we make it static
                 } else {
                     $EM_Notices->add_error('<strong>' . sprintf(__('Your %s details are incorrect and cannot be published, please correct these errors first:', 'events-manager'), __('event', 'events-manager')) . '</strong>', true);
                     //Always seems to redirect, so we make it static
                 }
                 $EM_Notices->add_error($EM_Event->get_errors(), true);
                 //Always seems to redirect, so we make it static
                 apply_filters('em_event_save', false, $EM_Event);
             } else {
                 //if this is just published, we need to email the user about the publication, or send to pending mode again for review
                 if (!$EM_Event->is_recurring() && !current_user_can('publish_events') || $EM_Event->is_recurring() && !current_user_can('publish_recurring_events')) {
                     if ($EM_Event->is_published()) {
                         $EM_Event->set_status(0, true);
                     }
                     //no publishing and editing... security threat
                 }
                 apply_filters('em_event_save', true, $EM_Event);
             }
         } else {
             //we're updating only the quick-edit style information, which is only post info saved into the index
             if ($EM_Event->validate()) {
                 do_action('em_event_save_pre', $EM_Event);
                 //technically, the event is saved... but the meta isn't. wp doesn't give an pre-intervention action for this (or does it?)
                 //first things first, we must make sure we have an index, if not, reset it to a new one:
                 $event_truly_exists = $wpdb->get_var('SELECT event_id FROM ' . EM_EVENTS_TABLE . " WHERE event_id={$EM_Event->event_id}") == $EM_Event->event_id;
                 if (empty($EM_Event->event_id) || !$event_truly_exists) {
                     $EM_Event->save_meta();
                 }
                 //we can save the status now
                 $EM_Event->get_previous_status();
                 //before we save anything
                 $event_status = $EM_Event->get_status(true);
                 //if this is just published, we need to email the user about the publication, or send to pending mode again for review
                 if (!$EM_Event->is_recurring() && !current_user_can('publish_events') || $EM_Event->is_recurring() && !current_user_can('publish_recurring_events')) {
                     if ($EM_Event->is_published()) {
                         $EM_Event->set_status(0, true);
                     }
                     //no publishing and editing... security threat
                 }
                 //now update the db
                 $where_array = array($EM_Event->event_name, $EM_Event->event_owner, $EM_Event->event_slug, $EM_Event->event_private, $EM_Event->event_id);
                 $sql = $wpdb->prepare("UPDATE " . EM_EVENTS_TABLE . " SET event_name=%s, event_owner=%d, event_slug=%s, event_status={$event_status}, event_private=%d WHERE event_id=%d", $where_array);
                 $wpdb->query($sql);
                 if ($EM_Event->is_recurring() && $EM_Event->is_published()) {
                     //recurrences are (re)saved only if event is published
                     $EM_Event->save_events();
                 }
                 apply_filters('em_event_save', true, $EM_Event);
             } else {
                 do_action('em_event_save_pre', $EM_Event);
                 //technically, the event is saved... but the meta isn't. wp doesn't give an pre-intervention action for this (or does it?)
                 //Event doesn't validate, so set status to null
                 $EM_Event->set_status(null, true);
                 apply_filters('em_event_save', false, $EM_Event);
             }
         }
         self::maybe_publish_location($EM_Event);
     }
 }