Example #1
0
/**
 * Maps primary capabilities
 *
 * @since 2.2.0 bbPress (r4242)
 *
 * @param array  $caps Capabilities for meta capability.
 * @param string $cap Capability name.
 * @param int    $user_id User id.
 * @param array  $args Arguments.
 * @uses bbp_is_user_inactive() To check if user is spammer or deleted
 * @uses get_post() To get the post
 * @uses bbp_get_forum_post_type() To get the forum post type
 * @uses bbp_get_topic_post_type() To get the topic post type
 * @uses bbp_get_topic_forum_id() To get the topic forum id
 * @uses bbp_get_reply_post_type() To get the reply post type
 * @uses bbp_get_reply_forum_id() To get the reply forum id
 * @uses bbp_is_user_forum_mod() To check if the user is a forum moderator
 * @uses apply_filters() Filter mapped results
 *
 * @return array Actual capabilities for meta capability
 */
function bbp_map_primary_meta_caps($caps = array(), $cap = '', $user_id = 0, $args = array())
{
    // What capability is being checked?
    switch ($cap) {
        case 'spectate':
            // Do not allow inactive users.
            if (bbp_is_user_inactive($user_id)) {
                $caps = array('do_not_allow');
                // Default to the current cap.
            } else {
                $caps = array($cap);
            }
            break;
        case 'participate':
            // Do not allow inactive users.
            if (bbp_is_user_inactive($user_id)) {
                $caps = array('do_not_allow');
                // Default to the current cap.
            } else {
                $caps = array($cap);
            }
            break;
        case 'moderate':
            // Do not allow inactive users.
            if (bbp_is_user_inactive($user_id)) {
                $caps = array('do_not_allow');
                // Default to the current cap.
            } else {
                $caps = array($cap);
                // Bail if no post to check.
                if (empty($args[0])) {
                    break;
                }
                // Get the post.
                $_post = get_post($args[0]);
                if (empty($_post)) {
                    break;
                }
                // Get forum ID for specific type of post.
                switch ($_post->post_type) {
                    // Forum.
                    case bbp_get_forum_post_type():
                        $forum_id = $_post->ID;
                        break;
                        // Topic.
                    // Topic.
                    case bbp_get_topic_post_type():
                        $forum_id = bbp_get_topic_forum_id($_post->ID);
                        break;
                        // Reply.
                    // Reply.
                    case bbp_get_reply_post_type():
                        $forum_id = bbp_get_reply_forum_id($_post->ID);
                        break;
                        // Any other post type defaults to 0.
                    // Any other post type defaults to 0.
                    default:
                        $forum_id = 0;
                        break;
                }
                // Bail if no forum ID.
                if (empty($forum_id)) {
                    break;
                }
                // If user is a per-forum moderator, make sure they can spectate.
                if (bbp_is_user_forum_mod($user_id, $forum_id)) {
                    $caps = array('spectate');
                }
            }
            break;
    }
    return apply_filters('bbp_map_primary_meta_caps', $caps, $cap, $user_id, $args);
}
Example #2
0
/**
 * Maps topic capabilities
 *
 * @since 2.2.0 bbPress (r4242)
 *
 * @param array $caps Capabilities for meta capability
 * @param string $cap Capability name
 * @param int $user_id User id
 * @param array $args Arguments
 * @uses get_post() To get the post
 * @uses get_post_type_object() To get the post type object
 * @uses bbp_get_public_status_id() To get the public status id
 * @uses bbp_is_user_forum_mod() To check if the user is a forum moderator
 * @uses bbp_get_reply_forum_id() To get the repliy forum id
 * @uses apply_filters() Filter mapped results
 *
 * @return array Actual capabilities for meta capability
 */
function bbp_map_reply_meta_caps($caps = array(), $cap = '', $user_id = 0, $args = array())
{
    // What capability is being checked?
    switch ($cap) {
        /** Reading ***********************************************************/
        case 'read_reply':
            // User cannot spectate
            if (!user_can($user_id, 'spectate')) {
                $caps = array('do_not_allow');
                // Do some post ID based logic
            } else {
                // Get the post
                $_post = get_post($args[0]);
                if (!empty($_post)) {
                    // Get post type object
                    $post_type = get_post_type_object($_post->post_type);
                    // Post is public
                    if (bbp_get_public_status_id() === $_post->post_status) {
                        $caps = array('spectate');
                        // User is author so allow read
                    } elseif ((int) $user_id === (int) $_post->post_author) {
                        $caps = array('spectate');
                        // Unknown so map to private posts
                    } else {
                        $caps = array($post_type->cap->read_private_posts);
                    }
                }
            }
            break;
            /** Publishing ********************************************************/
        /** Publishing ********************************************************/
        case 'publish_replies':
            // Moderators can always publish
            if (user_can($user_id, 'moderate')) {
                $caps = array('moderate');
            }
            break;
            /** Editing ***********************************************************/
            // Used primarily in wp-admin
        /** Editing ***********************************************************/
        // Used primarily in wp-admin
        case 'edit_replies':
        case 'edit_others_replies':
            // Moderators can always edit
            if (user_can($user_id, 'moderate')) {
                $caps = array('moderate');
                // Otherwise, block
            } else {
                $caps = array('do_not_allow');
            }
            break;
            // Used everywhere
        // Used everywhere
        case 'edit_reply':
            // Get the post
            $_post = get_post($args[0]);
            if (!empty($_post)) {
                // Get post type object
                $post_type = get_post_type_object($_post->post_type);
                $caps = array();
                // Add 'do_not_allow' cap if user is spam or deleted
                if (bbp_is_user_inactive($user_id)) {
                    $caps[] = 'do_not_allow';
                    // User is author so allow edit if not in admin
                } elseif (!is_admin() && (int) $user_id === (int) $_post->post_author) {
                    $caps[] = $post_type->cap->edit_posts;
                    // User is a per-forum moderator, make sure they can spectate.
                } elseif (bbp_allow_forum_mods() && bbp_is_user_forum_mod($user_id, bbp_get_reply_forum_id($_post->ID))) {
                    $caps = array('spectate');
                    // Fallback to edit_others_posts.
                } else {
                    $caps[] = $post_type->cap->edit_others_posts;
                }
            }
            break;
            /** Deleting **********************************************************/
        /** Deleting **********************************************************/
        case 'delete_reply':
            // Get the post
            $_post = get_post($args[0]);
            if (!empty($_post)) {
                // Get post type object
                $post_type = get_post_type_object($_post->post_type);
                $caps = array();
                // Add 'do_not_allow' cap if user is spam or deleted
                if (bbp_is_user_inactive($user_id)) {
                    $caps[] = 'do_not_allow';
                    // Moderators can always edit forum content
                } elseif (user_can($user_id, 'moderate')) {
                    $caps[] = 'moderate';
                    // User is author so allow edit if not in admin
                } elseif (!is_admin() && (int) $user_id === (int) $_post->post_author) {
                    $caps[] = $post_type->cap->delete_posts;
                    // Unknown so map to delete_others_posts
                } else {
                    $caps[] = $post_type->cap->delete_others_posts;
                }
            }
            break;
            // Moderation override
        // Moderation override
        case 'delete_replies':
        case 'delete_others_replies':
            // Moderators can always delete
            if (user_can($user_id, 'moderate')) {
                $caps = array('moderate');
            }
            break;
            /** Admin *************************************************************/
        /** Admin *************************************************************/
        case 'bbp_replies_admin':
            $caps = array('moderate');
            break;
    }
    return apply_filters('bbp_map_reply_meta_caps', $caps, $cap, $user_id, $args);
}