/**
 * @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 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 
}
/**
 * 
 * Create the UI for the Prefix Setting
 * 
 * @since 0.4
 * @return null
 * 
 */
function mdp_default_prefix_render()
{
    $options = get_option('mdp_settings');
    ?>
	<input type='text' name='mdp_settings[mdp_default_prefix]' value='<?php 
    echo mpd_get_prefix();
    ?>
'>

	<p class="mpdtip"><?php 
    _e('Change the default prefix for your duplication across the network.', MPD_DOMAIN);
    ?>
</p>
	<?php 
}
/**
 * 
 * This function generates the markup for the MPD Metabox
 * 
 * @since 0.4
 * @return null
 * 
 */
function mpd_publish_top_right()
{
    $post_statuses = get_post_statuses();
    $sites = mpd_wp_get_sites();
    ?>


    <div id="clone_multisite_box">

        <div class="metabox">

            <?php 
    do_action('mpd_before_metabox_content');
    ?>

            <p><?php 
    _e('Duplicated post status', MPD_DOMAIN);
    ?>
:

            <select id="mpd-new-status" name="mpd-new-status">
             <?php 
    foreach ($post_statuses as $post_status_key => $post_status_value) {
        ?>
                      <option value="<?php 
        echo $post_status_key;
        ?>
" <?php 
        echo $post_status_key == 'draft' ? 'selected' : '';
        ?>
><?php 
        echo $post_status_value;
        ?>
</option>
               <?php 
    }
    ?>
            </select>
               
            </p>

            <p><?php 
    _e('Title prefix for new post', MPD_DOMAIN);
    ?>
:
            
                <input type="text" name="mpd-prefix" value="<?php 
    echo mpd_get_prefix();
    ?>
"/>
                
            </p>

            <p><?php 
    _e('Site(s) you want duplicate to', MPD_DOMAIN);
    ?>
:

                <ul id="mpd_blogschecklist" data-wp-lists="list:category" class="mpd_blogschecklist" style="padding-left: 5px;margin-top: -8px;">
                    
                    <?php 
    foreach ($sites as $site) {
        ?>

                        <?php 
        if (current_user_can_for_blog($site['blog_id'], mpd_get_required_cap())) {
            ?>

                            <?php 
            $blog_details = get_blog_details($site['blog_id']);
            ?>
                            
                                <li id="mpd_blog_<?php 
            echo $site['blog_id'];
            ?>
" class="mpd-site-checkbox">

                                    <label class="selectit">
                                        <input value="<?php 
            echo $site['blog_id'];
            ?>
" type="checkbox" name="mpd_blogs[]" id="in_blog_<?php 
            echo $site['blog_id'];
            ?>
"> <?php 
            echo $blog_details->blogname;
            ?>

                                    </label>

                                </li>
                            
                        <?php 
        }
        ?>

                    <?php 
    }
    ?>

                </ul>
            </p>

            <p>
                <em>
                    <?php 
    _e('If you have checked any of the checkboxes above then this post will be duplicated on save.', MPD_DOMAIN);
    ?>
                </em>
            </p>

            <p style="font-size: 80%; text-align:right; font-style:italic">

                <a target="_blank" href="<?php 
    echo esc_url(get_admin_url(null, 'options-general.php?page=multisite_post_duplicator'));
    ?>
"><?php 
    _e('Settings', MPD_DOMAIN);
    ?>
</a>
                
            </p>

            <?php 
    do_action('mpd_after_metabox_content');
    ?>
            
        </div>

    </div>

<?php 
}