Advertisement
designbymerovingi

myCRED Assign Rank Function Fix

Oct 3rd, 2017
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.43 KB | None | 0 0
  1. if ( ! function_exists( 'mycred_assign_ranks' ) ) :
  2.     function mycred_assign_ranks( $point_type = MYCRED_DEFAULT_TYPE_KEY ) {
  3.  
  4.         global $wpdb;
  5.  
  6.         $mycred = mycred( $point_type );
  7.  
  8.         $end = '';
  9.         if ( $point_type != MYCRED_DEFAULT_TYPE_KEY )
  10.             $end = $point_type;
  11.  
  12.         do_action( 'mycred_assign_ranks_start' );
  13.  
  14.         $balance_format = '%d';
  15.         if ( isset( $mycred->format['decimals'] ) && $mycred->format['decimals'] > 0 ) {
  16.             $length         = 65 - $mycred->format['decimals'];
  17.             $balance_format = 'CAST( %f AS DECIMAL( ' . $length . ', ' . $mycred->format['decimals'] . ' ) )';
  18.         }
  19.  
  20.         $ranks = mycred_get_ranks( 'publish', '-1', 'ASC', $point_type );
  21.  
  22.         $balance_key =  mycred_get_meta_key( $point_type );
  23.         if ( mycred_rank_based_on_total( $point_type ) )
  24.             $balance_key =  mycred_get_meta_key( $point_type, '_total' );
  25.  
  26.         $count = 0;
  27.         if ( ! empty( $ranks ) ) {
  28.             foreach ( $ranks as $rank ) {
  29.  
  30.                 $count += $wpdb->query( $wpdb->prepare( "
  31.                     UPDATE {$wpdb->usermeta} ranks
  32.                         INNER JOIN {$wpdb->usermeta} balance ON ( ranks.user_id = balance.user_id AND balance.meta_key = %s )
  33.                     SET ranks.meta_value = %d
  34.                     WHERE ranks.meta_key = %s
  35.                         AND balance.meta_value BETWEEN {$balance_format} AND {$balance_format};", $balance_key, $rank->post_id, mycred_get_meta_key( 'mycred_rank', $end ), $rank->minimum, $rank->maximum ) );
  36.  
  37.             }
  38.         }
  39.  
  40.         do_action( 'mycred_assign_ranks_end' );
  41.  
  42.         return $count;
  43.  
  44.     }
  45. endif;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement