Skip to content

Commit e03d37b

Browse files
committed
1.1.4 ... fix problems with network user count.
1 parent c0580f9 commit e03d37b

7 files changed

+141
-134
lines changed

admin/user-handler.php

+109-109
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,41 @@ public function filter__wp_is_large_user_count( $is_large_user_count, $count, $n
6363
return false;
6464
}
6565

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+
66101
/** Make sure wp_capabilities option values don't contain unexpected junk.
67102
*
68103
* @param array $option Option value, from dbms.
@@ -98,27 +133,29 @@ private function getCurrentUserRoles( $user_id, $meta_key = null ) {
98133
}
99134

100135
/**
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
110139
*
111140
* @return void
112-
* @since 2.9.0
113-
*
114141
*/
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+
}
118158
}
119-
$newRoles = $this->sanitizeCapabilitiesOption( $meta_value );
120-
$oldRoles = $this->getCurrentUserRoles( $user_id, $meta_key );
121-
$this->userRoleChange( $user_id, $newRoles, $oldRoles );
122159
}
123160

124161
/**
@@ -170,43 +207,6 @@ public function action__delete_user_meta( $meta_ids, $user_id, $meta_key, $meta_
170207
$this->userRoleChange( $user_id, [], $oldRoles );
171208
}
172209

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-
210210
/**
211211
* Filters the user count before queries are run.
212212
*
@@ -273,7 +273,7 @@ public function filter__wp_dropdown_users_args( $query_args, $parsed_args ) {
273273
$threshold = get_option( $this->options_name )['quickedit_threshold_limit'];
274274
$editors = $this->indexer->getEditors();
275275
$this->doAutocomplete = true;
276-
if ( count( $editors ) <= $threshold ) {
276+
if ( ! is_array( $editors ) || count( $editors ) <= $threshold ) {
277277
$this->doAutocomplete = false;
278278
$query_args['include'] = $editors;
279279
} 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 ) {
368368
return $query_args;
369369
}
370370

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
373372
*
374-
* @param array $query
373+
* @param string $role
374+
* @param string $compare 'NOT EXISTS' or 'EXISTS' (the default).
375375
*
376-
* @return array Flattened array.
376+
* @return array meta query arg array
377377
*/
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;
392385

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 ];
411387
}
412388

413389
/**
@@ -472,6 +448,48 @@ public function filter_meta_sql(
472448
return $sql;
473449
}
474450

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+
475493
/**
476494
* Filters the users array before the query takes place.
477495
*
@@ -728,24 +746,6 @@ function mungRoleFilters(
728746
$qv['role__not_in'] = [];
729747
}
730748

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-
749749
/**
750750
* Filters WP_User_Query arguments when querying users via the REST API. (Gutenberg author-selection box)
751751
*

includes/indexer.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -325,9 +325,12 @@ private function fakeUserCounts() {
325325
}
326326

327327
public static function getNetworkUserCount() {
328-
global $wpdb;
329328

330-
return get_user_count();
329+
if (function_exists('get_user_count')) {
330+
return get_user_count();
331+
}
332+
global $wpdb;
333+
return $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->users;");
331334
}
332335

333336
public function removeIndexRole( $user_id, $role ) {

includes/tasks/populate-meta-index-roles.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ public function doChunk() {
6262

6363
/* update table stats at the end of the indexing */
6464
if ( $done ) {
65-
$query = $wpdb->prepare( "ANALYZE TABLE $wpdb->usermeta;" );
66-
$this->doQuery( $query );
65+
$this->doQuery( "ANALYZE TABLE $wpdb->usermeta;" );
6766
}
6867

6968
$this->endChunk();

includes/tasks/task.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function doTaskStep() {
7575
}
7676
}
7777

78-
public abstract function doChunk();
78+
abstract public function doChunk();
7979

8080
public function schedule( $time = 0, $frequency = false ) {
8181
$cronArg = $this->persist();
@@ -107,7 +107,8 @@ public static function toSnake(
107107
$symbol, $delim = '-'
108108
) {
109109
$res = [];
110-
for ( $i = 0; $i < strlen( $symbol ); $i ++ ) {
110+
$len = strlen( $symbol );
111+
for ( $i = 0; $i < $len; $i ++ ) {
111112
$c = $symbol[ $i ];
112113
if ( ctype_upper( $c ) ) {
113114
$res[] = $delim;
@@ -127,11 +128,10 @@ protected function generateCallTrace() {
127128
$trace = array_reverse( $trace );
128129
array_shift( $trace ); // remove {main}
129130
array_pop( $trace ); // remove call to this method
130-
$length = count( $trace );
131131
$result = [];
132132

133-
for ( $i = 0; $i < $length; $i ++ ) {
134-
$result[] = ( $i + 1 ) . ')' . substr( $trace[ $i ], strpos( $trace[ $i ], ' ' ) ); // replace '#someNum' with '$i)', set the right ordering
133+
foreach ( $trace as $i => $iValue ) {
134+
$result[] = ( $i + 1 ) . ')' . substr( $iValue, strpos( $iValue, ' ' ) ); // replace '#someNum' with '$i)', set the right ordering
135135
}
136136

137137
return "\t" . implode( "\n\t", $result );

index-wp-users-for-speed.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
* @license GPL-2.0-or-later
88
* @wordpress-plugin0
99
* Plugin Name: Index WP Users For Speed
10-
* Version: 1.1.3
11-
* Stable tag: 1.1.3
10+
* Version: 1.1.4
11+
* Stable tag: 1.1.4
1212
* Plugin URI: https://plumislandmedia.org/index-wp-users-for-speed/
1313
* Description: Speed up your WordPress site with thousands of users.
1414
* Requires at least: 5.2
15-
* Tested up to: 6.1
15+
* Tested up to: 6.1.1
1616
* Requires PHP: 5.6
1717
* Author: Oliver Jones
1818
* Author URI: https://github.com/OllieJones
@@ -34,7 +34,7 @@
3434

3535
const INDEX_WP_USERS_FOR_SPEED_NAME = 'index-wp-users-for-speed';
3636
define( 'INDEX_WP_USERS_FOR_SPEED_FILENAME', plugin_basename( __FILE__ ) );
37-
const INDEX_WP_USERS_FOR_SPEED_VERSION = '1.1.3';
37+
const INDEX_WP_USERS_FOR_SPEED_VERSION = '1.1.4';
3838
const INDEX_WP_USERS_FOR_SPEED_PREFIX = 'index-wp-users-for-speed-';
3939
const INDEX_WP_USERS_FOR_SPEED_HOOKNAME = 'index_wp_users_for_speed_task';
4040
const INDEX_WP_USERS_FOR_SPEED_KEY_PREFIX = 'iufs';

0 commit comments

Comments
 (0)