/**
  * Get a SQL clause representing member_type include/exclusion.
  *
  * @since 2.4.0
  *
  * @param string|array $member_types Array or comma-separated list of member types.
  * @param string       $operator     'IN' or 'NOT IN'.
  * @return string
  */
 protected function get_sql_clause_for_member_types($member_types, $operator)
 {
     global $wpdb;
     // Sanitize.
     if ('NOT IN' !== $operator) {
         $operator = 'IN';
     }
     // Parse and sanitize types.
     if (!is_array($member_types)) {
         $member_types = preg_split('/[,\\s+]/', $member_types);
     }
     $types = array();
     foreach ($member_types as $mt) {
         if (bp_get_member_type_object($mt)) {
             $types[] = $mt;
         }
     }
     $tax_query = new WP_Tax_Query(array(array('taxonomy' => bp_get_member_type_tax_name(), 'field' => 'name', 'operator' => $operator, 'terms' => $types)));
     // Switch to the root blog, where member type taxonomies live.
     $site_id = bp_get_taxonomy_term_site_id(bp_get_member_type_tax_name());
     $switched = false;
     if ($site_id !== get_current_blog_id()) {
         switch_to_blog($site_id);
         $switched = true;
     }
     $sql_clauses = $tax_query->get_sql('u', $this->uid_name);
     $clause = '';
     // The no_results clauses are the same between IN and NOT IN.
     if (false !== strpos($sql_clauses['where'], '0 = 1')) {
         $clause = $this->no_results['where'];
         // The tax_query clause generated for NOT IN can be used almost as-is. We just trim the leading 'AND'.
     } elseif ('NOT IN' === $operator) {
         $clause = preg_replace('/^\\s*AND\\s*/', '', $sql_clauses['where']);
         // IN clauses must be converted to a subquery.
     } elseif (preg_match('/' . $wpdb->term_relationships . '\\.term_taxonomy_id IN \\([0-9, ]+\\)/', $sql_clauses['where'], $matches)) {
         $clause = "u.{$this->uid_name} IN ( SELECT object_id FROM {$wpdb->term_relationships} WHERE {$matches[0]} )";
     }
     if ($switched) {
         restore_current_blog();
     }
     return $clause;
 }
/**
 * Get term data for terms in BuddyPress taxonomies.
 *
 * Note that term data is from the `bp_get_taxonomy_term_site_id()`, which on some
 * multisite configurations may not be the same as the current site.
 *
 * @since 2.7.0
 *
 * @see get_term_by() for a full description of function and parameters.
 *
 * @param string     $field    Either 'slug', 'name', 'id' (term_id), or 'term_taxonomy_id'
 * @param string|int $value    Search for this term value
 * @param string     $taxonomy Taxonomy name. Optional, if `$field` is 'term_taxonomy_id'.
 * @param string     $output   Constant OBJECT, ARRAY_A, or ARRAY_N
 * @param string     $filter   Optional, default is raw or no WordPress defined filter will applied.
 *
 * @return WP_Term|bool WP_Term instance on success. Will return false if `$taxonomy` does not exist
 *                      or `$term` was not found.
 */
function bp_get_term_by($field, $value, $taxonomy = '', $output = OBJECT, $filter = 'raw')
{
    $site_id = bp_get_taxonomy_term_site_id($taxonomy);
    $switched = false;
    if ($site_id !== get_current_blog_id()) {
        switch_to_blog($site_id);
        bp_register_taxonomies();
        $switched = true;
    }
    $term = get_term_by($field, $value, $taxonomy, $output, $filter);
    if ($switched) {
        restore_current_blog();
    }
    return $term;
}
 /**
  * Get SQL clause for group type(s).
  *
  * @since 2.6.0
  *
  * @param  string|array $group_types Group type(s).
  * @param  string       $operator    'IN' or 'NOT IN'.
  * @return string       $clause      SQL clause.
  */
 protected static function get_sql_clause_for_group_types($group_types, $operator)
 {
     global $wpdb;
     // Sanitize operator.
     if ('NOT IN' !== $operator) {
         $operator = 'IN';
     }
     // Parse and sanitize types.
     if (!is_array($group_types)) {
         $group_types = preg_split('/[,\\s+]/', $group_types);
     }
     $types = array();
     foreach ($group_types as $gt) {
         if (bp_groups_get_group_type_object($gt)) {
             $types[] = $gt;
         }
     }
     $tax_query = new WP_Tax_Query(array(array('taxonomy' => 'bp_group_type', 'field' => 'name', 'operator' => $operator, 'terms' => $types)));
     $site_id = bp_get_taxonomy_term_site_id('bp_group_type');
     $switched = false;
     if ($site_id !== get_current_blog_id()) {
         switch_to_blog($site_id);
         $switched = true;
     }
     $sql_clauses = $tax_query->get_sql('g', 'id');
     $clause = '';
     // The no_results clauses are the same between IN and NOT IN.
     if (false !== strpos($sql_clauses['where'], '0 = 1')) {
         $clause = self::strip_leading_and($sql_clauses['where']);
         // The tax_query clause generated for NOT IN can be used almost as-is.
     } elseif ('NOT IN' === $operator) {
         $clause = self::strip_leading_and($sql_clauses['where']);
         // IN clauses must be converted to a subquery.
     } elseif (preg_match('/' . $wpdb->term_relationships . '\\.term_taxonomy_id IN \\([0-9, ]+\\)/', $sql_clauses['where'], $matches)) {
         $clause = " g.id IN ( SELECT object_id FROM {$wpdb->term_relationships} WHERE {$matches[0]} )";
     }
     if ($switched) {
         restore_current_blog();
     }
     return $clause;
 }