first commit
This commit is contained in:
860
wp-content/plugins/bbpress/includes/users/capabilities.php
Normal file
860
wp-content/plugins/bbpress/includes/users/capabilities.php
Normal file
@@ -0,0 +1,860 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* bbPress User Capabilites
|
||||
*
|
||||
* Used to map user capabilities to WordPress's existing capabilities.
|
||||
*
|
||||
* @package bbPress
|
||||
* @subpackage Capabilities
|
||||
*/
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @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' );
|
||||
|
||||
// Keymasters can always moderate.
|
||||
} elseif ( bbp_is_user_keymaster( $user_id ) ) {
|
||||
$caps = array( 'spectate' );
|
||||
|
||||
// Check if user can moderate forum.
|
||||
} elseif ( bbp_allow_forum_mods() ) {
|
||||
$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 = bbp_get_forum_id( $_post->ID );
|
||||
break;
|
||||
|
||||
// Topic.
|
||||
case bbp_get_topic_post_type() :
|
||||
$forum_id = bbp_get_topic_forum_id( $_post->ID );
|
||||
break;
|
||||
|
||||
// Reply.
|
||||
case bbp_get_reply_post_type() :
|
||||
$forum_id = bbp_get_reply_forum_id( $_post->ID );
|
||||
break;
|
||||
|
||||
// Any other post type defaults to 0.
|
||||
default :
|
||||
$forum_id = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
// Bail if no forum ID.
|
||||
if ( empty( $forum_id ) ) {
|
||||
break;
|
||||
}
|
||||
|
||||
// User is mod of this forum
|
||||
if ( bbp_is_object_of_user( $forum_id, $user_id, '_bbp_moderator_id' ) ) {
|
||||
$caps = array( 'spectate' );
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
/** Super Moderators **************************************************/
|
||||
|
||||
case 'edit_user' :
|
||||
case 'edit_users' :
|
||||
|
||||
// Moderators can edit users if super moderators is enabled
|
||||
if ( bbp_allow_super_mods() ) {
|
||||
|
||||
// Get the user ID
|
||||
$_user_id = ! empty( $args[0] )
|
||||
? (int) $args[0]
|
||||
: bbp_get_displayed_user_id();
|
||||
|
||||
// Users can always edit themselves, so only map for others
|
||||
if ( ! empty( $_user_id ) && ( $_user_id !== $user_id ) ) {
|
||||
|
||||
// Super moderators cannot edit keymasters
|
||||
if ( ! bbp_is_user_keymaster( $_user_id ) ) {
|
||||
$caps = array( 'moderate' );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
// Filter & return
|
||||
return (array) apply_filters( 'bbp_map_primary_meta_caps', $caps, $cap, $user_id, $args );
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a user's role in the forums
|
||||
*
|
||||
* @since 2.1.0 bbPress (r3860)
|
||||
*
|
||||
* @param int $user_id
|
||||
*
|
||||
* @return mixed False if no change. String of new role if changed.
|
||||
*/
|
||||
function bbp_set_user_role( $user_id = 0, $new_role = '' ) {
|
||||
|
||||
// Validate user id
|
||||
$user_id = bbp_get_user_id( $user_id, false, false );
|
||||
$user = get_userdata( $user_id );
|
||||
|
||||
// User exists
|
||||
if ( ! empty( $user ) ) {
|
||||
|
||||
// Get user forum role
|
||||
$role = bbp_get_user_role( $user_id );
|
||||
|
||||
// User already has this role so no new role is set
|
||||
if ( $new_role === $role ) {
|
||||
$new_role = false;
|
||||
|
||||
// User role is different than the new (valid) role
|
||||
} elseif ( bbp_is_valid_role( $new_role ) ) {
|
||||
|
||||
// Remove the old role
|
||||
if ( ! empty( $role ) ) {
|
||||
$user->remove_role( $role );
|
||||
}
|
||||
|
||||
// Add the new role
|
||||
if ( ! empty( $new_role ) ) {
|
||||
$user->add_role( $new_role );
|
||||
}
|
||||
}
|
||||
|
||||
// User does don exist so return false
|
||||
} else {
|
||||
$new_role = false;
|
||||
}
|
||||
|
||||
// Filter & return
|
||||
return apply_filters( 'bbp_set_user_role', $new_role, $user_id, $user );
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a user's forums role
|
||||
*
|
||||
* @since 2.1.0 bbPress (r3860)
|
||||
*
|
||||
* @param int $user_id
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function bbp_get_user_role( $user_id = 0 ) {
|
||||
|
||||
// Validate user id
|
||||
$user_id = bbp_get_user_id( $user_id );
|
||||
$user = get_userdata( $user_id );
|
||||
$role = false;
|
||||
|
||||
// User has roles so look for a bbPress one
|
||||
if ( ! empty( $user->roles ) ) {
|
||||
|
||||
// Look for a bbPress role
|
||||
$roles = array_intersect(
|
||||
array_values( $user->roles ),
|
||||
array_keys( bbp_get_dynamic_roles() )
|
||||
);
|
||||
|
||||
// If there's a role in the array, use the first one. This isn't very
|
||||
// smart, but since roles aren't exactly hierarchical, and bbPress
|
||||
// does not yet have a UI for multiple user roles, it's fine for now.
|
||||
if ( ! empty( $roles ) ) {
|
||||
$role = array_shift( $roles );
|
||||
}
|
||||
}
|
||||
|
||||
// Filter & return
|
||||
return apply_filters( 'bbp_get_user_role', $role, $user_id, $user );
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a user's blog role
|
||||
*
|
||||
* @since 2.3.0 bbPress (r4446)
|
||||
*
|
||||
* @param int $user_id
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function bbp_get_user_blog_role( $user_id = 0 ) {
|
||||
|
||||
// Validate user id
|
||||
$user_id = bbp_get_user_id( $user_id );
|
||||
$user = get_userdata( $user_id );
|
||||
$role = false;
|
||||
|
||||
// User has roles so lets
|
||||
if ( ! empty( $user->roles ) ) {
|
||||
|
||||
// Look for a non bbPress role
|
||||
$roles = array_intersect(
|
||||
array_values( $user->roles ),
|
||||
array_keys( bbp_get_blog_roles() )
|
||||
);
|
||||
|
||||
// If there's a role in the array, use the first one. This isn't very
|
||||
// smart, but since roles aren't exactly hierarchical, and WordPress
|
||||
// does not yet have a UI for multiple user roles, it's fine for now.
|
||||
if ( ! empty( $roles ) ) {
|
||||
$role = array_shift( $roles );
|
||||
}
|
||||
}
|
||||
|
||||
// Filter & return
|
||||
return apply_filters( 'bbp_get_user_blog_role', $role, $user_id, $user );
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function hooked to 'bbp_profile_update' action to save or
|
||||
* update user roles and capabilities.
|
||||
*
|
||||
* @since 2.2.0 bbPress (r4235)
|
||||
*
|
||||
* @param int $user_id
|
||||
*/
|
||||
function bbp_profile_update_role( $user_id = 0 ) {
|
||||
|
||||
// Bail if no user ID was passed
|
||||
if ( empty( $user_id ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Bail if no role
|
||||
if ( ! isset( $_POST['bbp-forums-role'] ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Forums role we want the user to have
|
||||
$new_role = sanitize_key( $_POST['bbp-forums-role'] );
|
||||
$forums_role = bbp_get_user_role( $user_id );
|
||||
|
||||
// Bail if no role change
|
||||
if ( $new_role === $forums_role ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Bail if trying to set their own role
|
||||
if ( bbp_is_user_home_edit() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Bail if current user cannot promote the passing user
|
||||
if ( ! current_user_can( 'promote_user', $user_id ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Set the new forums role
|
||||
bbp_set_user_role( $user_id, $new_role );
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a role string is valid
|
||||
*
|
||||
* @since 2.6.5
|
||||
*
|
||||
* @param string $role
|
||||
*
|
||||
* @return bool True if role is valid. False if role is not valid.
|
||||
*/
|
||||
function bbp_is_valid_role( $role = '' ) {
|
||||
|
||||
// Default return value
|
||||
$retval = false;
|
||||
|
||||
// Skip if no role to check
|
||||
if ( ! empty( $role ) && is_string( $role ) ) {
|
||||
|
||||
// Get the dynamic role IDs
|
||||
$roles = array_keys( bbp_get_dynamic_roles() );
|
||||
|
||||
// Skip if no known role IDs
|
||||
if ( ! empty( $roles ) ) {
|
||||
|
||||
// Is role in dynamic roles array?
|
||||
$retval = in_array( $role, $roles, true );
|
||||
}
|
||||
}
|
||||
|
||||
// Filter & return
|
||||
return (bool) apply_filters( 'bbp_is_valid_role', $retval, $role );
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 2.0.0 bbPress (r3380)
|
||||
*
|
||||
* @return If not multisite, not global, or user is deleted/spammed
|
||||
*/
|
||||
function bbp_set_current_user_default_role() {
|
||||
|
||||
/** Sanity ****************************************************************/
|
||||
|
||||
// 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 = bbp_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 or default role
|
||||
$new_role = empty( $user_role ) || ! isset( $role_map[ $user_role ] )
|
||||
? bbp_get_default_role()
|
||||
: $role_map[ $user_role ];
|
||||
|
||||
/** Add or Map ************************************************************/
|
||||
|
||||
// Add the user to the site
|
||||
if ( true === $add_to_site ) {
|
||||
bbp_set_user_role( $user_id, $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();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a map of WordPress roles to bbPress roles. Used to automatically grant
|
||||
* appropriate bbPress roles to WordPress users that wouldn't already have a
|
||||
* role in the forums. Also guarantees WordPress admins get the Keymaster role.
|
||||
*
|
||||
* @since 2.2.0 bbPress (r4334)
|
||||
*
|
||||
* @return array Filtered array of WordPress roles to bbPress roles
|
||||
*/
|
||||
function bbp_get_user_role_map() {
|
||||
|
||||
// Get the default role once here
|
||||
$default_role = bbp_get_default_role();
|
||||
|
||||
// Filter & return
|
||||
return (array) apply_filters( 'bbp_get_user_role_map', array(
|
||||
'administrator' => bbp_get_keymaster_role(),
|
||||
'editor' => $default_role,
|
||||
'author' => $default_role,
|
||||
'contributor' => $default_role,
|
||||
'subscriber' => $default_role
|
||||
) );
|
||||
}
|
||||
|
||||
/** User Status ***************************************************************/
|
||||
|
||||
/**
|
||||
* Checks if the user has been marked as a spammer.
|
||||
*
|
||||
* @since 2.0.0 bbPress (r3355)
|
||||
*
|
||||
* @param int $user_id int The ID for the user.
|
||||
* @return bool True if spammer, False if not.
|
||||
*/
|
||||
function bbp_is_user_spammer( $user_id = 0 ) {
|
||||
|
||||
// Default to current user
|
||||
if ( empty( $user_id ) && is_user_logged_in() ) {
|
||||
$user_id = bbp_get_current_user_id();
|
||||
}
|
||||
|
||||
// No user to check
|
||||
if ( empty( $user_id ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Assume user is not spam
|
||||
$is_spammer = false;
|
||||
|
||||
// Get user data
|
||||
$user = get_userdata( $user_id );
|
||||
|
||||
// No user found
|
||||
if ( empty( $user ) ) {
|
||||
$is_spammer = false;
|
||||
|
||||
// Check if spam
|
||||
} elseif ( ! empty( $user->spam ) ) {
|
||||
$is_spammer = true;
|
||||
}
|
||||
|
||||
// Filter & return
|
||||
return (bool) apply_filters( 'bbp_core_is_user_spammer', $is_spammer );
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark a users topics and replies as spam when the user is marked as spam
|
||||
*
|
||||
* @since 2.0.0 bbPress (r3405)
|
||||
*
|
||||
* @param int $user_id Optional. User ID to spam. Defaults to displayed user.
|
||||
*
|
||||
* @return bool If no user ID passed.
|
||||
*/
|
||||
function bbp_make_spam_user( $user_id = 0 ) {
|
||||
|
||||
// Use displayed user if it's not yourself
|
||||
if ( empty( $user_id ) && bbp_is_single_user() && ! bbp_is_user_home() ) {
|
||||
$user_id = bbp_get_displayed_user_id();
|
||||
}
|
||||
|
||||
// Bail if no user ID
|
||||
if ( empty( $user_id ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Bail if user ID is keymaster
|
||||
if ( bbp_is_user_keymaster( $user_id ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Arm the torpedos
|
||||
$bbp_db = bbp_db();
|
||||
|
||||
// Get the blog IDs of the user to mark as spam
|
||||
$blogs = get_blogs_of_user( $user_id, true );
|
||||
|
||||
// If user has no blogs, they are a guest on this site
|
||||
if ( empty( $blogs ) ) {
|
||||
$blogs[ $bbp_db->blogid ] = array();
|
||||
}
|
||||
|
||||
// Get array of post types to mark as spam
|
||||
$post_types = array( bbp_get_topic_post_type(), bbp_get_reply_post_type() );
|
||||
$post_types = "'" . implode( "', '", $post_types ) . "'";
|
||||
|
||||
// Get array of statuses to mark as spam
|
||||
$post_statuses = bbp_get_public_topic_statuses();
|
||||
$post_statuses = "'" . implode( "', '", $post_statuses ) . "'";
|
||||
|
||||
// Loop through blogs and remove their posts
|
||||
foreach ( (array) array_keys( $blogs ) as $blog_id ) {
|
||||
|
||||
// Switch to the site ID
|
||||
bbp_switch_to_site( $blog_id );
|
||||
|
||||
// Get topics and replies
|
||||
$query = $bbp_db->prepare( "SELECT ID FROM {$bbp_db->posts} WHERE post_author = %d AND post_status IN ( {$post_statuses} ) AND post_type IN ( {$post_types} )", $user_id );
|
||||
$posts = $bbp_db->get_col( $query );
|
||||
|
||||
// Loop through posts and spam them
|
||||
if ( ! empty( $posts ) ) {
|
||||
foreach ( $posts as $post_id ) {
|
||||
|
||||
// The routines for topics ang replies are different, so use the
|
||||
// correct one based on the post type
|
||||
switch ( get_post_type( $post_id ) ) {
|
||||
|
||||
case bbp_get_topic_post_type() :
|
||||
bbp_spam_topic( $post_id );
|
||||
break;
|
||||
|
||||
case bbp_get_reply_post_type() :
|
||||
bbp_spam_reply( $post_id );
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Switch back to current site
|
||||
bbp_restore_current_site();
|
||||
}
|
||||
|
||||
// Delete user options
|
||||
bbp_delete_user_options( $user_id );
|
||||
|
||||
// Success
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark a users topics and replies as spam when the user is marked as spam
|
||||
*
|
||||
* @since 2.0.0 bbPress (r3405)
|
||||
*
|
||||
* @param int $user_id Optional. User ID to unspam. Defaults to displayed user.
|
||||
*
|
||||
* @return bool If no user ID passed.
|
||||
*/
|
||||
function bbp_make_ham_user( $user_id = 0 ) {
|
||||
|
||||
// Use displayed user if it's not yourself
|
||||
if ( empty( $user_id ) && bbp_is_single_user() && ! bbp_is_user_home() ) {
|
||||
$user_id = bbp_get_displayed_user_id();
|
||||
}
|
||||
|
||||
// Bail if no user ID
|
||||
if ( empty( $user_id ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Bail if user ID is keymaster
|
||||
if ( bbp_is_user_keymaster( $user_id ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Arm the torpedos
|
||||
$bbp_db = bbp_db();
|
||||
|
||||
// Get the blog IDs of the user to mark as spam
|
||||
$blogs = get_blogs_of_user( $user_id, true );
|
||||
|
||||
// If user has no blogs, they are a guest on this site
|
||||
if ( empty( $blogs ) ) {
|
||||
$blogs[ $bbp_db->blogid ] = array();
|
||||
}
|
||||
|
||||
// Get array of post types to mark as spam
|
||||
$post_types = array( bbp_get_topic_post_type(), bbp_get_reply_post_type() );
|
||||
$post_types = "'" . implode( "', '", $post_types ) . "'";
|
||||
|
||||
// Get array of statuses to unmark as spam
|
||||
$post_statuses = array( bbp_get_spam_status_id() );
|
||||
$post_statuses = "'" . implode( "', '", $post_statuses ) . "'";
|
||||
|
||||
// Loop through blogs and remove their posts
|
||||
foreach ( (array) array_keys( $blogs ) as $blog_id ) {
|
||||
|
||||
// Switch to the site ID
|
||||
bbp_switch_to_site( $blog_id );
|
||||
|
||||
// Get topics and replies
|
||||
$query = $bbp_db->prepare( "SELECT ID FROM {$bbp_db->posts} WHERE post_author = %d AND post_status IN ( {$post_statuses} ) AND post_type IN ( {$post_types} )", $user_id );
|
||||
$posts = $bbp_db->get_col( $query );
|
||||
|
||||
// Loop through posts and spam them
|
||||
if ( ! empty( $posts ) ) {
|
||||
foreach ( $posts as $post_id ) {
|
||||
|
||||
// The routines for topics ang replies are different, so use the
|
||||
// correct one based on the post type
|
||||
switch ( get_post_type( $post_id ) ) {
|
||||
|
||||
case bbp_get_topic_post_type() :
|
||||
bbp_unspam_topic( $post_id );
|
||||
break;
|
||||
|
||||
case bbp_get_reply_post_type() :
|
||||
bbp_unspam_reply( $post_id );
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Switch back to current site
|
||||
bbp_restore_current_site();
|
||||
}
|
||||
|
||||
// Update topic & reply counts
|
||||
bbp_update_user_topic_count( $user_id, bbp_get_user_topic_count_raw( $user_id ) );
|
||||
bbp_update_user_reply_count( $user_id, bbp_get_user_reply_count_raw( $user_id ) );
|
||||
|
||||
// Update last posted (to now)
|
||||
bbp_update_user_last_posted( $user_id );
|
||||
|
||||
// Success
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the user has been marked as deleted.
|
||||
*
|
||||
* @since 2.0.0 bbPress (r3355)
|
||||
*
|
||||
* @param int $user_id int The ID for the user.
|
||||
* @return bool True if deleted, False if not.
|
||||
*/
|
||||
function bbp_is_user_deleted( $user_id = 0 ) {
|
||||
|
||||
// Default to current user
|
||||
if ( empty( $user_id ) && is_user_logged_in() ) {
|
||||
$user_id = bbp_get_current_user_id();
|
||||
}
|
||||
|
||||
// No user to check
|
||||
if ( empty( $user_id ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Assume user is not deleted
|
||||
$is_deleted = false;
|
||||
|
||||
// Get user data
|
||||
$user = get_userdata( $user_id );
|
||||
|
||||
// No user found
|
||||
if ( empty( $user ) ) {
|
||||
$is_deleted = true;
|
||||
|
||||
// Check if deleted
|
||||
} elseif ( ! empty( $user->deleted ) ) {
|
||||
$is_deleted = true;
|
||||
}
|
||||
|
||||
// Filter & return
|
||||
return (bool) apply_filters( 'bbp_core_is_user_deleted', $is_deleted );
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if user is active
|
||||
*
|
||||
* @since 2.0.0 bbPress (r3502)
|
||||
*
|
||||
* @param int $user_id The user ID to check
|
||||
* @return bool True if public, false if not
|
||||
*/
|
||||
function bbp_is_user_active( $user_id = 0 ) {
|
||||
|
||||
// No user to check
|
||||
$user_id = bbp_get_user_id( $user_id, false, true );
|
||||
if ( empty( $user_id ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check spam
|
||||
if ( bbp_is_user_spammer( $user_id ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check deleted
|
||||
if ( bbp_is_user_deleted( $user_id ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Assume true if not spam or deleted
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if user is not active.
|
||||
*
|
||||
* @since 2.0.0 bbPress (r3502)
|
||||
*
|
||||
* @param int $user_id The user ID to check. Defaults to current user ID
|
||||
* @return bool True if inactive, false if active
|
||||
*/
|
||||
function bbp_is_user_inactive( $user_id = 0 ) {
|
||||
return ! bbp_is_user_active( $user_id );
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if user is a keymaster
|
||||
*
|
||||
* @since 2.3.0 bbPress (r4783)
|
||||
*
|
||||
* @param int $user_id
|
||||
* @return bool True if keymaster, false if not
|
||||
*/
|
||||
function bbp_is_user_keymaster( $user_id = 0 ) {
|
||||
$_user_id = bbp_get_user_id( $user_id, false, true );
|
||||
$retval = user_can( $_user_id, 'keep_gate' );
|
||||
|
||||
// Filter & return
|
||||
return (bool) apply_filters( 'bbp_is_user_keymaster', $retval, $_user_id, $user_id );
|
||||
}
|
||||
|
||||
/**
|
||||
* Does a user have a profile for the current site
|
||||
*
|
||||
* @since 2.2.0 bbPress (r4362)
|
||||
*
|
||||
* @param int $user_id User ID to check
|
||||
*
|
||||
* @return bool 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 & return
|
||||
return (bool) apply_filters( 'bbp_show_user_profile', $retval, $user_id );
|
||||
}
|
||||
|
||||
/** Moderators ****************************************************************/
|
||||
|
||||
/**
|
||||
* Add a moderator to an object
|
||||
*
|
||||
* @since 2.6.0 bbPress (r6056)
|
||||
*
|
||||
* @param int $object_id Traditionally a post ID
|
||||
* @param int $user_id User ID
|
||||
* @param string $object_type Type of meta (post,term,user,comment)
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
function bbp_add_moderator( $object_id = 0, $user_id = 0, $object_type = 'post' ) {
|
||||
return bbp_add_user_to_object( $object_id, $user_id, '_bbp_moderator_id', $object_type );
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a moderator user ID from an object
|
||||
*
|
||||
* @since 2.6.0 bbPress (r6056)
|
||||
*
|
||||
* @param int $object_id Traditionally a post ID
|
||||
* @param int $user_id User ID
|
||||
* @param string $object_type Type of meta (post,term,user,comment)
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
function bbp_remove_moderator( $object_id = 0, $user_id = 0, $object_type = 'post' ) {
|
||||
return bbp_remove_user_from_object( $object_id, $user_id, '_bbp_moderator_id', $object_type );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get user IDs of moderators for an object
|
||||
*
|
||||
* @since 2.6.0 bbPress (r6056)
|
||||
*
|
||||
* @param int $object_id Traditionally a post ID
|
||||
* @param string $object_type Type of meta (post,term,user,comment)
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function bbp_get_moderator_ids( $object_id = 0, $object_type = 'post' ) {
|
||||
return bbp_get_users_for_object( $object_id, '_bbp_moderator_id', $object_type );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get moderators for a specific object ID. Will return global moderators when
|
||||
* object ID is empty.
|
||||
*
|
||||
* @since 2.6.0 bbPress (r6056)
|
||||
*
|
||||
* @param int $object_id Traditionally a post ID
|
||||
* @param string $object_type Type of meta (post,term,user,comment)
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function bbp_get_moderators( $object_id = 0, $object_type = 'post' ) {
|
||||
|
||||
// Get global moderators
|
||||
if ( empty( $object_id ) ) {
|
||||
$users = get_users( array(
|
||||
'role__in' => bbp_get_moderator_role(),
|
||||
) );
|
||||
|
||||
// Get object moderators
|
||||
} else {
|
||||
$users = get_users( array(
|
||||
'include' => bbp_get_moderator_ids( $object_id, $object_type ),
|
||||
) );
|
||||
}
|
||||
|
||||
// Filter & return
|
||||
return (array) apply_filters( 'bbp_get_moderators', $users, $object_id, $object_type );
|
||||
}
|
||||
1229
wp-content/plugins/bbpress/includes/users/engagements.php
Normal file
1229
wp-content/plugins/bbpress/includes/users/engagements.php
Normal file
File diff suppressed because it is too large
Load Diff
988
wp-content/plugins/bbpress/includes/users/functions.php
Normal file
988
wp-content/plugins/bbpress/includes/users/functions.php
Normal file
@@ -0,0 +1,988 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* bbPress User Functions
|
||||
*
|
||||
* @package bbPress
|
||||
* @subpackage Functions
|
||||
*/
|
||||
|
||||
// Exit if accessed directly
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Redirect back to $url when attempting to use the login page
|
||||
*
|
||||
* @since 2.0.0 bbPress (r2815)
|
||||
*
|
||||
* @param string $url The url
|
||||
* @param string $raw_url Raw url
|
||||
* @param object $user User object
|
||||
*/
|
||||
function bbp_redirect_login( $url = '', $raw_url = '', $user = '' ) {
|
||||
|
||||
// Raw redirect_to was passed, so use it
|
||||
if ( ! empty( $raw_url ) ) {
|
||||
$url = $raw_url;
|
||||
|
||||
// $url was manually set in wp-login.php to redirect to admin
|
||||
} elseif ( admin_url() === $url ) {
|
||||
$url = home_url();
|
||||
|
||||
// $url is empty
|
||||
} elseif ( empty( $url ) ) {
|
||||
$url = home_url();
|
||||
}
|
||||
|
||||
// Filter & return
|
||||
return apply_filters( 'bbp_redirect_login', $url, $raw_url, $user );
|
||||
}
|
||||
|
||||
/**
|
||||
* Is an anonymous topic/reply being made?
|
||||
*
|
||||
* @since 2.0.0 bbPress (r2688)
|
||||
*
|
||||
* @return bool True if anonymous is allowed and user is not logged in, false if
|
||||
* anonymous is not allowed or user is logged in
|
||||
*/
|
||||
function bbp_is_anonymous() {
|
||||
$is_anonymous = ( ! is_user_logged_in() && bbp_allow_anonymous() );
|
||||
|
||||
// Filter & return
|
||||
return (bool) apply_filters( 'bbp_is_anonymous', $is_anonymous );
|
||||
}
|
||||
|
||||
/**
|
||||
* Echoes the values for current poster (uses WP comment cookies)
|
||||
*
|
||||
* @since 2.0.0 bbPress (r2734)
|
||||
*
|
||||
* @param string $key Which value to echo?
|
||||
*/
|
||||
function bbp_current_anonymous_user_data( $key = '' ) {
|
||||
echo esc_attr( bbp_get_current_anonymous_user_data( $key ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the cookies for current poster (uses WP comment cookies).
|
||||
*
|
||||
* @since 2.0.0 bbPress (r2734)
|
||||
*
|
||||
* @param string $key Optional. Which value to get? If not given, then
|
||||
* an array is returned.
|
||||
* @return string|array Cookie(s) for current poster
|
||||
*/
|
||||
function bbp_get_current_anonymous_user_data( $key = '' ) {
|
||||
|
||||
// Array of allowed cookie names
|
||||
$cookie_names = array(
|
||||
'name' => 'comment_author',
|
||||
'email' => 'comment_author_email',
|
||||
'url' => 'comment_author_url',
|
||||
|
||||
// Here just for the sake of them, use the above ones
|
||||
'comment_author' => 'comment_author',
|
||||
'comment_author_email' => 'comment_author_email',
|
||||
'comment_author_url' => 'comment_author_url',
|
||||
);
|
||||
|
||||
// Get the current poster's info from the cookies
|
||||
$bbp_current_poster = wp_get_current_commenter();
|
||||
|
||||
// Sanitize the cookie key being retrieved
|
||||
$key = sanitize_key( $key );
|
||||
|
||||
// Maybe return a specific key
|
||||
if ( ! empty( $key ) && in_array( $key, array_keys( $cookie_names ), true ) ) {
|
||||
return $bbp_current_poster[ $cookie_names[ $key ] ];
|
||||
}
|
||||
|
||||
// Return all keys
|
||||
return $bbp_current_poster;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the cookies for current poster (uses WP comment cookies)
|
||||
*
|
||||
* @since 2.0.0 bbPress (r2734)
|
||||
*
|
||||
* @param array $anonymous_data Optional - if it's an anonymous post. Do not
|
||||
* supply if supplying $author_id. Should be
|
||||
* sanitized (see {@link bbp_filter_anonymous_post_data()}
|
||||
*/
|
||||
function bbp_set_current_anonymous_user_data( $anonymous_data = array() ) {
|
||||
|
||||
// Bail if empty or not an array
|
||||
if ( empty( $anonymous_data ) || ! is_array( $anonymous_data ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Setup cookie expiration
|
||||
$lifetime = (int) apply_filters( 'comment_cookie_lifetime', 30000000 );
|
||||
$expiry = time() + $lifetime;
|
||||
$secure = ( 'https' === parse_url( home_url(), PHP_URL_SCHEME ) );
|
||||
|
||||
// Set the cookies
|
||||
setcookie( 'comment_author_' . COOKIEHASH, $anonymous_data['bbp_anonymous_name'], $expiry, COOKIEPATH, COOKIE_DOMAIN, $secure );
|
||||
setcookie( 'comment_author_email_' . COOKIEHASH, $anonymous_data['bbp_anonymous_email'], $expiry, COOKIEPATH, COOKIE_DOMAIN, $secure );
|
||||
setcookie( 'comment_author_url_' . COOKIEHASH, $anonymous_data['bbp_anonymous_website'], $expiry, COOKIEPATH, COOKIE_DOMAIN, $secure );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the poster IP address
|
||||
*
|
||||
* @since 2.0.0 bbPress (r3120)
|
||||
* @since 2.6.0 bbPress (r5609) Added `empty()` check for unit tests
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function bbp_current_author_ip() {
|
||||
|
||||
// Check for remote address
|
||||
$remote_address = ! empty( $_SERVER['REMOTE_ADDR'] )
|
||||
? wp_unslash( $_SERVER['REMOTE_ADDR'] )
|
||||
: '127.0.0.1';
|
||||
|
||||
// Remove any unsavory bits
|
||||
$retval = preg_replace( '/[^0-9a-fA-F:., ]/', '', $remote_address );
|
||||
|
||||
// Filter & return
|
||||
return apply_filters( 'bbp_current_author_ip', $retval, $remote_address );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the poster user agent
|
||||
*
|
||||
* @since 2.0.0 bbPress (r3446)
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function bbp_current_author_ua() {
|
||||
$retval = ! empty( $_SERVER['HTTP_USER_AGENT'] )
|
||||
? mb_substr( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ), 0, 254 )
|
||||
: '';
|
||||
|
||||
// Filter & return
|
||||
return apply_filters( 'bbp_current_author_ua', $retval );
|
||||
}
|
||||
|
||||
/** Edit **********************************************************************/
|
||||
|
||||
/**
|
||||
* Handles the front end user editing from POST requests
|
||||
*
|
||||
* @since 2.0.0 bbPress (r2790)
|
||||
*
|
||||
* @param string $action The requested action to compare this function to
|
||||
*/
|
||||
function bbp_edit_user_handler( $action = '' ) {
|
||||
|
||||
// Bail if action is not `bbp-update-user`
|
||||
if ( 'bbp-update-user' !== $action ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Bail if in wp-admin
|
||||
if ( is_admin() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Get the displayed user ID
|
||||
$user_id = bbp_get_displayed_user_id();
|
||||
|
||||
// Nonce check
|
||||
if ( ! bbp_verify_nonce_request( 'update-user_' . $user_id ) ) {
|
||||
bbp_add_error( 'bbp_update_user_nonce', __( '<strong>Error</strong>: Are you sure you wanted to do that?', 'bbpress' ) );
|
||||
return;
|
||||
}
|
||||
|
||||
// Cap check
|
||||
if ( ! current_user_can( 'edit_user', $user_id ) ) {
|
||||
bbp_add_error( 'bbp_update_user_capability', __( '<strong>Error</strong>: Are you sure you wanted to do that?', 'bbpress' ) );
|
||||
return;
|
||||
}
|
||||
|
||||
// Empty email check
|
||||
if ( empty( $_POST['email'] ) ) {
|
||||
bbp_add_error( 'bbp_user_email_empty', __( '<strong>Error</strong>: That is not a valid email address.', 'bbpress' ), array( 'form-field' => 'email' ) );
|
||||
return;
|
||||
}
|
||||
|
||||
// Get the users current email address to use for comparisons
|
||||
$user_email = bbp_get_displayed_user_field( 'user_email', 'raw' );
|
||||
|
||||
// Bail if no email change
|
||||
if ( $user_email !== $_POST['email'] ) {
|
||||
|
||||
// Check that new email address is valid
|
||||
if ( ! is_email( $_POST['email'] ) ) {
|
||||
bbp_add_error( 'bbp_user_email_invalid', __( '<strong>Error</strong>: That is not a valid email address.', 'bbpress' ), array( 'form-field' => 'email' ) );
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if email address is already in use
|
||||
if ( email_exists( $_POST['email'] ) ) {
|
||||
bbp_add_error( 'bbp_user_email_taken', __( '<strong>Error</strong>: That email address is already in use.', 'bbpress' ), array( 'form-field' => 'email' ) );
|
||||
return;
|
||||
}
|
||||
|
||||
// Update the option
|
||||
$option = array(
|
||||
'hash' => md5( $_POST['email'] . time() . wp_rand() ),
|
||||
'newemail' => $_POST['email'],
|
||||
);
|
||||
update_user_meta( $user_id, '_new_email', $option );
|
||||
|
||||
// Attempt to notify the user of email address change
|
||||
bbp_edit_user_email_send_notification( $user_id, $option );
|
||||
|
||||
// Set the POST email variable back to the user's email address
|
||||
// so `edit_user()` does not attempt to update it. This is not ideal,
|
||||
// but it's also what send_confirmation_on_profile_email() does.
|
||||
$_POST['email'] = $user_email;
|
||||
}
|
||||
|
||||
// Do action based on who's profile you're editing
|
||||
$edit_action = bbp_is_user_home_edit()
|
||||
? 'personal_options_update'
|
||||
: 'edit_user_profile_update';
|
||||
|
||||
do_action( $edit_action, $user_id );
|
||||
|
||||
// Prevent edit_user() from wiping out the user's Toolbar on front setting
|
||||
if ( ! isset( $_POST['admin_bar_front'] ) && _get_admin_bar_pref( 'front', $user_id ) ) {
|
||||
$_POST['admin_bar_front'] = 1;
|
||||
}
|
||||
|
||||
// Bail if errors already exist
|
||||
if ( bbp_has_errors() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Handle user edit
|
||||
$edit_user = edit_user( $user_id );
|
||||
|
||||
// Error(s) editng the user, so copy them into the global
|
||||
if ( is_wp_error( $edit_user ) ) {
|
||||
bbpress()->errors = $edit_user;
|
||||
|
||||
// Successful edit to redirect
|
||||
} elseif ( is_integer( $edit_user ) ) {
|
||||
|
||||
// Maybe update super admin ability
|
||||
if ( is_multisite() && ! bbp_is_user_home_edit() && current_user_can( 'manage_network_options' ) && is_super_admin() ) {
|
||||
empty( $_POST['super_admin'] )
|
||||
? revoke_super_admin( $edit_user )
|
||||
: grant_super_admin( $edit_user );
|
||||
}
|
||||
|
||||
// Redirect
|
||||
$args = array( 'updated' => 'true' );
|
||||
$user_url = bbp_get_user_profile_edit_url( $edit_user );
|
||||
$redirect = add_query_arg( $args, $user_url );
|
||||
|
||||
bbp_redirect( $redirect );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles user email address updating from GET requests
|
||||
*
|
||||
* @since 2.6.0 bbPress (r5660)
|
||||
*
|
||||
* @param string $action
|
||||
*/
|
||||
function bbp_user_email_change_handler( $action = '' ) {
|
||||
|
||||
// Bail if action is not `bbp-update-user-email`
|
||||
if ( 'bbp-update-user-email' !== $action ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Bail if not on users own profile
|
||||
if ( ! bbp_is_user_home_edit() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Bail if not attempting to modify user email address
|
||||
if ( empty( $_GET['newuseremail'] ) && empty( $_GET['dismiss'] ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Get the displayed user ID & option key
|
||||
$user_id = bbp_get_displayed_user_id();
|
||||
$key = '_new_email';
|
||||
$redirect_to = bbp_get_user_profile_edit_url( $user_id );
|
||||
|
||||
// Execute confirmed email change.
|
||||
if ( ! empty( $_GET['newuseremail'] ) ) {
|
||||
|
||||
// Check for email address change option
|
||||
$new_email = get_user_meta( $user_id, $key, true );
|
||||
|
||||
// Redirect if *no* email address change exists
|
||||
if ( false === $new_email ) {
|
||||
bbp_redirect( $redirect_to );
|
||||
}
|
||||
|
||||
// Cleanup & redirect if *invalid* email address change exists
|
||||
if ( empty( $new_email['hash'] ) || empty( $new_email['newemail'] ) ) {
|
||||
delete_user_meta( $user_id, $key );
|
||||
|
||||
bbp_redirect( $redirect_to );
|
||||
}
|
||||
|
||||
// Compare hashes, and update user if hashes match
|
||||
if ( hash_equals( $new_email['hash'], $_GET['newuseremail'] ) ) {
|
||||
|
||||
// Does another user have this email address already?
|
||||
if ( email_exists( $new_email['newemail'] ) ) {
|
||||
delete_user_meta( $user_id, $key );
|
||||
|
||||
bbp_add_error( 'bbp_user_email_taken', __( '<strong>Error</strong>: That email address is already in use.', 'bbpress' ), array( 'form-field' => 'email' ) );
|
||||
|
||||
// Email address is good to change to
|
||||
} else {
|
||||
|
||||
// Create a stdClass (for easy call to wp_update_user())
|
||||
$user = new stdClass();
|
||||
$user->ID = $user_id;
|
||||
$user->user_email = esc_html( trim( $new_email['newemail'] ) );
|
||||
|
||||
// Attempt to update user email
|
||||
$update_user = wp_update_user( $user );
|
||||
|
||||
// Error(s) editing the user, so copy them into the global
|
||||
if ( is_wp_error( $update_user ) ) {
|
||||
bbpress()->errors = $update_user;
|
||||
|
||||
// All done, so redirect and show the updated message
|
||||
} else {
|
||||
|
||||
// Update signups table, if signups table & entry exists
|
||||
// For Multisite & BuddyPress compatibility
|
||||
$bbp_db = bbp_db();
|
||||
if ( ! empty( $bbp_db->signups ) && $bbp_db->get_var( $bbp_db->prepare( "SELECT user_login FROM {$bbp_db->signups} WHERE user_login = %s", bbp_get_displayed_user_field( 'user_login', 'raw' ) ) ) ) {
|
||||
$bbp_db->query( $bbp_db->prepare( "UPDATE {$bbp_db->signups} SET user_email = %s WHERE user_login = %s", $user->user_email, bbp_get_displayed_user_field( 'user_login', 'raw' ) ) );
|
||||
}
|
||||
|
||||
delete_user_meta( $user_id, $key );
|
||||
|
||||
bbp_redirect( add_query_arg( array( 'updated' => 'true' ), $redirect_to ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Delete new email address from user options
|
||||
} elseif ( ! empty( $_GET['dismiss'] ) && ( "{$user_id}{$key}" === $_GET['dismiss'] ) ) {
|
||||
if ( ! bbp_verify_nonce_request( "dismiss-{$user_id}{$key}" ) ) {
|
||||
bbp_add_error( 'bbp_dismiss_new_email_nonce', __( '<strong>Error</strong>: Are you sure you wanted to do that?', 'bbpress' ) );
|
||||
return;
|
||||
}
|
||||
|
||||
delete_user_meta( $user_id, $key );
|
||||
bbp_redirect( $redirect_to );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends an email when an email address change occurs on POST requests
|
||||
*
|
||||
* @since 2.6.0 bbPress (r5660)
|
||||
*
|
||||
* @see send_confirmation_on_profile_email()
|
||||
*/
|
||||
function bbp_edit_user_email_send_notification( $user_id = 0, $args = array() ) {
|
||||
|
||||
// Parse args
|
||||
$r = bbp_parse_args( $args, array(
|
||||
'hash' => '',
|
||||
'newemail' => '',
|
||||
) );
|
||||
|
||||
// Bail if any relevant parameters are empty
|
||||
if ( empty( $user_id ) || empty( $r['hash'] ) || empty( $r['newemail'] ) ) {
|
||||
bbp_add_error( 'bbp_user_email_invalid_hash', __( '<strong>Error</strong>: An error occurred while updating your email address.', 'bbpress' ), array( 'form-field' => 'email' ) );
|
||||
return;
|
||||
}
|
||||
|
||||
// Build the nonced URL to dismiss the pending change
|
||||
$user_login = bbp_get_displayed_user_field( 'user_login', 'raw' );
|
||||
$user_url = bbp_get_user_profile_edit_url( $user_id );
|
||||
$confirm_url = add_query_arg( array(
|
||||
'action' => 'bbp-update-user-email',
|
||||
'newuseremail' => $r['hash']
|
||||
), $user_url );
|
||||
|
||||
$email_text = __( '%1$s
|
||||
|
||||
Someone requested a change to the email address on your account.
|
||||
|
||||
Please click the following link to confirm this change:
|
||||
%2$s
|
||||
|
||||
If you did not request this, you can safely ignore and delete this notification.
|
||||
|
||||
This email was sent to: %3$s
|
||||
|
||||
Regards,
|
||||
The %4$s Team
|
||||
%5$s', 'bbpress' );
|
||||
|
||||
/**
|
||||
* Filter the email text sent when a user changes emails.
|
||||
*
|
||||
* The following strings have a special meaning and will get replaced dynamically:
|
||||
*
|
||||
* %1$s - The current user's username
|
||||
* %2$s - The link to click on to confirm the email change
|
||||
* %3$s - The new email
|
||||
* %4$s - The name of the site
|
||||
* %5$s - The URL to the site
|
||||
*
|
||||
* @param string $email_text Text in the email.
|
||||
* @param string $r New user email that the current user has changed to.
|
||||
*/
|
||||
$content = apply_filters( 'bbp_user_email_update_content', $email_text, $r );
|
||||
|
||||
// Build the email message
|
||||
$message = sprintf( $content, $user_login, $confirm_url, $r['newemail'], get_site_option( 'site_name' ), network_home_url() );
|
||||
|
||||
// Build the email subject
|
||||
$subject = sprintf( __( '[%s] New Email Address', 'bbpress' ), wp_specialchars_decode( get_option( 'blogname' ) ) );
|
||||
|
||||
// Send the email
|
||||
wp_mail( $r['newemail'], $subject, $message );
|
||||
}
|
||||
|
||||
/**
|
||||
* Conditionally hook the core WordPress output actions to the end of the
|
||||
* default user's edit profile template
|
||||
*
|
||||
* This allows clever plugin authors to conditionally unhook the WordPress core
|
||||
* output actions if they don't want any unexpected junk to appear there, and
|
||||
* also avoids needing to pollute the templates with additional logic and actions.
|
||||
*
|
||||
* @since 2.2.0 bbPress (r4273)
|
||||
*/
|
||||
function bbp_user_edit_after() {
|
||||
$action = bbp_is_user_home_edit() ? 'show_user_profile' : 'edit_user_profile';
|
||||
|
||||
do_action( $action, get_userdata( bbp_get_displayed_user_id() ) );
|
||||
}
|
||||
|
||||
/** User Queries **************************************************************/
|
||||
|
||||
/**
|
||||
* Get the topics that a user created
|
||||
*
|
||||
* @since 2.0.0 bbPress (r2660)
|
||||
* @since 2.6.0 bbPress (r6618) Signature changed to accept an array of arguments
|
||||
*
|
||||
* @param array $args Optional. Arguments to pass into bbp_has_topics()
|
||||
*
|
||||
* @return bool True if user has started topics, otherwise false
|
||||
*/
|
||||
function bbp_get_user_topics_started( $args = array() ) {
|
||||
|
||||
// Backwards compat for pre-2.6.0
|
||||
if ( is_numeric( $args ) ) {
|
||||
$args = array(
|
||||
'author' => bbp_get_user_id( $args, false, false )
|
||||
);
|
||||
}
|
||||
|
||||
// Default arguments
|
||||
$defaults = array(
|
||||
'author' => bbp_get_displayed_user_id()
|
||||
);
|
||||
|
||||
// Parse arguments
|
||||
$r = bbp_parse_args( $args, $defaults, 'get_user_topics_started' );
|
||||
|
||||
// Get the topics
|
||||
$query = bbp_has_topics( $r );
|
||||
$user_id = $r['author'];
|
||||
|
||||
// Filter & return
|
||||
return apply_filters( 'bbp_get_user_topics_started', $query, $user_id, $r, $args );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the replies that a user created
|
||||
*
|
||||
* @since 2.2.0 bbPress (r4225)
|
||||
* @since 2.6.0 bbPress (r6618) Signature changed to accept an array of arguments
|
||||
*
|
||||
* @param array $args Optional. Arguments to pass into bbp_has_replies()
|
||||
*
|
||||
* @return bool True if user has created replies, otherwise false
|
||||
*/
|
||||
function bbp_get_user_replies_created( $args = array() ) {
|
||||
|
||||
// Backwards compat for pre-2.6.0
|
||||
if ( is_numeric( $args ) ) {
|
||||
$args = array(
|
||||
'author' => bbp_get_user_id( $args, false, false ),
|
||||
'post_type' => bbp_get_reply_post_type(),
|
||||
'order' => 'DESC'
|
||||
);
|
||||
}
|
||||
|
||||
// Default arguments
|
||||
$defaults = array(
|
||||
'author' => bbp_get_displayed_user_id(),
|
||||
'post_type' => bbp_get_reply_post_type(),
|
||||
'order' => 'DESC'
|
||||
);
|
||||
|
||||
// Parse arguments
|
||||
$r = bbp_parse_args( $args, $defaults, 'get_user_replies_created' );
|
||||
|
||||
// Get the replies
|
||||
$query = bbp_has_replies( $r );
|
||||
$user_id = $r['author'];
|
||||
|
||||
// Filter & return
|
||||
return apply_filters( 'bbp_get_user_replies_created', $query, $user_id, $r, $args );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get user IDs from nicenames
|
||||
*
|
||||
* This function is primarily used when saving object moderators
|
||||
*
|
||||
* @since 2.6.0 bbPress
|
||||
*
|
||||
* @param mixed $user_nicenames
|
||||
* @return array
|
||||
*/
|
||||
function bbp_get_user_ids_from_nicenames( $user_nicenames = array() ) {
|
||||
|
||||
// Default value
|
||||
$retval = array();
|
||||
|
||||
// Only query if nicenames
|
||||
if ( ! empty( $user_nicenames ) ) {
|
||||
|
||||
// Maybe explode by comma
|
||||
$user_nicenames = ( is_string( $user_nicenames ) && strstr( $user_nicenames, ',' ) )
|
||||
? explode( ',', $user_nicenames )
|
||||
: (array) $user_nicenames;
|
||||
|
||||
// Sanitize each nicename in the array
|
||||
$user_nicenames = array_map( 'sanitize_title', $user_nicenames );
|
||||
|
||||
// Get users
|
||||
$users = get_users( array(
|
||||
'nicename__in' => $user_nicenames
|
||||
) );
|
||||
|
||||
// Pluck or empty
|
||||
if ( ! empty( $users ) ) {
|
||||
$retval = wp_list_pluck( $users, 'ID' );
|
||||
}
|
||||
}
|
||||
|
||||
// Filter & return
|
||||
return (array) apply_filters( 'bbp_get_user_ids_from_nicenames', $retval, $user_nicenames );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get user nicenames from IDs
|
||||
*
|
||||
* This function is primarily used when saving object moderators
|
||||
*
|
||||
* @since 2.6.0 bbPress
|
||||
*
|
||||
* @param mixed $user_ids
|
||||
* @return array
|
||||
*/
|
||||
function bbp_get_user_nicenames_from_ids( $user_ids = array() ) {
|
||||
|
||||
// Default value
|
||||
$retval = array();
|
||||
|
||||
// Only query if nicenames
|
||||
if ( ! empty( $user_ids ) ) {
|
||||
|
||||
// Get users
|
||||
$users = get_users( array(
|
||||
'include' => $user_ids
|
||||
) );
|
||||
|
||||
// Pluck or empty
|
||||
if ( ! empty( $users ) ) {
|
||||
$retval = wp_list_pluck( $users, 'user_nicename' );
|
||||
}
|
||||
}
|
||||
|
||||
// Filter & return
|
||||
return (array) apply_filters( 'bbp_get_user_nicenames_from_ids', $retval, $user_ids );
|
||||
}
|
||||
|
||||
/** Post Counts ***************************************************************/
|
||||
|
||||
/**
|
||||
* Return the raw database count of topics by a user
|
||||
*
|
||||
* @since 2.1.0 bbPress (r3633)
|
||||
*
|
||||
* @param int $user_id User ID to get count for
|
||||
*
|
||||
* @return int Raw DB count of topics
|
||||
*/
|
||||
function bbp_get_user_topic_count_raw( $user_id = 0 ) {
|
||||
$user_id = bbp_get_user_id( $user_id );
|
||||
$bbp_db = bbp_db();
|
||||
$statii = "'" . implode( "', '", bbp_get_public_topic_statuses() ) . "'";
|
||||
$sql = "SELECT COUNT(*)
|
||||
FROM {$bbp_db->posts}
|
||||
WHERE post_author = %d
|
||||
AND post_type = %s
|
||||
AND post_status IN ({$statii})";
|
||||
|
||||
$query = $bbp_db->prepare( $sql, $user_id, bbp_get_topic_post_type() );
|
||||
$count = (int) $bbp_db->get_var( $query );
|
||||
|
||||
// Filter & return
|
||||
return (int) apply_filters( 'bbp_get_user_topic_count_raw', $count, $user_id );
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the raw database count of replies by a user
|
||||
*
|
||||
* @since 2.1.0 bbPress (r3633)
|
||||
*
|
||||
* @param int $user_id User ID to get count for
|
||||
*
|
||||
* @return int Raw DB count of replies
|
||||
*/
|
||||
function bbp_get_user_reply_count_raw( $user_id = 0 ) {
|
||||
$user_id = bbp_get_user_id( $user_id );
|
||||
$bbp_db = bbp_db();
|
||||
$statii = "'" . implode( "', '", bbp_get_public_reply_statuses() ) . "'";
|
||||
$sql = "SELECT COUNT(*)
|
||||
FROM {$bbp_db->posts}
|
||||
WHERE post_author = %d
|
||||
AND post_type = %s
|
||||
AND post_status IN ({$statii})";
|
||||
|
||||
$query = $bbp_db->prepare( $sql, $user_id, bbp_get_reply_post_type() );
|
||||
$count = (int) $bbp_db->get_var( $query );
|
||||
|
||||
// Filter & return
|
||||
return (int) apply_filters( 'bbp_get_user_reply_count_raw', $count, $user_id );
|
||||
}
|
||||
|
||||
/**
|
||||
* Bump the topic count for a user by a certain amount.
|
||||
*
|
||||
* @since 2.6.0 bbPress (r5309)
|
||||
*
|
||||
* @param int $user_id
|
||||
* @param int $difference
|
||||
*/
|
||||
function bbp_bump_user_topic_count( $user_id = 0, $difference = 1 ) {
|
||||
|
||||
// Bail if no bump
|
||||
if ( empty( $difference ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Validate user ID
|
||||
$user_id = bbp_get_user_id( $user_id );
|
||||
if ( empty( $user_id ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check meta for count, or query directly if not found
|
||||
$count = bbp_get_user_topic_count( $user_id, true );
|
||||
if ( empty( $count ) ) {
|
||||
$count = bbp_get_user_topic_count_raw( $user_id );
|
||||
}
|
||||
|
||||
$difference = (int) $difference;
|
||||
$user_topic_count = (int) ( $count + $difference );
|
||||
|
||||
// Add them up and filter them
|
||||
$new_count = (int) apply_filters( 'bbp_bump_user_topic_count', $user_topic_count, $user_id, $difference, $count );
|
||||
|
||||
return bbp_update_user_topic_count( $user_id, $new_count );
|
||||
}
|
||||
|
||||
/**
|
||||
* Bump the reply count for a user by a certain amount.
|
||||
*
|
||||
* @since 2.6.0 bbPress (r5309)
|
||||
*
|
||||
* @param int $user_id
|
||||
* @param int $difference
|
||||
*/
|
||||
function bbp_bump_user_reply_count( $user_id = 0, $difference = 1 ) {
|
||||
|
||||
// Bail if no bump
|
||||
if ( empty( $difference ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Validate user ID
|
||||
$user_id = bbp_get_user_id( $user_id );
|
||||
if ( empty( $user_id ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check meta for count, or query directly if not found
|
||||
$count = bbp_get_user_reply_count( $user_id, true );
|
||||
if ( empty( $count ) ) {
|
||||
$count = bbp_get_user_reply_count_raw( $user_id );
|
||||
}
|
||||
|
||||
$difference = (int) $difference;
|
||||
$user_reply_count = (int) ( $count + $difference );
|
||||
|
||||
// Add them up and filter them
|
||||
$new_count = (int) apply_filters( 'bbp_bump_user_reply_count', $user_reply_count, $user_id, $difference, $count );
|
||||
|
||||
return bbp_update_user_reply_count( $user_id, $new_count );
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function used to increase (by one) the count of topics for a user when
|
||||
* a topic is published.
|
||||
*
|
||||
* @since 2.6.0 bbPress (r5309)
|
||||
*
|
||||
* @access
|
||||
* @param $topic_id
|
||||
* @param $forum_id
|
||||
* @param $anonymous_data
|
||||
* @param $topic_author
|
||||
*/
|
||||
function bbp_increase_user_topic_count( $topic_id = 0 ) {
|
||||
$user_id = bbp_get_topic_author_id( $topic_id );
|
||||
return bbp_bump_user_topic_count( $user_id, 1 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function used to increase (by one) the count of replies for a user when
|
||||
* a reply is published.
|
||||
*
|
||||
* This is a helper function, hooked to `bbp_new_reply`
|
||||
*
|
||||
* @since 2.6.0 bbPress (r5309)
|
||||
*
|
||||
* @param $topic_id
|
||||
* @param $forum_id
|
||||
* @param $anonymous_data
|
||||
* @param $topic_author
|
||||
*/
|
||||
function bbp_increase_user_reply_count( $reply_id = 0 ) {
|
||||
$user_id = bbp_get_reply_author_id( $reply_id );
|
||||
return bbp_bump_user_reply_count( $user_id, 1 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function used to decrease (by one) the count of topics for a user when
|
||||
* a topic is unpublished.
|
||||
*
|
||||
* @since 2.6.0 bbPress (r5309)
|
||||
*
|
||||
* @param $topic_id
|
||||
*/
|
||||
function bbp_decrease_user_topic_count( $topic_id = 0 ) {
|
||||
$user_id = bbp_get_topic_author_id( $topic_id );
|
||||
return bbp_bump_user_topic_count( $user_id, -1 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function used to increase (by one) the count of replies for a user when
|
||||
* a topic is unpublished.
|
||||
*
|
||||
* @since 2.6.0 bbPress (r5309)
|
||||
*
|
||||
* @param $reply_id
|
||||
*/
|
||||
function bbp_decrease_user_reply_count( $reply_id = 0 ) {
|
||||
$user_id = bbp_get_reply_author_id( $reply_id );
|
||||
return bbp_bump_user_reply_count( $user_id, -1 );
|
||||
}
|
||||
|
||||
/** Permissions ***************************************************************/
|
||||
|
||||
/**
|
||||
* Redirect if unauthorized user is attempting to edit another user
|
||||
*
|
||||
* This is hooked to 'bbp_template_redirect' and controls the conditions under
|
||||
* which a user can edit another user (or themselves.) If these conditions are
|
||||
* met, we assume a user cannot perform this task, and look for ways they can
|
||||
* earn the ability to access this template.
|
||||
*
|
||||
* @since 2.1.0 bbPress (r3605)
|
||||
*/
|
||||
function bbp_check_user_edit() {
|
||||
|
||||
// Bail if not editing a user
|
||||
if ( ! bbp_is_single_user_edit() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Default to false
|
||||
$redirect = true;
|
||||
$user_id = bbp_get_displayed_user_id();
|
||||
|
||||
// Allow user to edit their own profile
|
||||
if ( bbp_is_user_home_edit() ) {
|
||||
$redirect = false;
|
||||
|
||||
// Allow if current user can edit the displayed user
|
||||
} elseif ( current_user_can( 'edit_user', $user_id ) ) {
|
||||
$redirect = false;
|
||||
|
||||
// Allow if user can manage network users, or edit-any is enabled
|
||||
} elseif ( current_user_can( 'manage_network_users' ) || apply_filters( 'enable_edit_any_user_configuration', false ) ) {
|
||||
$redirect = false;
|
||||
}
|
||||
|
||||
// Allow conclusion to be overridden
|
||||
$redirect = (bool) apply_filters( 'bbp_check_user_edit', $redirect, $user_id );
|
||||
|
||||
// Bail if not redirecting
|
||||
if ( false === $redirect ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Filter redirect URL
|
||||
$profile_url = bbp_get_user_profile_url( $user_id );
|
||||
$redirect_to = apply_filters( 'bbp_check_user_edit_redirect_to', $profile_url, $user_id );
|
||||
|
||||
// Redirect
|
||||
bbp_redirect( $redirect_to );
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a user is blocked, or cannot spectate the forums.
|
||||
*
|
||||
* @since 2.0.0 bbPress (r2996)
|
||||
*/
|
||||
function bbp_forum_enforce_blocked() {
|
||||
|
||||
// Bail if not logged in or keymaster
|
||||
if ( ! is_user_logged_in() || bbp_is_user_keymaster() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Set 404 if in bbPress and user cannot spectate
|
||||
if ( is_bbpress() && ! current_user_can( 'spectate' ) ) {
|
||||
bbp_set_404();
|
||||
}
|
||||
}
|
||||
|
||||
/** Sanitization **************************************************************/
|
||||
|
||||
/**
|
||||
* Sanitize displayed user data, when viewing and editing any user.
|
||||
*
|
||||
* This somewhat monolithic function handles the escaping and sanitization of
|
||||
* user data for a bbPress profile. There are two reasons this all happens here:
|
||||
*
|
||||
* 1. bbPress took a similar approach to WordPress, and funnels all user profile
|
||||
* data through a central helper. This eventually calls sanitize_user_field()
|
||||
* which applies a few context based filters, which some third party plugins
|
||||
* might be relying on bbPress to play nicely with.
|
||||
*
|
||||
* 2. Early versions of bbPress 2.x templates did not escape this data meaning
|
||||
* a backwards compatible approach like this one was necessary to protect
|
||||
* existing installations that may have custom template parts.
|
||||
*
|
||||
* @since 2.6.0 bbPress (r5368)
|
||||
*
|
||||
* @param string $value
|
||||
* @param string $field
|
||||
* @param string $context
|
||||
* @return string
|
||||
*/
|
||||
function bbp_sanitize_displayed_user_field( $value = '', $field = '', $context = 'display' ) {
|
||||
|
||||
// Bail if not editing or displaying (maybe we'll do more here later)
|
||||
if ( ! in_array( $context, array( 'edit', 'display' ), true ) ) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
// By default, no filter set (consider making this an array later)
|
||||
$filter = false;
|
||||
|
||||
// Big switch statement to decide which user field we're sanitizing and how
|
||||
switch ( $field ) {
|
||||
|
||||
// Description is a paragraph
|
||||
case 'description' :
|
||||
$filter = ( 'edit' === $context ) ? '' : 'wp_kses_data';
|
||||
break;
|
||||
|
||||
// Email addresses are sanitized with a specific function
|
||||
case 'user_email' :
|
||||
$filter = 'sanitize_email';
|
||||
break;
|
||||
|
||||
// Name & login fields
|
||||
case 'user_login' :
|
||||
case 'display_name' :
|
||||
case 'first_name' :
|
||||
case 'last_name' :
|
||||
case 'nick_name' :
|
||||
$filter = ( 'edit' === $context ) ? 'esc_attr' : 'esc_html';
|
||||
break;
|
||||
|
||||
// wp-includes/default-filters.php escapes this for us via esc_url()
|
||||
case 'user_url' :
|
||||
break;
|
||||
}
|
||||
|
||||
// Run any applicable filters on the value
|
||||
if ( ! empty( $filter ) ) {
|
||||
$value = call_user_func( $filter, $value );
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/** Converter *****************************************************************/
|
||||
|
||||
/**
|
||||
* Convert passwords from previous platform encryption to WordPress encryption.
|
||||
*
|
||||
* @since 2.1.0 bbPress (r3813)
|
||||
*/
|
||||
function bbp_user_maybe_convert_pass() {
|
||||
|
||||
// Sanitize username
|
||||
$username = ! empty( $_POST['log'] )
|
||||
? sanitize_user( $_POST['log'] )
|
||||
: '';
|
||||
|
||||
// Bail if no username
|
||||
if ( empty( $username ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Bail if no user password to convert
|
||||
$bbp_db = bbp_db();
|
||||
$query = $bbp_db->prepare( "SELECT * FROM {$bbp_db->users} INNER JOIN {$bbp_db->usermeta} ON user_id = ID WHERE meta_key = %s AND user_login = %s LIMIT 1", '_bbp_class', $username );
|
||||
$row = $bbp_db->get_row( $query );
|
||||
if ( empty( $row ) || is_wp_error( $row ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Setup the converter
|
||||
bbp_setup_converter();
|
||||
|
||||
// Try to convert the old password for this user
|
||||
$converter = bbp_new_converter( $row->meta_value );
|
||||
|
||||
// Try to call the conversion method
|
||||
if ( ( $converter instanceof BBP_Converter_Base ) && method_exists( $converter, 'callback_pass' ) ) {
|
||||
$converter->callback_pass( $username, $_POST['pwd'] );
|
||||
}
|
||||
}
|
||||
5
wp-content/plugins/bbpress/includes/users/index.php
Normal file
5
wp-content/plugins/bbpress/includes/users/index.php
Normal file
@@ -0,0 +1,5 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Do not modify the files in this folder.
|
||||
*/
|
||||
355
wp-content/plugins/bbpress/includes/users/options.php
Normal file
355
wp-content/plugins/bbpress/includes/users/options.php
Normal file
@@ -0,0 +1,355 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* bbPress User Options
|
||||
*
|
||||
* @package bbPress
|
||||
* @subpackage UserOptions
|
||||
*/
|
||||
|
||||
// Exit if accessed directly
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Get the default user options and their values
|
||||
*
|
||||
* @since 2.1.0 bbPress (r3910)
|
||||
*
|
||||
* @return array Filtered user option names and values
|
||||
*/
|
||||
function bbp_get_default_user_options() {
|
||||
|
||||
// Filter & return
|
||||
return (array) apply_filters( 'bbp_get_default_user_options', array(
|
||||
'_bbp_last_posted' => '0', // For checking flooding
|
||||
'_bbp_topic_count' => '0', // Total topics per site
|
||||
'_bbp_reply_count' => '0' // Total replies per site
|
||||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Add default user options
|
||||
*
|
||||
* This is destructive, so existing bbPress user options will be overridden.
|
||||
*
|
||||
* @since 2.1.0 bbPress (r3910)
|
||||
*/
|
||||
function bbp_add_user_options( $user_id = 0 ) {
|
||||
|
||||
// Validate user id
|
||||
$user_id = bbp_get_user_id( $user_id );
|
||||
if ( empty( $user_id ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Add default options
|
||||
foreach ( bbp_get_default_user_options() as $key => $value ) {
|
||||
update_user_option( $user_id, $key, $value );
|
||||
}
|
||||
|
||||
// Allow previously activated plugins to append their own user options.
|
||||
do_action( 'bbp_add_user_options', $user_id );
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete default user options
|
||||
*
|
||||
* Hooked to bbp_uninstall, it is only called once when bbPress is uninstalled.
|
||||
* This is destructive, so existing bbPress user options will be destroyed.
|
||||
*
|
||||
* @since 2.1.0 bbPress (r3910)
|
||||
*/
|
||||
function bbp_delete_user_options( $user_id = 0 ) {
|
||||
|
||||
// Validate user id
|
||||
$user_id = bbp_get_user_id( $user_id );
|
||||
if ( empty( $user_id ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Add default options
|
||||
foreach ( array_keys( bbp_get_default_user_options() ) as $key ) {
|
||||
delete_user_option( $user_id, $key );
|
||||
}
|
||||
|
||||
// Allow previously activated plugins to append their own options.
|
||||
do_action( 'bbp_delete_user_options', $user_id );
|
||||
}
|
||||
|
||||
/**
|
||||
* Add filters to each bbPress option and allow them to be overloaded from
|
||||
* inside the $bbp->options array.
|
||||
*
|
||||
* @since 2.1.0 bbPress (r3910)
|
||||
*/
|
||||
function bbp_setup_user_option_filters() {
|
||||
|
||||
// Add filters to each bbPress option
|
||||
foreach ( array_keys( bbp_get_default_user_options() ) as $key ) {
|
||||
add_filter( 'get_user_option_' . $key, 'bbp_filter_get_user_option', 10, 3 );
|
||||
}
|
||||
|
||||
// Allow previously activated plugins to append their own options.
|
||||
do_action( 'bbp_setup_user_option_filters' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter default options and allow them to be overloaded from inside the
|
||||
* $bbp->user_options array.
|
||||
*
|
||||
* @since 2.1.0 bbPress (r3910)
|
||||
*
|
||||
* @param bool $value Optional. Default value false
|
||||
* @return mixed false if not overloaded, mixed if set
|
||||
*/
|
||||
function bbp_filter_get_user_option( $value = false, $option = '', $user = 0 ) {
|
||||
$bbp = bbpress();
|
||||
|
||||
// Check the options global for preset value
|
||||
if ( isset( $user->ID ) && isset( $bbp->user_options[ $user->ID ] ) && ! empty( $bbp->user_options[ $user->ID ][ $option ] ) ) {
|
||||
$value = $bbp->user_options[ $user->ID ][ $option ];
|
||||
}
|
||||
|
||||
// Always return a value, even if false
|
||||
return $value;
|
||||
}
|
||||
|
||||
/** Post Counts ***************************************************************/
|
||||
|
||||
/**
|
||||
* Update the topic count for a user
|
||||
*
|
||||
* @since 2.6.0 bbPress (r5309)
|
||||
*
|
||||
* @param int $user_id
|
||||
* @param mixed $count
|
||||
* @return boolean
|
||||
*/
|
||||
function bbp_update_user_topic_count( $user_id = 0, $count = false ) {
|
||||
|
||||
// Validate user id
|
||||
$user_id = bbp_get_user_id( $user_id );
|
||||
if ( empty( $user_id ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Just in time filtering of the user's topic count
|
||||
$count = apply_filters( 'bbp_update_user_topic_count', $count, $user_id );
|
||||
|
||||
// Bail if no count was passed
|
||||
if ( false === $count ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Return the updated user option
|
||||
return update_user_option( $user_id, '_bbp_topic_count', $count );
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the reply count for a user
|
||||
*
|
||||
* @since 2.6.0 bbPress (r5309)
|
||||
*
|
||||
* @param int $user_id
|
||||
* @param mixed $count
|
||||
* @return boolean
|
||||
*/
|
||||
function bbp_update_user_reply_count( $user_id = 0, $count = false ) {
|
||||
|
||||
// Validate user id
|
||||
$user_id = bbp_get_user_id( $user_id );
|
||||
if ( empty( $user_id ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Just in time filtering of the user's reply count
|
||||
$count = apply_filters( 'bbp_update_user_reply_count', $count, $user_id );
|
||||
|
||||
// Bail if no count was passed
|
||||
if ( false === $count ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Return the updated user option
|
||||
return update_user_option( $user_id, '_bbp_reply_count', $count );
|
||||
}
|
||||
|
||||
/**
|
||||
* Output a users topic count
|
||||
*
|
||||
* @since 2.1.0 bbPress (r3632)
|
||||
*
|
||||
* @param int $user_id
|
||||
* @param boolean $integer Optional. Whether or not to format the result
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function bbp_user_topic_count( $user_id = 0, $integer = false ) {
|
||||
echo esc_html( bbp_get_user_topic_count( $user_id, $integer ) );
|
||||
}
|
||||
/**
|
||||
* Return a users reply count
|
||||
*
|
||||
* @since 2.1.0 bbPress (r3632)
|
||||
*
|
||||
* @param int $user_id
|
||||
* @param boolean $integer Optional. Whether or not to format the result
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function bbp_get_user_topic_count( $user_id = 0, $integer = false ) {
|
||||
|
||||
// Validate user id
|
||||
$user_id = bbp_get_user_id( $user_id );
|
||||
if ( empty( $user_id ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$count = get_user_option( '_bbp_topic_count', $user_id );
|
||||
$filter = ( true === $integer )
|
||||
? 'bbp_get_user_topic_count_int'
|
||||
: 'bbp_get_user_topic_count';
|
||||
|
||||
// Filter & return
|
||||
return apply_filters( $filter, $count, $user_id );
|
||||
}
|
||||
|
||||
/**
|
||||
* Output a users reply count
|
||||
*
|
||||
* @since 2.1.0 bbPress (r3632)
|
||||
*
|
||||
* @param int $user_id
|
||||
* @param boolean $integer Optional. Whether or not to format the result
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function bbp_user_reply_count( $user_id = 0, $integer = false ) {
|
||||
echo esc_html( bbp_get_user_reply_count( $user_id, $integer ) );
|
||||
}
|
||||
/**
|
||||
* Return a users reply count
|
||||
*
|
||||
* @since 2.1.0 bbPress (r3632)
|
||||
*
|
||||
* @param int $user_id
|
||||
* @param boolean $integer Optional. Whether or not to format the result
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function bbp_get_user_reply_count( $user_id = 0, $integer = false ) {
|
||||
|
||||
// Validate user id
|
||||
$user_id = bbp_get_user_id( $user_id );
|
||||
if ( empty( $user_id ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$count = get_user_option( '_bbp_reply_count', $user_id );
|
||||
$filter = ( true === $integer )
|
||||
? 'bbp_get_user_reply_count_int'
|
||||
: 'bbp_get_user_reply_count';
|
||||
|
||||
return apply_filters( $filter, $count, $user_id );
|
||||
}
|
||||
|
||||
/**
|
||||
* Output a users total post count
|
||||
*
|
||||
* @since 2.1.0 bbPress (r3632)
|
||||
*
|
||||
* @param int $user_id
|
||||
* @param boolean $integer Optional. Whether or not to format the result
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function bbp_user_post_count( $user_id = 0, $integer = false ) {
|
||||
echo esc_html( bbp_get_user_post_count( $user_id, $integer ) );
|
||||
}
|
||||
/**
|
||||
* Return a users total post count
|
||||
*
|
||||
* @since 2.1.0 bbPress (r3632)
|
||||
*
|
||||
* @param int $user_id
|
||||
* @param boolean $integer Optional. Whether or not to format the result
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function bbp_get_user_post_count( $user_id = 0, $integer = false ) {
|
||||
|
||||
// Validate user id
|
||||
$user_id = bbp_get_user_id( $user_id );
|
||||
if ( empty( $user_id ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$topics = bbp_get_user_topic_count( $user_id, true );
|
||||
$replies = bbp_get_user_reply_count( $user_id, true );
|
||||
$count = $topics + $replies;
|
||||
$filter = ( true === $integer )
|
||||
? 'bbp_get_user_post_count_int'
|
||||
: 'bbp_get_user_post_count';
|
||||
|
||||
return apply_filters( $filter, $count, $user_id );
|
||||
}
|
||||
|
||||
/** Last Posted ***************************************************************/
|
||||
|
||||
/**
|
||||
* Update a users last posted time, for use with post throttling
|
||||
*
|
||||
* @since 2.1.0 bbPress (r3910)
|
||||
*
|
||||
* @param int $user_id User ID to update
|
||||
* @param int $time Time in time() format
|
||||
* @return bool False if no user or failure, true if successful
|
||||
*/
|
||||
function bbp_update_user_last_posted( $user_id = 0, $time = 0 ) {
|
||||
|
||||
// Validate user id
|
||||
$user_id = bbp_get_user_id( $user_id );
|
||||
if ( empty( $user_id ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Set time to now if nothing is passed
|
||||
if ( empty( $time ) ) {
|
||||
$time = time();
|
||||
}
|
||||
|
||||
return update_user_option( $user_id, '_bbp_last_posted', $time );
|
||||
}
|
||||
|
||||
/**
|
||||
* Output the raw value of the last posted time.
|
||||
*
|
||||
* @since 2.1.0 bbPress (r3910)
|
||||
*
|
||||
* @param int $user_id User ID to retrieve value for
|
||||
*/
|
||||
function bbp_user_last_posted( $user_id = 0 ) {
|
||||
echo esc_html( bbp_get_user_last_posted( $user_id ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the raw value of the last posted time.
|
||||
*
|
||||
* @since 2.1.0 bbPress (r3910)
|
||||
*
|
||||
* @param int $user_id User ID to retrieve value for
|
||||
* @return mixed False if no user, time() format if exists
|
||||
*/
|
||||
function bbp_get_user_last_posted( $user_id = 0 ) {
|
||||
|
||||
// Validate user id
|
||||
$user_id = bbp_get_user_id( $user_id );
|
||||
if ( empty( $user_id ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$time = get_user_option( '_bbp_last_posted', $user_id );
|
||||
|
||||
// Filter & return
|
||||
return apply_filters( 'bbp_get_user_last_posted', $time, $user_id );
|
||||
}
|
||||
263
wp-content/plugins/bbpress/includes/users/signups.php
Normal file
263
wp-content/plugins/bbpress/includes/users/signups.php
Normal file
@@ -0,0 +1,263 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* bbPress Signups
|
||||
*
|
||||
* This file contains functions for assisting with adding forum data to user
|
||||
* accounts during signup, account creation, and invitation.
|
||||
*
|
||||
* @package bbPress
|
||||
* @subpackage Signups
|
||||
*/
|
||||
|
||||
// Exit if accessed directly
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Output the forum-role field when adding a new user
|
||||
*
|
||||
* @since 2.6.0 bbPress (r6674)
|
||||
*/
|
||||
function bbp_add_user_form_role_field() {
|
||||
|
||||
// Bail if current user cannot promote users
|
||||
if ( ! current_user_can( 'promote_users' ) ) {
|
||||
return;
|
||||
} ?>
|
||||
|
||||
<table class="form-table">
|
||||
<tr class="form-field">
|
||||
<th scope="row"><label for="bbp-forums-role"><?php esc_html_e( 'Forum Role', 'bbpress' ); ?></label></th>
|
||||
<td><?php
|
||||
|
||||
// Default user role
|
||||
$default_role = isset( $_POST['bbp-forums-role'] )
|
||||
? sanitize_key( $_POST['bbp-forums-role'] )
|
||||
: bbp_get_default_role();
|
||||
|
||||
// Get the folum roles
|
||||
$dynamic_roles = bbp_get_dynamic_roles();
|
||||
|
||||
// Only keymasters can set other keymasters
|
||||
if ( ! bbp_is_user_keymaster() ) {
|
||||
unset( $dynamic_roles[ bbp_get_keymaster_role() ] );
|
||||
} ?>
|
||||
|
||||
<select name="bbp-forums-role" id="bbp-forums-role">
|
||||
|
||||
<?php foreach ( $dynamic_roles as $role => $details ) : ?>
|
||||
|
||||
<option <?php selected( $default_role, $role ); ?> value="<?php echo esc_attr( $role ); ?>"><?php echo bbp_translate_user_role( $details['name'] ); ?></option>
|
||||
|
||||
<?php endforeach; ?>
|
||||
|
||||
</select>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<?php
|
||||
}
|
||||
|
||||
/**
|
||||
* Maybe add forum role to signup meta array
|
||||
*
|
||||
* @since 2.6.0 bbPress (r6674)
|
||||
*
|
||||
* @param array $meta
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function bbp_user_add_role_to_signup_meta( $meta = array() ) {
|
||||
|
||||
// Bail if already added
|
||||
if ( ! empty( $meta['bbp_new_role'] ) ) {
|
||||
return $meta;
|
||||
}
|
||||
|
||||
// Role to validate
|
||||
$to_validate = ! empty( $_POST['bbp-forums-role'] ) && is_string( $_POST['bbp-forums-role'] )
|
||||
? sanitize_key( $_POST['bbp-forums-role'] )
|
||||
: '';
|
||||
|
||||
// Validate the signup role
|
||||
$valid_role = bbp_validate_registration_role( $to_validate );
|
||||
|
||||
// Bail if errors
|
||||
if ( bbp_has_errors() ) {
|
||||
return $meta;
|
||||
}
|
||||
|
||||
// Add role to meta
|
||||
$meta['bbp_new_role'] = $valid_role;
|
||||
|
||||
// Return meta
|
||||
return $meta;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add forum meta data when inviting a user to a site
|
||||
*
|
||||
* @since 2.6.0 bbPress (r6674)
|
||||
*
|
||||
* @param int $user_id The invited user's ID.
|
||||
* @param array $role The role of invited user.
|
||||
* @param string $newuser_key The key of the invitation.
|
||||
*/
|
||||
function bbp_user_add_role_on_invite( $user_id = '', $role = '', $newuser_key = '' ) {
|
||||
|
||||
// Role to validate
|
||||
$to_validate = ! empty( $_POST['bbp-forums-role'] ) && is_string( $_POST['bbp-forums-role'] )
|
||||
? sanitize_key( $_POST['bbp-forums-role'] )
|
||||
: '';
|
||||
|
||||
// Validate the signup role
|
||||
$valid_role = bbp_validate_registration_role( $to_validate );
|
||||
|
||||
// Bail if errors
|
||||
if ( bbp_has_errors() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Option key
|
||||
$option_key = 'new_user_' . $newuser_key;
|
||||
|
||||
// Get the user option
|
||||
$user_option = get_option( $option_key, array() );
|
||||
|
||||
// Add the new role
|
||||
$user_option['bbp_new_role'] = $valid_role;
|
||||
|
||||
// Update the invitation
|
||||
update_option( $option_key, $user_option );
|
||||
}
|
||||
|
||||
/**
|
||||
* Single-site handler for adding a new user
|
||||
*
|
||||
* @since 2.6.0 bbPress (r6674)
|
||||
*
|
||||
* @param int $user_id
|
||||
*/
|
||||
function bbp_user_add_role_on_register( $user_id = '' ) {
|
||||
|
||||
// Role to validate
|
||||
$to_validate = ! empty( $_POST['bbp-forums-role'] ) && is_string( $_POST['bbp-forums-role'] )
|
||||
? sanitize_key( $_POST['bbp-forums-role'] )
|
||||
: '';
|
||||
|
||||
// Validate the signup role
|
||||
$valid_role = bbp_validate_registration_role( $to_validate );
|
||||
|
||||
// Bail if errors
|
||||
if ( bbp_has_errors() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Set the user role
|
||||
bbp_set_user_role( $user_id, $valid_role );
|
||||
}
|
||||
|
||||
/**
|
||||
* Multi-site handler for adding a new user
|
||||
*
|
||||
* @since 2.6.0 bbPress (r6674)
|
||||
*
|
||||
* @param int $user_id User ID.
|
||||
*/
|
||||
function bbp_user_add_role_on_activate( $user_id = 0, $password = '', $meta = array() ) {
|
||||
|
||||
// Role to validate
|
||||
$to_validate = ! empty( $meta['bbp_new_role'] ) && is_string( $meta['bbp_new_role'] )
|
||||
? sanitize_key( $meta['bbp_new_role'] )
|
||||
: '';
|
||||
|
||||
// Validate the signup role
|
||||
$valid_role = bbp_validate_activation_role( $to_validate );
|
||||
|
||||
// Bail if errors
|
||||
if ( bbp_has_errors() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Set the user role
|
||||
bbp_set_user_role( $user_id, $valid_role );
|
||||
}
|
||||
|
||||
/** Validators ****************************************************************/
|
||||
|
||||
/**
|
||||
* Validate the Forum role during signup
|
||||
*
|
||||
* This helper function performs a number of generic checks, and encapsulates
|
||||
* the logic used to validate if a Forum Role is valid, typically during new
|
||||
* user registration, but also when adding an existing user to a site in
|
||||
* Multisite installations.
|
||||
*
|
||||
* @since 2.6.5
|
||||
*
|
||||
* @param string $to_validate A role ID to validate
|
||||
* @return string A valid role ID, or empty string on error
|
||||
*/
|
||||
function bbp_validate_signup_role( $to_validate = '' ) {
|
||||
|
||||
// Default return value
|
||||
$retval = '';
|
||||
|
||||
// Add error if role is empty
|
||||
if ( empty( $to_validate ) ) {
|
||||
bbp_add_error( 'bbp_signup_role_empty', __( '<strong>Error</strong>: Empty role.', 'bbpress' ) );
|
||||
}
|
||||
|
||||
// Add error if posted role is not a valid role
|
||||
if ( ! bbp_is_valid_role( $to_validate ) ) {
|
||||
bbp_add_error( 'bbp_signup_role_invalid', __( '<strong>Error</strong>: Invalid role.', 'bbpress' ) );
|
||||
}
|
||||
|
||||
// If no errors, set return value to the role to validate
|
||||
if ( ! bbp_has_errors() ) {
|
||||
$retval = $to_validate;
|
||||
}
|
||||
|
||||
// Filter & return
|
||||
return (string) apply_filters( 'bbp_validate_signup_role', $retval, $to_validate );
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate the Forum role during the registration process
|
||||
*
|
||||
* @since 2.6.5
|
||||
*
|
||||
* @param string $to_validate A well-formed (string) role ID to validate
|
||||
* @return string A valid role ID, or empty string on error
|
||||
*/
|
||||
function bbp_validate_registration_role( $to_validate = '' ) {
|
||||
|
||||
// Default return value
|
||||
$retval = bbp_get_default_role();
|
||||
|
||||
// Conditionally handle posted values for capable users
|
||||
if ( is_admin() && current_user_can( 'create_users' ) ) {
|
||||
$retval = $to_validate;
|
||||
}
|
||||
|
||||
// Validate & return
|
||||
return bbp_validate_signup_role( $retval );
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate the Forum role during activation
|
||||
*
|
||||
* This function exists simply for parity with registrations, and to maintain an
|
||||
* intentional layer of abstraction from the more generic function it uses.
|
||||
*
|
||||
* @since 2.6.5
|
||||
*
|
||||
* @param string $to_validate A well-formed (string) role ID to validate
|
||||
* @return string A valid role ID, or empty string on error
|
||||
*/
|
||||
function bbp_validate_activation_role( $to_validate = '' ) {
|
||||
|
||||
// Validate & return
|
||||
return bbp_validate_signup_role( $to_validate );
|
||||
}
|
||||
2437
wp-content/plugins/bbpress/includes/users/template.php
Normal file
2437
wp-content/plugins/bbpress/includes/users/template.php
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user