Advertisement
designbymerovingi

Get Todays Creds

Jun 24th, 2013
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.17 KB | None | 0 0
  1. function mycred_todays_entries( $ref = '', $user_id = NULL, $format = true )
  2. {
  3.     // Reference is required
  4.     if ( empty( $ref ) ) return false;
  5.    
  6.     global $wpdb;
  7.    
  8.     // The myCRED Log
  9.     $db = $wpdb->prefix . 'myCRED_log';
  10.    
  11.     // Get myCRED
  12.     $mycred = mycred_get_settings();
  13.    
  14.     // Get Now
  15.     $now = date_i18n( 'U' );
  16.    
  17.     // Get start of today
  18.     $today = date_i18n( 'Y/m/d 00:00:00' );
  19.     $today = strtotime( $today );
  20.    
  21.     // The DB Query
  22.     if ( $user_id !== NULL ) {
  23.         // Query todays entries for a specific user
  24.         $sql = "SELECT creds FROM {$db} WHERE ref = %s AND user_id = %d AND time >= %d AND time <= %d ORDER BY time ";
  25.         $query = $wpdb->get_results( $wpdb->prepare( $sql, $ref, $user_id, $today, $now ) );
  26.     }
  27.     else {
  28.         // Query todays entries for everyone
  29.         $sql = "SELECT creds FROM {$db} WHERE ref = %s AND time >= %d AND time <= %d ORDER BY time ";
  30.         $query = $wpdb->get_results( $wpdb->prepare( $sql, $ref, $today, $now ) );
  31.     }
  32.    
  33.     $count = 0;
  34.     // if we have results we add creds up
  35.     if ( !empty( $query ) ) {
  36.         foreach ( $query as $entry ) {
  37.             $count = $count+$entry->creds;
  38.         }
  39.     }
  40.    
  41.     if ( $format )
  42.         return $mycred->number( $count );
  43.     else
  44.         return $count;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement