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);
 }
Beispiel #2
0
 /**
  * Update users balance
  * Returns the updated balance of the given user.
  *
  * @param $user_id (int), required user id
  * @param $amount (int|float), amount to add/deduct from users balance. This value must be pre-formated.
  * @returns the new balance.
  * @since 0.1
  * @version 1.4
  */
 public function update_users_balance($user_id = NULL, $amount = NULL, $type = 'mycred_default')
 {
     // Minimum Requirements: User id and amount can not be null
     if ($user_id === NULL || $amount === NULL) {
         return $amount;
     }
     if (empty($type)) {
         $type = $this->get_cred_id();
     }
     // Enforce max
     if ($this->max() > $this->zero() && $amount > $this->max()) {
         $amount = $this->number($this->max());
         do_action('mycred_max_enforced', $user_id, $_amount, $this->max());
     }
     // Adjust creds
     $current_balance = $this->get_users_balance($user_id, $type);
     $new_balance = $current_balance + $amount;
     // Update creds
     mycred_update_user_meta($user_id, $type, '', $new_balance);
     // Update total creds
     $total = mycred_query_users_total($user_id, $type);
     mycred_update_user_meta($user_id, $type, '_total', $total);
     // Let others play
     do_action('mycred_update_user_balance', $user_id, $current_balance, $amount, $type);
     // Return the new balance
     return $this->number($new_balance);
 }
Beispiel #3
0
 /**
  * Customize User Columns Content
  * @filter 'mycred_user_row_actions'
  * @since 0.1
  * @version 1.3.2
  */
 public function custom_user_column_content($value, $column_name, $user_id)
 {
     global $mycred_types;
     if (!array_key_exists($column_name, $mycred_types)) {
         return $value;
     }
     $mycred = mycred($column_name);
     // User is excluded
     if ($mycred->exclude_user($user_id) === true) {
         return __('Excluded', 'mycred');
     }
     $user = get_userdata($user_id);
     // Show balance
     $ubalance = $mycred->get_users_cred($user_id, $column_name);
     $balance = '<div id="mycred-user-' . $user_id . '-balance-' . $column_name . '">' . $mycred->before . ' <span>' . $mycred->format_number($ubalance) . '</span> ' . $mycred->after . '</div>';
     // Show total
     $total = mycred_query_users_total($user_id, $column_name);
     $balance .= '<small style="display:block;">' . sprintf('<strong>%s</strong>: %s', __('Total', 'mycred'), $mycred->format_number($total)) . '</small>';
     $balance = apply_filters('mycred_users_balance_column', $balance, $user_id, $column_name);
     $page = 'myCRED';
     if ($column_name != 'mycred_default') {
         $page .= '_' . $column_name;
     }
     // Row actions
     $row = array();
     $row['history'] = '<a href="' . esc_url(admin_url('admin.php?page=' . $page . '&user_id=' . $user_id)) . '">' . __('History', 'mycred') . '</a>';
     $row['adjust'] = '<a href="javascript:void(0)" class="mycred-open-points-editor" data-userid="' . $user_id . '" data-current="' . $ubalance . '" data-type="' . $column_name . '" data-username="******">' . __('Adjust', 'mycred') . '</a>';
     $rows = apply_filters('mycred_user_row_actions', $row, $user_id, $mycred);
     $balance .= $this->row_actions($rows);
     return $balance;
 }
