/**
  * Email Notice Check
  * @since 1.4.6
  * @version 1.1
  */
 public function get_events_from_instance($request, $mycred)
 {
     extract($request);
     $events = array('general|all');
     // Events based on amount being given or taken
     if ($amount < $mycred->zero()) {
         $events[] = 'general|negative';
     } else {
         $events[] = 'general|positive';
     }
     // Events based on this transaction leading to the users balance
     // reaching or surpassing zero
     $users_current_balance = $mycred->get_users_balance($user_id, $type);
     if ($users_current_balance - $amount < $mycred->zero()) {
         $events[] = 'general|minus';
     } elseif ($users_current_balance - $amount == $mycred->zero()) {
         $events[] = 'general|zero';
     }
     // Ranks Related
     if (function_exists('mycred_get_users_rank')) {
         $rank_id = mycred_find_users_rank($user_id, false, $type);
         if ($rank_id !== NULL && mycred_user_got_demoted($user_id, $rank_id)) {
             $events[] = 'ranks|negative';
         } elseif ($rank_id !== NULL && mycred_user_got_promoted($user_id, $rank_id)) {
             $events[] = 'ranks|positive';
         }
     }
     // Let others play
     return apply_filters('mycred_get_email_events', $events, $request, $mycred);
 }
Example #2
0
 /**
  * Balance Adjustment
  * Check if users rank should change.
  * @since 1.1
  * @version 1.3.1
  */
 public function update_balance($user_id, $current_balance, $amount, $type)
 {
     if ($type != 'mycred_default') {
         return;
     }
     if ($this->rank['base'] == 'total') {
         $total = mycred_get_users_total($user_id);
         $balance = $this->core->number($total + $amount);
         mycred_update_users_total($type, compact('user_id', 'amount'), $this->core);
     } else {
         $balance = $this->core->number($current_balance + $amount);
     }
     $balance_format = '%d';
     if (isset($this->core->format['decimals']) && $this->core->format['decimals'] > 0) {
         $balance_format = 'CAST( %f AS DECIMAL( 10, ' . $this->core->format['decimals'] . ' ) )';
     }
     global $wpdb;
     $rank_id = $wpdb->get_var($wpdb->prepare("\n\t\t\tSELECT rank.ID \n\t\t\tFROM {$wpdb->posts} rank \n\t\t\tINNER JOIN {$wpdb->postmeta} min \n\t\t\t\tON ( min.post_id = rank.ID AND min.meta_key = 'mycred_rank_min' )\n\t\t\tINNER JOIN {$wpdb->postmeta} max \n\t\t\t\tON ( max.post_id = rank.ID AND max.meta_key = 'mycred_rank_max' )\n\t\t\tWHERE rank.post_type = 'mycred_rank' \n\t\t\t\tAND rank.post_status = 'publish'\n\t\t\t\tAND {$balance_format} BETWEEN min.meta_value AND max.meta_value\n\t\t\tLIMIT 0,1;", $balance));
     if ($rank_id !== NULL) {
         if (mycred_user_got_demoted($user_id, $rank_id)) {
             do_action('mycred_user_got_demoted', $user_id, $rank_id);
         } elseif (mycred_user_got_promoted($user_id, $rank_id)) {
             do_action('mycred_user_got_promoted', $user_id, $rank_id);
         } else {
             do_action('mycred_find_users_rank', $user_id, $rank_id);
         }
         $rank_meta_key = 'mycred_rank';
         if (is_multisite() && !mycred_override_settings()) {
             $rank_meta_key .= $GLOBALS['blog_id'];
         }
         mycred_update_user_meta($user_id, 'mycred_rank', '', $rank_id);
     }
 }
 function mycred_find_users_rank($user_id = NULL, $save = false, $type = 'mycred_default')
 {
     global $wpdb;
     $mycred = mycred($type);
     // In case user id is not set
     if ($user_id === NULL) {
         $user_id = get_current_user_id();
     }
     // Get current balanace
     $current_balance = $wpdb->get_var($wpdb->prepare("\n\t\t\tSELECT meta_value \n\t\t\tFROM {$wpdb->usermeta} \n\t\t\tWHERE user_id = %d \n\t\t\tAND meta_key = %s;", $user_id, $type));
     if ($current_balance === NULL) {
         $current_balance = 0;
     }
     // If ranks are based on total we get the total balance which in turn
     // if not set will default to the users current balance.
     if (mycred_rank_based_on_total($type)) {
         $balance = mycred_query_users_total($user_id, $type);
         if ($balance == 0) {
             $balance = $current_balance;
         }
     } else {
         $balance = $current_balance;
     }
     // Prep format for the db query
     $balance_format = '%d';
     if (isset($mycred->format['decimals']) && $mycred->format['decimals'] > 0) {
         $balance_format = 'CAST( %f AS DECIMAL( 10, ' . $mycred->format['decimals'] . ' ) )';
     }
     // Get the appropriate post tables
     if (!mycred_override_settings()) {
         $posts = $wpdb->posts;
         $postmeta = $wpdb->postmeta;
     } else {
         $posts = $wpdb->base_prefix . 'posts';
         $postmeta = $wpdb->base_prefix . 'postmeta';
     }
     $type_filter = $wpdb->prepare("\n\t\t\tINNER JOIN {$postmeta} ctype \n\t\t\t\tON ( ranks.ID = ctype.post_id AND ctype.meta_key = %s AND ctype.meta_value = %s )", 'ctype', $type);
     // Get the rank based on balance
     $rank_id = $wpdb->get_var($wpdb->prepare("\n\t\t\tSELECT ranks.ID \n\t\t\tFROM {$posts} ranks \n\t\t\t{$type_filter}\n\t\t\tINNER JOIN {$postmeta} min \n\t\t\t\tON ( ranks.ID = min.post_id AND min.meta_key = 'mycred_rank_min' )\n\t\t\tINNER JOIN {$postmeta} max \n\t\t\t\tON ( ranks.ID = max.post_id AND max.meta_key = 'mycred_rank_max' )\n\t\t\tWHERE ranks.post_type = 'mycred_rank' \n\t\t\t\tAND ranks.post_status = 'publish'\n\t\t\t\tAND {$balance_format} BETWEEN min.meta_value AND max.meta_value\n\t\t\tLIMIT 0,1;", $balance));
     // Let others play
     if ($rank_id !== NULL) {
         if (mycred_user_got_demoted($user_id, $rank_id)) {
             do_action('mycred_user_got_demoted', $user_id, $rank_id);
         } elseif (mycred_user_got_promoted($user_id, $rank_id)) {
             do_action('mycred_user_got_promoted', $user_id, $rank_id);
         }
     }
     $end = '';
     if ($type != 'mycred_default') {
         $end = $type;
     }
     // Save if requested
     if ($save && $rank_id !== NULL) {
         mycred_update_user_meta($user_id, 'mycred_rank', $end, $rank_id);
     }
     return apply_filters('mycred_find_users_rank', $rank_id, $user_id, $save, $type);
 }
 function mycred_find_users_rank($user_id = NULL, $save = false, $amount = 0)
 {
     global $wpdb;
     $type = 'mycred_default';
     $mycred = mycred($type);
     // Check for exclusion
     if ($mycred->exclude_user($user_id)) {
         return false;
     }
     // In case user id is not set
     if ($user_id === NULL) {
         $user_id = get_current_user_id();
     }
     if (!isset($mycred->rank)) {
         return false;
     }
     // Get balance key
     $balance_key = $type;
     if ($mycred->is_multisite && $GLOBALS['blog_id'] > 1 && !$mycred->use_central_logging) {
         $balance_key .= '_' . $GLOBALS['blog_id'];
     }
     if (isset($mycred->rank['base']) && $mycred->rank['base'] == 'total') {
         $balance_key .= '_total';
     }
     // Get the balance as get_user_meta() will return the previous value if it was just changed.
     $balance = $wpdb->get_var($wpdb->prepare("\n\t\t\tSELECT meta_value \n\t\t\tFROM {$wpdb->usermeta} \n\t\t\tWHERE user_id = %d \n\t\t\t\tAND meta_key = %s;", $user_id, $balance_key));
     // User does not yet have a balance.
     if ($balance === NULL) {
         // If rank is based on total, we get their current balance
         if (isset($mycred->rank['base']) && $mycred->rank['base'] == 'total') {
             $balance = $mycred->get_users_balance($user_id, $type);
         } else {
             $balance = $mycred->zero();
         }
     }
     // The new balance before it is saved
     if ($amount < 0) {
         $balance = $mycred->number($balance - $amount);
     } else {
         $balance = $mycred->number($balance + $amount);
     }
     $balance_format = '%d';
     if (isset($mycred->format['decimals']) && $mycred->format['decimals'] > 0) {
         $balance_format = 'CAST( %f AS DECIMAL( 10, ' . $mycred->format['decimals'] . ' ) )';
     }
     if (!mycred_override_settings()) {
         $posts = $wpdb->posts;
         $postmeta = $wpdb->postmeta;
     } else {
         $posts = $wpdb->base_prefix . 'posts';
         $postmeta = $wpdb->base_prefix . 'postmeta';
     }
     $rank_id = $wpdb->get_var($wpdb->prepare("\n\t\t\tSELECT rank.ID \n\t\t\tFROM {$posts} rank \n\t\t\tINNER JOIN {$postmeta} min \n\t\t\t\tON ( min.post_id = rank.ID AND min.meta_key = 'mycred_rank_min' )\n\t\t\tINNER JOIN {$postmeta} max \n\t\t\t\tON ( max.post_id = rank.ID AND max.meta_key = 'mycred_rank_max' )\n\t\t\tWHERE rank.post_type = 'mycred_rank' \n\t\t\t\tAND rank.post_status = 'publish'\n\t\t\t\tAND {$balance_format} BETWEEN min.meta_value AND max.meta_value\n\t\t\tLIMIT 0,1;", $balance));
     // Let others play
     if ($rank_id !== NULL) {
         if (mycred_user_got_demoted($user_id, $rank_id)) {
             do_action('mycred_user_got_demoted', $user_id, $rank_id);
         } elseif (mycred_user_got_promoted($user_id, $rank_id)) {
             do_action('mycred_user_got_promoted', $user_id, $rank_id);
         } else {
             do_action('mycred_find_users_rank', $user_id, $rank_id);
         }
     }
     // Save if requested
     if ($save && $rank_id !== NULL) {
         mycred_update_user_meta($user_id, 'mycred_rank', '', $rank_id);
     }
     return apply_filters('mycred_find_users_rank', $rank_id, $user_id, $save, $amount);
 }