SHOW:
|
|
- or go back to the newest paste.
| 1 | /** | |
| 2 | - | * Custom myCRED Widget: Ranged Leaderboard |
| 2 | + | * Custom myCRED Widget: Total Leaderboard |
| 3 | - | * Shows a leaderboard of users sorted by their point gains |
| 3 | + | * Shows a leaderboard of users sorted by their total earned points. |
| 4 | - | * between two timestamps. |
| 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 | - | * @version 1.2 |
| 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 | - | add_action( 'widgets_init', 'mycred_register_ranged_leaderboard' ); |
| 15 | + | |
| 16 | - | function mycred_register_ranged_leaderboard() {
|
| 16 | + | |
| 17 | - | register_widget( 'myCRED_Widget_Ranged_Leaderboard' ); |
| 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 | - | if ( ! class_exists( 'myCRED_Widget_Ranged_Leaderboard' ) ) {
|
| 25 | + | |
| 26 | - | class myCRED_Widget_Ranged_Leaderboard extends WP_Widget {
|
| 26 | + | |
| 27 | */ | |
| 28 | if ( ! class_exists( 'myCRED_Widget_Total_Leaderboard' ) ) {
| |
| 29 | class myCRED_Widget_Total_Leaderboard extends WP_Widget {
| |
| 30 | ||
| 31 | // Constructor | |
| 32 | - | 'classname' => 'widget-mycred-ranged-leaderboard', |
| 32 | + | |
| 33 | - | 'description' => __( 'Show a leaderboard for point gains between two timestamps.', 'mycred' ) |
| 33 | + | |
| 34 | $widget_ops = array( | |
| 35 | - | $this->WP_Widget( 'mycred_widget_ranged_leaderboard', sprintf( __( '(%s) Ranged Leaderboard', 'mycred' ), mycred_label() ), $widget_ops ); |
| 35 | + | 'classname' => 'widget-mycred-total-leaderboard', |
| 36 | - | $this->alt_option_name = 'mycred_widget_ranged_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 | - | $leaderboard = $this->get_leaderboard( $instance['number'], $instance['excludes'], $instance['from'], $instance['until'] ); |
| 47 | + | |
| 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 = $mycred->template_tags_user( $template, $data->user_id ); |
| 71 | + | |
| 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 | - | $excludes = isset( $instance['excludes'] ) ? esc_attr( $instance['excludes'] ) : ''; |
| 91 | + | |
| 92 | $avatar = isset( $instance['avatar'] ) ? abs( $instance['avatar'] ) : 45; | |
| 93 | - | $from = isset( $instance['from'] ) ? esc_attr( $instance['from'] ) : ''; |
| 93 | + | $excludes = isset( $instance['excludes'] ) ? esc_attr( $instance['excludes'] ) : ''; ?> |
| 94 | - | $until = isset( $instance['until'] ) ? esc_attr( $instance['until'] ) : ''; ?> |
| 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 | - | <label for="<?php echo esc_attr( $this->get_field_id( 'from' ) ); ?>"><?php _e( 'From', 'mycred' ); ?>:</label> |
| 123 | + | |
| 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 /> |
| 124 | + | |
| 125 | - | <span class="description"><?php _e( 'Date from which to start counting point gains.', 'mycred' ); ?></span> |
| 125 | + | |
| 126 | $instance = $old_instance; | |
| 127 | $instance['number'] = (int) $new_instance['number']; | |
| 128 | - | <label for="<?php echo esc_attr( $this->get_field_id( 'until' ) ); ?>"><?php _e( 'Until', 'mycred' ); ?>:</label> |
| 128 | + | |
| 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 /> |
| 129 | + | |
| 130 | - | <span class="description"><?php _e( 'Use "today" to count point gains up until today or a date.', 'mycred' ); ?></span> |
| 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 | - | $instance['from'] = trim( $new_instance['from'] ); |
| 149 | + | |
| 150 | - | $instance['until'] = trim( $new_instance['until'] ); |
| 150 | + | |
| 151 | } | |
| 152 | - | mycred_flush_widget_cache( 'mycred_widget_ranged_leaderboard' ); |
| 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 | - | public function get_leaderboard( $number = 5, $excludes = array(), $from = '', $until = '' ) {
|
| 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 | - | |
| 173 | + | |
| 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 | - | if ( $until == 'today' ) |
| 185 | + | |
| 186 | - | $until = date_i18n( 'U' ); |
| 186 | + | |
| 187 | - | else |
| 187 | + |