function affwp_set_referral_status($referral, $new_status = '') { if (is_object($referral) && isset($referral->referral_id)) { $referral_id = $referral->referral_id; } elseif (is_numeric($referral)) { $referral_id = absint($referral); $referral = affwp_get_referral($referral_id); } else { return false; } $old_status = $referral->status; if ($old_status == $new_status) { return false; } if (affiliate_wp()->referrals->update($referral_id, array('status' => $new_status), '', 'referral')) { if ('paid' == $new_status) { affwp_increase_affiliate_earnings($referral->affiliate_id, $referral->amount); affwp_increase_affiliate_referral_count($referral->affiliate_id); } elseif ('unpaid' == $new_status && ('pending' == $old_status || 'rejected' == $old_status)) { // Update the visit ID that spawned this referral affiliate_wp()->visits->update($referral->visit_id, array('referral_id' => $referral->referral_id), '', 'visit'); do_action('affwp_referral_accepted', $referral->affiliate_id, $referral); } elseif ('paid' != $new_status && 'paid' == $old_status) { affwp_decrease_affiliate_earnings($referral->affiliate_id, $referral->amount); affwp_decrease_affiliate_referral_count($referral->affiliate_id); } do_action('affwp_set_referral_status', $referral_id, $new_status, $old_status); return true; } return false; }
public function do_referrals($step = 1) { global $wpdb; $offset = ($step - 1) * 100; $referrals = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}aff_referrals ORDER BY referral_id LIMIT {$offset}, 100;"); if ($referrals) { foreach ($referrals as $referral) { if (in_array($referral->affiliate_id, $this->direct_affiliates)) { continue; // Skip referrals for Direct } switch ($referral->status) { case 'accepted': $status = 'unpaid'; break; case 'closed': $status = 'paid'; break; case 'rejected': $status = 'rejected'; break; case 'pending': default: $status = 'pending'; break; } $context = ''; $data = maybe_unserialize($referral->data); if (!empty($data)) { if (!empty($data['order_id'])) { if (!empty($data['order_id']['domain'])) { $context = $data['order_id']['domain']; } } } $args = array('status' => $status, 'affiliate_id' => $referral->affiliate_id, 'date' => $referral->datetime, 'description' => $referral->description, 'amount' => $referral->amount, 'currency' => strtoupper($referral->currency_id), 'reference' => $referral->reference, 'context' => $context, 'visit_id' => 0, 'custom' => ''); $id = affiliate_wp()->referrals->add($args); if ('paid' == $status) { affwp_increase_affiliate_earnings($referral->affiliate_id, $referral->amount); affwp_increase_affiliate_referral_count($referral->affiliate_id); } } return true; } else { // No referrals found, so all done return false; } }
function test_adjust_affiliate_referral_count() { $this->assertEquals(1, affwp_increase_affiliate_referral_count($this->_affiliate_id)); $this->assertEquals(2, affwp_increase_affiliate_referral_count($this->_affiliate_id)); $this->assertEquals(1, affwp_decrease_affiliate_referral_count($this->_affiliate_id)); $this->assertFalse(affwp_decrease_affiliate_referral_count($this->_affiliate_id2)); }