settings = Data::instance(); $this->query = QueryDB::instance(); /// SIGN UP REFERRALS // add_action( 'user_register', array( $this, 'insert_signup_referral' ), 99, 1 ); /// Pay per click // add_action( 'affi_insert_into_cookie_new_affiliate', [ $this, 'ppc' ], 1, 1 ); } protected function set_affiliate_id() { self::$affiliate_id = apply_filters( 'affi_set_affiliate_id_filter', self::$affiliate_id ); $saved_cookie = $this->settings->getcookie( 'affi_referral' ); if ( empty( self::$affiliate_id ) && ! empty( $saved_cookie ) ) { $cookie_data = $this->settings->decode_referral_cookie( $saved_cookie ); if ( is_array( $cookie_data ) && ! empty( $cookie_data['affiliate_id'] ) ) { $temporaryAffiliateId = 0; if ( $this->settings->get_param( 'affiliate_link_format' ) == 'username' && ! is_numeric( $cookie_data['affiliate_id'] ) ) { $temporaryAffiliateId = $this->query->get_affiliate_id_by_username( $cookie_data['affiliate_id'] ); } if ( ! empty( $temporaryAffiliateId ) ) { self::$affiliate_id = $temporaryAffiliateId; } else { self::$affiliate_id = $cookie_data['affiliate_id']; } self::$visit_id = ( empty( $cookie_data['visit_id'] ) ) ? 0 : $cookie_data['visit_id']; } } } protected function valid_referral() { /// CHECK FOR OWN REFERRENCE $isValid = apply_filters( 'affi_filter_before_valid_referral', true, self::$affiliate_id, self::$user_id ); if ( ! $isValid ) { return false; } if ( self::$affiliate_id && $this->query->is_affiliate_active( self::$affiliate_id ) ) { return true; } return false; } public function save_referral_unverified( $args = array() ) { // protected $keys = array( 'refferal_wp_uid', 'campaign', 'affiliate_id', 'visit_id', 'description', 'source', 'reference', 'reference_details', 'amount', 'currency', ); $args = apply_filters( 'affi_public_filter_insert_referral_args', $args ); foreach ( $keys as $key ) { if ( ! isset( $args[ $key ] ) ) { return false; } } /// NEGATIVE REFERRALS? if ( $args['amount'] < 0 ) { $args['amount'] = 0; } /// EMPTY REFERRALS if ( ! $this->settings->get_param( 'empty_referrals_enable' ) ) { ///don't insert referrals with 0$ $min = 0.01; if ( $args['amount'] < $min ) { return false; } } /// EMPTY REFERRALS $args['date'] = current_time( 'Y-m-d H:i:s' );//date('Y-m-d H:i:s', time()); $args['status'] = 1;//unverified $args['payment'] = 0;//unpaid $args['parent_referral_id'] = '';// empty for moment, will be updated if it's case $args['child_referral_id'] = '';//always will be empty $referral_id = $this->query->save_referral( $args ); if ( $referral_id ) { $this->query->update_visit_referral_id( $args['visit_id'], $referral_id ); } return true; } protected function referral_verified( $reference = '', $source = '', $check_if_can_do = true ) { if ( $check_if_can_do ) { /// Don't change the Referral Status to Verified $dont = $this->settings->get_param( 'referral_status_dont_automatically_change' ); if ( $dont ) { // stop from change status of referral return; } } // error_log('referral_verified'); //skiped referrals table $w_order = wc_get_order( $reference ); if ( $w_order ) { // error_log('referral_verified success'); $aff_id = $w_order->get_meta( 'affi_affiliate_id', true ); $aff_amount = $w_order->get_meta( 'affi_affiliate_rate', true ); $aff_status = $w_order->get_meta( 'affi_affiliate_status', true ); if ( $aff_status && $aff_status != 'success' ) { $this->query->update_affiliates_reward( $aff_id, $aff_amount ); $w_order->update_meta_data( 'affi_affiliate_status', 'success' ); $w_order->save_meta_data(); } } return; $referral_id = $this->query->get_referral_id_for_reference( $reference, $source ); if ( $referral_id ) { $this->query->change_referral_status( $referral_id, 2 ); } } protected function referral_refuse( $reference = '', $source = '' ) { $referral_id = $this->query->get_referral_id_for_reference( $reference, $source ); if ( $referral_id ) { $this->query->change_referral_status( $referral_id, 0 ); } } protected function mlm_do_save_referral_unverified( $child_affiliate_id = 0, $child_referral_id = 0, $count = 1, $limit = 0, $amount = 0, $first_child_username = '', $first_child_referrence = '' ) { } // public function insert_signup_referral( $user_id = 0 ) { // if ( $this->settings->get_param( 'sign_up_referrals_enable' ) && $user_id ) { // self::$user_id = $user_id; // $this->set_affiliate_id(); // if ( $this->valid_referral() ) { // require_once AFFI_PATH . 'public/Affiliate_Referral_Amount.class.php'; // $do_math = new Affiliate_Referral_Amount( self::$affiliate_id, '' ); // $amount = $do_math->get_signup_amount(); // $args = array( // 'refferal_wp_uid' => self::$user_id, // 'campaign' => self::$campaign, // 'affiliate_id' => self::$affiliate_id, // 'visit_id' => self::$visit_id, // 'description' => 'User SignUp', // 'source' => 'User SignUp', // 'reference' => 'user_id_' . $user_id, // 'reference_details' => 'User SignUp', // 'amount' => $amount, // 'currency' => self::$currency, // ); // $this->save_referral_unverified( $args ); // $default_sts = $this->settings->get_param( 'affi_sign_up_default_referral_status' ); // if ( $default_sts == 2 ) { // /// MAKE VERIFIED // $this->referral_verified( 'user_id_' . $user_id, '', false ); // } // } // } // } public function pay_bonus( $amount_value = 0, $rank_name = '' ) { $status = $this->settings->get_param( 'bonus_on_rank_default_referral_sts' ); if ( $status === false ) { $status = 2; /// verified } $args = array( 'refferal_wp_uid' => 0, 'campaign' => '', 'affiliate_id' => self::$affiliate_id, 'visit_id' => '', 'description' => esc_html__( 'Bonus for reaching rank: ', 'affi-affiliate-marketing-for-woo' ) . $rank_name, 'source' => 'bonus', 'reference' => 0, 'reference_details' => 'Bonus', 'amount' => $amount_value, 'currency' => self::$currency, 'date' => current_time( 'Y-m-d H:i:s' ), //date('Y-m-d H:i:s', time()), 'status' => $status, 'payment' => 0, 'parent_referral_id' => '', 'child_referral_id' => '', ); $this->query->save_referral( $args ); } public function order_affiliate_rate( $aff_rate, $w_order, $affiliate_id ) { if ( ! is_array( $aff_rate ) || ! isset( $aff_rate['rate_type'] ) || ! isset( $aff_rate['rate'] ) ) { $aff_rate = []; } $g_r_type = $this->settings->get_param( 'commission_type' ); $g_r_rate = floatval( $this->settings->get_param( 'commission_rate' ) ); $w_order_total = floatval( $w_order->get_total() ); if ( $w_order_total != 0 && $this->settings->get_param( 'commission_tax' ) ) { $w_order_tax = floatval( $w_order->get_total_tax() ); $w_order_total = $w_order_total - $w_order_tax; } $c_wp_user = get_current_user_id(); $is_current_aff = $this->query->get_affiliate_id_by_user_id( $c_wp_user ); if ( $this->settings->get_param( 'commission_owner' ) && ! empty( $is_current_aff ) && $is_current_aff == $affiliate_id ) { return 0; } if ( empty( $aff_rate ) || empty( $aff_rate['rate_type'] ) || empty( $aff_rate['rate'] ) ) { if ( ! empty( $g_r_rate ) ) { if ( $g_r_type == 'fixed' ) { return floatval( $g_r_rate ); } else { return ( $w_order_total * $g_r_rate ) / 100; } } return 0; } else { if ( $aff_rate['rate_type'] == 'fixed' ) { $rank_amount = floatval( $aff_rate['rate'] ); } else { $rank_amount = ( $w_order_total * $aff_rate['rate'] ) / 100; } if ( ! empty( $g_r_rate ) ) { if ( $g_r_type == 'fixed' ) { return floatval( $g_r_rate ) + $rank_amount; } else { return ( ( $w_order_total * $g_r_rate ) / 100 ) + $rank_amount; } } else { return $rank_amount; } } } }