function mycred_update_users_transfer_history($user_id, $history, $type = 'mycred_default', $key = NULL) { if ($key === NULL) { $key = 'mycred_transactions'; } if ($type != 'mycred_default' && $type != '') { $key .= '_' . $type; } // Get current history $current = mycred_get_users_transfer_history($user_id, $type, $key); // Reset if ($history === true) { $new_history = array('frame' => '', 'amount' => 0); } else { $new_history = mycred_apply_defaults($current, $history); } mycred_update_user_meta($user_id, $key, '', $new_history); }
/** * AJAX Transfer Creds * @since 0.1 * @version 1.4 */ public function ajax_call_transfer() { // Security if (!check_ajax_referer('mycred-transfer-creds', 'token', false)) { die(json_encode('error_1')); } parse_str($_POST['form'], $post); unset($_POST); // Required if (!isset($post['mycred-transfer-to']) || !isset($post['mycred-transfer-amount'])) { die(json_encode($post)); } // Prep $to = $post['mycred-transfer-to']; if (!isset($post['mycred-sender'])) { $from = get_current_user_id(); } else { $from = absint($post['mycred-sender']); $from_user = get_userdata($from); if ($from_user === false) { die(-1); } } $ref = 'transfer'; if (isset($post['mycred-transfer-ref'])) { $ref = sanitize_key($post['mycred-transfer-ref']); } $amount = abs($post['mycred-transfer-amount']); // Type $mycred_types = mycred_get_types(); $type = ''; if (isset($post['mycred-transfer-type']) && array_key_exists($post['mycred-transfer-type'], $mycred_types)) { $type = sanitize_text_field($post['mycred-transfer-type']); } if ($type == '') { $type = 'mycred_default'; } $mycred = mycred($type); // Add-on has not been installed if (!isset($this->transfers)) { die(json_encode('error_6')); } $prefs = $this->transfers; if (!isset($prefs['limit']['limit']) || !isset($prefs['logs']['sending'])) { die(json_encode('error_6')); } // Get Recipient $recipient_id = $this->get_recipient($to); if ($recipient_id === false) { die(json_encode('error_3')); } if ($mycred->exclude_user($recipient_id)) { die(json_encode('error_4')); } // Prevent transfers to ourselves if ($recipient_id == $from) { die(json_encode('error_4')); } // Check amount $amount = $mycred->number($amount); if ($amount == $mycred->zero()) { die(json_encode('error_5')); } // Check if user can transfer $transfer = mycred_user_can_transfer($from, $amount, $type); // Insufficient funds if ($transfer == 'low') { die(json_encode('error_7')); } elseif ($transfer == 'limit') { die(json_encode('error_8')); } // All good $after_transfer = $transfer; // Generate Transaction ID for our records $transaction_id = 'TXID' . date_i18n('U') . $from; // Let others play before we execute the transfer do_action('mycred_transfer_ready', $transaction_id, $post, $prefs, $this->core, $type); $data = apply_filters('mycred_transfer_data', array('ref_type' => 'user', 'tid' => $transaction_id), $transaction_id, $post, $prefs, $type); // First take the amount from the sender $mycred->add_creds($ref, $from, 0 - $amount, $prefs['logs']['sending'], $recipient_id, $data, $type); // Update history if limits are imposed if ($prefs['limit']['limit'] != 'none') { $history = mycred_get_users_transfer_history($from, $type); mycred_update_users_transfer_history($from, array('amount' => $mycred->number($amount + $history['amount'])), $type); } // Then add the amount to the receipient $mycred->add_creds($ref, $recipient_id, $amount, $prefs['logs']['receiving'], $from, $data, $type); // Let others play once transaction is completed do_action('mycred_transfer_completed', $transaction_id, $post, $prefs, $this->core, $type); // Return the good news die(json_encode('ok')); }