/**
  * Overwrites the `do_trail_items()` method and creates custom trail items.
  *
  * @since  1.0.0
  * @access public
  * @return void
  */
 public function do_trail_items()
 {
     /* Add the network and site home links. */
     $this->do_network_home_link();
     $this->do_site_home_link();
     $this->mb_do_board_home_link();
     /* Single forum, topic, or reply. */
     if (mb_is_single_forum() || mb_is_single_topic() || mb_is_single_reply()) {
         $this->do_singular_items();
     } elseif (mb_is_forum_archive() || mb_is_topic_archive() || mb_is_reply_archive()) {
         $this->do_post_type_archive_items();
     } elseif (mb_is_role_archive()) {
         $this->mb_do_user_archive_link();
         if (is_paged()) {
             $this->items[] = sprintf('<a href="%s">%s</a>', mb_get_role_archive_url(), mb_get_role_archive_title());
         } elseif ($this->args['show_title']) {
             $this->items[] = mb_get_role_archive_title();
         }
     } elseif (mb_is_single_role()) {
         $this->mb_do_user_archive_link();
         $this->mb_do_role_archive_link();
         if (is_paged()) {
             $this->items[] = sprintf('<a href="%s">%s</a>', mb_get_role_url(), mb_get_single_role_title());
         } elseif ($this->args['show_title']) {
             $this->items[] = mb_get_single_role_title();
         }
     } elseif (mb_is_user_archive()) {
         if (is_paged()) {
             $this->items[] = sprintf('<a href="%s">%s</a>', mb_get_user_archive_url(), mb_get_user_archive_title());
         } elseif ($this->args['show_title']) {
             $this->items[] = mb_get_user_archive_title();
         }
     } elseif (mb_is_single_user()) {
         $this->mb_do_user_archive_link();
         /* If single user subpage. */
         if (mb_is_user_page()) {
             $this->items[] = sprintf('<a href="%s">%s</a>', mb_get_user_url(), get_the_author_meta('display_name', mb_get_user_id()));
             if (is_paged()) {
                 if (mb_is_user_forums()) {
                     $this->items[] = sprintf('<a href="%s">%s</a>', mb_get_user_forums_url(), mb_get_user_forums_title());
                 } elseif (mb_is_user_topics()) {
                     $this->items[] = sprintf('<a href="%s">%s</a>', mb_get_user_topics_url(), mb_get_user_topics_title());
                 } elseif (mb_is_user_replies()) {
                     $this->items[] = sprintf('<a href="%s">%s</a>', mb_get_user_replies_url(), mb_get_user_replies_title());
                 } elseif (mb_is_user_bookmarks()) {
                     $this->items[] = sprintf('<a href="%s">%s</a>', mb_get_user_bookmarks_url(), mb_get_user_bookmarks_title());
                 } elseif (mb_is_user_forum_subscriptions()) {
                     $this->items[] = sprintf('<a href="%s">%s</a>', mb_get_user_forum_subscriptions_url(), mb_get_user_forum_subscriptions_title());
                 } elseif (mb_is_user_topic_subscriptions()) {
                     $this->items[] = sprintf('<a href="%s">%s</a>', mb_get_user_topic_subscriptions_url(), mb_get_user_topic_subscriptions_title());
                 }
             } elseif ($this->args['show_title']) {
                 $this->items[] = mb_get_user_page_title();
             }
             /* If viewing the single user page but not a subpage. */
         } elseif ($this->args['show_title']) {
             $this->items[] = mb_get_single_user_title();
         }
         /* Login page. */
     } elseif (mb_is_forum_login()) {
         $this->items[] = mb_get_login_page_title();
     }
     /* Add paged items. */
     $this->do_paged_items();
     /* Return the board breadcrumb trail items. */
     $this->items = apply_filters('mb_get_breadcrumb_trail_items', $this->items, $this->args);
 }
/**
 * Builds the template hierarchy for the plugin.  This function figures out what the current page 
 * is and returns an array of possible templates to use.  Note that this function only returns 
 * the templates name and not a full paths.  It is meant to be used within other functions that actually 
 * locate/load the templates.
 *
 * @since  1.0.0
 * @access public
 * @return array
 */
