• 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

63.41
/src/Data/Cursor/UserCursor.php
1
<?php
2

3
namespace WPGraphQL\Data\Cursor;
4

5
use WP_User_Query;
6

7
/**
8
 * User Cursor
9
 *
10
 * This class generates the SQL AND operators for cursor based pagination for users
11
 *
12
 * @package WPGraphQL\Data\Cursor
13
 */
14
class UserCursor extends AbstractCursor {
15

16
        /**
17
         * @var ?\WP_User
18
         */
19
        public $cursor_node;
20

21
        /**
22
         * Counter for meta value joins
23
         *
24
         * @var int
25
         */
26
        public $meta_join_alias = 0;
27

28
        /**
29
         * {@inheritDoc}
30
         *
31
         * @param array<string,mixed>|\WP_User_Query $query_vars The query vars to use when building the SQL statement.
32
         */
33
        public function __construct( $query_vars, $cursor = 'after' ) {
71✔
34
                // Handle deprecated use of $query.
35
                if ( $query_vars instanceof WP_User_Query ) {
71✔
36
                        _doing_it_wrong( __FUNCTION__, 'The first argument should be an array of $query_vars, not the WP_Query object', '1.9.0' );
×
37
                        $query_vars = $query_vars->query_vars;
×
38
                }
39

40
                // Initialize the class properties.
41
                parent::__construct( $query_vars, $cursor );
71✔
42

43
                // Set ID key.
44
                $this->id_key = "{$this->wpdb->users}.ID";
71✔
45
        }
46

47
        /**
48
         * {@inheritDoc}
49
         *
50
         * Unlike most queries, users by default are in ascending order.
51
         */
52
        public function get_cursor_compare() {
71✔
53
                if ( 'before' === $this->cursor ) {
71✔
54
                        return '<';
7✔
55
                }
56
                return '>';
71✔
57
        }
58

59
        /**
60
         * {@inheritDoc}
61
         *
62
         * @return ?\WP_User
63
         */
64
        public function get_cursor_node() {
71✔
65
                // Bail if no offset.
66
                if ( ! $this->cursor_offset ) {
71✔
67
                        return null;
71✔
68
                }
69

70
                /**
71
                 * If pre-hooked, return filtered node.
72
                 *
73
                 * @param \WP_User|null                        $pre_user The pre-filtered user node.
74
                 * @param int                                  $offset   The cursor offset.
75
                 * @param \WPGraphQL\Data\Cursor\UserCursor    $node     The cursor instance.
76
                 *
77
                 * @return \WP_User|null
78
                 */
79
                $pre_user = apply_filters( 'graphql_pre_user_cursor_node', null, $this->cursor_offset, $this );
30✔
80
                if ( null !== $pre_user ) {
30✔
81
                        return $pre_user;
×
82
                }
83

84
                // Get cursor node.
85
                $user = get_user_by( 'id', $this->cursor_offset );
30✔
86

87
                return false !== $user ? $user : null;
30✔
88
        }
89

90
        /**
91
         * Deprecated in favor of get_cursor_node().
92
         *
93
         * @return ?\WP_User
94
         * @deprecated 1.9.0
95
         */
96
        public function get_cursor_user() {
×
97
                _deprecated_function( __METHOD__, '1.9.0', self::class . '::get_cursor_node()' );
×
98

99
                return $this->cursor_node;
×
100
        }
101

102
        /**
103
         * {@inheritDoc}
104
         */
105
        public function to_sql() {
30✔
106
                return ' AND ' . $this->builder->to_sql();
30✔
107
        }
108

109
        /**
110
         * {@inheritDoc}
111
         */
112
        public function get_where() {
30✔
113
                // If we have a bad cursor, just skip.
114
                if ( ! $this->is_valid_offset_and_node() ) {
30✔
115
                        return '';
×
116
                }
117

118
                $orderby = $this->get_query_var( 'orderby' );
30✔
119
                $order   = $this->get_query_var( 'order' );
30✔
120

121
                if ( ! empty( $orderby ) && is_array( $orderby ) ) {
30✔
122

123
                        /**
124
                         * Loop through all order keys if it is an array
125
                         */
126
                        foreach ( $orderby as $by => $order ) {
×
127
                                $this->compare_with( $by, $order );
×
128
                        }
129
                } elseif ( ! empty( $orderby ) && is_string( $orderby ) && 'login' !== $orderby ) {
30✔
130

131
                        /**
132
                         * If $orderby is just a string just compare with it directly as DESC
133
                         */
134
                        $this->compare_with( $orderby, $order );
9✔
135
                }
136

137
                /**
138
                 * If there's no orderby specified yet, compare with the following fields.
139
                 */
140
                if ( ! $this->builder->has_fields() ) {
30✔
141
                        $this->compare_with_cursor_fields(
21✔
142
                                [
21✔
143
                                        [
21✔
144
                                                'key'   => "{$this->wpdb->users}.user_login",
21✔
145
                                                'value' => $this->cursor_node ? $this->cursor_node->user_login : null,
21✔
146
                                                'type'  => 'CHAR',
21✔
147
                                        ],
21✔
148
                                ]
21✔
149
                        );
21✔
150
                }
151

152
                $this->compare_with_id_field();
30✔
153

154
                return $this->to_sql();
30✔
155
        }
156

157
        /**
158
         * Get AND operator for given order by key
159
         *
160
         * @param string $by    The order by key
161
         * @param string $order The order direction ASC or DESC
162
         */
163
        private function compare_with( $by, $order ): void {
9✔
164
                // Bail early, if "key" and "value" provided in query_vars.
165
                $key   = $this->get_query_var( "graphql_cursor_compare_by_{$by}_key" );
9✔
166
                $value = $this->get_query_var( "graphql_cursor_compare_by_{$by}_value" );
9✔
167
                if ( ! empty( $key ) && ! empty( $value ) ) {
9✔
168
                        $this->builder->add_field( $key, $value, null, $order );
×
169
                        return;
×
170
                }
171

172
                /**
173
                 * Find out whether this is a user field
174
                 */
175
                $orderby_user_fields = [
9✔
176
                        'user_email',
9✔
177
                        'user_login',
9✔
178
                        'user_nicename',
9✔
179
                        'user_registered',
9✔
180
                        'user_url',
9✔
181
                ];
9✔
182
                if ( in_array( $by, $orderby_user_fields, true ) ) {
9✔
183
                        $key   = "{$this->wpdb->users}.{$by}";
9✔
184
                        $value = $this->cursor_node->{$by} ?? null;
9✔
185
                }
186

187
                /**
188
                 * If key or value are null, check whether this is a meta key based ordering before bailing.
189
                 */
190
                if ( null === $key || null === $value ) {
9✔
191
                        $meta_key = $this->get_meta_key( $by );
×
192
                        if ( $meta_key ) {
×
193
                                $this->compare_with_meta_field( $meta_key, $order );
×
194
                        }
195
                        return;
×
196
                }
197

198
                // Add field to build.
199
                $this->builder->add_field( $key, $value, null, $order );
9✔
200
        }
201

202
        /**
203
         * Compare with meta key field
204
         *
205
         * @param string $meta_key user meta key
206
         * @param string $order    The comparison string
207
         */
208
        private function compare_with_meta_field( string $meta_key, string $order ): void {
×
209
                $meta_type  = $this->get_query_var( 'meta_type' );
×
210
                $meta_value = get_user_meta( $this->cursor_offset, $meta_key, true );
×
211

212
                $key = "{$this->wpdb->usermeta}.meta_value";
×
213

214
                /**
215
                 * WP uses mt1, mt2 etc. style aliases for additional meta value joins.
216
                 */
217
                if ( 0 !== $this->meta_join_alias ) {
×
218
                        $key = "mt{$this->meta_join_alias}.meta_value";
×
219
                }
220

221
                ++$this->meta_join_alias;
×
222

223
                $this->builder->add_field( $key, $meta_value, $meta_type, $order );
×
224
        }
225

226
        /**
227
         * Get the actual meta key if any
228
         *
229
         * @param string $by The order by key
230
         *
231
         * @return string|null
232
         */
233
        private function get_meta_key( $by ) {
×
234
                if ( 'meta_value' === $by ) {
×
235
                        return $this->get_query_var( 'meta_key' );
×
236
                }
237

238
                /**
239
                 * Check for the WP 4.2+ style meta clauses
240
                 * https://make.wordpress.org/core/2015/03/30/query-improvements-in-wp-4-2-orderby-and-meta_query/
241
                 */
242
                if ( ! isset( $this->query_vars['meta_query'][ $by ] ) ) {
×
243
                        return null;
×
244
                }
245

246
                $clause = $this->query_vars['meta_query'][ $by ];
×
247

248
                return empty( $clause['key'] ) ? null : $clause['key'];
×
249
        }
250
}
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