/**
 * 
 * Add Multisite Post Duplucator Tool Page to WordPress Navigation
 * @since 0.1
 * 
 */
function mpd_admin_pages()
{
    $active_mpd = apply_filters('mpd_is_active', true);
    if ($active_mpd && current_user_can(mpd_get_required_cap())) {
        add_submenu_page('tools.php', __('Multisite Post Duplicator', MPD_DOMAIN), __('Multisite Post Duplicator', MPD_DOMAIN), 'manage_options', 'mpd', 'mpd_admin_menu_markup');
    }
}
/**
 * @ignore
 */
function mpd_bulk_admin_script()
{
    if (is_multisite()) {
        $defaultoptions = mdp_get_default_options();
        $sites = mpd_wp_get_sites();
        $options = get_option('mdp_settings');
        $post_status = isset($_REQUEST["post_status"]) ? $_REQUEST["post_status"] : null;
        $active_mpd = apply_filters('mpd_is_active', true);
        if (isset($options['add_bulk_settings']) || $defaultoptions['add_bulk_settings'] == 'allow-batch' && !$options) {
            ?>

          <?php 
            if (!$post_status && $active_mpd) {
                ?>

            <script type="text/javascript">

              jQuery(document).ready(function() {

                <?php 
                foreach ($sites as $site) {
                    ?>

                  <?php 
                    $blog_details = get_blog_details($site['blog_id']);
                    ?>
 

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

                      jQuery('<option>').val("dup-<?php 
                        echo $site['blog_id'];
                        ?>
").text('<?php 
                        _e('Duplicate to ');
                        echo $blog_details->blogname;
                        ?>
').appendTo("select[name='action']");
                      jQuery('<option>').val("dup-<?php 
                        echo $site['blog_id'];
                        ?>
").text('<?php 
                        _e('Duplicate to ');
                        echo $blog_details->blogname;
                        ?>
').appendTo("select[name='action2']");

                    <?php 
                    }
                    ?>
                    
                  <?php 
                }
                ?>

              });
              
            </script>

          <?php 
            }
            ?>

        <?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 
}