PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/includes/user.php

https://bitbucket.org/lordmuffin/origin
PHP | 372 lines | 219 code | 54 blank | 99 comment | 82 complexity | 843417da7185fc3e44f28fc07ee2cbf3 MD5 | raw file
  1. <?php
  2. /**
  3. * WordPress user administration API.
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. */
  8. /**
  9. * Creates a new user from the "Users" form using $_POST information.
  10. *
  11. * @since 2.0
  12. *
  13. * @return null|WP_Error|int Null when adding user, WP_Error or User ID integer when no parameters.
  14. */
  15. function add_user() {
  16. return edit_user();
  17. }
  18. /**
  19. * Edit user settings based on contents of $_POST
  20. *
  21. * Used on user-edit.php and profile.php to manage and process user options, passwords etc.
  22. *
  23. * @since 2.0
  24. *
  25. * @param int $user_id Optional. User ID.
  26. * @return int user id of the updated user
  27. */
  28. function edit_user( $user_id = 0 ) {
  29. global $wp_roles, $wpdb;
  30. $user = new stdClass;
  31. if ( $user_id ) {
  32. $update = true;
  33. $user->ID = (int) $user_id;
  34. $userdata = get_userdata( $user_id );
  35. $user->user_login = wp_slash( $userdata->user_login );
  36. } else {
  37. $update = false;
  38. }
  39. if ( !$update && isset( $_POST['user_login'] ) )
  40. $user->user_login = sanitize_user($_POST['user_login'], true);
  41. $pass1 = $pass2 = '';
  42. if ( isset( $_POST['pass1'] ))
  43. $pass1 = wp_unslash( $_POST['pass1'] );
  44. if ( isset( $_POST['pass2'] ))
  45. $pass2 = wp_unslash( $_POST['pass2'] );
  46. if ( isset( $_POST['role'] ) && current_user_can( 'edit_users' ) ) {
  47. $new_role = sanitize_text_field( $_POST['role'] );
  48. $potential_role = isset($wp_roles->role_objects[$new_role]) ? $wp_roles->role_objects[$new_role] : false;
  49. // Don't let anyone with 'edit_users' (admins) edit their own role to something without it.
  50. // Multisite super admins can freely edit their blog roles -- they possess all caps.
  51. if ( ( is_multisite() && current_user_can( 'manage_sites' ) ) || $user_id != get_current_user_id() || ($potential_role && $potential_role->has_cap( 'edit_users' ) ) )
  52. $user->role = $new_role;
  53. // If the new role isn't editable by the logged-in user die with error
  54. $editable_roles = get_editable_roles();
  55. if ( ! empty( $new_role ) && empty( $editable_roles[$new_role] ) )
  56. wp_die(__('You can&#8217;t give users that role.'));
  57. }
  58. if ( isset( $_POST['email'] ))
  59. $user->user_email = sanitize_text_field( $_POST['email'] );
  60. if ( isset( $_POST['url'] ) ) {
  61. if ( empty ( $_POST['url'] ) || $_POST['url'] == 'http://' ) {
  62. $user->user_url = '';
  63. } else {
  64. $user->user_url = esc_url_raw( $_POST['url'] );
  65. $protocols = implode( '|', array_map( 'preg_quote', wp_allowed_protocols() ) );
  66. $user->user_url = preg_match('/^(' . $protocols . '):/is', $user->user_url) ? $user->user_url : 'http://'.$user->user_url;
  67. }
  68. }
  69. if ( isset( $_POST['first_name'] ) )
  70. $user->first_name = sanitize_text_field( $_POST['first_name'] );
  71. if ( isset( $_POST['last_name'] ) )
  72. $user->last_name = sanitize_text_field( $_POST['last_name'] );
  73. if ( isset( $_POST['nickname'] ) )
  74. $user->nickname = sanitize_text_field( $_POST['nickname'] );
  75. if ( isset( $_POST['display_name'] ) )
  76. $user->display_name = sanitize_text_field( $_POST['display_name'] );
  77. if ( isset( $_POST['description'] ) )
  78. $user->description = trim( $_POST['description'] );
  79. foreach ( _wp_get_user_contactmethods( $user ) as $method => $name ) {
  80. if ( isset( $_POST[$method] ))
  81. $user->$method = sanitize_text_field( $_POST[$method] );
  82. }
  83. if ( $update ) {
  84. $user->rich_editing = isset( $_POST['rich_editing'] ) && 'false' == $_POST['rich_editing'] ? 'false' : 'true';
  85. $user->admin_color = isset( $_POST['admin_color'] ) ? sanitize_text_field( $_POST['admin_color'] ) : 'fresh';
  86. $user->show_admin_bar_front = isset( $_POST['admin_bar_front'] ) ? 'true' : 'false';
  87. }
  88. $user->comment_shortcuts = isset( $_POST['comment_shortcuts'] ) && 'true' == $_POST['comment_shortcuts'] ? 'true' : '';
  89. $user->use_ssl = 0;
  90. if ( !empty($_POST['use_ssl']) )
  91. $user->use_ssl = 1;
  92. $errors = new WP_Error();
  93. /* checking that username has been typed */
  94. if ( $user->user_login == '' )
  95. $errors->add( 'user_login', __( '<strong>ERROR</strong>: Please enter a username.' ) );
  96. /* checking the password has been typed twice */
  97. do_action_ref_array( 'check_passwords', array( $user->user_login, &$pass1, &$pass2 ) );
  98. if ( $update ) {
  99. if ( empty($pass1) && !empty($pass2) )
  100. $errors->add( 'pass', __( '<strong>ERROR</strong>: You entered your new password only once.' ), array( 'form-field' => 'pass1' ) );
  101. elseif ( !empty($pass1) && empty($pass2) )
  102. $errors->add( 'pass', __( '<strong>ERROR</strong>: You entered your new password only once.' ), array( 'form-field' => 'pass2' ) );
  103. } else {
  104. if ( empty($pass1) )
  105. $errors->add( 'pass', __( '<strong>ERROR</strong>: Please enter your password.' ), array( 'form-field' => 'pass1' ) );
  106. elseif ( empty($pass2) )
  107. $errors->add( 'pass', __( '<strong>ERROR</strong>: Please enter your password twice.' ), array( 'form-field' => 'pass2' ) );
  108. }
  109. /* Check for "\" in password */
  110. if ( false !== strpos( stripslashes($pass1), "\\" ) )
  111. $errors->add( 'pass', __( '<strong>ERROR</strong>: Passwords may not contain the character "\\".' ), array( 'form-field' => 'pass1' ) );
  112. /* checking the password has been typed twice the same */
  113. if ( $pass1 != $pass2 )
  114. $errors->add( 'pass', __( '<strong>ERROR</strong>: Please enter the same password in the two password fields.' ), array( 'form-field' => 'pass1' ) );
  115. if ( !empty( $pass1 ) )
  116. $user->user_pass = $pass1;
  117. if ( !$update && isset( $_POST['user_login'] ) && !validate_username( $_POST['user_login'] ) )
  118. $errors->add( 'user_login', __( '<strong>ERROR</strong>: This username is invalid because it uses illegal characters. Please enter a valid username.' ));
  119. if ( !$update && username_exists( $user->user_login ) )
  120. $errors->add( 'user_login', __( '<strong>ERROR</strong>: This username is already registered. Please choose another one.' ));
  121. /* checking e-mail address */
  122. if ( empty( $user->user_email ) ) {
  123. $errors->add( 'empty_email', __( '<strong>ERROR</strong>: Please enter an e-mail address.' ), array( 'form-field' => 'email' ) );
  124. } elseif ( !is_email( $user->user_email ) ) {
  125. $errors->add( 'invalid_email', __( '<strong>ERROR</strong>: The email address isn&#8217;t correct.' ), array( 'form-field' => 'email' ) );
  126. } elseif ( ( $owner_id = email_exists($user->user_email) ) && ( !$update || ( $owner_id != $user->ID ) ) ) {
  127. $errors->add( 'email_exists', __('<strong>ERROR</strong>: This email is already registered, please choose another one.'), array( 'form-field' => 'email' ) );
  128. }
  129. // Allow plugins to return their own errors.
  130. do_action_ref_array( 'user_profile_update_errors', array( &$errors, $update, &$user ) );
  131. if ( $errors->get_error_codes() )
  132. return $errors;
  133. if ( $update ) {
  134. $user_id = wp_update_user( $user );
  135. } else {
  136. $user_id = wp_insert_user( $user );
  137. wp_new_user_notification( $user_id, isset( $_POST['send_password'] ) ? $pass1 : '' );
  138. }
  139. return $user_id;
  140. }
  141. /**
  142. * Fetch a filtered list of user roles that the current user is
  143. * allowed to edit.
  144. *
  145. * Simple function who's main purpose is to allow filtering of the
  146. * list of roles in the $wp_roles object so that plugins can remove
  147. * inappropriate ones depending on the situation or user making edits.
  148. * Specifically because without filtering anyone with the edit_users
  149. * capability can edit others to be administrators, even if they are
  150. * only editors or authors. This filter allows admins to delegate
  151. * user management.
  152. *
  153. * @since 2.8
  154. *
  155. * @return unknown
  156. */
  157. function get_editable_roles() {
  158. global $wp_roles;
  159. $all_roles = $wp_roles->roles;
  160. $editable_roles = apply_filters('editable_roles', $all_roles);
  161. return $editable_roles;
  162. }
  163. /**
  164. * Retrieve user data and filter it.
  165. *
  166. * @since 2.0.5
  167. *
  168. * @param int $user_id User ID.
  169. * @return object WP_User object with user data.
  170. */
  171. function get_user_to_edit( $user_id ) {
  172. $user = get_userdata( $user_id );
  173. $user->filter = 'edit';
  174. return $user;
  175. }
  176. /**
  177. * Retrieve the user's drafts.
  178. *
  179. * @since 2.0.0
  180. *
  181. * @param int $user_id User ID.
  182. * @return array
  183. */
  184. function get_users_drafts( $user_id ) {
  185. global $wpdb;
  186. $query = $wpdb->prepare("SELECT ID, post_title FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'draft' AND post_author = %d ORDER BY post_modified DESC", $user_id);
  187. $query = apply_filters('get_users_drafts', $query);
  188. return $wpdb->get_results( $query );
  189. }
  190. /**
  191. * Remove user and optionally reassign posts and links to another user.
  192. *
  193. * If the $reassign parameter is not assigned to an User ID, then all posts will
  194. * be deleted of that user. The action 'delete_user' that is passed the User ID
  195. * being deleted will be run after the posts are either reassigned or deleted.
  196. * The user meta will also be deleted that are for that User ID.
  197. *
  198. * @since 2.0.0
  199. *
  200. * @param int $id User ID.
  201. * @param int $reassign Optional. Reassign posts and links to new User ID.
  202. * @return bool True when finished.
  203. */
  204. function wp_delete_user( $id, $reassign = 'novalue' ) {
  205. global $wpdb;
  206. $id = (int) $id;
  207. $user = new WP_User( $id );
  208. if ( !$user->exists() )
  209. return false;
  210. // allow for transaction statement
  211. do_action('delete_user', $id);
  212. if ( 'novalue' === $reassign || null === $reassign ) {
  213. $post_types_to_delete = array();
  214. foreach ( get_post_types( array(), 'objects' ) as $post_type ) {
  215. if ( $post_type->delete_with_user ) {
  216. $post_types_to_delete[] = $post_type->name;
  217. } elseif ( null === $post_type->delete_with_user && post_type_supports( $post_type->name, 'author' ) ) {
  218. $post_types_to_delete[] = $post_type->name;
  219. }
  220. }
  221. $post_types_to_delete = apply_filters( 'post_types_to_delete_with_user', $post_types_to_delete, $id );
  222. $post_types_to_delete = implode( "', '", $post_types_to_delete );
  223. $post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_author = %d AND post_type IN ('$post_types_to_delete')", $id ) );
  224. if ( $post_ids ) {
  225. foreach ( $post_ids as $post_id )
  226. wp_delete_post( $post_id );
  227. }
  228. // Clean links
  229. $link_ids = $wpdb->get_col( $wpdb->prepare("SELECT link_id FROM $wpdb->links WHERE link_owner = %d", $id) );
  230. if ( $link_ids ) {
  231. foreach ( $link_ids as $link_id )
  232. wp_delete_link($link_id);
  233. }
  234. } else {
  235. $reassign = (int) $reassign;
  236. $post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_author = %d", $id ) );
  237. $wpdb->update( $wpdb->posts, array('post_author' => $reassign), array('post_author' => $id) );
  238. if ( ! empty( $post_ids ) ) {
  239. foreach ( $post_ids as $post_id )
  240. clean_post_cache( $post_id );
  241. }
  242. $link_ids = $wpdb->get_col( $wpdb->prepare("SELECT link_id FROM $wpdb->links WHERE link_owner = %d", $id) );
  243. $wpdb->update( $wpdb->links, array('link_owner' => $reassign), array('link_owner' => $id) );
  244. if ( ! empty( $link_ids ) ) {
  245. foreach ( $link_ids as $link_id )
  246. clean_bookmark_cache( $link_id );
  247. }
  248. }
  249. // FINALLY, delete user
  250. if ( is_multisite() ) {
  251. remove_user_from_blog( $id, get_current_blog_id() );
  252. } else {
  253. $meta = $wpdb->get_col( $wpdb->prepare( "SELECT umeta_id FROM $wpdb->usermeta WHERE user_id = %d", $id ) );
  254. foreach ( $meta as $mid )
  255. delete_metadata_by_mid( 'user', $mid );
  256. $wpdb->delete( $wpdb->users, array( 'ID' => $id ) );
  257. }
  258. clean_user_cache( $user );
  259. // allow for commit transaction
  260. do_action('deleted_user', $id);
  261. return true;
  262. }
  263. /**
  264. * Remove all capabilities from user.
  265. *
  266. * @since 2.1.0
  267. *
  268. * @param int $id User ID.
  269. */
  270. function wp_revoke_user($id) {
  271. $id = (int) $id;
  272. $user = new WP_User($id);
  273. $user->remove_all_caps();
  274. }
  275. add_action('admin_init', 'default_password_nag_handler');
  276. /**
  277. * @since 2.8.0
  278. */
  279. function default_password_nag_handler($errors = false) {
  280. global $user_ID;
  281. if ( ! get_user_option('default_password_nag') ) //Short circuit it.
  282. return;
  283. //get_user_setting = JS saved UI setting. else no-js-fallback code.
  284. if ( 'hide' == get_user_setting('default_password_nag') || isset($_GET['default_password_nag']) && '0' == $_GET['default_password_nag'] ) {
  285. delete_user_setting('default_password_nag');
  286. update_user_option($user_ID, 'default_password_nag', false, true);
  287. }
  288. }
  289. add_action('profile_update', 'default_password_nag_edit_user', 10, 2);
  290. /**
  291. * @since 2.8.0
  292. */
  293. function default_password_nag_edit_user($user_ID, $old_data) {
  294. if ( ! get_user_option('default_password_nag', $user_ID) ) //Short circuit it.
  295. return;
  296. $new_data = get_userdata($user_ID);
  297. if ( $new_data->user_pass != $old_data->user_pass ) { //Remove the nag if the password has been changed.
  298. delete_user_setting('default_password_nag');
  299. update_user_option($user_ID, 'default_password_nag', false, true);
  300. }
  301. }
  302. add_action('admin_notices', 'default_password_nag');
  303. /**
  304. * @since 2.8.0
  305. */
  306. function default_password_nag() {
  307. global $pagenow;
  308. if ( 'profile.php' == $pagenow || ! get_user_option('default_password_nag') ) //Short circuit it.
  309. return;
  310. echo '<div class="error default-password-nag">';
  311. echo '<p>';
  312. echo '<strong>' . __('Notice:') . '</strong> ';
  313. _e('You&rsquo;re using the auto-generated password for your account. Would you like to change it to something easier to remember?');
  314. echo '</p><p>';
  315. printf( '<a href="%s">' . __('Yes, take me to my profile page') . '</a> | ', get_edit_profile_url( get_current_user_id() ) . '#password' );
  316. printf( '<a href="%s" id="default-password-nag-no">' . __('No thanks, do not remind me again') . '</a>', '?default_password_nag=0' );
  317. echo '</p></div>';
  318. }