/** * 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; }
function affwp_delete_referral($referral) { 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; } if ($referral && 'paid' == $referral->status) { // This referral has already been paid, so decrease the affiliate's earnings affwp_decrease_affiliate_earnings($referral->affiliate_id, $referral->amount); // Decrease the referral count affwp_decrease_affiliate_referral_count($referral->affiliate_id); } if (affiliate_wp()->referrals->delete($referral_id)) { do_action('affwp_delete_referral', $referral_id); return true; } return false; }
function test_adjust_affiliate_earnings() { $this->assertEquals(10, affwp_increase_affiliate_earnings($this->_affiliate_id, '10')); $this->assertFalse(affwp_increase_affiliate_earnings(0, '10')); $this->assertEquals(8, affwp_decrease_affiliate_earnings($this->_affiliate_id, 2)); $this->assertFalse(affwp_decrease_affiliate_earnings(0, '10')); $this->assertEquals('12.2', affwp_increase_affiliate_earnings($this->_affiliate_id, '4.2')); }