Bp_Search_Albums::sql( string $search_term, false $only_totalrow_count = false )

Prepare SQL query for albums search.

Description

Parameters

$search_term

(Required) Search terms.

$only_totalrow_count

(Optional) Total row count.

Default value: false

Return

(mixed|void)

Source

File: bp-search/classes/class-bp-search-albums.php

64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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 ma.id ) ';
    } else {
        $sql .= $wpdb->prepare( " DISTINCT ma.id, 'albums' as type, ma.title LIKE %s AS relevance, ma.date_created as entry_date  ", '%' . $wpdb->esc_like( $search_term ) . '%' );
    }
 
    $sql .= " FROM {$bp->media->table_name_albums} ma WHERE";
 
    $privacy = array( 'public' );
    if ( is_user_logged_in() ) {
        $privacy[] = 'loggedin';
    }
 
    $sql .= $wpdb->prepare(
        " (
            (
                ma.title LIKE %s
            )
            AND
            (
                    ( ma.privacy IN ( '" . implode( "','", $privacy ) . "' ) ) " . // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
                    ( isset( $user_groups ) && ! empty( $user_groups ) ? " OR ( ma.group_id IN ( '" . implode( "','", $user_groups ) . "' ) AND ma.privacy = 'grouponly' )" : '' ) . // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.QuotedDynamicPlaceholderGeneration
                    ( bp_is_active( 'friends' ) && ! empty( $friends ) ? " OR ( ma.user_id IN ( '" . implode( "','", $friends ) . "' ) AND ma.privacy = 'friends' )" : '' ) . // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.QuotedDynamicPlaceholderGeneration
                    ( is_user_logged_in() ? " OR ( ma.user_id = '" . bp_loggedin_user_id() . "' AND ma.privacy = 'onlyme' )" : '' ) . // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
            ')
        )',
        '%' . $wpdb->esc_like( $search_term ) . '%'
    );
 
    return apply_filters(
        'bp_search_albums_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.