Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nadeem-khan/2ba59ef6337e8eb761bb to your computer and use it in GitHub Desktop.
Save nadeem-khan/2ba59ef6337e8eb761bb to your computer and use it in GitHub Desktop.
/**
* Custom myCRED Widget: This months leaderboard
* This widget will show this months leaderbaord with the option to set
* a title, the number of users to include and if it should be visible for
* non-members.
* @install Paste into your theme or child-themes functions.php file
* @author Gabriel S Merovingi
* @version 1.0
*/
if ( function_exists( 'mycred_label' ) ) :
/**
* Step 1: Register Widget
* @version 1.0
*/
add_action( 'widgets_init', 'mycred_register_custom_monthly_mycred_widget' );
function mycred_register_custom_monthly_mycred_widget()
{
register_widget( 'myCRED_Widget_This_Months_Leaderboard' );
}
/**
* Step 2: Construct Widget
* @version 1.0
*/
if ( ! class_exists( 'myCRED_Widget_This_Months_Leaderboard' ) ) {
class myCRED_Widget_This_Months_Leaderboard extends WP_Widget {
// Constructor
public function __construct() {
$name = mycred_label();
// Basic details about our widget
$widget_ops = array(
'classname' => 'widget-mycred-this-months-leaderboard',
'description' => __( 'Show the leaderboard for a given timeframe.', 'mycred' )
);
$this->WP_Widget( 'mycred_widget_this_months_leaderboard', __( 'This Months Leaderboard', 'mycred' ), $widget_ops );
$this->alt_option_name = 'mycred_widget_this_months_leaderboard';
}
// Widget Output (what users see)
public function widget( $args, $instance ) {
extract( $args, EXTR_SKIP );
// Check if we want to show this to visitors
if ( ! $instance['show_visitors'] && ! is_user_logged_in() ) return;
// Get the leaderboard
$leaderboard = $this->get_leaderboard( $instance['number'], $widget_id );
// Load myCRED
$mycred = mycred();
// Start constructing Widget
echo $before_widget;
// Title (if not empty)
if ( ! empty( $instance['title'] ) ) {
echo $before_title;
// Allow general tempalte tags in the title
echo $mycred->template_tags_general( $instance['title'] );
echo $after_title;
}
// Construct unorganized list for each row
echo '<ul class="mycred-this-weeks-leaderboard">';
foreach ($leaderboard as $position => $data) {
echo '<li>';
$user_info = get_userdata($data->user_id);
$tempname = $user_info->user_login;
$lowerstring = strtolower($tempname);
$stringwithoutspace = strtr($lowerstring, ' ', '-');
$username = preg_replace('/[^a-zA-Z0-9_]/', '-', $stringwithoutspace);
?>
<?php if (!empty($username)) { ?>
<div class="leaderboard-info-block">
<span class="leaderboard-avatar"><?php echo $avatar = get_avatar($data->user_id, 49); ?></span> <span class="leaderboard-profile-link"><a href="<?php echo 'http://chillopedia.com/forums/users/' . $username; ?>"><?php echo $data->display_name; ?></a></span> <br>
<span class="leaderboard-user-points"><?php echo $mycred->format_creds($data->total); ?></span>
</div>
<?php
}
echo '</li><br>';
}
echo '</ul>';
echo $after_widget;
}
// Widget Settings (when editing / setting up widget)
public function form( $instance ) {
$title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : __( 'This Months Leaderboard', 'mycred' );
$number = isset( $instance['number'] ) ? abs( $instance['number'] ) : 5;
$show_visitors = isset( $instance['show_visitors'] ) ? 1 : 0; ?>
<p class="myCRED-widget-field">
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title', 'mycred' ); ?>:</label>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
</p>
<p class="myCRED-widget-field">
<label for="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>"><?php _e( 'Number of users', 'mycred' ); ?>:</label>
<input id="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'number' ) ); ?>" type="text" value="<?php echo $number; ?>" size="3" class="align-right" />
</p>
<p class="myCRED-widget-field">
<input type="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'show_visitors' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'show_visitors' ) ); ?>" value="1"<?php checked( $show_visitors, 1 ); ?> class="checkbox" />
<label for="<?php echo esc_attr( $this->get_field_id( 'show_visitors' ) ); ?>"><?php _e( 'Visible to non-members', 'mycred' ); ?></label>
</p>
<?php if ( isset( $this->id ) && $this->id !== false ) : ?>
<p class="myCRED-widget-field">
<input type="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'reset_leaderboard' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'reset_leaderboard' ) ); ?>" value="1" class="checkbox" />
<label for="<?php echo esc_attr( $this->get_field_id( 'reset_leaderboard' ) ); ?>"><?php _e( 'Reset Leaderboard', 'mycred' ); ?></label>
</p>
<?php
endif;
}
// Save Widget Settings
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['number'] = (int) $new_instance['number'];
$instance['title'] = trim( $new_instance['title'] );
$instance['show_visitors'] = $new_instance['show_visitors'];
if ( isset( $new_instance['reset_leaderboard'] ) && $this->id !== false ) {
delete_transient( 'mycred_tml_' . $this->id );
}
mycred_flush_widget_cache( 'mycred_widget_this_months_leaderboard' );
return $instance;
}
// Grabs the leaderboard
public function get_leaderboard( $number = 5, $widget_id = '' ) {
// Get transient
$data = get_transient( 'mycred_tml_' . $widget_id );
// If transient does not exist or a new number is set, do a new DB Query
if ( $data === false || $number != 5 ) {
// Load the wpdb class
global $wpdb;
$mycred = $wpdb->prefix . 'myCRED_log';
$SQL = "
SELECT m.user_id, u.display_name, sum( m.creds ) AS total
FROM {$mycred} m
LEFT JOIN {$wpdb->users} u
ON u.ID = m.user_id
WHERE ( m.time >= %d AND m.time <= %d AND m.creds > 0 )
GROUP BY m.user_id
ORDER BY total DESC LIMIT 0,%d;";
$now = date_i18n( 'U' );
$start_of_month = strtotime( "first day of this month" );
$data = $wpdb->get_results( $wpdb->prepare( $SQL, $start_of_month, $now, $number ) );
// Save results till end of month
$lifespan = (int) strtotime( "last day of this month" ) - time();
set_transient( 'mycred_tml_' . $widget_id, $data, $lifespan );
}
return $data;
}
}
}
endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment