diff --git includes/core/actions.php includes/core/actions.php
index b6046e4..b79814e 100644
|
|
|
add_action( 'make_ham_user', 'bbp_make_ham_user' ); |
| 246 | 246 | add_action( 'make_spam_user', 'bbp_make_spam_user' ); |
| 247 | 247 | |
| 248 | 248 | // User role |
| 249 | | add_action( 'bbp_profile_update', 'bbp_profile_update_role' ); |
| | 249 | add_action( 'bbp_profile_update', 'bbp_profile_update_role', 10, 2 ); |
| 250 | 250 | |
| 251 | 251 | // Hook WordPress admin actions to bbPress profiles on save |
| 252 | 252 | add_action( 'bbp_user_edit_after', 'bbp_user_edit_after' ); |
| … |
… |
add_action( 'bbp_get_request', 'bbp_search_results_redirect', 10 ); |
| 303 | 303 | // Maybe convert the users password |
| 304 | 304 | add_action( 'bbp_login_form_login', 'bbp_user_maybe_convert_pass' ); |
| 305 | 305 | |
| 306 | | add_action( 'bbp_activation', 'bbp_add_activation_redirect' ); |
| 307 | | No newline at end of file |
| | 306 | add_action( 'bbp_activation', 'bbp_add_activation_redirect' ); |
diff --git includes/users/capabilities.php includes/users/capabilities.php
index 77c5e41..2cb116a 100644
|
|
|
function bbp_map_primary_meta_caps( $caps = array(), $cap = '', $user_id = 0, $a |
| 50 | 50 | * @since bbPress (r3860) |
| 51 | 51 | * |
| 52 | 52 | * @param int $user_id |
| | 53 | * @param string $new_role |
| | 54 | * @param string $old_role |
| 53 | 55 | * @uses bbp_get_user_id() To get the user id |
| 54 | 56 | * @uses get_userdata() To get the user data |
| 55 | 57 | * @uses apply_filters() Calls 'bbp_set_user_role' with the role and user id |
| 56 | 58 | * @return string |
| 57 | 59 | */ |
| 58 | | function bbp_set_user_role( $user_id = 0, $new_role = '' ) { |
| | 60 | function bbp_set_user_role( $user_id = 0, $new_role = '', $old_role = '' ) { |
| | 61 | |
| | 62 | $forum_role = bbp_get_user_role( $user_id ); |
| | 63 | $old_role = $forum_role ?: $old_role; |
| 59 | 64 | |
| 60 | 65 | // Validate user id |
| 61 | 66 | $user_id = bbp_get_user_id( $user_id, false, false ); |
| … |
… |
function bbp_set_user_role( $user_id = 0, $new_role = '' ) { |
| 64 | 69 | // User exists |
| 65 | 70 | if ( !empty( $user ) ) { |
| 66 | 71 | |
| 67 | | // Get users forum role |
| 68 | | $role = bbp_get_user_role( $user_id ); |
| | 72 | $blocked_role = bbp_get_blocked_role(); |
| | 73 | |
| | 74 | // Prevent blocked users from logging in, which also invalidates their sessions |
| | 75 | if ( $blocked_role === $new_role ) { |
| | 76 | bbp_break_password( $user_id, $user->data->user_pass ); |
| | 77 | // Allow a previously blocked user to login |
| | 78 | } elseif ( $blocked_role === $old_role ) { |
| | 79 | bbp_fix_password( $user_id, $user->data->user_pass ); |
| | 80 | } |
| | 81 | |
| | 82 | // When a user is edited, Wordpress removes the custom role. If no change was |
| | 83 | // made to the role, we'll re-add it |
| | 84 | if ( $new_role === $old_role ) { |
| 69 | 85 | |
| 70 | | // User already has this role so no new role is set |
| 71 | | if ( $new_role === $role ) { |
| 72 | | $new_role = false; |
| | 86 | $user->add_role($old_role); |
| 73 | 87 | |
| 74 | | // Users role is different than the new role |
| | 88 | // User's role is different than the new role |
| 75 | 89 | } else { |
| 76 | 90 | |
| 77 | | // Remove the old role |
| 78 | | if ( ! empty( $role ) ) { |
| 79 | | $user->remove_role( $role ); |
| | 91 | // Remove the old role if there is one |
| | 92 | if ( !empty($old_role) ) { |
| | 93 | $user->remove_role( $old_role ); |
| 80 | 94 | } |
| 81 | 95 | |
| 82 | 96 | // Add the new role |
| … |
… |
function bbp_set_user_role( $user_id = 0, $new_role = '' ) { |
| 97 | 111 | return apply_filters( 'bbp_set_user_role', $new_role, $user_id, $user ); |
| 98 | 112 | } |
| 99 | 113 | |
| | 114 | function bbp_password_broken( $user_pass ) { |
| | 115 | return '_' === $user_pass[0]; |
| | 116 | } |
| | 117 | |
| | 118 | function bbp_break_password( $user_id, $user_pass ) { |
| | 119 | global $wpdb; |
| | 120 | |
| | 121 | if ( !bbp_password_broken( $user_pass ) ) { |
| | 122 | |
| | 123 | $wpdb->update( |
| | 124 | $wpdb->users, |
| | 125 | // Prefixing an underscore makes the hash invalid |
| | 126 | array( 'user_pass' => "_{$user_pass}" ), |
| | 127 | array( 'ID' => $user_id ) |
| | 128 | ); |
| | 129 | |
| | 130 | clean_user_cache( $user_id ); |
| | 131 | |
| | 132 | } |
| | 133 | } |
| | 134 | |
| | 135 | function bbp_fix_password( $user_id, $user_pass ) { |
| | 136 | global $wpdb; |
| | 137 | |
| | 138 | if ( bbp_password_broken( $user_pass ) ) { |
| | 139 | |
| | 140 | $wpdb->update( |
| | 141 | $wpdb->users, |
| | 142 | array( 'user_pass' => substr( $user_pass, 1 ) ), |
| | 143 | array( 'ID' => $user_id ) |
| | 144 | ); |
| | 145 | |
| | 146 | clean_user_cache( $user_id ); |
| | 147 | |
| | 148 | } |
| | 149 | } |
| | 150 | |
| 100 | 151 | /** |
| 101 | 152 | * Return a user's forums role |
| 102 | 153 | * |
| 103 | 154 | * @since bbPress (r3860) |
| 104 | 155 | * |
| 105 | 156 | * @param int $user_id |
| | 157 | * @param WP_User|null $user |
| 106 | 158 | * @uses bbp_get_user_id() To get the user id |
| 107 | 159 | * @uses get_userdata() To get the user data |
| 108 | 160 | * @uses apply_filters() Calls 'bbp_get_user_role' with the role and user id |
| 109 | 161 | * @return string |
| 110 | 162 | */ |
| 111 | | function bbp_get_user_role( $user_id = 0 ) { |
| | 163 | function bbp_get_user_role( $user_id = 0, $user = null ) { |
| 112 | 164 | |
| 113 | 165 | // Validate user id |
| 114 | 166 | $user_id = bbp_get_user_id( $user_id ); |
| 115 | | $user = get_userdata( $user_id ); |
| | 167 | $user = $user === null ? get_userdata( $user_id ) : $user; |
| 116 | 168 | $role = false; |
| 117 | 169 | |
| 118 | 170 | // User has roles so look for a bbPress one |
| … |
… |
function bbp_get_user_blog_role( $user_id = 0 ) { |
| 183 | 235 | * @since bbPress (r4235) |
| 184 | 236 | * |
| 185 | 237 | * @param int $user_id |
| | 238 | * @param WP_User|null $old_user |
| 186 | 239 | * @uses bbp_reset_user_caps() to reset caps |
| 187 | 240 | * @usse bbp_save_user_caps() to save caps |
| 188 | 241 | */ |
| 189 | | function bbp_profile_update_role( $user_id = 0 ) { |
| | 242 | function bbp_profile_update_role( $user_id = 0, $old_user = null ) { |
| 190 | 243 | |
| 191 | 244 | // Bail if no user ID was passed |
| 192 | 245 | if ( empty( $user_id ) ) |
| 193 | 246 | return; |
| 194 | 247 | |
| | 248 | // Bail if no old user data was passed |
| | 249 | if ( null === $old_user ) |
| | 250 | return; |
| | 251 | |
| 195 | 252 | // Bail if no role |
| 196 | 253 | if ( ! isset( $_POST['bbp-forums-role'] ) ) |
| 197 | 254 | return; |
| 198 | 255 | |
| 199 | | // Fromus role we want the user to have |
| | 256 | // Forums role we want the user to have |
| 200 | 257 | $new_role = sanitize_text_field( $_POST['bbp-forums-role'] ); |
| 201 | | $forums_role = bbp_get_user_role( $user_id ); |
| 202 | | |
| 203 | | // Bail if no role change |
| 204 | | if ( $new_role === $forums_role ) |
| 205 | | return; |
| | 258 | $forums_role = bbp_get_user_role( $user_id, $old_user ); |
| 206 | 259 | |
| 207 | 260 | // Bail if trying to set their own role |
| 208 | 261 | if ( bbp_is_user_home_edit() ) |
| … |
… |
function bbp_profile_update_role( $user_id = 0 ) { |
| 213 | 266 | return; |
| 214 | 267 | |
| 215 | 268 | // Set the new forums role |
| 216 | | bbp_set_user_role( $user_id, $new_role ); |
| | 269 | bbp_set_user_role( $user_id, $new_role, $forums_role ); |
| 217 | 270 | } |
| 218 | 271 | |
| 219 | 272 | /** |