__( 'Unauthorized.', 'aisp-user-exclusive-products' ) ), 403 ); } // Nonce verification. if ( ! isset( $_GET['_ajax_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_ajax_nonce'] ) ), 'aisp_search_users_nonce' ) ) { wp_send_json_error( array( 'message' => __( 'Invalid nonce.', 'aisp-user-exclusive-products' ) ), 403 ); } $term = isset( $_GET['term'] ) ? sanitize_text_field( wp_unslash( $_GET['term'] ) ) : ''; $page = isset( $_GET['page'] ) ? max( 1, (int) $_GET['page'] ) : 1; $args = array( 'number' => self::PER_PAGE, 'paged' => $page, 'orderby' => 'display_name', 'order' => 'ASC', 'fields' => array( 'ID', 'display_name', 'user_email' ), ); // Search by display_name, login, or email when a term is given. if ( ! empty( $term ) ) { $args['search'] = '*' . $term . '*'; $args['search_columns'] = array( 'display_name', 'user_login', 'user_email' ); } $user_query = new WP_User_Query( $args ); $users = $user_query->get_results(); $total = (int) $user_query->get_total(); $results = array(); foreach ( $users as $user ) { $results[] = array( 'id' => (int) $user->ID, 'text' => $user->display_name . ' (' . $user->user_email . ')', ); } $has_more = ( $page * self::PER_PAGE ) < $total; wp_send_json( array( 'results' => $results, 'pagination' => array( 'more' => $has_more ), ) ); } } endif;