Advertisement
designbymerovingi

temp rank progress

Jun 7th, 2016
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. // Get user's rank progress
  2. function get_mycred_users_rank_progress( $user_id = NULL, $show_rank = 'no' ) {
  3.  
  4. if ( ! function_exists( 'mycred' ) ) return '';
  5.  
  6. // Change rank data to displayed user when on a user's profile
  7. if ( function_exists( 'bp_is_user' ) && bp_is_user() && $user_id === NULL ) {
  8. $user_id = bp_displayed_user_id();
  9. }
  10.  
  11. // Load myCRED
  12. $mycred = mycred();
  13.  
  14. // Check if user is excluded
  15. if ( $mycred->core->exclude_user( $user_id ) ) return '';
  16.  
  17. // Ranks are based on a total
  18. if ( $mycred->rank['base'] == 'total' )
  19. $key = $mycred->get_cred_id() . '_total';
  20.  
  21. // Ranks are based on current balance
  22. else
  23. $key = $mycred->get_cred_id();
  24.  
  25. // Get Balance
  26. $users_balance = $mycred->get_users_cred( $user_id, $key );
  27.  
  28. // Rank Progress
  29.  
  30. // Get the users current rank post ID
  31. $users_rank = mycred_get_users_rank_id( $user_id );
  32.  
  33. // Get the name of the users current rank
  34. $users_rank_name = get_the_title( $users_rank );
  35.  
  36. // Get the ranks set max
  37. $max = get_post_meta( $users_rank, 'mycred_rank_max', true );
  38.  
  39. global $wpdb;
  40.  
  41. $tabl_name = $wpdb->prefix . 'postmeta';
  42.  
  43. // Get the users next rank post ID
  44. $next_ranks = $wpdb->get_results( $wpdb->prepare( "SELECT post_id FROM {$tabl_name} WHERE meta_key = %s AND meta_value > %d ORDER BY meta_value * 1 LIMIT 1;", 'mycred_rank_min', $max ) );
  45. foreach( $next_ranks as $next_rank ) {
  46. $next_rank = $next_rank->post_id;
  47. }
  48.  
  49. // Get the name of the users next rank
  50. $next_rank_name = get_the_title( $next_rank );
  51.  
  52. // Get the ranks set min
  53. $next_rank_min = get_post_meta( $next_rank, 'mycred_rank_min', true );
  54.  
  55. // Calculate progress. We need a percentage with 1 decimal
  56. if ( $users_balance > 0 )
  57. $progress = number_format( ( ( $users_balance / $max ) * 100 ), 0 );
  58. else
  59. $progress = 0;
  60.  
  61. // Display rank progress bar
  62. echo '<div class="mycred-rank-progress">';
  63. echo '<h3 class="rank-progress-label" style="font-weight:bold;">Rank Progress ('. $progress .'%)</h3>';
  64. echo '<progress max="' . $max . '" value="' . $users_balance . '" class="rank-progress-bar">';
  65. echo '</progress>';
  66. if ( $show_rank == 'yes' ) {
  67. echo '<span class="current-rank" style="float:left;padding-top:1%;font-weight:bold;">'. $users_rank_name .'</span>';
  68. echo '<span class="next-rank" style="float:right;padding-top:1%;font-weight:bold;">'. $next_rank_name .'</span>';
  69. echo '<span class="points-progress" style="width:100%;float:left;margin-top: -4.5%;padding-top:1%;font-weight:bold;text-align:center;">'. $users_balance .' of '. $next_rank_min .'</span>';
  70. }
  71. echo '</div>';
  72.  
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement