Advertisement
designbymerovingi

myCRED Custom Widget: Ranged Leaderboard

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