Changeset 7086
- Timestamp:
- 05/28/2020 03:38:54 PM (6 years ago)
- Location:
- trunk/src/includes
- Files:
-
- 3 edited
-
core/actions.php (modified) (1 diff)
-
users/capabilities.php (modified) (4 diffs)
-
users/signups.php (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/includes/core/actions.php
r7072 r7086 408 408 add_action( 'clean_post_cache', 'bbp_clean_post_cache', 10, 2 ); 409 409 410 // User Creation 410 // User Registration 411 add_action( 'added_existing_user', 'bbp_user_add_role_on_register', 10, 1 ); 412 add_action( 'bbp_user_register', 'bbp_user_add_role_on_register', 10, 1 ); 413 414 // Invite a New User 411 415 add_action( 'invite_user', 'bbp_user_add_role_on_invite', 10, 3 ); 416 417 // Multisite Activation (does not work in wp-activate.php) 412 418 add_action( 'wpmu_activate_user', 'bbp_user_add_role_on_activate', 10, 3 ); 413 add_action( 'bbp_user_register', 'bbp_user_add_role_on_register', 10, 1 );414 add_action( 'added_existing_user', 'bbp_user_add_role_on_register', 10, 1 );415 add_action( 'register_new_user', 'bbp_user_add_role_on_register', 10, 1 );416 419 417 420 /** -
trunk/src/includes/users/capabilities.php
r7060 r7086 149 149 * @param int $user_id 150 150 * 151 * @return string151 * @return mixed False if no change. String of new role if changed. 152 152 */ 153 153 function bbp_set_user_role( $user_id = 0, $new_role = '' ) { … … 160 160 if ( ! empty( $user ) ) { 161 161 162 // Get user sforum role162 // Get user forum role 163 163 $role = bbp_get_user_role( $user_id ); 164 164 … … 167 167 $new_role = false; 168 168 169 // User s role is different than the newrole170 } else {169 // User role is different than the new (valid) role 170 } elseif ( bbp_is_valid_role( $new_role ) ) { 171 171 172 172 // Remove the old role … … 305 305 // Set the new forums role 306 306 bbp_set_user_role( $user_id, $new_role ); 307 } 308 309 /** 310 * Check if a role ID is valid 311 * 312 * This helper function accepts a role ID as a string, and compares it against 313 * the array of registered dynamic roles. 314 * 315 * Use this function anytime you are manually attempting to set a user role 316 * without using the bbp_set_user_role() function, or if you need to halt 317 * additional processing during role validation. 318 * 319 * @since 2.6.5 320 * 321 * @param string $role A well-formed (string) role ID to validate 322 * 323 * @return bool True if role is valid. False if role is not valid. 324 */ 325 function bbp_is_valid_role( $role = '' ) { 326 327 // Default return value 328 $retval = false; 329 330 // Skip if no role to check 331 if ( ! empty( $role ) && is_string( $role ) ) { 332 333 // Get the dynamic role IDs 334 $roles = array_keys( bbp_get_dynamic_roles() ); 335 336 // Skip if no known role IDs 337 if ( ! empty( $roles ) ) { 338 339 // Is role in dynamic roles array? 340 $retval = in_array( $role, $roles, true ); 341 } 342 } 343 344 // Filter & return 345 return (bool) apply_filters( 'bbp_is_valid_role', $retval, $role ); 307 346 } 308 347 -
trunk/src/includes/users/signups.php
r6675 r7086 20 20 */ 21 21 function bbp_add_user_form_role_field() { 22 ?> 22 23 // Bail if current user cannot promote users 24 if ( ! current_user_can( 'promote_users' ) ) { 25 return; 26 } ?> 23 27 24 28 <table class="form-table"> … … 67 71 function bbp_user_add_role_to_signup_meta( $meta = array() ) { 68 72 69 // Posted role 70 $forum_role = isset( $_POST['bbp-forums-role'] ) 73 // Bail if already added 74 if ( ! empty( $meta['bbp_new_role'] ) ) { 75 return $meta; 76 } 77 78 // Role to validate 79 $to_validate = ! empty( $_POST['bbp-forums-role'] ) && is_string( $_POST['bbp-forums-role'] ) 71 80 ? sanitize_key( $_POST['bbp-forums-role'] ) 72 : bbp_get_default_role();73 74 // Role keys75 $ roles = array_keys( bbp_get_dynamic_roles());76 77 // Bail if posted role is not in dynamic roles78 if ( empty( $forum_role ) || ! in_array( $forum_role, $roles, true) ) {81 : ''; 82 83 // Validate the signup role 84 $valid_role = bbp_validate_registration_role( $to_validate ); 85 86 // Bail if errors 87 if ( bbp_has_errors() ) { 79 88 return $meta; 80 89 } 81 90 82 91 // Add role to meta 83 $meta['bbp_new_role'] = $ forum_role;92 $meta['bbp_new_role'] = $valid_role; 84 93 85 94 // Return meta … … 95 104 * @param array $role The role of invited user. 96 105 * @param string $newuser_key The key of the invitation. 106 * 107 * @return void 97 108 */ 98 109 function bbp_user_add_role_on_invite( $user_id = '', $role = '', $newuser_key = '' ) { 99 110 100 // Posted role101 $ forum_role = isset( $_POST['bbp-forums-role'] )111 // Role to validate 112 $to_validate = ! empty( $_POST['bbp-forums-role'] ) && is_string( $_POST['bbp-forums-role'] ) 102 113 ? sanitize_key( $_POST['bbp-forums-role'] ) 103 : bbp_get_default_role(); 104 105 // Role keys 106 $roles = array_keys( bbp_get_dynamic_roles() ); 107 108 // Bail if posted role is not in dynamic roles 109 if ( empty( $forum_role ) || ! in_array( $forum_role, $roles, true ) ) { 114 : ''; 115 116 // Validate the signup role 117 $valid_role = bbp_validate_registration_role( $to_validate ); 118 119 // Bail if errors 120 if ( bbp_has_errors() ) { 121 return; 122 } 123 124 // Bail if malformed user key 125 if ( empty( $newuser_key ) || ! is_string( $newuser_key ) ) { 110 126 return; 111 127 } … … 118 134 119 135 // Add the new role 120 $user_option['bbp_new_role'] = $ forum_role;136 $user_option['bbp_new_role'] = $valid_role; 121 137 122 138 // Update the invitation … … 130 146 * 131 147 * @param int $user_id 148 * 149 * @return void 132 150 */ 133 151 function bbp_user_add_role_on_register( $user_id = '' ) { 134 152 135 // Posted role136 $ forum_role = isset( $_POST['bbp-forums-role'] )153 // Role to validate 154 $to_validate = ! empty( $_POST['bbp-forums-role'] ) && is_string( $_POST['bbp-forums-role'] ) 137 155 ? sanitize_key( $_POST['bbp-forums-role'] ) 138 : bbp_get_default_role();139 140 // Role keys141 $ roles = array_keys( bbp_get_dynamic_roles());142 143 // Bail if posted role is not in dynamic roles144 if ( empty( $forum_role ) || ! in_array( $forum_role, $roles, true) ) {156 : ''; 157 158 // Validate the signup role 159 $valid_role = bbp_validate_registration_role( $to_validate ); 160 161 // Bail if errors 162 if ( bbp_has_errors() ) { 145 163 return; 146 164 } 147 165 148 166 // Set the user role 149 bbp_set_user_role( $user_id, $ forum_role );167 bbp_set_user_role( $user_id, $valid_role ); 150 168 } 151 169 … … 155 173 * @since 2.6.0 bbPress (r6674) 156 174 * 157 * @param int $user_id User ID. 175 * @param int $user_id User ID 176 * @param string $password User password 177 * @param array $meta Array of metadata 178 * 179 * @return void 158 180 */ 159 181 function bbp_user_add_role_on_activate( $user_id = 0, $password = '', $meta = array() ) { 160 182 161 // Posted role162 $ forum_role = isset( $meta['bbp_new_role'] )183 // Role to validate 184 $to_validate = ! empty( $meta['bbp_new_role'] ) && is_string( $meta['bbp_new_role'] ) 163 185 ? sanitize_key( $meta['bbp_new_role'] ) 164 : bbp_get_default_role();165 166 // Sanitizerole167 $ roles = array_keys( bbp_get_dynamic_roles());168 169 // Bail if posted role is not in dynamic roles170 if ( empty( $forum_role ) || ! in_array( $forum_role, $roles, true) ) {186 : ''; 187 188 // Validate the signup role 189 $valid_role = bbp_validate_activation_role( $to_validate ); 190 191 // Bail if errors 192 if ( bbp_has_errors() ) { 171 193 return; 172 194 } 173 195 174 196 // Set the user role 175 bbp_set_user_role( $user_id, $forum_role ); 176 } 197 bbp_set_user_role( $user_id, $valid_role ); 198 } 199 200 /** Validators ****************************************************************/ 201 202 /** 203 * Validate the Forum role during signup 204 * 205 * This helper function performs a number of generic checks, and encapsulates 206 * the logic used to validate if a Forum Role is valid, typically during new 207 * user registration, but also when adding an existing user to a site in 208 * Multisite installations. 209 * 210 * @since 2.6.5 211 * 212 * @param string $to_validate A role ID to validate 213 * 214 * @return string A valid role ID, or empty string on error 215 */ 216 function bbp_validate_signup_role( $to_validate = '' ) { 217 218 // Default return value 219 $retval = ''; 220 221 // Add error if role is empty 222 if ( empty( $to_validate ) ) { 223 bbp_add_error( 'bbp_signup_role_empty', __( '<strong>ERROR</strong>: Empty role.', 'bbpress' ) ); 224 } 225 226 // Add error if posted role is not a valid role 227 if ( ! bbp_is_valid_role( $to_validate ) ) { 228 bbp_add_error( 'bbp_signup_role_invalid', __( '<strong>ERROR</strong>: Invalid role.', 'bbpress' ) ); 229 } 230 231 // If no errors, set return value to the role to validate 232 if ( ! bbp_has_errors() ) { 233 $retval = $to_validate; 234 } 235 236 // Filter & return 237 return (string) apply_filters( 'bbp_validate_signup_role', $retval, $to_validate ); 238 } 239 240 /** 241 * Validate the Forum role during the registration process 242 * 243 * @since 2.6.5 244 * 245 * @param string $to_validate A role ID to validate 246 * 247 * @return string A valid role ID, or empty string on error 248 */ 249 function bbp_validate_registration_role( $to_validate = '' ) { 250 251 // Default return value 252 $retval = bbp_get_default_role(); 253 254 /** 255 * Conditionally accept admin-area posted values for capable users. This is 256 * to allow for Site/Network Admins to assign a default role when inviting 257 * or creating a new User account. 258 */ 259 if ( is_admin() && current_user_can( 'create_users' ) ) { 260 $retval = $to_validate; 261 } 262 263 // Validate & return 264 return bbp_validate_signup_role( $retval ); 265 } 266 267 /** 268 * Validate the Forum role during multisite activation 269 * 270 * This function exists simply for parity with registrations, and to maintain an 271 * intentional layer of abstraction from the more generic function it uses. 272 * 273 * Note: this will not fire inside of wp-activate.php unless it is hooked in 274 * during sunrise.php, and is considered an advanced use-case. 275 * 276 * @since 2.6.5 277 * 278 * @param string $to_validate A role ID to validate 279 * 280 * @return string A valid role ID, or empty string on error 281 */ 282 function bbp_validate_activation_role( $to_validate = '' ) { 283 284 // Validate & return 285 return bbp_validate_signup_role( $to_validate ); 286 }
Note: See TracChangeset
for help on using the changeset viewer.