Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Custom Shortcode
- add_shortcode( 'mycred_total_given', 'mycred_total_given_shortcode' );
- function mycred_total_given_shortcode( $atts ) {
- if ( ! function_exists( 'mycred' ) ) return;
- global $wpdb, $mycred;
- $totals = get_option( 'mycred_total_given', false );
- // Only query the database if there is no saved totals
- if ( $totals === false ) {
- $types = mycred_get_types();
- $totals = array();
- // Loop through each point type
- foreach ( $types as $type_id => $label ) {
- // Today
- $today = $wpdb->get_var( $wpdb->prepare( "
- SELECT SUM( creds )
- FROM {$mycred->log_table}
- WHERE ctype = %s
- AND time > %d;", $type_id, strtotime( "today midnight" ) ) );
- if ( $today === NULL )
- $today = 0;
- // This week
- $this_week = $wpdb->get_var( $wpdb->prepare( "
- SELECT SUM( creds )
- FROM {$mycred->log_table}
- WHERE ctype = %s
- AND time > %d;", $type_id, mktime( 0, 0, 0, date( 'n' ), date( 'j' ), date( 'Y' ) ) - ( ( date( 'N' )-1 ) *3600 *24 ) ) );
- if ( $this_week === NULL )
- $this_week = 0;
- // This month
- $this_month = $wpdb->get_var( $wpdb->prepare( "
- SELECT SUM( creds )
- FROM {$mycred->log_table}
- WHERE ctype = %s
- AND time > %d;", $type_id, mktime( 0, 0, 0, date( 'm' ), 1, date( 'Y' ) ) ) );
- if ( $this_month === NULL )
- $this_month = 0;
- // This year
- $this_year = $wpdb->get_var( $wpdb->prepare( "
- SELECT SUM( creds )
- FROM {$mycred->log_table}
- WHERE ctype = %s
- AND time > %d;", $type_id, mktime( 0, 0, 0, 1, 1, date( 'Y' ) ) ) );
- if ( $this_year === NULL )
- $this_year = 0;
- $totals[ $type_id ] = array(
- 'today' => $today,
- 'week' => $this_week,
- 'month' => $this_month,
- 'year' => $this_year
- );
- }
- update_option( 'mycred_total_given', $totals );
- }
- ob_start();
- foreach ( $totals as $type_id => $total ) {
- $mycred = mycred( $type_id );
- ?>
- <h3><?php echo $mycred->plural(); ?></h3>
- <ul>
- <li>Today: <?php echo $mycred->format_creds( $total['today'] ); ?></li>
- <li>This Week: <?php echo $mycred->format_creds( $total['week'] ); ?></li>
- <li>This Month: <?php echo $mycred->format_creds( $total['month'] ); ?></li>
- <li>This Year: <?php echo $mycred->format_creds( $total['year'] ); ?></li>
- </ul>
- <?php
- }
- $content = ob_get_contents();
- ob_end_clean();
- return $content;
- }
- // Force a new query
- add_action( 'mycred_update_user_balance', 'force_new_mycred_total_query' );
- function force_new_mycred_total_query() {
- delete_option( 'mycred_total_given' );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement