| 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 |
|