class.wpcom-json-api-list-roles-endpoint.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. new WPCOM_JSON_API_List_Roles_Endpoint( array(
  3. 'description' => 'List the user roles of a site.',
  4. 'group' => '__do_not_document',
  5. 'stat' => 'roles:list',
  6. 'max_version' => '1.1',
  7. 'method' => 'GET',
  8. 'path' => '/sites/%s/roles',
  9. 'path_labels' => array(
  10. '$site' => '(int|string) Site ID or domain',
  11. ),
  12. 'query_parameters' => array(
  13. ),
  14. 'response_format' => array(
  15. 'roles' => '(array:role) Array of role objects.',
  16. ),
  17. 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/82974409/roles',
  18. 'example_request_data' => array(
  19. 'headers' => array(
  20. 'authorization' => 'Bearer YOUR_API_TOKEN'
  21. ),
  22. )
  23. ) );
  24. new WPCOM_JSON_API_List_Roles_Endpoint( array(
  25. 'description' => 'List the user roles of a site.',
  26. 'group' => '__do_not_document',
  27. 'stat' => 'roles:list',
  28. 'min_version' => '1.2',
  29. 'force' => 'wpcom',
  30. 'method' => 'GET',
  31. 'path' => '/sites/%s/roles',
  32. 'path_labels' => array(
  33. '$site' => '(int|string) Site ID or domain',
  34. ),
  35. 'query_parameters' => array(),
  36. 'response_format' => array(
  37. 'roles' => '(array:role) Array of role objects.',
  38. ),
  39. 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/82974409/roles',
  40. 'example_request_data' => array(
  41. 'headers' => array(
  42. 'authorization' => 'Bearer YOUR_API_TOKEN',
  43. ),
  44. ),
  45. ) );
  46. class WPCOM_JSON_API_List_Roles_Endpoint extends WPCOM_JSON_API_Endpoint {
  47. var $response_format = array(
  48. 'roles' => '(array:role) Array of role objects',
  49. );
  50. static function role_sort( $a, $b ) {
  51. $core_role_names = array( 'administrator', 'editor', 'author', 'contributor', 'subscriber' );
  52. $a_is_core_role = in_array( $a->name, $core_role_names );
  53. $b_is_core_role = in_array( $b->name, $core_role_names );
  54. // if $a is a core_role and $b is not, $a always comes first
  55. if ( $a_is_core_role && ! $b_is_core_role ) {
  56. return -1;
  57. }
  58. // if $b is a core_role and $a is not, $b always comes first
  59. if ( $b_is_core_role && ! $a_is_core_role ) {
  60. return 1;
  61. }
  62. // otherwise the one with the > number of capabilities comes first
  63. $a_cap_count = count( $a->capabilities );
  64. $b_cap_count = count( $b->capabilities );
  65. if ( $a_cap_count === $b_cap_count ) {
  66. return 0;
  67. }
  68. return ( $a_cap_count > $b_cap_count ) ? -1 : 1;
  69. }
  70. // /sites/%s/roles/ -> $blog_id
  71. function callback( $path = '', $blog_id = 0 ) {
  72. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
  73. if ( is_wp_error( $blog_id ) ) {
  74. return $blog_id;
  75. }
  76. $roles = array();
  77. $sal_site = $this->get_platform()->get_site( $blog_id );
  78. $wp_roles = $sal_site->get_roles();
  79. // Check if the site is connected and talks to us on a regular basis
  80. $is_connected = $sal_site->is_connected_site();
  81. if ( is_wp_error( $is_connected ) ) {
  82. return $is_connected;
  83. }
  84. if ( ! $sal_site->current_user_can( 'list_users' ) ) {
  85. return new WP_Error( 'unauthorized', 'User cannot view roles for specified site', 403 );
  86. }
  87. if ( method_exists( $wp_roles, 'get_names' ) ) {
  88. $role_names = $wp_roles->get_names();
  89. $role_keys = array_keys( $role_names );
  90. foreach ( (array) $role_keys as $role_key ) {
  91. $role_details = get_role( $role_key );
  92. $role_details->display_name = translate_user_role( $role_names[$role_key] );
  93. $roles[] = $role_details;
  94. }
  95. } else {
  96. // Jetpack Shadow Site side of things.
  97. foreach ( $wp_roles as $role_key => $role ) {
  98. $roles[] = (object) array(
  99. 'name' => $role_key,
  100. 'display_name' => $role['name'],
  101. 'capabilities' => (object) $role['capabilities']
  102. );
  103. }
  104. }
  105. // Sort the array so roles with the most number of capabilities comes first, then the next role, and so on
  106. usort( $roles, array( 'self', 'role_sort' ) );
  107. return array( 'roles' => $roles );
  108. }
  109. }