• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wp-graphql / wp-graphql / 14716683875

28 Apr 2025 07:58PM UTC coverage: 84.287% (+1.6%) from 82.648%
14716683875

push

github

actions-user
release: merge develop into master for v2.3.0

15905 of 18870 relevant lines covered (84.29%)

257.23 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

99.09
/src/Type/ObjectType/User.php
1
<?php
2

3
namespace WPGraphQL\Type\ObjectType;
4

5
use WPGraphQL\Data\Connection\EnqueuedScriptsConnectionResolver;
6
use WPGraphQL\Data\Connection\EnqueuedStylesheetConnectionResolver;
7
use WPGraphQL\Data\Connection\PostObjectConnectionResolver;
8
use WPGraphQL\Data\Connection\UserRoleConnectionResolver;
9
use WPGraphQL\Data\DataSource;
10
use WPGraphQL\Model\User as UserModel;
11
use WPGraphQL\Type\Connection\PostObjects;
12

13
/**
14
 * Class User
15
 *
16
 * @package WPGraphQL\Type\Object
17
 */
18
class User {
19

20
        /**
21
         * Registers the User type
22
         *
23
         * @return void
24
         */
25
        public static function register_type() {
593✔
26
                register_graphql_object_type(
593✔
27
                        'User',
593✔
28
                        [
593✔
29
                                'description' => static function () {
593✔
30
                                        return __( 'A registered user account. Users can be assigned roles, author content, and have various capabilities within the site.', 'wp-graphql' );
18✔
31
                                },
593✔
32
                                'model'       => UserModel::class,
593✔
33
                                'interfaces'  => [ 'Node', 'UniformResourceIdentifiable', 'Commenter', 'DatabaseIdentifier' ],
593✔
34
                                'connections' => [
593✔
35
                                        'enqueuedScripts'     => [
593✔
36
                                                'toType'  => 'EnqueuedScript',
593✔
37
                                                'resolve' => static function ( $source, $args, $context, $info ) {
593✔
38
                                                        $resolver = new EnqueuedScriptsConnectionResolver( $source, $args, $context, $info );
3✔
39

40
                                                        return $resolver->get_connection();
3✔
41
                                                },
593✔
42
                                        ],
593✔
43
                                        'enqueuedStylesheets' => [
593✔
44
                                                'toType'  => 'EnqueuedStylesheet',
593✔
45
                                                'resolve' => static function ( $source, $args, $context, $info ) {
593✔
46
                                                        $resolver = new EnqueuedStylesheetConnectionResolver( $source, $args, $context, $info );
3✔
47

48
                                                        return $resolver->get_connection();
3✔
49
                                                },
593✔
50
                                        ],
593✔
51
                                        'revisions'           => [
593✔
52
                                                'toType'             => 'ContentNode',
593✔
53
                                                'connectionTypeName' => 'UserToRevisionsConnection',
593✔
54
                                                'queryClass'         => 'WP_Query',
593✔
55
                                                'description'        => static function () {
593✔
56
                                                        return __( 'Connection between the User and Revisions authored by the user', 'wp-graphql' );
16✔
57
                                                },
593✔
58
                                                'connectionArgs'     => PostObjects::get_connection_args(),
593✔
59
                                                'resolve'            => static function ( $root, $args, $context, $info ) {
593✔
60
                                                        $resolver = new PostObjectConnectionResolver( $root, $args, $context, $info, 'revision' );
×
61

62
                                                        return $resolver->get_connection();
×
63
                                                },
593✔
64
                                        ],
593✔
65
                                        'roles'               => [
593✔
66
                                                'toType'        => 'UserRole',
593✔
67
                                                'fromFieldName' => 'roles',
593✔
68
                                                'resolve'       => static function ( UserModel $user, $args, $context, $info ) {
593✔
69
                                                        $resolver = new UserRoleConnectionResolver( $user, $args, $context, $info );
15✔
70

71
                                                        // abort if no roles are set
72
                                                        if ( empty( $user->roles ) ) {
15✔
73
                                                                return null;
6✔
74
                                                        }
75

76
                                                        // Only get roles matching the slugs of the roles belonging to the user
77
                                                        $resolver->set_query_arg( 'slugIn', $user->roles );
13✔
78
                                                        return $resolver->get_connection();
13✔
79
                                                },
593✔
80
                                        ],
593✔
81
                                ],
593✔
82
                                'fields'      => static function () {
593✔
83
                                        return [
204✔
84
                                                'id'                     => [
204✔
85
                                                        'description' => static function () {
204✔
86
                                                                return __( 'The globally unique identifier for the user object.', 'wp-graphql' );
18✔
87
                                                        },
204✔
88
                                                ],
204✔
89
                                                'capabilities'           => [
204✔
90
                                                        'type'        => [
204✔
91
                                                                'list_of' => 'String',
204✔
92
                                                        ],
204✔
93
                                                        'description' => static function () {
204✔
94
                                                                return __( 'A list of capabilities (permissions) granted to the user', 'wp-graphql' );
18✔
95
                                                        },
204✔
96
                                                ],
204✔
97
                                                'capKey'                 => [
204✔
98
                                                        'type'        => 'String',
204✔
99
                                                        'description' => static function () {
204✔
100
                                                                return __( 'User metadata option name. Usually it will be "wp_capabilities".', 'wp-graphql' );
18✔
101
                                                        },
204✔
102
                                                ],
204✔
103
                                                'databaseId'             => [
204✔
104
                                                        'type'        => [ 'non_null' => 'Int' ],
204✔
105
                                                        'description' => static function () {
204✔
106
                                                                return __( 'Identifies the primary key from the database.', 'wp-graphql' );
18✔
107
                                                        },
204✔
108
                                                        'resolve'     => static function ( \WPGraphQL\Model\User $user ) {
204✔
109
                                                                return (int) $user->databaseId;
44✔
110
                                                        },
204✔
111
                                                ],
204✔
112
                                                'description'            => [
204✔
113
                                                        'type'        => 'String',
204✔
114
                                                        'description' => static function () {
204✔
115
                                                                return __( 'Description of the user.', 'wp-graphql' );
18✔
116
                                                        },
204✔
117
                                                ],
204✔
118
                                                'email'                  => [
204✔
119
                                                        'type'        => 'String',
204✔
120
                                                        'description' => static function () {
204✔
121
                                                                return __( 'Email address of the user. This is equivalent to the WP_User->user_email property.', 'wp-graphql' );
18✔
122
                                                        },
204✔
123
                                                ],
204✔
124
                                                'extraCapabilities'      => [
204✔
125
                                                        'type'        => [
204✔
126
                                                                'list_of' => 'String',
204✔
127
                                                        ],
204✔
128
                                                        'description' => static function () {
204✔
129
                                                                return __( 'A complete list of capabilities including capabilities inherited from a role. This is equivalent to the array keys of WP_User->allcaps.', 'wp-graphql' );
18✔
130
                                                        },
204✔
131
                                                ],
204✔
132
                                                'firstName'              => [
204✔
133
                                                        'type'        => 'String',
204✔
134
                                                        'description' => static function () {
204✔
135
                                                                return __( 'First name of the user. This is equivalent to the WP_User->user_first_name property.', 'wp-graphql' );
18✔
136
                                                        },
204✔
137
                                                ],
204✔
138
                                                'lastName'               => [
204✔
139
                                                        'type'        => 'String',
204✔
140
                                                        'description' => static function () {
204✔
141
                                                                return __( 'Last name of the user. This is equivalent to the WP_User->user_last_name property.', 'wp-graphql' );
18✔
142
                                                        },
204✔
143
                                                ],
204✔
144

145
                                                'username'               => [
204✔
146
                                                        'type'        => 'String',
204✔
147
                                                        'description' => static function () {
204✔
148
                                                                return __( 'Username for the user. This field is equivalent to WP_User->user_login.', 'wp-graphql' );
18✔
149
                                                        },
204✔
150
                                                ],
204✔
151
                                                'name'                   => [
204✔
152
                                                        'type'        => 'String',
204✔
153
                                                        'description' => static function () {
204✔
154
                                                                return __( 'Display name of the user. This is equivalent to the WP_User->display_name property.', 'wp-graphql' );
18✔
155
                                                        },
204✔
156
                                                ],
204✔
157
                                                'registeredDate'         => [
204✔
158
                                                        'type'        => 'String',
204✔
159
                                                        'description' => static function () {
204✔
160
                                                                return __( 'The date the user registered or was created. The field follows a full ISO8601 date string format.', 'wp-graphql' );
18✔
161
                                                        },
204✔
162
                                                ],
204✔
163
                                                'nickname'               => [
204✔
164
                                                        'type'        => 'String',
204✔
165
                                                        'description' => static function () {
204✔
166
                                                                return __( 'Nickname of the user.', 'wp-graphql' );
18✔
167
                                                        },
204✔
168
                                                ],
204✔
169
                                                'url'                    => [
204✔
170
                                                        'type'        => 'String',
204✔
171
                                                        'description' => static function () {
204✔
172
                                                                return __( 'A website url that is associated with the user.', 'wp-graphql' );
18✔
173
                                                        },
204✔
174
                                                ],
204✔
175
                                                'slug'                   => [
204✔
176
                                                        'type'        => 'String',
204✔
177
                                                        'description' => static function () {
204✔
178
                                                                return __( 'The slug for the user. This field is equivalent to WP_User->user_nicename', 'wp-graphql' );
18✔
179
                                                        },
204✔
180
                                                ],
204✔
181
                                                'nicename'               => [
204✔
182
                                                        'type'        => 'String',
204✔
183
                                                        'description' => static function () {
204✔
184
                                                                return __( 'The nicename for the user. This field is equivalent to WP_User->user_nicename', 'wp-graphql' );
18✔
185
                                                        },
204✔
186
                                                ],
204✔
187
                                                'locale'                 => [
204✔
188
                                                        'type'        => 'String',
204✔
189
                                                        'description' => static function () {
204✔
190
                                                                return __( 'The preferred language locale set for the user. Value derived from get_user_locale().', 'wp-graphql' );
18✔
191
                                                        },
204✔
192
                                                ],
204✔
193
                                                'userId'                 => [
204✔
194
                                                        'type'              => 'Int',
204✔
195
                                                        'description'       => static function () {
204✔
196
                                                                return __( 'The Id of the user. Equivalent to WP_User->ID', 'wp-graphql' );
18✔
197
                                                        },
204✔
198
                                                        'deprecationReason' => static function () {
204✔
199
                                                                return __( 'Deprecated in favor of the databaseId field', 'wp-graphql' );
18✔
200
                                                        },
204✔
201
                                                ],
204✔
202
                                                'isRestricted'           => [
204✔
203
                                                        'type'        => 'Boolean',
204✔
204
                                                        'description' => static function () {
204✔
205
                                                                return __( 'Whether the object is restricted from the current viewer', 'wp-graphql' );
18✔
206
                                                        },
204✔
207
                                                ],
204✔
208
                                                'shouldShowAdminToolbar' => [
204✔
209
                                                        'type'        => 'Boolean',
204✔
210
                                                        'description' => static function () {
204✔
211
                                                                return __( 'Whether the Toolbar should be displayed when the user is viewing the site.', 'wp-graphql' );
18✔
212
                                                        },
204✔
213
                                                ],
204✔
214
                                                'avatar'                 => [
204✔
215
                                                        'args'    => [
204✔
216
                                                                'size'         => [
204✔
217
                                                                        'type'         => 'Int',
204✔
218
                                                                        'description'  => static function () {
204✔
219
                                                                                return __( 'The size attribute of the avatar field can be used to fetch avatars of different sizes. The value corresponds to the dimension in pixels to fetch. The default is 96 pixels.', 'wp-graphql' );
18✔
220
                                                                        },
204✔
221
                                                                        'defaultValue' => 96,
204✔
222
                                                                ],
204✔
223
                                                                'forceDefault' => [
204✔
224
                                                                        'type'        => 'Boolean',
204✔
225
                                                                        'description' => static function () {
204✔
226
                                                                                return __( 'Whether to always show the default image, never the Gravatar. Default false', 'wp-graphql' );
18✔
227
                                                                        },
204✔
228
                                                                ],
204✔
229
                                                                'rating'       => [
204✔
230
                                                                        'type'        => 'AvatarRatingEnum',
204✔
231
                                                                        'description' => static function () {
204✔
232
                                                                                return __( 'The rating level of the avatar.', 'wp-graphql' );
18✔
233
                                                                        },
204✔
234
                                                                ],
204✔
235

236
                                                        ],
204✔
237
                                                        'resolve' => static function ( $user, $args ) {
204✔
238
                                                                $avatar_args = [];
6✔
239
                                                                if ( is_numeric( $args['size'] ) ) {
6✔
240
                                                                        $avatar_args['size'] = absint( $args['size'] );
6✔
241
                                                                        if ( ! $avatar_args['size'] ) {
6✔
242
                                                                                $avatar_args['size'] = 96;
1✔
243
                                                                        }
244
                                                                }
245

246
                                                                if ( ! empty( $args['forceDefault'] ) && true === $args['forceDefault'] ) {
6✔
247
                                                                        $avatar_args['force_default'] = true;
1✔
248
                                                                }
249

250
                                                                if ( ! empty( $args['rating'] ) ) {
6✔
251
                                                                        $avatar_args['rating'] = esc_sql( $args['rating'] );
1✔
252
                                                                }
253

254
                                                                return DataSource::resolve_avatar( $user->userId, $avatar_args );
6✔
255
                                                        },
204✔
256
                                                ],
204✔
257
                                        ];
204✔
258
                                },
593✔
259
                        ]
593✔
260
                );
593✔
261
        }
262
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc