Bp_Search_Documents::sql( $search_term, $only_totalrow_count = false )
Description
Source
File: bp-search/classes/class-bp-search-documents.php
public function sql( $search_term, $only_totalrow_count = false ) { global $wpdb, $bp; $query_placeholder = array(); $user_groups = array(); if ( bp_is_active( 'groups' ) ) { // Fetch public groups. $public_groups = groups_get_groups( array( 'fields' => 'ids', 'status' => 'public', 'per_page' => - 1, ) ); if ( ! empty( $public_groups['groups'] ) ) { $public_groups = $public_groups['groups']; } else { $public_groups = array(); } $user_groups = array(); if ( is_user_logged_in() ) { $groups = groups_get_user_groups( bp_loggedin_user_id() ); if ( ! empty( $groups['groups'] ) ) { $user_groups = $groups['groups']; } else { $user_groups = array(); } } $user_groups = array_unique( array_merge( $user_groups, $public_groups ) ); } $friends = array(); if ( bp_is_active( 'friends' ) && is_user_logged_in() ) { // Determine friends of user. $friends = friends_get_friend_user_ids( bp_loggedin_user_id() ); if ( empty( $friends ) ) { $friends = array( 0 ); } array_push( $friends, bp_loggedin_user_id() ); } $sql = ' SELECT '; if ( $only_totalrow_count ) { $sql .= ' COUNT( DISTINCT d.id ) '; } else { $sql .= $wpdb->prepare(" DISTINCT d.id, 'documents' as type, d.title LIKE %s AS relevance, d.date_created as entry_date ", '%' . $wpdb->esc_like( $search_term ) . '%' ); } $sql .= " FROM {$bp->document->table_name} d, {$bp->document->table_name_meta} dm WHERE ( d.id = dm.document_id )"; $privacy = array( 'public' ); if( is_user_logged_in() ) { $privacy[] = 'loggedin'; } $sql .= $wpdb->prepare( " AND ( ( d.title LIKE %s OR dm.meta_key = 'extension' AND dm.meta_value LIKE %s OR dm.meta_key = 'file_name' AND dm.meta_value LIKE %s ) AND ( ( d.privacy IN ( '" . implode( "','", $privacy ) . "' ) ) " . ( isset( $user_groups ) && ! empty( $user_groups ) ? " OR ( d.group_id IN ( '" . implode( "','", $user_groups ) . "' ) AND d.privacy = 'grouponly' )" : '' ) . ( bp_is_active( 'friends' ) && ! empty( $friends ) ? " OR ( d.user_id IN ( '" . implode( "','", $friends ) . "' ) AND d.privacy = 'friends' )" : '' ) . ( is_user_logged_in() ? " OR ( d.user_id = '" . bp_loggedin_user_id() . "' AND d.privacy = 'onlyme' )" : '' ) . ") )", '%' . $wpdb->esc_like( $search_term ) . '%', '%' . $wpdb->esc_like( $search_term ) . '%', '%' . $wpdb->esc_like( $search_term ) . '%' ); return apply_filters( 'bp_search_documents_sql', $sql, array( 'search_term' => $search_term, 'only_totalrow_count' => $only_totalrow_count, ) ); }
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.