Advertisement
designbymerovingi

myCRED: My friends leaderboard

Jan 28th, 2015
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.00 KB | None | 0 0
  1. /**
  2.  * BP Friends Leaderboard
  3.  * Creates a leaderboard that shows your and your friends balances
  4.  * sorted by your positions. Will show the friend above and below you,
  5.  * unless you are in the bottom or top.
  6.  * @version 1.0
  7.  */
  8. add_shortcode( 'bp_friends_leaderboard', 'mycred_pro_my_friends_leaderboard' );
  9. function mycred_pro_my_friends_leaderboard( $atts ) {
  10.  
  11.     if ( ! function_exists( 'mycred' ) && ! function_exists( 'friends_get_friend_user_ids' ) )
  12.         return 'missing required plugins';
  13.  
  14.     $mycred = mycred();
  15.     $user_id = get_current_user_id();
  16.     $leaderboard = array();
  17.  
  18.     // Make sure the user is not excluded to start of
  19.     if ( ! $mycred->exclude_user( $user_id ) ) {
  20.  
  21.         // Get friends
  22.         $friends = friends_get_friend_user_ids( $user_id );
  23.  
  24.         // If we have friends
  25.         if ( ! empty( $friends ) ) {
  26.  
  27.             // Add ourselves in
  28.             $leaderboard[ $user_id ] = $mycred->get_users_balance( $user_id );
  29.  
  30.             // Add friends to the list with their balance
  31.             foreach ( $friends as $friend_id )
  32.                 $leaderboard[ $friend_id ] = $mycred->get_users_balance( $friend_id );
  33.  
  34.         }
  35.  
  36.         // If we have a leaderbaord
  37.         if ( ! empty( $leaderboard ) ) {
  38.  
  39.             // Sort the array
  40.             arsort( $leaderboard, SORT_NUMERIC );
  41.  
  42.             // Now we have all friends and us sorted according to balance
  43.             $keys = array_flip( array_keys( $leaderboard ) );
  44.             $values = array_values( $leaderboard );
  45.  
  46.             $content = '<ul>';
  47.  
  48.             // We are in top position
  49.             if ( ! isset( $values[ $keys[ $user_id ] - 1 ] ) ) {
  50.  
  51.                 $content .= '<li>Me: ' . $values[ $keys[ $user_id ] ] . '</li>';
  52.  
  53.                 if ( isset( $values[ $keys[ $user_id ] + 1 ] ) )
  54.                     $content .= '<li>' . bp_core_get_username( $keys[ $user_id ] + 1 ) . ': ' . $values[ $keys[ $user_id ] + 1 ] . '</li>';
  55.  
  56.                 if ( isset( $values[ $keys[ $user_id ] + 2 ] ) )
  57.                     $content .= '<li>' . bp_core_get_username( $keys[ $user_id ] + 2 ) . ': ' . $values[ $keys[ $user_id ] + 2 ] . '</li>';
  58.  
  59.             }
  60.  
  61.             // We are in bottom position
  62.             elseif ( ! isset( $values[ $keys[ $user_id ] + 1 ] ) ) {
  63.  
  64.                 if ( isset( $values[ $keys[ $user_id ] - 2 ] ) )
  65.                     $content .= '<li>' . bp_core_get_username( $keys[ $user_id ] - 2 ) . ': ' . $values[ $keys[ $user_id ] - 2 ] . '</li>';
  66.  
  67.                 if ( isset( $values[ $keys[ $user_id ] - 1 ] ) )
  68.                     $content .= '<li>' . bp_core_get_username( $keys[ $user_id ] - 1 ) . ': ' . $values[ $keys[ $user_id ] - 1 ] . '</li>';
  69.  
  70.                 $content .= '<li>Me: ' . $values[ $keys[ $user_id ] ] . '</li>';
  71.  
  72.             }
  73.  
  74.             // We are surrounded
  75.             else {
  76.  
  77.                 if ( isset( $values[ $keys[ $user_id ] - 1 ] ) )
  78.                     $content .= '<li>' . bp_core_get_username( $keys[ $user_id ] - 1 ) . ': ' . $values[ $keys[ $user_id ] - 1 ] . '</li>';
  79.  
  80.                 $content .= '<li>Me: ' . $values[ $keys[ $user_id ] ] . '</li>';
  81.  
  82.                 if ( isset( $values[ $keys[ $user_id ] + 1 ] ) )
  83.                     $content .= '<li>' . bp_core_get_username( $keys[ $user_id ] + 1 ) . ': ' . $values[ $keys[ $user_id ] + 1 ] . '</li>';
  84.  
  85.             }
  86.  
  87.             $content .= '</ul>';
  88.             return $content;
  89.  
  90.         }
  91.  
  92.     }
  93.  
  94.     return 'you have no friends';
  95.  
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement