PluginProbe
WPGraphQL / trunk
WPGraphQL vtrunk
2.22.3 2.22.2 2.22.1 2.22.0 2.21.1 2.21.0 2.20.0 2.19.0 2.18.0 2.17.0 2.16.0 2.15.1 2.15.0 2.14.1 2.14.0 2.13.0 2.2.0 2.3.0 2.3.3 2.3.6 2.3.8 2.5.0 2.5.1 2.5.2 2.5.3 All 177 releases
wp-graphql / src / Model / UserRole.php

UserRole.php in WPGraphQL trunk, at src/Model/UserRole.php

76 lines 1.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WPGraphQL\Model;
4
5 use GraphQLRelay\Relay;
6
7 /**
8 * Class UserRole - Models data for user roles
9 *
10 * @property ?string[] $capabilities
11 * @property ?string $displayName
12 * @property string $id
13 * @property ?string $name
14 *
15 * @package WPGraphQL\Model
16 *
17 * @extends \WPGraphQL\Model\Model<array<string,mixed>>
18 */
19 class UserRole extends Model {
20 /**
21 * UserRole constructor.
22 *
23 * @param array<string,mixed> $user_role The incoming user role to be modeled
24 *
25 * @return void
26 * @throws \Exception
27 */
28 public function __construct( $user_role ) {
29 $this->data = $user_role;
30 parent::__construct();
31 }
32
33 /**
34 * {@inheritDoc}
35 */
36 protected function is_private() {
37 if ( current_user_can( 'list_users' ) ) {
38 return false;
39 }
40
41 $current_user_roles = wp_get_current_user()->roles;
42
43 if ( in_array( $this->data['slug'], $current_user_roles, true ) ) {
44 return false;
45 }
46
47 return true;
48 }
49
50 /**
51 * {@inheritDoc}
52 */
53 protected function init() {
54 if ( empty( $this->fields ) ) {
55 $this->fields = [
56 'capabilities' => function () {
57 if ( empty( $this->data['capabilities'] ) || ! is_array( $this->data['capabilities'] ) ) {
58 return null;
59 }
60
61 return array_keys( $this->data['capabilities'] );
62 },
63 'displayName' => function () {
64 return ! empty( $this->data['displayName'] ) ? esc_html( $this->data['displayName'] ) : null;
65 },
66 'id' => function () {
67 return Relay::toGlobalId( 'user_role', $this->data['id'] );
68 },
69 'name' => function () {
70 return ! empty( $this->data['name'] ) ? esc_html( $this->data['name'] ) : null;
71 },
72 ];
73 }
74 }
75 }
76