/**
 * Helper function for checking if a user can read forums, topics, or replies. We need this to handle 
 * users who are not logged in but should have permission to read (e.g, non-private forums).  This 
 * function is meant to be used in conjunction with a filter on `map_meta_cap`.
 *
 * @since  1.0.0
 * @access public
 * @param  int     $user_id
 * @param  string  $cap
 * @param  int     $post_id
 * @return bool
 */
function mb_user_can($user_id, $cap, $post_id)
{
    // @todo Check hierarchy.
    if (in_array($cap, array('read_forum', 'read_topic', 'read_reply'))) {
        if ('read_forum' === $cap) {
            $status_obj = get_post_status_object(mb_get_forum_status($post_id));
        } elseif ('read_topic' === $cap) {
            $status_obj = get_post_status_object(mb_get_topic_status($post_id));
        } elseif ('read_forum' === $cap) {
            $status_obj = get_post_status_object(mb_get_reply_status($post_id));
        }
        if (false === $status_obj->private && false === $status_obj->protected) {
            return true;
        }
    }
    return user_can($user_id, $cap, $post_id);
}
/**
 * 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;
}
Example #3
0
 /**
  * @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('&nbsp;', $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";
 }
 /**
  * Handles the output for custom columns.
  *
  * @since  1.0.0
  * @access public
  * @param  string  $column
  * @param  int     $post_id
  */
 public function manage_columns($column, $post_id)
 {
     switch ($column) {
         /* Post status column. */
         case 'status':
             $post_type = mb_get_forum_post_type();
             $status = get_post_status_object(mb_get_forum_status($post_id));
             /* If the forum has the "publish" post status, change it to "open". */
             if (mb_get_publish_post_status() === $status->name) {
                 wp_update_post(array('ID' => $post_id, 'post_status' => mb_get_open_post_status()));
             }
             $url = add_query_arg(array('post_status' => $status->name, 'post_type' => $post_type), admin_url('edit.php'));
             printf('<a href="%s">%s</a>', $url, $status->label);
             break;
             /* Forum type column. */
         /* Forum type column. */
         case 'type':
             $post_type = mb_get_forum_post_type();
             $forum_type = mb_get_forum_type_object(mb_get_forum_type($post_id));
             $url = add_query_arg(array('post_type' => $post_type, 'forum_type' => $forum_type->name), admin_url('edit.php'));
             printf('<a href="%s">%s</a>', $url, $forum_type->label);
             break;
             /* Topic count column. */
         /* Topic count column. */
         case 'subforums':
             $subforum_count = mb_get_forum_subforum_count($post_id);
             $subforum_count = !empty($subforum_count) ? absint($subforum_count) : number_format_i18n(0);
             if (0 < $subforum_count) {
                 printf('<a href="%s">%s</a>', add_query_arg(array('post_type' => mb_get_forum_post_type(), 'post_parent' => $post_id), admin_url('edit.php')), $subforum_count);
             } else {
                 echo $subforum_count;
             }
             break;
             /* Topic count column. */
         /* Topic count column. */
         case 'topics':
             $topic_count = mb_get_forum_topic_count($post_id);
             $topic_count = !empty($topic_count) ? absint($topic_count) : number_format_i18n(0);
             if (0 < $topic_count && current_user_can('edit_topics')) {
                 printf('<a href="%s">%s</a>', add_query_arg(array('post_type' => mb_get_topic_post_type(), 'post_parent' => $post_id), admin_url('edit.php')), $topic_count);
             } else {
                 echo $topic_count;
             }
             break;
             /* Reply count column. */
         /* Reply count column. */
         case 'replies':
             $reply_count = mb_get_forum_reply_count($post_id);
             $reply_count = !empty($reply_count) ? absint($reply_count) : number_format_i18n(0);
             if (0 < $reply_count && current_user_can('edit_replies')) {
                 printf('<a href="%s">%s</a>', add_query_arg(array('post_type' => mb_get_reply_post_type(), 'mb_forum' => $post_id), admin_url('edit.php')), $reply_count);
             } else {
                 echo $reply_count;
             }
             break;
             /* Datetime column. */
         /* Datetime column. */
         case 'datetime':
             the_time(get_option('date_format'));
             echo '<br />';
             the_time(get_option('time_format'));
             break;
             /* Just break out of the switch statement for everything else. */
         /* Just break out of the switch statement for everything else. */
         default:
             break;
     }
 }
			<label for="mb_forum_type"><?php 
mb_forum_label('mb_form_type');
?>
</label>
			<?php 
mb_dropdown_forum_type();
?>
		</p><!-- .mb-form-type -->

		<p class="mb-form-status">
			<label for="mb_post_status"><?php 
mb_forum_label('mb_form_status');
?>
</label>
			<?php 
mb_dropdown_post_status(array('post_type' => mb_get_forum_post_type(), 'name' => 'mb_post_status', 'id' => 'mb_post_status', 'selected' => mb_get_forum_status()));
?>
		</p><!-- .mb-form-status -->

		<p class="mb-form-order">
			<label for="mb_menu_order"><?php 
mb_forum_label('mb_form_order');
?>
</label>
			<input type="number" id="mb_menu_order" name="mb_menu_order" value="<?php 
echo esc_attr(mb_get_forum_order());
?>
" />
		</p><!-- .mb-form-order -->

		<div class="mb-form-content">