Make WordPress Core


Ignore:
Timestamp:
09/12/2012 12:04:21 AM (14 years ago)
Author:
nacin
Message:

XML-RPC: Introduce wp.getUsers, wp.getUser, wp.getProfile, wp.editProfile.

props maxcutler.
props nprasath002 for earlier patches.

see #18428.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/class-wp-xmlrpc-server.php

    r21805 r21824  
    4949                        'wp.getTaxonomy'                => 'this:wp_getTaxonomy',
    5050                        'wp.getTaxonomies'              => 'this:wp_getTaxonomies',
     51                        'wp.getUser'                    => 'this:wp_getUser',
     52                        'wp.getUsers'                   => 'this:wp_getUsers',
     53                        'wp.getProfile'                 => 'this:wp_getProfile',
     54                        'wp.editProfile'                => 'this:wp_editProfile',
    5155                        'wp.getPage'                    => 'this:wp_getPage',
    5256                        'wp.getPages'                   => 'this:wp_getPages',
     
    896900
    897901        /**
     902         * Prepares user data for return in an XML-RPC object.
     903         *
     904         * @access protected
     905         *
     906         * @param WP_User $user The unprepared user object
     907         * @param array $fields The subset of user fields to return
     908         * @return array The prepared user data
     909         */
     910        protected function _prepare_user( $user, $fields ) {
     911                $_user = array( 'user_id' => strval( $user->ID ) );
     912
     913                $user_fields = array(
     914                        'username'          => $user->user_login,
     915                        'first_name'        => $user->user_firstname,
     916                        'last_name'         => $user->user_lastname,
     917                        'registered'        => $this->_convert_date( $user->user_registered ),
     918                        'bio'               => $user->user_description,
     919                        'email'             => $user->user_email,
     920                        'nickname'          => $user->nickname,
     921                        'nicename'          => $user->user_nicename,
     922                        'url'               => $user->user_url,
     923                        'display_name'      => $user->display_name,
     924                        'roles'             => $user->roles,
     925                );
     926
     927                if ( in_array( 'all', $fields ) ) {
     928                        $_user = array_merge( $_user, $user_fields );
     929                }
     930                else {
     931                        if ( in_array( 'basic', $fields ) ) {
     932                                $basic_fields = array( 'username', 'email', 'registered', 'display_name', 'nicename' );
     933                                $fields = array_merge( $fields, $basic_fields );
     934                        }
     935                        $requested_fields = array_intersect_key( $user_fields, array_flip( $fields ) );
     936                        $_user = array_merge( $_user, $requested_fields );
     937                }
     938
     939                return apply_filters( 'xmlrpc_prepare_user', $_user, $user, $fields );
     940        }
     941
     942        /**
    898943         * Create a new post for any registered post type.
    899944         *
     
    19401985
    19411986                return $struct;
     1987        }
     1988
     1989        /**
     1990         * Retrieve a user.
     1991         *
     1992         * The optional $fields parameter specifies what fields will be included
     1993         * in the response array. This should be a list of field names. 'user_id' will
     1994         * always be included in the response regardless of the value of $fields.
     1995         *
     1996         * Instead of, or in addition to, individual field names, conceptual group
     1997         * names can be used to specify multiple fields. The available conceptual
     1998         * groups are 'basic' and 'all'.
     1999         *
     2000         * @uses get_userdata()
     2001         * @param array $args Method parameters. Contains:
     2002         *  - int     $blog_id
     2003         *  - string  $username
     2004         *  - string  $password
     2005         *  - int     $user_id
     2006         *  - array   $fields optional
     2007         * @return array contains (based on $fields parameter):
     2008         *  - 'user_id'
     2009         *  - 'username'
     2010         *  - 'first_name'
     2011         *  - 'last_name'
     2012         *  - 'registered'
     2013         *  - 'bio'
     2014         *  - 'email'
     2015         *  - 'nickname'
     2016         *  - 'nicename'
     2017         *  - 'url'
     2018         *  - 'display_name'
     2019         *  - 'roles'
     2020         */
     2021        function wp_getUser( $args ) {
     2022                if ( ! $this->minimum_args( $args, 4 ) )
     2023                        return $this->error;
     2024
     2025                $this->escape( $args );
     2026
     2027                $blog_id    = (int) $args[0];
     2028                $username   = $args[1];
     2029                $password   = $args[2];
     2030                $user_id    = (int) $args[3];
     2031
     2032                if ( isset( $args[4] ) )
     2033                        $fields = $args[4];
     2034                else
     2035                        $fields = apply_filters( 'xmlrpc_default_user_fields', array( 'all' ), 'wp.getUser' );
     2036
     2037                if ( ! $user = $this->login( $username, $password ) )
     2038                        return $this->error;
     2039
     2040                do_action( 'xmlrpc_call', 'wp.getUser' );
     2041
     2042                if ( ! current_user_can( 'edit_user', $user_id ) )
     2043                        return new IXR_Error( 401, __( 'Sorry, you cannot edit users.' ) );
     2044
     2045                $user_data = get_userdata( $user_id );
     2046
     2047                if ( ! $user_data )
     2048                        return new IXR_Error( 404, __( 'Invalid user ID' ) );
     2049
     2050                return $this->_prepare_user( $user_data, $fields );
     2051        }
     2052
     2053        /**
     2054         * Retrieve users.
     2055         *
     2056         * The optional $filter parameter modifies the query used to retrieve users.
     2057         * Accepted keys are 'number' (default: 50), 'offset' (default: 0), 'role',
     2058         * 'who', 'orderby', and 'order'.
     2059         *
     2060         * The optional $fields parameter specifies what fields will be included
     2061         * in the response array.
     2062         *
     2063         * @uses get_users()
     2064         * @see wp_getUser() for more on $fields and return values
     2065         *
     2066         * @param array $args Method parameters. Contains:
     2067         *  - int     $blog_id
     2068         *  - string  $username
     2069         *  - string  $password
     2070         *  - array   $filter optional
     2071         *  - array   $fields optional
     2072         * @return array users data
     2073         */
     2074        function wp_getUsers( $args ) {
     2075                if ( ! $this->minimum_args( $args, 3 ) )
     2076                        return $this->error;
     2077
     2078                $this->escape( $args );
     2079
     2080                $blog_id    = (int) $args[0];
     2081                $username   = $args[1];
     2082                $password   = $args[2];
     2083                $filter     = isset( $args[3] ) ? $args[3] : array();
     2084
     2085                if ( isset( $args[4] ) )
     2086                        $fields = $args[4];
     2087                else
     2088                        $fields = apply_filters( 'xmlrpc_default_user_fields', array( 'all' ), 'wp.getUsers' );
     2089
     2090                if ( ! $user = $this->login( $username, $password ) )
     2091                        return $this->error;
     2092
     2093                do_action( 'xmlrpc_call', 'wp.getUsers' );
     2094
     2095                if ( ! current_user_can( 'list_users' ) )
     2096                        return new IXR_Error( 401, __( 'Sorry, you cannot list users.' ) );
     2097
     2098                $query = array();
     2099
     2100                $query['number'] = ( isset( $filter['number'] ) ) ? absint( $filter['number'] ) : 50;
     2101                $query['offset'] = ( isset( $filter['offset'] ) ) ? absint( $filter['offset'] ) : 0;
     2102
     2103                if ( isset( $filter['orderby'] ) ) {
     2104                        $query['orderby'] = $filter['orderby'];
     2105
     2106                        if ( isset( $filter['order'] ) )
     2107                                $query['order'] = $filter['order'];
     2108                }
     2109
     2110                if ( isset( $filter['role'] ) ) {
     2111                        if ( get_role( $filter['role'] ) === null )
     2112                                return new IXR_Error( 403, __( 'The role specified is not valid' ) );
     2113
     2114                        $query['role'] = $filter['role'];
     2115                }
     2116
     2117                if ( isset( $filter['who'] ) ) {
     2118                        $query['who'] = $filter['who'];
     2119                }
     2120
     2121                $users = get_users( $query );
     2122
     2123                $_users = array();
     2124                foreach ( $users as $user_data ) {
     2125                        if ( current_user_can( 'edit_user', $user_data->ID ) )
     2126                                $_users[] = $this->_prepare_user( $user_data, $fields );
     2127                }
     2128                return $_users;
     2129        }
     2130
     2131        /**
     2132         * Retrieve information about the requesting user.
     2133         *
     2134         * @uses get_userdata()
     2135         * @param array $args Method parameters. Contains:
     2136         *  - int     $blog_id
     2137         *  - string  $username
     2138         *  - string  $password
     2139         *  - array   $fields optional
     2140         * @return array (@see wp_getUser)
     2141         */
     2142        function wp_getProfile( $args ) {
     2143                if ( ! $this->minimum_args( $args, 3 ) )
     2144                        return $this->error;
     2145
     2146                $this->escape( $args );
     2147
     2148                $blog_id    = (int) $args[0];
     2149                $username   = $args[1];
     2150                $password   = $args[2];
     2151
     2152                if ( isset( $args[3] ) )
     2153                        $fields = $args[3];
     2154                else
     2155                        $fields = apply_filters( 'xmlrpc_default_user_fields', array( 'all' ), 'wp.getProfile' );
     2156
     2157                if ( ! $user = $this->login( $username, $password ) )
     2158                        return $this->error;
     2159
     2160                do_action( 'xmlrpc_call', 'wp.getProfile' );
     2161
     2162                if ( ! current_user_can( 'edit_user', $user->ID ) )
     2163                        return new IXR_Error( 401, __( 'Sorry, you cannot edit your profile.' ) );
     2164
     2165                $user_data = get_userdata( $user->ID );
     2166
     2167                return $this->_prepare_user( $user_data, $fields );
     2168        }
     2169
     2170        /**
     2171         * Edit user's profile.
     2172         *
     2173         * @uses wp_update_user()
     2174         * @param array $args Method parameters. Contains:
     2175         *  - int     $blog_id
     2176         *  - string  $username
     2177         *  - string  $password
     2178         *  - int     $user_id
     2179         *  - array   $content_struct
     2180         *      It can optionally contain:
     2181         *      - 'first_name'
     2182         *      - 'last_name'
     2183         *      - 'website'
     2184         *      - 'display_name'
     2185         *      - 'nickname'
     2186         *      - 'nicename'
     2187         *      - 'bio'
     2188         * @return bool True, on success.
     2189         */
     2190        function wp_editProfile( $args ) {
     2191                if ( ! $this->minimum_args( $args, 4 ) )
     2192                        return $this->error;
     2193
     2194                $this->escape( $args );
     2195
     2196                $blog_id        = (int) $args[0];
     2197                $username       = $args[1];
     2198                $password       = $args[2];
     2199                $content_struct = $args[3];
     2200
     2201                if ( ! $user = $this->login( $username, $password ) )
     2202                        return $this->error;
     2203
     2204                do_action( 'xmlrpc_call', 'wp.editProfile' );
     2205
     2206                if ( ! current_user_can( 'edit_user', $user->ID ) )
     2207                        return new IXR_Error( 401, __( 'Sorry, you cannot edit your profile.' ) );
     2208
     2209                // holds data of the user
     2210                $user_data = array();
     2211                $user_data['ID'] = $user->ID;
     2212
     2213                // only set the user details if it was given
     2214                if ( isset( $content_struct['first_name'] ) )
     2215                        $user_data['first_name'] = $content_struct['first_name'];
     2216
     2217                if ( isset( $content_struct['last_name'] ) )
     2218                        $user_data['last_name'] = $content_struct['last_name'];
     2219
     2220                if ( isset( $content_struct['website'] ) )
     2221                        $user_data['user_url'] = $content_struct['website'];
     2222
     2223                if ( isset( $content_struct['display_name'] ) )
     2224                        $user_data['display_name'] = $content_struct['display_name'];
     2225
     2226                if ( isset( $content_struct['nickname'] ) )
     2227                        $user_data['nickname'] = $content_struct['nickname'];
     2228
     2229                if ( isset( $content_struct['nicename'] ) )
     2230                        $user_data['user_nicename'] = $content_struct['nicename'];
     2231
     2232                if ( isset( $content_struct['bio'] ) )
     2233                        $user_data['description'] = $content_struct['bio'];
     2234
     2235                $result = wp_update_user( $user_data );
     2236
     2237                if ( is_wp_error( $result ) )
     2238                        return new IXR_Error( 500, $result->get_error_message() );
     2239
     2240                if ( ! $result )
     2241                        return new IXR_Error( 500, __( 'Sorry, the user cannot be updated.' ) );
     2242
     2243                return true;
    19422244        }
    19432245
Note: See TracChangeset for help on using the changeset viewer.