WordPress.org

Support

Support » Plugins and Hacks » [Resolved] Shortcode for Current user Score

[Resolved] Shortcode for Current user Score

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author AntoineH

    @antoineh

    No, there is not a shortcode for this. But I put something together that may be useful for you. If you are familiar with PHP you can add the following to your plugin code.

    Add the following function to class Football_Pool_Pool in file classes\class-football-pool-pool.php:

    public function get_user_score( $user, $ranking_id = FOOTBALLPOOL_RANKING_DEFAULT, $score_date = '' ) {
    	global $wpdb;
    	$prefix = FOOTBALLPOOL_DB_PREFIX;
    
    	$sql = $wpdb->prepare(
    					sprintf( "SELECT totalScore FROM {$prefix}scorehistory
    							WHERE userId = %%d AND ranking_id = %%d
    							AND ( %s scoreDate <= %%s )
    							ORDER BY scoreDate DESC LIMIT 1"
    							, ( $score_date == '' ? '1 = 1 OR' : '' )
    					) , $user, $ranking_id, $score_date );
    	return $wpdb->get_var( $sql ); // return null if nothing found
    }

    Register the shortcode in file classes\class-football-pool-shortcodes.php (somewhere at the top of the file):

    add_shortcode( 'fp-user-score', array( 'Football_Pool_Shortcodes', 'shortcode_user_score' ) );

    Add the handling method for the shortcode to the class Football_Pool_Shortcodes in the same file:

    //[fp-user-score]
    //  Displays the score for a given user in the given ranking.
    //
    //    user    : user Id, defaults to the logged in user
    //    ranking : ranking Id, defaults to the default ranking
    //    date    : show score up until this date,
    //              possible values 'now', 'postdate', a datetime value formatted like this 'Y-m-d H:i',
    //              defaults to 'now'
    //    text    : text to display if no user or no score is found, defaults to "0"
    public function shortcode_user_score( $atts ) {
    	extract( shortcode_atts( array(
    				'user' => '',
    				'ranking' => FOOTBALLPOOL_RANKING_DEFAULT,
    				'date' => 'now',
    				'text' => '0',
    			), $atts ) );
    
    	$output = $text;
    
    	if ( $user == '' || ! is_numeric( $user ) ) {
    		$user = get_current_user_id();
    	}
    
    	if ( ( int ) $user > 0 ) {
    		if ( $date == 'postdate' ) {
    			$score_date = get_the_date( 'Y-m-d H:i' );
    		} elseif ( ( $score_date = date_create( $date ) ) !== false ) {
    			$score_date = $score_date->format( 'Y-m-d H:i' );
    		} else {
    			$score_date = ''; // default aka now
    		}
    
    		$pool = new Football_Pool_Pool;
    		$score = $pool->get_user_score( $user, $ranking, $score_date );
    		if ( $score != null ) $output = $score;
    	}
    
    	return $output;
    }

    I will release this code (after some more testing) in my next version.

    p.s. This code was made in my development branch, so it is possible that it may not work in your code because of some missing functionality. I did not test it in an v2.2.x version of the plugin.

    Thanks AntoineH,

    It works like a charm!!!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘[Resolved] Shortcode for Current user Score’ is closed to new replies.