/** * 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; }
/** * Callback function on the `after_delete_post` hook for when a forum is deleted. * * @todo All forum topics need to become orphans at this point. Attempt to move topics into parent if avail. * @todo Reset counts for parent forums. * @todo `wp_die()` if this is the default forum. * * @since 1.0.0 * @access public * @param int $post_id * @return void */ function mb_after_delete_forum($post_id) { global $wpdb; $mb = message_board(); if (is_object($mb->deleted_post) && $mb->deleted_post->ID === $post_id) { $forum_id = mb_get_forum_id($post_id); $user_id = mb_get_user_id($post->deleted_post->post_author); $parent_forum_id = $mb->deleted_post->post_parent; /* Get the current forum's subforum IDs. */ $subforum_ids = $wpdb->get_col($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_type = %s AND post_parent = %s ORDER BY menu_order DESC", mb_get_forum_post_type(), absint($forum_id))); if (!empty($subforum_ids)) { $moved_forums = false; while (0 < $parent_forum_id) { if (mb_forum_allows_subforums($parent_forum_id)) { /* Change all of the subforums' parents to the new forum ID. */ $wpdb->query($wpdb->prepare("UPDATE {$wpdb->posts} SET post_parent = %d WHERE ID IN (" . implode(',', $subforum_ids) . ")", absint($parent_forum_id))); /* Reset data based on new forum. */ mb_reset_forum_data($parent_forum_id); $parent_forum_id = 0; $moved_forums = true; /* Break out of the while loop at this point. */ break; } $post = get_post($parent_forum_id); $parent_forum_id = $post->post_parent; } /* If subforums didn't get moved to a new forum, make them a top-level forum. */ if (false === $moved_forums) { $wpdb->query($wpdb->prepare("UPDATE {$wpdb->posts} SET post_parent = %d WHERE ID IN (" . implode(',', $subforum_ids) . ")", 0)); } } $parent_forum_id = $mb->deleted_post->post_parent; /* Get the current forum's topic IDs. */ $topic_ids = $wpdb->get_col($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_type = %s AND post_parent = %s ORDER BY menu_order DESC", mb_get_topic_post_type(), absint($forum_id))); if (!empty($topic_ids)) { $moved_topics = false; while (0 < $parent_forum_id) { if (mb_forum_allows_topics($parent_forum_id)) { /* Change all of the topics' parents to the new forum ID. */ $wpdb->query($wpdb->prepare("UPDATE {$wpdb->posts} SET post_parent = %d WHERE ID IN (" . implode(',', $topic_ids) . ")", absint($parent_forum_id))); /* Reset data based on new forum. */ mb_reset_forum_data($parent_forum_id); $parent_forum_id = 0; $moved_topics = true; /* Break out of the while loop at this point. */ break; } $post = get_post($parent_forum_id); $parent_forum_id = $post->post_parent; } /* If topics didn't get moved to a new forum, set their status to "orphan". */ if (false === $moved_topics) { $wpdb->query($wpdb->prepare("UPDATE {$wpdb->posts} SET post_status = %s WHERE ID IN (" . implode(',', $topic_ids) . ")", mb_get_orphan_post_status())); } } /* Reset user forum count. */ mb_set_user_forum_count($user_id); } }
/** * @see Walker::start_el() * @since 1.0.0 * * @param string $output Passed by reference. Used to append additional content. * @param object $page Page data object. * @param int $depth Depth of page in reference to parent pages. Used for padding. * @param array $args Uses 'selected' argument for selected page to set selected HTML attribute for option element. * @param int $id */ public function start_el(&$output, $page, $depth = 0, $args = array(), $id = 0) { $forum_type = mb_get_forum_type_object(mb_get_forum_type($page->ID)); $pad = str_repeat(' ', $depth * 3); $output .= "\t<option class=\"level-{$depth}\" value=\"{$page->ID}\""; if ($page->ID == $args['selected']) { $output .= ' selected="selected"'; } $post_status = mb_get_forum_status($page->ID); if (mb_get_forum_post_type() === $args['child_type'] && !mb_forum_allows_subforums($page->ID)) { $output .= ' disabled="disabled"'; } elseif (mb_get_topic_post_type() === $args['child_type'] && !mb_forum_allows_topics($page->ID)) { $output .= ' disabled="disabled"'; } $output .= '>'; $title = $page->post_title; if ('' === $title) { $title = sprintf(__('#%d (no title)'), $page->ID); } /** * Filter the page title when creating an HTML drop-down list of pages. * * @since 3.1.0 * * @param string $title Page title. * @param object $page Page data object. */ $title = apply_filters('list_pages', $title, $page); $output .= $pad . esc_html($title); $output .= "</option>\n"; }
</div><!-- .mb-forum-content --> <p class="mb-forum-info"> <?php if (mb_forum_allows_subforums()) { ?> <span class="mb-subforum-count"><?php printf(mb_forum_allows_topics() ? __('Sub-forums: %s', 'message-board') : __('Forums: %s', 'message-board'), mb_get_forum_subforum_count()); ?> </span> <?php } ?> <?php if (mb_forum_allows_topics()) { ?> <span class="mb-topic-count"><?php printf(__('Topics: %s', 'message-board'), mb_get_forum_topic_count()); ?> </span> <span class="mb-reply-count"><?php printf(__('Replies: %s', 'message-board'), mb_get_forum_reply_count()); ?> </span> <?php } ?> <?php mb_forum_toggle_open_link();
/** * Conditional check to see if a topic allows new replies to be created. * * @since 1.0.0 * @access public * @param int $topic * @return bool */ function mb_topic_allows_replies($topic_id = 0) { $topic_id = mb_get_forum_id($topic_id); $forum_id = mb_get_topic_forum_id($topic_id); $allow = true; /* Check if the topic type allows replies. */ if (!mb_topic_type_allows_replies(mb_get_topic_type($topic_id))) { $allow = false; } elseif (!mb_topic_status_allows_replies(mb_get_topic_status($topic_id))) { $allow = false; } elseif (0 < $forum_id && !mb_forum_allows_topics($forum_id)) { $allow = false; } return apply_filters('mb_topic_allows_replies', $allow, $topic_id); }