| 1706 | /** |
| 1707 | * Edit a new user |
| 1708 | * |
| 1709 | * @uses wp_update_user() |
| 1710 | * @param array $args Method parameters. Contains: |
| 1711 | * - int $blog_id |
| 1712 | * - string $username |
| 1713 | * - string $password |
| 1714 | * - int $user_id |
| 1715 | * - array $content_struct. |
| 1716 | * It can optionally contain: |
| 1717 | * - 'email' |
| 1718 | * - 'first_name' |
| 1719 | * - 'last_name' |
| 1720 | * - 'website' |
| 1721 | * - 'role' |
| 1722 | * - 'nickname' |
| 1723 | * - 'usernicename' |
| 1724 | * - 'bio' |
| 1725 | * - 'usercontacts' |
| 1726 | * - 'password' |
| 1727 | * - boolean $send_mail optional. Defaults to false |
| 1728 | * @return int user_id |
| 1729 | */ |
| 1730 | function wp_editUser( $args ) { |
| 1731 | $this->escape( $args ); |
| 1732 | |
| 1733 | $blog_id = (int) $args[0]; |
| 1734 | $username = $args[1]; |
| 1735 | $password = $args[2]; |
| 1736 | $user_id = (int) $args[3]; |
| 1737 | $content_struct = $args[4]; |
| 1738 | |
| 1739 | if ( ! $user = $this->login( $username, $password ) ) |
| 1740 | return $this->error; |
| 1741 | |
| 1742 | do_action( 'xmlrpc_call', 'wp.editUser' ); |
| 1743 | |
| 1744 | $user_info = get_userdata( $user_id ); |
| 1745 | |
| 1746 | if( ! $user_info ) |
| 1747 | return new IXR_Error( 404, __( 'Invalid user ID.' ) ); |
| 1748 | |
| 1749 | if( ! ( $user_id == $user->ID || current_user_can( 'edit_users' ) ) ) |
| 1750 | return new IXR_Error(401, __( 'Sorry, you cannot edit this user.' ) ); |
| 1751 | |
| 1752 | // holds data of the user |
| 1753 | $user_data = array(); |
| 1754 | $user_data['ID'] = $user_id; |
| 1755 | |
| 1756 | if ( isset( $content_struct['username'] ) && $content_struct['username'] !== $user_info->user_login ) |
| 1757 | return new IXR_Error( 401, __( 'Username cannot be changed.' ) ); |
| 1758 | |
| 1759 | if ( isset( $content_struct['email'] ) ) { |
| 1760 | if( ! is_email( $content_struct['email'] ) ) |
| 1761 | return new IXR_Error( 403, __( 'This email address is not valid.' ) ); |
| 1762 | |
| 1763 | // check whether it is already registered |
| 1764 | if( $content_struct['email'] !== $user_info->user_email && email_exists( $content_struct['email'] ) ) |
| 1765 | return new IXR_Error( 403, __( 'This email address is already registered.' ) ); |
| 1766 | |
| 1767 | $user_data['user_email'] = $content_struct['email']; |
| 1768 | } |
| 1769 | |
| 1770 | if( isset ( $content_struct['role'] ) ) { |
| 1771 | if ( ! current_user_can( 'edit_users' ) ) |
| 1772 | return new IXR_Error( 401, __( 'You are not allowed to change roles for this user.' ) ); |
| 1773 | |
| 1774 | if ( get_role( $content_struct['role'] ) === null ) |
| 1775 | return new IXR_Error( 403, __( 'The role specified is not valid' ) ); |
| 1776 | |
| 1777 | $user_data['role'] = $content_struct['role']; |
| 1778 | } |
| 1779 | |
| 1780 | // only set the user details if it was given |
| 1781 | if ( isset( $content_struct['first_name'] ) ) |
| 1782 | $user_data['first_name'] = $content_struct['first_name']; |
| 1783 | |
| 1784 | if ( isset( $content_struct['last_name'] ) ) |
| 1785 | $user_data['last_name'] = $content_struct['last_name']; |
| 1786 | |
| 1787 | if ( isset( $content_struct['website'] ) ) |
| 1788 | $user_data['user_url'] = $content_struct['url']; |
| 1789 | |
| 1790 | if ( isset( $content_struct['nickname'] ) ) |
| 1791 | $user_data['nickname'] = $content_struct['nickname']; |
| 1792 | |
| 1793 | if ( isset( $content_struct['usernicename'] ) ) |
| 1794 | $user_data['user_nicename'] = $content_struct['nicename']; |
| 1795 | |
| 1796 | if ( isset( $content_struct['bio'] ) ) |
| 1797 | $user_data['description'] = $content_struct['bio']; |
| 1798 | |
| 1799 | if( isset ( $content_struct['user_contacts'] ) ) { |
| 1800 | $user_contacts = _wp_get_user_contactmethods( $user_data ); |
| 1801 | foreach( $content_struct['user_contacts'] as $key => $value ) { |
| 1802 | if( ! array_key_exists( $key, $user_contacts ) ) |
| 1803 | return new IXR_Error( 403, __( 'One of the contact method specified is not valid' ) ); |
| 1804 | |
| 1805 | $user_data[ $key ] = $value; |
| 1806 | } |
| 1807 | } |
| 1808 | |
| 1809 | if( isset ( $content_struct['password'] ) ) |
| 1810 | $user_data['user_pass'] = $content_struct['password']; |
| 1811 | |
| 1812 | $result = wp_update_user( $user_data ); |
| 1813 | |
| 1814 | if ( is_wp_error( $result ) ) |
| 1815 | return new IXR_Error( 500, $result->get_error_message() ); |
| 1816 | |
| 1817 | if ( ! $result ) |
| 1818 | return new IXR_Error( 500, __( 'Sorry, the user cannot be updated. Something wrong happened.' ) ); |
| 1819 | |
| 1820 | return $result; |
| 1821 | } |
| 1822 | |