| 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 string user_id |
| 1726 | */ |
| 1727 | function wp_newUser( $args ) { |
| 1728 | |
| 1729 | $this->escape( $args ); |
| 1730 | |
| 1731 | $blog_ID = (int) $args[0]; // for future use |
| 1732 | $username = $args[1]; |
| 1733 | $password = $args[2]; |
| 1734 | $content_struct = $args[3]; |
| 1735 | $send_mail = isset( $args[4] ) ? $args[4] : false; |
| 1736 | |
| 1737 | if ( ! $user = $this->login( $username, $password ) ) |
| 1738 | return $this->error; |
| 1739 | |
| 1740 | if ( ! current_user_can( 'create_users' ) ) |
| 1741 | return new IXR_Error( 401, __( 'You are not allowed to create users' ) ); |
| 1742 | |
| 1743 | // this hold all the user data |
| 1744 | $user_data = array(); |
| 1745 | |
| 1746 | $user_data['user_login'] = ''; |
| 1747 | if( isset ( $content_struct['user_login'] ) ) { |
| 1748 | |
| 1749 | $user_data['user_login'] = sanitize_user( $content_struct['user_login'] ); |
| 1750 | //Remove any non-printable chars from the login string to see if we have ended up with an empty username |
| 1751 | $user_data['user_login'] = trim( $user_data['user_login'] ); |
| 1752 | |
| 1753 | } |
| 1754 | |
| 1755 | if( empty ( $user_data['user_login'] ) ) |
| 1756 | return new IXR_Error( 403, __( 'Cannot create a user with an empty login name. ' ) ); |
| 1757 | if( username_exists ( $user_data['user_login'] ) ) |
| 1758 | return new IXR_Error( 403, __( 'This username is already registered.' ) ); |
| 1759 | |
| 1760 | //password cannot be empty |
| 1761 | if( empty ( $content_struct['user_pass'] ) ) |
| 1762 | return new IXR_Error( 403, __( 'password cannot be empty' ) ); |
| 1763 | |
| 1764 | $user_data['user_pass'] = $content_struct['user_pass']; |
| 1765 | |
| 1766 | // check whether email address is valid |
| 1767 | if( ! is_email( $content_struct['user_email'] ) ) |
| 1768 | return new IXR_Error( 403, __( 'email id is not valid' ) ); |
| 1769 | |
| 1770 | // check whether it is already registered |
| 1771 | if( email_exists( $content_struct['user_email'] ) ) |
| 1772 | return new IXR_Error( 403, __( 'This email address is already registered' ) ); |
| 1773 | |
| 1774 | $user_data['user_email'] = $content_struct['user_email']; |
| 1775 | |
| 1776 | // If no role is specified default role is used |
| 1777 | $user_data['role'] = get_option('default_role'); |
| 1778 | if( isset ( $content_struct['role'] ) ) { |
| 1779 | |
| 1780 | if( ! isset ( $wp_roles ) ) |
| 1781 | $wp_roles = new WP_Roles (); |
| 1782 | if( ! array_key_exists( $content_struct['role'], $wp_roles->get_names() ) ) |
| 1783 | return new IXR_Error( 403, __( 'The role specified is not valid' ) ); |
| 1784 | $user_data['role'] = $content_struct['role']; |
| 1785 | |
| 1786 | } |
| 1787 | |
| 1788 | $user_data['first_name'] = ''; |
| 1789 | if( isset ( $content_struct['first_name'] ) ) |
| 1790 | $user_data['first_name'] = $content_struct['first_name']; |
| 1791 | |
| 1792 | $user_data['last_name'] = ''; |
| 1793 | if( isset ( $content_struct['last_name'] ) ) |
| 1794 | $user_data['last_name'] = $content_struct['last_name']; |
| 1795 | |
| 1796 | $user_data['user_url'] = ''; |
| 1797 | if( isset ( $content_struct['user_url'] ) ) |
| 1798 | $user_data['user_url'] = $content_struct['user_url']; |
| 1799 | |
| 1800 | $user_id = wp_insert_user( $user_data ); |
| 1801 | |
| 1802 | if ( is_wp_error( $user_id ) ) |
| 1803 | return new IXR_Error( 500, $user_id->get_error_message() ); |
| 1804 | |
| 1805 | if ( ! $user_id ) |
| 1806 | return new IXR_Error( 500, __( 'Sorry, your entry could not be posted. Something wrong happened.' ) ); |
| 1807 | |
| 1808 | if( $send_mail ) { |
| 1809 | |
| 1810 | $subject = "[".get_bloginfo('name')."] Your username and password"; |
| 1811 | $message = "Username: ".$user_data['user_login']."\nPassword: ".$user_data['user_pass']."\n".get_bloginfo('siteurl')."/wp-login.php"; |
| 1812 | wp_mail( $user_data['user_email'], $subject, $message ); |
| 1813 | |
| 1814 | } |
| 1815 | |
| 1816 | return strval( $user_id ); |
| 1817 | |
| 1818 | } |
| 1819 | |