function test_set_referral_status() { $this->assertEquals('pending', affwp_get_referral_status($this->_referral_id)); affwp_set_referral_status($this->_referral_id, 'unpaid'); $this->assertEquals('unpaid', affwp_get_referral_status($this->_referral_id)); affwp_set_referral_status($this->_referral_id, 'rejected'); $this->assertEquals('rejected', affwp_get_referral_status($this->_referral_id)); }
/** * Restore a rejected referral when untrashing a submission * * @access private * @since 1.6 */ public function restore_referral($sub_id = 0) { if (!affiliate_wp()->settings->get('revoke_on_refund')) { return; } if ('nf_sub' != get_post_type($sub_id)) { return; } $referral_id = affiliate_wp()->referrals->get_column_by('referral_id', 'reference', $sub_id); if ($referral_id) { affwp_set_referral_status($referral_id, 'unpaid'); } }
/** * Process the bulk actions * * @access public * @since 1.0 * @return void */ public function process_bulk_action() { if (empty($_REQUEST['_wpnonce'])) { return; } if (!wp_verify_nonce($_REQUEST['_wpnonce'], 'bulk-referrals') && !wp_verify_nonce($_REQUEST['_wpnonce'], 'referral-nonce')) { return; } $ids = isset($_GET['referral_id']) ? $_GET['referral_id'] : array(); if (!is_array($ids)) { $ids = array($ids); } $ids = array_map('absint', $ids); $action = !empty($_REQUEST['action']) ? $_REQUEST['action'] : false; if (empty($ids) || empty($action)) { return; } foreach ($ids as $id) { if ('delete' === $this->current_action()) { affwp_delete_referral($id); } if ('reject' === $this->current_action()) { affwp_set_referral_status($id, 'rejected'); } if ('accept' === $this->current_action()) { affwp_set_referral_status($id, 'unpaid'); } if ('mark_as_paid' === $this->current_action()) { affwp_set_referral_status($id, 'paid'); } if ('mark_as_unpaid' === $this->current_action()) { affwp_set_referral_status($id, 'unpaid'); } do_action('affwp_referrals_do_bulk_action_' . $this->current_action(), $id); } }
/** * Get the data being exported * * @access public * @since 1.0 * @return array $data Data for Export */ public function get_data() { $args = array('status' => 'unpaid', 'date' => !empty($this->date) ? $this->date : '', 'number' => -1); // Final data to be exported $data = array(); // The affiliates that have earnings to be paid $affiliates = array(); // The list of referrals that are possibly getting marked as paid $to_maybe_pay = array(); // Retrieve the referrals from the database $referrals = affiliate_wp()->referrals->get_referrals($args); // The minimum payout amount $minimum = !empty($_POST['minimum']) ? sanitize_text_field(affwp_sanitize_amount($_POST['minimum'])) : 0; if ($referrals) { foreach ($referrals as $referral) { if (in_array($referral->affiliate_id, $affiliates)) { // Add the amount to an affiliate that already has a referral in the export $amount = $data[$referral->affiliate_id]['amount'] + $referral->amount; $data[$referral->affiliate_id]['amount'] = $amount; } else { $email = affwp_get_affiliate_email($referral->affiliate_id); $data[$referral->affiliate_id] = array('email' => $email, 'amount' => $referral->amount, 'currency' => !empty($referral->currency) ? $referral->currency : affwp_get_currency()); $affiliates[] = $referral->affiliate_id; } // Add the referral to the list of referrals to maybe payout if (!array_key_exists($referral->affiliate_id, $to_maybe_pay)) { $to_maybe_pay[$referral->affiliate_id] = array(); } $to_maybe_pay[$referral->affiliate_id][] = $referral->referral_id; } // Now determine which affiliates are above the minimum payout amount if ($minimum > 0) { foreach ($data as $affiliate_id => $payout) { if ($payout['amount'] < $minimum) { unset($data[$affiliate_id]); unset($to_maybe_pay[$affiliate_id]); } } } // We now know which referrals should be marked as paid foreach ($to_maybe_pay as $referral_list) { foreach ($referral_list as $referral_id) { affwp_set_referral_status($referral_id, 'paid'); } } } $data = apply_filters('affwp_export_get_data', $data); $data = apply_filters('affwp_export_get_data_' . $this->export_type, $data); return $data; }
/** * Adds a new referral to the database * * @since 1.0 * @return bool */ function affwp_add_referral($data = array()) { if (empty($data['user_id']) && empty($data['affiliate_id'])) { return false; } if (empty($data['affiliate_id'])) { $user_id = absint($data['user_id']); $affiliate_id = affiliate_wp()->affiliates->get_column_by('affiliate_id', 'user_id', $user_id); if (!empty($affiliate_id)) { $data['affiliate_id'] = $affiliate_id; } else { return false; } } $args = array('affiliate_id' => absint($data['affiliate_id']), 'amount' => !empty($data['amount']) ? sanitize_text_field($data['amount']) : '', 'description' => !empty($data['description']) ? sanitize_text_field($data['description']) : '', 'reference' => !empty($data['reference']) ? sanitize_text_field($data['reference']) : '', 'context' => !empty($data['context']) ? sanitize_text_field($data['context']) : '', 'status' => 'pending'); $referral_id = affiliate_wp()->referrals->add($args); if ($referral_id) { $status = !empty($data['status']) ? sanitize_text_field($data['status']) : 'pending'; affwp_set_referral_status($referral_id, $status); return true; } return false; }
/** * Update a referral * * @access public * @since 1.5 */ public function update_referral($referral_id = 0, $data = array()) { if (empty($referral_id)) { return false; } $referral = $this->get($referral_id); if (!$referral) { return false; } if (isset($data['amount'])) { $data['amount'] = affwp_sanitize_amount($data['amount']); } if (!empty($data['products'])) { $data['products'] = maybe_serialize($data['products']); } $update = $this->update($referral_id, $data, '', 'referral'); if ($update) { if (!empty($data['status']) && $referral->status !== $data['status']) { affwp_set_referral_status($referral, $data['status']); } elseif ('paid' === $referral->status) { if ($referral->amount > $data['amount']) { $change = $referral->amount - $data['amount']; affwp_decrease_affiliate_earnings($referral->affiliate_id, $change); } elseif ($referral->amount < $data['amount']) { $change = $data['amount'] - $referral->amount; affwp_increase_affiliate_earnings($referral->affiliate_id, $change); } } return $update; } return false; }
/** * Record referral conversion via ajax * * This is called anytime a referred visitor lands on a success page, defined by the [affiliate_conversion_script] shortcode * * @since 1.0 */ public function track_conversion() { $affiliate_id = absint($_POST['affiliate']); if ($this->is_valid_affiliate($affiliate_id)) { $md5 = md5($_POST['amount'] . $_POST['description'] . $_POST['reference'] . $_POST['context'] . $_POST['status']); if ($md5 !== $_POST['md5']) { die('-3'); // The args were modified } if (affiliate_wp()->referrals->get_by('visit_id', $this->get_visit_id())) { die('-4'); // This visit has already generated a referral } $status = !empty($_POST['status']) ? $_POST['status'] : 'unpaid'; $amount = sanitize_text_field(urldecode($_POST['amount'])); if (0 == $amount && affiliate_wp()->settings->get('ignore_zero_referrals')) { die('-5'); // Ignore a zero amount referral } $amount = $amount > 0 ? affwp_calc_referral_amount($amount, $affiliate_id) : 0; // Store the visit in the DB $referral_id = affiliate_wp()->referrals->add(array('affiliate_id' => $affiliate_id, 'amount' => $amount, 'status' => 'pending', 'description' => sanitize_text_field($_POST['description']), 'context' => sanitize_text_field($_POST['context']), 'campaign' => sanitize_text_field($_POST['campaign']), 'reference' => sanitize_text_field($_POST['reference']), 'visit_id' => $this->get_visit_id())); affwp_set_referral_status($referral_id, $status); affiliate_wp()->visits->update($this->get_visit_id(), array('referral_id' => $referral_id), '', 'visit'); echo $referral_id; exit; } else { die('-2'); } }
/** * Completes a referal. Used when orders are marked as completed * * @access public * @since 1.0 * @param $reference The reference column for the referral to complete per the current context * @return bool */ public function complete_referral($reference = 0) { if (empty($reference)) { return false; } $referral = affiliate_wp()->referrals->get_by('reference', $reference, $this->context); if (empty($referral)) { return false; } if (is_object($referral) && $referral->status != 'pending') { // This referral has already been completed, rejected, or paid return false; } if (!apply_filters('affwp_auto_complete_referral', true)) { return false; } if (affwp_set_referral_status($referral->referral_id, 'unpaid')) { do_action('affwp_complete_referral', $referral->referral_id, $referral, $reference); return true; } return false; }