bp_ps_xprofile_search( $f )

Return results from BuddyBoss Profile Search Extended.

Description

Source

File: bp-core/profile-search/bps-xprofile.php

function bp_ps_xprofile_search ($f)
{   
    global $bp, $wpdb;
    
	$value = $f->value;
	$filter = $f->format. '_'.  ($f->filter == ''? 'is': $f->filter);
    
	$sql = array ('select' => '', 'where' => array ());
	$sql['select'] = "SELECT user_id FROM {$bp->profile->table_name_data}";
	$sql['where']['field_id'] = $wpdb->prepare ("field_id = %d", $f->id);

	switch ($filter)
	{
	case 'integer_range':
		if (isset ($value['min']))  $sql['where']['min'] = $wpdb->prepare ("value >= %d", $value['min']);
		if (isset ($value['max']))  $sql['where']['max'] = $wpdb->prepare ("value <= %d", $value['max']);
		break;

	case 'decimal_range':
		if (isset ($value['min']))  $sql['where']['min'] = $wpdb->prepare ("value >= %f", $value['min']);
		if (isset ($value['max']))  $sql['where']['max'] = $wpdb->prepare ("value <= %f", $value['max']);
		break;

	case 'date_date_range':
        $range_types = array( 'min', 'max' );
        foreach ( $range_types as $range_type ) {
            if ( isset( $value[ $range_type ]['year'] ) && !empty( $value[ $range_type ]['year'] ) ) {
                $year = $f->value[ $range_type ]['year'];
                $month  = !empty( $f->value[ $range_type ]['month'] ) ? $f->value[ $range_type ]['month'] : '00';
                $day    = !empty( $f->value[ $range_type ]['day'] ) ? $f->value[ $range_type ]['day'] : '00';
                $date = $year . '-' . $month . '-' . $day;
                
                $operator = 'min' == $range_type ? '>=' : '<=';
                
                $sql['where'][ $range_type ] = $wpdb->prepare ( "DATE(value) $operator %s", $date );
            }
        }
		break;

	case 'date_age_range':
		$day = date ('j');
		$month = date ('n');
		$year = date ('Y');

		if (isset ($value['max']))
		{
			$ymin = $year - $value['max'] - 1; 
			$sql['where']['age_min'] = $wpdb->prepare ("DATE(value) > %s", "$ymin-$month-$day");
		}
		if (isset ($value['min']))
		{
			$ymax = $year - $value['min'];
			$sql['where']['age_max'] = $wpdb->prepare ("DATE(value) <= %s", "$ymax-$month-$day");
		}
		break;

	case 'text_contains':
	case 'location_contains':
        if ( is_array( $value ) ) {
            $values = (array)$value;
            $parts = array ();
            foreach ( $values as $v ) {
                $v = str_replace ( '&', '&amp;', $v );
                $escaped = '%'. bp_ps_esc_like ( $v ). '%';
                $parts[] = $wpdb->prepare ( "value LIKE %s", $escaped);
            }
            $match = ' OR ';
            $sql['where'][$filter] = '('. implode ($match, $parts). ')';
        } else {
            $value = str_replace ('&', '&amp;', $value);
            $escaped = '%'. bp_ps_esc_like ($value). '%';
            $sql['where'][$filter] = $wpdb->prepare ("value LIKE %s", $escaped);
        }
		break;

	case 'text_like':
	case 'location_like':
		$value = str_replace ('&', '&amp;', $value);
		$value = str_replace ('\\\\%', '\\%', $value);
		$value = str_replace ('\\\\_', '\\_', $value);
		$sql['where'][$filter] = $wpdb->prepare ("value LIKE %s", $value);
		break;

	case 'text_is':
	case 'location_is':
		$value = str_replace ('&', '&amp;', $value);
		$sql['where'][$filter] = $wpdb->prepare ("value = %s", $value);
		break;

	case 'integer_is':
		$sql['where'][$filter] = $wpdb->prepare ("value = %d", $value);
		break;

	case 'decimal_is':
		$sql['where'][$filter] = $wpdb->prepare ("value = %f", $value);
		break;

	case 'date_is':
		$sql['where'][$filter] = $wpdb->prepare ("DATE(value) = %s", $value);
		break;

	case 'text_one_of':
		$values = (array)$value;
		$parts = array ();
		foreach ($values as $value)
		{
			$value = str_replace ('&', '&amp;', $value);
			$parts[] = $wpdb->prepare ("value = %s", $value);
		}
		$sql['where'][$filter] = '('. implode (' OR ', $parts). ')';
		break;

	case 'set_match_any':
	case 'set_match_all':
		$values = (array)$value;
		$parts = array ();
		foreach ($values as $value)
		{
			$value = str_replace ('&', '&amp;', $value);
			$escaped = '%:"'. bp_ps_esc_like ($value). '";%';
			$parts[] = $wpdb->prepare ("value LIKE %s", $escaped);
		}
		$match = ($filter == 'set_match_any')? ' OR ': ' AND ';
		$sql['where'][$filter] = '('. implode ($match, $parts). ')';
		break;

	default:
		return array ();
	}

	$sql = apply_filters ('bp_ps_field_sql', $sql, $f);
	$query = $sql['select']. ' WHERE '. implode (' AND ', $sql['where']);
    
	$results = $wpdb->get_col ($query);
	return $results;
}

Changelog

Changelog
Version Description
BuddyBoss 1.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.