/**
 * Echoes the right link to BuddyDrive root folder regarding to context
 *
 * @uses bp_is_user() to check for user's buddydrive
 * @uses bp_current_action() to check for BuddyDrive nav
 * @uses buddydrive_get_user_buddydrive_url() to print the BuuddyBox user's url
 * @uses buddydrive_get_friends_subnav_slug() to get friends subnav slug
 * @uses buddydrive_get_friends_buddydrive_url() to print the Shared by friends BuddyDrive Url
 * @uses buddydrive_is_group() to check for the BuddyDrive group area
 * @uses buddydrive_get_group_buddydrive_url() to print the BuddyDrive group's url
 * @return the right url
 */
function buddydrive_component_home_url()
{
    if (bp_is_user() && bp_current_action() == 'files') {
        echo buddydrive_get_user_buddydrive_url();
    } else {
        if (bp_is_user() && bp_current_action() == buddydrive_get_friends_subnav_slug()) {
            echo buddydrive_get_friends_buddydrive_url();
        } else {
            if (buddydrive_is_group()) {
                echo buddydrive_get_group_buddydrive_url();
            }
        }
    }
}
/**
 * Adds post datas to include a file / folder to a private message
 *
 * @uses buddydrive_get_buddyfile() gets the BuddyFile Object
 * @uses buddydrive_get_file_post_type() gets the BuddyFile post type
 * @uses buddydrive_get_name() so that it's possible to brand the plugin
 * @uses buddydrive_get_folder_post_type() gets the BuddyFolder post type
 * @uses bp_loggedin_user_id() to get current user id
 * @uses buddydrive_get_user_buddydrive_url() to build the folder url on user's BuddyDrive
 * @return string html output and inputs
 */
function buddydrive_attached_file_to_message()
{
    if (!empty($_REQUEST['buddyitem'])) {
        $link = $buddytype = $password = false;
        $buddyitem = buddydrive_get_buddyfile($_REQUEST['buddyitem'], array(buddydrive_get_file_post_type(), buddydrive_get_folder_post_type()));
        if (!empty($buddyitem->ID)) {
            if ($buddyitem->user_id != bp_loggedin_user_id()) {
                ?>
				<div id="message" class="error"><p><?php 
                _e('Cheating ?', 'buddydrive');
                ?>
</p></div>
				<?php 
                return;
            }
            $link = $buddyitem->link;
            if ($buddyitem->post_type == buddydrive_get_file_post_type()) {
                $displayed_link = $buddyitem->link;
                $buddytype = buddydrive_get_name() . ' File';
                if (!empty($buddyitem->post_parent)) {
                    $parent = buddydrive_get_buddyfile($buddyitem->post_parent, buddydrive_get_folder_post_type());
                    $password = !empty($parent->password) ? $parent->password : false;
                } else {
                    $password = !empty($buddyitem->password) ? $buddyitem->password : false;
                }
            } else {
                $displayed_link = buddydrive_get_user_buddydrive_url(bp_loggedin_user_id()) . '?folder-' . $buddyitem->ID;
                $buddytype = buddydrive_get_name() . ' Folder';
                $password = !empty($buddyitem->password) ? $buddyitem->password : false;
            }
            ?>
			<p>
				<label for="buddyitem-link"><?php 
            printf(__('%s attached : %s', 'buddydrive'), esc_html($buddytype), '<a href="' . esc_url($displayed_link) . '">' . esc_html($buddyitem->title) . '</a>');
            ?>
</label>
				<input type="hidden" value="<?php 
            echo esc_url($link);
            ?>
" id="buddyitem-link" name="_buddyitem_link">
				<input type="hidden" value="<?php 
            echo esc_attr($buddyitem->ID);
            ?>
" id="buddyitem-id" name="_buddyitem_id">

				<?php 
            if (!empty($password)) {
                ?>
					<input type="checkbox" name="_buddyitem_pass" value="1" checked> <?php 
                _e('Automatically add the password in the message', 'buddydrive');
                ?>
				<?php 
            }
            ?>
			</p>
			<?php 
        }
    }
}
/**
 * Handles an embed BuddyDrive item
 *
 * @param array $matches the result of the preg_match
 * @param array $attr
 * @param string $url
 * @param array $rawattr
 * @uses is_multisite() to check for multisite config
 * @uses bp_get_root_blog_id() to get the root blog id
 * @uses switch_to_blog() to change for root blog id
 * @uses buddydrive_get_buddyfile() to get the BuddyDrive Item
 * @uses buddydrive_get_file_post_type() to get the BuddyFile post type
 * @uses wp_mime_type_icon() to get the WordPress crystal icon
 * @uses buddydrive_get_folder_post_type() to get the BuddyFolder post type
 * @uses buddydrive_get_group_buddydrive_url() to build the url to the BuddyDrive group
 * @uses buddydrive_get_user_buddydrive_url() to get the user's BuddyDrive url
 * @uses buddydrive_get_images_url() to get the image url of the plugin
 * @uses the BuddyDrive Loop and some tempkate tags
 * @uses wp_reset_postdata() to avoid some weird link..
 * @uses restore_current_blog() to restore the child blog.
 * @return string $embed the html output
 */
