bp_xprofile_bp_user_query_search( array $sql, BP_User_Query $query )

When search_terms are passed to BP_User_Query, search against xprofile fields.

Description

Parameters

$sql

(Required) Clauses in the user_id SQL query.

$query

(Required) User query object.

Return

(array)

Source

File: bp-xprofile/bp-xprofile-functions.php

function bp_xprofile_bp_user_query_search( $sql, BP_User_Query $query ) {
	global $wpdb;

	if ( empty( $query->query_vars['search_terms'] ) || empty( $sql['where']['search'] ) ) {
		return $sql;
	}

	$bp = buddypress();

	$search_terms_clean = bp_esc_like( wp_kses_normalize_entities( $query->query_vars['search_terms'] ) );

	if ( $query->query_vars['search_wildcard'] === 'left' ) {
		$search_terms_nospace = '%' . $search_terms_clean;
		$search_terms_space   = '%' . $search_terms_clean . ' %';
	} elseif ( $query->query_vars['search_wildcard'] === 'right' ) {
		$search_terms_nospace =        $search_terms_clean . '%';
		$search_terms_space   = '% ' . $search_terms_clean . '%';
	} else {
		$search_terms_nospace = '%' . $search_terms_clean . '%';
		$search_terms_space   = '%' . $search_terms_clean . '%';
	}

	// Combine the core search (against wp_users) into a single OR clause
	// with the xprofile_data search.
	$matched_user_ids = $wpdb->get_col( $wpdb->prepare(
		"SELECT user_id FROM {$bp->profile->table_name_data} WHERE value LIKE %s OR value LIKE %s",
		$search_terms_nospace,
		$search_terms_space
	) );

	// Checked profile fields based on privacy settings of particular user while searching
	if ( ! empty( $matched_user_ids ) ) {
		$matched_user_data = $wpdb->get_results( $wpdb->prepare(
			"SELECT * FROM {$bp->profile->table_name_data} WHERE value LIKE %s OR value LIKE %s",
			$search_terms_nospace,
			$search_terms_space
		) );

		foreach ( $matched_user_data as $key => $user ) {
			$field_visibility = xprofile_get_field_visibility_level( $user->field_id, $user->user_id );
			if ( 'adminsonly' === $field_visibility && !current_user_can('administrator') ) {
				if (($key = array_search($user->user_id, $matched_user_ids)) !== false) {
					unset($matched_user_ids[$key]);
				}
			}
			if ( 'friends' === $field_visibility && !current_user_can('administrator') && false === friends_check_friendship( intval($user->user_id), bp_loggedin_user_id() ) ) {
				if (($key = array_search($user->user_id, $matched_user_ids)) !== false) {
					unset($matched_user_ids[$key]);
				}
			}
		}
	}

	if ( ! empty( $matched_user_ids ) ) {
		$search_core     = $sql['where']['search'];
		$search_combined = " ( u.{$query->uid_name} IN (" . implode(',', $matched_user_ids) . ") OR {$search_core} )";
		$sql['where']['search'] = $search_combined;
	}

	return $sql;
}

Changelog

Changelog
Version Description
BuddyPress 2.0.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.