Beispiel #4
0
 /**
  * AJAX: Calculate Totals
  * @since 1.2
  * @version 1.3.1
  */
 public function calculate_totals()
 {
     // Security
     check_ajax_referer('mycred-calc-totals', 'token');
     $type = 'mycred_default';
     if (isset($_POST['ctype']) && array_key_exists($_POST['ctype'], $this->point_types)) {
         $type = sanitize_text_field($_POST['ctype']);
     }
     $balance_key = $type;
     $mycred = mycred($type);
     if ($mycred->is_multisite && $GLOBALS['blog_id'] > 1 && !$mycred->use_central_logging) {
         $balance_key .= '_' . $GLOBALS['blog_id'];
     }
     global $wpdb;
     // Get all users that have a balance. Excluded users will have no balance
     $users = $wpdb->get_col($wpdb->prepare("\n\t\t\t\tSELECT DISTINCT user_id \n\t\t\t\tFROM {$wpdb->usermeta} \n\t\t\t\tWHERE meta_key = %s", $balance_key));
     $count = 0;
     if (!empty($users)) {
         // Get the total for each user with a balance
         foreach ($users as $user_id) {
             $total = mycred_query_users_total($user_id, $type);
             mycred_update_user_meta($user_id, $type, '_total', $total);
             $count++;
         }
     }
     die(json_encode(sprintf(__('Completed - Total of %d users effected', 'mycred'), $count)));
 }
 function mycred_render_shortcode_total($atts, $content = '')
 {
     extract(shortcode_atts(array('user_id' => '', 'types' => 'mycred_default', 'raw' => 0, 'total' => 0), $atts));
     // If user ID is not set, get the current users ID
     if (!is_user_logged_in() && $user_id == '') {
         return $content;
     }
     if ($user_id == '') {
         $user_id = get_current_user_id();
     }
     // Get types
     $types_to_addup = array();
     $all = false;
     $existing_types = mycred_get_types();
     if ($types == 'all') {
         $types_to_addup = array_keys($existing_types);
     } else {
         $types = explode(',', $types);
         if (!empty($types)) {
             foreach ($types as $type_key) {
                 $type_key = sanitize_text_field($type_key);
                 if (!array_key_exists($type_key, $existing_types)) {
                     continue;
                 }
                 if (!in_array($type_key, $types_to_addup)) {
                     $types_to_addup[] = $type_key;
                 }
             }
         }
     }
     // In case we still have no types, we add the default one
     if (empty($types_to_addup)) {
         $types_to_addup = array('mycred_default');
     }
     // Add up all point type balances
     $total_balance = 0;
     foreach ($types_to_addup as $type) {
         // Get the balance for this type
         $mycred = mycred($type);
         if ($total == 1) {
             $balance = mycred_query_users_total($user_id, $type);
         } else {
             $balance = $mycred->get_users_balance($user_id, $type);
         }
         $total_balance = $total_balance + $balance;
     }
     // If results should be formatted
     if ($raw == 0) {
         $mycred = mycred();
         $total_balance = $mycred->format_number($total_balance);
     }
     return apply_filters('mycred_total_balances_output', $total_balance, $atts);
 }
Beispiel #6
0
 function mycred_render_shortcode_total($atts, $content = '')
 {
     extract(shortcode_atts(array('user_id' => NULL, 'types' => 'mycred_default', 'raw' => 0), $atts));
     // If user ID is not set, get the current users ID
     if ($user_id === NULL) {
         // If user is not logged in bail now
         if (!is_user_logged_in()) {
             return $content;
         }
         $user_id = get_current_user_id();
     }
     // Get types
     $types_to_addup = array();
     $all = false;
     $existing_types = mycred_get_types();
     if ($types == 'all') {
         $types_to_addup = array_keys($existing_types);
     } else {
         $types = explode(',', $types);
         if (!empty($types)) {
             foreach ($types as $type_key) {
                 $type_key = sanitize_text_field($type_key);
                 if (!array_key_exists($type_key, $existing_types)) {
                     continue;
                 }
                 if (!in_array($type_key, $types_to_addup)) {
                     $types_to_addup[] = $type_key;
                 }
             }
         }
     }
     // In case we still have no types, we add the default one
     if (empty($types_to_addup)) {
         $types_to_addup = array('mycred_default');
     }
     // Add up all point type balances
     $total = 0;
     foreach ($types_to_addup as $type) {
         // Get the balance for this type
         $balance = mycred_query_users_total($user_id, $type);
         $total = $total + $balance;
     }
     // If we want the total unformatted return this now
     if ($raw) {
         return $total;
     }
     // Return formatted
     return apply_filters('mycred_total_balances_output', $total, $atts);
 }
 /**
  * Update users balance
  * Returns the updated balance of the given user.
  *
  * @param $user_id (int), required user id
  * @param $amount (int|float), amount to add/deduct from users balance. This value must be pre-formated.
  * @returns the new balance.
  * @since 0.1
  * @version 1.4.2
  */
 public function update_users_balance($user_id = NULL, $amount = NULL, $type = NULL)
 {
     // Minimum Requirements: User id and amount can not be null
     if ($user_id === NULL || $amount === NULL) {
         return $amount;
     }
     // Type
     $point_types = mycred_get_types();
     if ($type === NULL || !array_key_exists($type, $point_types)) {
         $type = $this->get_cred_id();
     }
     // Enforce max
     if ($this->max() > $this->zero() && $amount > $this->max()) {
         $_amount = $amount;
         $amount = $this->number($this->max());
         do_action('mycred_max_enforced', $user_id, $_amount, $this->max());
     }
     // Adjust creds
     $current_balance = $this->get_users_balance($user_id, $type);
     $new_balance = $current_balance + $amount;
     // Update creds
     mycred_update_user_meta($user_id, $type, '', $new_balance);
     // Update total creds
     $total = mycred_query_users_total($user_id, $type);
     mycred_update_user_meta($user_id, $type, '_total', $total);
     // Clear caches
     mycred_delete_option('mycred-cache-total-' . $type);
     // Let others play
     do_action('mycred_update_user_balance', $user_id, $current_balance, $amount, $type);
     // Return the new balance
     return $this->number($new_balance);
 }