Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * BP Friends Leaderboard
- * Creates a leaderboard that shows your and your friends balances
- * sorted by your positions. Will show the friend above and below you,
- * unless you are in the bottom or top.
- * @version 1.0
- */
- add_shortcode( 'bp_friends_leaderboard', 'mycred_pro_my_friends_leaderboard' );
- function mycred_pro_my_friends_leaderboard( $atts ) {
- if ( ! function_exists( 'mycred' ) && ! function_exists( 'friends_get_friend_user_ids' ) )
- return 'missing required plugins';
- $mycred = mycred();
- $user_id = get_current_user_id();
- $leaderboard = array();
- // Make sure the user is not excluded to start of
- if ( ! $mycred->exclude_user( $user_id ) ) {
- // Get friends
- $friends = friends_get_friend_user_ids( $user_id );
- // If we have friends
- if ( ! empty( $friends ) ) {
- // Add ourselves in
- $leaderboard[ $user_id ] = $mycred->get_users_balance( $user_id );
- // Add friends to the list with their balance
- foreach ( $friends as $friend_id )
- $leaderboard[ $friend_id ] = $mycred->get_users_balance( $friend_id );
- }
- // If we have a leaderbaord
- if ( ! empty( $leaderboard ) ) {
- // Sort the array
- arsort( $leaderboard, SORT_NUMERIC );
- // Now we have all friends and us sorted according to balance
- $keys = array_flip( array_keys( $leaderboard ) );
- $values = array_values( $leaderboard );
- $content = '<ul>';
- // We are in top position
- if ( ! isset( $values[ $keys[ $user_id ] - 1 ] ) ) {
- $content .= '<li>Me: ' . $values[ $keys[ $user_id ] ] . '</li>';
- if ( isset( $values[ $keys[ $user_id ] + 1 ] ) )
- $content .= '<li>' . bp_core_get_username( $keys[ $user_id ] + 1 ) . ': ' . $values[ $keys[ $user_id ] + 1 ] . '</li>';
- if ( isset( $values[ $keys[ $user_id ] + 2 ] ) )
- $content .= '<li>' . bp_core_get_username( $keys[ $user_id ] + 2 ) . ': ' . $values[ $keys[ $user_id ] + 2 ] . '</li>';
- }
- // We are in bottom position
- elseif ( ! isset( $values[ $keys[ $user_id ] + 1 ] ) ) {
- if ( isset( $values[ $keys[ $user_id ] - 2 ] ) )
- $content .= '<li>' . bp_core_get_username( $keys[ $user_id ] - 2 ) . ': ' . $values[ $keys[ $user_id ] - 2 ] . '</li>';
- if ( isset( $values[ $keys[ $user_id ] - 1 ] ) )
- $content .= '<li>' . bp_core_get_username( $keys[ $user_id ] - 1 ) . ': ' . $values[ $keys[ $user_id ] - 1 ] . '</li>';
- $content .= '<li>Me: ' . $values[ $keys[ $user_id ] ] . '</li>';
- }
- // We are surrounded
- else {
- if ( isset( $values[ $keys[ $user_id ] - 1 ] ) )
- $content .= '<li>' . bp_core_get_username( $keys[ $user_id ] - 1 ) . ': ' . $values[ $keys[ $user_id ] - 1 ] . '</li>';
- $content .= '<li>Me: ' . $values[ $keys[ $user_id ] ] . '</li>';
- if ( isset( $values[ $keys[ $user_id ] + 1 ] ) )
- $content .= '<li>' . bp_core_get_username( $keys[ $user_id ] + 1 ) . ': ' . $values[ $keys[ $user_id ] + 1 ] . '</li>';
- }
- $content .= '</ul>';
- return $content;
- }
- }
- return 'you have no friends';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement