function mycred_render_shortcode_total_points($atts) { extract(shortcode_atts(array('type' => 'mycred_default', 'ref' => '', 'ref_id' => '', 'user_id' => 'current', 'formatted' => 1), $atts)); if (!mycred_point_type_exists($type)) { $type = 'mycred_default'; } if ($user_id == 'current') { if (!is_user_logged_in()) { return; } $user_id = get_current_user_id(); } $mycred = mycred($type); global $wpdb; // Simple if ($ref == '' && $ref_id == '' && $user_id == '') { // Add up all balances $total = $wpdb->get_var($wpdb->prepare("SELECT SUM( meta_value ) FROM {$wpdb->usermeta} WHERE meta_key = %s", mycred_get_meta_key($type))); } else { $wheres = array(); $wheres[] = $wpdb->prepare("ctype = %s", mycred_get_meta_key($type)); $ref = sanitize_key($ref); if (strlen($ref) > 0) { // Either we have just one reference $multiple = explode(',', $ref); if (count($multiple) == 1) { $wheres[] = $wpdb->prepare("ref = %s", $ref); } else { $_clean = array(); foreach ($multiple as $ref) { $ref = sanitize_key($ref); if (strlen($ref) > 0) { $_clean[] = $ref; } } if (!empty($_clean)) { $wheres[] = "ref IN ( '" . implode("', '", $_clean) . "' )"; } } } $ref_id = absint($ref_id); if ($ref_id > 0) { $wheres[] = $wpdb->prepare("ref_id = %d", $ref_id); } $user_id = absint($user_id); if ($user_id > 0) { $wheres[] = $wpdb->prepare("user_id = %d", $user_id); } $wheres = implode(" AND ", $wheres); $total = $wpdb->get_var("SELECT SUM( creds ) FROM {$mycred->log_table} WHERE {$wheres};"); } if ($total === NULL) { $total = 0; } if ($formatted == 1) { return $mycred->format_creds($total); } return $total; }
function mycred_update_users_total($type = 'mycred_default', $request = NULL, $mycred = NULL) { if ($request === NULL || !is_object($mycred) || !isset($request['user_id']) || !isset($request['amount'])) { return false; } if (!mycred_point_type_exists($type)) { $type = $mycred->get_cred_id(); } $amount = $mycred->number($request['amount']); $user_id = absint($request['user_id']); $users_total = mycred_get_user_meta($user_id, $type, '_total', true); if ($users_total == '') { $users_total = mycred_query_users_total($user_id, $type); } $new_total = $mycred->number($users_total + $amount); mycred_update_user_meta($user_id, $type, '_total', $new_total); return apply_filters('mycred_update_users_total', $new_total, $type, $request, $mycred); }