*/ public static function search( string $term, callable $url, ?callable $allow = null, int $limit = 10 ): array { $term = trim( $term ); if ( mb_strlen( $term ) < 2 ) { return array(); } $args = array( 'number' => $limit * 3, 'orderby' => 'display_name', 'fields' => 'all', ); // Username, email or display name contains the whole term. $by_account = new \WP_User_Query( $args + array( 'search' => '*' . $term . '*', 'search_columns' => array( 'user_login', 'user_email', 'display_name' ), ) ); // First or last name: every word must be in one of them, so // "alice", "smith", "alice smith" and "smith alice" all match. $words = array_slice( array_filter( (array) preg_split( '/\s+/', $term ) ), 0, 4 ); $names = array( 'relation' => 'AND' ); foreach ( $words as $word ) { $names[] = array( 'relation' => 'OR', array( 'key' => 'first_name', 'value' => $word, 'compare' => 'LIKE', ), array( 'key' => 'last_name', 'value' => $word, 'compare' => 'LIKE', ), ); } $by_name = new \WP_User_Query( $args + array( 'meta_query' => $names ) ); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- admin-only search, limited and typed by an admin. $users = array(); foreach ( array_merge( (array) $by_account->get_results(), (array) $by_name->get_results() ) as $user ) { $users[ $user->ID ] = $user; } uasort( $users, static fn( \WP_User $x, \WP_User $y ): int => strcasecmp( $x->display_name, $y->display_name ) ); $found = array(); foreach ( $users as $user ) { if ( count( $found ) >= $limit || ! current_user_can( 'edit_user', $user->ID ) || ( $allow && ! $allow( $user ) ) ) { continue; } $found[] = array( 'id' => (int) $user->ID, 'name' => (string) $user->display_name, 'login' => (string) $user->user_login, 'email' => (string) $user->user_email, 'url' => (string) $url( (int) $user->ID ), 'avatar' => (string) get_avatar_url( $user->ID, array( 'size' => 64 ) ), ); } return $found; } /** * Answer the type-ahead's AJAX request (call from the page's own * `wp_ajax_{action}` handler). Needs `$cap` and the context's nonce. * * @param array $context See the class description. * @param string $cap Capability required. * @return void */ public static function respond( array $context, string $cap ): void { if ( ! current_user_can( $cap ) ) { wp_send_json_error( array( 'message' => __( 'You are not allowed to do this.', 'all-in-one-utilities' ) ), 403 ); } check_ajax_referer( (string) $context['nonce'] ); $term = isset( $_POST['term'] ) ? sanitize_text_field( wp_unslash( $_POST['term'] ) ) : ''; wp_send_json_success( array( 'users' => self::search( $term, $context['url'], $context['allow'] ?? null ) ) ); } /** * The search box (and, after a no-JavaScript search, its results). * * @param array $context See the class description. * @return void */ public static function render( array $context ): void { $term = isset( $_GET['s'] ) ? sanitize_text_field( wp_unslash( $_GET['s'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only search. $results = '' !== $term ? self::search( $term, $context['url'], $context['allow'] ?? null ) : array(); ?>