Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Custom myCRED Widget: Ranged Leaderboard
- * Shows a leaderboard of users sorted by their point gains
- * between two timestamps.
- * @author Gabriel S Merovingi
- * @version 1.2
- */
- if ( function_exists( 'mycred_label' ) ) :
- /**
- * Step 1: Register Widget
- * @since 1.2
- * @version 1.0
- */
- add_action( 'widgets_init', 'mycred_register_ranged_leaderboard' );
- function mycred_register_ranged_leaderboard() {
- register_widget( 'myCRED_Widget_Ranged_Leaderboard' );
- }
- /**
- * Step 2: Construct Widget
- * @since 1.2
- * @version 1.0
- */
- if ( ! class_exists( 'myCRED_Widget_Ranged_Leaderboard' ) ) {
- class myCRED_Widget_Ranged_Leaderboard extends WP_Widget {
- // Constructor
- public function __construct() {
- // Basic details about our widget
- $widget_ops = array(
- 'classname' => 'widget-mycred-ranged-leaderboard',
- 'description' => __( 'Show a leaderboard for point gains between two timestamps.', 'mycred' )
- );
- $this->WP_Widget( 'mycred_widget_ranged_leaderboard', sprintf( __( '(%s) Ranged Leaderboard', 'mycred' ), mycred_label() ), $widget_ops );
- $this->alt_option_name = 'mycred_widget_ranged_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'], $instance['excludes'], $instance['from'], $instance['until'] );
- // Load myCRED
- $mycred = mycred();
- // Start constructing Widget
- echo $before_widget;
- // Title (if not empty)
- if ( ! empty( $instance['title'] ) )
- echo $before_title . $mycred->template_tags_general( $instance['title'] ) . $after_title;
- echo $this->styling();
- // Construct unorganized list for each row
- echo '<ul class="mycred-custom-leaderboard">';
- if ( ! empty( $leaderboard ) ) {
- foreach ( $leaderboard as $position => $data ) {
- $template = $instance['template'];
- $template = str_replace( '%avatar%', get_avatar( $data->user_id, $instance['avatar'] ), $template );
- $template = str_replace( '%name%', $data->display_name, $template );
- $template = str_replace( '%pos%', $position+1, $template );
- $template = str_replace( '%points%', $mycred->format_creds( $data->total ), $template );
- $template = $mycred->template_tags_user( $template, $data->user_id );
- echo '<li>' . $template . '</li>';
- }
- }
- else {
- echo '<li>No user has earned points yet.</li>';
- }
- echo '</ul>';
- echo $after_widget;
- }
- // Widget Settings (when editing / setting up widget)
- public function form( $instance ) {
- $title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : __( 'Leaderboard', 'mycred' );
- $number = isset( $instance['number'] ) ? abs( $instance['number'] ) : 5;
- $show_visitors = isset( $instance['show_visitors'] ) ? 1 : 0;
- $template = isset( $instance['template'] ) ? esc_attr( $instance['template'] ) : '<h3>%avatar% #%pos% %name%<span>With %points% points.</span></h3>';
- $avatar = isset( $instance['avatar'] ) ? abs( $instance['avatar'] ) : 45;
- $excludes = isset( $instance['excludes'] ) ? esc_attr( $instance['excludes'] ) : '';
- $from = isset( $instance['from'] ) ? esc_attr( $instance['from'] ) : '';
- $until = isset( $instance['until'] ) ? esc_attr( $instance['until'] ) : ''; ?>
- <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>
- <p class="myCRED-widget-field">
- <label for="<?php echo esc_attr( $this->get_field_id( 'template' ) ); ?>"><?php _e( 'Row Template', 'mycred' ); ?></label><br />
- <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 />
- <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>
- </p>
- <p class="myCRED-widget-field">
- <label for="<?php echo esc_attr( $this->get_field_id( 'avatar' ) ); ?>"><?php _e( 'Avatar Size', 'mycred' ); ?>:</label>
- <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" />
- </p>
- <p class="myCRED-widget-field">
- <label for="<?php echo esc_attr( $this->get_field_id( 'excludes' ) ); ?>"><?php _e( 'Exclude Users', 'mycred' ); ?>:</label>
- <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 />
- <span class="description"><?php _e( 'Comma seperated list of user IDs to exclude from the leaderboard.', 'mycred' ); ?></span>
- </p>
- <p class="myCRED-widget-field">
- <label for="<?php echo esc_attr( $this->get_field_id( 'from' ) ); ?>"><?php _e( 'From', 'mycred' ); ?>:</label>
- <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 />
- <span class="description"><?php _e( 'Date from which to start counting point gains.', 'mycred' ); ?></span>
- </p>
- <p class="myCRED-widget-field">
- <label for="<?php echo esc_attr( $this->get_field_id( 'until' ) ); ?>"><?php _e( 'Until', 'mycred' ); ?>:</label>
- <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 />
- <span class="description"><?php _e( 'Use "today" to count point gains up until today or a date.', 'mycred' ); ?></span>
- </p>
- <?php
- }
- // 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_twl_' . $this->id );
- $instance['template'] = trim( $new_instance['template'] );
- $instance['avatar'] = abs( $new_instance['avatar'] );
- $instance['excludes'] = trim( $new_instance['excludes'] );
- $instance['from'] = trim( $new_instance['from'] );
- $instance['until'] = trim( $new_instance['until'] );
- mycred_flush_widget_cache( 'mycred_widget_ranged_leaderboard' );
- return $instance;
- }
- public function styling() {
- return '
- <style type="text/css">
- .mycred-this-weeks-leaderboard { display: block; margin: 0; padding: 0; }
- .mycred-this-weeks-leaderboard li { display: block; min-height: 45px; float: none; clear: both; text-decoration: none; }
- .mycred-this-weeks-leaderboard li img { float: left; margin: 0 12px 0 0; }
- .widget .mycred-this-weeks-leaderboard li h3 { font-size: 18px; }
- .mycred-this-weeks-leaderboard li h3 span { display: block; line-height: 12px; font-size: 12px; }
- </style>';
- }
- // Grabs the leaderboard
- public function get_leaderboard( $number = 5, $excludes = array(), $from = '', $until = '' ) {
- // Load the wpdb class
- global $wpdb;
- $mycred = mycred();
- // If excluding users, sanitize and add to query
- $exclude = '';
- if ( ! empty( $excludes ) ) {
- $_ex = array();
- $excludes = explode( ',', $excludes );
- foreach ( $excludes as $ex )
- $_ex[] = trim( $ex );
- $exclude = 'AND u.ID NOT IN (' . implode( ',', $_ex ) . ')';
- }
- if ( $until == 'today' )
- $until = date_i18n( 'U' );
- else
- $until = strtotime( $until );
- return $wpdb->get_results( $wpdb->prepare( "
- SELECT m.user_id, u.display_name, sum( m.creds ) AS total
- FROM {$mycred->log_table} m
- LEFT JOIN {$wpdb->users} u
- ON ( u.ID = m.user_id )
- WHERE m.time BETWEEN %d AND %d
- {$exclude}
- GROUP BY m.user_id
- ORDER BY total DESC LIMIT 0,%d;", strtotime( $from ), $until, $number ) );
- }
- }
- }
- endif;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement