PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3
Jetpack – WP Security, Backup, Speed, & Growth v16.3
16.3 16.3-beta 16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 All 508 releases
← All changes | json-endpoints/class.wpcom-json-api-list-roles-endpoint.php +169 -0 16.2-beta → 16.3 View file →
@@ -1,0 +1,169 @@
1 +<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2 +
3 +if ( ! defined( 'ABSPATH' ) ) {
4 + exit( 0 );
5 +}
6 +
7 +/**
8 + * List roles endpoint.
9 + */
10 +new WPCOM_JSON_API_List_Roles_Endpoint(
11 + array(
12 + 'description' => 'List the user roles of a site.',
13 + 'group' => '__do_not_document',
14 + 'stat' => 'roles:list',
15 + 'max_version' => '1.1',
16 + 'method' => 'GET',
17 + 'path' => '/sites/%s/roles',
18 + 'path_labels' => array(
19 + '$site' => '(int|string) Site ID or domain',
20 + ),
21 +
22 + 'query_parameters' => array(),
23 +
24 + 'response_format' => array(
25 + 'roles' => '(array:role) Array of role objects.',
26 + ),
27 +
28 + 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/82974409/roles',
29 + 'example_request_data' => array(
30 + 'headers' => array(
31 + 'authorization' => 'Bearer YOUR_API_TOKEN',
32 + ),
33 + ),
34 + )
35 +);
36 +
37 +new WPCOM_JSON_API_List_Roles_Endpoint(
38 + array(
39 + 'description' => 'List the user roles of a site.',
40 + 'group' => '__do_not_document',
41 + 'stat' => 'roles:list',
42 + 'min_version' => '1.2',
43 + 'force' => 'wpcom',
44 + 'method' => 'GET',
45 + 'path' => '/sites/%s/roles',
46 + 'path_labels' => array(
47 + '$site' => '(int|string) Site ID or domain',
48 + ),
49 +
50 + 'query_parameters' => array(),
51 +
52 + 'response_format' => array(
53 + 'roles' => '(array:role) Array of role objects.',
54 + ),
55 +
56 + 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/82974409/roles',
57 + 'example_request_data' => array(
58 + 'headers' => array(
59 + 'authorization' => 'Bearer YOUR_API_TOKEN',
60 + ),
61 + ),
62 + )
63 +);
64 +
65 +/**
66 + * List Roles endpoint class.
67 + *
68 + * /sites/%s/roles/ -> $blog_id
69 + *
70 + * @phan-constructor-used-for-side-effects
71 + */
72 +class WPCOM_JSON_API_List_Roles_Endpoint extends WPCOM_JSON_API_Endpoint {
73 +
74 + /**
75 + * Response format.
76 + *
77 + * @var array
78 + */
79 + public $response_format = array(
80 + 'roles' => '(array:role) Array of role objects',
81 + );
82 +
83 + /**
84 + * Sort so roles with the most number of capabilities comes first, then the next role, and so on.
85 + *
86 + * @param object $a - the first object we're comparing.
87 + * @param object $b - the second object we're comparing.
88 + */
89 + public static function role_sort( $a, $b ) {
90 + $core_role_names = array( 'administrator', 'editor', 'author', 'contributor', 'subscriber' );
91 + $a_is_core_role = in_array( $a->name, $core_role_names, true );
92 + $b_is_core_role = in_array( $b->name, $core_role_names, true );
93 +
94 + // Core roles always come before non-core roles.
95 + if ( $a_is_core_role !== $b_is_core_role ) {
96 + return $b_is_core_role <=> $a_is_core_role;
97 + }
98 +
99 + // otherwise the one with the > number of capabilities comes first.
100 + $a_cap_count = is_countable( $a->capabilities ) ? count( $a->capabilities ) : 0;
101 + $b_cap_count = is_countable( $b->capabilities ) ? count( $b->capabilities ) : 0;
102 +
103 + return $b_cap_count <=> $a_cap_count;
104 + }
105 +
106 + /**
107 + * API callback.
108 + *
109 + * @param string $path - the path.
110 + * @param string $blog_id - the blog ID.
111 + */
112 + public function callback( $path = '', $blog_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 +
118 + $roles = array();
119 +
120 + $sal_site = $this->get_platform()->get_site( $blog_id );
121 + $wp_roles = $sal_site->get_roles();
122 +
123 + // Check if the site is connected and talks to us on a regular basis.
124 + $is_connected = $sal_site->is_connected_site();
125 + if ( is_wp_error( $is_connected ) ) {
126 + return $is_connected;
127 + }
128 +
129 + if ( ! $sal_site->current_user_can( 'list_users' ) ) {
130 + return new WP_Error( 'unauthorized', 'User cannot view roles for specified site', 403 );
131 + }
132 +
133 + if ( $wp_roles instanceof WP_Roles ) {
134 + $role_names = $wp_roles->get_names();
135 +
136 + $role_keys = array_keys( $role_names );
137 +
138 + foreach ( (array) $role_keys as $role_key ) {
139 + $role_details = get_role( $role_key );
140 + $role_details->display_name = translate_user_role( $role_names[ $role_key ] );
141 + $roles[] = $role_details;
142 + }
143 + } elseif ( is_array( $wp_roles ) ) {
144 + // Jetpack Shadow Site side of things.
145 + foreach ( $wp_roles as $role_key => $role ) {
146 + $roles[] = (object) array(
147 + 'name' => $role_key,
148 + 'display_name' => $role['name'],
149 + 'capabilities' => (object) $role['capabilities'],
150 + );
151 + }
152 + }
153 +
154 + usort( $roles, array( 'self', 'role_sort' ) );
155 +
156 + /**
157 + * Filter for curating the list of roles available for a wpcom site.
158 + *
159 + * @module json-api
160 + *
161 + * @since 8.7.0
162 + *
163 + * @param array $roles List of role objects available to the site.
164 + */
165 + $roles = apply_filters( 'wpcom_api_site_roles', $roles );
166 +
167 + return array( 'roles' => $roles );
168 + }
169 +}