| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* bbPress Signups |
| 5 |
* |
| 6 |
* This file contains functions for assisting with adding forum data to user |
| 7 |
* accounts during signup, account creation, and invitation. |
| 8 |
* |
| 9 |
* @package bbPress |
| 10 |
* @subpackage Signups |
| 11 |
*/ |
| 12 |
|
| 13 |
// Exit if accessed directly |
| 14 |
defined( 'ABSPATH' ) || exit; |
| 15 |
|
| 16 |
/** |
| 17 |
* Output the forum-role field when adding a new user |
| 18 |
* |
| 19 |
* @since 2.6.0 bbPress (r6674) |
| 20 |
*/ |
| 21 |
function bbp_add_user_form_role_field() { |
| 22 |
|
| 23 |
// Bail if current user cannot promote users |
| 24 |
if ( ! current_user_can( 'promote_users' ) ) { |
| 25 |
return; |
| 26 |
} ?> |
| 27 |
|
| 28 |
<table class="form-table"> |
| 29 |
<tr class="form-field"> |
| 30 |
<th scope="row"><label for="bbp-forums-role"><?php esc_html_e( 'Forum Role', 'bbpress' ); ?></label></th> |
| 31 |
<td><?php |
| 32 |
|
| 33 |
// Default user role |
| 34 |
$default_role = isset( $_POST['bbp-forums-role'] ) |
| 35 |
? sanitize_key( $_POST['bbp-forums-role'] ) |
| 36 |
: bbp_get_default_role(); |
| 37 |
|
| 38 |
// Get the folum roles |
| 39 |
$dynamic_roles = bbp_get_dynamic_roles(); |
| 40 |
|
| 41 |
// Only keymasters can set other keymasters |
| 42 |
if ( ! bbp_is_user_keymaster() ) { |
| 43 |
unset( $dynamic_roles[ bbp_get_keymaster_role() ] ); |
| 44 |
} ?> |
| 45 |
|
| 46 |
<select name="bbp-forums-role" id="bbp-forums-role"> |
| 47 |
|
| 48 |
<?php foreach ( $dynamic_roles as $role => $details ) : ?> |
| 49 |
|
| 50 |
<option <?php selected( $default_role, $role ); ?> value="<?php echo esc_attr( $role ); ?>"><?php echo bbp_translate_user_role( $details['name'] ); ?></option> |
| 51 |
|
| 52 |
<?php endforeach; ?> |
| 53 |
|
| 54 |
</select> |
| 55 |
</td> |
| 56 |
</tr> |
| 57 |
</table> |
| 58 |
|
| 59 |
<?php |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Maybe add forum role to signup meta array |
| 64 |
* |
| 65 |
* @since 2.6.0 bbPress (r6674) |
| 66 |
* |
| 67 |
* @param array $meta |
| 68 |
* |
| 69 |
* @return array |
| 70 |
*/ |
| 71 |
function bbp_user_add_role_to_signup_meta( $meta = array() ) { |
| 72 |
|
| 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'] ) |
| 80 |
? sanitize_key( $_POST['bbp-forums-role'] ) |
| 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() ) { |
| 88 |
return $meta; |
| 89 |
} |
| 90 |
|
| 91 |
// Add role to meta |
| 92 |
$meta['bbp_new_role'] = $valid_role; |
| 93 |
|
| 94 |
// Return meta |
| 95 |
return $meta; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Add forum meta data when inviting a user to a site |
| 100 |
* |
| 101 |
* @since 2.6.0 bbPress (r6674) |
| 102 |
* |
| 103 |
* @param int $user_id The invited user's ID. |
| 104 |
* @param array $role The role of invited user. |
| 105 |
* @param string $newuser_key The key of the invitation. |
| 106 |
*/ |
| 107 |
function bbp_user_add_role_on_invite( $user_id = '', $role = '', $newuser_key = '' ) { |
| 108 |
|
| 109 |
// Role to validate |
| 110 |
$to_validate = ! empty( $_POST['bbp-forums-role'] ) && is_string( $_POST['bbp-forums-role'] ) |
| 111 |
? sanitize_key( $_POST['bbp-forums-role'] ) |
| 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() ) { |
| 119 |
return; |
| 120 |
} |
| 121 |
|
| 122 |
// Option key |
| 123 |
$option_key = 'new_user_' . $newuser_key; |
| 124 |
|
| 125 |
// Get the user option |
| 126 |
$user_option = get_option( $option_key, array() ); |
| 127 |
|
| 128 |
// Add the new role |
| 129 |
$user_option['bbp_new_role'] = $valid_role; |
| 130 |
|
| 131 |
// Update the invitation |
| 132 |
update_option( $option_key, $user_option ); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Single-site handler for adding a new user |
| 137 |
* |
| 138 |
* @since 2.6.0 bbPress (r6674) |
| 139 |
* |
| 140 |
* @param int $user_id |
| 141 |
*/ |
| 142 |
function bbp_user_add_role_on_register( $user_id = '' ) { |
| 143 |
|
| 144 |
// Role to validate |
| 145 |
$to_validate = ! empty( $_POST['bbp-forums-role'] ) && is_string( $_POST['bbp-forums-role'] ) |
| 146 |
? sanitize_key( $_POST['bbp-forums-role'] ) |
| 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() ) { |
| 154 |
return; |
| 155 |
} |
| 156 |
|
| 157 |
// Set the user role |
| 158 |
bbp_set_user_role( $user_id, $valid_role ); |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Multi-site handler for adding a new user |
| 163 |
* |
| 164 |
* @since 2.6.0 bbPress (r6674) |
| 165 |
* |
| 166 |
* @param int $user_id User ID. |
| 167 |
*/ |
| 168 |
function bbp_user_add_role_on_activate( $user_id = 0, $password = '', $meta = array() ) { |
| 169 |
|
| 170 |
// Role to validate |
| 171 |
$to_validate = ! empty( $meta['bbp_new_role'] ) && is_string( $meta['bbp_new_role'] ) |
| 172 |
? sanitize_key( $meta['bbp_new_role'] ) |
| 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() ) { |
| 180 |
return; |
| 181 |
} |
| 182 |
|
| 183 |
// Set the user role |
| 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 |
} |
| 264 |
|