@@ -63,6 +63,41 @@ public function filter__wp_is_large_user_count( $is_large_user_count, $count, $n
63
63
return false ;
64
64
}
65
65
66
+ /**
67
+ * Fires immediately before updating user metadata.
68
+ *
69
+ * We use this to watch for changes in the wp_capabilities metadata.
70
+ * (It's named wp_2_capabilities etc in multisite).
71
+ *
72
+ * @param int $meta_id ID of the metadata entry to update.
73
+ * @param int $user_id ID of the object metadata is for.
74
+ * @param string $meta_key Metadata key.
75
+ * @param mixed $meta_value Metadata value, not serialized.
76
+ *
77
+ * @return void
78
+ * @since 2.9.0
79
+ *
80
+ */
81
+ public function action__update_user_meta ( $ meta_id , $ user_id , $ meta_key , $ meta_value ) {
82
+ if ( ! $ this ->isCapabilitiesKey ( $ meta_key ) ) {
83
+ return ;
84
+ }
85
+ $ newRoles = $ this ->sanitizeCapabilitiesOption ( $ meta_value );
86
+ $ oldRoles = $ this ->getCurrentUserRoles ( $ user_id , $ meta_key );
87
+ $ this ->userRoleChange ( $ user_id , $ newRoles , $ oldRoles );
88
+ }
89
+
90
+ /** Returns the capabilities meta key, or false if it's not the capabilities key.
91
+ *
92
+ * @param $meta_key
93
+ *
94
+ * @return false|string
95
+ */
96
+ private function isCapabilitiesKey ( $ meta_key ) {
97
+ global $ wpdb ;
98
+ return $ meta_key === $ wpdb ->prefix . 'capabilities ' ;
99
+ }
100
+
66
101
/** Make sure wp_capabilities option values don't contain unexpected junk.
67
102
*
68
103
* @param array $option Option value, from dbms.
@@ -98,27 +133,29 @@ private function getCurrentUserRoles( $user_id, $meta_key = null ) {
98
133
}
99
134
100
135
/**
101
- * Fires immediately before updating user metadata.
102
- *
103
- * We use this to watch for changes in the wp_capabilities metadata.
104
- * (It's named wp_2_capabilities etc in multisite).
105
- *
106
- * @param int $meta_id ID of the metadata entry to update.
107
- * @param int $user_id ID of the object metadata is for.
108
- * @param string $meta_key Metadata key.
109
- * @param mixed $meta_value Metadata value, not serialized.
136
+ * @param int $user_id
137
+ * @param array $newRoles
138
+ * @param array $oldRoles
110
139
*
111
140
* @return void
112
- * @since 2.9.0
113
- *
114
141
*/
115
- public function action__update_user_meta ( $ meta_id , $ user_id , $ meta_key , $ meta_value ) {
116
- if ( ! $ this ->isCapabilitiesKey ( $ meta_key ) ) {
117
- return ;
142
+ private function userRoleChange ( $ user_id , $ newRoles , $ oldRoles ) {
143
+
144
+ if ( $ newRoles !== $ oldRoles ) {
145
+ $ toAdd = array_diff_key ( $ newRoles , $ oldRoles );
146
+ $ toRemove = array_diff_key ( $ oldRoles , $ newRoles );
147
+
148
+ foreach ( array_keys ( $ toRemove ) as $ role ) {
149
+ $ this ->indexer ->updateUserCounts ( $ role , - 1 );
150
+ $ this ->indexer ->updateEditors ( $ user_id , true );
151
+ $ this ->indexer ->removeIndexRole ( $ user_id , $ role );
152
+ }
153
+ foreach ( array_keys ( $ toAdd ) as $ role ) {
154
+ $ this ->indexer ->updateUserCounts ( $ role , + 1 );
155
+ $ this ->indexer ->updateEditors ( $ user_id , false );
156
+ $ this ->indexer ->addIndexRole ( $ user_id , $ role );
157
+ }
118
158
}
119
- $ newRoles = $ this ->sanitizeCapabilitiesOption ( $ meta_value );
120
- $ oldRoles = $ this ->getCurrentUserRoles ( $ user_id , $ meta_key );
121
- $ this ->userRoleChange ( $ user_id , $ newRoles , $ oldRoles );
122
159
}
123
160
124
161
/**
@@ -170,43 +207,6 @@ public function action__delete_user_meta( $meta_ids, $user_id, $meta_key, $meta_
170
207
$ this ->userRoleChange ( $ user_id , [], $ oldRoles );
171
208
}
172
209
173
- /** Returns the capabilities meta key, or false if it's not the capabilities key.
174
- *
175
- * @param $meta_key
176
- *
177
- * @return false|string
178
- */
179
- private function isCapabilitiesKey ( $ meta_key ) {
180
- global $ wpdb ;
181
- return $ meta_key === $ wpdb ->prefix . 'capabilities ' ;
182
- }
183
-
184
- /**
185
- * @param int $user_id
186
- * @param array $newRoles
187
- * @param array $oldRoles
188
- *
189
- * @return void
190
- */
191
- private function userRoleChange ( $ user_id , $ newRoles , $ oldRoles ) {
192
-
193
- if ( $ newRoles !== $ oldRoles ) {
194
- $ toAdd = array_diff_key ( $ newRoles , $ oldRoles );
195
- $ toRemove = array_diff_key ( $ oldRoles , $ newRoles );
196
-
197
- foreach ( array_keys ( $ toRemove ) as $ role ) {
198
- $ this ->indexer ->updateUserCounts ( $ role , - 1 );
199
- $ this ->indexer ->updateEditors ( $ user_id , true );
200
- $ this ->indexer ->removeIndexRole ( $ user_id , $ role );
201
- }
202
- foreach ( array_keys ( $ toAdd ) as $ role ) {
203
- $ this ->indexer ->updateUserCounts ( $ role , + 1 );
204
- $ this ->indexer ->updateEditors ( $ user_id , false );
205
- $ this ->indexer ->addIndexRole ( $ user_id , $ role );
206
- }
207
- }
208
- }
209
-
210
210
/**
211
211
* Filters the user count before queries are run.
212
212
*
@@ -273,7 +273,7 @@ public function filter__wp_dropdown_users_args( $query_args, $parsed_args ) {
273
273
$ threshold = get_option ( $ this ->options_name )['quickedit_threshold_limit ' ];
274
274
$ editors = $ this ->indexer ->getEditors ();
275
275
$ this ->doAutocomplete = true ;
276
- if ( count ( $ editors ) <= $ threshold ) {
276
+ if ( ! is_array ( $ editors ) || count ( $ editors ) <= $ threshold ) {
277
277
$ this ->doAutocomplete = false ;
278
278
$ query_args ['include ' ] = $ editors ;
279
279
} else if ( array_key_exists ( 'include_selected ' , $ parsed_args ) && $ parsed_args ['include_selected ' ]
@@ -368,46 +368,22 @@ private function filtered_query_args( $query_args, $parsed_args ) {
368
368
return $ query_args ;
369
369
}
370
370
371
- /** Traverse a nested WP_Query_Meta query array flattening the
372
- * real terms in it.
371
+ /** Create a meta arg for looking for an exsisting role tag
373
372
*
374
- * @param array $query
373
+ * @param string $role
374
+ * @param string $compare 'NOT EXISTS' or 'EXISTS' (the default).
375
375
*
376
- * @return array Flattened array.
376
+ * @return array meta query arg array
377
377
*/
378
- private function parseQuery ( $ query ) {
379
- $ result = [];
380
-
381
- foreach ( $ query as $ k => $ q ) {
382
- if ( $ k !== 'relation ' ) {
383
- if ( array_key_exists ( 'key ' , $ q ) && array_key_exists ( 'compare ' , $ q ) ) {
384
- $ result [] = $ q ;
385
- } else {
386
- $ result = array_merge ( $ result , $ this ->parseQuery ( $ q ) );
387
- }
388
- }
389
- }
390
- return $ result ;
391
- }
378
+ private
379
+ function makeRoleQueryArgs (
380
+ $ role , $ compare = 'EXISTS '
381
+ ) {
382
+ global $ wpdb ;
383
+ $ roleMetaPrefix = $ wpdb ->prefix . INDEX_WP_USERS_FOR_SPEED_KEY_PREFIX . 'r: ' ;
384
+ $ roleMetaKey = $ roleMetaPrefix . $ role ;
392
385
393
- /** Flatten out the meta query terms we get from a multisite setup,
394
- * allowing their interpretation as if from a single site.
395
- * This is kludgey because it removes AND exists(wp_capabilities key).
396
- *
397
- * @param array $query
398
- * @param string $keyToRemove Something like wp_2_capapabilities or wp_capabilities
399
- *
400
- * @return string[]
401
- */
402
- private function flattenQuery ( $ query , $ keyToRemove ) {
403
- $ r = $ this ->parseQuery ( $ query );
404
- $ q = [ 'relation ' => 'OR ' ];
405
- foreach ( $ r as $ item ) {
406
- if ( $ item ['key ' ] !== $ keyToRemove ) {
407
- $ q [] = $ item ;
408
- }
409
- }
410
- return $ q ;
386
+ return [ 'key ' => $ roleMetaKey , 'compare ' => $ compare ];
411
387
}
412
388
413
389
/**
@@ -472,6 +448,48 @@ public function filter_meta_sql(
472
448
return $ sql ;
473
449
}
474
450
451
+ /** Flatten out the meta query terms we get from a multisite setup,
452
+ * allowing their interpretation as if from a single site.
453
+ * This is kludgey because it removes AND exists(wp_capabilities key).
454
+ *
455
+ * @param array $query
456
+ * @param string $keyToRemove Something like wp_2_capapabilities or wp_capabilities
457
+ *
458
+ * @return string[]
459
+ */
460
+ private function flattenQuery ( $ query , $ keyToRemove ) {
461
+ $ r = $ this ->parseQuery ( $ query );
462
+ $ q = [ 'relation ' => 'OR ' ];
463
+ foreach ( $ r as $ item ) {
464
+ if ( $ item ['key ' ] !== $ keyToRemove ) {
465
+ $ q [] = $ item ;
466
+ }
467
+ }
468
+ return $ q ;
469
+ }
470
+
471
+ /** Traverse a nested WP_Query_Meta query array flattening the
472
+ * real terms in it.
473
+ *
474
+ * @param array $query
475
+ *
476
+ * @return array Flattened array.
477
+ */
478
+ private function parseQuery ( $ query ) {
479
+ $ result = [];
480
+
481
+ foreach ( $ query as $ k => $ q ) {
482
+ if ( $ k !== 'relation ' ) {
483
+ if ( array_key_exists ( 'key ' , $ q ) && array_key_exists ( 'compare ' , $ q ) ) {
484
+ $ result [] = $ q ;
485
+ } else {
486
+ $ result = array_merge ( $ result , $ this ->parseQuery ( $ q ) );
487
+ }
488
+ }
489
+ }
490
+ return $ result ;
491
+ }
492
+
475
493
/**
476
494
* Filters the users array before the query takes place.
477
495
*
@@ -728,24 +746,6 @@ function mungRoleFilters(
728
746
$ qv ['role__not_in ' ] = [];
729
747
}
730
748
731
- /** Create a meta arg for looking for an exsisting role tag
732
- *
733
- * @param string $role
734
- * @param string $compare 'NOT EXISTS' or 'EXISTS' (the default).
735
- *
736
- * @return array meta query arg array
737
- */
738
- private
739
- function makeRoleQueryArgs (
740
- $ role , $ compare = 'EXISTS '
741
- ) {
742
- global $ wpdb ;
743
- $ roleMetaPrefix = $ wpdb ->prefix . INDEX_WP_USERS_FOR_SPEED_KEY_PREFIX . 'r: ' ;
744
- $ roleMetaKey = $ roleMetaPrefix . $ role ;
745
-
746
- return [ 'key ' => $ roleMetaKey , 'compare ' => $ compare ];
747
- }
748
-
749
749
/**
750
750
* Filters WP_User_Query arguments when querying users via the REST API. (Gutenberg author-selection box)
751
751
*
0 commit comments