| 1706 | /** |
| 1707 | * Edit a new user |
| 1708 | * |
| 1709 | * @uses wp_update_user() |
| 1710 | * @param array $args Method parameters. Contains: |
| 1711 | * - int $blog_id |
| 1712 | * - int $user_id |
| 1713 | * - string $username |
| 1714 | * - string $password |
| 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 string user_id |
| 1729 | */ |
| 1730 | function wp_editUser( $args ) { |
| 1731 | |
| 1732 | global $wp_roles; |
| 1733 | $this->escape( $args ); |
| 1734 | |
| 1735 | $blog_ID = (int) $args[0]; |
| 1736 | $user_ID = (int) $args[1]; |
| 1737 | $username = $args[2]; |
| 1738 | $password = $args[3]; |
| 1739 | $content_struct = $args[4]; |
| 1740 | |
| 1741 | if ( ! $user = $this->login( $username, $password ) ) |
| 1742 | return $this->error; |
| 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['user_login'] ) ) |
| 1757 | return new IXR_Error(401, __('Username cannot be changed')); |
| 1758 | |
| 1759 | if ( isset( $content_struct['user_email'] ) ) { |
| 1760 | |
| 1761 | if( ! is_email( $content_struct['user_email'] ) ) |
| 1762 | return new IXR_Error( 403, __( 'Email id is not valid' ) ); |
| 1763 | // check whether it is already registered |
| 1764 | if( email_exists( $content_struct['user_email'] ) ) |
| 1765 | return new IXR_Error( 403, __( 'This email address is already registered' ) ); |
| 1766 | $user_data['user_email'] = $content_struct['user_email']; |
| 1767 | |
| 1768 | } |
| 1769 | |
| 1770 | if( isset ( $content_struct['role'] ) ) { |
| 1771 | |
| 1772 | if ( ! current_user_can( 'edit_users' ) ) |
| 1773 | return new IXR_Error( 401, __( 'You are not allowed to change roles for this user' ) ); |
| 1774 | |
| 1775 | if( ! isset ( $wp_roles ) ) |
| 1776 | $wp_roles = new WP_Roles (); |
| 1777 | if( !array_key_exists( $content_struct['role'], $wp_roles->get_names() ) ) |
| 1778 | return new IXR_Error( 403, __( 'The role specified is not valid' ) ); |
| 1779 | $user_data['role'] = $content_struct['role']; |
| 1780 | |
| 1781 | } |
| 1782 | |
| 1783 | // only set the user details if it was given |
| 1784 | if ( isset( $content_struct['first_name'] ) ) |
| 1785 | $user_data['first_name'] = $content_struct['first_name']; |
| 1786 | |
| 1787 | if ( isset( $content_struct['last_name'] ) ) |
| 1788 | $user_data['last_name'] = $content_struct['last_name']; |
| 1789 | |
| 1790 | if ( isset( $content_struct['user_url'] ) ) |
| 1791 | $user_data['user_url'] = $content_struct['user_url']; |
| 1792 | |
| 1793 | if ( isset( $content_struct['nickname'] ) ) |
| 1794 | $user_data['nickname'] = $content_struct['nickname']; |
| 1795 | |
| 1796 | if ( isset( $content_struct['user_nicename'] ) ) |
| 1797 | $user_data['user_nicename'] = $content_struct['user_nicename']; |
| 1798 | |
| 1799 | if ( isset( $content_struct['description'] ) ) |
| 1800 | $user_data['description'] = $content_struct['description']; |
| 1801 | |
| 1802 | if( isset ( $content_struct['usercontacts'] ) ) { |
| 1803 | |
| 1804 | $user_contacts = _wp_get_user_contactmethods( $user_data ); |
| 1805 | foreach( $content_struct['usercontacts'] as $key => $value ) { |
| 1806 | |
| 1807 | if( ! array_key_exists( $key, $user_contacts ) ) |
| 1808 | return new IXR_Error( 401, __( 'One of the contact method specified is not valid' ) ); |
| 1809 | $user_data[ $key ] = $value; |
| 1810 | |
| 1811 | } |
| 1812 | |
| 1813 | } |
| 1814 | |
| 1815 | if( isset ( $content_struct['user_pass'] ) ) |
| 1816 | $user_data['user_pass'] = $content_struct['user_pass']; |
| 1817 | |
| 1818 | $result = wp_update_user( $user_data ); |
| 1819 | |
| 1820 | if ( is_wp_error( $result ) ) |
| 1821 | return new IXR_Error( 500, $result->get_error_message() ); |
| 1822 | |
| 1823 | if ( ! $result ) |
| 1824 | return new IXR_Error( 500, __( 'Sorry, the user cannot be updated. Something wrong happened.' ) ); |
| 1825 | |
| 1826 | return $result; |
| 1827 | |
| 1828 | } |
| 1829 | |