Created
February 26, 2017 12:22
-
-
Save gabrielmerovingi/5afe4b743274d73822a5e80b32ddb8c4 to your computer and use it in GitHub Desktop.
Example of how to run a custom leaderboard query based on annual point gains.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Query Custom BP Leaderboard | |
* @since 1.0 | |
* @version 1.0 | |
*/ | |
function mycred_pro_query_custom_leaderboard( $leaderboard, $group_id, $point_type, $append_user, $fresh ) { | |
// Get setup | |
$setup = mycred_get_bp_group_setup( $group_id ); | |
// This function should only handle our custom type | |
if ( $setup['type'] != 'annual' ) return $leaderboard; | |
// See if we have a cached result, unless a fresh one is requested | |
if ( ! $fresh ) { | |
$leaderboard = groups_get_groupmeta( $group_id, 'cached_leaderboard' . $point_type ); | |
$leaderboard = maybe_unserialize( $leaderboard ); | |
} | |
// If the leaderboard is empty | |
if ( empty( $leaderboard ) || $fresh ) { | |
global $wpdb, $mycred; | |
$until = current_time( 'timestamp' ); | |
$from = mktime( 0, 0, 0, 1, 1, date( 'Y', $until ) ); | |
$group_table = $wpdb->prefix . 'bp_groups_members'; | |
$leaderboard = $wpdb->get_results( $wpdb->prepare( " | |
SELECT log.user_id, SUM( log.creds ) as balance | |
FROM {$mycred->log_table} log | |
LEFT JOIN {$group_table} groups ON ( groups.user_id = log.user_id ) | |
WHERE groups.group_id = %d | |
AND groups.is_confirmed = 1 | |
AND groups.is_banned = 0 | |
AND log.ctype = %s | |
AND log.time BETWEEN %d AND %d | |
GROUP BY log.user_id | |
ORDER BY balance DESC, log.user_id ASC | |
LIMIT 0,%d", $group_id, $point_type, $from, $until, $setup['size'] ) ); | |
// If we have results, cache it | |
if ( ! empty( $leaderboard ) ) | |
groups_update_groupmeta( $group_id, 'cached_leaderboard' . $point_type, serialize( $leaderboard ) ); | |
} | |
// If we need to append the current user | |
if ( ! empty( $leaderboard ) && $append_user !== false ) { | |
// Check if the current user is in the leaderboard | |
$in_list = false; | |
foreach ( $leaderboard as $position => $entry ) { | |
$entry->position = $position+1; | |
$entry->current = false; | |
if ( $entry->user_id == $append_user ) { | |
$entry->current = true; | |
$in_list = true; | |
} | |
} | |
// If we want to attach the current user because they are not in the leaderboard | |
if ( $setup['current'] && ! $in_list ) { | |
$position = mycred_get_users_bp_group_position( $append_user, $group_id, $point_type ); | |
if ( $position !== false ) { | |
// Append the current user | |
$row = new StdClass(); | |
$row->position = $position; | |
$row->user_id = $append_user; | |
$row->balance = mycred_get_users_cred( $append_user, $point_type ); | |
$row->current = true; | |
$leaderboard[] = $row; | |
} | |
} | |
} | |
return $leaderboard; | |
} | |
add_filter( 'mycred_bp_leaderboard_get', 'mycred_pro_query_custom_leaderboard', 10, 5 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment