Bp_Search_Media

BuddyPress Global Search – search media class

Description

Source

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

17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
class Bp_Search_Media extends Bp_Search_Type {
 
    /**
     * Search item type name
     *
     * @var string
     */
    private $type = 'photos';
 
    /**
     * Insures that only one instance of Class exists in memory at any
     * one time. Also prevents needing to define globals all over the place.
     *
     * @since BuddyBoss 1.5.0
     *
     * @return object Bp_Search_Media
     */
    public static function instance() {
        // Store the instance locally to avoid private static replication.
        static $instance = null;
 
        // Only run these methods if they haven't been run previously.
        if ( null === $instance ) {
            $instance = new Bp_Search_Media();
        }
 
        // Always return the instance.
        return $instance;
    }
 
    /**
     * A dummy constructor to prevent this class from being loaded more than once.
     *
     * @since BuddyBoss 1.4.0
     */
    private function __construct() {
        /* Do nothing here */
    }
 
    /**
     * Prepare SQL query for media search.
     *
     * @param string $search_term Search terms.
     * @param false  $only_totalrow_count Total row count.
     *
     * @return mixed|void
     */
    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 m.id ) ';
        } else {
            $sql .= $wpdb->prepare( " DISTINCT m.id, 'photos' as type, m.title LIKE %s AS relevance, m.date_created as entry_date  ", '%' . $wpdb->esc_like( $search_term ) . '%' );
        }
 
        $sql .= " FROM {$bp->media->table_name} m WHERE";
 
        $privacy = array( 'public' );
        if ( is_user_logged_in() ) {
            $privacy[] = 'loggedin';
        }
 
        $sql .= $wpdb->prepare(
            " (
                (
                    m.title LIKE %s
                )
                AND
                (
                        ( m.privacy IN ( '" . implode( "','", $privacy ) . "' ) ) " . // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
                        ( isset( $user_groups ) && ! empty( $user_groups ) ? " OR ( m.group_id IN ( '" . implode( "','", $user_groups ) . "' ) AND m.privacy = 'grouponly' )" : '' ) . // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.QuotedDynamicPlaceholderGeneration
                        ( bp_is_active( 'friends' ) && ! empty( $friends ) ? " OR ( m.user_id IN ( '" . implode( "','", $friends ) . "' ) AND m.privacy = 'friends' )" : '' ) . // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.QuotedDynamicPlaceholderGeneration
                        ( is_user_logged_in() ? " OR ( m.user_id = '" . bp_loggedin_user_id() . "' AND m.privacy = 'onlyme' )" : '' ) . // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
                ')
            )',
            '%' . $wpdb->esc_like( $search_term ) . '%'
        );
 
        return apply_filters(
            'bp_search_photos_sql',
            $sql,
            array(
                'search_term'         => $search_term,
                'only_totalrow_count' => $only_totalrow_count,
            )
        );
    }
 
    /**
     * Generare Html for media search
     *
     * @param string $template_type Template type.
     */
    protected function generate_html( $template_type = '' ) {
        $document_ids = array();
        foreach ( $this->search_results['items'] as $item_id => $item_html ) {
            $document_ids[] = $item_id;
        }
 
        // now we have all the posts.
        // lets do a media loop.
        $args = array(
            'include'      => implode( ',', $document_ids ),
            'per_page'     => count( $document_ids ),
            'search_terms' => false,
        );
 
        do_action( 'bp_before_search_photos_html' );
 
        if ( bp_has_media( $args ) ) {
 
            while ( bp_media() ) :
                bp_the_media();
 
                $result = array(
                    'id'    => bp_get_media_id(),
                    'type'  => $this->type,
                    'title' => bp_get_media_title(),
                    'html'  => bp_search_buffer_template_part( 'loop/photos', $template_type, false ),
                );
 
                $this->search_results['items'][ bp_get_media_id() ] = $result;
            endwhile;
        }
 
        do_action( 'bp_after_search_photos_html' );
    }
}

Methods

  • __construct — A dummy constructor to prevent this class from being loaded more than once.
  • generate_html — Generare Html for media search
  • instance — Insures that only one instance of Class exists in memory at any one time. Also prevents needing to define globals all over the place.
  • sql — Prepare SQL query for media search.

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.