Changeset 7087
- Timestamp:
- 05/28/2020 03:43:58 PM (6 years ago)
- Location:
- branches/2.6/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
-
branches/2.6/src/includes/core/actions.php
r7073 r7087 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 /** -
branches/2.6/src/includes/users/capabilities.php
r7059 r7087 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 string is valid 311 * 312 * @since 2.6.5 313 * 314 * @param string $role 315 * 316 * @return bool True if role is valid. False if role is not valid. 317 */ 318 function bbp_is_valid_role( $role = '' ) { 319 320 // Default return value 321 $retval = false; 322 323 // Skip if no role to check 324 if ( ! empty( $role ) && is_string( $role ) ) { 325 326 // Get the dynamic role IDs 327 $roles = array_keys( bbp_get_dynamic_roles() ); 328 329 // Skip if no known role IDs 330 if ( ! empty( $roles ) ) { 331 332 // Is role in dynamic roles array? 333 $retval = in_array( $role, $roles, true ); 334 } 335 } 336 337 // Filter & return 338 return (bool) apply_filters( 'bbp_is_valid_role', $retval, $role ); 307 339 } 308 340 -
branches/2.6/src/includes/users/signups.php
r6675 r7087 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 … … 98 107 function bbp_user_add_role_on_invite( $user_id = '', $role = '', $newuser_key = '' ) { 99 108 100 // Posted role101 $ forum_role = isset( $_POST['bbp-forums-role'] )109 // Role to validate 110 $to_validate = ! empty( $_POST['bbp-forums-role'] ) && is_string( $_POST['bbp-forums-role'] ) 102 111 ? sanitize_key( $_POST['bbp-forums-role'] ) 103 : bbp_get_default_role();104 105 // Role keys106 $ roles = array_keys( bbp_get_dynamic_roles());107 108 // Bail if posted role is not in dynamic roles109 if ( empty( $forum_role ) || ! in_array( $forum_role, $roles, true) ) {112 : ''; 113 114 // Validate the signup role 115 $valid_role = bbp_validate_registration_role( $to_validate ); 116 117 // Bail if errors 118 if ( bbp_has_errors() ) { 110 119 return; 111 120 } … … 118 127 119 128 // Add the new role 120 $user_option['bbp_new_role'] = $ forum_role;129 $user_option['bbp_new_role'] = $valid_role; 121 130 122 131 // Update the invitation … … 133 142 function bbp_user_add_role_on_register( $user_id = '' ) { 134 143 135 // Posted role136 $ forum_role = isset( $_POST['bbp-forums-role'] )144 // Role to validate 145 $to_validate = ! empty( $_POST['bbp-forums-role'] ) && is_string( $_POST['bbp-forums-role'] ) 137 146 ? 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) ) {147 : ''; 148 149 // Validate the signup role 150 $valid_role = bbp_validate_registration_role( $to_validate ); 151 152 // Bail if errors 153 if ( bbp_has_errors() ) { 145 154 return; 146 155 } 147 156 148 157 // Set the user role 149 bbp_set_user_role( $user_id, $ forum_role );158 bbp_set_user_role( $user_id, $valid_role ); 150 159 } 151 160 … … 159 168 function bbp_user_add_role_on_activate( $user_id = 0, $password = '', $meta = array() ) { 160 169 161 // Posted role162 $ forum_role = isset( $meta['bbp_new_role'] )170 // Role to validate 171 $to_validate = ! empty( $meta['bbp_new_role'] ) && is_string( $meta['bbp_new_role'] ) 163 172 ? 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) ) {173 : ''; 174 175 // Validate the signup role 176 $valid_role = bbp_validate_activation_role( $to_validate ); 177 178 // Bail if errors 179 if ( bbp_has_errors() ) { 171 180 return; 172 181 } 173 182 174 183 // Set the user role 175 bbp_set_user_role( $user_id, $forum_role ); 176 } 184 bbp_set_user_role( $user_id, $valid_role ); 185 } 186 187 /** Validators ****************************************************************/ 188 189 /** 190 * Validate the Forum role during signup 191 * 192 * This helper function performs a number of generic checks, and encapsulates 193 * the logic used to validate if a Forum Role is valid, typically during new 194 * user registration, but also when adding an existing user to a site in 195 * Multisite installations. 196 * 197 * @since 2.6.5 198 * 199 * @param string $to_validate A role ID to validate 200 * @return string A valid role ID, or empty string on error 201 */ 202 function bbp_validate_signup_role( $to_validate = '' ) { 203 204 // Default return value 205 $retval = ''; 206 207 // Add error if role is empty 208 if ( empty( $to_validate ) ) { 209 bbp_add_error( 'bbp_signup_role_empty', __( '<strong>ERROR</strong>: Empty role.', 'bbpress' ) ); 210 } 211 212 // Add error if posted role is not a valid role 213 if ( ! bbp_is_valid_role( $to_validate ) ) { 214 bbp_add_error( 'bbp_signup_role_invalid', __( '<strong>ERROR</strong>: Invalid role.', 'bbpress' ) ); 215 } 216 217 // If no errors, set return value to the role to validate 218 if ( ! bbp_has_errors() ) { 219 $retval = $to_validate; 220 } 221 222 // Filter & return 223 return (string) apply_filters( 'bbp_validate_signup_role', $retval, $to_validate ); 224 } 225 226 /** 227 * Validate the Forum role during the registration process 228 * 229 * @since 2.6.5 230 * 231 * @param string $to_validate A well-formed (string) role ID to validate 232 * @return string A valid role ID, or empty string on error 233 */ 234 function bbp_validate_registration_role( $to_validate = '' ) { 235 236 // Default return value 237 $retval = bbp_get_default_role(); 238 239 // Conditionally handle posted values for capable users 240 if ( is_admin() && current_user_can( 'create_users' ) ) { 241 $retval = $to_validate; 242 } 243 244 // Validate & return 245 return bbp_validate_signup_role( $retval ); 246 } 247 248 /** 249 * Validate the Forum role during activation 250 * 251 * This function exists simply for parity with registrations, and to maintain an 252 * intentional layer of abstraction from the more generic function it uses. 253 * 254 * @since 2.6.5 255 * 256 * @param string $to_validate A well-formed (string) role ID to validate 257 * @return string A valid role ID, or empty string on error 258 */ 259 function bbp_validate_activation_role( $to_validate = '' ) { 260 261 // Validate & return 262 return bbp_validate_signup_role( $to_validate ); 263 }
Note: See TracChangeset
for help on using the changeset viewer.