<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[21824] trunk/wp-includes/class-wp-xmlrpc-server.php: XML-RPC: Introduce wp.getUsers, wp.getUser, wp.getProfile, wp.editProfile.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg > ul, #logmsg > ol { margin-left: 0; margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://core.trac.wordpress.org/changeset/21824">21824</a></dd>
<dt>Author</dt> <dd>nacin</dd>
<dt>Date</dt> <dd>2012-09-12 00:04:21 +0000 (Wed, 12 Sep 2012)</dd>
</dl>

<h3>Log Message</h3>
<pre>XML-RPC: Introduce wp.getUsers, wp.getUser, wp.getProfile, wp.editProfile.

props maxcutler.
props nprasath002 for earlier patches.

see <a href="http://core.trac.wordpress.org/ticket/18428">#18428</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpincludesclasswpxmlrpcserverphp">trunk/wp-includes/class-wp-xmlrpc-server.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpincludesclasswpxmlrpcserverphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/class-wp-xmlrpc-server.php (21823 => 21824)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/class-wp-xmlrpc-server.php        2012-09-11 22:22:20 UTC (rev 21823)
+++ trunk/wp-includes/class-wp-xmlrpc-server.php        2012-09-12 00:04:21 UTC (rev 21824)
</span><span class="lines">@@ -48,6 +48,10 @@
</span><span class="cx">                         'wp.getTerms'                        =&gt; 'this:wp_getTerms',
</span><span class="cx">                         'wp.getTaxonomy'                =&gt; 'this:wp_getTaxonomy',
</span><span class="cx">                         'wp.getTaxonomies'                =&gt; 'this:wp_getTaxonomies',
</span><ins>+                        'wp.getUser'                        =&gt; 'this:wp_getUser',
+                        'wp.getUsers'                        =&gt; 'this:wp_getUsers',
+                        'wp.getProfile'                        =&gt; 'this:wp_getProfile',
+                        'wp.editProfile'                =&gt; 'this:wp_editProfile',
</ins><span class="cx">                         'wp.getPage'                        =&gt; 'this:wp_getPage',
</span><span class="cx">                         'wp.getPages'                        =&gt; 'this:wp_getPages',
</span><span class="cx">                         'wp.newPage'                        =&gt; 'this:wp_newPage',
</span><span class="lines">@@ -895,6 +899,47 @@
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         /**
</span><ins>+         * Prepares user data for return in an XML-RPC object.
+         *
+         * @access protected
+         *
+         * @param WP_User $user The unprepared user object
+         * @param array $fields The subset of user fields to return
+         * @return array The prepared user data
+         */
+        protected function _prepare_user( $user, $fields ) {
+                $_user = array( 'user_id' =&gt; strval( $user-&gt;ID ) );
+
+                $user_fields = array(
+                        'username'          =&gt; $user-&gt;user_login,
+                        'first_name'        =&gt; $user-&gt;user_firstname,
+                        'last_name'         =&gt; $user-&gt;user_lastname,
+                        'registered'        =&gt; $this-&gt;_convert_date( $user-&gt;user_registered ),
+                        'bio'               =&gt; $user-&gt;user_description,
+                        'email'             =&gt; $user-&gt;user_email,
+                        'nickname'          =&gt; $user-&gt;nickname,
+                        'nicename'          =&gt; $user-&gt;user_nicename,
+                        'url'               =&gt; $user-&gt;user_url,
+                        'display_name'      =&gt; $user-&gt;display_name,
+                        'roles'             =&gt; $user-&gt;roles,
+                );
+
+                if ( in_array( 'all', $fields ) ) {
+                        $_user = array_merge( $_user, $user_fields );
+                }
+                else {
+                        if ( in_array( 'basic', $fields ) ) {
+                                $basic_fields = array( 'username', 'email', 'registered', 'display_name', 'nicename' );
+                                $fields = array_merge( $fields, $basic_fields );
+                        }
+                        $requested_fields = array_intersect_key( $user_fields, array_flip( $fields ) );
+                        $_user = array_merge( $_user, $requested_fields );
+                }
+
+                return apply_filters( 'xmlrpc_prepare_user', $_user, $user, $fields );
+        }
+
+        /**
</ins><span class="cx">          * Create a new post for any registered post type.
</span><span class="cx">          *
</span><span class="cx">          * @since 3.4.0
</span><span class="lines">@@ -1942,6 +1987,263 @@
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         /**
</span><ins>+         * Retrieve a user.
+         *
+         * The optional $fields parameter specifies what fields will be included
+         * in the response array. This should be a list of field names. 'user_id' will
+         * always be included in the response regardless of the value of $fields.
+         *
+         * Instead of, or in addition to, individual field names, conceptual group
+         * names can be used to specify multiple fields. The available conceptual
+         * groups are 'basic' and 'all'.
+         *
+         * @uses get_userdata()
+         * @param array $args Method parameters. Contains:
+         *  - int     $blog_id
+         *  - string  $username
+         *  - string  $password
+         *  - int     $user_id
+         *  - array   $fields optional
+         * @return array contains (based on $fields parameter):
+         *  - 'user_id'
+         *  - 'username'
+         *  - 'first_name'
+         *  - 'last_name'
+         *  - 'registered'
+         *  - 'bio'
+         *  - 'email'
+         *  - 'nickname'
+         *  - 'nicename'
+         *  - 'url'
+         *  - 'display_name'
+         *  - 'roles'
+         */
+        function wp_getUser( $args ) {
+                if ( ! $this-&gt;minimum_args( $args, 4 ) )
+                        return $this-&gt;error;
+
+                $this-&gt;escape( $args );
+
+                $blog_id    = (int) $args[0];
+                $username   = $args[1];
+                $password   = $args[2];
+                $user_id    = (int) $args[3];
+
+                if ( isset( $args[4] ) )
+                        $fields = $args[4];
+                else
+                        $fields = apply_filters( 'xmlrpc_default_user_fields', array( 'all' ), 'wp.getUser' );
+
+                if ( ! $user = $this-&gt;login( $username, $password ) )
+                        return $this-&gt;error;
+
+                do_action( 'xmlrpc_call', 'wp.getUser' );
+
+                if ( ! current_user_can( 'edit_user', $user_id ) )
+                        return new IXR_Error( 401, __( 'Sorry, you cannot edit users.' ) );
+
+                $user_data = get_userdata( $user_id );
+
+                if ( ! $user_data )
+                        return new IXR_Error( 404, __( 'Invalid user ID' ) );
+
+                return $this-&gt;_prepare_user( $user_data, $fields );
+        }
+
+        /**
+         * Retrieve users.
+         *
+         * The optional $filter parameter modifies the query used to retrieve users.
+         * Accepted keys are 'number' (default: 50), 'offset' (default: 0), 'role',
+         * 'who', 'orderby', and 'order'.
+         *
+         * The optional $fields parameter specifies what fields will be included
+         * in the response array.
+         *
+         * @uses get_users()
+         * @see wp_getUser() for more on $fields and return values
+         *
+         * @param array $args Method parameters. Contains:
+         *  - int     $blog_id
+         *  - string  $username
+         *  - string  $password
+         *  - array   $filter optional
+         *  - array   $fields optional
+         * @return array users data
+         */
+        function wp_getUsers( $args ) {
+                if ( ! $this-&gt;minimum_args( $args, 3 ) )
+                        return $this-&gt;error;
+
+                $this-&gt;escape( $args );
+
+                $blog_id    = (int) $args[0];
+                $username   = $args[1];
+                $password   = $args[2];
+                $filter     = isset( $args[3] ) ? $args[3] : array();
+
+                if ( isset( $args[4] ) )
+                        $fields = $args[4];
+                else
+                        $fields = apply_filters( 'xmlrpc_default_user_fields', array( 'all' ), 'wp.getUsers' );
+
+                if ( ! $user = $this-&gt;login( $username, $password ) )
+                        return $this-&gt;error;
+
+                do_action( 'xmlrpc_call', 'wp.getUsers' );
+
+                if ( ! current_user_can( 'list_users' ) )
+                        return new IXR_Error( 401, __( 'Sorry, you cannot list users.' ) );
+
+                $query = array();
+
+                $query['number'] = ( isset( $filter['number'] ) ) ? absint( $filter['number'] ) : 50;
+                $query['offset'] = ( isset( $filter['offset'] ) ) ? absint( $filter['offset'] ) : 0;
+
+                if ( isset( $filter['orderby'] ) ) {
+                        $query['orderby'] = $filter['orderby'];
+
+                        if ( isset( $filter['order'] ) )
+                                $query['order'] = $filter['order'];
+                }
+
+                if ( isset( $filter['role'] ) ) {
+                        if ( get_role( $filter['role'] ) === null )
+                                return new IXR_Error( 403, __( 'The role specified is not valid' ) );
+
+                        $query['role'] = $filter['role'];
+                }
+
+                if ( isset( $filter['who'] ) ) {
+                        $query['who'] = $filter['who'];
+                }
+
+                $users = get_users( $query );
+
+                $_users = array();
+                foreach ( $users as $user_data ) {
+                        if ( current_user_can( 'edit_user', $user_data-&gt;ID ) )
+                                $_users[] = $this-&gt;_prepare_user( $user_data, $fields );
+                }
+                return $_users;
+        }
+
+        /**
+         * Retrieve information about the requesting user.
+         *
+         * @uses get_userdata()
+         * @param array $args Method parameters. Contains:
+         *  - int     $blog_id
+         *  - string  $username
+         *  - string  $password
+         *  - array   $fields optional
+         * @return array (@see wp_getUser)
+         */
+        function wp_getProfile( $args ) {
+                if ( ! $this-&gt;minimum_args( $args, 3 ) )
+                        return $this-&gt;error;
+
+                $this-&gt;escape( $args );
+
+                $blog_id    = (int) $args[0];
+                $username   = $args[1];
+                $password   = $args[2];
+
+                if ( isset( $args[3] ) )
+                        $fields = $args[3];
+                else
+                        $fields = apply_filters( 'xmlrpc_default_user_fields', array( 'all' ), 'wp.getProfile' );
+
+                if ( ! $user = $this-&gt;login( $username, $password ) )
+                        return $this-&gt;error;
+
+                do_action( 'xmlrpc_call', 'wp.getProfile' );
+
+                if ( ! current_user_can( 'edit_user', $user-&gt;ID ) )
+                        return new IXR_Error( 401, __( 'Sorry, you cannot edit your profile.' ) );
+
+                $user_data = get_userdata( $user-&gt;ID );
+
+                return $this-&gt;_prepare_user( $user_data, $fields );
+        }
+
+        /**
+         * Edit user's profile.
+         *
+         * @uses wp_update_user()
+         * @param array $args Method parameters. Contains:
+         *  - int     $blog_id
+         *  - string  $username
+         *  - string  $password
+         *  - int     $user_id
+         *  - array   $content_struct
+         *      It can optionally contain:
+         *      - 'first_name'
+         *      - 'last_name'
+         *      - 'website'
+         *      - 'display_name'
+         *      - 'nickname'
+         *      - 'nicename'
+         *      - 'bio'
+         * @return bool True, on success.
+         */
+        function wp_editProfile( $args ) {
+                if ( ! $this-&gt;minimum_args( $args, 4 ) )
+                        return $this-&gt;error;
+
+                $this-&gt;escape( $args );
+
+                $blog_id        = (int) $args[0];
+                $username       = $args[1];
+                $password       = $args[2];
+                $content_struct = $args[3];
+
+                if ( ! $user = $this-&gt;login( $username, $password ) )
+                        return $this-&gt;error;
+
+                do_action( 'xmlrpc_call', 'wp.editProfile' );
+
+                if ( ! current_user_can( 'edit_user', $user-&gt;ID ) )
+                        return new IXR_Error( 401, __( 'Sorry, you cannot edit your profile.' ) );
+
+                // holds data of the user
+                $user_data = array();
+                $user_data['ID'] = $user-&gt;ID;
+
+                // only set the user details if it was given
+                if ( isset( $content_struct['first_name'] ) )
+                        $user_data['first_name'] = $content_struct['first_name'];
+
+                if ( isset( $content_struct['last_name'] ) )
+                        $user_data['last_name'] = $content_struct['last_name'];
+
+                if ( isset( $content_struct['website'] ) )
+                        $user_data['user_url'] = $content_struct['website'];
+
+                if ( isset( $content_struct['display_name'] ) )
+                        $user_data['display_name'] = $content_struct['display_name'];
+
+                if ( isset( $content_struct['nickname'] ) )
+                        $user_data['nickname'] = $content_struct['nickname'];
+
+                if ( isset( $content_struct['nicename'] ) )
+                        $user_data['user_nicename'] = $content_struct['nicename'];
+
+                if ( isset( $content_struct['bio'] ) )
+                        $user_data['description'] = $content_struct['bio'];
+
+                $result = wp_update_user( $user_data );
+
+                if ( is_wp_error( $result ) )
+                        return new IXR_Error( 500, $result-&gt;get_error_message() );
+
+                if ( ! $result )
+                        return new IXR_Error( 500, __( 'Sorry, the user cannot be updated.' ) );
+
+                return true;
+        }
+
+        /**
</ins><span class="cx">          * Retrieve page.
</span><span class="cx">          *
</span><span class="cx">          * @since 2.2.0
</span></span></pre>
</div>
</div>

</body>
</html>