Making WordPress.org


Ignore:
Timestamp:
11/04/2022 08:48:57 AM (23 months ago)
Author:
coffee2code
Message:

Photo Directory, Search: Add function to determine if search query string matches a contributor's username.

See #6362.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/photo-directory/inc/search.php

    r11679 r12203  
    2929    public static function is_search( $query ) {
    3030        return ! is_admin() && $query->is_search() && $query->is_main_query();
     31    }
     32
     33    /**
     34     * Determines if the search query matches the username of a contributor.
     35     *
     36     * The search must be solely for the user's nicename and a found user must
     37     * also have a published photo.
     38     *
     39     * @param string $search The search string.
     40     * @return WP_User|false The user if the search matches a contributor, else false.
     41     */
     42    public static function query_matches_username( $search ) {
     43        $matches = false;
     44
     45        if ( preg_match( '/^[a-zA-Z0-9_]{3,}$/', $search ) ) {
     46            $user = get_user_by( 'slug', $search );
     47            if ( $user && User::count_published_photos( $user->ID ) ) {
     48                $matches = $user;
     49            }
     50        }
     51
     52        return $matches;
    3153    }
    3254
Note: See TracChangeset for help on using the changeset viewer.