Advertisement
designbymerovingi

myCRED Custom Widget: Total Leaderboard

Jun 3rd, 2014
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Custom myCRED Widget: Total Leaderboard
  3.  * Shows a leaderboard of users sorted by their total earned points.
  4.  * Requires the myCRED Rank add-on to be enabled along with ranks being set
  5.  * to be based on a users total point balance and not their current balance!
  6.  * Note! This widget requires myCRED 1.4 or higher and is intended for the main point type.
  7.  * This example does not support myCRED Multisite features.
  8.  * @author Gabriel S Merovingi
  9.  * @version 1.0
  10.  */
  11. if ( function_exists( 'mycred_label' ) ) :
  12.  
  13.     /**
  14.      * Step 1: Register Widget
  15.      * @since 1.2
  16.      * @version 1.0
  17.      */
  18.     add_action( 'widgets_init', 'mycred_register_total_leaderboard' );
  19.     function mycred_register_total_leaderboard() {
  20.         register_widget( 'myCRED_Widget_Total_Leaderboard' );
  21.     }
  22.  
  23.     /**
  24.      * Step 2: Construct Widget
  25.      * @since 1.2
  26.      * @version 1.0
  27.      */
  28.     if ( ! class_exists( 'myCRED_Widget_Total_Leaderboard' ) ) {
  29.         class myCRED_Widget_Total_Leaderboard extends WP_Widget {
  30.  
  31.             // Constructor
  32.             public function __construct() {
  33.                 // Basic details about our widget
  34.                 $widget_ops = array(
  35.                     'classname'   => 'widget-mycred-total-leaderboard',
  36.                     'description' => __( 'Show a leaderboard based on total point gains.', 'mycred' )
  37.                 );
  38.                 $this->WP_Widget( 'mycred_widget_total_leaderboard', sprintf( __( '(%s) Total Leaderboard', 'mycred' ), mycred_label() ), $widget_ops );
  39.                 $this->alt_option_name = 'mycred_widget_total_leaderboard';
  40.             }
  41.  
  42.             // Widget Output (what users see)
  43.             public function widget( $args, $instance ) {
  44.                 extract( $args, EXTR_SKIP );
  45.  
  46.                 // Check if we want to show this to visitors
  47.                 if ( ! $instance['show_visitors'] && ! is_user_logged_in() ) return;
  48.            
  49.                 // Get the leaderboard
  50.                 $leaderboard = $this->get_leaderboard( $instance['number'], $instance['excludes'] );
  51.            
  52.                 // Load myCRED
  53.                 $mycred = mycred();
  54.  
  55.                 // Start constructing Widget
  56.                 echo $before_widget;
  57.            
  58.                 // Title (if not empty)
  59.                 if ( ! empty( $instance['title'] ) )
  60.                     echo $before_title . $mycred->template_tags_general( $instance['title'] ) . $after_title;
  61.  
  62.                 echo $this->styling();
  63.  
  64.                 // Construct unorganized list for each row
  65.                 echo '<ul class="mycred-custom-leaderboard">';
  66.                 if ( ! empty( $leaderboard ) ) {
  67.                     foreach ( $leaderboard as $position => $data ) {
  68.                         $template = $instance['template'];
  69.                
  70.                         $template = str_replace( '%avatar%', get_avatar( $data->user_id, $instance['avatar'] ), $template );
  71.                         $template = str_replace( '%name%',   $data->display_name, $template );
  72.                         $template = str_replace( '%pos%',    $position+1, $template );
  73.                         $template = str_replace( '%points%', $mycred->format_creds( $data->total ), $template );
  74.                
  75.                         echo '<li>' . $template . '</li>';
  76.                     }
  77.                 }
  78.                 else {
  79.                     echo '<li>No user has earned points yet.</li>';
  80.                 }
  81.                 echo '</ul>';
  82.  
  83.                 echo $after_widget;
  84.             }
  85.  
  86.             // Widget Settings (when editing / setting up widget)
  87.             public function form( $instance ) {
  88.                 $title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : __( 'Leaderboard', 'mycred' );
  89.                 $number = isset( $instance['number'] ) ? abs( $instance['number'] ) : 5;
  90.                 $show_visitors = isset( $instance['show_visitors'] ) ? 1 : 0;
  91.                 $template = isset( $instance['template'] ) ? esc_attr( $instance['template'] ) : '<h3>%avatar% #%pos%  %name%<span>With %points% points.</span></h3>';
  92.                 $avatar = isset( $instance['avatar'] ) ? abs( $instance['avatar'] ) : 45;
  93.                 $excludes = isset( $instance['excludes'] ) ? esc_attr( $instance['excludes'] ) : ''; ?>
  94.  
  95. <p class="myCRED-widget-field">
  96.     <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title', 'mycred' ); ?>:</label>
  97.     <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 ); ?>" />
  98. </p>
  99. <p class="myCRED-widget-field">
  100.     <label for="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>"><?php _e( 'Number of users', 'mycred' ); ?>:</label>
  101.     <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" />
  102. </p>
  103. <p class="myCRED-widget-field">
  104.     <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" />
  105.     <label for="<?php echo esc_attr( $this->get_field_id( 'show_visitors' ) ); ?>"><?php _e( 'Visible to non-members', 'mycred' ); ?></label>
  106. </p>
  107. <p class="myCRED-widget-field">
  108.     <label for="<?php echo esc_attr( $this->get_field_id( 'template' ) ); ?>"><?php _e( 'Row Template', 'mycred' ); ?></label><br />
  109.     <textarea id="<?php echo esc_attr( $this->get_field_id( 'teamplte' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'template' ) ); ?>" style="width:95%;" rows="5"><?php echo $template; ?></textarea><br />
  110.     <span class="description"><?php _e( 'Use %avatar% to the users avatar, %display_name% for the users display name, %pos% for the users position and %points% for their current points amount.', 'mycred' ); ?></span>
  111. </p>
  112. <p class="myCRED-widget-field">
  113.     <label for="<?php echo esc_attr( $this->get_field_id( 'avatar' ) ); ?>"><?php _e( 'Avatar Size', 'mycred' ); ?>:</label>
  114.     <input id="<?php echo esc_attr( $this->get_field_id( 'avatar' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'avatar' ) ); ?>" type="text" value="<?php echo $avatar; ?>" size="3" class="align-right" />
  115. </p>
  116. <p class="myCRED-widget-field">
  117.     <label for="<?php echo esc_attr( $this->get_field_id( 'excludes' ) ); ?>"><?php _e( 'Exclude Users', 'mycred' ); ?>:</label>
  118.     <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'excludes' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'excludes' ) ); ?>" type="text" value="<?php echo esc_attr( $excludes ); ?>" /><br />
  119.     <span class="description"><?php _e( 'Comma seperated list of user IDs to exclude from the leaderboard.', 'mycred' ); ?></span>
  120. </p>
  121. <?php
  122.             }
  123.  
  124.             // Save Widget Settings
  125.             public function update( $new_instance, $old_instance ) {
  126.                 $instance = $old_instance;
  127.                 $instance['number'] = (int) $new_instance['number'];
  128.                 $instance['title'] = trim( $new_instance['title'] );
  129.                 $instance['show_visitors'] = $new_instance['show_visitors'];
  130.            
  131.                 if ( isset( $new_instance['reset_leaderboard'] ) && $this->id !== false )
  132.                     delete_transient( 'mycred_twl_' . $this->id );
  133.  
  134.                 $instance['template'] = trim( $new_instance['template'] );
  135.                 $instance['avatar'] = abs( $new_instance['avatar'] );
  136.                 $instance['excludes'] = trim( $new_instance['excludes'] );
  137.  
  138.                 mycred_flush_widget_cache( 'mycred_widget_total_leaderboard' );
  139.                 return $instance;
  140.             }
  141.  
  142.             public function styling() {
  143.                 return '
  144. <style type="text/css">
  145. .mycred-this-weeks-leaderboard { display: block; margin: 0; padding: 0; }
  146. .mycred-this-weeks-leaderboard li { display: block; min-height: 45px; float: none; clear: both; text-decoration: none; }
  147. .mycred-this-weeks-leaderboard li img { float: left; margin: 0 12px 0 0; }
  148. .widget .mycred-this-weeks-leaderboard li h3 { font-size: 18px; }
  149. .mycred-this-weeks-leaderboard li h3 span { display: block; line-height: 12px; font-size: 12px; }
  150. </style>';
  151.             }
  152.  
  153.             // Grabs the leaderboard
  154.             public function get_leaderboard( $number = 5, $excludes = array() ) {
  155.                 // Load the wpdb class
  156.                 global $wpdb;
  157.                
  158.                 // If excluding users, sanitize and add to query
  159.                 $exclude = '';
  160.                 if ( ! empty( $excludes ) ) {
  161.                     $_ex = array();
  162.                     $excludes = explode( ',', $excludes );
  163.                     foreach ( $excludes as $ex )
  164.                         $_ex[] = trim( $ex );
  165.  
  166.                     $exclude = 'AND u.ID NOT IN (' . implode( ',', $_ex ) . ')';
  167.                 }
  168.  
  169.                 $balance_key = 'mycred_default';
  170.                 if ( is_multisite() && $GLOBALS['blog_id'] > 1 && ! mycred_centralize_log() )
  171.                     $balance_key = 'mycred_default_' . $GLOBALS['blog_id'] . '_total';
  172.  
  173.                 return $wpdb->get_results( $wpdb->prepare( "
  174.                     SELECT um.user_id, u.display_name, um.meta_value AS total
  175.                     FROM {$wpdb->usermeta} um
  176.                     LEFT JOIN {$wpdb->users} u
  177.                         ON ( u.ID = um.user_id )
  178.                     WHERE um.meta_key = %s  
  179.                     {$exclude}  
  180.                     GROUP BY um.user_id
  181.                     ORDER BY total DESC
  182.                     LIMIT 0,%d;", $balance_key, $number ) );
  183.             }
  184.         }
  185.     }
  186.  
  187. endif;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement