| 17 | | function bpcore_setup() { |
| 18 | | global $current_user, $source_domain, $bp_nav; |
| 19 | | |
| 20 | | $source_domain = 'http://' . get_usermeta( $current_user->ID, 'source_domain' ) . '/'; |
| | 17 | function bp_core_setup() { |
| | 18 | global $current_user, $loggedin_domain, $loggedin_userid; |
| | 19 | global $current_domain, $current_userid, $bp_nav; |
| | 20 | |
| | 21 | $loggedin_domain = bp_core_get_loggedin_domain(); |
| | 22 | $loggedin_userid = $current_user->ID; |
| | 23 | |
| | 24 | $current_domain = bp_core_get_current_domain(); |
| | 25 | $current_userid = bp_core_get_current_userid(); |
| 28 | | add_action( 'wp', 'bpcore_setup' ); |
| | 33 | add_action( 'wp', 'bp_core_setup' ); |
| | 34 | |
| | 35 | function bp_core_get_loggedin_domain() { |
| | 36 | global $current_user; |
| | 37 | |
| | 38 | if ( VHOST == 'yes' ) { |
| | 39 | $loggedin_domain = 'http://' . get_usermeta( $current_user->ID, 'source_domain' ) . '/'; |
| | 40 | } else { |
| | 41 | $loggedin_domain = 'http://' . get_usermeta( $current_user->ID, 'source_domain' ) . '/' . get_usermeta( $current_user->ID, 'user_login' ) . '/'; |
| | 42 | } |
| | 43 | |
| | 44 | return $loggedin_domain; |
| | 45 | } |
| | 46 | |
| | 47 | function bp_core_get_current_domain() { |
| | 48 | global $current_blog; |
| | 49 | |
| | 50 | if ( VHOST == 'yes' ) { |
| | 51 | $current_domain = 'http://' . $current_blog->domain . '/'; |
| | 52 | } else { |
| | 53 | $current_domain = get_bloginfo('wpurl') . '/'; |
| | 54 | } |
| | 55 | |
| | 56 | return $current_domain; |
| | 57 | } |
| | 58 | |
| | 59 | function bp_core_get_current_userid() { |
| | 60 | $siteuser = bp_core_get_primary_username(); |
| | 61 | $current_userid = bp_core_get_userid($siteuser); |
| | 62 | |
| | 63 | return $current_userid; |
| | 64 | } |
| | 65 | |
| | 66 | function bp_core_get_primary_username() { |
| | 67 | global $current_blog; |
| | 68 | |
| | 69 | if ( VHOST == 'yes' ) { |
| | 70 | $siteuser = explode('.', $current_blog->domain); |
| | 71 | $siteuser = $siteuser[0]; |
| | 72 | } else { |
| | 73 | $siteuser = str_replace('/', '', $current_blog->path); |
| | 74 | } |
| | 75 | |
| | 76 | return $siteuser; |
| | 77 | } |
| 355 | | |
| 356 | | // get the IDs of user blogs in a comma-separated list for use in SQL statements |
| 357 | | function bp_get_blog_ids_of_user( $id, $all = false ) { |
| 358 | | $blogs = get_blogs_of_user( $id, $all ); |
| 359 | | $blog_ids = ""; |
| 360 | | |
| 361 | | if ( $blogs && count($blogs) > 0 ){ |
| 362 | | foreach( $blogs as $blog ) { |
| 363 | | $blog_ids .= $blog->blog_id.","; |
| 364 | | } |
| 365 | | } |
| 366 | | $blog_ids = trim( $blog_ids, "," ); |
| 367 | | return $blog_ids; |
| 368 | | } |
| 369 | | |
| 370 | | // return a tick for a checkbox for a true boolean value |
| 371 | | function bp_boolean_ticked($bool) { |
| 372 | | if ( $bool ) { |
| 373 | | return " checked=\"checked\""; |
| 374 | | } |
| 375 | | return ""; |
| 376 | | } |
| 377 | | |
| 378 | | // return a tick for a checkbox for a particular value |
| 379 | | function bp_value_ticked( $var, $value ) { |
| 380 | | if ( $var == $value ) { |
| 381 | | return " checked=\"checked\""; |
| 382 | | } |
| 383 | | return ""; |
| 384 | | } |
| 385 | | |
| 386 | | // return true for a boolean value from a checkbox |
| 387 | | function bp_boolean( $value = 0 ) { |
| 388 | | if ( $value != "" ) { |
| 389 | | return 1; |
| 390 | | } else { |
| 391 | | return 0; |
| 392 | | } |
| 393 | | } |
| 394 | | |
| 395 | | // return an integer |
| 396 | | function bp_int( $var, $nullToOne=false ) { |
| 397 | | if ( @$var == "" ) { |
| 398 | | if ( $nullToOne ) { |
| 399 | | return 1; |
| 400 | | } else { |
| 401 | | return 0; |
| 402 | | } |
| 403 | | } else { |
| 404 | | return (int)$var; |
| 405 | | } |
| 406 | | } |
| 407 | | |
| 408 | | |
| 409 | | // show a friendly date |
| 410 | | function bp_friendly_date($timestamp) { |
| 411 | | // set the timestamp to now if it hasn't been given |
| 412 | | if ( strlen($timestamp) == 0 ) |
| 413 | | $timestamp = time(); |
| 414 | | |
| 415 | | // create the date string |
| 416 | | if ( date( "m", $timestamp ) == date("m") && date( "d", $timestamp ) == date("d") - 1 && date( "Y", $timestamp ) == date("Y") ) { |
| 417 | | return "yesterday at " . date( "g:i a", $timestamp ); |
| 418 | | } else if ( date( "m", $timestamp ) == date("m") && date( "d", $timestamp ) == date("d") && date( "Y", $timestamp ) == date("Y") ) { |
| 419 | | return "at " . date( "g:i a", $timestamp ); |
| 420 | | } else if ( date( "m", $timestamp) == date("m") && date( "d", $timestamp ) > date("d") - 5 && date( "Y", $timestamp ) == date("Y") ) { |
| 421 | | return "on " . date( "l", $timestamp ) . " at " . date( "g:i a", $timestamp ); |
| 422 | | } else if ( date( "Y", $timestamp) == date("Y") ) { |
| 423 | | return "on " . date( "F jS", $timestamp ); |
| 424 | | } else { |
| 425 | | return "on " . date( "F jS Y", $timestamp ); |
| 426 | | } |
| 427 | | } |
| 428 | | |
| 429 | | // search users |
| 430 | | function bp_search_users( $q, $start = 0, $num = 10 ) { |
| 431 | | if ( trim($q) != "" ) { |
| 432 | | global $wpdb; |
| 433 | | global $current_user; |
| 434 | | |
| 435 | | $sql = "SELECT SQL_CALC_FOUND_ROWS id, user_login, display_name, user_nicename |
| 436 | | FROM " . $wpdb->base_prefix . "users |
| 437 | | WHERE (user_nicename like '%" . $wpdb->escape($q) . "%' |
| 438 | | OR user_email like '%" . $wpdb->escape($q) . "%' |
| 439 | | OR display_name like '%" . $wpdb->escape($q) . "%') |
| 440 | | AND (id <> " . $current_user->ID . " and id > 1) |
| 441 | | LIMIT " . $wpdb->escape($start) . ", " . $wpdb->escape($num) . ";"; |
| 442 | | |
| 443 | | if ( !$users = $wpdb->get_results($sql) ) { |
| 444 | | return false; |
| 445 | | } |
| 446 | | |
| 447 | | $rows = $wpdb->get_var( "SELECT found_rows() AS found_rows" ); |
| 448 | | |
| 449 | | if ( is_array($users) && count($users) > 0 ) { |
| 450 | | for ( $i = 0; $i < count($users); $i++ ) { |
| 451 | | $user = $users[$i]; |
| 452 | | $user->siteurl = $user->user_url; |
| 453 | | $user->blogs = ""; |
| 454 | | $user->blogs = get_blogs_of_user($user->id); |
| 455 | | $user->rows = $rows; |
| 456 | | } |
| 457 | | return $users; |
| 458 | | } else { |
| 459 | | return false; |
| 460 | | } |
| 461 | | } else { |
| 462 | | return false; |
| 463 | | } |
| 464 | | } |
| 465 | | |
| 466 | | // return a ' if the text ends in an "s", or "'s" otherwise |
| 467 | | function bp_end_with_s( $string ) { |
| 468 | | if ( substr( strtolower($string), - 1 ) == "s" ) { |
| 469 | | return $string . "'"; |
| 470 | | } else { |
| 471 | | return $string . "'s"; |
| 472 | | } |
| 473 | | } |
| 474 | | |
| 475 | | // pluralise a string |
| 476 | | function bp_plural( $num, $ifone = "", $ifmore = "s" ) { |
| 477 | | if ( bp_int($num) != 1 ) { |
| 478 | | return $ifmore; |
| 479 | | } else { |
| 480 | | return $ifone; |
| 481 | | } |
| 482 | | } |
| 483 | | |