/** * AJAX Transfer Creds * @since 0.1 * @version 1.5 */ 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 $type = ''; if (isset($post['mycred-transfer-type']) && array_key_exists($post['mycred-transfer-type'], $this->point_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, $ref); // Insufficient funds if ($transfer === 'low') { die(json_encode('error_7')); } elseif ($transfer === 'limit') { die(json_encode('error_8')); } // 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, $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); // 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, $type); // Return the good news die(json_encode('ok')); }
function mycred_transfer_render($atts, $content = NULL) { global $mycred_load_transfer; // Settings $mycred = mycred(); $pref = $mycred->transfers; // Get Attributes extract(shortcode_atts(array('button' => '', 'charge_from' => '', 'pay_to' => '', 'show_balance' => 0, 'show_limit' => 0, 'ref' => '', 'placeholder' => '', 'types' => $pref['types'], 'excluded' => ''), $atts)); $output = ''; $mycred_load_transfer = false; // If we are not logged in if (!is_user_logged_in()) { if (isset($pref['templates']['login']) && !empty($pref['templates']['login'])) { $output .= '<p class="mycred-transfer-login">' . $mycred->template_tags_general($pref['templates']['login']) . '</p>'; } return $output; } if ($ref == '') { $ref = 'transfer'; } // Who to charge $charge_other = false; if ($charge_from == '') { $charge_other = true; $charge_from = get_current_user_id(); } // Point Types if (!is_array($types)) { $raw = explode(',', $types); } else { $raw = $types; } $clean = array(); foreach ($raw as $id) { $clean[] = sanitize_text_field($id); } $available_types = array(); // Default if (count($clean) == 1 && in_array('mycred_default', $clean)) { // Make sure user is not excluded if ($mycred->exclude_user($charge_from)) { return ''; } $status = mycred_user_can_transfer($charge_from, NULL, 'mycred_default', $ref); $my_balance = $mycred->get_users_cred($charge_from); // Error. Not enough creds if ($status === 'low') { if (isset($pref['errors']['low']) && !empty($pref['errors']['low'])) { $no_cred = str_replace('%limit%', $pref['limit']['limit'], $pref['errors']['low']); $no_cred = str_replace('%Limit%', ucwords($pref['limit']['limit']), $no_cred); $no_cred = str_replace('%left%', $mycred->format_creds($status), $no_cred); $output .= '<p class="mycred-transfer-low">' . $mycred->template_tags_general($no_cred) . '</p>'; } return $output; } // Error. Over limit if ($status === 'limit') { if (isset($pref['errors']['over']) && !empty($pref['errors']['over'])) { $no_cred = str_replace('%limit%', $pref['limit']['limit'], $pref['errors']['over']); $no_cred = str_replace('%Limit%', ucwords($pref['limit']['limit']), $no_cred); $no_cred = str_replace('%left%', $mycred->format_creds($status), $no_cred); $output .= '<p class="mycred-transfer-over">' . $mycred->template_tags_general($no_cred) . '</p>'; } return $output; } $available_types['mycred_default'] = $mycred->plural(); } else { foreach ($clean as $point_type) { $points = mycred($point_type); if ($points->exclude_user($charge_from)) { continue; } $status = mycred_user_can_transfer($charge_from, NULL, $point_type, $ref); if ($status === 'low' || $status === 'limit') { continue; } $available_types[$point_type] = $points->plural(); } // User does not have access if (count($available_types) == 0) { return $excluded; } } // Flag for scripts & styles $mycred_load_transfer = true; // Placeholder if ($pref['autofill'] == 'user_login') { $pln = __('username', 'mycred'); } elseif ($pref['autofill'] == 'user_email') { $pln = __('email', 'mycred'); } $placeholder = apply_filters('mycred_transfer_to_placeholder', __('recipients %s', 'mycred'), $pref, $mycred); $placeholder = sprintf($placeholder, $pln); // Recipient Input field $to_input = '<input type="text" name="mycred-transfer-to" value="" aria-required="true" class="mycred-autofill" placeholder="' . $placeholder . '" />'; // If recipient is set, pre-populate it with the recipients details if ($pay_to != '') { $user = get_user_by('id', $pay_to); if ($user !== false) { $value = $user->display_name; if (isset($user->{$pref}['autofill'])) { $value = $user->{$pref}['autofill']; } $to_input = '<input type="text" name="mycred-transfer-to" value="' . $value . '" readonly="readonly" />'; } } // If we only use one type, we might as well reload the myCRED_Settings object // since formating might differ if (count($clean) == 1) { $mycred = mycred($clean[0]); } // Only use prefix / suffix if we have 1 type. if (count($clean) == 1) { if (!empty($mycred->before)) { $before = $mycred->before . ' '; } else { $before = ''; } if (!empty($mycred->after)) { $after = ' ' . $mycred->after; } else { $after = ''; } } else { $before = $after = ''; } // Select Point type if (count($available_types) == 1) { $type_input = '<input type="hidden" name="mycred-transfer-type" value="' . $clean[0] . '" />'; } else { $type_input = '<select name="mycred-transfer-type" id="mycred-transfer-type" class="form-control">'; foreach ($available_types as $type => $plural) { $type_input .= '<option value="' . $type . '">' . $plural . '</option>'; } $type_input .= '</select>'; } $extras = array(); // Show Balance if ((bool) $show_balance === true && !empty($pref['templates']['balance']) && count($available_types) == 1) { $balance_text = str_replace('%balance%', $mycred->format_creds($my_balance), $pref['templates']['balance']); $extras[] = $mycred->template_tags_general($balance_text); } // Show Limits if ((bool) $show_limit === true && !empty($pref['templates']['limit']) && $pref['limit']['limit'] != 'none' && count($available_types) == 1) { $limit_text = str_replace('%_limit%', $pref['limit']['limit'], $pref['templates']['limit']); $limit_text = str_replace('%limit%', ucwords($pref['limit']['limit']), $limit_text); $limit_text = str_replace('%left%', $mycred->format_creds($status), $limit_text); $extras[] = $mycred->template_tags_general($limit_text); } if ($button == '') { $button = $pref['templates']['button']; } // Main output ob_start(); ?> <div class="mycred-transfer-cred-wrapper"<?php if ($ref != '') { echo ' id="transfer-form-' . $ref . '"'; } ?> > <form class="mycred-transfer" method="post" action=""> <?php do_action('mycred_transfer_form_start', $atts, $pref); ?> <ol style="list-style-type:none;"> <li class="mycred-send-to"> <label><?php _e('To:', 'mycred'); ?> </label> <div class="transfer-to"><?php echo $to_input; ?> </div> <?php do_action('mycred_transfer_form_to', $atts, $pref); ?> </li> <li class="mycred-send-amount"> <label><?php _e('Amount:', 'mycred'); ?> </label> <div class="transfer-amount"><?php echo $before; ?> <input type="text" class="short" name="mycred-transfer-amount" value="<?php echo $mycred->zero(); ?> " size="8" aria-required="true" /><?php echo $after . ' ' . $type_input; ?> </div> <?php if ($charge_other) { ?> <input type="hidden" name="mycred-charge-other" value="<?php absint($charge_from); ?> " /><?php } ?> <?php if ($ref != '') { ?> <input type="hidden" name="mycred-transfer-ref" value="<?php echo esc_attr($ref); ?> " /><?php } ?> <input type="submit" class="button button-primary button-large mycred-click btn btn-primary btn-lg"<?php if ($pay_to == get_current_user_id()) { echo ' disabled="disabled"'; } ?> value="<?php echo esc_attr($button); ?> " /> <?php do_action('mycred_transfer_form_amount', $atts, $pref); ?> </li> <?php if (!empty($extras)) { ?> <li class="mycred-transfer-info"> <p><?php echo implode('</p><p>', $extras); ?> </p> <?php do_action('mycred_transfer_form_extra', $atts, $pref); ?> </li> <?php } ?> </ol> <?php do_action('mycred_transfer_form_end', $atts, $pref); ?> <div class="clear clearfix"></div> </form> <div class="clear clearfix clr"></div> </div> <?php $output = ob_get_contents(); ob_end_clean(); return do_shortcode(apply_filters('mycred_transfer_render', $output, $atts, $mycred)); }