| 1706 | /** |
| 1707 | * Create a new user |
| 1708 | * |
| 1709 | * @uses wp_insert_user() |
| 1710 | * @param array $args Method parameters. Contains: |
| 1711 | * - int $blog_id |
| 1712 | * - string $username |
| 1713 | * - string $password |
| 1714 | * - array $content_struct. |
| 1715 | * The $content_struct must contain: |
| 1716 | * - 'username' |
| 1717 | * - 'password' |
| 1718 | * - 'email' |
| 1719 | * Also, it can optionally contain: |
| 1720 | * - 'role' |
| 1721 | * - 'first_name' |
| 1722 | * - 'last_name' |
| 1723 | * - 'website' |
| 1724 | * - boolean $send_mail optional. Defaults to false |
| 1725 | * @return int user_id |
| 1726 | */ |
| 1727 | function wp_newUser( $args ) { |
| 1728 | $this->escape($args); |
| 1729 | |
| 1730 | $blog_id = (int) $args[0]; |
| 1731 | $username = $args[1]; |
| 1732 | $password = $args[2]; |
| 1733 | $content_struct = $args[3]; |
| 1734 | $send_mail = isset( $args[4] ) ? $args[4] : false; |
| 1735 | |
| 1736 | if ( ! $user = $this->login( $username, $password ) ) |
| 1737 | return $this->error; |
| 1738 | |
| 1739 | do_action( 'xmlrpc_call', 'wp.newUser' ); |
| 1740 | |
| 1741 | if ( ! current_user_can( 'create_users' ) ) |
| 1742 | return new IXR_Error( 401, __( 'You are not allowed to create users.' ) ); |
| 1743 | |
| 1744 | // this hold all the user data |
| 1745 | $user_data = array(); |
| 1746 | |
| 1747 | if( empty ( $content_struct['username'] ) ) |
| 1748 | return new IXR_Error( 403, __( 'Username cannot be empty.' ) ); |
| 1749 | $user_data['user_login'] = $content_struct['username']; |
| 1750 | |
| 1751 | if( empty ( $content_struct['password'] ) ) |
| 1752 | return new IXR_Error( 403, __( 'Password cannot be empty.' ) ); |
| 1753 | $user_data['user_pass'] = $content_struct['password']; |
| 1754 | |
| 1755 | if( empty ( $content_struct['email'] ) ) |
| 1756 | return new IXR_Error( 403, __( 'Email cannot be empty.' ) ); |
| 1757 | |
| 1758 | if( ! is_email( $content_struct['email'] ) ) |
| 1759 | return new IXR_Error( 403, __( 'This email address is not valid.' ) ); |
| 1760 | |
| 1761 | if( email_exists( $content_struct['email'] ) ) |
| 1762 | return new IXR_Error( 403, __( 'This email address is already registered.' ) ); |
| 1763 | |
| 1764 | $user_data['user_email'] = $content_struct['email']; |
| 1765 | |
| 1766 | if( isset ( $content_struct['role'] ) ) { |
| 1767 | if ( get_role( $content_struct['role'] ) === null ) |
| 1768 | return new IXR_Error( 403, __( 'The role specified is not valid.' ) ); |
| 1769 | |
| 1770 | $user_data['role'] = $content_struct['role']; |
| 1771 | } |
| 1772 | |
| 1773 | if( isset ( $content_struct['first_name'] ) ) |
| 1774 | $user_data['first_name'] = $content_struct['first_name']; |
| 1775 | |
| 1776 | if( isset ( $content_struct['last_name'] ) ) |
| 1777 | $user_data['last_name'] = $content_struct['last_name']; |
| 1778 | |
| 1779 | if( isset ( $content_struct['url'] ) ) |
| 1780 | $user_data['user_url'] = $content_struct['url']; |
| 1781 | |
| 1782 | $user_id = wp_insert_user( $user_data ); |
| 1783 | |
| 1784 | if ( is_wp_error( $user_id ) ) |
| 1785 | return new IXR_Error( 500, $user_id->get_error_message() ); |
| 1786 | |
| 1787 | if ( ! $user_id ) |
| 1788 | return new IXR_Error( 500, __( 'Sorry, the new user failed.' ) ); |
| 1789 | |
| 1790 | if ( $send_mail ) { |
| 1791 | wp_new_user_notification( $user_id, $user_data['user_pass'] ); |
| 1792 | } |
| 1793 | |
| 1794 | return $user_id; |
| 1795 | } |
| 1796 | |