Advertisement
Guest User

Untitled

a guest
Aug 5th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. <?php
  2. class Dataporten_oAuth_register {
  3. private $username;
  4. private $password;
  5. private $email;
  6. private $nickname;
  7. private $dataporten_main;
  8. private $oauth_identity;
  9. private $role;
  10. private $firstname;
  11. private $lastname;
  12. //
  13. //
  14. // Construct function for register class. Tells the user if the registration
  15. // is disabled at the current account, and redirecting the user. Sets all
  16. // variables, and generates password. Creates nickname from email, since the
  17. // username is set to be id of user. This can be changed later.
  18. //
  19. //
  20. public function __construct($dataporten_main, $oauth_identity) {
  21. if(!get_option("users_can_register") && get_option("dataporten_only") != 1) {
  22. header("Location: " . wp_login_url() . "?errors=6");
  23. exit;
  24. }
  25. $this->dataporten_main = $dataporten_main;
  26. $this->oauth_identity = $oauth_identity;
  27. if($oauth_identity['id'] != ""){
  28. $this->username = $oauth_identity['id'];
  29. $this->password = wp_generate_password();
  30. $this->email = $oauth_identity['email'];
  31. $this->nickname = explode('@', $this->email)[0];
  32. $this->firstname = $oauth_identity['firstname'];
  33. $this->lastname = $oauth_identity['lastname'];
  34. $this->groups = $oauth_identity["groups"];
  35. } else {
  36. $this->username = $_POST['identity'];
  37. $this->password = $_POST['password'];
  38. $this->nickname = $this->username;
  39. }
  40. }
  41. //
  42. //
  43. // Takes in the groups priority from dataporten_rolesets and creates user.
  44. // Sets username, display name and all required fields of an account. Sets
  45. // the role-name to default_role. If dataporten_default_role_enabled is enabled,
  46. // it checks the intended role of the user. Then links the dataporten account
  47. // with the wordpress account.
  48. //
  49. //
  50. public function dataporten_create_user() {
  51. global $wpdb;
  52. $groups_priority = json_decode(get_option("dataporten_rolesets"), true);
  53. $user_id = wp_create_user($this->username, $this->password, $this->username);
  54. if (is_wp_error($user_id)) {
  55. if(defined('WP_DEBUG') && WP_DEBUG == true) {
  56. error_log("Username " . $this->username);
  57. error_log($user_id->get_error_message());
  58. }
  59. header("Location: " . site_url() . "?errors=2");
  60. exit;
  61. }
  62. $update_username_result = $wpdb->update($wpdb->users, array(
  63. 'user_login' => $this->username,
  64. 'user_nicename' => $this->username,
  65. 'display_name' => $this->nickname,
  66. 'user_email' => $this->email,
  67. ), array('ID' => $user_id));
  68. $update_nickname_result = update_user_meta($user_id, "nickname", $this->nickname);
  69. $update_nickname_result = update_user_meta($user_id, "first_name", $this->firstname);
  70. $update_nickname_result = update_user_meta($user_id, "last_name", $this->lastname);
  71. $name = get_option('default_role');
  72. if(get_option('dataporten_default_role_enabled')) {
  73. $tmp_results = json_decode(get_option("dataporten_rolesets"), true);
  74. $highest = -1;
  75. foreach($this->groups as $tmp) {
  76. if(!is_array($tmp)) break;
  77. $curr_index = array_search($tmp["id"], array_keys($tmp_results));
  78. if($curr_index > $highest) {
  79. $highest = $curr_index;
  80. $name = $tmp_results[$tmp["id"]];
  81. }
  82. }
  83. $this->role = $name;
  84. }
  85. $update_role_result = wp_update_user(array('ID' => $user_id, 'role' => $this->role));
  86. if ($update_username_result == false || $update_nickname_result == false || $update_role_result == false) {
  87. header("Location: " . wp_login_url() . "?errors=5"); exit;
  88. } else {
  89. $this->dataporten_main->dataporten_link_account($user_id, $this->oauth_identity);
  90. $credentials = array(
  91. 'user_login' => $this->username,
  92. 'user_password' => $this->password,
  93. 'remember' => true,
  94. );
  95. $user = wp_signon($credentials, false);
  96. $this->dataporten_main->dataporten_end_login("", 0, site_url());
  97. }
  98. }
  99. }
  100. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement