Advertisement
designbymerovingi

myCRED Daily limit for mycred_give

Dec 19th, 2013
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.92 KB | None | 0 0
  1. /**
  2.  * Overwrite the mycred_give shortcode
  3.  * Runs after myCRED has registered the shortcode in order for us
  4.  * to remove it and replace it with our own.
  5.  */
  6. add_action( 'mycred_init', 'custom_mycred_start' );
  7. function custom_mycred_start() {
  8.     remove_shortcode( 'mycred_give' );
  9.     add_shortcode( 'mycred_give', 'mycred_custom_render_shortcode_give' );
  10. }
  11.  
  12. if ( ! function_exists( 'mycred_custom_render_shortcode_give' ) ) :
  13.     function mycred_custom_render_shortcode_give( $atts, $content )
  14.     {
  15.         if ( ! is_user_logged_in() ) return;
  16.  
  17.         extract( shortcode_atts( array(
  18.             'amount'  => NULL,
  19.             'user_id' => '',
  20.             'log'     => '',
  21.             'ref'     => 'gift',
  22.             'limit'   => 0,
  23.             'type'    => 'mycred_default'
  24.         ), $atts ) );
  25.        
  26.         if ( $amount === NULL )
  27.             return '<strong>' . __( 'error', 'mycred' ) . '</strong> ' . __( 'Amount missing!', 'mycred' );
  28.  
  29.         if ( empty( $log ) )
  30.             return '<strong>' . __( 'error', 'mycred' ) . '</strong> ' . __( 'Log Template Missing!', 'mycred' );
  31.        
  32.         $mycred = mycred_get_settings();
  33.        
  34.         if ( empty( $user_id ) )
  35.             $user_id = get_current_user_id();
  36.        
  37.         // Check for exclusion
  38.         if ( $mycred->exclude_user( $user_id ) ) return;
  39.  
  40.         // Prep amount
  41.         $amount = $mycred->number( $amount );
  42.  
  43.         // Daily limit handling
  44.         if ( $limit == 'daily' ) {
  45.             // Todays date as a numeric value
  46.             $today = (int) date_i18n( 'Ymd' );
  47.  
  48.             // Check if user has received points today
  49.             if ( $mycred->has_entry( $ref, $today, $user_id ) ) return;
  50.  
  51.             // Save today as a reference id to check for
  52.             $mycred->add_creds(
  53.                 $ref,
  54.                 $user_id,
  55.                 $amount,
  56.                 $log,
  57.                 $today,
  58.                 '',
  59.                 $type
  60.             );
  61.         }
  62.         // Default limit handling
  63.         else {
  64.             $limit = abs( $limit );
  65.             if ( $limit != 0 && mycred_count_ref_instances( $ref, $user_id ) >= $limit ) return;
  66.  
  67.             $mycred->add_creds(
  68.                 $ref,
  69.                 $user_id,
  70.                 $amount,
  71.                 $log,
  72.                 '',
  73.                 '',
  74.                 $type
  75.             );
  76.         }
  77.     }
  78. endif;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement