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