home *** CD-ROM | disk | FTP | other *** search
/ HTML Examples / WP.iso / wordpress2 / wp-admin / includes / user.php < prev    next >
Encoding:
PHP Script  |  2017-10-02  |  17.2 KB  |  541 lines

  1. <?php
  2. /**
  3.  * WordPress user administration API.
  4.  *
  5.  * @package WordPress
  6.  * @subpackage Administration
  7.  */
  8.  
  9. /**
  10.  * Creates a new user from the "Users" form using $_POST information.
  11.  *
  12.  * @since 2.0.0
  13.  *
  14.  * @return int|WP_Error WP_Error or User ID.
  15.  */
  16. function add_user() {
  17.     return edit_user();
  18. }
  19.  
  20. /**
  21.  * Edit user settings based on contents of $_POST
  22.  *
  23.  * Used on user-edit.php and profile.php to manage and process user options, passwords etc.
  24.  *
  25.  * @since 2.0.0
  26.  *
  27.  * @param int $user_id Optional. User ID.
  28.  * @return int|WP_Error user id of the updated user
  29.  */
  30. function edit_user( $user_id = 0 ) {
  31.     $wp_roles = wp_roles();
  32.     $user = new stdClass;
  33.     if ( $user_id ) {
  34.         $update = true;
  35.         $user->ID = (int) $user_id;
  36.         $userdata = get_userdata( $user_id );
  37.         $user->user_login = wp_slash( $userdata->user_login );
  38.     } else {
  39.         $update = false;
  40.     }
  41.  
  42.     if ( !$update && isset( $_POST['user_login'] ) )
  43.         $user->user_login = sanitize_user($_POST['user_login'], true);
  44.  
  45.     $pass1 = $pass2 = '';
  46.     if ( isset( $_POST['pass1'] ) )
  47.         $pass1 = $_POST['pass1'];
  48.     if ( isset( $_POST['pass2'] ) )
  49.         $pass2 = $_POST['pass2'];
  50.  
  51.     if ( isset( $_POST['role'] ) && current_user_can( 'edit_users' ) ) {
  52.         $new_role = sanitize_text_field( $_POST['role'] );
  53.         $potential_role = isset($wp_roles->role_objects[$new_role]) ? $wp_roles->role_objects[$new_role] : false;
  54.         // Don't let anyone with 'edit_users' (admins) edit their own role to something without it.
  55.         // Multisite super admins can freely edit their blog roles -- they possess all caps.
  56.         if ( ( is_multisite() && current_user_can( 'manage_sites' ) ) || $user_id != get_current_user_id() || ($potential_role && $potential_role->has_cap( 'edit_users' ) ) )
  57.             $user->role = $new_role;
  58.  
  59.         // If the new role isn't editable by the logged-in user die with error
  60.         $editable_roles = get_editable_roles();
  61.         if ( ! empty( $new_role ) && empty( $editable_roles[$new_role] ) )
  62.             wp_die( __( 'Sorry, you are not allowed to give users that role.' ), 403 );
  63.     }
  64.  
  65.     if ( isset( $_POST['email'] ))
  66.         $user->user_email = sanitize_text_field( wp_unslash( $_POST['email'] ) );
  67.     if ( isset( $_POST['url'] ) ) {
  68.         if ( empty ( $_POST['url'] ) || $_POST['url'] == 'http://' ) {
  69.             $user->user_url = '';
  70.         } else {
  71.             $user->user_url = esc_url_raw( $_POST['url'] );
  72.             $protocols = implode( '|', array_map( 'preg_quote', wp_allowed_protocols() ) );
  73.             $user->user_url = preg_match('/^(' . $protocols . '):/is', $user->user_url) ? $user->user_url : 'http://'.$user->user_url;
  74.         }
  75.     }
  76.     if ( isset( $_POST['first_name'] ) )
  77.         $user->first_name = sanitize_text_field( $_POST['first_name'] );
  78.     if ( isset( $_POST['last_name'] ) )
  79.         $user->last_name = sanitize_text_field( $_POST['last_name'] );
  80.     if ( isset( $_POST['nickname'] ) )
  81.         $user->nickname = sanitize_text_field( $_POST['nickname'] );
  82.     if ( isset( $_POST['display_name'] ) )
  83.         $user->display_name = sanitize_text_field( $_POST['display_name'] );
  84.  
  85.     if ( isset( $_POST['description'] ) )
  86.         $user->description = trim( $_POST['description'] );
  87.  
  88.     foreach ( wp_get_user_contact_methods( $user ) as $method => $name ) {
  89.         if ( isset( $_POST[$method] ))
  90.             $user->$method = sanitize_text_field( $_POST[$method] );
  91.     }
  92.  
  93.     if ( $update ) {
  94.         $user->rich_editing = isset( $_POST['rich_editing'] ) && 'false' === $_POST['rich_editing'] ? 'false' : 'true';
  95.         $user->syntax_highlighting = isset( $_POST['syntax_highlighting'] ) && 'false' === $_POST['syntax_highlighting'] ? 'false' : 'true';
  96.         $user->admin_color = isset( $_POST['admin_color'] ) ? sanitize_text_field( $_POST['admin_color'] ) : 'fresh';
  97.         $user->show_admin_bar_front = isset( $_POST['admin_bar_front'] ) ? 'true' : 'false';
  98.         $user->locale = '';
  99.  
  100.         if ( isset( $_POST['locale'] ) ) {
  101.             $locale = sanitize_text_field( $_POST['locale'] );
  102.             if ( 'site-default' === $locale ) {
  103.                 $locale = '';
  104.             } elseif ( '' === $locale ) {
  105.                 $locale = 'en_US';
  106.             } elseif ( ! in_array( $locale, get_available_languages(), true ) ) {
  107.                 $locale = '';
  108.             }
  109.  
  110.             $user->locale = $locale;
  111.         }
  112.     }
  113.  
  114.     $user->comment_shortcuts = isset( $_POST['comment_shortcuts'] ) && 'true' == $_POST['comment_shortcuts'] ? 'true' : '';
  115.  
  116.     $user->use_ssl = 0;
  117.     if ( !empty($_POST['use_ssl']) )
  118.         $user->use_ssl = 1;
  119.  
  120.     $errors = new WP_Error();
  121.  
  122.     /* checking that username has been typed */
  123.     if ( $user->user_login == '' )
  124.         $errors->add( 'user_login', __( '<strong>ERROR</strong>: Please enter a username.' ) );
  125.  
  126.     /* checking that nickname has been typed */
  127.     if ( $update && empty( $user->nickname ) ) {
  128.         $errors->add( 'nickname', __( '<strong>ERROR</strong>: Please enter a nickname.' ) );
  129.     }
  130.  
  131.     /**
  132.      * Fires before the password and confirm password fields are checked for congruity.
  133.      *
  134.      * @since 1.5.1
  135.      *
  136.      * @param string $user_login The username.
  137.      * @param string $pass1     The password (passed by reference).
  138.      * @param string $pass2     The confirmed password (passed by reference).
  139.      */
  140.     do_action_ref_array( 'check_passwords', array( $user->user_login, &$pass1, &$pass2 ) );
  141.  
  142.     // Check for blank password when adding a user.
  143.     if ( ! $update && empty( $pass1 ) ) {
  144.         $errors->add( 'pass', __( '<strong>ERROR</strong>: Please enter a password.' ), array( 'form-field' => 'pass1' ) );
  145.     }
  146.  
  147.     // Check for "\" in password.
  148.     if ( false !== strpos( wp_unslash( $pass1 ), "\\" ) ) {
  149.         $errors->add( 'pass', __( '<strong>ERROR</strong>: Passwords may not contain the character "\\".' ), array( 'form-field' => 'pass1' ) );
  150.     }
  151.  
  152.     // Checking the password has been typed twice the same.
  153.     if ( ( $update || ! empty( $pass1 ) ) && $pass1 != $pass2 ) {
  154.         $errors->add( 'pass', __( '<strong>ERROR</strong>: Please enter the same password in both password fields.' ), array( 'form-field' => 'pass1' ) );
  155.     }
  156.  
  157.     if ( !empty( $pass1 ) )
  158.         $user->user_pass = $pass1;
  159.  
  160.     if ( !$update && isset( $_POST['user_login'] ) && !validate_username( $_POST['user_login'] ) )
  161.         $errors->add( 'user_login', __( '<strong>ERROR</strong>: This username is invalid because it uses illegal characters. Please enter a valid username.' ));
  162.  
  163.     if ( !$update && username_exists( $user->user_login ) )
  164.         $errors->add( 'user_login', __( '<strong>ERROR</strong>: This username is already registered. Please choose another one.' ));
  165.  
  166.     /** This filter is documented in wp-includes/user.php */
  167.     $illegal_logins = (array) apply_filters( 'illegal_user_logins', array() );
  168.  
  169.     if ( in_array( strtolower( $user->user_login ), array_map( 'strtolower', $illegal_logins ) ) ) {
  170.         $errors->add( 'invalid_username', __( '<strong>ERROR</strong>: Sorry, that username is not allowed.' ) );
  171.     }
  172.  
  173.     /* checking email address */
  174.     if ( empty( $user->user_email ) ) {
  175.         $errors->add( 'empty_email', __( '<strong>ERROR</strong>: Please enter an email address.' ), array( 'form-field' => 'email' ) );
  176.     } elseif ( !is_email( $user->user_email ) ) {
  177.         $errors->add( 'invalid_email', __( '<strong>ERROR</strong>: The email address isn’t correct.' ), array( 'form-field' => 'email' ) );
  178.     } elseif ( ( $owner_id = email_exists($user->user_email) ) && ( !$update || ( $owner_id != $user->ID ) ) ) {
  179.         $errors->add( 'email_exists', __('<strong>ERROR</strong>: This email is already registered, please choose another one.'), array( 'form-field' => 'email' ) );
  180.     }
  181.  
  182.     /**
  183.      * Fires before user profile update errors are returned.
  184.      *
  185.      * @since 2.8.0
  186.      *
  187.      * @param WP_Error $errors WP_Error object (passed by reference).
  188.      * @param bool     $update  Whether this is a user update.
  189.      * @param stdClass $user   User object (passed by reference).
  190.      */
  191.     do_action_ref_array( 'user_profile_update_errors', array( &$errors, $update, &$user ) );
  192.  
  193.     if ( $errors->get_error_codes() )
  194.         return $errors;
  195.  
  196.     if ( $update ) {
  197.         $user_id = wp_update_user( $user );
  198.     } else {
  199.         $user_id = wp_insert_user( $user );
  200.         $notify  = isset( $_POST['send_user_notification'] ) ? 'both' : 'admin';
  201.  
  202.         /**
  203.           * Fires after a new user has been created.
  204.           *
  205.           * @since 4.4.0
  206.           *
  207.           * @param int    $user_id ID of the newly created user.
  208.           * @param string $notify  Type of notification that should happen. See wp_send_new_user_notifications()
  209.           *                        for more information on possible values.
  210.           */
  211.         do_action( 'edit_user_created_user', $user_id, $notify );
  212.     }
  213.     return $user_id;
  214. }
  215.  
  216. /**
  217.  * Fetch a filtered list of user roles that the current user is
  218.  * allowed to edit.
  219.  *
  220.  * Simple function who's main purpose is to allow filtering of the
  221.  * list of roles in the $wp_roles object so that plugins can remove
  222.  * inappropriate ones depending on the situation or user making edits.
  223.  * Specifically because without filtering anyone with the edit_users
  224.  * capability can edit others to be administrators, even if they are
  225.  * only editors or authors. This filter allows admins to delegate
  226.  * user management.
  227.  *
  228.  * @since 2.8.0
  229.  *
  230.  * @return array
  231.  */
  232. function get_editable_roles() {
  233.     $all_roles = wp_roles()->roles;
  234.  
  235.     /**
  236.      * Filters the list of editable roles.
  237.      *
  238.      * @since 2.8.0
  239.      *
  240.      * @param array $all_roles List of roles.
  241.      */
  242.     $editable_roles = apply_filters( 'editable_roles', $all_roles );
  243.  
  244.     return $editable_roles;
  245. }
  246.  
  247. /**
  248.  * Retrieve user data and filter it.
  249.  *
  250.  * @since 2.0.5
  251.  *
  252.  * @param int $user_id User ID.
  253.  * @return WP_User|bool WP_User object on success, false on failure.
  254.  */
  255. function get_user_to_edit( $user_id ) {
  256.     $user = get_userdata( $user_id );
  257.  
  258.     if ( $user )
  259.         $user->filter = 'edit';
  260.  
  261.     return $user;
  262. }
  263.  
  264. /**
  265.  * Retrieve the user's drafts.
  266.  *
  267.  * @since 2.0.0
  268.  *
  269.  * @global wpdb $wpdb WordPress database abstraction object.
  270.  *
  271.  * @param int $user_id User ID.
  272.  * @return array
  273.  */
  274. function get_users_drafts( $user_id ) {
  275.     global $wpdb;
  276.     $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);
  277.  
  278.     /**
  279.      * Filters the user's drafts query string.
  280.      *
  281.      * @since 2.0.0
  282.      *
  283.      * @param string $query The user's drafts query string.
  284.      */
  285.     $query = apply_filters( 'get_users_drafts', $query );
  286.     return $wpdb->get_results( $query );
  287. }
  288.  
  289. /**
  290.  * Remove user and optionally reassign posts and links to another user.
  291.  *
  292.  * If the $reassign parameter is not assigned to a User ID, then all posts will
  293.  * be deleted of that user. The action {@see 'delete_user'} that is passed the User ID
  294.  * being deleted will be run after the posts are either reassigned or deleted.
  295.  * The user meta will also be deleted that are for that User ID.
  296.  *
  297.  * @since 2.0.0
  298.  *
  299.  * @global wpdb $wpdb WordPress database abstraction object.
  300.  *
  301.  * @param int $id User ID.
  302.  * @param int $reassign Optional. Reassign posts and links to new User ID.
  303.  * @return bool True when finished.
  304.  */
  305. function wp_delete_user( $id, $reassign = null ) {
  306.     global $wpdb;
  307.  
  308.     if ( ! is_numeric( $id ) ) {
  309.         return false;
  310.     }
  311.  
  312.     $id = (int) $id;
  313.     $user = new WP_User( $id );
  314.  
  315.     if ( !$user->exists() )
  316.         return false;
  317.  
  318.     // Normalize $reassign to null or a user ID. 'novalue' was an older default.
  319.     if ( 'novalue' === $reassign ) {
  320.         $reassign = null;
  321.     } elseif ( null !== $reassign ) {
  322.         $reassign = (int) $reassign;
  323.     }
  324.  
  325.     /**
  326.      * Fires immediately before a user is deleted from the database.
  327.      *
  328.      * @since 2.0.0
  329.      *
  330.      * @param int      $id       ID of the user to delete.
  331.      * @param int|null $reassign ID of the user to reassign posts and links to.
  332.      *                           Default null, for no reassignment.
  333.      */
  334.     do_action( 'delete_user', $id, $reassign );
  335.  
  336.     if ( null === $reassign ) {
  337.         $post_types_to_delete = array();
  338.         foreach ( get_post_types( array(), 'objects' ) as $post_type ) {
  339.             if ( $post_type->delete_with_user ) {
  340.                 $post_types_to_delete[] = $post_type->name;
  341.             } elseif ( null === $post_type->delete_with_user && post_type_supports( $post_type->name, 'author' ) ) {
  342.                 $post_types_to_delete[] = $post_type->name;
  343.             }
  344.         }
  345.  
  346.         /**
  347.          * Filters the list of post types to delete with a user.
  348.          *
  349.          * @since 3.4.0
  350.          *
  351.          * @param array $post_types_to_delete Post types to delete.
  352.          * @param int   $id                   User ID.
  353.          */
  354.         $post_types_to_delete = apply_filters( 'post_types_to_delete_with_user', $post_types_to_delete, $id );
  355.         $post_types_to_delete = implode( "', '", $post_types_to_delete );
  356.         $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 ) );
  357.         if ( $post_ids ) {
  358.             foreach ( $post_ids as $post_id )
  359.                 wp_delete_post( $post_id );
  360.         }
  361.  
  362.         // Clean links
  363.         $link_ids = $wpdb->get_col( $wpdb->prepare("SELECT link_id FROM $wpdb->links WHERE link_owner = %d", $id) );
  364.  
  365.         if ( $link_ids ) {
  366.             foreach ( $link_ids as $link_id )
  367.                 wp_delete_link($link_id);
  368.         }
  369.     } else {
  370.         $post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_author = %d", $id ) );
  371.         $wpdb->update( $wpdb->posts, array('post_author' => $reassign), array('post_author' => $id) );
  372.         if ( ! empty( $post_ids ) ) {
  373.             foreach ( $post_ids as $post_id )
  374.                 clean_post_cache( $post_id );
  375.         }
  376.         $link_ids = $wpdb->get_col( $wpdb->prepare("SELECT link_id FROM $wpdb->links WHERE link_owner = %d", $id) );
  377.         $wpdb->update( $wpdb->links, array('link_owner' => $reassign), array('link_owner' => $id) );
  378.         if ( ! empty( $link_ids ) ) {
  379.             foreach ( $link_ids as $link_id )
  380.                 clean_bookmark_cache( $link_id );
  381.         }
  382.     }
  383.  
  384.     // FINALLY, delete user
  385.     if ( is_multisite() ) {
  386.         remove_user_from_blog( $id, get_current_blog_id() );
  387.     } else {
  388.         $meta = $wpdb->get_col( $wpdb->prepare( "SELECT umeta_id FROM $wpdb->usermeta WHERE user_id = %d", $id ) );
  389.         foreach ( $meta as $mid )
  390.             delete_metadata_by_mid( 'user', $mid );
  391.  
  392.         $wpdb->delete( $wpdb->users, array( 'ID' => $id ) );
  393.     }
  394.  
  395.     clean_user_cache( $user );
  396.  
  397.     /**
  398.      * Fires immediately after a user is deleted from the database.
  399.      *
  400.      * @since 2.9.0
  401.      *
  402.      * @param int      $id       ID of the deleted user.
  403.      * @param int|null $reassign ID of the user to reassign posts and links to.
  404.      *                           Default null, for no reassignment.
  405.      */
  406.     do_action( 'deleted_user', $id, $reassign );
  407.  
  408.     return true;
  409. }
  410.  
  411. /**
  412.  * Remove all capabilities from user.
  413.  *
  414.  * @since 2.1.0
  415.  *
  416.  * @param int $id User ID.
  417.  */
  418. function wp_revoke_user($id) {
  419.     $id = (int) $id;
  420.  
  421.     $user = new WP_User($id);
  422.     $user->remove_all_caps();
  423. }
  424.  
  425. /**
  426.  * @since 2.8.0
  427.  *
  428.  * @global int $user_ID
  429.  *
  430.  * @param false $errors Deprecated.
  431.  */
  432. function default_password_nag_handler($errors = false) {
  433.     global $user_ID;
  434.     // Short-circuit it.
  435.     if ( ! get_user_option('default_password_nag') )
  436.         return;
  437.  
  438.     // get_user_setting = JS saved UI setting. else no-js-fallback code.
  439.     if ( 'hide' == get_user_setting('default_password_nag') || isset($_GET['default_password_nag']) && '0' == $_GET['default_password_nag'] ) {
  440.         delete_user_setting('default_password_nag');
  441.         update_user_option($user_ID, 'default_password_nag', false, true);
  442.     }
  443. }
  444.  
  445. /**
  446.  * @since 2.8.0
  447.  *
  448.  * @param int    $user_ID
  449.  * @param object $old_data
  450.  */
  451. function default_password_nag_edit_user($user_ID, $old_data) {
  452.     // Short-circuit it.
  453.     if ( ! get_user_option('default_password_nag', $user_ID) )
  454.         return;
  455.  
  456.     $new_data = get_userdata($user_ID);
  457.  
  458.     // Remove the nag if the password has been changed.
  459.     if ( $new_data->user_pass != $old_data->user_pass ) {
  460.         delete_user_setting('default_password_nag');
  461.         update_user_option($user_ID, 'default_password_nag', false, true);
  462.     }
  463. }
  464.  
  465. /**
  466.  * @since 2.8.0
  467.  *
  468.  * @global string $pagenow
  469.  */
  470. function default_password_nag() {
  471.     global $pagenow;
  472.     // Short-circuit it.
  473.     if ( 'profile.php' == $pagenow || ! get_user_option('default_password_nag') )
  474.         return;
  475.  
  476.     echo '<div class="error default-password-nag">';
  477.     echo '<p>';
  478.     echo '<strong>' . __('Notice:') . '</strong> ';
  479.     _e('You’re using the auto-generated password for your account. Would you like to change it?');
  480.     echo '</p><p>';
  481.     printf( '<a href="%s">' . __('Yes, take me to my profile page') . '</a> | ', get_edit_profile_url() . '#password' );
  482.     printf( '<a href="%s" id="default-password-nag-no">' . __('No thanks, do not remind me again') . '</a>', '?default_password_nag=0' );
  483.     echo '</p></div>';
  484. }
  485.  
  486. /**
  487.  * @since 3.5.0
  488.  * @access private
  489.  */
  490. function delete_users_add_js() { ?>
  491. <script>
  492. jQuery(document).ready( function($) {
  493.     var submit = $('#submit').prop('disabled', true);
  494.     $('input[name="delete_option"]').one('change', function() {
  495.         submit.prop('disabled', false);
  496.     });
  497.     $('#reassign_user').focus( function() {
  498.         $('#delete_option1').prop('checked', true).trigger('change');
  499.     });
  500. });
  501. </script>
  502. <?php
  503. }
  504.  
  505. /**
  506.  * Optional SSL preference that can be turned on by hooking to the 'personal_options' action.
  507.  *
  508.  * See the {@see 'personal_options'} action.
  509.  *
  510.  * @since 2.7.0
  511.  *
  512.  * @param object $user User data object
  513.  */
  514. function use_ssl_preference($user) {
  515. ?>
  516.     <tr class="user-use-ssl-wrap">
  517.         <th scope="row"><?php _e('Use https')?></th>
  518.         <td><label for="use_ssl"><input name="use_ssl" type="checkbox" id="use_ssl" value="1" <?php checked('1', $user->use_ssl); ?> /> <?php _e('Always use https when visiting the admin'); ?></label></td>
  519.     </tr>
  520. <?php
  521. }
  522.  
  523. /**
  524.  *
  525.  * @param string $text
  526.  * @return string
  527.  */
  528. function admin_created_user_email( $text ) {
  529.     $roles = get_editable_roles();
  530.     $role = $roles[ $_REQUEST['role'] ];
  531.     /* translators: 1: Site name, 2: site URL, 3: role */
  532.     return sprintf( __( 'Hi,
  533. You\'ve been invited to join \'%1$s\' at
  534. %2$s with the role of %3$s.
  535. If you do not want to join this site please ignore
  536. this email. This invitation will expire in a few days.
  537.  
  538. Please click the following link to activate your user account:
  539. %%s' ), wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ), home_url(), wp_specialchars_decode( translate_user_role( $role['name'] ) ) );
  540. }
  541.