Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try sorting users list by email or username #41623

Draft
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: other

Sort users by email or username when some of those users are viewers.
Original file line number Diff line number Diff line change
Expand Up @@ -229,14 +229,53 @@

$combined_users = array_merge( $users, $viewers );

// When viewers are included, we ignore the order & orderby parameters.
// When viewers are included, we try to respect orderby if it's login or email.
if ( $include_viewers ) {
usort(
$combined_users,
function ( $a, $b ) {
return strcmp( strtolower( $a->name ), strtolower( $b->name ) );
// First we need to be sure to obtain the viewer emails and logins.
foreach ( $combined_users as &$user ) {
$user = new WP_User( $user->ID );
}

if ( 'email' === $args['order_by'] ) {
if ( 'asc' === strtolower( $args['order'] ) ) {
usort(
$combined_users,
function ( $a, $b ) {
return strcmp( strtolower( $a->user_email ), strtolower( $b->user_email ) );
}
);
} else {
usort(
$combined_users,
function ( $a, $b ) {

Check failure on line 250 in projects/plugins/jetpack/json-endpoints/class.wpcom-json-api-list-users-endpoint.php

View workflow job for this annotation

GitHub Actions / Static analysis

TypeError PhanTypeMismatchArgumentInternal Argument 2 ($callback) is (function) of type Closure(mixed,mixed):\GMP but \usort() takes callable(mixed,mixed):int FAQ on Phan issues: pdWQjU-Jb-p2
return gmp_neg( strcmp( strtolower( $a->user_email ), strtolower( $b->user_email ) ) );
}
);
}
} elseif ( 'login' === $args['order_by'] ) {
if ( 'asc' === strtolower( $args['order'] ) ) {
usort(
$combined_users,
function ( $a, $b ) {
return strcmp( strtolower( $a->user_login ), strtolower( $b->user_login ) );
}
);
} else {
usort(
$combined_users,
function ( $a, $b ) {

Check failure on line 266 in projects/plugins/jetpack/json-endpoints/class.wpcom-json-api-list-users-endpoint.php

View workflow job for this annotation

GitHub Actions / Static analysis

TypeError PhanTypeMismatchArgumentInternal Argument 2 ($callback) is (function) of type Closure(mixed,mixed):\GMP but \usort() takes callable(mixed,mixed):int FAQ on Phan issues: pdWQjU-Jb-p2
return gmp_neg( strcmp( strtolower( $a->user_login ), strtolower( $b->user_login ) ) );
}
);
}
);
} else {
usort(
$combined_users,
function ( $a, $b ) {
return strcmp( strtolower( $a->display_name ), strtolower( $b->display_name ) );
}
);
}
}

$return[ $key ] = $combined_users;
Expand Down
Loading