function mc_event_post($action, $data, $event_id)
{
    // if the event save was successful.
    if ($action == 'add' || $action == 'copy') {
        $post_id = mc_create_event_post($data, $event_id);
    } else {
        if ($action == 'edit') {
            if (isset($_POST['event_post']) && ($_POST['event_post'] == 0 || $_POST['event_post'] == '')) {
                $post_id = mc_create_event_post($data, $event_id);
            } else {
                $post_id = $_POST['event_post'];
            }
            // If, after all that, the post doesn't exist, create it.
            if (!get_post_status($post_id)) {
                mc_create_event_post($data, $event_id);
            }
            $term = mc_get_category_detail($data['event_category'], 'category_term');
            if (!$term) {
                $term = wp_insert_term('General', 'mc-event-category');
                $term = !is_wp_error($term) ? $term['term_id'] : false;
                if ($term) {
                    $update = mc_update_category('category_term', $term, $data['event_category']);
                }
            }
            $privacy = mc_get_category_detail($data['event_category'], 'category_private') == 1 ? 'private' : 'publish';
            $title = $data['event_title'];
            $template = apply_filters('mc_post_template', 'details', $term);
            $data['shortcode'] = "[my_calendar_event event='{$event_id}' template='{$template}' list='']";
            $description = $data['event_desc'];
            $excerpt = $data['event_short'];
            $post_status = $privacy;
            $auth = $data['event_author'];
            $type = 'mc-events';
            $my_post = array('ID' => $post_id, 'post_title' => $title, 'post_content' => $description, 'post_status' => $post_status, 'post_author' => $auth, 'post_name' => sanitize_title($title), 'post_date' => date('Y-m-d H:i:s', current_time('timestamp')), 'post_type' => $type, 'post_excerpt' => $excerpt);
            if (mc_switch_sites() && defined(BLOG_ID_CURRENT_SITE)) {
                switch_to_blog(BLOG_ID_CURRENT_SITE);
            }
            $post_id = wp_update_post($my_post);
            wp_set_object_terms($post_id, (int) $term, 'mc-event-category');
            if ($data['event_image'] == '') {
                delete_post_thumbnail($post_id);
            } else {
                $attachment_id = isset($_POST['event_image_id']) && is_numeric($_POST['event_image_id']) ? $_POST['event_image_id'] : false;
                if ($attachment_id) {
                    set_post_thumbnail($post_id, $attachment_id);
                }
            }
            $access = isset($_POST['events_access']) ? $_POST['events_access'] : array();
            $access_terms = implode(',', array_values($access));
            mc_update_event('event_access', $access_terms, $event_id, '%s');
            do_action('mc_update_event_post', $post_id, $_POST, $data, $event_id);
            if (mc_switch_sites()) {
                restore_current_blog();
            }
        }
    }
    return $post_id;
}
function mc_transition_db()
{
    // copy to post types. Don't do this if referencing remote sites.
    if (get_option('mc_remote') != 'true') {
        global $wpdb;
        $results = $wpdb->get_results('SELECT * FROM ' . my_calendar_locations_table(), ARRAY_A);
        $locations = array();
        foreach ($results as $result) {
            $location_id = $result['location_id'];
            unset($result['location_id']);
            $hash = md5(serialize($result));
            $locations[$location_id] = $result;
        }
        $results = $wpdb->get_results('SELECT * FROM ' . my_calendar_categories_table());
        foreach ($results as $category) {
            $term = wp_insert_term($category->category_name, 'mc-event-category');
            if (!is_wp_error($term)) {
                $term_id = $term['term_id'];
                mc_update_category('category_term', $term_id, $category->category_id);
            } else {
                if (isset($term->error_data['term_exists'])) {
                    $term_id = $term->error_data['term_exists'];
                    mc_update_category('category_term', $term_id, $category->category_id);
                }
            }
        }
        $results = $wpdb->get_results('SELECT * FROM ' . my_calendar_table(), ARRAY_A);
        foreach ($results as $event) {
            $post_id = mc_create_event_post($event, $event['event_id']);
            mc_update_event('event_post', $post_id, $event['event_id']);
            // false if not found, id if found.
            $location = mc_check_location_table($event, $locations);
            if ($location) {
                mc_update_event('event_location', $location, $event['event_id']);
            } else {
                if ($event['event_label'] == '' && $event['event_street'] == '' && $event['event_url'] == '' && $event['event_city'] == '' && $event['event_state'] == '' && $event['event_country'] == '') {
                    // don't insert the row if location does not have basic data.
                } else {
                    $add = array('location_label' => $event['event_label'], 'location_street' => $event['event_street'], 'location_street2' => $event['event_street2'], 'location_city' => $event['event_city'], 'location_state' => $event['event_state'], 'location_postcode' => $event['event_postcode'], 'location_region' => $event['event_region'], 'location_country' => $event['event_country'], 'location_url' => $event['event_url'], 'location_longitude' => $event['event_longitude'], 'location_latitude' => $event['event_latitude'], 'location_zoom' => $event['event_zoom'], 'location_phone' => $event['event_phone'], 'location_access' => '');
                    mc_insert_location($add);
                }
                // could add delete routine to allow user to select what location to use for events using a given location.
            }
        }
    }
}