| | 880 | * Prepares user data for return in an XML-RPC object. |
| | 881 | * |
| | 882 | * @access protected |
| | 883 | * |
| | 884 | * @param WP_User $user The unprepared user object |
| | 885 | * @param array $fields The subset of user fields to return |
| | 886 | * @return array The prepared user data |
| | 887 | */ |
| | 888 | protected function _prepare_user( $user, $fields ) { |
| | 889 | $_user = array( 'user_id' => strval( $user->ID ) ); |
| | 890 | |
| | 891 | $user_fields = array( |
| | 892 | 'username' => $user->user_login, |
| | 893 | 'first_name' => $user->user_firstname, |
| | 894 | 'last_name' => $user->user_lastname, |
| | 895 | 'registered' => $this->_convert_date( $user->user_registered ), |
| | 896 | 'bio' => $user->user_description, |
| | 897 | 'email' => $user->user_email, |
| | 898 | 'nickname' => $user->nickname, |
| | 899 | 'nicename' => $user->user_nicename, |
| | 900 | 'url' => $user->user_url, |
| | 901 | 'display_name' => $user->display_name, |
| | 902 | 'capabilities' => $user->caps, |
| | 903 | 'roles' => $user->roles, |
| | 904 | ); |
| | 905 | |
| | 906 | if ( in_array( 'all', $fields ) ) { |
| | 907 | $_user = array_merge( $_user, $user_fields ); |
| | 908 | } |
| | 909 | else { |
| | 910 | if ( in_array( 'basic', $fields ) ) { |
| | 911 | $basic_fields = array( 'username', 'email', 'registered', 'display_name', 'nicename' ); |
| | 912 | $fields = array_merge( $fields, $basic_fields ); |
| | 913 | } |
| | 914 | $requested_fields = array_intersect_key( $user_fields, array_flip( $fields ) ); |
| | 915 | $_user = array_merge( $_user, $requested_fields ); |
| | 916 | } |
| | 917 | |
| | 918 | return apply_filters( 'xmlrpc_prepare_user', $_user, $user, $fields ); |
| | 919 | } |
| | 920 | |
| | 921 | /** |
| | 1969 | * Retrieve a user. |
| | 1970 | * |
| | 1971 | * The optional $fields parameter specifies what fields will be included |
| | 1972 | * in the response array. This should be a list of field names. 'user_id' will |
| | 1973 | * always be included in the response regardless of the value of $fields. |
| | 1974 | * |
| | 1975 | * Instead of, or in addition to, individual field names, conceptual group |
| | 1976 | * names can be used to specify multiple fields. The available conceptual |
| | 1977 | * groups are 'basic' and 'all'. |
| | 1978 | * |
| | 1979 | * @uses get_userdata() |
| | 1980 | * @param array $args Method parameters. Contains: |
| | 1981 | * - int $blog_id |
| | 1982 | * - string $username |
| | 1983 | * - string $password |
| | 1984 | * - int $user_id |
| | 1985 | * - array $fields optional |
| | 1986 | * @return array contains (based on $fields parameter): |
| | 1987 | * - 'user_id' |
| | 1988 | * - 'username' |
| | 1989 | * - 'first_name' |
| | 1990 | * - 'last_name' |
| | 1991 | * - 'registered' |
| | 1992 | * - 'bio' |
| | 1993 | * - 'email' |
| | 1994 | * - 'nickname' |
| | 1995 | * - 'nicename' |
| | 1996 | * - 'url' |
| | 1997 | * - 'display_name' |
| | 1998 | * - 'capabilities' |
| | 1999 | * - 'user_level' |
| | 2000 | * - 'user_contacts' |
| | 2001 | */ |
| | 2002 | function wp_getUser( $args ) { |
| | 2003 | if ( ! $this->minimum_args( $args, 4 ) ) |
| | 2004 | return $this->error; |
| | 2005 | |
| | 2006 | $this->escape( $args ); |
| | 2007 | |
| | 2008 | $blog_id = (int) $args[0]; |
| | 2009 | $username = $args[1]; |
| | 2010 | $password = $args[2]; |
| | 2011 | $user_id = (int) $args[3]; |
| | 2012 | |
| | 2013 | if ( isset( $args[4] ) ) |
| | 2014 | $fields = $args[4]; |
| | 2015 | else |
| | 2016 | $fields = apply_filters( 'xmlrpc_default_user_fields', array( 'all' ), 'wp.getUser' ); |
| | 2017 | |
| | 2018 | if ( ! $user = $this->login( $username, $password ) ) |
| | 2019 | return $this->error; |
| | 2020 | |
| | 2021 | do_action( 'xmlrpc_call', 'wp.getUser' ); |
| | 2022 | |
| | 2023 | if ( ! ( $user_id == $user->ID || current_user_can( 'edit_user', $user_id ) ) ) |
| | 2024 | return new IXR_Error( 401, __( 'Sorry, you cannot edit users.' ) ); |
| | 2025 | |
| | 2026 | $user_data = get_userdata( $user_id ); |
| | 2027 | |
| | 2028 | if ( ! $user_data ) |
| | 2029 | return new IXR_Error( 404, __( 'Invalid user ID' ) ); |
| | 2030 | |
| | 2031 | return $this->_prepare_user( $user_data, $fields ); |
| | 2032 | } |
| | 2033 | |
| | 2034 | /** |
| | 2035 | * Retrieve users. |
| | 2036 | * |
| | 2037 | * The optional $filter parameter modifies the query used to retrieve users. |
| | 2038 | * Accepted keys are 'number' (default: 50), 'offset' (default: 0), 'role', |
| | 2039 | * 'orderby', and 'order'. |
| | 2040 | * |
| | 2041 | * The optional $fields parameter specifies what fields will be included |
| | 2042 | * in the response array. |
| | 2043 | * |
| | 2044 | * @uses get_users() |
| | 2045 | * @see wp_getUser() for more on $fields and return values |
| | 2046 | * |
| | 2047 | * @param array $args Method parameters. Contains: |
| | 2048 | * - int $blog_id |
| | 2049 | * - string $username |
| | 2050 | * - string $password |
| | 2051 | * - array $filter optional |
| | 2052 | * - array $fields optional |
| | 2053 | * @return array users data |
| | 2054 | */ |
| | 2055 | function wp_getUsers( $args ) { |
| | 2056 | if ( ! $this->minimum_args( $args, 3 ) ) |
| | 2057 | return $this->error; |
| | 2058 | |
| | 2059 | $this->escape( $args ); |
| | 2060 | |
| | 2061 | $blog_id = (int) $args[0]; |
| | 2062 | $username = $args[1]; |
| | 2063 | $password = $args[2]; |
| | 2064 | $filter = isset( $args[3] ) ? $args[3] : array(); |
| | 2065 | |
| | 2066 | if ( isset( $args[4] ) ) |
| | 2067 | $fields = $args[4]; |
| | 2068 | else |
| | 2069 | $fields = apply_filters( 'xmlrpc_default_user_fields', array( 'all' ), 'wp.getUsers' ); |
| | 2070 | |
| | 2071 | if ( ! $user = $this->login( $username, $password ) ) |
| | 2072 | return $this->error; |
| | 2073 | |
| | 2074 | do_action( 'xmlrpc_call', 'wp.getUsers' ); |
| | 2075 | |
| | 2076 | if ( ! current_user_can( 'edit_users' ) ) |
| | 2077 | return new IXR_Error( 401, __( 'Sorry, you cannot edit users.' ) ); |
| | 2078 | |
| | 2079 | $query = array(); |
| | 2080 | |
| | 2081 | $query['number'] = ( isset( $filter['number'] ) ) ? absint( $filter['number'] ) : 50; |
| | 2082 | $query['offset'] = ( isset( $filter['offset'] ) ) ? absint( $filter['offset'] ) : 0; |
| | 2083 | |
| | 2084 | if ( isset( $filter['orderby'] ) ) { |
| | 2085 | $query['orderby'] = $filter['orderby']; |
| | 2086 | |
| | 2087 | if ( isset( $filter['order'] ) ) |
| | 2088 | $query['order'] = $filter['order']; |
| | 2089 | } |
| | 2090 | |
| | 2091 | if ( isset( $filter['role'] ) ) { |
| | 2092 | if ( $filter['role'] == 'authors' ) { |
| | 2093 | $query['who'] = 'authors'; |
| | 2094 | } |
| | 2095 | else { |
| | 2096 | if ( get_role( $filter['role'] ) === null ) |
| | 2097 | return new IXR_Error( 403, __( 'The role specified is not valid' ) ); |
| | 2098 | |
| | 2099 | $query['role'] = $filter['role']; |
| | 2100 | } |
| | 2101 | } |
| | 2102 | |
| | 2103 | $users = get_users( $query ); |
| | 2104 | |
| | 2105 | $_users = array(); |
| | 2106 | foreach ( $users as $user_data ) { |
| | 2107 | if ( current_user_can( 'edit_user', $user_data->ID ) ) |
| | 2108 | $_users[] = $this->_prepare_user( $user_data, $fields ); |
| | 2109 | } |
| | 2110 | return $_users; |
| | 2111 | } |
| | 2112 | |
| | 2113 | /** |
| | 2114 | * Retrieve information about the requesting user. |
| | 2115 | * |
| | 2116 | * @uses get_userdata() |
| | 2117 | * @param array $args Method parameters. Contains: |
| | 2118 | * - int $blog_id |
| | 2119 | * - string $username |
| | 2120 | * - string $password |
| | 2121 | * - array $fields optional |
| | 2122 | * @return array (@see wp_getUser) |
| | 2123 | */ |
| | 2124 | function wp_getUserInfo( $args ) { |
| | 2125 | if ( ! $this->minimum_args( $args, 3 ) ) |
| | 2126 | return $this->error; |
| | 2127 | |
| | 2128 | $this->escape( $args ); |
| | 2129 | |
| | 2130 | $blog_id = (int) $args[0]; |
| | 2131 | $username = $args[1]; |
| | 2132 | $password = $args[2]; |
| | 2133 | |
| | 2134 | if ( isset( $args[3] ) ) |
| | 2135 | $fields = $args[3]; |
| | 2136 | else |
| | 2137 | $fields = apply_filters( 'xmlrpc_default_user_fields', array( 'all' ), 'wp.getUserInfo' ); |
| | 2138 | |
| | 2139 | if ( ! $user = $this->login( $username, $password ) ) |
| | 2140 | return $this->error; |
| | 2141 | |
| | 2142 | do_action( 'xmlrpc_call', 'wp.getUserInfo' ); |
| | 2143 | |
| | 2144 | $user_data = get_userdata( $user->ID ); |
| | 2145 | |
| | 2146 | return $this->_prepare_user( $user_data, $fields ); |
| | 2147 | } |
| | 2148 | |
| | 2149 | /** |
| | 2150 | * Edit user's profile information. |
| | 2151 | * |
| | 2152 | * @uses wp_update_user() |
| | 2153 | * @param array $args Method parameters. Contains: |
| | 2154 | * - int $blog_id |
| | 2155 | * - string $username |
| | 2156 | * - string $password |
| | 2157 | * - int $user_id |
| | 2158 | * - array $content_struct |
| | 2159 | * It can optionally contain: |
| | 2160 | * - 'first_name' |
| | 2161 | * - 'last_name' |
| | 2162 | * - 'website' |
| | 2163 | * - 'display_name' |
| | 2164 | * - 'nickname' |
| | 2165 | * - 'nicename' |
| | 2166 | * - 'bio' |
| | 2167 | * @return bool True, on success. |
| | 2168 | */ |
| | 2169 | function wp_editUserInfo( $args ) { |
| | 2170 | if ( ! $this->minimum_args( $args, 4 ) ) |
| | 2171 | return $this->error; |
| | 2172 | |
| | 2173 | $this->escape( $args ); |
| | 2174 | |
| | 2175 | $blog_id = (int) $args[0]; |
| | 2176 | $username = $args[1]; |
| | 2177 | $password = $args[2]; |
| | 2178 | $content_struct = $args[3]; |
| | 2179 | |
| | 2180 | if ( ! $user = $this->login( $username, $password ) ) |
| | 2181 | return $this->error; |
| | 2182 | |
| | 2183 | do_action( 'xmlrpc_call', 'wp.editUserInfo' ); |
| | 2184 | |
| | 2185 | // holds data of the user |
| | 2186 | $user_data = array(); |
| | 2187 | $user_data['ID'] = $user->ID; |
| | 2188 | |
| | 2189 | // only set the user details if it was given |
| | 2190 | if ( isset( $content_struct['first_name'] ) ) |
| | 2191 | $user_data['first_name'] = $content_struct['first_name']; |
| | 2192 | |
| | 2193 | if ( isset( $content_struct['last_name'] ) ) |
| | 2194 | $user_data['last_name'] = $content_struct['last_name']; |
| | 2195 | |
| | 2196 | if ( isset( $content_struct['website'] ) ) |
| | 2197 | $user_data['user_url'] = $content_struct['website']; |
| | 2198 | |
| | 2199 | if ( isset( $content_struct['display_name'] ) ) |
| | 2200 | $user_data['display_name'] = $content_struct['display_name']; |
| | 2201 | |
| | 2202 | if ( isset( $content_struct['nickname'] ) ) |
| | 2203 | $user_data['nickname'] = $content_struct['nickname']; |
| | 2204 | |
| | 2205 | if ( isset( $content_struct['nicename'] ) ) |
| | 2206 | $user_data['user_nicename'] = $content_struct['nicename']; |
| | 2207 | |
| | 2208 | if ( isset( $content_struct['bio'] ) ) |
| | 2209 | $user_data['description'] = $content_struct['bio']; |
| | 2210 | |
| | 2211 | $result = wp_update_user( $user_data ); |
| | 2212 | |
| | 2213 | if ( is_wp_error( $result ) ) |
| | 2214 | return new IXR_Error( 500, $result->get_error_message() ); |
| | 2215 | |
| | 2216 | if ( ! $result ) |
| | 2217 | return new IXR_Error( 500, __( 'Sorry, the user cannot be updated.' ) ); |
| | 2218 | |
| | 2219 | return true; |
| | 2220 | } |
| | 2221 | |
| | 2222 | /** |