<?php
// -----------------------------------//
// Function to Create New User/Member //
// -----------------------------------//
function subm_create_user($cemail,$cbreceipt,$fname,$lname,$u_role) {
      $username = $email = $cemail;     $fullname = "$fname $lname";
      $password = $cbreceipt;           $displayname = $fname.'_'.$lname; 
      
      if(email_exists($cemail)) {
        $userID = email_exists($cemail);
          // chK userID!= superAdmin: func user_can($userID,$capability) don't work, info not avail.yet
          $subm_userDat = get_userdata($userID);
          $subm_userCap = $subm_userDat->wp_capabilities;
          if( $subm_userCap[administrator] ) { // user is ADMIN 
               $ret_data="<h3><font color=red>Error Code: Invalid Email</font></h3>
               You may not choose an email that belongs to the admin.|0";
               $err_flag1=1;
               return($ret_data);
          } else {  //________user is NOT admin, but normal user, update user! ________//
              $user_id = $userID; // use current user ID to update its role
              $userdata = array(
                'ID'          => $user_id,
                'user_login'  => $username,
                'user_pass'   => $cbreceipt,
                'user_email'  => $cemail,
                'first_name'  => $fname,
                'last_name'   => $lname,
                'display_name'=> $displayname
                );
              wp_update_user($userdata);  // update current user with new data
              update_user_meta($user_id, 'cbreceipt',$cbreceipt,'');
              $user = new WP_User($user_id); // create new user object to set/update role.       
              $user->set_role($u_role); // set member's role 
              $ret_data = "$username|$cbreceipt";
              return($ret_data);
          }
      } else {  //___user does not exist, create NEW user______//
           $userdata = array(
              'user_login'  => $username,
              'user_pass'   => $cbreceipt,
              'user_email'  => $cemail,
              'first_name'  => $fname,
              'last_name'   => $lname,
              'display_name'=> $displayname,
              'user_nicename'=> $fullname
              );
          $user_id = wp_insert_user($userdata);
          add_metadata('user',$user_id,'cbreceipt',$cbreceipt,$unique='false');
          $ret_data = "$username|$cbreceipt";
          
          $user = new WP_User($user_id); // create new user object to set/update role.       
          $user->set_role($u_role); // set member's role
          return($ret_data);
          //echo "INSIDE: userID: $user_id <BR />RET: $ret_data<BR />$cemail,$cbreceipt,$fname,$lname,$u_role";exit();
      }
}
// GET subm_members_option //
$subm_mbrs = get_option('subm_members_option');

if($subm_mbrs[processor_type]=="1") {
   require("subm_clickbank_pop.php"); // Clickbank Validation
   $pay_processor = "CLICKBANK";
}
if($subm_mbrs[processor_type]=="2") {
   require("subm_paypal_pdt.php"); // Paypal Validation
   $pay_processor = "PAYPAL";
}
if($subm_mbrs[processor_type]=="3") {
   require("subm_digiresult_pop.php"); // DigiResult Validation
   $pay_processor = "DIGIRESULT";
}
if($subm_mbrs[processor_type]=="4") {
   require("subm_jvzoo_pop.php"); // DigiResult Validation
   $pay_processor = "JVZOO";
}

//__________________________________//
if($cbvldEflag) {
      //_______code to get the "Levels" based on the $item________//
      switch($level) {
        case "5";
          if($subm_mbrs[level5]==$key) { $u_role = "subm_level5"; $u_err=''; $listname = $subm_mbrs[listname5]; }
          else { $u_err="Error! Item ($key) purchased does not match Membership Level";} 
          break;
        case "4";
          if($subm_mbrs[level4]==$key) { $u_role = "subm_level4"; $u_err=''; $listname = $subm_mbrs[listname4]; }
          else { $u_err="Error! Item ($key) purchased does not match Membership Level";} 
          break;
        case "3";
          if($subm_mbrs[level3]==$key) { $u_role = "subm_level3"; $u_err=''; $listname = $subm_mbrs[listname3]; }
          else { $u_err="Error! Item ($key) purchased does not match Membership Level";} 
          break;
        case "2";
          if($subm_mbrs[level2]==$key) { $u_role = "subm_level2"; $u_err=''; $listname = $subm_mbrs[listname2]; }
          else { $u_err="Error! Item ($key) purchased does not match Membership Level";} 
          break;
        case "1";
          if($subm_mbrs[level1]==$key) { $u_role = "subm_level1"; $u_err=''; $listname = $subm_mbrs[listname1]; } 
          else { $u_err="Error! Item ($key) purchased does not match Membership Level";} 
          break;       
        default:
          $u_err = "Error! Key mis-match. Item purchased does not match Membership Level";
          break;
      }
      if($u_err!="") { get_header(); echo "<center><BR /><h2>$u_err</h2></center>"; get_footer(); exit(); }
      
      //___________call subm_create_user() to create the new user________________ //
      list($login,$passwd) = explode("|",subm_create_user($cemail,$cbreceipt,$fname,$lname,$u_role));
      $admin_email = get_option('admin_email'); $url = home_url() . "/wp-login.php";
      
      if($passwd !="0") { // if $passwd==0, then err msg in the $login var.
        //__________email member their details____________//
        $subj = "$fname, your membership login details";
        $mesg = "Dear $fname,\n\nPlease keep the following info in a safe place\n\nURL: $url\nlogin ID: $login\nPassword: $cbreceipt\n\n--- System Generated Mesg :: Subscription Mate ----\n";
         wp_mail($cemail,$subj,$mesg,"From: SubscriptionMate <$admin_email>");
                    
        //________subscribe new member__________//
        if($listname!="") {
          $artype = $subm_mbrs[artype];   $subtype = "Customers"; 
          $api = $subm_mbrs[api_key];
          $prodname = "Membership Level $level";
          $subdata = "$cemail|$cname|$cbreceipt|$key|$admin_email|$level";
          include_once('subm_autores.php');

          switch($artype) {
          case "1": // aweber
              //$subdata = "$cemail|$cname|$cbreceipt|$key|$admin_email|$level";
              subm_aweber($subdata, $api, $listname, $prodname, $subtype);
              break;
          case "2": // getresponse
              //$subdata = "$cemail|$cname|$cbreceipt|$key|$admin_email|$level";
              subm_getresponse( $subdata, $api, $listname, $prodname, $subtype);
              break;
          case "3": // not implemented
              break;
          case "4": // others, subs via email, eg AutoresponsePlus
              //$subdata = "$cemail|$cname|$cbreceipt|$key|$admin_email|$level";
              subm_emailsub( $subdata, $api, $listname, $prodname, $subtype);
              break;
          case "5": // MailChimp
              //$subdata = "$cemail|$cname|$cbreceipt|$key|$admin_email|$level";                
              subm_mailchimp( $subdata, $api, $listname, $prodname, $subtype);
              break; 
          case "8": // custom API - customer needs to create their own //
              if(file_exists('subm_customAPI.php')) { include_once('subm_customAPI.php'); } 
              break;
          }
        }
      } 
}
?>