Make WordPress Core

Ticket #18428: 18428.combo.2.patch

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