• mnicdpc

    (@mnicdpc)


    Hello, I’m modifying the This Months leaderboard widget but I’m trying to get besides the total of the month, i want to show in what he won those points. Lets say he have 7 in total that month, 5 in visit and 2 in loggin.
    The code i change isn’t showing the query or anything.
    The part I added were the function get_leaderboard_separated_points and modify the print of the list.
    I change a bit the code but now whenever I refresh a second time the page, the valor of total points says 0. What could it be? the transient number that was first created by one of the functions?

    Here’s the code:

    add_action( 'mycred_widgets_init', 'mycred_load_this_months_leaderboard_widget' );
    function mycred_load_this_months_leaderboard_widget() {
    
        if ( ! class_exists( 'myCRED_Widget_This_Months_Leaderboard' ) ) {
            class myCRED_Widget_This_Months_Leaderboard extends WP_Widget {
    
                // Constructor
                public function __construct() {
    
                    parent::__construct(
                        'mycred_widget_this_months_leaderboard',
                        sprintf( __( '(%s) This Months Leaderboard', 'mycred' ), mycred_label() ),
                        array(
                            'classname'   => 'widget-mycred-this-months-leaderboard',
                            'description' => __( 'Show the leaderboard for a given timeframe.', 'mycred' )
                        )
                    );
    
                }
    
                // 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 );
    
                    //Get the leaderboard points separated by type
                    $leaderboard_separated_points = $this->get_leaderboard_separated_points( $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-months-leaderboard">';
                    foreach ( $leaderboard as $position => $data ) {
                        $avatar = get_avatar( $data->user_id, 32 );
                        $username = get_user_by( 'id', $data->user_id );
                        echo '<li>' . $avatar . '<a href="https://localhost/tesis/miembros/' . $username->user_login . '"> ' . $data->display_name . '</a>' . ' con ' . $mycred->format_creds( $data->total );
                        
                        $new_position = $position;
                        while ( $leaderboard_separated_points[$new_position]->user_id == ($position + 1)) {
                            echo ' -> ' . $leaderboard_separated_points[$new_position]->ref . ' ' . $mycred->format_creds( $leaderboard_separated_points[$new_position]->totalpoint ) . ' ' ;
                            $new_position++;
                        }
                        echo '</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'] ) : __( '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']        = absint( $new_instance['number'] );
                    $instance['title']         = sanitize_text_field( $new_instance['title'] );
                    $instance['show_visitors'] = ( isset( $new_instance['show_visitors'] ) ) ? $new_instance['show_visitors'] : 0;
                
                    if ( isset( $new_instance['reset_leaderboard'] ) && $this->id !== false ) {
                        delete_transient( 'mycred_tml_' . $this->id );
                    }
    
                    return $instance;
    
                }
    
                // Grabs the leaderboard
                public function get_leaderboard( $number = 5, $widget_id = '' ) {
    
                    // Get transient
                    $leaderboard = get_transient( 'mycred_tml_' . $widget_id );
                
                    // If transient does not exist or a new number is set, do a new DB Query    
                    if ( $leaderboard === false || $number != 5 ) {
    
                        // Load the wpdb class
                        global $wpdb;
                    
                        $mycred         = mycred();
                        $now            = current_time( 'timestamp' );
                        $start_of_month = strtotime( "first day of this month" );
    
                        $leaderboard    = $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 >= %d AND m.time <= %d AND m.creds > 0 ) 
                            GROUP BY m.user_id 
                            ORDER BY total DESC LIMIT 0,%d;", $start_of_month, $now, $number ) );
    
                        // Save results till end of month
                        $lifespan = (int) strtotime( "last day of this month" ) - $now;
    
                        set_transient( 'mycred_tml_' . $widget_id, $leaderboard, $lifespan );
    
                    }
                
                    return $leaderboard;
    
                }
    
                // Grabs the leaderboard separated points of each user selected (in this case 5)
                public function get_leaderboard_separated_points( $number = 5, $widget_id = '' ) {
    
                    // Get transient
                    $leaderboard_separated_points = get_transient( 'mycred_tml_' . $widget_id );
                
                    // If transient does not exist or a new number is set, do a new DB Query    
                    //if ( $leaderboard_separated_points === false || $number != 5 ) {
    
                        // Load the wpdb class
                        global $wpdb;
                    
                        $mycred         = mycred();
                        $now            = current_time( 'timestamp' );
                        $start_of_month = strtotime( "first day of this month" );
    
                        $leaderboard_separated_points    = $wpdb->get_results( $wpdb->prepare( "
                            SELECT m.user_id, m.ref, SUM( m.creds ) AS totalpoint
                            FROM <code>wp_mycred_log</code> AS m 
                            WHERE ( m.time >= %d AND m.time <= %d AND m.creds > 0 ) 
                            GROUP BY m.ref
                            ORDER BY m.user_id DESC LIMIT 0,%d;", $start_of_month, $now, $number ) );
    
                        // Save results till end of month
                        $lifespan = (int) strtotime( "last day of this month" ) - $now;
    
                        set_transient( 'mycred_tml_' . $widget_id, $leaderboard_separated_points, $lifespan );
    
                    //}
                
                    return $leaderboard_separated_points;
    
                }
    
            }
    
        }
    
        register_widget( 'myCRED_Widget_This_Months_Leaderboard' );
    
    }
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘MONTHLY LEADERBOARD ADD TO WHERE IT WON IT SPECIFIC’ is closed to new replies.