How do I Set the Affiliate Cookie in Affiliates?

Hi @Paul,
I'm in the process of modifying Affiliates to allow my site visitors to enter an affiliate's reference code (user-16) into the Invite Code field, allowing them to register, and giving the affiliate credit.

It seems that I need to programmatically set the affiliate's cookie info. What file sets the affiliate Cookie that contains the COOKIEHASH data? I know that affiliateincludes/classes/affiliatepublic.php checks for the cookie, but I don't see any code that will generate the cookie for the user. Or at least generate the hash data I need to manually add the correct affiliate_referred_by information.

Any ideas?

  • Paul

    @Christian

    What file sets the affiliate Cookie that contains the COOKIEHASH data?

    You should see the line below in affiliatepublic.php line 551. This is a normal PHP set cookie() function all.

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

    And COOKIEHASH is defined by WordPress. You don't need the details. Just use it as is.

    but I don't see any code that will generate the cookie for the user. Or at least generate the hash data I need to manually add the correct affiliate_referred_by information.

    If you want to set the new user as an affiliate you will need to also programmatically set the user_meta values for

    Look into the affiliateadmin.php file lines 624-626. Call the function aff_build_reference() to build the unique affiliate ID. Note you must pass in a user object.

    $reference = aff_build_reference( $user );
    update_user_meta($user_ID, 'affiliate_reference', $reference);
    update_user_meta($user_ID, 'affiliate_hash', 'aff' . md5(AUTH_SALT . $reference));

    One other thing I'm showing you are still running the Affiliate 3.1.5.3-Beta6. would appreciate you upgrading to the released version of version 3.1.5.3 just in case there are slight differences between beta6 and the final version. Thanks. Let me know if you still have questions.

  • Christian

    Thanks @Paul,

    Your reply did clear up a few things for me.

    In the near future, I do plan to add a checkbox to the Membership registration form that will set the new user as an affiliate and use their submitted email address as their paypal email. To do this, I will definitely use aff_build_reference().

    Before, I thought that the only way to get the referring affiliate's user id manually, was to set the cookie at the point of new member registration. Then pull the user id from the matching affiliate_hash in the database.

    But now I’m thinking that it may not be necessary to set the cookie. My plan is to get the affiliate id the new user enters into the invite code field ($_POST[‘invitecode’]) at signup, then Membership will handle the invite code validation in simple.invitecodes.php automatically. But how do I take that invite code and check it against the affiliate_reference of all the affiliates in the database, and then return the user id of the matching affiliate?

    Before you mentioned using get_user_meta() to get the affiliate_reference, but that requires a user id to search the meta values. In this case, I would need to do the opposite – get the user id of the affiliate that has an affiliate_reference matches. Do you know of any way to do this?

    As I mentioned before, I’m working on a way to add a new affiliate’s affiliate_reference to the invitecodes meta key in membership_simpleinvite_options. This is what I have so far:

    // Get All Invite Code Options - Invite Codes List, Require Invites, & Remove After Use
    $Msi_options = M_get_option('membership_simpleinvite_options', array());
    
    // Make Sure that the Affiliate ID is NOT Already Listed on the Invite Codes List
    
    $codes = array_map( 'trim', explode( PHP_EOL, $Msi_options['invitecodes'] ) );
    if ( !in_array( $reference, $codes ) ) {
    
    // If the Affiliate ID is NOT Listed Add it into Invite Codes List in Database
    $Msi_options['invitecodes'] = $codes + $reference;
    
    M_update_option('membership_simpleinvite_options', $Msi_options['invitecodes']);
    
    } else {
    return __( 'Sorry, that Affiliate already has an Invite Code.', 'membership' );
    }

    My question with this is, am I able to call these Membership functions (such as M_get_option and M_update_option) from within affiliateadmin.php? Or does this code have to be entered into the membership addon code for that to work? And if so, would I need to hook into a particular function from affiliateadmin.php to add my new function to the existing process?

    Thanks again for your help with this. I know I’m about halfway done with coding these functions.

  • Paul

    @Christian,

    But how do I take that invite code and check it against the affiliate_reference of all the affiliates in the database, and then return the user id of the matching affiliate?

    Well do it like the plugin does. You need to query the user_meta table. Look into the function handle_affiliate_link() which is in affiliatepublic.php This is a good start and is where the initial referral link is handled. Specifically line 576 The $referrer is the unique affiliate code used from the url like 'admin-193'. This will return a user_id if found.

    $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) );

    My question with this is, am I able to call these Membership functions (such as M_get_option and M_update_option) from within affiliateadmin.php? Or does this code have to be entered into the membership addon code for that to work? And if so, would I need to hook into a particular function from affiliateadmin.php to add my new function to the existing process?

    First please, Please, PLEASE do not hack affiliate code. I'm working on a major update and all your changes will be lost when you update. Or worse you customize it and this prevents you from future upgrades. I know you are trying to make things custom. At the moment there is not an easy way to have affiliate call your custom function. So maybe I can add something like a do_action which will in turn call your function.

    Back to your question. Yes, you should be able to use those global Membership functions.

  • Paul

    @Christian,

    Maybe you need to switch to helping me with the new Affiliate beta. As I mentioned I did add an option to include a checkbox and paragraph on the WP signup form. Not the Membership. Would be much better to have your input on that codebase than to have you tweaking things in the older code. Since that all has changed. There is no affiliate public.php for example. It is all combined and sorted differently. Plus the admin screens are completely changed.

    I'm in the process of applying all the changers I just made to the latest version of affiliate to the new beta. Sound interesting?

  • Christian

    @Paul,
    Sounds great. I was just getting ready to ask you about affiliateadmin.php. I'm trying to change the $settingstextdefault output message for active affiliates, and those who have not enabled their affiliate status. For some reason, when I change the text, nothing happens. (I understand your concern with changing the core affiliate files, but I only want to add some minor cosmetic changes, and I'm keeping full documentation as to what I've changed so that I can re-add those changes to any further updates)

    But yes, I definitely want to work with you on the new beta.

  • Christian

    @Paul,
    I definitely welcome the restructuring of the code. I've only made some minor changes so far, and I'm sure that it will only take some small adjustments to adapt it to your rewritten version. I've forgotten how much I like writing code. I haven't been writing serious php code since Php 4 was just released, and Wordpress was nowhere on my radar. So it's time for me to get a better understanding of Php 5 and it's wordpress applications. This is a great crash course for me.

    Anyway, thanks again or helping me with this.

  • Christian

    Hey @Paul,
    I just want to check in with you to see what your progress is on the new beta. Any idea when you'll have it ready for me to help with the code mods?

    I've pretty much, finished the Referral ID as Invite Code function based on the old codebase and I've added some extra improvements as well. I just need to finish the function that stores the affiliate's referral id in the invite codes list, and that's about 90% complete as well.

    As far as the improvements are concerned, I've added a function that will automatically friend the affiliate that referred the new member who is registering. I also added the ability to remove the submitted invite code from the invite codes list if there is no matching affiliate id in the system. This way, there won't be any excess invite codes for affiliates that are no longer active in the system.

    My next feat will be a function that will automatically notify the affiliate & the site admin of a new signup or upgrade via messaging. This message will send the new member's signup info – Name, Username, Account URL, Membership Type, City, State, & Join Date.

    And last, but not least, a function that will post new signups and upgrades to the activity stream, with member info - Name, Username, Membership Type, City, & State.

  • Paul

    @Christian,

    I just want to check in with you to see what your progress is on the new beta. Any idea when you'll have it ready for me to help with the code mods?

    Just getting back to that code this weekend. I've not had a chance to touch it in more than a week. Was pulled off for changes to the current Affiliate version plugin some bug fixes on 3 of the other plugins I support.

    I've pretty much, finished the Referral ID as Invite Code function based on the old codebase and I've added some extra improvements as well. I just need to finish the function that stores the affiliate's referral id in the invite codes list, and that's about 90% complete as well.

    When you do finish that step please stop and send me a version of the plugin you have working with notes. Then I'll take a look at incorporating those changes into the new Membership module.

    As far as the improvements are concerned, I've added a function that will automatically friend the affiliate that referred the new member who is registering. I also added the ability to remove the submitted invite code from the invite codes list if there is no matching affiliate id in the system. This way, there won't be any excess invite codes for affiliates that are no longer active in the system.

    I'm not sure I 100% follow this. But maybe once I see the plugin changes in action. So is the invite code logic part of Affiliate or Membership or something else?

    My next feat will be a function that will automatically notify the affiliate & the site admin of a new signup or upgrade via messaging. This message will send the new member's signup info – Name, Username, Account URL, Membership Type, City, State, & Join Date.

    Cool. This was on my list also at some point. Though was looking just to integrate with the Membership signup process. Sort of just append the Affiliate into to the membership signup notification. In theory that was my plan. Have not gotten to that stage as of yet.

    And last, but not least, a function that will post new signups and upgrades to the activity stream, with member info - Name, Username, Membership Type, City, & State.

    Cool. Sounds like a good set of changes.

  • Christian

    Hi @Paul,
    I've been testing the functions for the Affiliate Invite Codes, and I'm not sure if I'm putting these functions into the correct flow of the document.

    So is the invite code logic part of Affiliate or Membership or something else?

    I've added my new functions onto the end of the Membership Addon file for Affiliates so far, but I don't think that they are being called correctly during the enable affiliate process, or the invite code validation process. This is what I've come up with:

    add_action( 'add_profile_report_page', 'add_affiliate_invite_code' );
    
    // Get Referrer's Affiliate ID and Insert it into Invite Codes List
    function add_affiliate_invite_code($reference){
    
    	$Msi_options = M_get_option('membership_simpleinvite_options', array());
    
    	// The Invite Codes List
    	$codes = array_map( 'trim', explode( PHP_EOL, $Msi_options['invitecodes'] ) );
    
    	// If the Affiliate ID is NOT Listed Add it into Invite Codes List
    	if ( !in_array( $reference, $codes ) ) {
    
    	$Msi_options['invitecodes'] = $codes + $reference;
    
    	$Msi_options['invitecodes'] = implode( PHP_EOL, $codes );
    
    	M_update_option('membership_simpleinvite_options', $Msi_options['invitecodes']);
    
    	} else {
    	return __( 'Sorry, that Affiliate already has an Invite Code.', 'membership' );
    	}
    
    }
    
    add_action( 'M_SimpleInviteFieldValidate', 'affiliate_invite_check' );
    
    // Validate Affiliate Invite Code
    function affiliate_invite_check($new_user_id){
    
    	if (isset($_POST['invitecode']) && (!empty($_POST['invitecode']))) {
    
    		$Msi_options = M_get_option('membership_simpleinvite_options', array());
    
    		$codes = array_map( 'trim', explode( PHP_EOL, $Msi_options['invitecodes'] ) );
    
    		$thekey = filter_input( INPUT_POST, 'invitecode' );
    
    		$affiliate = $this->db->get_var( $this->db->prepare( "SELECT user_id FROM {$this->db->usermeta} WHERE meta_key = 'affiliate_reference' AND meta_value='%s'", $thekey) );
    
    		// Check for invite code & remove it if it doesn't match an affiliate's id
    		$key = array_search( $thekey, $codes );
    
    		if ($key !== false && empty($affiliate)) {
    			unset( $codes[$key] );
    			$Msi_options['invitecodes'] = implode( PHP_EOL, $codes );
    			M_update_option( 'membership_simpleinvite_options', $Msi_options );
    
    			} else if($key !== false && !empty($affiliate)){
    
    						update_user_meta($new_user_id, 'affiliate_referred_by', $affiliate);
    
    					}
    		}
    	}
    
    	add_action( 'user_register', 'autofriend_affiliate' );
    	add_action( 'wpmu_activate_user', 'autofriend_affiliate' );
    
    	//Auto-Friend the Referring Affiliate of the Referred Member
    	function autofriend_affiliate($new_user_id, $new_blog_meta){
    
    		// Check for BuddyPress Friends Component
    		if ( bp_is_active( 'friends' ) && function_exists( 'friends_add_friend' ) ){
    
    			$affiliate_referred_by = get_user_meta($new_user_id, 'affiliate_referred_by', true);
    
    			// See if this signup was from an affiliate referral.
    			if (!$affiliate_referred_by) {
    				if (isset($new_blog_meta['affiliate_referred_by']) && (!empty($new_blog_meta['affiliate_referred_by']))) {
    
    					$affiliate_user_id = intval($new_blog_meta['affiliate_referred_by']);
    						friends_add_friend( $affiliate_user_id, $user_id );
    				}
    			}
    		}
    	}

    Any suggestions on how I can get these functions to fire? Or at least be able to tell if they are firing at all.

  • Christian

    Alright @Paul,

    I've made some corrections to my code, but I'm still in the testing phases when it comes to getting the functions to fire on time. Do you think that I should add these function into the files that contain the other functions that I'm hooking into via add_action?

    And I'm still trying to change the text output for the Affiliate Details page, any ideas on how I can get that done? The affiliate activation process really needs to provide the users with step-by-step instructions on how to activate their account and use their links. That's what I'm attempting to add now.

    And what's your progress like on the new codebase?

  • Paul

    @Christian,

    Apologies for the delay. Getting lots of support requests on other plugins I support.

    I've made some corrections to my code, but I'm still in the testing phases when it comes to getting the functions to fire on time.

    Ok. So the code you provided from a previous reply is no longer valid? I've reviewed the code you provided. I can't really comment on the Membership hook as I'm not familiar with them. But the friend logic seems correct. I'm not sure I follow your question in "getting the functions to fire on time". What exactly are you having trouble with?

    Do you think that I should add these function into the files that contain the other functions that I'm hooking into via add_action?

    And what file is that exactly? Can't you just add these to your own functions.php file as part of your theme? For now anyway.

    And I'm still trying to change the text output for the Affiliate Details page, any ideas on how I can get that done?

    In the current version of affiliate (3.1.5.4) the only option is to change the text on Affiliate > Settings > Affiliate advanced settings profile text Or you can change the language file. No other options to do this.

    The affiliate activation process really needs to provide the users with step-by-step instructions on how to activate their account and use their links. That's what I'm attempting to add now.

    Ok. If you say so. There are fields on the Affiliate > Settings to allow you to enter all the instructions you need. Also you are running WordPress so really you can create a page to show as much instructions as you feel your affiliate users need. should this be part of the plugin, probably. But really each implementation is different. What we generally tell other members is setup a public page which explains how the affiliate program works on their site. Have a link to the register form (WordPress or Membership) if the user is not a current user. And another link to the Affiliate page where you would use the shortcode to show the details. I know what you are saying there is a gap between the signup and figuring out how to use the links. Understood.

  • Christian

    Hi @Paul,

    I understand that you're busy, and I really appreciate you taking the time to work with me on this.

    I'm not sure I follow your question in "getting the functions to fire on time".

    Well, I'm not 100% certain that I'm hooking into the necessary functions to execute my code. And beyond that, I'm not sure how to execute the code at the right time. Since I posted last, I've done some research into the do_action function, and if I understand it correctly, it will allow me to choose exactly when to execute the functions that I register with add_action, right?

    1) I want to execute the add_affiliate_invite_code function when the user first enables their affiliate links and presses the update button on the Affiliate Details page. For this, I'm using:
    add_action( 'add_profile_report_page', 'add_affiliate_invite_code' );
    And I've recently added the following do_action to affiliateadmin.php on line 627:
    do_action('add_affiliate_invite_code');

    2) I guess this is best directed @Jonathan, but I want to execute the affiliate_invite_check function when the user's invite code is being validated during the submission of the registration form. For this, I'm using:
    add_action( 'M_SimpleInviteFieldValidate', 'affiliate_invite_check' );
    And I've recently added the following do_action to simple.invitecodes.php on line 151:
    do_action( 'affiliate_invite_check');

    3) And I want the autofriend_affiliate function to execute right after the new user is registered in the system. For this, I'm using:
    add_action( 'wpmu_activate_user', 'autofriend_affiliate' );
    And I've recently added the following do_action to affiliateadmin.php on line 309:
    do_action('autofriend_affiliate', $new_user_id, $new_blog_meta['affiliate_referred_by']);

    My main challenge with this is in finding a way to debug my functions. As it stands, I have no way of verifying that the correct data is even being passed in the variables that I'm using. I've tried to echo the variable for this reason, but the debug data doesn't appear on the page. (The form submission processes are behaving just as they would if my code had not been added)

    I do like the idea of using my theme's functions.php file for now. As long as I can get my functions to execute when needed.

    the only option is to change the text on Affiliate > Settings > Affiliate advanced settings profile text Or you can change the language file.

    I have tried to enter my text here, but for some reason, the text that I enter appears outside of the container (the yellow box) on the Affiliate Details page. I'm going to try to add some CSS selectors to it, just to see if I can get the formatting right. But otherwise, I'll take your suggestion and use another page to provide instructions.

  • Christian

    Oh, this is what my functions look like now:

    add_action( 'add_profile_report_page', 'add_affiliate_invite_code' );
    
    	// Get Referrer's Affiliate ID and Insert it into Invite Codes List
    	function add_affiliate_invite_code(){
    
    		$user = wp_get_current_user();
    		$user_ID = $user->ID;
    
    		$reference = get_user_meta($user_ID, 'affiliate_reference', true);
    
    		$Msi_options = M_get_option('membership_simpleinvite_options', array());
    
    		// The Invite Codes List
    		$codes = array_map( 'trim', explode( PHP_EOL, $Msi_options['invitecodes'] ) );
    
    		// If the Affiliate ID is NOT Listed Add it into Invite Codes List
    		if ( !in_array( $reference, $codes ) ) {
    
    		$codes .= $reference;
    
    		$updated_codes = implode( PHP_EOL, $codes );
    
    		M_update_option('membership_simpleinvite_options', $updated_codes);
    
    		} else {
    		return __( 'Sorry, that Affiliate already has an Invite Code.', 'affiliate' );
    		}
    
    	}
    
    	add_action( 'M_SimpleInviteFieldValidate', 'affiliate_invite_check' );
    
    	// Validate Affiliate Invite Code
    	function affiliate_invite_check(){
    
    		if (isset($_POST['invitecode']) && (!empty($_POST['invitecode']))) {
    
    			$Msi_options = M_get_option('membership_simpleinvite_options', array());
    
    			$codes = array_map( 'trim', explode( PHP_EOL, $Msi_options['invitecodes'] ) );
    
    			$thekey = filter_input( INPUT_POST, 'invitecode' );
    
    			$affiliate = $this->db->get_var( $this->db->prepare( "SELECT user_id FROM {$this->db->usermeta} WHERE meta_key = 'affiliate_reference' AND meta_value='%s'", $thekey) );
    
    			// Check for invite code & remove it if it doesn't match an affiliate's id
    			$key = array_search( $thekey, $codes );
    
    			if ($key !== false && empty($affiliate)) {
    				unset( $codes[$key] );
    				$Msi_options['invitecodes'] = implode( PHP_EOL, $codes );
    				M_update_option( 'membership_simpleinvite_options', $Msi_options['invitecodes'] );
    
    				} else if($key !== false && !empty($affiliate)){
    
    							update_user_meta($new_user_id, 'affiliate_referred_by', $affiliate);
    
    						}
    			}
    		}
    
    	add_action( 'user_register', 'autofriend_affiliate' );
    	add_action( 'wpmu_activate_user', 'autofriend_affiliate' );
    
    	//Auto-Friend the Referring Affiliate of the Referred Member
    	function autofriend_affiliate(){
    
    		global $bp, $new_user_id, $new_blog_meta;
    
    		// Check for BuddyPress Friends Component
    		if ( bp_is_active( 'friends' ) && function_exists( 'friends_add_friend' ) ){
    
    			$affiliate_referred_by = intval(get_user_meta($new_user_id, 'affiliate_referred_by', true));
    
    			// See if this signup was from an affiliate referral.
    			if (isset($affiliate_referred_by) && (!empty($affiliate_referred_by) ) ) {
    
    						// Request & accept the friendship
    						friends_add_friend( $new_user_id, $affiliate_user_id, $force_accept = true);
    						self::update_friendship_counts( $affiliate_user_id );			
    
    				} else if(isset($new_blog_meta['affiliate_referred_by']) && (!empty($new_blog_meta['affiliate_referred_by']) ) ){
    
    						$affiliate_user_id = intval($new_blog_meta['affiliate_referred_by']);
    
    						// Request & accept the friendship
    						friends_add_friend( $new_user_id, $affiliate_user_id, $force_accept = true);
    						self::update_friendship_counts( $new_user_id );
    					}
    		}
    	}
    
    	// Update Friend Count
    	function update_friendship_counts( $new_user_id ) {
    
    		/* Get friends of new user */
    		$friend_ids = BP_Friends_Friendship::get_friend_user_ids( $new_user_id );
    
    		/* Loop through the affiliate's friends and update their friend counts */
    		foreach ( (array) $friend_ids as $friend_id ) {
    			BP_Friends_Friendship::total_friend_count( $friend_id );
    		}
    
    		/* Update affiliate friend counts */
    		BP_Friends_Friendship::total_friend_count( $new_user_id );
    	}
  • Christian

    Ok @Paul,

    I've been making some serious progress here. I realized that my code is not executing because the action hooks I'm trying to use, aren't working. So, I've been adding my custom code right into the core plugin files, for now. This is just until I find a way to add my functions in dynamically.

    Here's my new challenge. I'm trying to add the following code to simple.invitecodes.php from the Membership plugin:

    $thekey = filter_input( INPUT_POST, 'invitecode' );
    
    		$affiliate = $this->db->get_var( $this->db->prepare( "SELECT user_id FROM {$this->db->usermeta} WHERE meta_key = 'affiliate_reference' AND meta_value='%s'", $thekey) );

    As you can see, this code is designed to get the user id of the affiliate whose affiliate reference matches the invite code that has been entered. The problem is, I get a php error:

    Fatal error: Using $this when not in object context in /home/theext7/public_html/wp-content/plugins/membership/membershipincludes/addons/simple.invitecodes.php on line 162

    How can I modify Membership to enable me to use this code? I've tried copying the affiliate class, but it gives me an error message stating that the affiliate class can't be called again. This is really the last obstacle on my way to getting this mod finished.

    Your assistance would be greatly appreciated.

  • Paul

    @Christian,

    How can I modify Membership to enable me to use this code?

    You don't. The problem is both Affiliate and Membership were authored by the same developer who seemed to like taking a copy of the global WordPress $wpdb instance.

    In your query you are just querying against the wp_user_meta table. So just use $wpdb like the following:

    global $wpdb; // <-- Needed if called from within your own function
    $thekey = filter_input( INPUT_POST, 'invitecode' );
    $affiliate = $wpdb->get_var( $wpdb->prepare( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = 'affiliate_reference' AND meta_value='%s'", $thekey) );

    Of course there are times where you really need to hook into the Membership of Affiliate because both define the plugin specific tables used. So in that case instead of this just call the global instance of the class.

    For Membership you need to global $membershippublic like I did for global $wpdb. Then you can use $membershippublic->db->prepare(), etc.

    For Affiliate there is a similar $affiliatepublic. Though in my new version there is only $affiliate.

  • Christian

    Hi @Paul,

    I've been making some good progress here. I just finished the invite code validation, where it checks for an existing invite code & a matching affiliate, and removes the invite code used if it exists on the list and there is no matching affiliate reference. It also sets the affiliate cookie upon manual entry and validation of the invite code. That part is smooth, but I have a couple of things left to fix.

    1) For some reason, when I try to record the click it's recorded twice in the system. I can't figure out why this is. I'm using this in simple.invitecodes.php:

    // Grab the referrer
    						if(isset($_SERVER['HTTP_REFERER'])) {
    							$referrer = parse_url(esc_attr($_SERVER['HTTP_REFERER']), PHP_URL_HOST);
    						} else {
    							$referrer = '';
    						}
    
    						$meta = array(
    							'REMOTE_URL'		=>	esc_attr($_SERVER['HTTP_REFERER']),
    							'LOCAL_URL'			=>	( is_ssl() ? 'https://' : 'http://' ) . esc_attr($_SERVER['HTTP_HOST']) . esc_attr($_SERVER['REQUEST_URI']),
    							'IP'				=>	(isset($_SERVER['HTTP_X_FORWARD_FOR'])) ? esc_attr($_SERVER['HTTP_X_FORWARD_FOR']) : esc_attr($_SERVER['REMOTE_ADDR']),
    							//'HTTP_USER_AGENT'	=>	esc_attr($_SERVER['HTTP_USER_AGENT'])
    						);
    
    						// Update a quick count for this month
    						$note = __('Referal', 'affiliate') .' '. esc_attr($_SERVER['HTTP_REFERER']);
    						do_action( 'affiliate_click', $affiliate, false, 'unique:click', false, $note, $meta);
    						do_action( 'affiliate_referrer', $affiliate, $referrer );

    2) It doesn't record the signup at all. I'm using this code to accomplish this in simple.invitecodes.php:

    // Check if this signup was from an affiliate referal.
    						$affiliate_referred_by = get_user_meta($new_user_id, 'affiliate_referred_by', true);
    						if (!$affiliate_referred_by) {
    							if (isset($new_blog_meta['affiliate_referred_by'])) {
    
    								$affiliate_user_id = intval($new_blog_meta['affiliate_referred_by']);
    
    								// Call the affiliate action
    								$meta = array(
    									'REMOTE_URL'		=>	esc_attr($_SERVER['HTTP_REFERER']),
    									'LOCAL_URL'			=>	( is_ssl() ? 'https://' : 'http://' ) . esc_attr($_SERVER['HTTP_HOST']) . esc_attr($_SERVER['REQUEST_URI']),
    									'IP'				=>	(isset($_SERVER['HTTP_X_FORWARD_FOR'])) ? esc_attr($_SERVER['HTTP_X_FORWARD_FOR']) : esc_attr($_SERVER['REMOTE_ADDR']),
    									//'HTTP_USER_AGENT'	=>	esc_attr($_SERVER['HTTP_USER_AGENT'])
    								);
    
    								//echo "blog_details<pre>"; print_r($blog_details); echo "</pre>";
    								//echo "adding affiliate_signup<br />";
    
    								$note = __('User', 'affiliate');
    								do_action( 'affiliate_signup', $affiliate_user_id, false, 'signup:user', $new_user_id, $note, $meta);
    								update_user_meta($new_user_id, 'affiliate_referred_by', $affiliate_user_id);
    							}
    						}

    3) In affiliateadmin.php, it's not adding the new affiliate reference to the invite codes list. For this I'm using:

    if(isset($_POST['enable_affiliate']) && addslashes($_POST['enable_affiliate']) == 'yes') {
    				// Set up the affiliation details
    				// Store a record of the reference
    				$reference = aff_build_reference( $user );
    				update_user_meta($user_ID, 'affiliate_reference', $reference);
    				update_user_meta($user_ID, 'affiliate_hash', 'aff' . md5(AUTH_SALT . $reference));
    
    				// Get the new affiliate's affiliate id and insert it into the invite codes list
    				$reference = get_user_meta($user_ID, 'affiliate_reference', true);
    
    				$Msi_options = M_get_option('membership_simpleinvite_options', array());
    
    				// The invite codes list
    				$codes = array_map( 'trim', explode( PHP_EOL, $Msi_options['invitecodes'] ) );
    
    				// If the affiliate id is NOT listed add it into the invite codes list
    				if ( !in_array( $reference, $codes ) ) {
    
    				$codes .= $reference;
    
    				$Msi_options['invitecodes'] = implode( PHP_EOL, $codes );
    				M_update_option( 'membership_simpleinvite_options', $Msi_options );
    
    				}

    So my question is, is there any thing that I need to use to enable do_action('affiliate_click') & do_action('affiliate_signup') to work inside of Membership?

    And conversely, is there anything that I have to add to affiliateadmin.php to get M_get_option('membership_simpleinvite_options', array()) to work within Affiliates?

  • Christian

    @Paul,

    I've decided to abandon the idea of recording a click for the manual entry of the affiliate invite code. I did manage to get do_action('affiliate_signup') to work, but it is doing the same thing that do_action('affiliate_click') was doing - recording duplicates.

    I also noticed that it's not updating the user meta "affiliate_referred_by", because the note for the signup doesn't show a username. (On the Affiliate System Administration page)

    Is there any other way for me to update the user meta "affiliate_referred_by" from within simple.invitecodes.php? Possibly a SQL query? And I think that part of the reason why update_user_meta() isn't working is because $new_user_id is empty. Is there another variable that I can use to get the id of the user that's currently signing up?

    I really need some help with this, I'm scheduled to launch this week and this mod HAS to be working before new users can sign up.

  • Paul

    @Christian,

    Sorry to hear you are distressed over this. I'll try and provide help as much as I can but generally as you are trying to custom code something we don't provide support.

    I know in concept I believe what you are attempting but in your previous posts to this thread you are providing snippets of code and I don't really have an idea of the big picture. Sorry I work many dozen tickets during the day and can't keep all the details straight. Is there something I can see online to get a deeper idea of the issue?

    At this point I don't know what the actions are failing. Maybe you are trying to call them too soon n the processing. Yes, you can manually update the 'affiliate_referred_by' using the standard WP function update_user_meta. But you have to know the affiliate user ID.

  • Christian

    Hi @Paul,

    Thanks for helping me with this Paul. I understand that you don't normally support custom coding, but I really do think that this will be a valuable addition to the Affiliates - Membership system. It makes online and offline referrals easier for your affiliates and it's something unique that none of the other competing plugins have.

    To answer your question, I'm on EST as well, and I usually don't start coding until around 10 - 11am. But my schedule is pretty flexible throughout the day, so I can easily work around your time frame.

    Is there something I can see online to get a deeper idea of the issue?

    Well, I'm unable to attach my modified versions of affiliatepublic.php, affiliateadmin.php, and simple.invitecodes.php, because this Q&A system doesn't allow me to upload php files. However, I can grant you support access if you need it to see what I'm talking about. I may also try to upload these files to Github so you can check them out.

  • Paul

    @Christian,

    Whatever access or information or better a working example of the form and access to the backend would be better so I don't have to spent time on my own trying to setup a similar environment.

    You can contact me directly. Go to https://premium.wpmudev.org/contact/ select 'I have a different question'. Then in the message body mention this thread and my name. The ticket will be routed to me. Then we can talk over that in private.

  • Paul

    @Christian,

    So looking first at affiliate public.php I see your lines 618-635. Questions:

    1. Why are you checking again for the cookie? You can see the cookie is set in previous section 578-611. See line 608 where the affiliate cookie is set. At that point the affiliate user ID is know. So you just need to add your user meta for the invite code.

    2. Per the previous point I'm seeing the code new the 'click' action. This is where the user clicks an external link is taken to the WP site. But I know the invite code is also showing on the register form (Membership). So are you auto-populating the form based on the $_GET value? I guess I'm confused how exactly you are handling the form submit with the invite code as it relates to Affiliate processing.

    3. Lines 311-336 and 341-355 are related to Friends process. I'm assuming that is all working correctly?

  • Christian

    @Paul,

    To answer questions 1 & 2, I'm populating the invite code field via the aff_code cookie that I set on line 632. After you pointed that out, I removed the second check for the cookie, and added the code from lines 626-632 onto line 609 in order to set the aff_code cookie, and pass the invite code.

    Regarding the auto-friending, no, it is not working correctly at the moment, but I'm not too worried about that now.

  • Christian

    @Paul

    I guess I'm confused how exactly you are handling the form submit with the invite code as it relates to Affiliate processing.

    At the moment, all validation of the invite code is handled through simple.invitecodes.php (on lines 153-206) and I think that's really a big part of my issue. If I had a way to use the Membership code inside of affiliatepublic, I'm sure that the duplicate records issue would be solved. It would also show me what I need to change in affiliateadmin.php to get that working also.

  • Christian

    Hi @Paul,

    I think you're right about the affiliate invite code process being executed at the wrong time. I changed my code in simple.invitecodes.php to set a cookie with the referring affiliate's user id, and I added a check for that cookie in affiliate public's wpmu_activate_user() (on lines 291-310) to make sure that the affiliate invite code has been verified. Unfortunately, this did not work. In affiliate debug, I can see that the cookie is being set, but when wpmu_activate_user() checks for it, it doesn't update the user meta key affiliate_referred_by.

    I was under the impression that once the submit button was pressed on the registration form, the invite code validation kicks in and stops the registration if the code doesn't match up. In this scenario, my code would execute before wpmu_activate_user(). But since wpmu_activate_user() is not recognizing the cookie, it leads me to believe that the cookie is not being set in time for wpmu_activate_user() to pick it up.

    Do you have any ideas on how I can pass the affiliate user_id from simple.invitecodes.php to affiliatepublic.php? I know that if I can do that, I can update the user meta key affiliate_referred_by for the new user, and affiliatepublic will handle the rest of the registration normally - with no duplicate records.

    Here's the link to the updated code:
    http://teamextreme-usa.com/affiliate-invite-codes.zip

  • Paul

    @Christian,

    Sorry to say I've not been able to spend much time getting my head into this over the past few days. Just too many other fires to focus on bug hunting in other plugins.

    I think you are correct on the cookie issue. Many time setting the cookie in one function does not make it available in other functions. PHP has always had this quick. Why not just define a global variable that you can set where you are now setting the cookie. Then in the affiliate public function just read that global var. Then you are not setting a cookie.

    Other thoughts I don't know why you are hacking the affiliate public anyway. The wpmu_activate_user() function is owned by WordPress. You can have multiple function which attach to this action. So you could just setup your own but make sure it runs AFTER the one for Affiliate.

    So the action from affiliatepublic.php
    add_action('wpmu_activate_user', array(&$this, 'wpmu_activate_user'), 10, 3);

    The third parameter is the priority. You could just define your own function to be called and set the priority hight like '99' so ensure it is called after affiliate. Then within your own function call get_user_meta to see if the referred_by items is set. Then set your invite code. Same god for the Friend logic. This way you are not changing the affiliate plugin. Unless I'm missing something.

  • Christian

    Hi @Paul,

    Thanks for the input. I managed to get it working. I took your advice and I stored the affiliate's user id in a session variable using simple.invitecodes.php. Then I modified membership public to update the usermeta and record the signup with that session variable.

    Other thoughts I don't know why you are hacking the affiliate public anyway.

    I do plan to use action hooks to get this done properly, but for right now, I just need to get it working the quick and dirty way, because I had too much trouble with the action hooks before. But now that you've told me how to set the priority properly, I should be able to turn my mods into independent functions.

    I'm working on automatically adding the affiliate's reference to the invite codes list now.

    Thanks again Paul.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.