/**
 * Post template functions.  In this plugin, both "topics" and "replies" are technically custom post types. 
 * This file exists so that we can consolidate some of these template functions into one.  For more-specific 
 * template tags that apply to topics and replies, see `template-topic.php` and `template-post.php`.
 *
 * Technically, you could use WP's built-in functions for getting the data needed because most of these 
 * functions are simply wrappers for those functions.  However, this is discouraged because there are 
 * certain hooks that will be executed when using these functions.
 */
function mb_get_content_type($post_id = 0)
{
    $post_type = get_post_type($post_id);
    if (mb_is_forum($post_id)) {
        $type = 'forum';
    } elseif (mb_is_topic($post_id)) {
        $type = 'topic';
    } elseif (mb_is_reply($post_id)) {
        $type = 'reply';
    } else {
        $type = get_post_type($post_id);
    }
    return $type;
}
/**
 * Overwrites capabilities in certain scenarios.
 *
 * @since  1.0.0
 * @access public
 * @param  array   $caps
 * @param  string  $cap
 * @param  int     $user_id
 * @param  array   $args
 * @return array
 */
function mb_forum_map_meta_cap($caps, $cap, $user_id, $args)
{
    /* Checks if a user can read a specific forum. */
    if ('read_post' === $cap && mb_is_forum($args[0])) {
        $post = get_post($args[0]);
        if ($user_id != $post->post_author) {
            $parent_id = $post->post_parent;
            /* If we have a parent forum and the user can't read it, don't allow reading this forum. */
            if (0 < $parent_id && !mb_user_can($user_id, 'read_forum', $parent_id)) {
                $caps = array('do_not_allow');
                /* If the user can read the parent forum, check if they can read this one. */
            } else {
                $post_type = get_post_type_object($post->post_type);
                $post_status = mb_get_forum_status($post->ID);
                $status_obj = get_post_status_object($post_status);
                if (mb_get_hidden_post_status() === $status_obj->name) {
                    $caps[] = $post_type->cap->read_hidden_forums;
                } elseif (mb_get_private_post_status() === $status_obj->name) {
                    $caps[] = $post_type->cap->read_private_posts;
                } elseif ($post_type->cap->read !== $post_type->cap->read_others_forums) {
                    $caps[] = $post_type->cap->read_others_forums;
                } else {
                    $caps = array();
                }
            }
        } else {
            $caps = array();
        }
        /* Meta cap for editing a single forum. */
    } elseif ('edit_post' === $cap && mb_is_forum($args[0])) {
        $post = get_post($args[0]);
        $forum_obj = get_post_type_object(mb_get_forum_post_type());
        if ($user_id != $post->post_author) {
            // Open forums.
            if (mb_is_forum_open($args[0])) {
                $caps[] = $forum_obj->cap->edit_open_forums;
            } elseif (mb_is_forum_closed($args[0])) {
                $caps[] = $forum_obj->cap->edit_closed_forums;
            } elseif (mb_is_forum_hidden($args[0])) {
                $caps[] = $forum_obj->cap->edit_hidden_forums;
            }
        }
        /* Meta cap for opening a single forum. */
    } elseif ('open_forum' === $cap) {
        $caps = array();
        $caps[] = user_can($user_id, 'edit_forum', $args[0]) ? 'open_forums' : 'do_not_allow';
        /* Meta cap for closing a single forum. */
    } elseif ('close_forum' === $cap) {
        $caps = array();
        $caps[] = user_can($user_id, 'edit_forum', $args[0]) ? 'close_forums' : 'do_not_allow';
        /* Meta cap for privatizing a single forum. */
    } elseif ('privatize_forum' === $cap) {
        $caps = array();
        $caps[] = user_can($user_id, 'edit_forum', $args[0]) ? 'privatize_forums' : 'do_not_allow';
        /* Meta cap for hiding a single forum. */
    } elseif ('hide_forum' === $cap) {
        $caps = array();
        $caps[] = user_can($user_id, 'edit_forum', $args[0]) ? 'hide_forums' : 'do_not_allow';
        /* Meta cap for spamming a single forum. */
    } elseif ('archive_forum' === $cap) {
        $caps = array();
        $caps[] = user_can($user_id, 'edit_forum', $args[0]) ? 'archive_forums' : 'do_not_allow';
        /* Meta cap for deleting a specific forum. */
    } elseif ('delete_post' === $cap && mb_is_forum($args[0])) {
        $forum_id = mb_get_forum_id($args[0]);
        if (mb_get_default_forum_id() === $forum_id) {
            $caps = array('do_not_allow');
        }
        /* Meta cap check for accessing the forum form. */
    } elseif ('access_forum_form' === $cap) {
        $caps = array('create_forums');
        /* If this is a single forum page, check if user can create sub-forums. */
        if (mb_is_single_forum()) {
            $forum_id = mb_get_forum_id();
            if (!current_user_can('read_forum', $forum_id)) {
                $caps[] = 'do_not_allow';
            } elseif (!mb_forum_allows_subforums($forum_id)) {
                $caps[] = 'do_not_allow';
            }
        } elseif (mb_is_forum_edit() && !user_can($user_id, 'edit_post', mb_get_forum_id())) {
            $caps[] = 'do_not_allow';
        }
    }
    return $caps;
}
			</tr>
		</tfoot>

		<tbody>
			<?php 
    while (mb_search_query()) {
        // Begins the loop through found posts.
        ?>

				<?php 
        mb_the_search_result();
        // Loads the post data.
        ?>

				<?php 
        if (mb_is_forum()) {
            ?>

					<tr <?php 
            post_class();
            ?>
>
						<td class="mb-col-title">
							<?php 
            mb_forum_link();
            ?>
							<div class="mb-forum-summary">
								<?php 
            the_excerpt();
            ?>
							</div><!-- .entry-meta -->
/**
 * Filter on the post type link for forums. If the user doesn't have permission to view the forum, 
 * return an empty string.
 *
 * @since  1.0.0
 * @access public
 * @param  string  $link
 * @param  object  $post
 * @return string
 */
function mb_forum_post_type_link($link, $post)
{
    return mb_is_forum($post->ID) && !current_user_can('read_forum', $post->ID) ? '' : $link;
}
/**
 * Returns the forum ID.
 *
 * @since  1.0.0
 * @access public
 * @param  int     $forum_id
 * @return int
 */
function mb_get_forum_id($forum_id = 0)
{
    $mb = message_board();
    if (is_numeric($forum_id) && 0 < $forum_id) {
        $_forum_id = $forum_id;
    } elseif ($mb->subforum_query->in_the_loop && isset($mb->subforum_query->post->ID)) {
        $_forum_id = $mb->subforum_query->post->ID;
    } elseif ($mb->forum_query->in_the_loop && isset($mb->forum_query->post->ID)) {
        $_forum_id = $mb->forum_query->post->ID;
    } elseif ($mb->search_query->in_the_loop && isset($mb->search_query->post->ID) && mb_is_forum($mb->search_query->post->ID)) {
        $_forum_id = $mb->search_query->post->ID;
    } elseif (mb_is_single_forum()) {
        $_forum_id = get_queried_object_id();
    } elseif (get_query_var('forum_id')) {
        $_forum_id = get_query_var('forum_id');
    } else {
        $_forum_id = 0;
    }
    return apply_filters('mb_get_forum_id', absint($_forum_id), $forum_id);
}
/**
 * Custom "enter title here" text.
 *
 * @since  1.0.0
 * @access public
 * @param  string  $title
 * @param  object  $post
 * @return string
 */
function mb_enter_title_here($title, $post)
{
    if (mb_is_forum($post->ID)) {
        $title = mb_get_forum_label('mb_form_title_placeholder');
    } elseif (mb_is_topic($post->ID)) {
        $title = mb_get_topic_label('mb_form_title_placeholder');
    }
    return $title;
}
/**
 * Filters the edit post link for front-end editing.
 *
 * @since  1.0.0
 * @access public
 * @param  string  $url
 * @param  int     $post_id
 */
function mb_get_edit_post_link($url, $post_id)
{
    if (is_admin()) {
        return $url;
    }
    if (mb_is_forum($post_id)) {
        $url = add_query_arg(array('mb_action' => 'edit', 'forum_id' => $post_id), mb_get_board_home_url());
    } elseif (mb_is_topic($post_id)) {
        $url = add_query_arg(array('mb_action' => 'edit', 'topic_id' => $post_id), mb_get_board_home_url());
    } elseif (mb_is_reply($post_id)) {
        $url = add_query_arg(array('mb_action' => 'edit', 'reply_id' => $post_id), mb_get_board_home_url());
    }
    return $url;
}