• 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

95.38
/src/Type/Connection/Users.php
1
<?php
2
namespace WPGraphQL\Type\Connection;
3

4
use GraphQL\Type\Definition\ResolveInfo;
5
use WPGraphQL\AppContext;
6
use WPGraphQL\Data\Connection\UserConnectionResolver;
7
use WPGraphQL\Data\DataSource;
8
use WPGraphQL\Model\Post;
9
use WPGraphQL\Utils\Utils;
10

11
/**
12
 * Class Users
13
 *
14
 * This class organizes the registration of connections to Users
15
 *
16
 * @package WPGraphQL\Type\Connection
17
 */
18
class Users {
19

20
        /**
21
         * Register connections to Users
22
         *
23
         * @return void
24
         */
25
        public static function register_connections() {
593✔
26

27
                /**
28
                 * Connection from RootQuery to Users
29
                 */
30
                register_graphql_connection(
593✔
31
                        [
593✔
32
                                'fromType'       => 'RootQuery',
593✔
33
                                'toType'         => 'User',
593✔
34
                                'fromFieldName'  => 'users',
593✔
35
                                'resolve'        => static function ( $source, $args, $context, $info ) {
593✔
36
                                        return DataSource::resolve_users_connection( $source, $args, $context, $info );
51✔
37
                                },
593✔
38
                                'connectionArgs' => self::get_connection_args(),
593✔
39
                        ]
593✔
40
                );
593✔
41

42
                register_graphql_connection(
593✔
43
                        [
593✔
44
                                'fromType'           => 'ContentNode',
593✔
45
                                'toType'             => 'User',
593✔
46
                                'connectionTypeName' => 'ContentNodeToEditLockConnection',
593✔
47
                                'edgeFields'         => [
593✔
48
                                        'lockTimestamp' => [
593✔
49
                                                'type'        => 'String',
593✔
50
                                                'description' => static function () {
593✔
51
                                                        return __( 'The timestamp for when the node was last edited', 'wp-graphql' );
14✔
52
                                                },
593✔
53
                                                'resolve'     => static function ( $edge ) {
593✔
54
                                                        if ( isset( $edge['source'] ) && ( $edge['source'] instanceof Post ) ) {
×
55
                                                                $edit_lock = $edge['source']->editLock;
×
56
                                                                $time      = ( is_array( $edit_lock ) && ! empty( $edit_lock[0] ) ) ? $edit_lock[0] : null;
×
57
                                                                return ! empty( $time ) ? Utils::prepare_date_response( $time, gmdate( 'Y-m-d H:i:s', (int) $time ) ) : null;
×
58
                                                        }
59
                                                        return null;
×
60
                                                },
593✔
61
                                        ],
593✔
62
                                ],
593✔
63
                                'fromFieldName'      => 'editingLockedBy',
593✔
64
                                'description'        => static function () {
593✔
65
                                        return __( 'If a user has edited the node within the past 15 seconds, this will return the user that last edited. Null if the edit lock doesn\'t exist or is greater than 15 seconds', 'wp-graphql' );
18✔
66
                                },
593✔
67
                                'oneToOne'           => true,
593✔
68
                                'resolve'            => static function ( Post $source, $args, $context, $info ) {
593✔
69
                                        if ( ! isset( $source->editLock[1] ) || ! absint( $source->editLock[1] ) ) {
3✔
70
                                                return null;
3✔
71
                                        }
72

73
                                        $resolver = new UserConnectionResolver( $source, $args, $context, $info );
×
74
                                        $resolver->one_to_one()->set_query_arg( 'include', [ absint( $source->editLock[1] ) ] );
×
75

76
                                        return $resolver->get_connection();
×
77
                                },
593✔
78
                        ]
593✔
79
                );
593✔
80

81
                register_graphql_connection(
593✔
82
                        [
593✔
83
                                'fromType'           => 'ContentNode',
593✔
84
                                'toType'             => 'User',
593✔
85
                                'fromFieldName'      => 'lastEditedBy',
593✔
86
                                'connectionTypeName' => 'ContentNodeToEditLastConnection',
593✔
87
                                'description'        => static function () {
593✔
88
                                        return __( 'The user that most recently edited the node', 'wp-graphql' );
18✔
89
                                },
593✔
90
                                'oneToOne'           => true,
593✔
91
                                'resolve'            => static function ( Post $source, $args, $context, $info ) {
593✔
92
                                        if ( empty( $source->editLastId ) ) {
3✔
93
                                                return null;
×
94
                                        }
95

96
                                        $resolver = new UserConnectionResolver( $source, $args, $context, $info );
3✔
97
                                        $resolver->set_query_arg( 'include', [ absint( $source->editLastId ) ] );
3✔
98
                                        return $resolver->one_to_one()->get_connection();
3✔
99
                                },
593✔
100
                        ]
593✔
101
                );
593✔
102

103
                register_graphql_connection(
593✔
104
                        [
593✔
105
                                'fromType'      => 'NodeWithAuthor',
593✔
106
                                'toType'        => 'User',
593✔
107
                                'fromFieldName' => 'author',
593✔
108
                                'oneToOne'      => true,
593✔
109
                                'resolve'       => static function ( Post $post, $args, AppContext $context, ResolveInfo $info ) {
593✔
110
                                        if ( empty( $post->authorDatabaseId ) ) {
26✔
111
                                                return null;
4✔
112
                                        }
113

114
                                        $resolver = new UserConnectionResolver( $post, $args, $context, $info );
22✔
115
                                        $resolver->set_query_arg( 'include', [ absint( $post->authorDatabaseId ) ] );
22✔
116
                                        return $resolver->one_to_one()->get_connection();
22✔
117
                                },
593✔
118
                        ]
593✔
119
                );
593✔
120
        }
121

122
        /**
123
         * Returns the connection args for use in the connection
124
         *
125
         * @return array<string,array<string,mixed>>
126
         */
127
        public static function get_connection_args() {
593✔
128
                return [
593✔
129
                        'role'              => [
593✔
130
                                'type'        => 'UserRoleEnum',
593✔
131
                                'description' => static function () {
593✔
132
                                        return __( 'An array of role names that users must match to be included in results. Note that this is an inclusive list: users must match *each* role.', 'wp-graphql' );
15✔
133
                                },
593✔
134
                        ],
593✔
135
                        'roleIn'            => [
593✔
136
                                'type'        => [
593✔
137
                                        'list_of' => 'UserRoleEnum',
593✔
138
                                ],
593✔
139
                                'description' => static function () {
593✔
140
                                        return __( 'An array of role names. Matched users must have at least one of these roles.', 'wp-graphql' );
15✔
141
                                },
593✔
142
                        ],
593✔
143
                        'roleNotIn'         => [
593✔
144
                                'type'        => [
593✔
145
                                        'list_of' => 'UserRoleEnum',
593✔
146
                                ],
593✔
147
                                'description' => static function () {
593✔
148
                                        return __( 'An array of role names to exclude. Users matching one or more of these roles will not be included in results.', 'wp-graphql' );
15✔
149
                                },
593✔
150
                        ],
593✔
151
                        'include'           => [
593✔
152
                                'type'        => [
593✔
153
                                        'list_of' => 'Int',
593✔
154
                                ],
593✔
155
                                'description' => static function () {
593✔
156
                                        return __( 'Array of userIds to include.', 'wp-graphql' );
15✔
157
                                },
593✔
158
                        ],
593✔
159
                        'exclude'           => [ // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude
593✔
160
                                'type'        => [
593✔
161
                                        'list_of' => 'Int',
593✔
162
                                ],
593✔
163
                                'description' => static function () {
593✔
164
                                        return __( 'Array of userIds to exclude.', 'wp-graphql' );
15✔
165
                                },
593✔
166
                        ],
593✔
167
                        'search'            => [
593✔
168
                                'type'        => 'String',
593✔
169
                                'description' => static function () {
593✔
170
                                        return __( 'Search keyword. Searches for possible string matches on columns. When "searchColumns" is left empty, it tries to determine which column to search in based on search string.', 'wp-graphql' );
15✔
171
                                },
593✔
172
                        ],
593✔
173
                        'searchColumns'     => [
593✔
174
                                'type'        => [
593✔
175
                                        'list_of' => 'UsersConnectionSearchColumnEnum',
593✔
176
                                ],
593✔
177
                                'description' => static function () {
593✔
178
                                        return __( 'Array of column names to be searched. Accepts \'ID\', \'login\', \'nicename\', \'email\', \'url\'.', 'wp-graphql' );
15✔
179
                                },
593✔
180
                        ],
593✔
181
                        'hasPublishedPosts' => [
593✔
182
                                'type'        => [
593✔
183
                                        'list_of' => 'ContentTypeEnum',
593✔
184
                                ],
593✔
185
                                'description' => static function () {
593✔
186
                                        return __( 'Pass an array of post types to filter results to users who have published posts in those post types.', 'wp-graphql' );
14✔
187
                                },
593✔
188
                        ],
593✔
189
                        'nicename'          => [
593✔
190
                                'type'        => 'String',
593✔
191
                                'description' => static function () {
593✔
192
                                        return __( 'The user nicename.', 'wp-graphql' );
15✔
193
                                },
593✔
194
                        ],
593✔
195
                        'nicenameIn'        => [
593✔
196
                                'type'        => [
593✔
197
                                        'list_of' => 'String',
593✔
198
                                ],
593✔
199
                                'description' => static function () {
593✔
200
                                        return __( 'An array of nicenames to include. Users matching one of these nicenames will be included in results.', 'wp-graphql' );
15✔
201
                                },
593✔
202
                        ],
593✔
203
                        'nicenameNotIn'     => [
593✔
204
                                'type'        => [
593✔
205
                                        'list_of' => 'String',
593✔
206
                                ],
593✔
207
                                'description' => static function () {
593✔
208
                                        return __( 'An array of nicenames to exclude. Users matching one of these nicenames will not be included in results.', 'wp-graphql' );
15✔
209
                                },
593✔
210
                        ],
593✔
211
                        'login'             => [
593✔
212
                                'type'        => 'String',
593✔
213
                                'description' => static function () {
593✔
214
                                        return __( 'The user login.', 'wp-graphql' );
15✔
215
                                },
593✔
216
                        ],
593✔
217
                        'loginIn'           => [
593✔
218
                                'type'        => [
593✔
219
                                        'list_of' => 'String',
593✔
220
                                ],
593✔
221
                                'description' => static function () {
593✔
222
                                        return __( 'An array of logins to include. Users matching one of these logins will be included in results.', 'wp-graphql' );
15✔
223
                                },
593✔
224
                        ],
593✔
225
                        'loginNotIn'        => [
593✔
226
                                'type'        => [
593✔
227
                                        'list_of' => 'String',
593✔
228
                                ],
593✔
229
                                'description' => static function () {
593✔
230
                                        return __( 'An array of logins to exclude. Users matching one of these logins will not be included in results.', 'wp-graphql' );
15✔
231
                                },
593✔
232
                        ],
593✔
233
                        'orderby'           => [
593✔
234
                                'type'        => [
593✔
235
                                        'list_of' => 'UsersConnectionOrderbyInput',
593✔
236
                                ],
593✔
237
                                'description' => static function () {
593✔
238
                                        return __( 'What parameter to use to order the objects by.', 'wp-graphql' );
15✔
239
                                },
593✔
240
                        ],
593✔
241
                ];
593✔
242
        }
243
}
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