function mb_get_template_hierarchy()
{
    $hierarchy = array();
    /* If viewing a single forum page. */
    if (mb_is_single_forum()) {
        $hierarchy[] = 'single-forum.php';
        /* If viewing the forum archive (default forum front). */
    } elseif (mb_is_forum_archive()) {
        $hierarchy[] = 'archive-forum.php';
        /* If viewing a single topic. */
    } elseif (mb_is_single_topic()) {
        $hierarchy[] = "single-topic.php";
        /* If viewing the topic archive (possible forum front page). */
    } elseif (mb_is_topic_archive()) {
        $hierarchy[] = 'archive-topic.php';
        /* If viewing a single reply. */
    } elseif (mb_is_single_reply()) {
        $hierarchy[] = "single-reply.php";
        /* If viewing the reply archive. */
    } elseif (mb_is_reply_archive()) {
        $hierarchy[] = 'archive-reply.php';
    } elseif (mb_is_role_archive()) {
        $hierarchy[] = 'archive-role.php';
    } elseif (mb_is_single_role()) {
        $hierarchy[] = 'single-role.php';
        /* If viewing a user sub-page. */
    } elseif (mb_is_user_page()) {
        $page = sanitize_key(get_query_var('mb_user_page'));
        $hierarchy[] = "single-user-{$page}.php";
        $hierarchy[] = 'single-user.php';
        /* If viewing a user profile page. */
    } elseif (mb_is_single_user()) {
        $hierarchy[] = 'single-user.php';
        /* If viewing the user archive. */
    } elseif (mb_is_user_archive()) {
        $hierarchy[] = 'archive-user.php';
        /* If viewing a search results page. */
    } elseif (mb_is_search_results()) {
        $hierarchy[] = 'search-results.php';
        /* If viewing the advanced search page. */
    } elseif (mb_is_search()) {
        $hierarchy[] = 'search.php';
        /* If viewing the forum login page. */
    } elseif (mb_is_forum_login()) {
        $hierarchy[] = 'login.php';
        /* If viewing an edit page. */
    } elseif (mb_is_edit()) {
        if (mb_is_forum_edit()) {
            $hierarchy[] = 'edit-forum.php';
        } elseif (mb_is_topic_edit()) {
            $hierarchy[] = 'edit-topic.php';
        } elseif (mb_is_reply_edit()) {
            $hierarchy[] = 'edit-reply.php';
        } elseif (mb_is_user_edit()) {
            $hierarchy[] = 'edit-user.php';
        }
        $hierarchy[] = 'edit.php';
    }
    /* Add the fallback template. */
    $hierarchy[] = 'board.php';
    return apply_filters('mb_get_template_hierarchy', $hierarchy);
}
/**
 * Returns a topic's labels.
 *
 * @since  1.0.0
 * @access public
 * @return string
 */
function mb_get_topic_states($topic_id = 0)
{
    $topic_id = mb_get_topic_id($topic_id);
    $labels = array();
    if (mb_is_topic_super($topic_id) && (mb_is_topic_archive() || mb_is_single_forum())) {
        $labels['super'] = __('[Sticky]', 'message-board');
    } elseif (mb_is_topic_sticky($topic_id) && mb_is_single_forum()) {
        $labels['sticky'] = __('[Sticky]', 'message-board');
    }
    if (mb_is_topic_closed($topic_id)) {
        $labels['closed'] = __('[Closed]', 'message-board');
    }
    $labels = apply_filters('mb_topic_labels', $labels, $topic_id);
    if (!empty($labels)) {
        $formatted = '';
        foreach ($labels as $key => $value) {
            $formatted .= sprintf('<span class="topic-label %s">%s</span> ', sanitize_html_class("topic-label-{$key}"), $value);
        }
        return sprintf('<span class="topic-labels">%s</span>', $formatted);
    }
    return '';
}
/**
 * Overwrites the main query depending on the situation.
 *
 * @since  1.0.0
 * @access public
 * @param  object  $query
 * @return void
 */
