/**
 *
 * This is the markup to display in the Multisite Post Duplicator Tools page
 * @since 0.1
 * 
 */
function mpd_admin_menu_markup()
{
    global $wp_post_types;
    $success = false;
    if (isset($_POST['duplicate-submit'])) {
        $mdp_PostType = $_POST['el0'];
        $mdp_PostID = $_POST['el1'];
        $mdp_NewBlog = $_POST['el2'];
        $mdp_userID = $_POST['el3'];
        $mdp_prefix = $_POST['mdp-prefix'];
        $mdp_postStatus = $_POST['mpd-post-status'];
        $new_post_obj = mpd_duplicate_over_multisite($mdp_PostID, $mdp_NewBlog, $mdp_PostType, $mdp_userID, $mdp_prefix, $mdp_postStatus);
        $success = true;
    }
    $post_types = get_post_types();
    $post_types_to_ignore = mpd_get_post_types_to_ignore();
    ob_start();
    ?>

	<div class="wrap">

    	<h2><?php 
    _e('Multisite Post Duplicator', MPD_DOMAIN);
    ?>
</h2>

    	<?php 
    if (!is_multisite()) {
        ?>

    		<h2><?php 
        _e('Attention!', MPD_DOMAIN);
        ?>
</h2>

    		<p><?php 
        _e('At the moment this plugin is solely for funtioning on a mulitisite. It appears this site doees not have multisite enabled', MPD_DOMAIN);
        ?>
</p>
			
			<?php 
        return;
        ?>

		<?php 
    }
    ?>

    	<?php 
    if ($success) {
        ?>

    		<div class="updated">

    			<p><?php 
        _e('Congratulations. The page/post was duplicated successfully.', MPD_DOMAIN);
        ?>
 <a href="<?php 
        echo $new_post_obj['edit_url'];
        ?>
"><?php 
        _e('Edit this post', MPD_DOMAIN);
        ?>
</a></p>

    		</div>

		<?php 
    }
    ?>

    	<form id="thefirstform" action="<?php 
    echo $_SERVER['REQUEST_URI'];
    ?>
" method="post">

    		<div class="metabox">

    			<h2><?php 
    _e('Options', MPD_DOMAIN);
    ?>
</h2>

    			<p><?php 
    _e('Select your preferences for the duplication.', MPD_DOMAIN);
    ?>
</p>

    			<h3><?php 
    _e('Select the status of the new post that will be created.', MPD_DOMAIN);
    ?>
</h3>

    			<?php 
    $post_statuses = get_post_statuses();
    ?>

    			<?php 
    foreach ($post_statuses as $post_status_key => $post_status_value) {
        ?>

    				<input type="radio" name="mpd-post-status" value="<?php 
        echo $post_status_key;
        ?>
" <?php 
        echo $post_status_key == 'draft' ? 'checked' : '';
        ?>
/><?php 
        echo $post_status_value;
        ?>


    			<?php 
    }
    ?>

    			<h3><?php 
    _e('Select a prefix, if any, for the new post/page to be created', MPD_DOMAIN);
    ?>
:</h3>
				
    			<input type="text" name="mdp-prefix" value="<?php 
    echo mpd_get_prefix();
    ?>
"/>
    		
			</div>

			<div class="metabox">

				<h2><?php 
    _e('Process the duplication', MPD_DOMAIN);
    ?>
</h2>

	    		<h3>1. <?php 
    _e('Select the post type of the post you want to duplicate', MPD_DOMAIN);
    ?>
</h3>
	    		
	    		<input type="hidden" name="action" value="add_foobar">

		    	<select name="el0" class="el0" style="width:300px;">

		    		<option></option>

		    		<option value="any" > - <?php 
    _e('All Post Types', MPD_DOMAIN);
    ?>
 -</option>

		    		<?php 
    foreach ($post_types as $post_type) {
        ?>

		    			<?php 
        if (!in_array($post_type, $post_types_to_ignore)) {
            ?>

			    			<option value="<?php 
            echo $post_type;
            ?>
">

			    				<?php 
            echo ucfirst($post_type);
            ?>

			    			</option>
		    			
		    			<?php 
        }
        ?>

					<?php 
    }
    ?>

					<?php 
    wp_reset_postdata();
    ?>

				</select>

				<div class="el0sc spinner-container"><img src="<?php 
    echo plugins_url('../css/select2-spinner.gif', __FILE__);
    ?>
"/></div>

				<div class="el1-container"></div>
		    	
				<div class="el2-container"></div>

				<div class="el3-container"></div>

			</div>

		</form>

	</div>

	<?php 
}
/**
 * @ignore
 */
function mpd_bulk_action()
{
    $wp_list_table = _get_list_table('WP_Posts_List_Table');
    $action = $wp_list_table->current_action();
    if (0 === strpos($action, 'dup')) {
        preg_match("/(?<=dup-)\\d+/", $action, $get_site);
        if (isset($_REQUEST['post'])) {
            $post_ids = array_map('intval', $_REQUEST['post']);
        }
        $results = array();
        foreach ($post_ids as $post_id) {
            $results[] = mpd_duplicate_over_multisite($post_id, $get_site[0], $_REQUEST['post_type'], get_current_user_id(), mpd_get_prefix(), 'draft');
        }
        $countBatch = count($results);
        $destination_name = get_blog_details($get_site[0])->blogname;
        $destination_edit_url = get_admin_url($get_site[0], 'edit.php?post_type=' . $_REQUEST['post_type']);
        $the_ess = $countBatch != 1 ? 'posts have' : 'post has';
        $notice = '<div class="updated"><p>' . $countBatch . " " . $the_ess . " " . __('been duplicated to', MPD_DOMAIN) . " '<a href='" . $destination_edit_url . "'>" . $destination_name . "'</a></p></div>";
        update_option('mpd_admin_bulk_notice', $notice);
    }
}
/**
 * 
 * This function sets up the MPD core function and calls it based on values added by the user in the MPD metabox
 * 
 * @since 0.4
 * @param int $post_id The post ID of the post currently being viewed.
 * @return int The post ID of the post currently being viewed.
 * 
 */
function mpd_clone_post($post_id)
{
    if (!count($_POST)) {
        return $post_id;
    }
    if (isset($_POST["post_status"]) && $_POST["post_status"] != "auto-draft" && isset($_POST['mpd_blogs']) && count($_POST['mpd_blogs']) && $_POST["post_ID"] == $post_id) {
        $mpd_blogs = $_POST['mpd_blogs'];
        foreach ($mpd_blogs as $mpd_blog_id) {
            mpd_duplicate_over_multisite($_POST["ID"], $mpd_blog_id, $_POST["post_type"], get_current_user_id(), $_POST["mpd-prefix"], $_POST["mpd-new-status"]);
        }
    }
    return $post_id;
}