WPCOM_JSON_API_List_Roles_Endpoint

The Jetpack by WordPress.com WPCOM JSON API List Roles Endpoint class.

Defined (1)

The class is defined in the following location(s).

/json-endpoints/class.wpcom-json-api-list-roles-endpoint.php  
  1. class WPCOM_JSON_API_List_Roles_Endpoint extends WPCOM_JSON_API_Endpoint { 
  2.  
  3. var $response_format = array( 
  4. 'roles' => '(array:role) Array of role objects',  
  5. ); 
  6.  
  7. static function role_sort( $a, $b ) { 
  8. $core_role_names = array( 'administrator', 'editor', 'author', 'contributor', 'subscriber' ); 
  9. $a_is_core_role = in_array( $a->name, $core_role_names ); 
  10. $b_is_core_role = in_array( $b->name, $core_role_names ); 
  11.  
  12. // if $a is a core_role and $b is not, $a always comes first 
  13. if ( $a_is_core_role && ! $b_is_core_role ) { 
  14. return -1; 
  15.  
  16. // if $b is a core_role and $a is not, $b always comes first 
  17. if ( $b_is_core_role && ! $a_is_core_role ) { 
  18. return 1; 
  19.  
  20. // otherwise the one with the > number of capabilities comes first 
  21. $a_cap_count = count( $a->capabilities ); 
  22. $b_cap_count = count( $b->capabilities ); 
  23.  
  24. if ( $a_cap_count === $b_cap_count ) { 
  25. return 0; 
  26.  
  27. return ( $a_cap_count > $b_cap_count ) ? -1 : 1; 
  28.  
  29. // /sites/%s/roles/ -> $blog_id 
  30. function callback( $path = '', $blog_id = 0 ) { 
  31.  
  32. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) ); 
  33. if ( is_wp_error( $blog_id ) ) { 
  34. return $blog_id; 
  35.  
  36. if ( ! current_user_can( 'list_users' ) ) { 
  37. return new WP_Error( 'unauthorized', 'User cannot view roles for specified site', 403 ); 
  38.  
  39. $roles = array(); 
  40.  
  41. global $wp_roles; 
  42. $wp_roles->reinit(); 
  43. $role_names = $wp_roles->get_names(); 
  44. $role_keys = array_keys( $role_names ); 
  45.  
  46. foreach ( (array) $role_keys as $role_key ) { 
  47. $role_details = get_role( $role_key ); 
  48. $role_details->display_name = $role_names[$role_key]; 
  49. $roles[] = $role_details; 
  50.  
  51. // Sort the array so roles with the most number of capabilities comes first, then the next role, and so on 
  52. usort( $roles, array( 'self', 'role_sort' ) ); 
  53.  
  54. return array( 'roles' => $roles );