Completed
Push — update/dialogue-focus-on-conte... ( 9f1745...fa862f )
by
unknown
80:03 queued 71:18
created

class.wpcom-json-api-list-roles-endpoint.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
107
		}
108
109
		if ( method_exists( $wp_roles, 'get_names' ) ) {
110
			$role_names = $wp_roles->get_names();
111
112
			$role_keys = array_keys( $role_names );
113
114
			foreach ( (array) $role_keys as $role_key ) {
115
				$role_details = get_role( $role_key );
116
				$role_details->display_name = translate_user_role( $role_names[$role_key] );
117
				$roles[] = $role_details;
118
			}
119
		} else {
120
			// Jetpack Shadow Site side of things.
121
			foreach ( $wp_roles as $role_key => $role ) {
122
				$roles[] = (object) array(
123
					'name' => $role_key,
124
					'display_name' => $role['name'],
125
					'capabilities' => (object) $role['capabilities']
126
				);
127
			}
128
		}
129
130
		// Sort the array so roles with the most number of capabilities comes first, then the next role, and so on
131
		usort( $roles, array( 'self', 'role_sort' ) );
132
133
		/**
134
		 * Filter for curating the list of roles available for a wpcom site.
135
		 *
136
		 * @module json-api
137
		 *
138
		 * @since 8.7.0
139
		 *
140
		 * @param array $roles List of role objects available to the site.
141
		 */
142
		$roles = apply_filters( 'wpcom_api_site_roles', $roles );
143
144
		return array( 'roles' => $roles );
145
	}
146
}
147