function edit_my_calendar()
{
    global $current_user, $wpdb, $users_entries;
    $mcdb = $wpdb;
    if (get_option('ko_calendar_imported') != 'true') {
        if (function_exists('check_calendar')) {
            echo "<div id='message'class='updated'>";
            echo "<p>";
            _e('My Calendar has identified that you have the Calendar plugin by Kieran O\'Shea installed. You can import those events and categories into the My Calendar database. Would you like to import these events?', 'my-calendar');
            echo "</p>";
            ?>
			<form method="post" action="<?php 
            echo admin_url('admin.php?page=my-calendar-config');
            ?>
">
				<div><input type="hidden" name="_wpnonce"
				            value="<?php 
            echo wp_create_nonce('my-calendar-nonce');
            ?>
"/>
				</div>
				<div>
					<input type="hidden" name="import" value="true"/>
					<input type="submit" value="<?php 
            _e('Import from Calendar', 'my-calendar');
            ?>
"
					       name="import-calendar" class="button-primary"/>
				</div>
			</form>
			<?php 
            echo "<p>";
            _e('Although it is possible that this import could fail to import your events correctly, it should not have any impact on your existing Calendar database. If you encounter any problems, <a href="http://www.joedolson.com/contact.php">please contact me</a>!', 'my-calendar');
            echo "</p>";
            echo "</div>";
        }
    }
    $action = !empty($_POST['event_action']) ? $_POST['event_action'] : '';
    $event_id = !empty($_POST['event_id']) ? $_POST['event_id'] : '';
    if (isset($_GET['mode'])) {
        $action = $_GET['mode'];
        if ($action == 'edit' || $action == 'copy') {
            $event_id = (int) $_GET['event_id'];
        }
    }
    if (isset($_POST['event_action'])) {
        $nonce = $_REQUEST['_wpnonce'];
        if (!wp_verify_nonce($nonce, 'my-calendar-nonce')) {
            die("Security check failed");
        }
        global $mc_output;
        $count = 0;
        if (isset($_POST['event_begin']) && is_array($_POST['event_begin'])) {
            $count = count($_POST['event_begin']);
        } else {
            $response = my_calendar_save($action, $mc_output, (int) $_POST['event_id']);
            echo $response['message'];
        }
        for ($i = 0; $i < $count; $i++) {
            $mc_output = mc_check_data($action, $_POST, $i);
            if ($action == 'add' || $action == 'copy') {
                $response = my_calendar_save($action, $mc_output);
            } else {
                $response = my_calendar_save($action, $mc_output, (int) $_POST['event_id']);
            }
            echo $response['message'];
        }
        if (isset($_POST['ref'])) {
            $url = esc_url(urldecode($_POST['ref']));
            echo "<p class='return'><a href='{$url}'>" . __('Return to Calendar', 'my-calendar') . "</a></p>";
        }
    }
    ?>

	<div class="wrap jd-my-calendar">
	<?php 
    my_calendar_check_db();
    if (get_site_option('mc_multisite') == 2) {
        if (get_option('mc_current_table') == 0) {
            $message = __('Currently editing your local calendar', 'my-calendar');
        } else {
            $message = __('Currently editing your central calendar', 'my-calendar');
        }
        echo "<div class='message updated'><p>{$message}</p></div>";
    }
    if ($action == 'edit') {
        ?>
		<h1><?php 
        _e('Edit Event', 'my-calendar');
        ?>
</h1>
		<?php 
        if (empty($event_id)) {
            echo "<div class='error'><p>" . __("You must provide an event id in order to edit it", 'my-calendar') . "</p></div>";
        } else {
            mc_edit_event_form('edit', $event_id);
        }
    } else {
        if ($action == 'copy') {
            ?>
		<h1><?php 
            _e('Copy Event', 'my-calendar');
            ?>
</h1>
		<?php 
            if (empty($event_id)) {
                echo "<div class=\"error\"><p>" . __("You must provide an event id in order to edit it", 'my-calendar') . "</p></div>";
            } else {
                mc_edit_event_form('copy', $event_id);
            }
        } else {
            ?>
		<h1><?php 
            _e('Add Event', 'my-calendar');
            ?>
</h1><?php 
            mc_edit_event_form();
        }
    }
    mc_show_sidebar();
    ?>
	</div><?php 
}
function mcs_processor($post)
{
    if (isset($post['mcs_submission'])) {
        $attach_id = false;
        $nonce = $post['event_nonce_name'];
        if (!wp_verify_nonce($nonce, 'event_nonce')) {
            return;
        }
        // honeypot - only bots should complete this field;
        $honeypot = isset($_POST['your_name']) && $_POST['your_name'] != '' ? true : false;
        if ($honeypot) {
            return;
        }
        // if files being uploaded, upload file and convert to a string for $post
        if (!empty($_FILES['event_image'])) {
            require_once ABSPATH . '/wp-admin/includes/file.php';
            require_once ABSPATH . '/wp-admin/includes/image.php';
            $file = $_FILES['event_image'];
            $upload = wp_handle_upload($file, array('test_form' => false));
            if (!isset($upload['error']) && isset($upload['file'])) {
                $filetype = wp_check_filetype(basename($upload['file']), null);
                $title = $file['name'];
                $ext = strrchr($title, '.');
                $title = $ext !== false ? substr($title, 0, -strlen($ext)) : $title;
                $attachment = array('post_mime_type' => $filetype['type'], 'post_title' => addslashes($title), 'post_content' => '', 'post_status' => 'inherit');
                $alt = isset($_POST['event_image_alt']) ? sanitize_text_field($_POST['event_image_alt']) : '';
                $attach_id = wp_insert_attachment($attachment, $upload['file']);
                update_post_meta($attach_id, '_wp_attachment_image_alt', $alt);
                $attach_data = wp_generate_attachment_metadata($attach_id, $upload['file']);
                wp_update_attachment_metadata($attach_id, $attach_data);
                $post['event_image'] = $upload['url'];
            }
        }
        // end file upload
        $check = mc_check_data('add', $post, 0);
        $message = '';
        if (mcs_payment_required()) {
            $key = isset($post['mcs_key']) ? $post['mcs_key'] : false;
            $quantity = mcs_check_key($key);
            if (!$quantity) {
                $reason = mcs_key_status($key);
                return array("<div class='notice error'><p>" . sprintf(__('That was not a valid payment key: %s', 'my-calendar-submissions'), $reason) . "</p></div>", $check[1], false);
            } else {
                $message = sprintf("<div class='notice error'><p>" . __('%d submissions remaining with this payment key.', 'my-calendar-submissions') . "</p></div>", $quantity - 1);
            }
        }
        if ($check[0]) {
            if (!isset($_POST['event_edit'])) {
                $response = my_calendar_save('add', $check);
                $action = 'add';
            } else {
                $response = my_calendar_save('edit', $check, (int) $_POST['event_edit']);
                $action = 'edit';
            }
            $event_id = $response['event_id'];
            $response = $response['message'];
            $event = mc_get_event_core($event_id);
            $post_id = $event->event_post;
            set_post_thumbnail($post_id, $attach_id);
            if ($message != '') {
                $response .= " {$message}";
            }
            $return = array($response, array(), true);
        } else {
            $return = array($check[3], $check[1], false);
            return $return;
        }
        if ($event_id) {
            $name = $post['mcs_name'];
            $email = $post['mcs_email'];
            if (mcs_payment_required()) {
                // Note: payments will be processed on both submissions & on edits.
                mcs_update_key_quantity($key, $quantity);
            }
            // if no errors and NOT SPAM send notifications.
            if (mc_event_is_spam($event_id)) {
                do_action('mcs_spam_submission', $name, $email, $event_id);
            } else {
                do_action('mcs_complete_submission', $name, $email, $event_id, $action);
            }
        }
        return $return;
    } else {
        return false;
    }
}
function mcs_import_event($event)
{
    $check = mc_check_data('add', $event, 0);
    if ($check[0]) {
        $response = my_calendar_save('add', $check);
        $event_id = $response['event_id'];
        $response = $response['message'];
        if (isset($event['event_image']) && $event['event_image'] != '') {
            $e = mc_get_event_core($event_id);
            $post_id = $e->event_post;
            $image = media_sideload_image($event['event_image'], $post_id);
            $media = get_attached_media('image', $post_id);
            $attach = array_shift($media);
            $attach_id = $attach->ID;
            set_post_thumbnail($post_id, $attach_id);
        }
    }
}
function mc_save_event_post($id)
{
    $post_types = is_array(get_option('mcs_post_event_types')) ? get_option('mcs_post_event_types') : array();
    $is_valid_type = !empty($post_types) && in_array(get_post_type($id), $post_types) ? true : false;
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE || wp_is_post_revision($id) || !$is_valid_type) {
        return $id;
    }
    if (isset($_POST['event_nonce_name']) && isset($_POST['event_source']) && $_POST['event_source'] == 'post') {
        if (!isset($_POST['event_title']) || empty($_POST['event_title'])) {
            return $id;
        }
        $post = $_POST;
        if (isset($_POST['event_desc'])) {
            $post['content'] = $_POST['event_desc'];
        } else {
            $post['content'] = '';
        }
        $attach_id = get_post_thumbnail_id($id);
        $featured_image = wp_get_attachment_url($attach_id);
        if (isset($_POST['post_author_override'])) {
            $_POST['event_author'] = intval($_POST['post_author_override']);
        } else {
            $_POST['event_author'] = get_current_user_id();
        }
        $check = mc_check_data('add', $post, 0);
        if ($check[0]) {
            $response = my_calendar_save('add', $check);
            $event_id = $response['event_id'];
            $response = $response['message'];
            $event = mc_get_first_event($event_id);
            $post_id = $event->event_post;
            set_post_thumbnail($post_id, $attach_id);
            mc_update_data($event_id, 'event_image', $featured_image, '%s');
            update_post_meta($id, '_mc_new_event', $response);
            update_post_meta($id, '_mc_generated_event', $event_id);
        }
    }
    return $id;
}