function adjust_meta_caps($caps, $cap, $user_id, $args) { // only run for REST API requests if (!defined('REST_API_REQUEST') || !REST_API_REQUEST) { return $caps; } // only modify caps for meta caps and for bbPress meta keys if (!in_array($cap, array('edit_post_meta', 'delete_post_meta', 'add_post_meta')) || empty($args[1]) || false === strpos($args[1], '_bbp_')) { return $caps; } // $args[0] could be a post ID or a post_type string if (is_int($args[0])) { $_post = get_post($args[0]); if (!empty($_post)) { $post_type = get_post_type_object($_post->post_type); } } elseif (is_string($args[0])) { $post_type = get_post_type_object($args[0]); } // no post type found, bail if (empty($post_type)) { return $caps; } // reset the needed caps $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 meta } elseif (user_can($user_id, 'moderate')) { $caps[] = 'moderate'; // Unknown so map to edit_posts } else { $caps[] = $post_type->cap->edit_posts; } return $caps; }
/** * Add the default role and mapped bbPress caps to the current user if needed * * This function will bail if the forum is not global in a multisite * installation of WordPress, or if the user is marked as spam or deleted. * * @since bbPress (r3380) * * @uses bbp_allow_global_access() * @uses bbp_is_user_inactive() * @uses is_user_logged_in() * @uses current_user_can() * @uses get_option() * @uses bbp_get_caps_for_role() * * @return If not multisite, not global, or user is deleted/spammed */ function bbp_global_access_role_mask() { // Bail if not multisite or forum is not global if (!bbp_allow_global_access()) { return; } // Bail if user is marked as spam or is deleted if (bbp_is_user_inactive()) { return; } // Normal user is logged in but has no caps if (is_user_logged_in() && !current_user_can('read')) { // Assign user the minimal participant role to map caps to $default_role = bbp_get_participant_role(); // Get bbPress caps for the default role $caps_for_role = bbp_get_caps_for_role($default_role); // Set all caps to true foreach ($caps_for_role as $cap) { $mapped_meta_caps[$cap] = true; } // Add 'read' cap just in case $mapped_meta_caps['read'] = true; $mapped_meta_caps['bbp_masked'] = true; // Allow global access caps to be manipulated $mapped_meta_caps = apply_filters('bbp_global_access_mapped_meta_caps', $mapped_meta_caps); // Assign the role and mapped caps to the current user $bbp = bbpress(); $bbp->current_user->roles[0] = $default_role; $bbp->current_user->caps = $mapped_meta_caps; $bbp->current_user->allcaps = $mapped_meta_caps; } }
/** * Finds and links @-mentioned users in the content * * @since 2.2.0 bbPress (r4323) * * @uses bbp_find_mentions() To get usernames in content areas * @return string $content Content filtered for mentions */ function bbp_mention_filter($content = '') { // Get Usernames and bail if none exist $usernames = bbp_find_mentions($content); if (empty($usernames)) { return $content; } // Loop through usernames and link to profiles foreach ((array) $usernames as $username) { // Skip if username does not exist or user is not active $user = get_user_by('slug', $username); if (empty($user->ID) || bbp_is_user_inactive($user->ID)) { continue; } // Replace name in content $content = preg_replace('/(@' . $username . '\\b)/', sprintf('<a href="%1$s" rel="nofollow">@%2$s</a>', bbp_get_user_profile_url($user->ID), $username), $content); } // Return modified content return $content; }
/** * Does a user have a profile for the current site * * @since bbPress (r4362) * * @param int $user_id User ID to check * @param int $blog_id Blog ID to check * * @uses bbp_get_user_id() To verify the user ID * @uses get_userdata() To get the user's data * @uses bbp_is_user_keymaster() To determine if user can see inactive users * @uses bbp_is_user_inactive() To check if user is spammer or deleted * @uses apply_filters() To allow override of this functions result * * @return boolean Whether or not the user has a profile on this blog_id */ function bbp_user_has_profile($user_id = 0) { // Assume every user has a profile $retval = true; // Validate user ID, default to displayed or current user $user_id = bbp_get_user_id($user_id, true, true); // Try to get this user's data $user = get_userdata($user_id); // No user found, return false if (empty($user)) { $retval = false; // User is inactive, and current user is not a keymaster } elseif (!bbp_is_user_keymaster() && bbp_is_user_inactive($user->ID)) { $retval = false; } // Filter and return return (bool) apply_filters('bbp_show_user_profile', $retval, $user_id); }
/** * Record an activity stream entry when a reply is created * * @since bbPress (r3395) * @param int $topic_id * @param int $forum_id * @param array $anonymous_data * @param int $topic_author_id * @uses bbp_get_reply_id() * @uses bbp_get_topic_id() * @uses bbp_get_forum_id() * @uses bbp_get_user_profile_link() * @uses bbp_get_reply_url() * @uses bbp_get_reply_content() * @uses bbp_get_topic_permalink() * @uses bbp_get_topic_title() * @uses bbp_get_forum_permalink() * @uses bbp_get_forum_title() * @uses bp_create_excerpt() * @uses apply_filters() * @return Bail early if topic is by anonywous user */ public function reply_create($reply_id, $topic_id, $forum_id, $anonymous_data, $reply_author_id) { // Do not log activity of anonymous users if (!empty($anonymous_data)) { return; } // Bail if site is private if (!bbp_is_site_public()) { return; } // Validate activity data $user_id = $reply_author_id; $reply_id = bbp_get_reply_id($reply_id); $topic_id = bbp_get_topic_id($topic_id); $forum_id = bbp_get_forum_id($forum_id); // Bail if user is not active if (bbp_is_user_inactive($user_id)) { return; } // Bail if reply is not published if (!bbp_is_reply_published($reply_id)) { return; } // Setup links for activity stream $user_link = bbp_get_user_profile_link($user_id); // Reply $reply_url = bbp_get_reply_url($reply_id); $reply_content = get_post_field('post_content', $reply_id, 'raw'); // Topic $topic_permalink = bbp_get_topic_permalink($topic_id); $topic_title = get_post_field('post_title', $topic_id, 'raw'); $topic_link = '<a href="' . $topic_permalink . '" title="' . $topic_title . '">' . $topic_title . '</a>'; // Forum $forum_permalink = bbp_get_forum_permalink($forum_id); $forum_title = get_post_field('post_title', $forum_id, 'raw'); $forum_link = '<a href="' . $forum_permalink . '" title="' . $forum_title . '">' . $forum_title . '</a>'; // Activity action & text $activity_text = sprintf(__('%1$s replied to the topic %2$s in the forum %3$s', 'bbpress'), $user_link, $topic_link, $forum_link); $activity_action = apply_filters('bbp_activity_reply_create', $activity_text, $user_id, $reply_id, $topic_id); $activity_content = apply_filters('bbp_activity_reply_create_excerpt', bp_create_excerpt($reply_content), $reply_content); // Compile the activity stream results $activity = array('id' => $this->get_activity_id($reply_id), 'user_id' => $user_id, 'action' => $activity_action, 'content' => $activity_content, 'primary_link' => $reply_url, 'type' => $this->reply_create, 'item_id' => $reply_id, 'secondary_item_id' => $topic_id, 'recorded_time' => get_post_time('Y-m-d H:i:s', true, $reply_id), 'hide_sitewide' => !bbp_is_forum_public($forum_id, false)); // Record the activity $activity_id = $this->record_activity($activity); // Add the activity entry ID as a meta value to the reply if (!empty($activity_id)) { update_post_meta($reply_id, '_bbp_activity_id', $activity_id); } }
/** * Maps topic capabilities * * @since bbPress (r4242) * * @param array $caps Capabilities for meta capability * @param string $cap Capability name * @param int $user_id User id * @param mixed $args Arguments * @uses get_post() To get the post * @uses get_post_type_object() To get the post type object * @uses apply_filters() Filter capability map results * @return array Actual capabilities for meta capability */ function bbp_map_topic_meta_caps($caps = array(), $cap = '', $user_id = 0, $args = array()) { // What capability is being checked? switch ($cap) { /** Reading ***********************************************************/ case 'read_topic': // 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 caps for 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_topics': // 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_topics': case 'edit_others_topics': // Moderators can always edit if (user_can($user_id, 'moderate')) { $caps = array($cap); // Otherwise, block } else { $caps = array('do_not_allow'); } break; // Used everywhere // Used everywhere case 'edit_topic': // Get the post $_post = get_post($args[0]); if (!empty($_post)) { // Get caps for 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; // Unknown, so map to edit_others_posts } else { $caps[] = $post_type->cap->edit_others_posts; } } break; /** Deleting **********************************************************/ /** Deleting **********************************************************/ case 'delete_topic': // Get the post $_post = get_post($args[0]); if (!empty($_post)) { // Get caps for 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'; // Unknown so map to delete_others_posts } else { $caps[] = $post_type->cap->delete_others_posts; } } break; // Moderation override // Moderation override case 'delete_topics': case 'delete_others_topics': // Moderators can always delete if (user_can($user_id, 'moderate')) { $caps = array($cap); } break; /** Admin *************************************************************/ /** Admin *************************************************************/ case 'bbp_topics_admin': $caps = array('moderate'); break; } return apply_filters('bbp_map_topic_meta_caps', $caps, $cap, $user_id, $args); }
/** * Return a user's main role for display * * @since 2.1.0 bbPress (r3860) * * @param int $user_id * @uses bbp_get_user_id() to verify the user ID * @uses bbp_is_user_inactive() to check if user is inactive * @uses user_can() to check if user has special capabilities * @uses apply_filters() Calls 'bbp_get_user_display_role' with the * display role, user id, and user role * @return string */ function bbp_get_user_display_role($user_id = 0) { // Validate user id $user_id = bbp_get_user_id($user_id); // User is not registered if (empty($user_id)) { $role = __('Guest', 'bbpress'); // User is not active } elseif (bbp_is_user_inactive($user_id)) { $role = __('Inactive', 'bbpress'); // User have a role } else { $role_id = bbp_get_user_role($user_id); $role = bbp_get_dynamic_role_name($role_id); } // No role found so default to generic "Member" if (empty($role)) { $role = __('Member', 'bbpress'); } return apply_filters('bbp_get_user_display_role', $role, $user_id); }
/** * Add the default role to the current user if needed * * This function will bail if the forum is not global in a multisite * installation of WordPress, or if the user is marked as spam or deleted. * * @since bbPress (r3380) * * @uses bbp_allow_global_access() * @uses bbp_is_user_inactive() * @uses is_user_logged_in() * @uses is_user_member_of_blog() * @uses get_option() * * @return If not multisite, not global, or user is deleted/spammed */ function bbp_set_current_user_default_role() { // Bail if forum is not global if (!bbp_allow_global_access()) { return; } // Bail if not logged in or already a member of this site if (!is_user_logged_in() || is_user_member_of_blog()) { return; } // Bail if user is marked as spam or is deleted if (bbp_is_user_inactive()) { return; } // Assign the default role to the current user bbpress()->current_user->set_role(get_option('default_role', 'subscriber')); }
function etheme_bb_user_role() { if (!function_exists('bbp_is_deactivation')) { return; } // Bail if deactivating bbPress if (bbp_is_deactivation()) { return; } // Catch all, to prevent premature user initialization if (!did_action('set_current_user')) { return; } // Bail if not logged in or already a member of this site if (!is_user_logged_in()) { return; } // Get the current user ID $user_id = get_current_user_id(); // Bail if user already has a forums role if (bbp_get_user_role($user_id)) { return; } // Bail if user is marked as spam or is deleted if (bbp_is_user_inactive($user_id)) { return; } /** Ready *****************************************************************/ // Load up bbPress once $bbp = bbpress(); // Get whether or not to add a role to the user account $add_to_site = bbp_allow_global_access(); // Get the current user's WordPress role. Set to empty string if none found. $user_role = bbp_get_user_blog_role($user_id); // Get the role map $role_map = bbp_get_user_role_map(); /** Forum Role ************************************************************/ // Use a mapped role if (isset($role_map[$user_role])) { $new_role = $role_map[$user_role]; // Use the default role } else { $new_role = bbp_get_default_role(); } /** Add or Map ************************************************************/ // Add the user to the site if (true === $add_to_site) { // Make sure bbPress roles are added bbp_add_forums_roles(); $bbp->current_user->add_role($new_role); // Don't add the user, but still give them the correct caps dynamically } else { $bbp->current_user->caps[$new_role] = true; $bbp->current_user->get_role_caps(); } $new_role = bbp_get_default_role(); bbp_set_user_role($user_id, $new_role); }
/** * Maps forum capabilities * * @since bbPress (r4242) * * @param array $caps Capabilities for meta capability * @param string $cap Capability name * @param int $user_id User id * @param mixed $args Arguments * @uses get_post() To get the post * @uses get_post_type_object() To get the post type object * @uses apply_filters() Filter capability map results * @return array Actual capabilities for meta capability */ function bbp_map_forum_meta_caps($caps = array(), $cap = '', $user_id = 0, $args = array()) { // What capability is being checked? switch ($cap) { /** Reading ***********************************************************/ case 'read_private_forums': case 'read_hidden_forums': // Moderators can always read private/hidden forums if (user_can($user_id, 'moderate')) { $caps = array('moderate'); } break; case 'read_forum': // 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 caps for 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_forums': // Moderators can always edit if (user_can($user_id, 'moderate')) { $caps = array('moderate'); } break; /** Editing ***********************************************************/ // Used primarily in wp-admin /** Editing ***********************************************************/ // Used primarily in wp-admin case 'edit_forums': case 'edit_others_forums': // Moderators can always edit if (user_can($user_id, 'moderate')) { $caps = array('moderate'); } break; // Used everywhere // Used everywhere case 'edit_forum': // Get the post $_post = get_post($args[0]); if (!empty($_post)) { // Get caps for 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 } elseif ((int) $user_id == (int) $_post->post_author) { $caps[] = $post_type->cap->edit_posts; // Unknown, so map to edit_others_posts } else { $caps[] = $post_type->cap->edit_others_posts; } } break; /** Deleting **********************************************************/ // Allow forum authors to delete forums (for BuddyPress groups, etc) /** Deleting **********************************************************/ // Allow forum authors to delete forums (for BuddyPress groups, etc) case 'delete_forum': // Get the post $_post = get_post($args[0]); if (!empty($_post)) { // Get caps for 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 to delete } elseif ((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; /** Admin *************************************************************/ /** Admin *************************************************************/ case 'bbp_forums_admin': $caps = array('manage_options'); break; } return apply_filters('bbp_map_forum_meta_caps', $caps, $cap, $user_id, $args); }
/** * Callback to convert mention matchs to HTML A tag. * * @since 2.6.0 (r6014) * * @param array $matches Single Regex Match. * * @return string HTML A tag with link to user profile. */ function bbp_make_mentions_clickable_callback($matches = array()) { // Get user; bail if not found $user = get_user_by('slug', $matches[2]); if (empty($user) || bbp_is_user_inactive($user->ID)) { return $matches[0]; } // Create the link to the user's profile $url = bbp_get_user_profile_url($user->ID); $anchor = '<a href="%1$s" rel="nofollow">@%2$s</a>'; $link = sprintf($anchor, esc_url($url), esc_html($user->user_nicename)); return $matches[1] . $link; }
/** * Finds and links @-mentioned users in the content * * @since bbPress (r4323) * * @uses bbp_find_mentions() To get usernames in content areas * @return string $content Content filtered for mentions */ function bbp_mention_filter($content = '') { // Get Usernames and bail if none exist $usernames = bbp_find_mentions($content); if (empty($usernames)) { return $content; } // Loop through usernames and link to profiles foreach ((array) $usernames as $username) { // Skip if username does not exist or user is not active $user_id = username_exists($username); if (empty($user_id) || bbp_is_user_inactive($user_id)) { continue; } // Replace name in content $content = preg_replace('/(@' . $username . '\\b)/', "<a href='" . bbp_get_user_profile_url($user_id) . "' rel='nofollow' class='bbp-mention-link {$username}'>@{$username}</a>", $content); } // Return modified content return $content; }
/** * Return a user's main role for display * * @since bbPress (r3860) * * @param int $user_id * @uses bbp_get_user_id() to verify the user ID * @uses is_super_admin() to check if user is a super admin * @uses bbp_is_user_inactive() to check if user is inactive * @uses user_can() to check if user has special capabilities * @uses apply_filters() Calls 'bbp_get_user_display_role' with the * display role, user id, and user role * @return string */ function bbp_get_user_display_role($user_id = 0) { // Validate user id $user_id = bbp_get_user_id($user_id, false, false); // Capes earn Vinz Clortho status if (is_super_admin($user_id)) { $role = __('Key Master', 'bbpress'); // Inactive } elseif (bbp_is_user_inactive()) { $role = __('Inactive', 'bbpress'); // User is not registered } elseif (empty($user_id)) { $role = __('Guest', 'bbpress'); // Moderator } elseif (user_can($user_id, 'moderate')) { $role = __('Moderator', 'bbpress'); // Participant } elseif (user_can($user_id, 'participate')) { $role = __('Participant', 'bbpress'); // Anyone else } else { $role = __('Member', 'bbpress'); } return apply_filters('bbp_get_user_display_role', $role, $user_id); }