1 | <?php |
---|
2 | |
---|
3 | // Exit if accessed directly |
---|
4 | if ( !defined( 'ABSPATH' ) ) exit; |
---|
5 | |
---|
6 | class BP_Friends_Friendship { |
---|
7 | var $id; |
---|
8 | var $initiator_user_id; |
---|
9 | var $friend_user_id; |
---|
10 | var $is_confirmed; |
---|
11 | var $is_limited; |
---|
12 | var $date_created; |
---|
13 | |
---|
14 | var $is_request; |
---|
15 | var $populate_friend_details; |
---|
16 | |
---|
17 | var $friend; |
---|
18 | |
---|
19 | function __construct( $id = null, $is_request = false, $populate_friend_details = true ) { |
---|
20 | $this->is_request = $is_request; |
---|
21 | |
---|
22 | if ( !empty( $id ) ) { |
---|
23 | $this->id = $id; |
---|
24 | $this->populate_friend_details = $populate_friend_details; |
---|
25 | $this->populate( $this->id ); |
---|
26 | } |
---|
27 | } |
---|
28 | |
---|
29 | function populate() { |
---|
30 | global $wpdb, $bp; |
---|
31 | |
---|
32 | if ( $friendship = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->friends->table_name} WHERE id = %d", $this->id ) ) ) { |
---|
33 | $this->initiator_user_id = $friendship->initiator_user_id; |
---|
34 | $this->friend_user_id = $friendship->friend_user_id; |
---|
35 | $this->is_confirmed = $friendship->is_confirmed; |
---|
36 | $this->is_limited = $friendship->is_limited; |
---|
37 | $this->date_created = $friendship->date_created; |
---|
38 | } |
---|
39 | |
---|
40 | if ( !empty( $this->populate_friend_details ) ) { |
---|
41 | if ( $this->friend_user_id == bp_displayed_user_id() ) { |
---|
42 | $this->friend = new BP_Core_User( $this->initiator_user_id ); |
---|
43 | } else { |
---|
44 | $this->friend = new BP_Core_User( $this->friend_user_id ); |
---|
45 | } |
---|
46 | } |
---|
47 | } |
---|
48 | |
---|
49 | function save() { |
---|
50 | global $wpdb, $bp; |
---|
51 | |
---|
52 | $this->initiator_user_id = apply_filters( 'friends_friendship_initiator_user_id_before_save', $this->initiator_user_id, $this->id ); |
---|
53 | $this->friend_user_id = apply_filters( 'friends_friendship_friend_user_id_before_save', $this->friend_user_id, $this->id ); |
---|
54 | $this->is_confirmed = apply_filters( 'friends_friendship_is_confirmed_before_save', $this->is_confirmed, $this->id ); |
---|
55 | $this->is_limited = apply_filters( 'friends_friendship_is_limited_before_save', $this->is_limited, $this->id ); |
---|
56 | $this->date_created = apply_filters( 'friends_friendship_date_created_before_save', $this->date_created, $this->id ); |
---|
57 | |
---|
58 | do_action_ref_array( 'friends_friendship_before_save', array( &$this ) ); |
---|
59 | |
---|
60 | // Update |
---|
61 | if (!empty( $this->id ) ) { |
---|
62 | $result = $wpdb->query( $wpdb->prepare( "UPDATE {$bp->friends->table_name} SET initiator_user_id = %d, friend_user_id = %d, is_confirmed = %d, is_limited = %d, date_created = %s ) WHERE id = %d", $this->initiator_user_id, $this->friend_user_id, $this->is_confirmed, $this->is_limited, $this->date_created, $this->id ) ); |
---|
63 | |
---|
64 | // Save |
---|
65 | } else { |
---|
66 | $result = $wpdb->query( $wpdb->prepare( "INSERT INTO {$bp->friends->table_name} ( initiator_user_id, friend_user_id, is_confirmed, is_limited, date_created ) VALUES ( %d, %d, %d, %d, %s )", $this->initiator_user_id, $this->friend_user_id, $this->is_confirmed, $this->is_limited, $this->date_created ) ); |
---|
67 | $this->id = $wpdb->insert_id; |
---|
68 | } |
---|
69 | |
---|
70 | do_action( 'friends_friendship_after_save', array( &$this ) ); |
---|
71 | |
---|
72 | return $result; |
---|
73 | } |
---|
74 | |
---|
75 | function delete() { |
---|
76 | global $wpdb, $bp; |
---|
77 | |
---|
78 | return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->friends->table_name} WHERE id = %d", $this->id ) ); |
---|
79 | } |
---|
80 | |
---|
81 | /** Static Methods ********************************************************/ |
---|
82 | |
---|
83 | function get_friend_user_ids( $user_id, $friend_requests_only = false, $assoc_arr = false ) { |
---|
84 | global $wpdb, $bp; |
---|
85 | |
---|
86 | if ( !empty( $friend_requests_only ) ) { |
---|
87 | $oc_sql = 'AND is_confirmed = 0'; |
---|
88 | $friend_sql = $wpdb->prepare( " WHERE friend_user_id = %d", $user_id ); |
---|
89 | } else { |
---|
90 | $oc_sql = 'AND is_confirmed = 1'; |
---|
91 | $friend_sql = $wpdb->prepare( " WHERE (initiator_user_id = %d OR friend_user_id = %d)", $user_id, $user_id ); |
---|
92 | } |
---|
93 | |
---|
94 | $friends = $wpdb->get_results( "SELECT friend_user_id, initiator_user_id FROM {$bp->friends->table_name} {$friend_sql} {$oc_sql} ORDER BY date_created DESC" ); |
---|
95 | $fids = array(); |
---|
96 | |
---|
97 | for ( $i = 0, $count = count( $friends ); $i < $count; ++$i ) { |
---|
98 | if ( !empty( $assoc_arr ) ) { |
---|
99 | $fids[] = array( 'user_id' => ( $friends[$i]->friend_user_id == $user_id ) ? $friends[$i]->initiator_user_id : $friends[$i]->friend_user_id ); |
---|
100 | } else { |
---|
101 | $fids[] = ( $friends[$i]->friend_user_id == $user_id ) ? $friends[$i]->initiator_user_id : $friends[$i]->friend_user_id; |
---|
102 | } |
---|
103 | } |
---|
104 | |
---|
105 | return $fids; |
---|
106 | } |
---|
107 | |
---|
108 | function get_friendship_id( $user_id, $friend_id ) { |
---|
109 | global $wpdb, $bp; |
---|
110 | |
---|
111 | return $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->friends->table_name} WHERE ( initiator_user_id = %d AND friend_user_id = %d ) OR ( initiator_user_id = %d AND friend_user_id = %d ) AND is_confirmed = 1", $user_id, $friend_id, $friend_id, $user_id ) ); |
---|
112 | } |
---|
113 | |
---|
114 | function get_friendship_request_user_ids( $user_id ) { |
---|
115 | global $wpdb, $bp; |
---|
116 | |
---|
117 | return $wpdb->get_col( $wpdb->prepare( "SELECT initiator_user_id FROM {$bp->friends->table_name} WHERE friend_user_id = %d AND is_confirmed = 0", $user_id ) ); |
---|
118 | } |
---|
119 | |
---|
120 | function total_friend_count( $user_id = 0 ) { |
---|
121 | global $wpdb, $bp; |
---|
122 | |
---|
123 | if ( empty( $user_id ) ) |
---|
124 | $user_id = ( bp_displayed_user_id() ) ? bp_displayed_user_id() : bp_loggedin_user_id(); |
---|
125 | |
---|
126 | /* This is stored in 'total_friend_count' usermeta. |
---|
127 | This function will recalculate, update and return. */ |
---|
128 | |
---|
129 | $count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(id) FROM {$bp->friends->table_name} WHERE (initiator_user_id = %d OR friend_user_id = %d) AND is_confirmed = 1", $user_id, $user_id ) ); |
---|
130 | |
---|
131 | // Do not update meta if user has never had friends |
---|
132 | if ( empty( $count ) && !bp_get_user_meta( $user_id, 'total_friend_count', true ) ) |
---|
133 | return 0; |
---|
134 | |
---|
135 | bp_update_user_meta( $user_id, 'total_friend_count', (int) $count ); |
---|
136 | return (int) $count; |
---|
137 | } |
---|
138 | |
---|
139 | function search_friends( $filter, $user_id, $limit = null, $page = null ) { |
---|
140 | global $wpdb, $bp; |
---|
141 | |
---|
142 | // TODO: Optimize this function. |
---|
143 | |
---|
144 | if ( empty( $user_id ) ) |
---|
145 | $user_id = bp_loggedin_user_id(); |
---|
146 | |
---|
147 | $filter = esc_sql( like_escape( $filter ) ); |
---|
148 | |
---|
149 | if ( !empty( $limit ) && !empty( $page ) ) |
---|
150 | $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) ); |
---|
151 | |
---|
152 | if ( !$friend_ids = BP_Friends_Friendship::get_friend_user_ids( $user_id ) ) |
---|
153 | return false; |
---|
154 | |
---|
155 | // Get all the user ids for the current user's friends. |
---|
156 | $fids = implode( ',', wp_parse_id_list( $friend_ids ) ); |
---|
157 | |
---|
158 | if ( empty( $fids ) ) |
---|
159 | return false; |
---|
160 | |
---|
161 | // filter the user_ids based on the search criteria. |
---|
162 | if ( bp_is_active( 'xprofile' ) ) { |
---|
163 | $sql = "SELECT DISTINCT user_id FROM {$bp->profile->table_name_data} WHERE user_id IN ({$fids}) AND value LIKE '{$filter}%%' {$pag_sql}"; |
---|
164 | $total_sql = "SELECT COUNT(DISTINCT user_id) FROM {$bp->profile->table_name_data} WHERE user_id IN ({$fids}) AND value LIKE '{$filter}%%'"; |
---|
165 | } else { |
---|
166 | $sql = "SELECT DISTINCT user_id FROM {$wpdb->usermeta} WHERE user_id IN ({$fids}) AND meta_key = 'nickname' AND meta_value LIKE '{$filter}%%' {$pag_sql}"; |
---|
167 | $total_sql = "SELECT COUNT(DISTINCT user_id) FROM {$wpdb->usermeta} WHERE user_id IN ({$fids}) AND meta_key = 'nickname' AND meta_value LIKE '{$filter}%%'"; |
---|
168 | } |
---|
169 | |
---|
170 | $filtered_friend_ids = $wpdb->get_col( $sql ); |
---|
171 | $total_friend_ids = $wpdb->get_var( $total_sql ); |
---|
172 | |
---|
173 | if ( empty( $filtered_friend_ids ) ) |
---|
174 | return false; |
---|
175 | |
---|
176 | return array( 'friends' => $filtered_friend_ids, 'total' => (int) $total_friend_ids ); |
---|
177 | } |
---|
178 | |
---|
179 | function check_is_friend( $loggedin_userid, $possible_friend_userid ) { |
---|
180 | global $wpdb, $bp; |
---|
181 | |
---|
182 | if ( empty( $loggedin_userid ) || empty( $possible_friend_userid ) ) |
---|
183 | return false; |
---|
184 | |
---|
185 | $result = $wpdb->get_results( $wpdb->prepare( "SELECT id, is_confirmed, initiator_user_id FROM {$bp->friends->table_name} WHERE (initiator_user_id = %d AND friend_user_id = %d) OR (initiator_user_id = %d AND friend_user_id = %d)", $loggedin_userid, $possible_friend_userid, $possible_friend_userid, $loggedin_userid ) ); |
---|
186 | if ( !empty( $result ) ) { |
---|
187 | if ( 0 == (int) $result[0]->is_confirmed ) { |
---|
188 | if ( $loggedin_userid == $result[0]->initiator_user_id ) { |
---|
189 | return 'pending'; |
---|
190 | } else { |
---|
191 | return 'respond'; |
---|
192 | } |
---|
193 | } else { |
---|
194 | return 'is_friend'; |
---|
195 | } |
---|
196 | } else { |
---|
197 | return 'not_friends'; |
---|
198 | } |
---|
199 | } |
---|
200 | |
---|
201 | function get_bulk_last_active( $user_ids ) { |
---|
202 | global $wpdb; |
---|
203 | |
---|
204 | $user_ids = implode( ',', wp_parse_id_list( $user_ids ) ); |
---|
205 | |
---|
206 | return $wpdb->get_results( $wpdb->prepare( "SELECT meta_value as last_activity, user_id FROM {$wpdb->usermeta} WHERE meta_key = %s AND user_id IN ( {$user_ids} ) ORDER BY meta_value DESC", bp_get_user_meta_key( 'last_activity' ) ) ); |
---|
207 | } |
---|
208 | |
---|
209 | function accept($friendship_id) { |
---|
210 | global $wpdb, $bp; |
---|
211 | |
---|
212 | return $wpdb->query( $wpdb->prepare( "UPDATE {$bp->friends->table_name} SET is_confirmed = 1, date_created = %s WHERE id = %d AND friend_user_id = %d", bp_core_current_time(), $friendship_id, bp_loggedin_user_id() ) ); |
---|
213 | } |
---|
214 | |
---|
215 | function withdraw($friendship_id) { |
---|
216 | global $wpdb, $bp; |
---|
217 | |
---|
218 | return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->friends->table_name} WHERE id = %d AND initiator_user_id = %d", $friendship_id, bp_loggedin_user_id() ) ); |
---|
219 | } |
---|
220 | |
---|
221 | function reject($friendship_id) { |
---|
222 | global $wpdb, $bp; |
---|
223 | |
---|
224 | return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->friends->table_name} WHERE id = %d AND friend_user_id = %d", $friendship_id, bp_loggedin_user_id() ) ); |
---|
225 | } |
---|
226 | |
---|
227 | function search_users( $filter, $user_id, $limit = null, $page = null ) { |
---|
228 | global $wpdb, $bp; |
---|
229 | |
---|
230 | $filter = esc_sql( like_escape( $filter ) ); |
---|
231 | |
---|
232 | $usermeta_table = $wpdb->base_prefix . 'usermeta'; |
---|
233 | $users_table = $wpdb->base_prefix . 'users'; |
---|
234 | |
---|
235 | if ( !empty( $limit ) && !empty( $page ) ) |
---|
236 | $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * intval( $limit ) ), intval( $limit ) ); |
---|
237 | |
---|
238 | // filter the user_ids based on the search criteria. |
---|
239 | if ( bp_is_active( 'xprofile' ) ) { |
---|
240 | $sql = "SELECT DISTINCT d.user_id as id FROM {$bp->profile->table_name_data} d, {$users_table} u WHERE d.user_id = u.id AND d.value LIKE '{$filter}%%' ORDER BY d.value DESC {$pag_sql}"; |
---|
241 | } else { |
---|
242 | $sql = "SELECT DISTINCT user_id as id FROM {$usermeta_table} WHERE meta_value LIKE '{$filter}%%' ORDER BY d.value DESC {$pag_sql}"; |
---|
243 | } |
---|
244 | |
---|
245 | $filtered_fids = $wpdb->get_col($sql); |
---|
246 | |
---|
247 | if ( empty( $filtered_fids ) ) |
---|
248 | return false; |
---|
249 | |
---|
250 | return $filtered_fids; |
---|
251 | } |
---|
252 | |
---|
253 | function search_users_count( $filter ) { |
---|
254 | global $wpdb, $bp; |
---|
255 | |
---|
256 | $filter = esc_sql( like_escape( $filter ) ); |
---|
257 | |
---|
258 | $usermeta_table = $wpdb->prefix . 'usermeta'; |
---|
259 | $users_table = $wpdb->base_prefix . 'users'; |
---|
260 | |
---|
261 | // filter the user_ids based on the search criteria. |
---|
262 | if ( bp_is_active( 'xprofile' ) ) { |
---|
263 | $sql = "SELECT COUNT(DISTINCT d.user_id) FROM {$bp->profile->table_name_data} d, {$users_table} u WHERE d.user_id = u.id AND d.value LIKE '{$filter}%%'"; |
---|
264 | } else { |
---|
265 | $sql = "SELECT COUNT(DISTINCT user_id) FROM {$usermeta_table} WHERE meta_value LIKE '{$filter}%%'"; |
---|
266 | } |
---|
267 | |
---|
268 | $user_count = $wpdb->get_col($sql); |
---|
269 | |
---|
270 | if ( empty( $user_count ) ) |
---|
271 | return false; |
---|
272 | |
---|
273 | return $user_count[0]; |
---|
274 | } |
---|
275 | |
---|
276 | function sort_by_name( $user_ids ) { |
---|
277 | global $wpdb, $bp; |
---|
278 | |
---|
279 | if ( !bp_is_active( 'xprofile' ) ) |
---|
280 | return false; |
---|
281 | |
---|
282 | $user_ids = implode( ',', wp_parse_id_list( $user_ids ) ); |
---|
283 | |
---|
284 | return $wpdb->get_results( $wpdb->prepare( "SELECT user_id FROM {$bp->profile->table_name_data} pd, {$bp->profile->table_name_fields} pf WHERE pf.id = pd.field_id AND pf.name = %s AND pd.user_id IN ( {$user_ids} ) ORDER BY pd.value ASC", bp_xprofile_fullname_field_name() ) ); |
---|
285 | } |
---|
286 | |
---|
287 | function get_random_friends( $user_id, $total_friends = 5 ) { |
---|
288 | global $wpdb, $bp; |
---|
289 | |
---|
290 | $fids = array(); |
---|
291 | $sql = $wpdb->prepare( "SELECT friend_user_id, initiator_user_id FROM {$bp->friends->table_name} WHERE (friend_user_id = %d || initiator_user_id = %d) && is_confirmed = 1 ORDER BY rand() LIMIT %d", $user_id, $user_id, $total_friends ); |
---|
292 | $results = $wpdb->get_results( $sql ); |
---|
293 | |
---|
294 | for ( $i = 0, $count = count( $results ); $i < $count; ++$i ) { |
---|
295 | $fids[] = ( $results[$i]->friend_user_id == $user_id ) ? $results[$i]->initiator_user_id : $results[$i]->friend_user_id; |
---|
296 | } |
---|
297 | |
---|
298 | // remove duplicates |
---|
299 | if ( count( $fids ) > 0 ) |
---|
300 | return array_flip( array_flip( $fids ) ); |
---|
301 | else |
---|
302 | return false; |
---|
303 | } |
---|
304 | |
---|
305 | function get_invitable_friend_count( $user_id, $group_id ) { |
---|
306 | |
---|
307 | // Setup some data we'll use below |
---|
308 | $is_group_admin = BP_Groups_Member::check_is_admin( $user_id, $group_id ); |
---|
309 | $friend_ids = BP_Friends_Friendship::get_friend_user_ids( $user_id ); |
---|
310 | $invitable_count = 0; |
---|
311 | |
---|
312 | for ( $i = 0, $count = count( $friend_ids ); $i < $count; ++$i ) { |
---|
313 | |
---|
314 | // If already a member, they cannot be invited again |
---|
315 | if ( BP_Groups_Member::check_is_member( (int) $friend_ids[$i], $group_id ) ) |
---|
316 | continue; |
---|
317 | |
---|
318 | // If user already has invite, they cannot be added |
---|
319 | if ( BP_Groups_Member::check_has_invite( (int) $friend_ids[$i], $group_id ) ) |
---|
320 | continue; |
---|
321 | |
---|
322 | // If user is not group admin and friend is banned, they cannot be invited |
---|
323 | if ( ( false === $is_group_admin ) && BP_Groups_Member::check_is_banned( (int) $friend_ids[$i], $group_id ) ) |
---|
324 | continue; |
---|
325 | |
---|
326 | $invitable_count++; |
---|
327 | } |
---|
328 | |
---|
329 | return $invitable_count; |
---|
330 | } |
---|
331 | |
---|
332 | function get_user_ids_for_friendship( $friendship_id ) { |
---|
333 | global $wpdb, $bp; |
---|
334 | |
---|
335 | return $wpdb->get_row( $wpdb->prepare( "SELECT friend_user_id, initiator_user_id FROM {$bp->friends->table_name} WHERE id = %d", $friendship_id ) ); |
---|
336 | } |
---|
337 | |
---|
338 | function delete_all_for_user( $user_id ) { |
---|
339 | global $wpdb, $bp; |
---|
340 | |
---|
341 | // Get friends of $user_id |
---|
342 | $friend_ids = BP_Friends_Friendship::get_friend_user_ids( $user_id ); |
---|
343 | |
---|
344 | // Delete all friendships related to $user_id |
---|
345 | $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->friends->table_name} WHERE friend_user_id = %d OR initiator_user_id = %d", $user_id, $user_id ) ); |
---|
346 | |
---|
347 | // Delete friend request notifications for members who have a notification from this user. |
---|
348 | $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->core->table_name_notifications} WHERE component_name = 'friends' AND ( component_action = 'friendship_request' OR component_action = 'friendship_accepted' ) AND item_id = %d", $user_id ) ); |
---|
349 | |
---|
350 | // Loop through friend_ids and update their counts |
---|
351 | foreach ( (array) $friend_ids as $friend_id ) { |
---|
352 | BP_Friends_Friendship::total_friend_count( $friend_id ); |
---|
353 | } |
---|
354 | } |
---|
355 | } |
---|