[ Index ] |
PHP Cross Reference of BuddyPress |
[Summary view] [Print] [Text view]
1 <?php 2 3 /* Users */ 4 5 function bb_block_current_user() { 6 global $bbdb; 7 if ( $id = bb_get_current_user_info( 'id' ) ) 8 bb_update_usermeta( $id, $bbdb->prefix . 'been_blocked', 1 ); // Just for logging. 9 bb_logout(); 10 bb_die(__("You've been blocked. If you think a mistake has been made, contact this site's administrator.")); 11 } 12 13 function bb_get_user( $user_id, $args = null ) { 14 global $bbdb, $wp_users_object; 15 16 // Get user 17 $user = $wp_users_object->get_user( $user_id, $args ); 18 19 // Return on no user or error object 20 if ( !is_object( $user ) || is_wp_error( $user ) ) 21 return false; 22 23 // Re calculate the user's meta in case we're pulling from a value cached on another site 24 if ( $user_vars = get_object_vars( $user ) ) { 25 $prefix_length = strlen( $bbdb->prefix ); 26 foreach ( $user_vars as $k => $v ) { 27 if ( 0 === strpos( $k, $bbdb->prefix ) ) { 28 $user->{substr( $k, $prefix_length )} = $v; 29 } 30 } 31 } 32 33 return $user; 34 } 35 36 function bb_cache_users( $ids ) { 37 global $wp_users_object; 38 $wp_users_object->get_user( $ids ); 39 } 40 41 function bb_get_user_by_nicename( $nicename ) { 42 global $wp_users_object; 43 $user = $wp_users_object->get_user( $nicename, array( 'by' => 'nicename' ) ); 44 if ( is_wp_error($user) ) 45 return false; 46 return bb_get_user( $user->ID ); 47 } 48 49 function bb_delete_user( $user_id, $reassign = 0 ) { 50 global $wp_users_object, $bbdb; 51 52 if ( !$user = bb_get_user( $user_id ) ) 53 return false; 54 55 if ( $reassign ) { 56 if ( !$new_user = bb_get_user( $reassign ) ) 57 return false; 58 $bbdb->update( $bbdb->posts, array( 'poster_id' => $new_user->ID ), array( 'poster_id' => $user->ID ) ); 59 $bbdb->update( $bbdb->term_relationships, array( 'user_id' => $new_user->ID ), array( 'user_id' => $user->ID ) ); 60 $bbdb->update( $bbdb->topics, array( 'topic_poster' => $new_user->ID, 'topic_poster_name' => $new_user->user_login), array( 'topic_poster' => $user->ID ) ); 61 $bbdb->update( $bbdb->topics, array( 'topic_last_poster' => $new_user->ID, 'topic_last_poster_name' => $new_user->user_login ), array( 'topic_last_poster' => $user->ID ) ); 62 bb_update_topics_replied( $new_user->ID ); 63 wp_cache_flush( 'bb_post' ); 64 wp_cache_flush( 'bb_thread' ); 65 wp_cache_flush( 'bb_topic_tag' ); 66 wp_cache_flush( 'bb_topic' ); 67 } 68 69 do_action( 'bb_delete_user', $user->ID, $reassign ); 70 71 $wp_users_object->delete_user( $user->ID ); 72 73 return true; 74 } 75 76 function bb_update_topics_replied( $user_id ) { 77 global $bbdb; 78 79 $user_id = (int) $user_id; 80 81 if ( !$user = bb_get_user( $user_id ) ) 82 return false; 83 84 $topics_replied = (int) $bbdb->get_var( $bbdb->prepare( "SELECT COUNT(DISTINCT topic_id) FROM $bbdb->posts WHERE post_status = '0' AND poster_id = %d", $user_id ) ); 85 return bb_update_usermeta( $user_id, $bbdb->prefix . 'topics_replied', $topics_replied ); 86 } 87 88 function bb_update_user_status( $user_id, $user_status = 0 ) { 89 global $wp_users_object; 90 $user = bb_get_user( $user_id ); 91 $user_status = (int) $user_status; 92 $wp_users_object->update_user( $user->ID, compact( 'user_status' ) ); 93 } 94 95 function bb_trusted_roles() { 96 return apply_filters( 'bb_trusted_roles', array('moderator', 'administrator', 'keymaster') ); 97 } 98 99 function bb_is_trusted_user( $user ) { // ID, user_login, WP_User, DB user obj 100 if ( is_numeric($user) || is_string($user) ) 101 $user = new BP_User( $user ); 102 elseif ( is_object($user) && is_a($user, 'BP_User') ); // Intentional 103 elseif ( is_object($user) && isset($user->ID) && isset($user->user_login) ) // Make sure it's actually a user object 104 $user = new BP_User( $user->ID ); 105 else 106 return; 107 108 if ( !$user->ID ) 109 return; 110 111 return apply_filters( 'bb_is_trusted_user', (bool) array_intersect(bb_trusted_roles(), $user->roles), $user->ID ); 112 } 113 114 function bb_apply_wp_role_map_to_user( $user, $reload = true ) { 115 // Expects only user ids 116 if ( !is_numeric( $user ) ) { 117 return; 118 } 119 120 $user = (int) $user; 121 122 if ( !$wordpress_table_prefix = bb_get_option('wp_table_prefix') ) { 123 return; 124 } 125 126 if ( $wordpress_mu_primary_blog_id = bb_get_option( 'wordpress_mu_primary_blog_id' ) ) { 127 $wordpress_table_prefix .= $wordpress_mu_primary_blog_id . '_'; 128 } 129 130 if ( !$wordpress_roles_map = bb_get_option( 'wp_roles_map' ) ) { 131 return; 132 } 133 134 global $bbdb; 135 global $wp_roles; 136 global $bb; 137 138 static $bbpress_roles_map = false; 139 140 if ( !$bbpress_roles_map ) { 141 $bbpress_roles_map = array(); 142 foreach ( $wp_roles->get_names() as $_bbpress_role => $_bbpress_rolename ) { 143 $bbpress_roles_map[$_bbpress_role] = 'subscriber'; 144 } 145 unset( $_bbpress_role, $_bbpress_rolename ); 146 $bbpress_roles_map = array_merge( $bbpress_roles_map, array_flip( $wordpress_roles_map ) ); 147 unset( $bbpress_roles_map['inactive'], $bbpress_roles_map['blocked'] ); 148 } 149 150 static $wordpress_userlevel_map = array( 151 'administrator' => 10, 152 'editor' => 7, 153 'author' => 2, 154 'contributor' => 1, 155 'subscriber' => 0 156 ); 157 158 $bbpress_roles = bb_get_usermeta( $user, $bbdb->prefix . 'capabilities' ); 159 $wordpress_roles = bb_get_usermeta( $user, $wordpress_table_prefix . 'capabilities' ); 160 161 if ( !$bbpress_roles && is_array( $wordpress_roles ) ) { 162 $bbpress_roles_new = array(); 163 164 foreach ( $wordpress_roles as $wordpress_role => $wordpress_role_value ) { 165 if ( $wordpress_roles_map[strtolower( $wordpress_role )] && $wordpress_role_value ) { 166 $bbpress_roles_new[$wordpress_roles_map[strtolower( $wordpress_role )]] = true; 167 } 168 } 169 170 if ( count( $bbpress_roles_new ) ) { 171 bb_update_usermeta( $user, $bbdb->prefix . 'capabilities', $bbpress_roles_new ); 172 if ( $reload ) { 173 header( 'Location: ' . bb_get_uri( null, null, BB_URI_CONTEXT_HEADER ) ); 174 exit; 175 } 176 } 177 } elseif ( !$wordpress_roles && is_array( $bbpress_roles ) ) { 178 $wordpress_roles_new = array(); 179 180 foreach ( $bbpress_roles as $bbpress_role => $bbpress_role_value ) { 181 if ( $bbpress_roles_map[strtolower( $bbpress_role )] && $bbpress_role_value ) { 182 $wordpress_roles_new[$bbpress_roles_map[strtolower( $bbpress_role )]] = true; 183 $wordpress_userlevels_new[] = $wordpress_userlevel_map[$bbpress_roles_map[strtolower( $bbpress_role )]]; 184 } 185 } 186 187 if ( count( $wordpress_roles_new ) ) { 188 bb_update_usermeta( $user, $wordpress_table_prefix . 'capabilities', $wordpress_roles_new ); 189 bb_update_usermeta( $user, $wordpress_table_prefix . 'user_level', max( $wordpress_userlevels_new ) ); 190 } 191 } 192 } 193 194 function bb_apply_wp_role_map_to_orphans() { 195 if ( !$wordpress_table_prefix = bb_get_option('wp_table_prefix') ) { 196 return; 197 } 198 199 if ( $wordpress_mu_primary_blog_id = bb_get_option( 'wordpress_mu_primary_blog_id' ) ) { 200 $wordpress_table_prefix .= $wordpress_mu_primary_blog_id . '_'; 201 } 202 203 $role_query = <<<EOQ 204 SELECT 205 ID 206 FROM 207 `%1\$s` 208 LEFT JOIN `%2\$s` AS bbrole 209 ON ID = bbrole.user_id 210 AND bbrole.meta_key = '%3\$scapabilities' 211 LEFT JOIN `%2\$s` AS wprole 212 ON ID = wprole.user_id 213 AND wprole.meta_key = '%4\$scapabilities' 214 WHERE 215 bbrole.meta_key IS NULL OR 216 bbrole.meta_value IS NULL OR 217 wprole.meta_key IS NULL OR 218 wprole.meta_value IS NULL 219 ORDER BY 220 ID 221 EOQ; 222 223 global $bbdb; 224 225 $role_query = $bbdb->prepare( $role_query, $bbdb->users, $bbdb->usermeta, $bbdb->prefix, $wordpress_table_prefix ); 226 227 if ( $user_ids = $bbdb->get_col( $role_query ) ) { 228 foreach ( $user_ids as $user_id ) { 229 bb_apply_wp_role_map_to_user( $user_id, false ); 230 } 231 } 232 } 233 234 /** 235 * Updates a user's details in the database 236 * 237 * {@internal Missing Long Description}} 238 * 239 * @since 0.7.2 240 * @global bbdb $bbdb 241 * 242 * @param int $user_id 243 * @param string $user_email 244 * @param string $user_url 245 * @return int 246 */ 247 function bb_update_user( $user_id, $user_email, $user_url, $display_name ) { 248 global $wp_users_object; 249 250 $user_id = (int) $user_id; 251 $user_url = bb_fix_link( $user_url ); 252 253 $wp_users_object->update_user( $user_id, compact( 'user_email', 'user_url', 'display_name' ) ); 254 255 do_action('bb_update_user', $user_id); 256 return $user_id; 257 } 258 259 /** 260 * Sends a reset password email 261 * 262 * Sends an email to the email address specified in the user's profile 263 * prompting them to change their password. 264 * 265 * @since 0.7.2 266 * @global bbdb $bbdb 267 * 268 * @param string $user_login 269 * @return bool 270 */ 271 function bb_reset_email( $user_login ) 272 { 273 global $bbdb; 274 275 $user_login = sanitize_user( $user_login, true ); 276 277 if ( !$user = $bbdb->get_row( $bbdb->prepare( "SELECT * FROM $bbdb->users WHERE user_login = %s", $user_login ) ) ) { 278 return new WP_Error( 'user_does_not_exist', __( 'The specified user does not exist.' ) ); 279 } 280 281 $resetkey = substr( md5( bb_generate_password() ), 0, 15 ); 282 bb_update_usermeta( $user->ID, 'newpwdkey', $resetkey ); 283 284 $reseturi = bb_get_uri( 285 'bb-reset-password.php', 286 array( 'key' => $resetkey ), 287 BB_URI_CONTEXT_TEXT + BB_URI_CONTEXT_BB_USER_FORMS 288 ); 289 290 $message = sprintf( 291 __( "If you wanted to reset your password, you may do so by visiting the following address:\n\n%s\n\nIf you don't want to reset your password, just ignore this email. Thanks!" ), 292 $reseturi 293 ); 294 $message = apply_filters( 'bb_reset_email_message', $message, $user, $reseturi, $resetkey ); 295 296 $subject = sprintf( 297 __( '%s: Password Reset' ), 298 bb_get_option( 'name' ) 299 ); 300 $subject = apply_filters( 'bb_reset_email_subject', $subject, $user ); 301 302 $mail_result = bb_mail( 303 bb_get_user_email( $user->ID ), 304 $subject, 305 $message 306 ); 307 308 if ( !$mail_result ) { 309 return new WP_Error( 'sending_mail_failed', __( 'The email containing the password reset link could not be sent.' ) ); 310 } 311 312 return true; 313 } 314 315 /** 316 * Handles the resetting of users' passwords 317 * 318 * Handles resetting a user's password, prompted by an email sent by 319 * {@see bb_reset_email()} 320 * 321 * @since 0.7.2 322 * @global bbdb $bbdb 323 * 324 * @param string $key 325 * @return unknown 326 */ 327 function bb_reset_password( $key ) 328 { 329 global $bbdb; 330 331 $key = sanitize_user( $key, true ); 332 333 if ( empty( $key ) || !is_string( $key ) ) { 334 return new WP_Error( 'invalid_key', __( 'Invalid key' ) ); 335 } 336 337 if ( !$user_id = $bbdb->get_var( $bbdb->prepare( "SELECT user_id FROM $bbdb->usermeta WHERE meta_key = 'newpwdkey' AND meta_value = %s", $key ) ) ) { 338 return new WP_Error( 'invalid_key', __( 'Invalid key' ) ); 339 } 340 341 $user = new BP_User( $user_id ); 342 343 if ( !$user || is_wp_error( $user ) ) { 344 return new WP_Error( 'invalid_key', __( 'Invalid key' ) ); 345 } 346 347 if ( bb_has_broken_pass( $user->ID ) ) { 348 bb_block_current_user(); 349 } 350 351 if ( !$user->has_cap( 'change_user_password', $user->ID ) ) { 352 return new WP_Error( 'permission_denied', __( 'You are not allowed to change your password.' ) ); 353 } 354 355 $newpass = bb_generate_password(); 356 bb_update_user_password( $user->ID, $newpass ); 357 if ( !bb_send_pass( $user->ID, $newpass ) ) { 358 return new WP_Error( 'sending_mail_failed', __( 'The email containing the new password could not be sent.' ) ); 359 } 360 361 bb_update_usermeta( $user->ID, 'newpwdkey', '' ); 362 return true; 363 } 364 365 /** 366 * Updates a user's password in the database 367 * 368 * {@internal Missing Long Description}} 369 * 370 * @since 0.7.2 371 * @global bbdb $bbdb 372 * 373 * @param int $user_id 374 * @param string $password 375 * @return int 376 */ 377 function bb_update_user_password( $user_id, $password ) { 378 global $wp_users_object; 379 380 $user_id = (int) $user_id; 381 382 $wp_users_object->set_password( $password, $user_id ); 383 384 do_action('bb_update_user_password', $user_id); 385 return $user_id; 386 } 387 388 /** 389 * Sends an email with the user's new password 390 * 391 * {@internal Missing Long Description}} 392 * 393 * @since 0.7.2 394 * @global bbdb $bbdb {@internal Not used}} 395 * 396 * @param int|string $user 397 * @param string $pass 398 * @return bool 399 */ 400 function bb_send_pass( $user, $pass ) 401 { 402 if ( !$user = bb_get_user( $user ) ) { 403 return false; 404 } 405 406 $message = sprintf( 407 __( "Your username is: %1\$s \nYour password is: %2\$s \nYou can now log in: %3\$s \n\nEnjoy!" ), 408 $user->user_login, 409 $pass, 410 bb_get_uri( null, null, BB_URI_CONTEXT_TEXT ) 411 ); 412 $message = apply_filters( 'bb_send_pass_message', $message, $user, $pass ); 413 414 $subject = sprintf( 415 __( '%s: Password' ), 416 bb_get_option( 'name' ) 417 ); 418 $subject = apply_filters( 'bb_send_pass_subject', $subject, $user ); 419 420 return bb_mail( 421 bb_get_user_email( $user->ID ), 422 $subject, 423 $message 424 ); 425 } 426 427 428 429 /* Favorites */ 430 431 function get_user_favorites( $user_id, $topics = false ) { 432 $user = bb_get_user( $user_id ); 433 if ( !empty($user->favorites) ) { 434 if ( $topics ) 435 $query = new BB_Query( 'topic', array('favorites' => $user_id, 'index_hint' => 'USE INDEX (`forum_time`)'), 'get_user_favorites' ); 436 else 437 $query = new BB_Query( 'post', array('favorites' => $user_id), 'get_user_favorites' ); 438 return $query->results; 439 } 440 } 441 442 function is_user_favorite( $user_id = 0, $topic_id = 0 ) { 443 if ( $user_id ) 444 $user = bb_get_user( $user_id ); 445 else 446 global $user; 447 if ( $topic_id ) 448 $topic = get_topic( $topic_id ); 449 else 450 global $topic; 451 if ( !$user || !$topic ) 452 return; 453 454 if ( isset($user->favorites) ) 455 return in_array($topic->topic_id, explode(',', $user->favorites)); 456 return false; 457 } 458 459 function bb_add_user_favorite( $user_id, $topic_id ) { 460 global $bbdb; 461 $user_id = (int) $user_id; 462 $topic_id = (int) $topic_id; 463 $user = bb_get_user( $user_id ); 464 $topic = get_topic( $topic_id ); 465 if ( !$user || !$topic ) 466 return false; 467 468 $favorites_key = $bbdb->prefix . 'favorites'; 469 $fav = $user->$favorites_key ? explode(',', $user->$favorites_key) : array(); 470 if ( ! in_array( $topic_id, $fav ) ) { 471 $fav[] = $topic_id; 472 $fav = implode(',', $fav); 473 bb_update_usermeta( $user->ID, $favorites_key, $fav ); 474 } 475 do_action('bb_add_user_favorite', $user_id, $topic_id); 476 return true; 477 } 478 479 function bb_remove_user_favorite( $user_id, $topic_id ) { 480 global $bbdb; 481 $user_id = (int) $user_id; 482 $topic_id = (int) $topic_id; 483 $user = bb_get_user( $user_id ); 484 if ( !$user ) 485 return false; 486 487 $favorites_key = $bbdb->prefix . 'favorites'; 488 $fav = explode(',', $user->$favorites_key); 489 if ( is_int( $pos = array_search($topic_id, $fav) ) ) { 490 array_splice($fav, $pos, 1); 491 $fav = implode(',', $fav); 492 bb_update_usermeta( $user->ID, $favorites_key, $fav); 493 } 494 do_action('bb_remove_user_favorite', $user_id, $topic_id); 495 return true; 496 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Dec 7 01:01:35 2017 | Cross-referenced by PHPXref 0.7.1 |