SHARE
TWEET

Untitled

a guest Apr 2nd, 2019 93 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.   require('class-phpass.php');
  3.  
  4.     // CSV SETUP   
  5.     $users = array_map('str_getcsv', file('javascript_basics.csv'));       //   -- REPLACE THIS BEFORE IMPORT
  6.  
  7.     array_walk($users, function(&$a) use ($users) {
  8.     $a = array_combine($users[0], $a);
  9.   });
  10.   array_shift($users);
  11.  
  12.   // DATABASE SETUP
  13.   $user = "root";
  14.   $pass = '';
  15.  
  16.   // FUNCTIONS WE NEED
  17.   function generatePassword($length = 10) {
  18.     $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  19.     $charactersLength = strlen($characters);
  20.     $password = '';
  21.     for ($i = 0; $i < $length; $i++) {
  22.         $password .= $characters[rand(0, $charactersLength - 1)];
  23.     }
  24.     return $password;
  25.     }
  26.  
  27.     function hashPassword($password){
  28.         $wp_hasher = new PasswordHash(8, TRUE );
  29.         $hashed_password = $wp_hasher->HashPassword($password);
  30.       return $hashed_password;
  31.     }
  32.   // GLOBAL SETUP
  33.   $packageId = 3007; // ID of package that this course is included in         -- REPLACE THIS BEFORE IMPORT
  34.   $courseId = 1271; // ID of course we are adding                             -- REPLACE THIS BEFORE IMPORT
  35.   $emptyValue = ''; // For values that can't be NULL
  36.   $currentTime = date("Y-m-d H:i:s"); // Current time for user info
  37.  
  38.   // POST [WC_USER_MEMBERSHIP] SETUP
  39.   $postStatus = 'wcm-active';
  40.   $postType = 'wc_user_membership';
  41.   $commentStatus = 'closed';
  42.   $pingStatus = 'closed';
  43.  
  44.   // POST META SETUP
  45.   $metaKey = '_start_date';
  46.  
  47.   try {
  48.     // SETUP DATABASE CONNECTION
  49.     $db = new PDO('mysql:host=localhost;dbname=checking', $user, $pass);
  50.  
  51.     foreach($users as $user){
  52.       // Get user info in variables
  53.       $userEmail = $user['email'];
  54.       $userFullname = $user['fullname'];
  55.       $userJoinDate = $user['joined_at'];
  56.  
  57.       if(!$userEmail){
  58.         continue;
  59.       }
  60.  
  61.         // Generate Password
  62.         $password = generatePassword(); // Readable password
  63.         $userPass = hashPassword($password); // Hashed password
  64.  
  65.         // Create other information
  66.             $userLogin = str_replace(" ", ".", strtolower($user['fullname'])); // create user login from fullname
  67.  
  68.       $selectIdQuery = $db->prepare("SELECT ID FROM wp_users WHERE user_email = :user_email");
  69.       $selectIdQuery->execute(array("user_email" => $userEmail));
  70.       $userId = $selectIdQuery->fetch()['0'];
  71.  
  72.       if(!$userId){
  73.         $insertUserQuery = $db->prepare('INSERT INTO wp_users (user_login, user_pass, user_nicename, user_email, user_registered, display_name) VALUES (:user_login, :user_pass, :user_nicename, :user_email, :user_registered, :display_name)');
  74.         $insertUserQuery->bindParam(':user_login', $userLogin);
  75.         $insertUserQuery->bindParam(':user_pass', $userPass);
  76.         $insertUserQuery->bindParam(':user_nicename', $userFullname);
  77.         $insertUserQuery->bindParam(':user_email', $userEmail);
  78.         $insertUserQuery->bindParam(':user_registered', $currentTime);
  79.         $insertUserQuery->bindParam(':display_name', $userLogin);
  80.  
  81.         $insertUserQuery->execute();
  82.  
  83.         // Get user ID
  84.         $userId = $db->lastInsertId();
  85.       }
  86.  
  87.       // User couldn't be inserted - echo name email and continue to next row
  88.       if(!$userId){
  89.         echo "<b>IMPORTANT:</b> User couldn't be inserted name: " . $userFullname . " email: " . $userEmail . " id: " . $user['userid'] . "<br>";
  90.         continue;
  91.       }
  92.       $selectMemberPackageQuery = $db->prepare("SELECT 1 FROM wp_posts WHERE post_type = :post_type AND post_author = :post_author AND post_parent = :post_parent");
  93.       $selectMemberPackageQuery->execute(array('post_type' => $postType, 'post_author' => $userId, 'post_parent' => $packageId));
  94.       $memberPackage = $selectMemberPackageQuery->fetch();
  95.       if($memberPackage){
  96.         echo "User ".$userEmail." is already enrolled in package that contains this course.";
  97.         continue;
  98.       }
  99.  
  100.       $selectMemberQuery = $db->prepare("SELECT 1 FROM wp_posts WHERE post_type = :post_type AND post_author = :post_author AND post_parent = :post_parent");
  101.       $selectMemberQuery->execute(array('post_type' => $postType, 'post_author' => $userId, 'post_parent' => $courseId));
  102.       $memberPost = $selectMemberQuery->fetch();
  103.       if(!$memberPost){
  104.         $insertMemberQuery = $db->prepare('INSERT INTO wp_posts (post_author, post_date, post_date_gmt, post_status, post_modified, post_modified_gmt, post_parent, post_type, comment_status, ping_status) VALUES (:post_author, :post_date, :post_date_gmt, :post_status, :post_modified, :post_modified_gmt, :post_parent, :post_type, :comment_status, :ping_status)');
  105.         $insertMemberQuery->bindParam(':post_author', $userId);
  106.         $insertMemberQuery->bindParam(':post_date', $currentTime);
  107.         $insertMemberQuery->bindParam(':post_date_gmt', $currentTime);
  108.         $insertMemberQuery->bindParam(':post_status', $postStatus);
  109.         $insertMemberQuery->bindParam(':post_modified', $currentTime);
  110.         $insertMemberQuery->bindParam(':post_modified_gmt', $currentTime);
  111.         $insertMemberQuery->bindParam(':post_parent', $courseId);
  112.         $insertMemberQuery->bindParam(':post_type', $postType);
  113.         $insertMemberQuery->bindParam(':ping_status', $pingStatus);
  114.         $insertMemberQuery->bindParam(':comment_status', $commentStatus);
  115.  
  116.         $insertMemberQuery->execute();
  117.  
  118.         $memberId = $db->lastInsertId();
  119.  
  120.         if($memberId){
  121.           $insertMemberMetaQuery = $db->prepare('INSERT INTO wp_postmeta (post_id, meta_key, meta_value) VALUES (:post_id, :meta_key, :meta_value)');
  122.           $insertMemberMetaQuery->bindParam(':post_id', $memberId);
  123.           $insertMemberMetaQuery->bindParam(':meta_key', $metaKey);
  124.           $insertMemberMetaQuery->bindParam(':meta_value', $userJoinDate);
  125.           $insertMemberMetaQuery->execute();
  126.  
  127.           // Done, go to next loop
  128.           continue;
  129.         } else {
  130.           // Member couldn't be inserted - echo ID and Course ID and continue to next row
  131.           echo "<b>IMPORTANT:</b> Member couldn't be inserted wordpress user email: " . $userEmail . " course: " . $courseId . "<br>";
  132.           continue;
  133.         }
  134.       } else {
  135.         // User has already been manually added
  136.         echo "User: " . $userEmail . " has already been enrolled in this course" . "<br>";
  137.         continue;
  138.       }
  139.     }
  140.   } catch (PDOException $e) {
  141.     print "Error: " . $e->getMessage();
  142.     die();
  143.   }
  144.  ?>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top