class.wpcom-json-api-site-user-endpoint.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. new WPCOM_JSON_API_Site_User_Endpoint( array(
  3. 'description' => 'Get details of a user of a site by ID.',
  4. 'group' => '__do_not_document', //'users'
  5. 'stat' => 'sites:1:user',
  6. 'method' => 'GET',
  7. 'path' => '/sites/%s/users/%d',
  8. 'path_labels' => array(
  9. '$site' => '(int|string) Site ID or domain',
  10. '$user_id' => '(int) User ID',
  11. ),
  12. 'response_format' => WPCOM_JSON_API_Site_User_Endpoint::$user_format,
  13. 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/30434183/user/23',
  14. 'example_request_data' => array(
  15. 'headers' => array(
  16. 'authorization' => 'Bearer YOUR_API_TOKEN'
  17. ),
  18. ),
  19. 'example_response' => '{
  20. "ID": 18342963,
  21. "login": "binarysmash",
  22. "email": false,
  23. "name": "binarysmash",
  24. "URL": "http:\/\/binarysmash.wordpress.com",
  25. "avatar_URL": "http:\/\/0.gravatar.com\/avatar\/a178ebb1731d432338e6bb0158720fcc?s=96&d=identicon&r=G",
  26. "profile_URL": "http:\/\/en.gravatar.com\/binarysmash",
  27. "roles": [ "administrator" ]
  28. }'
  29. ) );
  30. new WPCOM_JSON_API_Site_User_Endpoint( array(
  31. 'description' => 'Get details of a user of a site by login.',
  32. 'group' => 'users',
  33. 'stat' => 'sites:1:user',
  34. 'method' => 'GET',
  35. 'path' => '/sites/%s/users/login:%s',
  36. 'path_labels' => array(
  37. '$site' => '(int|string) The site ID or domain.',
  38. '$user_id' => '(string) The user\'s login.',
  39. ),
  40. 'response_format' => WPCOM_JSON_API_Site_User_Endpoint::$user_format,
  41. 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/30434183/user/login:binarysmash',
  42. 'example_request_data' => array(
  43. 'headers' => array(
  44. 'authorization' => 'Bearer YOUR_API_TOKEN'
  45. ),
  46. ),
  47. 'example_response' => '{
  48. "ID": 18342963,
  49. "login": "binarysmash",
  50. "email": false,
  51. "name": "binarysmash",
  52. "URL": "http:\/\/binarysmash.wordpress.com",
  53. "avatar_URL": "http:\/\/0.gravatar.com\/avatar\/a178ebb1731d432338e6bb0158720fcc?s=96&d=identicon&r=G",
  54. "profile_URL": "http:\/\/en.gravatar.com\/binarysmash",
  55. "roles": [ "administrator" ]
  56. }'
  57. ) );
  58. new WPCOM_JSON_API_Site_User_Endpoint( array(
  59. 'description' => 'Update details of a user of a site.',
  60. 'group' => 'users',
  61. 'stat' => 'sites:1:user',
  62. 'method' => 'POST',
  63. 'path' => '/sites/%s/users/%d',
  64. 'path_labels' => array(
  65. '$site' => '(int|string) The site ID or domain.',
  66. '$user_id' => '(int) The user\'s ID.',
  67. ),
  68. 'request_format' => WPCOM_JSON_API_Site_User_Endpoint::$user_format,
  69. 'response_format' => WPCOM_JSON_API_Site_User_Endpoint::$user_format,
  70. 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/30434183/user/23',
  71. 'example_request_data' => array(
  72. 'headers' => array(
  73. 'authorization' => 'Bearer YOUR_API_TOKEN'
  74. ),
  75. 'body' => array(
  76. 'roles' => array(
  77. array(
  78. 'administrator',
  79. )
  80. ),
  81. 'first_name' => 'Rocco',
  82. 'last_name' => 'Tripaldi',
  83. )
  84. ),
  85. 'example_response' => '{
  86. "ID": 18342963,
  87. "login": "binarysmash",
  88. "email": false,
  89. "name": "binarysmash",
  90. "URL": "http:\/\/binarysmash.wordpress.com",
  91. "avatar_URL": "http:\/\/0.gravatar.com\/avatar\/a178ebb1731d432338e6bb0158720fcc?s=96&d=identicon&r=G",
  92. "profile_URL": "http:\/\/en.gravatar.com\/binarysmash",
  93. "roles": [ "administrator" ]
  94. }'
  95. ) );
  96. class WPCOM_JSON_API_Site_User_Endpoint extends WPCOM_JSON_API_Endpoint {
  97. public static $user_format = array(
  98. 'ID' => '(int) The ID of the user',
  99. 'login' => '(string) The login username of the user',
  100. 'email' => '(string) The email of the user',
  101. 'name' => '(string) The name to display for the user',
  102. 'first_name' => '(string) The first name of the user',
  103. 'last_name' => '(string) The last name of the user',
  104. 'nice_name' => '(string) The nice_name to display for the user',
  105. 'URL' => '(string) The primary blog of the user',
  106. 'avatar_URL' => '(url) Gravatar image URL',
  107. 'profile_URL' => '(url) Gravatar Profile URL',
  108. 'site_ID' => '(int) ID of the user\'s primary blog',
  109. 'roles' => '(array|string) The role or roles of the user',
  110. );
  111. // /sites/%s/users/%d -> $blog_id, $user_id
  112. function callback( $path = '', $blog_id = 0, $user_id = 0 ) {
  113. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
  114. if ( is_wp_error( $blog_id ) ) {
  115. return $blog_id;
  116. }
  117. if ( ! current_user_can_for_blog( $blog_id, 'list_users' ) ) {
  118. return new WP_Error( 'unauthorized', 'User cannot view users for specified site', 403 );
  119. }
  120. // Get the user by ID or login
  121. $get_by = false !== strpos( $path, '/users/login:' ) ? 'login' : 'id';
  122. $user = get_user_by( $get_by, $user_id );
  123. if ( ! $user ) {
  124. return new WP_Error( 'unknown_user', 'Unknown user', 404 );
  125. }
  126. if ( ! is_user_member_of_blog( $user->ID, $blog_id ) ) {
  127. return new WP_Error( 'unknown_user_for_site', 'Unknown user for site', 404 );
  128. }
  129. if ( 'GET' === $this->api->method ) {
  130. return $this->get_user( $user->ID );
  131. } else if ( 'POST' === $this->api->method ) {
  132. if ( ! current_user_can_for_blog( $blog_id, 'promote_users' ) ) {
  133. return new WP_Error( 'unauthorized_no_promote_cap', 'User cannot promote users for specified site', 403 );
  134. }
  135. return $this->update_user( $user_id, $blog_id );
  136. } else {
  137. return new WP_Error( 'bad_request', 'An unsupported request method was used.' );
  138. }
  139. }
  140. public function get_user( $user_id ) {
  141. $the_user = $this->get_author( $user_id, true );
  142. if ( $the_user && ! is_wp_error( $the_user ) ) {
  143. $userdata = get_userdata( $user_id );
  144. $the_user->roles = ! is_wp_error( $userdata ) ? array_values( $userdata->roles ) : array();
  145. }
  146. return $the_user;
  147. }
  148. /**
  149. * Updates user data
  150. *
  151. * @return array
  152. */
  153. public function update_user( $user_id, $blog_id ) {
  154. $input = $this->input();
  155. $user['ID'] = $user_id;
  156. $is_wpcom = defined( 'IS_WPCOM' ) && IS_WPCOM;
  157. if ( get_current_user_id() == $user_id && isset( $input['roles'] ) ) {
  158. return new WP_Error( 'unauthorized', 'You cannot change your own role', 403 );
  159. }
  160. if ( $is_wpcom && $user_id !== get_current_user_id() && $user_id == wpcom_get_blog_owner( $blog_id ) ) {
  161. return new WP_Error( 'unauthorized_edit_owner', 'Current user can not edit blog owner', 403 );
  162. }
  163. if ( ! $is_wpcom ) {
  164. foreach ( $input as $key => $value ) {
  165. if ( ! is_array( $value ) ) {
  166. $value = trim( $value );
  167. }
  168. $value = wp_unslash( $value );
  169. switch ( $key ) {
  170. case 'first_name':
  171. case 'last_name':
  172. $user[ $key ] = $value;
  173. break;
  174. case 'display_name':
  175. case 'name':
  176. $user[ 'display_name' ] = $value;
  177. break;
  178. }
  179. }
  180. }
  181. if ( isset( $input[ 'roles' ] ) ) {
  182. // For now, we only use the first role in the array.
  183. if ( is_array( $input['roles'] ) ) {
  184. $user['role'] = $input['roles'][0];
  185. } else if ( is_string( $input['roles'] ) ) {
  186. $user['role'] = $input['roles'];
  187. } else {
  188. return new WP_Error( 'invalid_input', __( 'The roles property must be a string or an array.', 'jetpack' ), 400 );
  189. }
  190. $editable_roles = array_keys( get_editable_roles() );
  191. if ( ! in_array( $user['role'], $editable_roles ) ) {
  192. return new WP_Error( 'invalid_input', sprintf( __( '%s is not a valid role.', 'jetpack' ), $editable_roles ), 400 );
  193. }
  194. }
  195. $result = wp_update_user( $user );
  196. if ( is_wp_error( $result ) ) {
  197. return $result;
  198. }
  199. return $this->get_user( $user_id );
  200. }
  201. }