Advertisement
designbymerovingi

leaderboard multisite fix

Jan 25th, 2017
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.26 KB | None | 0 0
  1. <?php
  2. if ( ! defined( 'myCRED_VERSION' ) ) exit;
  3.  
  4. /**
  5.  * myCRED Shortcode: mycred_leaderboard
  6.  * @see http://codex.mycred.me/shortcodes/mycred_leaderboard/
  7.  * @since 0.1
  8.  * @version 1.5.3
  9.  */
  10. if ( ! function_exists( 'mycred_render_shortcode_leaderboard' ) ) :
  11.     function mycred_render_shortcode_leaderboard( $atts, $content = '' ) {
  12.  
  13.         extract( shortcode_atts( array(
  14.             'number'       => 25,
  15.             'order'        => 'DESC',
  16.             'offset'       => 0,
  17.             'type'         => MYCRED_DEFAULT_TYPE_KEY,
  18.             'based_on'     => 'balance',
  19.             'total'        => 0,
  20.             'wrap'         => 'li',
  21.             'template'     => '#%position% %user_profile_link% %cred_f%',
  22.             'nothing'      => 'Leaderboard is empty',
  23.             'current'      => 0,
  24.             'exclude_zero' => 1,
  25.             'timeframe'    => ''
  26.         ), $atts ) );
  27.  
  28.         if ( ! MYCRED_ENABLE_LOGGING ) return '';
  29.  
  30.         if ( ! mycred_point_type_exists( $type ) )
  31.             $type = MYCRED_DEFAULT_TYPE_KEY;
  32.  
  33.         if ( ! in_array( $order, array( 'ASC', 'DESC' ) ) )
  34.             $order = 'DESC';
  35.  
  36.         if ( $number != '-1' )
  37.             $limit = 'LIMIT ' . absint( $offset ) . ',' . absint( $number );
  38.         else
  39.             $limit = '';
  40.  
  41.         $mycred = mycred( $type );
  42.  
  43.         global $wpdb;
  44.  
  45.         // Option to exclude zero balances
  46.         $excludes = '';
  47.         if ( $exclude_zero == 1 ) {
  48.  
  49.             $balance_format = '%d';
  50.             if ( isset( $mycred->format['decimals'] ) && $mycred->format['decimals'] > 0 ) {
  51.                 $length         = absint( 65 - $mycred->format['decimals'] );
  52.                 $balance_format = 'CAST( %f AS DECIMAL( ' . $length . ', ' . $mycred->format['decimals'] . ' ) )';
  53.             }
  54.  
  55.             if ( $total == 0 )
  56.                 $excludes = $wpdb->prepare( "AND l.meta_value != {$balance_format}", $mycred->zero() );
  57.  
  58.         }
  59.  
  60.         $based_on = sanitize_text_field( $based_on );
  61.  
  62.         // Leaderboard based on balance
  63.         if ( $based_on == 'balance' ) {
  64.  
  65.             $multisite_check = "";
  66.             if ( ! mycred_centralize_log() ) {
  67.  
  68.                 $blog_id         = absint( $GLOBALS['blog_id'] );
  69.                 $multisite_check = "LEFT JOIN {$wpdb->usermeta} cap ON ( l.user_id = cap.user_id AND cap.meta_key = 'cap.wp_{$blog_id}_capabilities' )";
  70.  
  71.             }
  72.  
  73.             // Total balance
  74.             if ( $total == 1 ) {
  75.  
  76.                 $query = $wpdb->prepare( "
  77.                     SELECT l.user_id AS ID, SUM( l.creds ) AS cred
  78.                     FROM {$mycred->log_table} l
  79.                     {$multisite_check}
  80.                     WHERE l.ctype = %s AND ( ( l.creds > 0 ) OR ( l.creds < 0 AND l.ref = 'manual' ) )
  81.                     {$excludes}
  82.                     GROUP BY l.user_id
  83.                     ORDER BY SUM( l.creds ) {$order}, l.user_id ASC
  84.                     {$limit};", $type );
  85.  
  86.             }
  87.  
  88.             // Current Balance
  89.             else {
  90.  
  91.                 $query = $wpdb->prepare( "
  92.                     SELECT DISTINCT u.ID, l.meta_value AS cred
  93.                     FROM {$wpdb->users} u
  94.                     INNER JOIN {$wpdb->usermeta} l ON ( u.ID = l.user_id )
  95.                     {$multisite_check}
  96.                     WHERE l.meta_key = %s
  97.                     {$excludes}
  98.                     ORDER BY l.meta_value+0 {$order}, l.user_id ASC
  99.                     {$limit};", mycred_get_meta_key( $type ) );
  100.  
  101.             }
  102.  
  103.         }
  104.  
  105.         // Leaderboard based on reference
  106.         else {
  107.  
  108.             $time_filter = '';
  109.             $now         = current_time( 'timestamp' );
  110.             if ( $timeframe != '' ) {
  111.  
  112.                 // Start of the week based of our settings
  113.                 $week_starts = get_option( 'start_of_week' );
  114.                 if ( $week_starts == 0 )
  115.                     $week_starts = 'sunday';
  116.                 else
  117.                     $week_starts = 'monday';
  118.  
  119.                 // Filter: Daily
  120.                 if ( $timeframe == 'today' )
  121.                     $time_filter = $wpdb->prepare( "AND log.time BETWEEN %d AND %d", strtotime( 'today midnight', $now ), $now );
  122.  
  123.                 // Filter: Weekly
  124.                 elseif ( $timeframe == 'this-week' )
  125.                     $time_filter = $wpdb->prepare( "AND log.time BETWEEN %d AND %d", strtotime( $week_starts . ' this week', $now ), $now );
  126.  
  127.                 // Filter: Monthly
  128.                 elseif ( $timeframe == 'this-month' )
  129.                     $time_filter = $wpdb->prepare( "AND log.time BETWEEN %d AND %d", strtotime( 'Y-m-01', $now ), $now );
  130.  
  131.                 else {
  132.  
  133.                     $start_from = strtotime( $timeframe, $now );
  134.                     if ( $start_from !== false && $start_from > 0 )
  135.                         $time_filter = $wpdb->prepare( "AND log.time BETWEEN %d AND %d", $start_from, $now );
  136.  
  137.                 }
  138.  
  139.                 $time_filter = apply_filters( 'mycred_leaderboard_time_filter', $time_filter, $based_on, $user_id, $ctype );
  140.  
  141.             }
  142.  
  143.             if ( mycred_centralize_log() )
  144.                 $query = $wpdb->prepare( "SELECT DISTINCT log.user_id AS ID, SUM( log.creds ) AS cred FROM {$mycred->log_table} log WHERE log.ref = %s {$time_filter} GROUP BY log.user_id ORDER BY SUM( log.creds ) {$order} {$limit};", $based_on );
  145.  
  146.             // Multisite support
  147.             else {
  148.  
  149.                 $blog_id = absint( $GLOBALS['blog_id'] );
  150.                 $query   = $wpdb->prepare( "
  151.                     SELECT DISTINCT log.user_id AS ID, SUM( log.creds ) AS cred
  152.                     FROM {$mycred->log_table} log
  153.                     LEFT JOIN {$wpdb->usermeta} cap ON ( log.user_id = cap.user_id AND cap.meta_key = 'cap.wp_{$blog_id}_capabilities' )
  154.                     WHERE log.ref = %s
  155.                     {$time_filter}
  156.                     GROUP BY log.user_id
  157.                     ORDER BY SUM( log.creds ) {$order}, log.user_id ASC
  158.                     {$limit};", $based_on );
  159.  
  160.             }
  161.  
  162.         }
  163.  
  164.         $leaderboard  = $wpdb->get_results( apply_filters( 'mycred_ranking_sql', $query, $atts ), 'ARRAY_A' );
  165.         $output       = '';
  166.         $in_list      = false;
  167.         $current_user = wp_get_current_user();
  168.  
  169.         if ( ! empty( $leaderboard ) ) {
  170.  
  171.             // Check if current user is in the leaderboard
  172.             if ( $current == 1 && is_user_logged_in() ) {
  173.  
  174.                 // Find the current user in the leaderboard
  175.                 foreach ( $leaderboard as $position => $user ) {
  176.                     if ( $user['ID'] == $current_user->ID ) {
  177.                         $in_list = true;
  178.                         break;
  179.                     }
  180.                 }
  181.  
  182.             }
  183.  
  184.             // Load myCRED
  185.             $mycred = mycred( $type );
  186.  
  187.             // Wrapper
  188.             if ( $wrap == 'li' )
  189.                 $output .= '<ol class="myCRED-leaderboard list-unstyled">';
  190.  
  191.             // Loop
  192.             foreach ( $leaderboard as $position => $user ) {
  193.  
  194.                 // Prep
  195.                 $class = array();
  196.  
  197.                 // Position
  198.                 if ( $offset != '' && $offset > 0 )
  199.                     $position = $position + $offset;
  200.  
  201.                 // Classes
  202.                 $class[] = 'item-' . $position;
  203.                 if ( $position == 0 )
  204.                     $class[] = 'first-item';
  205.  
  206.                 if ( $user['ID'] == $current_user->ID )
  207.                     $class[] = 'current-user';
  208.  
  209.                 if ( $position % 2 != 0 )
  210.                     $class[] = 'alt';
  211.  
  212.                 $row_template = $template;
  213.                 if ( ! empty( $content ) )
  214.                     $row_template = $content;
  215.  
  216.                 // Template Tags
  217.                 $layout = str_replace( array( '%ranking%', '%position%' ), $position+1, $row_template );
  218.  
  219.                 $layout = $mycred->template_tags_amount( $layout, $user['cred'] );
  220.                 $layout = $mycred->template_tags_user( $layout, $user['ID'] );
  221.  
  222.                 // Wrapper
  223.                 if ( ! empty( $wrap ) )
  224.                     $layout = '<' . $wrap . ' class="%classes%">' . $layout . '</' . $wrap . '>';
  225.  
  226.                 $layout = str_replace( '%classes%', apply_filters( 'mycred_ranking_classes', implode( ' ', $class ) ), $layout );
  227.                 $layout = apply_filters( 'mycred_ranking_row', $layout, $template, $user, $position+1 );
  228.  
  229.                 $output .= $layout . "\n";
  230.  
  231.             }
  232.  
  233.             // Current user is not in list but we want to show his position
  234.             if ( ! $in_list && $current == 1 && is_user_logged_in() ) {
  235.  
  236.                 // Flush previous query
  237.                 $wpdb->flush();
  238.  
  239.                 $current_position = mycred_render_shortcode_leaderbaord_position( array(
  240.                     'based_on'  => $based_on,
  241.                     'user_id'   => 'current',
  242.                     'timeframe' => $timeframe,
  243.                     'ctype'     => $type
  244.                 ), $content );
  245.  
  246.                 $row_template = $template;
  247.                 if ( ! empty( $content ) )
  248.                     $row_template = $content;
  249.  
  250.                 // Template Tags
  251.                 $layout = str_replace( array( '%ranking%', '%position%' ), $current_position, $row_template );
  252.  
  253.                 $layout = $mycred->template_tags_amount( $layout, $mycred->get_users_balance( $current_user->ID, $type ) );
  254.                 $layout = $mycred->template_tags_user( $layout, false, $current_user );
  255.  
  256.                 // Wrapper
  257.                 if ( ! empty( $wrap ) )
  258.                     $layout = '<' . $wrap . ' class="%classes%">' . $layout . '</' . $wrap . '>';
  259.  
  260.                 $layout = str_replace( '%classes%', apply_filters( 'mycred_ranking_classes', implode( ' ', $class ) ), $layout );
  261.                 $layout = apply_filters( 'mycred_ranking_row', $layout, $template, $current_user, $current_position );
  262.  
  263.                 $output .= $layout . "\n";
  264.  
  265.             }
  266.  
  267.             if ( $wrap == 'li' )
  268.                 $output .= '</ol>';
  269.  
  270.         }
  271.  
  272.         // No result template is set
  273.         else {
  274.  
  275.             $output .= '<p class="mycred-leaderboard-none">' . $nothing . '</p>';
  276.  
  277.         }
  278.  
  279.         return do_shortcode( apply_filters( 'mycred_leaderboard', $output, $atts ) );
  280.  
  281.     }
  282. endif;
  283. add_shortcode( 'mycred_leaderboard', 'mycred_render_shortcode_leaderboard' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement