prefix."affp_referrals"; $structure = "CREATE TABLE $table ( affp_id BIGINT(20) unsigned NOT NULL, affp_referral VARCHAR(100) NOT NULL, UNIQUE KEY affp_id (affp_id) );"; require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); dbDelta($structure); } register_activation_hook(__FILE__,'affiplus_install'); // process incomming referral function affiplus_getreferral() { if(isset($_GET['affid'])) { $affid = $_GET['affid']; global $wpdb; $affiliate_plus_cfg = array( 'affp_override' => '', 'affp_expire' => '', 'affp_redir' => '' ); $affiliate_plus_cfg = get_option('affiliate_plus_cfg'); if (!$affiliate_plus_cfg['affp_override']){ // check if cookie already exists if(isset($_COOKIE['affiplus'])){ return; } } if($affiliate_plus_cfg['affp_expire']){ $exp = time()+60*60*24*$affiliate_plus_cfg['affp_expire']; } $wp_root = get_option('home'); $htp = "http://"; $htps = "https://"; $affp_domain = str_replace($htp, ".", $wp_root); $affp_domain = str_replace($htps, ".", $affp_domain); $affp_domain = explode("/",$affp_domain); // set cookie setcookie('affiplus', $affid, $exp, '/', $affp_domain[0]); } } add_action("init", "affiplus_getreferral"); function affiplus_signupform() { // check if we have a cookie if(isset($_COOKIE['affiplus'])){ echo $_COOKIE['affiplus']; } } add_action("register_form", "affiplus_signupform"); function affiplus_register($userid) { global $wpdb; $reffered = $wpdb->escape($_COOKIE['affiplus']); $table = $wpdb->prefix."affp_referrals"; $wpdb->query("INSERT INTO $table(affp_id, affp_referral) VALUES('$userid', '$reffered')"); } add_action("user_register", "affiplus_register"); function affiplus_settings() { global $wpdb; include 'affipsettings.php'; } function affiplus_menu() { add_submenu_page('options-general.php', 'Affiliate Plus Settings', 'Affiliate Plus', 9, 'affiplus_settings', 'affiplus_settings'); } add_action("admin_menu", "affiplus_menu"); function affiplus_redirect($redirect_to, $requested_redirect_to, $user) { if ( !isset ( $user->user_login ) ) { return $redirect_to; } if($user->user_level){ if($user->user_level > 7){ return $requested_redirect_to; } } $affiliate_plus_cfg = array( 'affp_override' => '', 'affp_expire' => '', 'affp_redir' => '' ); $affiliate_plus_cfg = get_option('affiliate_plus_cfg'); if ($affiliate_plus_cfg['affp_redir']){ return $affiliate_plus_cfg['affp_redir']; } else { return $requested_redirect_to; } } add_filter("login_redirect", "affiplus_redirect", 10, 3); // Add new column to the user list function affiplus_addcolumn( $columns ) { // This requires WP 2.8+ $columns['affiplus_refbycol'] = __('referred by', 'user-locker'); $columns['affiplus_refcountcol'] = __('referred', 'user-locker'); return $columns; } add_filter("manage_users_columns", "affiplus_addcolumn"); // Add column content for each user on user list function affiplus_fillcolumn( $value, $column_name, $user_id ) { global $wpdb; if ( $column_name == 'affiplus_refbycol' ) { // get referral name $user_info = get_userdata($user_id); $table = $wpdb->prefix."affp_referrals"; $referral = $wpdb->get_var("SELECT affp_referral FROM $table WHERE affp_id=$user_id"); if($referral){ return $referral; }else{ return "-"; } } if ( $column_name == 'affiplus_refcountcol' ) { // count referrals by this user $user_info = get_userdata($user_id); $table = $wpdb->prefix."affp_referrals"; $ref_count = $wpdb->get_var("SELECT COUNT(*) FROM $table WHERE affp_referral = '$user_info->user_login'"); if($ref_count){ return $ref_count; }else{ return "-"; } } return $value; } add_filter("manage_users_custom_column", "affiplus_fillcolumn", 10, 3 ); function affiplus_userpage($user_id) { global $wpdb; $affurl = get_option('siteurl').'/?affid='.$user_id->user_login; // count referrals by this user $table = $wpdb->prefix."affp_referrals"; $ref_count = $wpdb->get_var("SELECT COUNT(*) FROM $table WHERE affp_referral = '$user_id->user_login'"); if(!$ref_count){ $ref_count = "-"; } // get referral name $referral = $wpdb->get_var("SELECT affp_referral FROM $table WHERE affp_id = '$user_id->ID'"); if(!$referral){ $referral = '-'; } echo"
| referred by | $referral |
|---|---|
| number of members referred | $ref_count |
| referral URL | send people to this URL to get credit for new signups |