function mb_pre_get_posts($query)
{
    /* If viewing an admin page or this isn't the main query, bail. */
    if (is_admin() || !$query->is_main_query()) {
        return;
    }
    /* If viewing the forum archive page. */
    if (mb_is_forum_archive()) {
        $statuses = array(mb_get_open_post_status(), mb_get_close_post_status(), mb_get_publish_post_status(), mb_get_private_post_status(), mb_get_archive_post_status());
        if (current_user_can('read_hidden_forums')) {
            $statuses[] = mb_get_hidden_post_status();
        }
        $query->set('post_type', mb_get_forum_post_type());
        $query->set('post_status', $statuses);
        $query->set('posts_per_page', mb_get_forums_per_page());
        $query->set('orderby', array('menu_order' => 'ASC', 'title' => 'ASC'));
        $query->set('post_parent', 0);
        add_filter('the_posts', 'mb_posts_hierarchy_filter', 10, 2);
    } elseif (mb_is_topic_archive()) {
        $statuses = array(mb_get_open_post_status(), mb_get_close_post_status(), mb_get_publish_post_status(), mb_get_private_post_status());
        if (current_user_can('read_hidden_topics')) {
            $statuses[] = mb_get_hidden_post_status();
        }
        $query->set('post_type', mb_get_topic_post_type());
        $query->set('post_status', $statuses);
        $query->set('posts_per_page', mb_get_topics_per_page());
        $query->set('order', 'DESC');
        $query->set('orderby', 'menu_order');
        add_filter('the_posts', 'mb_posts_super_filter', 10, 2);
    } elseif (mb_is_user_page()) {
        /* Single user forums created page. */
        if (mb_is_user_page('forums')) {
            $query->set('post_type', mb_get_forum_post_type());
            $query->set('posts_per_page', mb_get_forums_per_page());
            $query->set('order', 'ASC');
            $query->set('orderby', 'title');
            /* Single user topics created page. */
        } elseif (mb_is_user_page('topics')) {
            $query->set('post_type', mb_get_topic_post_type());
            $query->set('posts_per_page', mb_get_topics_per_page());
            $query->set('order', 'DESC');
            $query->set('orderby', 'menu_order');
            /* Single user replies created page. */
        } elseif (mb_is_user_page('replies')) {
            $query->set('post_type', mb_get_reply_post_type());
            $query->set('posts_per_page', mb_get_replies_per_page());
            $query->set('order', 'DESC');
            $query->set('orderby', 'date');
            /* Single user bookmarks saved page. */
        } elseif (mb_is_user_page('bookmarks')) {
            $user = get_user_by('slug', get_query_var('author_name'));
            $favs = get_user_meta($user->ID, mb_get_user_topic_bookmarks_meta_key(), true);
            $favs = wp_parse_id_list($favs);
            /* Empty array with `post_in` hack. @link https://core.trac.wordpress.org/ticket/28099 */
            if (empty($favs)) {
                $favs = array(0);
            }
            $query->set('post__in', $favs);
            $query->set('post_type', mb_get_topic_post_type());
            $query->set('posts_per_page', mb_get_topics_per_page());
            $query->set('order', 'DESC');
            $query->set('orderby', 'menu_order');
            add_filter('posts_where', 'mb_auth_posts_where', 10, 2);
            /* Single user topic subscriptions page. */
        } elseif (mb_is_user_page('topic-subscriptions')) {
            $user = get_user_by('slug', get_query_var('author_name'));
            $subs = mb_get_user_topic_subscriptions($user->ID);
            /* Empty array with `post_in` hack. @link https://core.trac.wordpress.org/ticket/28099 */
            if (empty($subs)) {
                $subs = array(0);
            }
            $query->set('post__in', $subs);
            $query->set('post_type', mb_get_topic_post_type());
            $query->set('posts_per_page', mb_get_topics_per_page());
            $query->set('order', 'DESC');
            $query->set('orderby', 'menu_order');
            add_filter('posts_where', 'mb_auth_posts_where', 10, 2);
            /* Single user forum subscriptions page. */
        } elseif (mb_is_user_page('forum-subscriptions')) {
            $user = get_user_by('slug', get_query_var('author_name'));
            $subs = mb_get_user_forum_subscriptions($user->ID);
            /* Empty array with `post_in` hack. @link https://core.trac.wordpress.org/ticket/28099 */
            if (empty($subs)) {
                $subs = array(0);
            }
            $query->set('post__in', $subs);
            $query->set('post_type', mb_get_forum_post_type());
            $query->set('posts_per_page', mb_get_forums_per_page());
            $query->set('order', 'DESC');
            $query->set('orderby', 'menu_order');
            add_filter('posts_where', 'mb_auth_posts_where', 10, 2);
        }
    } elseif (mb_is_search_results()) {
        $post_type = $query->get('post_type');
        /* If not searching a specific post type, make sure to search all forum-related post types. */
        if (empty($post_type) || 'any' === $post_type || !isset($_GET['mb_search_mode'])) {
            $query->set('post_type', array(mb_get_forum_post_type(), mb_get_topic_post_type(), mb_get_reply_post_type()));
        }
        $query->set('post_status', array(mb_get_open_post_status(), mb_get_close_post_status(), mb_get_publish_post_status()));
        $query->set('posts_per_page', mb_get_topics_per_page());
    } elseif (mb_is_user_archive() && get_query_var('mb_role')) {
        $role = get_query_var('mb_role');
        if ($role && in_array("mb_{$role}", array_keys(mb_get_dynamic_roles()))) {
            $query->set('mb_role', "mb_{$role}");
        }
    } elseif ($query->is_single && isset($query->query_vars['post_type']) && in_array($query->query_vars['post_type'], array(mb_get_forum_post_type(), mb_get_topic_post_type(), mb_get_reply_post_type()))) {
        add_filter('the_posts', 'mb_posts_can_read_parent');
    }
}
/**
 * Filter on `get_the_archive_title` to output the correct archive page title.
 *
 * @since  1.0.0
 * @access public
 * @param  string  $title
 * @return string
 */
function mb_the_archive_title_filter($title)
{
    if (!mb_is_message_board()) {
        return $title;
    }
    if (mb_is_forum_archive()) {
        $title = mb_get_forum_archive_title();
    } elseif (mb_is_topic_archive()) {
        $title = mb_get_topic_archive_title();
    } elseif (mb_is_reply_archive()) {
        $title = mb_get_reply_archive_title();
    } elseif (mb_is_role_archive()) {
        $title = mb_get_role_archive_title();
    } elseif (mb_is_user_archive()) {
        $title = mb_get_user_archive_title();
    }
    return $title;
}