← All changes
|
json-endpoints/class.wpcom-json-api-list-roles-endpoint.php
+12
-15
12.7.3
→
16.3-a.1
View file →
| @@ -1,6 +1,10 @@ | ||
| 1 | 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 | 2 | |
| 3 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 4 | + exit( 0 ); | |
| 5 | +} | |
| 6 | + | |
| 3 | 7 | /** |
| 4 | 8 | * List roles endpoint. |
| 5 | 9 | */ |
| 6 | 10 | new WPCOM_JSON_API_List_Roles_Endpoint( |
| @@ -61,8 +65,10 @@ | ||
| 61 | 65 | /** |
| 62 | 66 | * List Roles endpoint class. |
| 63 | 67 | * |
| 64 | 68 | * /sites/%s/roles/ -> $blog_id |
| 69 | + * | |
| 70 | + * @phan-constructor-used-for-side-effects | |
| 65 | 71 | */ |
| 66 | 72 | class WPCOM_JSON_API_List_Roles_Endpoint extends WPCOM_JSON_API_Endpoint { |
| 67 | 73 | |
| 68 | 74 | /** |
| @@ -84,27 +90,18 @@ | ||
| 84 | 90 | $core_role_names = array( 'administrator', 'editor', 'author', 'contributor', 'subscriber' ); |
| 85 | 91 | $a_is_core_role = in_array( $a->name, $core_role_names, true ); |
| 86 | 92 | $b_is_core_role = in_array( $b->name, $core_role_names, true ); |
| 87 | 93 | |
| 88 | - // if $a is a core_role and $b is not, $a always comes first. | |
| 89 | - if ( $a_is_core_role && ! $b_is_core_role ) { | |
| 90 | - return -1; | |
| 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; | |
| 91 | 97 | } |
| 92 | 98 | |
| 93 | - // if $b is a core_role and $a is not, $b always comes first. | |
| 94 | - if ( $b_is_core_role && ! $a_is_core_role ) { | |
| 95 | - return 1; | |
| 96 | - } | |
| 97 | - | |
| 98 | 99 | // otherwise the one with the > number of capabilities comes first. |
| 99 | 100 | $a_cap_count = is_countable( $a->capabilities ) ? count( $a->capabilities ) : 0; |
| 100 | 101 | $b_cap_count = is_countable( $b->capabilities ) ? count( $b->capabilities ) : 0; |
| 101 | 102 | |
| 102 | - if ( $a_cap_count === $b_cap_count ) { | |
| 103 | - return 0; | |
| 104 | - } | |
| 105 | - | |
| 106 | - return ( $a_cap_count > $b_cap_count ) ? -1 : 1; | |
| 103 | + return $b_cap_count <=> $a_cap_count; | |
| 107 | 104 | } |
| 108 | 105 | |
| 109 | 106 | /** |
| 110 | 107 | * API callback. |
| @@ -132,9 +129,9 @@ | ||
| 132 | 129 | if ( ! $sal_site->current_user_can( 'list_users' ) ) { |
| 133 | 130 | return new WP_Error( 'unauthorized', 'User cannot view roles for specified site', 403 ); |
| 134 | 131 | } |
| 135 | 132 | |
| 136 | - if ( method_exists( $wp_roles, 'get_names' ) ) { | |
| 133 | + if ( $wp_roles instanceof WP_Roles ) { | |
| 137 | 134 | $role_names = $wp_roles->get_names(); |
| 138 | 135 | |
| 139 | 136 | $role_keys = array_keys( $role_names ); |
| 140 | 137 | |
| @@ -142,9 +139,9 @@ | ||
| 142 | 139 | $role_details = get_role( $role_key ); |
| 143 | 140 | $role_details->display_name = translate_user_role( $role_names[ $role_key ] ); |
| 144 | 141 | $roles[] = $role_details; |
| 145 | 142 | } |
| 146 | - } else { | |
| 143 | + } elseif ( is_array( $wp_roles ) ) { | |
| 147 | 144 | // Jetpack Shadow Site side of things. |
| 148 | 145 | foreach ( $wp_roles as $role_key => $role ) { |
| 149 | 146 | $roles[] = (object) array( |
| 150 | 147 | 'name' => $role_key, |