Make WordPress Core

Ticket #18428: 18428.combo.patch

File 18428.combo.patch, 16.2 KB (added by maxcutler, 13 years ago)
  • wp-includes/class-wp-xmlrpc-server.php

     
    4848                        'wp.getTerms'                   => 'this:wp_getTerms',
    4949                        'wp.getTaxonomy'                => 'this:wp_getTaxonomy',
    5050                        'wp.getTaxonomies'              => 'this:wp_getTaxonomies',
     51                        'wp.newUser'            => 'this:wp_newUser',
     52                        'wp.editUser'           => 'this:wp_editUser',
     53                        'wp.deleteUser'         => 'this:wp_deleteUser',
     54                        'wp.getUser'            => 'this:wp_getUser',
     55                        'wp.getUsers'           => 'this:wp_getUsers',
     56                        'wp.getUserInfo'        => 'this:wp_getUserInfo',
    5157                        'wp.getPage'                    => 'this:wp_getPage',
    5258                        'wp.getPages'                   => 'this:wp_getPages',
    5359                        'wp.newPage'                    => 'this:wp_newPage',
     
    867873        }
    868874
    869875        /**
     876         * Prepares user data for return in an XML-RPC object.
     877         *
     878         * @access protected
     879         *
     880         * @param WP_User $user The unprepared user object
     881         * @param array $fields The subset of user fields to return
     882         * @return array The prepared user data
     883         */
     884        protected function _prepare_user( $user, $fields ) {
     885                $contact_methods = _wp_get_user_contactmethods();
     886
     887                $user_contacts = array();
     888                foreach ( $contact_methods as $key => $value ) {
     889                        $user_contacts[$key] = $user->$key;
     890                }
     891
     892                $_user = array( 'user_id' => strval( $user->ID ) );
     893
     894                $user_fields = array(
     895                        'username'          => $user->user_login,
     896                        'first_name'        => $user->user_firstname,
     897                        'last_name'         => $user->user_lastname,
     898                        'registered'        => $this->_convert_date( $user->user_registered ),
     899                        'bio'               => $user->user_description,
     900                        'email'             => $user->user_email,
     901                        'nickname'          => $user->nickname,
     902                        'nicename'          => $user->user_nicename,
     903                        'url'               => $user->user_url,
     904                        'display_name'      => $user->display_name,
     905                        'capabilities'      => $user->wp_capabilities,
     906                        'roles'             => $user->roles,
     907                        'user_contacts'     => $user_contacts
     908                );
     909
     910                if ( in_array( 'all', $fields ) ) {
     911                        $_user = array_merge( $_user, $user_fields );
     912                }
     913                else {
     914                        if ( in_array( 'basic', $fields ) ) {
     915                                $basic_fields = array( 'username', 'email', 'registered', 'display_name', 'nicename' );
     916                                $fields = array_merge( $fields, $basic_fields );
     917                        }
     918                        $requested_fields = array_intersect_key( $user_fields, array_flip( $fields ) );
     919                        $_user = array_merge( $_user, $requested_fields );
     920                }
     921
     922                return apply_filters( 'xmlrpc_prepare_user', $_user, $user, $fields );
     923        }
     924
     925        /**
    870926         * Create a new post for any registered post type.
    871927         *
    872928         * @since 3.4.0
     
    19121968        }
    19131969
    19141970        /**
     1971         * Create a new user.
     1972         *
     1973         * @uses wp_insert_user()
     1974         * @param array $args Method parameters. Contains:
     1975         *  - int     $blog_id
     1976         *  - string  $username
     1977         *  - string  $password
     1978         *  - array   $content_struct
     1979         *      The $content_struct must contain:
     1980         *      - 'username'
     1981         *      - 'password'
     1982         *      - 'email'
     1983         *      Also, it can optionally contain:
     1984         *      - 'role'
     1985         *      - 'first_name'
     1986         *      - 'last_name'
     1987         *      - 'url'
     1988         *      - 'display_name'
     1989         *      - 'nickname'
     1990         *      - 'nicename'
     1991         *      - 'bio'
     1992         *  - boolean $send_mail optional. Defaults to false
     1993         * @return int user_id
     1994         */
     1995        function wp_newUser( $args ) {
     1996                if ( ! $this->minimum_args( $args, 4 ) )
     1997                        return $this->error;
     1998
     1999                $this->escape( $args );
     2000
     2001                $blog_id        = (int) $args[0];
     2002                $username       = $args[1];
     2003                $password       = $args[2];
     2004                $content_struct = $args[3];
     2005                $send_mail      = isset( $args[4] ) ? $args[4] : false;
     2006
     2007                if ( ! $user = $this->login( $username, $password ) )
     2008                        return $this->error;
     2009
     2010                do_action( 'xmlrpc_call', 'wp.newUser' );
     2011
     2012                if ( ! current_user_can( 'create_users' ) )
     2013                        return new IXR_Error( 401, __( 'You are not allowed to create users.' ) );
     2014
     2015                // this hold all the user data
     2016                $user_data = array();
     2017
     2018                if ( empty( $content_struct['username'] ) )
     2019                        return new IXR_Error( 403, __( 'Username cannot be empty.' ) );
     2020                $user_data['user_login'] = $content_struct['username'];
     2021
     2022                if ( empty( $content_struct['password'] ) )
     2023                        return new IXR_Error( 403, __( 'Password cannot be empty.' ) );
     2024                $user_data['user_pass'] = $content_struct['password'];
     2025
     2026                if ( empty( $content_struct['email'] ) )
     2027                        return new IXR_Error( 403, __( 'Email cannot be empty.' ) );
     2028
     2029                if ( ! is_email( $content_struct['email'] ) )
     2030                        return new IXR_Error( 403, __( 'This email address is not valid.' ) );
     2031
     2032                if ( email_exists( $content_struct['email'] ) )
     2033                        return new IXR_Error( 403, __( 'This email address is already registered.' ) );
     2034
     2035                $user_data['user_email'] = $content_struct['email'];
     2036
     2037                if ( isset( $content_struct['role'] ) ) {
     2038                        if ( get_role( $content_struct['role'] ) === null )
     2039                                return new IXR_Error( 403, __( 'The role specified is not valid.' ) );
     2040
     2041                        $user_data['role'] = $content_struct['role'];
     2042                }
     2043
     2044                if ( isset( $content_struct['first_name'] ) )
     2045                        $user_data['first_name'] = $content_struct['first_name'];
     2046
     2047                if ( isset( $content_struct['last_name'] ) )
     2048                        $user_data['last_name'] = $content_struct['last_name'];
     2049
     2050                if ( isset( $content_struct['url'] ) )
     2051                        $user_data['user_url'] = $content_struct['url'];
     2052
     2053                if ( isset( $content_struct['display_name'] ) )
     2054                        $user_data['display_name'] = $content_struct['display_name'];
     2055
     2056                if ( isset( $content_struct['nickname'] ) )
     2057                        $user_data['nickname'] = $content_struct['nickname'];
     2058
     2059                if ( isset( $content_struct['nicename'] ) )
     2060                        $user_data['user_nicename'] = $content_struct['nicename'];
     2061
     2062                if ( isset( $content_struct['bio'] ) )
     2063                        $user_data['description'] = $content_struct['bio'];
     2064
     2065                $user_id = wp_insert_user( $user_data );
     2066
     2067                if ( is_wp_error( $user_id ) )
     2068                        return new IXR_Error( 500, $user_id->get_error_message() );
     2069
     2070                if ( ! $user_id )
     2071                        return new IXR_Error( 500, __( 'Sorry, the new user creation failed.' ) );
     2072
     2073                if ( $send_mail ) {
     2074                        wp_new_user_notification( $user_id, $user_data['user_pass'] );
     2075                }
     2076
     2077                return $user_id;
     2078        }
     2079
     2080        /**
     2081         * Edit a user.
     2082         *
     2083         * @uses wp_update_user()
     2084         * @param array $args Method parameters. Contains:
     2085         *  - int     $blog_id
     2086         *  - string  $username
     2087         *  - string  $password
     2088         *  - int     $user_id
     2089         *  - array   $content_struct
     2090         *      It can optionally contain:
     2091         *      - 'email'
     2092         *      - 'first_name'
     2093         *      - 'last_name'
     2094         *      - 'website'
     2095         *      - 'role'
     2096         *      - 'display_name'
     2097         *      - 'nickname'
     2098         *      - 'nicename'
     2099         *      - 'bio'
     2100         *      - 'usercontacts'
     2101         *      - 'password'
     2102         *  - boolean $send_mail optional. Defaults to false
     2103         * @return bool True, on success.
     2104         */
     2105        function wp_editUser( $args ) {
     2106                if ( ! $this->minimum_args( $args, 5 ) )
     2107                        return $this->error;
     2108
     2109                $this->escape( $args );
     2110
     2111                $blog_id        = (int) $args[0];
     2112                $username       = $args[1];
     2113                $password       = $args[2];
     2114                $user_id        = (int) $args[3];
     2115                $content_struct = $args[4];
     2116
     2117                if ( ! $user = $this->login( $username, $password ) )
     2118                        return $this->error;
     2119
     2120                do_action( 'xmlrpc_call', 'wp.editUser' );
     2121
     2122                $user_info = get_userdata( $user_id );
     2123
     2124                if ( ! $user_info )
     2125                        return new IXR_Error( 404, __( 'Invalid user ID.' ) );
     2126
     2127                if ( ! ( $user_id == $user->ID || current_user_can( 'edit_users' ) ) )
     2128                        return new IXR_Error( 401, __( 'Sorry, you cannot edit this user.' ) );
     2129
     2130                // holds data of the user
     2131                $user_data = array();
     2132                $user_data['ID'] = $user_id;
     2133
     2134                if ( isset( $content_struct['username'] ) && $content_struct['username'] !== $user_info->user_login )
     2135                        return new IXR_Error( 401, __( 'Username cannot be changed.' ) );
     2136
     2137                if ( isset( $content_struct['email'] ) ) {
     2138                        if ( ! is_email( $content_struct['email'] ) )
     2139                                return new IXR_Error( 403, __( 'This email address is not valid.' ) );
     2140
     2141                        // check whether it is already registered
     2142                        if ( $content_struct['email'] !== $user_info->user_email && email_exists( $content_struct['email'] ) )
     2143                                return new IXR_Error( 403, __( 'This email address is already registered.' ) );
     2144
     2145                        $user_data['user_email'] = $content_struct['email'];
     2146                }
     2147
     2148                if ( isset( $content_struct['role'] ) ) {
     2149                        if ( ! current_user_can( 'edit_users' ) )
     2150                                return new IXR_Error( 401, __( 'You are not allowed to change roles for this user.' ) );
     2151
     2152                        if ( get_role( $content_struct['role'] ) === null )
     2153                                return new IXR_Error( 403, __( 'The role specified is not valid' ) );
     2154
     2155                        $user_data['role'] = $content_struct['role'];
     2156                }
     2157
     2158                // only set the user details if it was given
     2159                if ( isset( $content_struct['first_name'] ) )
     2160                        $user_data['first_name'] = $content_struct['first_name'];
     2161
     2162                if ( isset( $content_struct['last_name'] ) )
     2163                        $user_data['last_name'] = $content_struct['last_name'];
     2164
     2165                if ( isset( $content_struct['website'] ) )
     2166                        $user_data['user_url'] = $content_struct['url'];
     2167
     2168                if ( isset( $content_struct['display_name'] ) )
     2169                        $user_data['display_name'] = $content_struct['display_name'];
     2170
     2171                if ( isset( $content_struct['nickname'] ) )
     2172                        $user_data['nickname'] = $content_struct['nickname'];
     2173
     2174                if ( isset( $content_struct['nicename'] ) )
     2175                        $user_data['user_nicename'] = $content_struct['nicename'];
     2176
     2177                if ( isset( $content_struct['bio'] ) )
     2178                        $user_data['description'] = $content_struct['bio'];
     2179
     2180                if ( isset( $content_struct['user_contacts'] ) ) {
     2181                        $user_contacts = _wp_get_user_contactmethods( $user_data );
     2182                        foreach ( $content_struct['user_contacts'] as $key => $value ) {
     2183                                if ( ! array_key_exists( $key, $user_contacts ) )
     2184                                        return new IXR_Error( 403, __( 'One of the contact method specified is not valid' ) );
     2185
     2186                                $user_data[ $key ] = $value;
     2187                        }
     2188                }
     2189
     2190                if ( isset ( $content_struct['password'] ) )
     2191                        $user_data['user_pass'] = $content_struct['password'];
     2192
     2193                $result = wp_update_user( $user_data );
     2194
     2195                if ( is_wp_error( $result ) )
     2196                        return new IXR_Error( 500, $result->get_error_message() );
     2197
     2198                if ( ! $result )
     2199                        return new IXR_Error( 500, __( 'Sorry, the user cannot be updated.' ) );
     2200
     2201                return true;
     2202        }
     2203
     2204        /**
     2205         * Delete a user.
     2206         *
     2207         * @uses wp_delete_user()
     2208         * @param array $args Method parameters. Contains:
     2209         *  - int     $blog_id
     2210         *  - string  $username
     2211         *  - string  $password
     2212         *  - int     $user_id
     2213         *  - int     $reassign_id (optional)
     2214         * @return True when user is deleted.
     2215         */
     2216        function wp_deleteUser( $args ) {
     2217                if ( ! $this->minimum_args( $args, 4 ) )
     2218                        return $this->error;
     2219
     2220                $this->escape( $args );
     2221
     2222                $blog_id    = (int) $args[0];
     2223                $username   = $args[1];
     2224                $password   = $args[2];
     2225                $user_id    = (int) $args[3];
     2226
     2227                if ( ! $user = $this->login( $username, $password ) )
     2228                        return $this->error;
     2229
     2230                do_action( 'xmlrpc_call', 'wp.deleteUser' );
     2231
     2232                if ( ! current_user_can( 'delete_users' ) )
     2233                        return new IXR_Error( 401, __( 'You are not allowed to delete users.' ) );
     2234
     2235                if ( ! get_userdata( $user_id ) )
     2236                        return new IXR_Error( 404, __( 'Invalid user ID.' ) );
     2237
     2238                if ( $user->ID == $user_id )
     2239                        return new IXR_Error( 401, __( 'You cannot delete yourself.' ) );
     2240
     2241                $reassign_id = 'novalue';
     2242                if ( isset( $args[4] ) ) {
     2243                        $reassign_id = (int) $args[4];
     2244
     2245                        if ( ! get_userdata( $reassign_id ) )
     2246                                return new IXR_Error( 404, __( 'Invalid reassign user ID.' ) );
     2247
     2248                        if ( $reassign_id === $user_id )
     2249                                return new IXR_Error( 401, __( 'You cannot reassign to the user being deleted.' ) );
     2250                }
     2251
     2252                return wp_delete_user( $user_id, $reassign_id );
     2253        }
     2254
     2255        /**
     2256         * Retrieve a user.
     2257         *
     2258         * The optional $fields parameter specifies what fields will be included
     2259         * in the response array. This should be a list of field names. 'user_id' will
     2260         * always be included in the response regardless of the value of $fields.
     2261         *
     2262         * Instead of, or in addition to, individual field names, conceptual group
     2263         * names can be used to specify multiple fields. The available conceptual
     2264         * groups are 'basic' and 'all'.
     2265         *
     2266         * @uses get_userdata()
     2267         * @param array $args Method parameters. Contains:
     2268         *  - int     $blog_id
     2269         *  - string  $username
     2270         *  - string  $password
     2271         *  - int     $user_id
     2272         *  - array   $fields optional
     2273         * @return array contains (based on $fields parameter):
     2274         *  - 'user_id'
     2275         *  - 'username'
     2276         *  - 'first_name'
     2277         *  - 'last_name'
     2278         *  - 'registered'
     2279         *  - 'bio'
     2280         *  - 'email'
     2281         *  - 'nickname'
     2282         *  - 'nicename'
     2283         *  - 'url'
     2284         *  - 'display_name'
     2285         *  - 'capabilities'
     2286         *  - 'user_level'
     2287         *  - 'user_contacts'
     2288         */
     2289        function wp_getUser( $args ) {
     2290                if ( ! $this->minimum_args( $args, 4 ) )
     2291                        return $this->error;
     2292
     2293                $this->escape( $args );
     2294
     2295                $blog_id    = (int) $args[0];
     2296                $username   = $args[1];
     2297                $password   = $args[2];
     2298                $user_id    = (int) $args[3];
     2299
     2300                if ( isset( $args[4] ) )
     2301                        $fields = $args[4];
     2302                else
     2303                        $fields = apply_filters( 'xmlrpc_default_user_fields', array( 'all' ), 'wp.getUser' );
     2304
     2305                if ( ! $user = $this->login( $username, $password ) )
     2306                        return $this->error;
     2307
     2308                do_action( 'xmlrpc_call', 'wp.getUser' );
     2309
     2310                if ( ! ( $user_id == $user->ID || current_user_can( 'edit_users' ) ) )
     2311                        return new IXR_Error( 401, __( 'Sorry, you cannot edit users.' ) );
     2312
     2313                $user_data = get_userdata( $user_id );
     2314
     2315                if ( ! $user_data )
     2316                        return new IXR_Error( 404, __( 'Invalid user ID' ) );
     2317
     2318                return $this->_prepare_user( $user_data, $fields );
     2319        }
     2320
     2321        /**
     2322         * Retrieve users.
     2323         *
     2324         * The optional $filter parameter modifies the query used to retrieve users.
     2325         * Accepted keys are 'number' (default: 50), 'offset' (default: 0), 'role',
     2326         * 'orderby', and 'order'.
     2327         *
     2328         * The optional $fields parameter specifies what fields will be included
     2329         * in the response array.
     2330         *
     2331         * @uses get_users()
     2332         * @see wp_getUser() for more on $fields and return values
     2333         *
     2334         * @param array $args Method parameters. Contains:
     2335         *  - int     $blog_id
     2336         *  - string  $username
     2337         *  - string  $password
     2338         *  - array   $filter optional
     2339         *  - array   $fields optional
     2340         * @return array users data
     2341         */
     2342        function wp_getUsers( $args ) {
     2343                if ( ! $this->minimum_args( $args, 3 ) )
     2344                        return $this->error;
     2345
     2346                $this->escape( $args );
     2347
     2348                $blog_id    = (int) $args[0];
     2349                $username   = $args[1];
     2350                $password   = $args[2];
     2351                $filter     = isset( $args[3] ) ? $args[3] : array();
     2352
     2353                if ( isset( $args[4] ) )
     2354                        $fields = $args[4];
     2355                else
     2356                        $fields = apply_filters( 'xmlrpc_default_user_fields', array( 'all' ), 'wp.getUsers' );
     2357
     2358                if ( ! $user = $this->login( $username, $password ) )
     2359                        return $this->error;
     2360
     2361                do_action( 'xmlrpc_call', 'wp.getUsers' );
     2362
     2363                if ( ! current_user_can( 'edit_users' ) )
     2364                        return new IXR_Error( 401, __( 'Sorry, you cannot edit users.' ) );
     2365
     2366                $query = array();
     2367
     2368                $query['number'] = ( isset( $filter['number'] ) ) ? absint( $filter['number'] ) : 50;
     2369                $query['offset'] = ( isset( $filter['offset'] ) ) ? absint( $filter['offset'] ) : 0;
     2370
     2371                if ( isset( $filter['orderby'] ) ) {
     2372                        $query['orderby'] = $filter['orderby'];
     2373
     2374                        if ( isset( $filter['order'] ) )
     2375                                $query['order'] = $filter['order'];
     2376                }
     2377
     2378                if ( isset( $filter['role'] ) ) {
     2379                        if ( get_role( $filter['role'] ) === null )
     2380                                return new IXR_Error( 403, __( 'The role specified is not valid' ) );
     2381
     2382                        $query['role'] = $filter['role'];
     2383                }
     2384
     2385                $users = get_users( $query );
     2386
     2387                $_users = array();
     2388                foreach ( $users as $user_data ) {
     2389                        $_users[] = $this->_prepare_user( $user_data, $fields );
     2390                }
     2391                return $_users;
     2392        }
     2393
     2394        /**
     2395         * Retrieve information about the requesting user.
     2396         *
     2397         * @uses get_userdata()
     2398         * @param array $args Method parameters. Contains:
     2399         *  - int     $blog_id
     2400         *  - string  $username
     2401         *  - string  $password
     2402         *  - array   $fields optional
     2403         * @return array (@see wp_getUser)
     2404         */
     2405        function wp_getUserInfo( $args ) {
     2406                if ( ! $this->minimum_args( $args, 3 ) )
     2407                        return $this->error;
     2408
     2409                $this->escape( $args );
     2410
     2411                $blog_id    = (int) $args[0];
     2412                $username   = $args[1];
     2413                $password   = $args[2];
     2414
     2415                if ( isset( $args[3] ) )
     2416                        $fields = $args[3];
     2417                else
     2418                        $fields = apply_filters( 'xmlrpc_default_user_fields', array( 'all' ), 'wp.getUserInfo' );
     2419
     2420                if ( ! $user = $this->login( $username, $password ) )
     2421                        return $this->error;
     2422
     2423                do_action( 'xmlrpc_call', 'wp.getUserInfo' );
     2424
     2425                $user_data = get_userdata( $user->ID );
     2426
     2427                return $this->_prepare_user( $user_data, $fields );
     2428        }
     2429
     2430        /**
    19152431         * Retrieve page.
    19162432         *
    19172433         * @since 2.2.0