/** * 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); }
/** * 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; }
/** * 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_topic_map_meta_cap($caps, $cap, $user_id, $args) { /* Checks if a user can read a specific topic. */ if ('read_post' === $cap && mb_is_topic($args[0])) { $post = get_post($args[0]); /* Only run our code if the user isn't the post author. */ if ($user_id != $post->post_author) { $forum_id = $post->post_parent; /* If we have a forum and the user can't read it, don't allow reading the topic. */ if (0 < $forum_id && !mb_user_can($user_id, 'read_forum', $forum_id)) { $caps = array('do_not_allow'); /* If the user can read the forum, check if they can read the topic. */ } else { $post_type = get_post_type_object($post->post_type); $post_status = mb_get_topic_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_topics; } 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_topics) { $caps[] = $post_type->cap->read_others_topics; } else { $caps = array(); } //$caps[] = $post_type->cap->read; } } else { $caps = array(); } /* Meta cap for editing a single topic. */ } elseif ('edit_post' === $cap && mb_is_topic($args[0])) { $post = get_post($args[0]); $topic_obj = get_post_type_object(mb_get_topic_post_type()); if ($user_id != $post->post_author) { // Open topics. if (mb_is_topic_open($args[0])) { $caps[] = $topic_obj->cap->edit_open_topics; } elseif (mb_is_topic_closed($args[0])) { $caps[] = $topic_obj->cap->edit_closed_topics; } elseif (mb_is_topic_hidden($args[0])) { $caps[] = $topic_obj->cap->edit_hidden_topics; } } // Spam topics if (mb_is_topic_spam($args[0])) { $caps[] = $topic_obj->cap->edit_spam_topics; } elseif (mb_is_topic_orphan($args[0])) { $caps[] = $topic_obj->cap->edit_orphan_topics; } /* Meta cap for opening a single topic. */ } elseif ('open_topic' === $cap) { $caps = array(); $caps[] = user_can($user_id, 'edit_topic', $args[0]) ? 'open_topics' : 'do_not_allow'; /* Meta cap for closing a single topic. */ } elseif ('close_topic' === $cap) { $caps = array(); $caps[] = user_can($user_id, 'edit_topic', $args[0]) ? 'close_topics' : 'do_not_allow'; /* Meta cap for privatizing a single topic. */ } elseif ('privatize_topic' === $cap) { $caps = array(); $caps[] = user_can($user_id, 'edit_topic', $args[0]) ? 'privatize_topics' : 'do_not_allow'; /* Meta cap for hiding a single topic. */ } elseif ('hide_topic' === $cap) { $caps = array(); $caps[] = user_can($user_id, 'edit_topic', $args[0]) ? 'hide_topics' : 'do_not_allow'; /* Meta cap for spamming a single topic. */ } elseif ('spam_topic' === $cap) { $caps = array(); $caps[] = user_can($user_id, 'edit_topic', $args[0]) ? 'spam_topics' : 'do_not_allow'; /* Meta cap for spamming a single topic. */ } elseif ('super_topic' === $cap) { $caps = array(); $caps[] = user_can($user_id, 'edit_topic', $args[0]) ? 'super_topics' : 'do_not_allow'; /* Meta cap for spamming a single topic. */ } elseif ('stick_topic' === $cap) { $caps = array(); $caps[] = user_can($user_id, 'edit_topic', $args[0]) ? 'stick_topics' : 'do_not_allow'; /* Meta cap check for accessing the topic form. */ } elseif ('access_topic_form' === $cap) { $caps = array('create_topics'); 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_topics($forum_id)) { $caps[] = 'do_not_allow'; } } elseif (mb_is_topic_edit() && !user_can($user_id, 'edit_post', mb_get_topic_id())) { $caps[] = 'do_not_allow'; } } return $caps; }
/** * 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); }
/** * Adds sticky posts to the posts array. Meant to be used as a filter on `the_posts`. * * @since 1.0.0 * @access public * @param array $posts * @param object $query * @return array */ function mb_posts_sticky_filter($posts, $query) { remove_filter('the_posts', 'mb_posts_sticky_filter'); $forum_id = mb_is_single_forum() ? get_queried_object_id() : 0; return mb_add_stickies($posts, mb_get_sticky_topics(), $forum_id); }
/** * Adds hidden topic form fields. * * @since 1.0.0 * @access public * @return void */ function mb_topic_form_fields() { if (mb_is_topic_edit()) { return; } if (mb_is_single_forum()) { ?> <input type="hidden" name="mb_forum_id" value="<?php echo absint(get_queried_object_id()); ?> " /> <?php } wp_nonce_field('mb_new_topic_action', 'mb_new_topic_nonce', false); }
</th> <th class="mb-col-count"><?php _e('Topics / Replies', 'message-board'); ?> </th> <th class="mb-col-latest"><?php _e('Last Post', 'message-board'); ?> </th> </tr> </thead> <tfoot> <tr> <th class="mb-col-title"><?php mb_is_single_forum() && !mb_is_forum_category() ? _e('Sub-forum', 'message-board') : _e('Forum', 'message-board'); ?> </th> <th class="mb-col-count"><?php _e('Topics / Replies', 'message-board'); ?> </th> <th class="mb-col-latest"><?php _e('Last Post', 'message-board'); ?> </th> </tr> </tfoot> <tbody> <?php
<p class="mb-form-title"> <label for="mb_topic_title"><?php mb_topic_label('mb_form_title'); ?> </label> <input type="text" id="mb_topic_title" name="mb_topic_title" value="<?php echo esc_attr(mb_get_topic_title()); ?> " placeholder="<?php echo esc_attr(mb_get_topic_label('mb_form_title_placeholder')); ?> " /> </p><!-- .mb-form-title --> <?php if (!mb_is_single_forum()) { ?> <p class="mb-form-parent"> <label for="mb_forum_id"><?php mb_topic_label('parent_item_colon'); ?> </label> <?php mb_dropdown_forums(array('child_type' => mb_get_topic_post_type(), 'name' => 'mb_forum_id', 'id' => 'mb_forum_id', 'selected' => mb_get_topic_forum_id())); ?> </p><!-- .mb-form-parent --> <?php } ?>
/** * Checks if viewing a paginated forum. Only for use on single forum pages. * * @since 1.0.0 * @access public * @return bool */ function mb_is_forum_paged() { return mb_is_single_forum() && is_paged() ? true : false; }
/** * Returns the URL to the new topic form. * * @since 1.0.0 * @access public * @return string */ function mb_get_topic_form_url() { if (mb_is_single_forum() && !mb_is_forum_open(get_queried_object_id())) { $url = ''; } else { $url = esc_url('#mb-topic-form'); } return apply_filters('mb_topic_form_url', $url); }
/** * Filter on `body_class` to add custom classes for the plugin's pages on the front end. * * @todo Remove `bbpress` class. * @todo Decide on class naming system. * * @since 1.0.0 * @access public * @param array $classes * @return array */ function mb_body_class($classes) { global $wp; if (mb_is_message_board()) { $classes[] = 'mb'; $classes[] = 'bbpress'; // temporary class for compat $forum_type = mb_get_forum_post_type(); $topic_type = mb_get_topic_post_type(); $reply_type = mb_get_reply_post_type(); $_classes = $classes; $remove = array("single-{$forum_type}", "single-{$topic_type}", "single-{$reply_type}", "singular-{$forum_type}", "singular-{$topic_type}", "singular-{$reply_type}", "archive-{$forum_type}", "archive-{$topic_type}", "archive-{$reply_type}"); foreach ($_classes as $class_key => $class_value) { if (in_array($class_value, $remove)) { unset($classes[$class_key]); } } if (mb_is_forum_front()) { $classes[] = 'forum-front'; } elseif (mb_is_single_forum()) { $classes[] = 'single-forum'; } elseif (mb_is_single_topic()) { $classes[] = 'single-topic'; } elseif (mb_is_single_reply()) { $classes[] = 'single-reply'; } elseif (mb_is_single_role()) { $classes[] = 'single-role'; } elseif (mb_is_forum_archive()) { $classes[] = 'archive-forum'; } elseif (mb_is_topic_archive()) { $classes[] = 'archive-topic'; } elseif (mb_is_reply_archive()) { $classes[] = 'archive-reply'; } elseif (mb_is_role_archive()) { $classes[] = 'archive-role'; } elseif (mb_is_user_archive()) { $classes[] = 'archive-user'; } } return $classes; }