Advertisement
designbymerovingi

Total Points Given Shortcode

Sep 24th, 2014
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.55 KB | None | 0 0
  1. // Custom Shortcode
  2. add_shortcode( 'mycred_total_given', 'mycred_total_given_shortcode' );
  3. function mycred_total_given_shortcode( $atts ) {
  4.  
  5.     if ( ! function_exists( 'mycred' ) ) return;
  6.  
  7.     global $wpdb, $mycred;
  8.  
  9.     $totals = get_option( 'mycred_total_given', false );
  10.  
  11.     // Only query the database if there is no saved totals
  12.     if ( $totals === false ) {
  13.  
  14.         $types = mycred_get_types();
  15.         $totals = array();
  16.  
  17.         // Loop through each point type
  18.         foreach ( $types as $type_id => $label ) {
  19.  
  20.             // Today
  21.             $today = $wpdb->get_var( $wpdb->prepare( "
  22.                 SELECT SUM( creds )
  23.                 FROM {$mycred->log_table}
  24.                 WHERE ctype = %s
  25.                 AND time > %d;", $type_id, strtotime( "today midnight" ) ) );
  26.  
  27.             if ( $today === NULL )
  28.                 $today = 0;
  29.  
  30.             // This week
  31.             $this_week = $wpdb->get_var( $wpdb->prepare( "
  32.                 SELECT SUM( creds )
  33.                 FROM {$mycred->log_table}
  34.                 WHERE ctype = %s
  35.                 AND time > %d;", $type_id, mktime( 0, 0, 0, date( 'n' ), date( 'j' ), date( 'Y' ) ) - ( ( date( 'N' )-1 ) *3600 *24 ) ) );
  36.  
  37.             if ( $this_week === NULL )
  38.                 $this_week = 0;
  39.  
  40.             // This month
  41.             $this_month = $wpdb->get_var( $wpdb->prepare( "
  42.                 SELECT SUM( creds )
  43.                 FROM {$mycred->log_table}
  44.                 WHERE ctype = %s
  45.                 AND time > %d;", $type_id, mktime( 0, 0, 0, date( 'm' ), 1, date( 'Y' ) ) ) );
  46.  
  47.             if ( $this_month === NULL )
  48.                 $this_month = 0;
  49.  
  50.             // This year
  51.             $this_year = $wpdb->get_var( $wpdb->prepare( "
  52.                 SELECT SUM( creds )
  53.                 FROM {$mycred->log_table}
  54.                 WHERE ctype = %s
  55.                 AND time > %d;", $type_id, mktime( 0, 0, 0, 1, 1, date( 'Y' ) ) ) );
  56.  
  57.             if ( $this_year === NULL )
  58.                 $this_year = 0;
  59.  
  60.             $totals[ $type_id ] = array(
  61.                 'today' => $today,
  62.                 'week'  => $this_week,
  63.                 'month' => $this_month,
  64.                 'year'  => $this_year
  65.             );
  66.  
  67.         }
  68.  
  69.         update_option( 'mycred_total_given', $totals );
  70.  
  71.     }
  72.  
  73.     ob_start();
  74.  
  75.     foreach ( $totals as $type_id => $total ) {
  76.         $mycred = mycred( $type_id );
  77. ?>
  78. <h3><?php echo $mycred->plural(); ?></h3>
  79. <ul>
  80. <li>Today: <?php echo $mycred->format_creds( $total['today'] ); ?></li>
  81. <li>This Week: <?php echo $mycred->format_creds( $total['week'] ); ?></li>
  82. <li>This Month: <?php echo $mycred->format_creds( $total['month'] ); ?></li>
  83. <li>This Year: <?php echo $mycred->format_creds( $total['year'] ); ?></li>
  84. </ul>
  85. <?php
  86.  
  87.     }
  88.  
  89.     $content = ob_get_contents();
  90.     ob_end_clean();
  91.     return $content;
  92.  
  93. }
  94.  
  95. // Force a new query
  96. add_action( 'mycred_update_user_balance', 'force_new_mycred_total_query' );
  97. function force_new_mycred_total_query() {
  98.     delete_option( 'mycred_total_given' );
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement