PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/includes/user.php

https://github.com/sharpmachine/wakeupmedia.com
PHP | 358 lines | 206 code | 53 blank | 99 comment | 79 complexity | fed9ff7afb6283509ac2b14de740a30c 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 = $wpdb->escape( $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 = $_POST['pass1'];
  44. if ( isset( $_POST['pass2'] ))
  45. $pass2 = $_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. $user->user_url = preg_match('/^(https?|ftps?|mailto|news|irc|gopher|nntp|feed|telnet):/is', $user->user_url) ? $user->user_url : 'http://'.$user->user_url;
  66. }
  67. }
  68. if ( isset( $_POST['first_name'] ) )
  69. $user->first_name = sanitize_text_field( $_POST['first_name'] );
  70. if ( isset( $_POST['last_name'] ) )
  71. $user->last_name = sanitize_text_field( $_POST['last_name'] );
  72. if ( isset( $_POST['nickname'] ) )
  73. $user->nickname = sanitize_text_field( $_POST['nickname'] );
  74. if ( isset( $_POST['display_name'] ) )
  75. $user->display_name = sanitize_text_field( $_POST['display_name'] );
  76. if ( isset( $_POST['description'] ) )
  77. $user->description = trim( $_POST['description'] );
  78. foreach ( _wp_get_user_contactmethods( $user ) as $method => $name ) {
  79. if ( isset( $_POST[$method] ))
  80. $user->$method = sanitize_text_field( $_POST[$method] );
  81. }
  82. if ( $update ) {
  83. $user->rich_editing = isset( $_POST['rich_editing'] ) && 'false' == $_POST['rich_editing'] ? 'false' : 'true';
  84. $user->admin_color = isset( $_POST['admin_color'] ) ? sanitize_text_field( $_POST['admin_color'] ) : 'fresh';
  85. $user->show_admin_bar_front = isset( $_POST['admin_bar_front'] ) ? 'true' : 'false';
  86. }
  87. $user->comment_shortcuts = isset( $_POST['comment_shortcuts'] ) && 'true' == $_POST['comment_shortcuts'] ? 'true' : '';
  88. $user->use_ssl = 0;
  89. if ( !empty($_POST['use_ssl']) )
  90. $user->use_ssl = 1;
  91. $errors = new WP_Error();
  92. /* checking that username has been typed */
  93. if ( $user->user_login == '' )
  94. $errors->add( 'user_login', __( '<strong>ERROR</strong>: Please enter a username.' ));
  95. /* checking the password has been typed twice */
  96. do_action_ref_array( 'check_passwords', array ( $user->user_login, & $pass1, & $pass2 ));
  97. if ( $update ) {
  98. if ( empty($pass1) && !empty($pass2) )
  99. $errors->add( 'pass', __( '<strong>ERROR</strong>: You entered your new password only once.' ), array( 'form-field' => 'pass1' ) );
  100. elseif ( !empty($pass1) && empty($pass2) )
  101. $errors->add( 'pass', __( '<strong>ERROR</strong>: You entered your new password only once.' ), array( 'form-field' => 'pass2' ) );
  102. } else {
  103. if ( empty($pass1) )
  104. $errors->add( 'pass', __( '<strong>ERROR</strong>: Please enter your password.' ), array( 'form-field' => 'pass1' ) );
  105. elseif ( empty($pass2) )
  106. $errors->add( 'pass', __( '<strong>ERROR</strong>: Please enter your password twice.' ), array( 'form-field' => 'pass2' ) );
  107. }
  108. /* Check for "\" in password */
  109. if ( false !== strpos( stripslashes($pass1), "\\" ) )
  110. $errors->add( 'pass', __( '<strong>ERROR</strong>: Passwords may not contain the character "\\".' ), array( 'form-field' => 'pass1' ) );
  111. /* checking the password has been typed twice the same */
  112. if ( $pass1 != $pass2 )
  113. $errors->add( 'pass', __( '<strong>ERROR</strong>: Please enter the same password in the two password fields.' ), array( 'form-field' => 'pass1' ) );
  114. if ( !empty( $pass1 ) )
  115. $user->user_pass = $pass1;
  116. if ( !$update && isset( $_POST['user_login'] ) && !validate_username( $_POST['user_login'] ) )
  117. $errors->add( 'user_login', __( '<strong>ERROR</strong>: This username is invalid because it uses illegal characters. Please enter a valid username.' ));
  118. if ( !$update && username_exists( $user->user_login ) )
  119. $errors->add( 'user_login', __( '<strong>ERROR</strong>: This username is already registered. Please choose another one.' ));
  120. /* checking e-mail address */
  121. if ( empty( $user->user_email ) ) {
  122. $errors->add( 'empty_email', __( '<strong>ERROR</strong>: Please enter an e-mail address.' ), array( 'form-field' => 'email' ) );
  123. } elseif ( !is_email( $user->user_email ) ) {
  124. $errors->add( 'invalid_email', __( '<strong>ERROR</strong>: The e-mail address isn&#8217;t correct.' ), array( 'form-field' => 'email' ) );
  125. } elseif ( ( $owner_id = email_exists($user->user_email) ) && ( !$update || ( $owner_id != $user->ID ) ) ) {
  126. $errors->add( 'email_exists', __('<strong>ERROR</strong>: This email is already registered, please choose another one.'), array( 'form-field' => 'email' ) );
  127. }
  128. // Allow plugins to return their own errors.
  129. do_action_ref_array('user_profile_update_errors', array ( &$errors, $update, &$user ) );
  130. if ( $errors->get_error_codes() )
  131. return $errors;
  132. if ( $update ) {
  133. $user_id = wp_update_user( get_object_vars( $user ) );
  134. } else {
  135. $user_id = wp_insert_user( get_object_vars( $user ) );
  136. wp_new_user_notification( $user_id, isset($_POST['send_password']) ? $pass1 : '' );
  137. }
  138. return $user_id;
  139. }
  140. /**
  141. * Fetch a filtered list of user roles that the current user is
  142. * allowed to edit.
  143. *
  144. * Simple function who's main purpose is to allow filtering of the
  145. * list of roles in the $wp_roles object so that plugins can remove
  146. * inappropriate ones depending on the situation or user making edits.
  147. * Specifically because without filtering anyone with the edit_users
  148. * capability can edit others to be administrators, even if they are
  149. * only editors or authors. This filter allows admins to delegate
  150. * user management.
  151. *
  152. * @since 2.8
  153. *
  154. * @return unknown
  155. */
  156. function get_editable_roles() {
  157. global $wp_roles;
  158. $all_roles = $wp_roles->roles;
  159. $editable_roles = apply_filters('editable_roles', $all_roles);
  160. return $editable_roles;
  161. }
  162. /**
  163. * Retrieve user data and filter it.
  164. *
  165. * @since 2.0.5
  166. *
  167. * @param int $user_id User ID.
  168. * @return object WP_User object with user data.
  169. */
  170. function get_user_to_edit( $user_id ) {
  171. $user = new WP_User( $user_id );
  172. $user->filter = 'edit';
  173. return $user;
  174. }
  175. /**
  176. * Retrieve the user's drafts.
  177. *
  178. * @since 2.0.0
  179. *
  180. * @param int $user_id User ID.
  181. * @return array
  182. */
  183. function get_users_drafts( $user_id ) {
  184. global $wpdb;
  185. $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);
  186. $query = apply_filters('get_users_drafts', $query);
  187. return $wpdb->get_results( $query );
  188. }
  189. /**
  190. * Remove user and optionally reassign posts and links to another user.
  191. *
  192. * If the $reassign parameter is not assigned to an User ID, then all posts will
  193. * be deleted of that user. The action 'delete_user' that is passed the User ID
  194. * being deleted will be run after the posts are either reassigned or deleted.
  195. * The user meta will also be deleted that are for that User ID.
  196. *
  197. * @since 2.0.0
  198. *
  199. * @param int $id User ID.
  200. * @param int $reassign Optional. Reassign posts and links to new User ID.
  201. * @return bool True when finished.
  202. */
  203. function wp_delete_user( $id, $reassign = 'novalue' ) {
  204. global $wpdb;
  205. $id = (int) $id;
  206. $user = new WP_User( $id );
  207. // allow for transaction statement
  208. do_action('delete_user', $id);
  209. if ( 'novalue' === $reassign || null === $reassign ) {
  210. $post_types_to_delete = array();
  211. foreach ( get_post_types( array(), 'objects' ) as $post_type ) {
  212. if ( $post_type->delete_with_user ) {
  213. $post_types_to_delete[] = $post_type->name;
  214. } elseif ( null === $post_type->delete_with_user && post_type_supports( $post_type->name, 'author' ) ) {
  215. $post_types_to_delete[] = $post_type->name;
  216. }
  217. }
  218. $post_types_to_delete = apply_filters( 'post_types_to_delete_with_user', $post_types_to_delete, $id );
  219. $post_types_to_delete = implode( "', '", $post_types_to_delete );
  220. $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 ) );
  221. if ( $post_ids ) {
  222. foreach ( $post_ids as $post_id )
  223. wp_delete_post( $post_id );
  224. }
  225. // Clean links
  226. $link_ids = $wpdb->get_col( $wpdb->prepare("SELECT link_id FROM $wpdb->links WHERE link_owner = %d", $id) );
  227. if ( $link_ids ) {
  228. foreach ( $link_ids as $link_id )
  229. wp_delete_link($link_id);
  230. }
  231. } else {
  232. $reassign = (int) $reassign;
  233. $wpdb->update( $wpdb->posts, array('post_author' => $reassign), array('post_author' => $id) );
  234. $wpdb->update( $wpdb->links, array('link_owner' => $reassign), array('link_owner' => $id) );
  235. }
  236. // FINALLY, delete user
  237. if ( is_multisite() ) {
  238. remove_user_from_blog( $id, get_current_blog_id() );
  239. } else {
  240. $meta = $wpdb->get_col( $wpdb->prepare( "SELECT umeta_id FROM $wpdb->usermeta WHERE user_id = %d", $id ) );
  241. foreach ( $meta as $mid )
  242. delete_metadata_by_mid( 'user', $mid );
  243. $wpdb->delete( $wpdb->users, array( 'ID' => $id ) );
  244. }
  245. clean_user_cache( $user );
  246. // allow for commit transaction
  247. do_action('deleted_user', $id);
  248. return true;
  249. }
  250. /**
  251. * Remove all capabilities from user.
  252. *
  253. * @since 2.1.0
  254. *
  255. * @param int $id User ID.
  256. */
  257. function wp_revoke_user($id) {
  258. $id = (int) $id;
  259. $user = new WP_User($id);
  260. $user->remove_all_caps();
  261. }
  262. add_action('admin_init', 'default_password_nag_handler');
  263. /**
  264. * @since 2.8.0
  265. */
  266. function default_password_nag_handler($errors = false) {
  267. global $user_ID;
  268. if ( ! get_user_option('default_password_nag') ) //Short circuit it.
  269. return;
  270. //get_user_setting = JS saved UI setting. else no-js-fallback code.
  271. if ( 'hide' == get_user_setting('default_password_nag') || isset($_GET['default_password_nag']) && '0' == $_GET['default_password_nag'] ) {
  272. delete_user_setting('default_password_nag');
  273. update_user_option($user_ID, 'default_password_nag', false, true);
  274. }
  275. }
  276. add_action('profile_update', 'default_password_nag_edit_user', 10, 2);
  277. /**
  278. * @since 2.8.0
  279. */
  280. function default_password_nag_edit_user($user_ID, $old_data) {
  281. if ( ! get_user_option('default_password_nag', $user_ID) ) //Short circuit it.
  282. return;
  283. $new_data = get_userdata($user_ID);
  284. if ( $new_data->user_pass != $old_data->user_pass ) { //Remove the nag if the password has been changed.
  285. delete_user_setting('default_password_nag', $user_ID);
  286. update_user_option($user_ID, 'default_password_nag', false, true);
  287. }
  288. }
  289. add_action('admin_notices', 'default_password_nag');
  290. /**
  291. * @since 2.8.0
  292. */
  293. function default_password_nag() {
  294. global $pagenow;
  295. if ( 'profile.php' == $pagenow || ! get_user_option('default_password_nag') ) //Short circuit it.
  296. return;
  297. echo '<div class="error default-password-nag">';
  298. echo '<p>';
  299. echo '<strong>' . __('Notice:') . '</strong> ';
  300. _e('You&rsquo;re using the auto-generated password for your account. Would you like to change it to something easier to remember?');
  301. echo '</p><p>';
  302. printf( '<a href="%s">' . __('Yes, take me to my profile page') . '</a> | ', admin_url('profile.php') . '#password' );
  303. printf( '<a href="%s" id="default-password-nag-no">' . __('No thanks, do not remind me again') . '</a>', '?default_password_nag=0' );
  304. echo '</p></div>';
  305. }