function wp_embed_handler_buddydrive($matches, $attr, $url, $rawattr)
{
    $link = $title = $icon = $content = $mime_type = $filelist = $hw_attr = false;
    $current_blog = get_current_blog_id();
    if (is_multisite() && (int) $current_blog !== (int) bp_get_root_blog_id()) {
        switch_to_blog(bp_get_root_blog_id());
    }
    if ($matches[1] == 'file') {
        $buddyfile = buddydrive_get_buddyfile($matches[2], buddydrive_get_file_post_type());
        if (empty($buddyfile)) {
            return '';
        }
        $link = $buddyfile->link;
        $title = $buddyfile->title;
        $content = $buddyfile->content;
        $mime_type = $buddyfile->mime_type;
        $icon = wp_mime_type_icon($buddyfile->ID);
        if ('public' === $buddyfile->check_for) {
            $thumbnail = buddydrive_get_thumbnail($buddyfile->ID, 'thumburl', false);
            if (!empty($thumbnail[0])) {
                $icon = $thumbnail[0];
                $hw_attr = image_hwstring($thumbnail[1], $thumbnail[2]);
            }
        }
        // It's a folfer
    } else {
        $buddyfile = buddydrive_get_buddyfile($matches[2], buddydrive_get_folder_post_type());
        if (empty($buddyfile)) {
            return '';
        }
        $buddydrive_root_link = $buddyfile->check_for == 'groups' ? buddydrive_get_group_buddydrive_url($buddyfile->group) : buddydrive_get_user_buddydrive_url($buddyfile->user_id);
        $link = $buddydrive_root_link . '?folder-' . $buddyfile->ID;
        $title = $buddyfile->title;
        $mime_type = $buddyfile->mime_type;
        $icon = buddydrive_get_images_url() . 'folder.png';
    }
    $embed = '<table style="width:auto"><tr>';
    $tdwidth = 'width:60px;';
    if (!empty($hw_attr)) {
        $tdwidth = '';
    }
    $embed .= '<td style="vertical-align:middle;' . $tdwidth . '"><a href="' . esc_url($link) . '" title="' . esc_attr($title) . '"><img src="' . esc_url($icon) . '" alt="' . esc_attr($mime_type) . '" class="buddydrive-thumb" ' . $hw_attr . '></a></td>';
    $embed .= '<td style="vertical-align:middle"><h6 style="margin:0"><a href="' . esc_url($link) . '" title="' . esc_attr($title) . '">' . esc_html($title) . '</a></h6>';
    if (!empty($content)) {
        $embed .= '<p style="margin:0">' . esc_html($content) . '</p>';
    }
    if ($matches[1] == 'folder') {
        global $buddydrive_template;
        if (buddydrive_has_items(array('buddydrive_parent' => $buddyfile->ID))) {
            $filelist = '<p style="margin-top:1em;margin-bottom:0">' . esc_html__('Files included in this folder :', 'buddydrive') . '</p><ul>';
            while (buddydrive_has_items()) {
                buddydrive_the_item();
                $filelist .= '<li><a href="' . esc_url(buddydrive_get_action_link()) . '" title="' . esc_attr(buddydrive_get_item_title()) . '">' . esc_html(buddydrive_get_item_title()) . '</a></li>';
            }
            $filelist .= '</ul>';
            $buddydrive_template = false;
        }
        wp_reset_postdata();
        $embed .= $filelist;
    }
    $embed .= '</td></tr></table>';
    if (is_multisite() && (int) $current_blog !== (int) bp_get_root_blog_id()) {
        restore_current_blog();
    }
    return apply_filters('embed_buddydrive', $embed, $matches, $attr, $url, $rawattr);
}