Advertisement
designbymerovingi

Last Months Leaderboard Shortcode

Dec 15th, 2016
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.08 KB | None | 0 0
  1. /**
  2.  * Last Months Points Leaderboard
  3.  * @version 1.2.1
  4.  */
  5. if ( ! function_exists( 'mycred_pro_last_months_leaderboard' ) ) :
  6.     function mycred_pro_last_months_leaderboard( $atts, $content = '' ) {
  7.  
  8.         extract( shortcode_atts( array(
  9.             'number'       => 25,
  10.             'start'        => '',
  11.             'end'          => '',
  12.             'order'        => 'DESC',
  13.             'type'         => MYCRED_DEFAULT_TYPE_KEY,
  14.             'wrap'         => 'li',
  15.             'template'     => '#%position% %user_profile_link% %cred_f%',
  16.             'nothing'      => 'Leaderboard is empty'
  17.         ), $atts ) );
  18.  
  19.         if ( $type != '' && ! mycred_point_type_exists( $type ) )
  20.             $type = MYCRED_DEFAULT_TYPE_KEY;
  21.  
  22.         if ( ! in_array( $order, array( 'ASC', 'DESC' ) ) )
  23.             $order = 'DESC';
  24.  
  25.         global $wpdb;
  26.  
  27.         $mycred       = mycred( $type );
  28.         $wheres       = array();
  29.  
  30.         if ( $type != '' )
  31.             $wheres[] = $wpdb->prepare( "ctype = %s", $type );
  32.  
  33.         $wheres[]     = $wpdb->prepare( "( time BETWEEN %d AND %d )", strtotime( $start ), strtotime( $end ) );
  34.  
  35.         $where        = implode( ' AND ', $wheres );
  36.  
  37.         $limit        = '';
  38.         if ( $number > 0 ) $limit = $wpdb->prepare( "LIMIT 0,%d", $number );
  39.  
  40.         $leaderboard = $wpdb->get_results( "SELECT DISTINCT user_id AS ID, SUM( creds ) AS cred FROM {$mycred->log_table} WHERE {$where} GROUP BY user_id ORDER BY cred {$order} {$limit};", 'ARRAY_A'  );
  41.  
  42.         $output       = '';
  43.         $in_list      = false;
  44.         $current_user = wp_get_current_user();
  45.  
  46.         if ( ! empty( $leaderboard ) ) {
  47.  
  48.             // Wrapper
  49.             if ( $wrap == 'li' )
  50.                 $output .= '<ol class="myCRED-leaderboard list-unstyled">';
  51.  
  52.             // Loop
  53.             foreach ( $leaderboard as $position => $user ) {
  54.  
  55.                 // Prep
  56.                 $class   = array();
  57.  
  58.                 // Classes
  59.                 $class[] = 'item-' . $position;
  60.                 if ( $position == 0 )
  61.                     $class[] = 'first-item';
  62.  
  63.                 if ( isset( $current_user->ID ) && $user['ID'] == $current_user->ID )
  64.                     $class[] = 'current-user';
  65.  
  66.                 if ( $position % 2 != 0 )
  67.                     $class[] = 'alt';
  68.  
  69.                 $row_template = $template;
  70.                 if ( ! empty( $content ) )
  71.                     $row_template = $content;
  72.  
  73.                 // Template Tags
  74.                 $layout  = str_replace( array( '%ranking%', '%position%' ), $position+1, $row_template );
  75.  
  76.                 $layout  = $mycred->template_tags_amount( $layout, $user['cred'] );
  77.                 $layout  = $mycred->template_tags_user( $layout, $user['ID'] );
  78.  
  79.                 // Wrapper
  80.                 if ( ! empty( $wrap ) )
  81.                     $layout = '<' . $wrap . ' class="%classes%">' . $layout . '</' . $wrap . '>';
  82.  
  83.                 $layout  = str_replace( '%classes%', apply_filters( 'mycred_ranking_classes', implode( ' ', $class ) ), $layout );
  84.                 $layout  = apply_filters( 'mycred_ranking_row', $layout, $template, $user, $position+1 );
  85.  
  86.                 $output .= $layout . "\n";
  87.  
  88.             }
  89.  
  90.             if ( $wrap == 'li' )
  91.                 $output .= '</ol>';
  92.  
  93.         }
  94.  
  95.         // No result template is set
  96.         else {
  97.  
  98.             $output .= '<p class="mycred-leaderboard-none">' . $nothing . '</p>';
  99.  
  100.         }
  101.  
  102.         return $output;
  103.  
  104.     }
  105. endif;
  106.  
  107. function mycred_pro_load_custom_shortcode() {
  108.  
  109.     add_shortcode( 'last_months_leaderboard', 'mycred_pro_last_months_leaderboard' );
  110.  
  111. }
  112. add_action( 'mycred_init', 'mycred_pro_load_custom_shortcode' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement