From 3171bb332a4fe20b3eaefc41595aeacc219aa3bb Mon Sep 17 00:00:00 2001 From: Michiel De Mey Date: Fri, 6 Mar 2015 14:50:38 +0100 Subject: [PATCH 1/2] Added user functions --- lib/fields.js | 14 +++++++++ lib/wordpress.js | 82 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) diff --git a/lib/fields.js b/lib/fields.js index c170f82..fa85ac4 100644 --- a/lib/fields.js +++ b/lib/fields.js @@ -66,6 +66,20 @@ maps.labels = createFieldMaps({ viewItem: "view_item" }); +maps.user = createFieldMaps({ + id: /* string */ "user_id", + userName: /* string */ "username", + firstName: /* string */ "first_name", + lastName: /* string */ "last_name", + bio: /* string */ "bio", + email: /* string */ "email", + nickname: /* string */ "nickname", + nicename: /* string */ "nicename", + url: /* string */ "url", + displayName: /* string */ "display_name", + registered: /* datetime */ "registered" +}); + maps.post = createFieldMaps({ author: /* int */ "post_author", commentStatus: /* string */ "comment_status", diff --git a/lib/wordpress.js b/lib/wordpress.js index 8bd9e91..7bd39b3 100644 --- a/lib/wordpress.js +++ b/lib/wordpress.js @@ -292,6 +292,88 @@ extend( Client.prototype, { } }); +extend( Client.prototype, { + getUsersBlogs: function( fn ) { + this.credentialsCall( "wp.getUsersBlogs", fn); + }, + + getUser: function( id, fields, fn ) { + if ( typeof fields === "function" ) { + fn = fields; + fields = null; + } + + if ( fields ) { + fields = fieldMap.array( fields, "user" ); + } + + this.authenticatedCall( "wp.getUser", id, fields, function( error, user ) { + if ( error ) { + return fn( error ); + } + + fn( null, fieldMap.from( user, "user" ) ); + }); + }, + + getUsers: function ( filter, fields, fn ) { + if ( typeof filter === "function" ) { + fn = filter; + fields = null; + filter = {}; + } + + if ( typeof fields === "function" ) { + fn = fields; + fields = null; + } + + if ( filter.orderby ) { + filter.orderby = fieldMap.array( [ filter.orderby ], "user" )[ 0 ]; + } + + if ( fields ) { + fields = fieldMap.array( fields, "user" ); + } + + this.authenticatedCall( "wp.getUsers", filter, fields, function( error, users ) { + if ( error ) { + return fn( error ); + } + + fn( null, users.map(function( user ) { + return fieldMap.from( user, "user" ); + })); + }); + }, + + getProfile: function ( fields, fn ) { + this.authenticatedCall( "wp.getProfile", fields, function (error, user ) { + if ( error ) { + return fn ( error ); + } + + fn( null, fieldMap.from( user, "user" )); + }); + }, + + editProfile: function ( data, fn ) { + this.authenticatedCall( "wp.editProfile", fieldMap.to( data, "user" ), fn ); + }, + + getAuthors: function( fn ) { + this.authenticatedCall( "wp.getAuthors", function ( error, authors ) { + if ( error ) { + return fn( error ); + } + + fn( null, authors.map(function( author ) { + return fieldMap.from( author, "author" ); + })); + }); + } +}); + module.exports = { Client: Client, From 0b1114a70399f62744aa91d0982737a4cf30c2eb Mon Sep 17 00:00:00 2001 From: Nathan Kleekamp Date: Wed, 28 Sep 2016 13:49:40 -0400 Subject: [PATCH 2/2] added requested changes to pull request 11 --- lib/fields.js | 19 ++++++++++++------ lib/wordpress.js | 27 +++++++++++++++---------- readme.md | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 17 deletions(-) diff --git a/lib/fields.js b/lib/fields.js index fa85ac4..be3593c 100644 --- a/lib/fields.js +++ b/lib/fields.js @@ -67,17 +67,24 @@ maps.labels = createFieldMaps({ }); maps.user = createFieldMaps({ - id: /* string */ "user_id", - userName: /* string */ "username", - firstName: /* string */ "first_name", - lastName: /* string */ "last_name", bio: /* string */ "bio", + displayName: /* string */ "display_name", email: /* string */ "email", - nickname: /* string */ "nickname", + firstName: /* string */ "first_name", + id: /* string */ "user_id", + lastName: /* string */ "last_name", nicename: /* string */ "nicename", + nickname: /* string */ "nickname", + registered: /* datetime */ "registered", + roles: /* string */ "roles", url: /* string */ "url", + userName: /* string */ "username" +}); + +maps.author = createFieldMaps({ + blogId: /* string */ "blog_id", displayName: /* string */ "display_name", - registered: /* datetime */ "registered" + userLogin: /* string */ "user_login" }); maps.post = createFieldMaps({ diff --git a/lib/wordpress.js b/lib/wordpress.js index 7bd39b3..0e553c0 100644 --- a/lib/wordpress.js +++ b/lib/wordpress.js @@ -293,10 +293,6 @@ extend( Client.prototype, { }); extend( Client.prototype, { - getUsersBlogs: function( fn ) { - this.credentialsCall( "wp.getUsersBlogs", fn); - }, - getUser: function( id, fields, fn ) { if ( typeof fields === "function" ) { fn = fields; @@ -316,7 +312,7 @@ extend( Client.prototype, { }); }, - getUsers: function ( filter, fields, fn ) { + getUsers: function( filter, fields, fn ) { if ( typeof filter === "function" ) { fn = filter; fields = null; @@ -347,22 +343,31 @@ extend( Client.prototype, { }); }, - getProfile: function ( fields, fn ) { - this.authenticatedCall( "wp.getProfile", fields, function (error, user ) { + getProfile: function( fields, fn ) { + if ( typeof fields === "function" ) { + fn = fields; + fields = null; + } + + if ( fields ) { + fields = fieldMap.from( fields, "user" ); + } + + this.authenticatedCall( "wp.getProfile", fields, function( error, user ) { if ( error ) { - return fn ( error ); + return fn( error ); } - fn( null, fieldMap.from( user, "user" )); + fn( null, fieldMap.from( user, "user" ) ); }); }, - editProfile: function ( data, fn ) { + editProfile: function( data, fn ) { this.authenticatedCall( "wp.editProfile", fieldMap.to( data, "user" ), fn ); }, getAuthors: function( fn ) { - this.authenticatedCall( "wp.getAuthors", function ( error, authors ) { + this.authenticatedCall( "wp.getAuthors", function( error, authors ) { if ( error ) { return fn( error ); } diff --git a/readme.md b/readme.md index 1e4a9d9..0181ae3 100644 --- a/readme.md +++ b/readme.md @@ -198,6 +198,38 @@ Uploads a file to Wordpress. * `callback` (`function( error, file )`): A callback to invoke when the API call is complete. * `file`: An object containing the file data. +### Users + +#### client.getUser( id [,fields], callback ) + +* `id`: The ID of the user to get. +* `fields`: (optional): An array of fields to return. +* `callback` (`function( error, user )`): A callback to invoke when the API call is complete. + * `user`: An object containing the user data. + +#### client.getUsers( [filter] [, fields], callback ) + +* `filter`: (optional): A hash of key/value pairs for filtering which users to get. +* `fields`: (optional): An array of fields to return. +* `callback`: (`function( error, users )`): A callback to invoke when the API call is complete. + * `users`: An array containing the users. + +#### client.getProfile( [fields], callback ) + +* `fields`: (optional): An array of fields to return. +* `callback`: (`function( error, profile )`): A callback to invoke when the API call is complete. + * `profile`: An object containing the profile data. + +#### client.editProfile( data, callback ) + +* `data`: The data to update the profile +* `callback`: (`function( error )`): A callback to invoke when the API call is complete. + +#### client.getAuthors( callback ) + +* `callback`: (`function( error, authors )`): A callback to invoke when the API call is complete. + * `authors`: An array containing the authors. + ### Utilities #### client.listMethods( callback ) @@ -357,6 +389,26 @@ Invokes a method with the username and password provided by the client. * termId * termTaxonomyId +#### Users + +* bio +* displayName +* email +* firstName +* id +* lastName +* nicename +* nickname +* registered +* roles +* url +* userName + +##### Authors + +* blogId +* displayName +* userLogin ## License