PageRenderTime 26ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/registration.php

https://gitlab.com/endomorphosis/reservationtelco
PHP | 333 lines | 156 code | 49 blank | 128 comment | 38 complexity | 0b8edb9c2cfcdfbc8c04119332d4adaa MD5 | raw file
  1. <?php
  2. /**
  3. * User Registration API
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * Checks whether the given username exists.
  9. *
  10. * @since 2.0.0
  11. *
  12. * @param string $username Username.
  13. * @return null|int The user's ID on success, and null on failure.
  14. */
  15. function username_exists( $username ) {
  16. if ( $user = get_userdatabylogin( $username ) ) {
  17. return $user->ID;
  18. } else {
  19. return null;
  20. }
  21. }
  22. /**
  23. * Checks whether the given email exists.
  24. *
  25. * @since 2.1.0
  26. * @uses $wpdb
  27. *
  28. * @param string $email Email.
  29. * @return bool|int The user's ID on success, and false on failure.
  30. */
  31. function email_exists( $email ) {
  32. if ( $user = get_user_by_email($email) )
  33. return $user->ID;
  34. return false;
  35. }
  36. /**
  37. * Checks whether an username is valid.
  38. *
  39. * @since 2.0.1
  40. * @uses apply_filters() Calls 'validate_username' hook on $valid check and $username as parameters
  41. *
  42. * @param string $username Username.
  43. * @return bool Whether username given is valid
  44. */
  45. function validate_username( $username ) {
  46. $sanitized = sanitize_user( $username, true );
  47. $valid = ( $sanitized == $username );
  48. return apply_filters( 'validate_username', $valid, $username );
  49. }
  50. /**
  51. * Insert an user into the database.
  52. *
  53. * Can update a current user or insert a new user based on whether the user's ID
  54. * is present.
  55. *
  56. * Can be used to update the user's info (see below), set the user's role, and
  57. * set the user's preference on whether they want the rich editor on.
  58. *
  59. * Most of the $userdata array fields have filters associated with the values.
  60. * The exceptions are 'rich_editing', 'role', 'jabber', 'aim', 'yim',
  61. * 'user_registered', and 'ID'. The filters have the prefix 'pre_user_' followed
  62. * by the field name. An example using 'description' would have the filter
  63. * called, 'pre_user_description' that can be hooked into.
  64. *
  65. * The $userdata array can contain the following fields:
  66. * 'ID' - An integer that will be used for updating an existing user.
  67. * 'user_pass' - A string that contains the plain text password for the user.
  68. * 'user_login' - A string that contains the user's username for logging in.
  69. * 'user_nicename' - A string that contains a nicer looking name for the user.
  70. * The default is the user's username.
  71. * 'user_url' - A string containing the user's URL for the user's web site.
  72. * 'user_email' - A string containing the user's email address.
  73. * 'display_name' - A string that will be shown on the site. Defaults to user's
  74. * username. It is likely that you will want to change this, for both
  75. * appearance and security through obscurity (that is if you don't use and
  76. * delete the default 'admin' user).
  77. * 'nickname' - The user's nickname, defaults to the user's username.
  78. * 'first_name' - The user's first name.
  79. * 'last_name' - The user's last name.
  80. * 'description' - A string containing content about the user.
  81. * 'rich_editing' - A string for whether to enable the rich editor. False
  82. * if not empty.
  83. * 'user_registered' - The date the user registered. Format is 'Y-m-d H:i:s'.
  84. * 'role' - A string used to set the user's role.
  85. * 'jabber' - User's Jabber account.
  86. * 'aim' - User's AOL IM account.
  87. * 'yim' - User's Yahoo IM account.
  88. *
  89. * @since 2.0.0
  90. * @uses $wpdb WordPress database layer.
  91. * @uses apply_filters() Calls filters for most of the $userdata fields with the prefix 'pre_user'. See note above.
  92. * @uses do_action() Calls 'profile_update' hook when updating giving the user's ID
  93. * @uses do_action() Calls 'user_register' hook when creating a new user giving the user's ID
  94. *
  95. * @param array $userdata An array of user data.
  96. * @return int|WP_Error The newly created user's ID or a WP_Error object if the user could not be created.
  97. */
  98. function wp_insert_user($userdata) {
  99. global $wpdb;
  100. extract($userdata, EXTR_SKIP);
  101. // Are we updating or creating?
  102. if ( !empty($ID) ) {
  103. $ID = (int) $ID;
  104. $update = true;
  105. $old_user_data = get_userdata($ID);
  106. } else {
  107. $update = false;
  108. // Hash the password
  109. $user_pass = wp_hash_password($user_pass);
  110. }
  111. $user_login = sanitize_user($user_login, true);
  112. $user_login = apply_filters('pre_user_login', $user_login);
  113. //Remove any non-printable chars from the login string to see if we have ended up with an empty username
  114. $user_login = trim($user_login);
  115. if ( empty($user_login) )
  116. return new WP_Error('empty_user_login', __('Cannot create a user with an empty login name.') );
  117. if ( !$update && username_exists( $user_login ) )
  118. return new WP_Error('existing_user_login', __('This username is already registered.') );
  119. if ( empty($user_nicename) )
  120. $user_nicename = sanitize_title( $user_login );
  121. $user_nicename = apply_filters('pre_user_nicename', $user_nicename);
  122. if ( empty($user_url) )
  123. $user_url = '';
  124. $user_url = apply_filters('pre_user_url', $user_url);
  125. if ( empty($user_email) )
  126. $user_email = '';
  127. $user_email = apply_filters('pre_user_email', $user_email);
  128. if ( !$update && ! defined( 'WP_IMPORTING' ) && email_exists($user_email) )
  129. return new WP_Error('existing_user_email', __('This email address is already registered.') );
  130. if ( empty($display_name) )
  131. $display_name = $user_login;
  132. $display_name = apply_filters('pre_user_display_name', $display_name);
  133. if ( empty($nickname) )
  134. $nickname = $user_login;
  135. $nickname = apply_filters('pre_user_nickname', $nickname);
  136. if ( empty($first_name) )
  137. $first_name = '';
  138. $first_name = apply_filters('pre_user_first_name', $first_name);
  139. if ( empty($last_name) )
  140. $last_name = '';
  141. $last_name = apply_filters('pre_user_last_name', $last_name);
  142. if ( empty($description) )
  143. $description = '';
  144. $description = apply_filters('pre_user_description', $description);
  145. if ( empty($rich_editing) )
  146. $rich_editing = 'true';
  147. if ( empty($comment_shortcuts) )
  148. $comment_shortcuts = 'false';
  149. if ( empty($admin_color) )
  150. $admin_color = 'fresh';
  151. $admin_color = preg_replace('|[^a-z0-9 _.\-@]|i', '', $admin_color);
  152. if ( empty($use_ssl) )
  153. $use_ssl = 0;
  154. if ( empty($user_registered) )
  155. $user_registered = gmdate('Y-m-d H:i:s');
  156. $user_nicename_check = $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->users WHERE user_nicename = %s AND user_login != %s LIMIT 1" , $user_nicename, $user_login));
  157. if ( $user_nicename_check ) {
  158. $suffix = 2;
  159. while ($user_nicename_check) {
  160. $alt_user_nicename = $user_nicename . "-$suffix";
  161. $user_nicename_check = $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->users WHERE user_nicename = %s AND user_login != %s LIMIT 1" , $alt_user_nicename, $user_login));
  162. $suffix++;
  163. }
  164. $user_nicename = $alt_user_nicename;
  165. }
  166. $data = compact( 'user_pass', 'user_email', 'user_url', 'user_nicename', 'display_name', 'user_registered' );
  167. $data = stripslashes_deep( $data );
  168. if ( $update ) {
  169. $wpdb->update( $wpdb->users, $data, compact( 'ID' ) );
  170. $user_id = (int) $ID;
  171. } else {
  172. $wpdb->insert( $wpdb->users, $data + compact( 'user_login' ) );
  173. $user_id = (int) $wpdb->insert_id;
  174. }
  175. update_user_meta( $user_id, 'first_name', $first_name);
  176. update_user_meta( $user_id, 'last_name', $last_name);
  177. update_user_meta( $user_id, 'nickname', $nickname );
  178. update_user_meta( $user_id, 'description', $description );
  179. update_user_meta( $user_id, 'rich_editing', $rich_editing);
  180. update_user_meta( $user_id, 'comment_shortcuts', $comment_shortcuts);
  181. update_user_meta( $user_id, 'admin_color', $admin_color);
  182. update_user_meta( $user_id, 'use_ssl', $use_ssl);
  183. foreach ( _wp_get_user_contactmethods() as $method => $name ) {
  184. if ( empty($$method) )
  185. $$method = '';
  186. update_user_meta( $user_id, $method, $$method );
  187. }
  188. if ( isset($role) ) {
  189. $user = new WP_User($user_id);
  190. $user->set_role($role);
  191. } elseif ( !$update ) {
  192. $user = new WP_User($user_id);
  193. $user->set_role(get_option('default_role'));
  194. }
  195. wp_cache_delete($user_id, 'users');
  196. wp_cache_delete($user_login, 'userlogins');
  197. if ( $update )
  198. do_action('profile_update', $user_id, $old_user_data);
  199. else
  200. do_action('user_register', $user_id);
  201. return $user_id;
  202. }
  203. /**
  204. * Update an user in the database.
  205. *
  206. * It is possible to update a user's password by specifying the 'user_pass'
  207. * value in the $userdata parameter array.
  208. *
  209. * If $userdata does not contain an 'ID' key, then a new user will be created
  210. * and the new user's ID will be returned.
  211. *
  212. * If current user's password is being updated, then the cookies will be
  213. * cleared.
  214. *
  215. * @since 2.0.0
  216. * @see wp_insert_user() For what fields can be set in $userdata
  217. * @uses wp_insert_user() Used to update existing user or add new one if user doesn't exist already
  218. *
  219. * @param array $userdata An array of user data.
  220. * @return int The updated user's ID.
  221. */
  222. function wp_update_user($userdata) {
  223. $ID = (int) $userdata['ID'];
  224. // First, get all of the original fields
  225. $user = get_userdata($ID);
  226. // Escape data pulled from DB.
  227. $user = add_magic_quotes(get_object_vars($user));
  228. // If password is changing, hash it now.
  229. if ( ! empty($userdata['user_pass']) ) {
  230. $plaintext_pass = $userdata['user_pass'];
  231. $userdata['user_pass'] = wp_hash_password($userdata['user_pass']);
  232. }
  233. wp_cache_delete($user[ 'user_email' ], 'useremail');
  234. // Merge old and new fields with new fields overwriting old ones.
  235. $userdata = array_merge($user, $userdata);
  236. $user_id = wp_insert_user($userdata);
  237. // Update the cookies if the password changed.
  238. $current_user = wp_get_current_user();
  239. if ( $current_user->id == $ID ) {
  240. if ( isset($plaintext_pass) ) {
  241. wp_clear_auth_cookie();
  242. wp_set_auth_cookie($ID);
  243. }
  244. }
  245. return $user_id;
  246. }
  247. /**
  248. * A simpler way of inserting an user into the database.
  249. *
  250. * Creates a new user with just the username, password, and email. For a more
  251. * detail creation of a user, use wp_insert_user() to specify more infomation.
  252. *
  253. * @since 2.0.0
  254. * @see wp_insert_user() More complete way to create a new user
  255. *
  256. * @param string $username The user's username.
  257. * @param string $password The user's password.
  258. * @param string $email The user's email (optional).
  259. * @return int The new user's ID.
  260. */
  261. function wp_create_user($username, $password, $email = '') {
  262. $user_login = esc_sql( $username );
  263. $user_email = esc_sql( $email );
  264. $user_pass = $password;
  265. $userdata = compact('user_login', 'user_email', 'user_pass');
  266. return wp_insert_user($userdata);
  267. }
  268. /**
  269. * Set up the default contact methods
  270. *
  271. * @access private
  272. * @since
  273. *
  274. * @return array $user_contactmethods Array of contact methods and their labels.
  275. */
  276. function _wp_get_user_contactmethods() {
  277. $user_contactmethods = array(
  278. 'aim' => __('AIM'),
  279. 'yim' => __('Yahoo IM'),
  280. 'jabber' => __('Jabber / Google Talk')
  281. );
  282. return apply_filters('user_contactmethods',$user_contactmethods);
  283. }
  284. ?>