Possible logic error in Affiliate plugin?

I have been working with an affiliate integration and was having trouble getting through the logic of the signup process and the way in which affiliates are assigned. (basically I want to use coupon codes that will potentially overwrite the affiliate to give credit to the owner of the coupon).

In the end, I think the problem I was having is due to what looks like an error in logic in the handle_affiliate_link() function defined in affiliate.php.

Specifically...

Line 264 - tries to check for ref in URL
if(isset($_GET['ref'])) {
// There is an affiliate type query item, check it for validity and then redirect

Line 292 - tries to check all if there is no REF
if(defined('AFFILIATE_CHECKALL')) {
// We are here if there isn't a reference passed, so we need to check the referrer.

Shouldn't this be an elseif? The way is it is now, this section is run even when $_GET['ref'] is set.

Line 308 - Then when it defines the cookie - it tries to use $_GET['ref'], even though we are only supposed to be here if there was no $_GET['ref'], according to the comments at least.

A question:
AFFILIATE_SETNOCOOKIE adds a "noaffiliate_" cookie if no referrer was found. Does this mean that this new user will never be able to be credited to an affiliate? Or if this user then comes to the site via and affiliate link, will it then be given an affiliate?

    Marty Thornley

    Seeing now what this section was going for, I think...

    Should the second part be using $_SERVER['HTTP_REFERER']) in the cookie instead of $_GET['ref']?

    And looking again at the SETNOCOOKIE idea...

    Looks like line 260 should check if(defined('AFFILIATE_SETNOCOOKIE'))...

    Instead of

    if(isset($_COOKIE['noaffiliate_' . COOKIEHASH])) {
    return true;
    }

    Should be

    if(defined('AFFILIATE_SETNOCOOKIE') && isset($_COOKIE['noaffiliate_' . COOKIEHASH])) {
    return true;
    }

    Right?

    jozwikjp

    I modified the plugin to set the referrer based on the domain they are at if the ref is missing..

    Also added a table affiliate_ref to store affiliate hashes and referralID's in one column and user_id in the next column. As the one that was in there before was looking up the value in the meta table was not indexed.

    Keep in mind this is just a example clip of the code. Just to give you an idea.

    CREATE TABLE
    wp_affiliate_ref (
    ref_key varchar(250) NOT NULL,
    user_id int(11) NOT NULL,
    PRIMARY KEY (ref_key)
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Stores affialite hashes and ref id for fast reference.';

    function handle_affiliate_link() {

    if(isset($_COOKIE['noaffiliate_' . COOKIEHASH])) {
    return true;
    }

    if(isset($_GET['ref'])) {
    // There is an affiliate type query item, check it for validity and then redirect

    if(!isset( $_COOKIE['affiliate_' . COOKIEHASH])) {
    // We haven't already been referred here by someone else - note only the first referrer
    // within a time period gets the cookie.

    // Check if the user is a valid referrer
    //echo $this->db->prepare( "SELECT user_id FROM {$this->db->affiliate_ref} WHERE ref_key = '%s'", $_GET['ref']); exit;
    global $wpdb;
    $affiliate = $wpdb->get_var( $wpdb->prepare( "SELECT user_id FROM ".$wpdb->base_prefix ."affiliate_ref WHERE ref_key = '%s'", $_GET['ref']) );
    //print $this->db->prepare( "SELECT user_id FROM {$this->db->affiliate_ref} WHERE meta_key = '%s'", $_GET['ref']); exit;
    if($affiliate) {
    // Update a quick count for this month
    do_action( 'affiliate_click', $affiliate);

    // Grab the referrer
    //$referrer = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST);
    //do_action( 'affiliate_referrer', $affiliate, $referrer );

    // Write the affiliate hash out - valid for 30 days.
    @setcookie('affiliate_' . COOKIEHASH, 'aff' . md5(AUTH_SALT . $_GET['ref']), (time() + (60*60*24*30)), COOKIEPATH, COOKIE_DOMAIN);
    }
    }

    // The cookie is set so redirect to the page called but without the ref in the url
    // for SEO reasons.
    //$this->redirect( remove_query_arg( array('ref') ) );
    //die();
    }
    if(defined('AFFILIATE_CHECKALL')) {
    // We are here if there isn't a reference passed, so we need to check the referrer.
    if(!isset( $_COOKIE['affiliate_' . COOKIEHASH]) && isset($_SERVER['HTTP_REFERER'])) {
    // We haven't already been referred here by someone else - note only the first referrer
    // within a time period gets the cookie.
    $referrer = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST);
    global $wpdb;

    // Check if the user is a valid referrer
    //$affiliate = $this->db->get_var( $this->db->prepare( "SELECT user_id FROM {$this->db->usermeta} WHERE meta_key = 'affiliate_referrer' AND meta_value='%s'", $referrer) );
    $affiliate = $this->db->get_var( $this->db->prepare( "SELECT b.user_id FROM " . $wpdb->base_prefix . "blogs a, " . $wpdb->base_prefix . "bp_user_blogs b WHERE a.blog_id=b.blog_id AND a.domain='%s' LIMIT 1", $referrer) );

    // Check for mapped domain.
    if(!$affiliate){
    $affiliate = $this->db->get_var( $this->db->prepare( "SELECT b.user_id FROM " . $wpdb->base_prefix . "domain_mapping a, " . $wpdb->base_prefix . "bp_user_blogs b WHERE a.blog_id=b.blog_id AND a.domain='%s' LIMIT 1", $referrer) );
    }

    if($affiliate) {
    $_GET['ref']=$this->db->get_var( $this->db->prepare( "SELECT meta_value FROM {$this->db->usermeta} WHERE meta_key = 'affiliate_reference' AND user_id='%s'", $affiliate) );
    // Update a quick count for this month
    do_action( 'affiliate_click', $affiliate);
    // Store the referrer
    do_action( 'affiliate_referrer', $affiliate, $referrer );

    // Write the affiliate hash out - valid for 30 days.
    @setcookie('affiliate_' . COOKIEHASH, 'aff' . md5(AUTH_SALT . $_GET['ref']), (time() + (60*60*24*30)), COOKIEPATH, COOKIE_DOMAIN);
    } else {
    if(defined('AFFILIATE_SETNOCOOKIE')) @setcookie('noaffiliate_' . COOKIEHASH, 'notanaff', 0, COOKIEPATH, COOKIE_DOMAIN);
    }
    }
    }

    }