Skip to:
Content

BuddyPress.org

Ticket #5157: bp-friends-template.php

File bp-friends-template.php, 14.7 KB (added by terraling, 13 years ago)
Line 
1<?php
2
3/**
4 * BuddyPress Friends Template Functions
5 *
6 * @package BuddyPress
7 * @subpackage FriendsTemplate
8 */
9
10// Exit if accessed directly
11if ( !defined( 'ABSPATH' ) ) exit;
12
13/**
14 * Output the friends component slug
15 *
16 * @package BuddyPress
17 * @subpackage Friends Template
18 * @since BuddyPress (1.5)
19 *
20 * @uses bp_get_friends_slug()
21 */
22function bp_friends_slug() {
23 echo bp_get_friends_slug();
24}
25 /**
26 * Return the friends component slug
27 *
28 * @package BuddyPress
29 * @subpackage Friends Template
30 * @since BuddyPress (1.5)
31 */
32 function bp_get_friends_slug() {
33 global $bp;
34 return apply_filters( 'bp_get_friends_slug', $bp->friends->slug );
35 }
36
37/**
38 * Output the friends component root slug
39 *
40 * @package BuddyPress
41 * @subpackage Friends Template
42 * @since BuddyPress (1.5)
43 *
44 * @uses bp_get_friends_root_slug()
45 */
46function bp_friends_root_slug() {
47 echo bp_get_friends_root_slug();
48}
49 /**
50 * Return the friends component root slug
51 *
52 * @package BuddyPress
53 * @subpackage Friends Template
54 * @since BuddyPress (1.5)
55 */
56 function bp_get_friends_root_slug() {
57 global $bp;
58 return apply_filters( 'bp_get_friends_root_slug', $bp->friends->root_slug );
59 }
60
61function bp_friends_random_friends() {
62
63 if ( !$friend_ids = wp_cache_get( 'friends_friend_ids_' . bp_displayed_user_id(), 'bp' ) ) {
64 $friend_ids = BP_Friends_Friendship::get_random_friends( bp_displayed_user_id() );
65 wp_cache_set( 'friends_friend_ids_' . bp_displayed_user_id(), $friend_ids, 'bp' );
66 } ?>
67
68 <div class="info-group">
69 <h4><?php bp_word_or_name( __( "My Friends", 'buddypress' ), __( "%s's Friends", 'buddypress' ) ) ?> (<?php echo BP_Friends_Friendship::total_friend_count( bp_displayed_user_id() ) ?>) <span><a href="<?php echo trailingslashit( bp_displayed_user_domain() . bp_get_friends_slug() ) ?>"><?php _e('See All', 'buddypress') ?></a></span></h4>
70
71 <?php if ( $friend_ids ) { ?>
72
73 <ul class="horiz-gallery">
74
75 <?php for ( $i = 0, $count = count( $friend_ids ); $i < $count; ++$i ) { ?>
76
77 <li>
78 <a href="<?php echo bp_core_get_user_domain( $friend_ids[$i] ) ?>"><?php echo bp_core_fetch_avatar( array( 'item_id' => $friend_ids[$i], 'type' => 'thumb' ) ) ?></a>
79 <h5><?php echo bp_core_get_userlink($friend_ids[$i]) ?></h5>
80 </li>
81
82 <?php } ?>
83
84 </ul>
85
86 <?php } else { ?>
87
88 <div id="message" class="info">
89 <p><?php bp_word_or_name( __( "You haven't added any friend connections yet.", 'buddypress' ), __( "%s hasn't created any friend connections yet.", 'buddypress' ) ) ?></p>
90 </div>
91
92 <?php } ?>
93
94 <div class="clear"></div>
95 </div>
96
97<?php
98}
99
100/**
101 * Pull up a group of random members, and display some profile data about them
102 *
103 * This function is no longer used by BuddyPress core.
104 *
105 * @package BuddyPress
106 *
107 * @param int $total_members The number of members to retrieve
108 */
109function bp_friends_random_members( $total_members = 5 ) {
110
111 if ( !$user_ids = wp_cache_get( 'friends_random_users', 'bp' ) ) {
112 $user_ids = BP_Core_User::get_users( 'random', $total_members );
113 wp_cache_set( 'friends_random_users', $user_ids, 'bp' );
114 }
115
116 ?>
117
118 <?php if ( $user_ids['users'] ) { ?>
119
120 <ul class="item-list" id="random-members-list">
121
122 <?php for ( $i = 0, $count = count( $user_ids['users'] ); $i < $count; ++$i ) { ?>
123
124 <li>
125 <a href="<?php echo bp_core_get_user_domain( $user_ids['users'][$i]->id ) ?>"><?php echo bp_core_fetch_avatar( array( 'item_id' => $user_ids['users'][$i]->id, 'type' => 'thumb' ) ) ?></a>
126 <h5><?php echo bp_core_get_userlink( $user_ids['users'][$i]->id ) ?></h5>
127
128 <?php if ( bp_is_active( 'xprofile' ) ) { ?>
129
130 <?php $random_data = xprofile_get_random_profile_data( $user_ids['users'][$i]->id, true ); ?>
131
132 <div class="profile-data">
133 <p class="field-name"><?php echo $random_data[0]->name ?></p>
134
135 <?php echo $random_data[0]->value ?>
136
137 </div>
138
139 <?php } ?>
140
141 <div class="action">
142
143 <?php if ( bp_is_active( 'friends' ) ) { ?>
144
145 <?php bp_add_friend_button( $user_ids['users'][$i]->id ) ?>
146
147 <?php } ?>
148
149 </div>
150 </li>
151
152 <?php } ?>
153
154 </ul>
155
156 <?php } else { ?>
157
158 <div id="message" class="info">
159 <p><?php _e( "There aren't enough site members to show a random sample just yet.", 'buddypress' ) ?></p>
160 </div>
161
162 <?php } ?>
163<?php
164}
165
166function bp_friend_search_form() {
167
168 $action = bp_displayed_user_domain() . bp_get_friends_slug() . '/my-friends/search/';
169 $label = __( 'Filter Friends', 'buddypress' ); ?>
170
171 <form action="<?php echo $action ?>" id="friend-search-form" method="post">
172
173 <label for="friend-search-box" id="friend-search-label"><?php echo $label ?></label>
174 <input type="search" name="friend-search-box" id="friend-search-box" value="<?php echo $value ?>"<?php echo $disabled ?> />
175
176 <?php wp_nonce_field( 'friends_search', '_wpnonce_friend_search' ) ?>
177
178 <input type="hidden" name="initiator" id="initiator" value="<?php echo esc_attr( bp_displayed_user_id() ) ?>" />
179
180 </form>
181
182 <?php
183}
184
185function bp_member_add_friend_button() {
186 global $members_template;
187
188 if ( !isset( $members_template->member->is_friend ) || null === $members_template->member->is_friend )
189 $friend_status = 'not_friends';
190 else
191 $friend_status = ( 0 == $members_template->member->is_friend ) ? 'pending' : 'is_friend';
192
193 echo bp_get_add_friend_button( $members_template->member->id, $friend_status );
194}
195add_action( 'bp_directory_members_actions', 'bp_member_add_friend_button' );
196
197function bp_member_total_friend_count() {
198 echo bp_get_member_total_friend_count();
199}
200 function bp_get_member_total_friend_count() {
201 global $members_template;
202
203 if ( 1 == (int) $members_template->member->total_friend_count )
204 return apply_filters( 'bp_get_member_total_friend_count', sprintf( __( '%d friend', 'buddypress' ), (int) $members_template->member->total_friend_count ) );
205 else
206 return apply_filters( 'bp_get_member_total_friend_count', sprintf( __( '%d friends', 'buddypress' ), (int) $members_template->member->total_friend_count ) );
207 }
208
209/**
210 * bp_potential_friend_id( $user_id )
211 *
212 * Outputs the ID of the potential friend
213 *
214 * @uses bp_get_potential_friend_id()
215 * @param int $user_id Optional
216 */
217function bp_potential_friend_id( $user_id = 0 ) {
218 echo bp_get_potential_friend_id( $user_id );
219}
220 /**
221 * bp_get_potential_friend_id( $user_id )
222 *
223 * Returns the ID of the potential friend
224 *
225 * @global object $friends_template
226 * @param int $user_id
227 * @return int ID of potential friend
228 */
229 function bp_get_potential_friend_id( $user_id = 0 ) {
230 global $friends_template;
231
232 if ( empty( $user_id ) && isset( $friends_template->friendship->friend ) )
233 $user_id = $friends_template->friendship->friend->id;
234 else if ( empty( $user_id ) && !isset( $friends_template->friendship->friend ) )
235 $user_id = bp_displayed_user_id();
236
237 return apply_filters( 'bp_get_potential_friend_id', (int) $user_id );
238 }
239
240/**
241 * bp_is_friend( $user_id )
242 *
243 * Returns - 'is_friend', 'not_friends', 'pending'
244 *
245 * @param int $potential_friend_id
246 * @return string
247 */
248function bp_is_friend( $user_id = 0 ) {
249
250 if ( !is_user_logged_in() )
251 return false;
252
253 if ( empty( $user_id ) )
254 $user_id = bp_get_potential_friend_id( $user_id );
255
256 if ( bp_loggedin_user_id() == $user_id )
257 return false;
258
259 return apply_filters( 'bp_is_friend', friends_check_friendship_status( bp_loggedin_user_id(), $user_id ), $user_id );
260}
261
262function bp_add_friend_button( $potential_friend_id = 0, $friend_status = false ) {
263 echo bp_get_add_friend_button( $potential_friend_id, $friend_status );
264}
265 function bp_get_add_friend_button( $potential_friend_id = 0, $friend_status = false ) {
266
267 if ( empty( $potential_friend_id ) )
268 $potential_friend_id = bp_get_potential_friend_id( $potential_friend_id );
269
270 $is_friend = bp_is_friend( $potential_friend_id );
271
272 if ( empty( $is_friend ) )
273 return false;
274
275 switch ( $is_friend ) {
276
277 case 'respond' :
278 $button = array(
279 'id' => 'respond',
280 'component' => 'friends',
281 'must_be_logged_in' => true,
282 'block_self' => true,
283 'wrapper_class' => 'friendship-button pending_friend',
284 'wrapper_id' => 'friendship-button-' . $potential_friend_id,
285 'link_href' => wp_nonce_url( bp_loggedin_user_domain() . bp_get_friends_slug() . '/requests/cancel/' . $potential_friend_id . '/', 'friends_withdraw_friendship' ),
286 'link_text' => __( 'Cancel Friendship Request', 'buddypress' ),
287 'link_title' => __( 'Cancel Friendship Requested', 'buddypress' ),
288 'link_id' => 'friend-' . $potential_friend_id,
289 'link_rel' => 'remove',
290 'link_class' => 'friendship-button pending_friend requested'
291 );
292 break;
293
294 case 'pending' :
295 $button = array(
296 'id' => 'pending',
297 'component' => 'friends',
298 'must_be_logged_in' => true,
299 'block_self' => true,
300 'wrapper_class' => 'friendship-button pending_friend',
301 'wrapper_id' => 'friendship-button-' . $potential_friend_id,
302 'link_href' => wp_nonce_url( bp_loggedin_user_domain() . bp_get_friends_slug() . '/requests/cancel/' . $potential_friend_id . '/', 'friends_withdraw_friendship' ),
303 'link_text' => __( 'Cancel Friendship Request', 'buddypress' ),
304 'link_title' => __( 'Cancel Friendship Requested', 'buddypress' ),
305 'link_id' => 'friend-' . $potential_friend_id,
306 'link_rel' => 'remove',
307 'link_class' => 'friendship-button pending_friend requested'
308 );
309 break;
310
311 case 'is_friend' :
312 $button = array(
313 'id' => 'is_friend',
314 'component' => 'friends',
315 'must_be_logged_in' => true,
316 'block_self' => false,
317 'wrapper_class' => 'friendship-button is_friend',
318 'wrapper_id' => 'friendship-button-' . $potential_friend_id,
319 'link_href' => wp_nonce_url( bp_loggedin_user_domain() . bp_get_friends_slug() . '/remove-friend/' . $potential_friend_id . '/', 'friends_remove_friend' ),
320 'link_text' => __( 'Cancel Friendship', 'buddypress' ),
321 'link_title' => __( 'Cancel Friendship', 'buddypress' ),
322 'link_id' => 'friend-' . $potential_friend_id,
323 'link_rel' => 'remove',
324 'link_class' => 'friendship-button is_friend remove'
325 );
326 break;
327
328 default:
329 $button = array(
330 'id' => 'not_friends',
331 'component' => 'friends',
332 'must_be_logged_in' => true,
333 'block_self' => true,
334 'wrapper_class' => 'friendship-button not_friends',
335 'wrapper_id' => 'friendship-button-' . $potential_friend_id,
336 'link_href' => wp_nonce_url( bp_loggedin_user_domain() . bp_get_friends_slug() . '/add-friend/' . $potential_friend_id . '/', 'friends_add_friend' ),
337 'link_text' => __( 'Add Friend', 'buddypress' ),
338 'link_title' => __( 'Add Friend', 'buddypress' ),
339 'link_id' => 'friend-' . $potential_friend_id,
340 'link_rel' => 'add',
341 'link_class' => 'friendship-button not_friends add'
342 );
343 break;
344 }
345
346 // Filter and return the HTML button
347 return bp_get_button( apply_filters( 'bp_get_add_friend_button', $button ) );
348 }
349
350function bp_get_friend_ids( $user_id = 0 ) {
351
352 if ( empty( $user_id ) )
353 $user_id = ( bp_displayed_user_id() ) ? bp_displayed_user_id() : bp_loggedin_user_id();
354
355 $friend_ids = friends_get_friend_user_ids( $user_id );
356
357 if ( empty( $friend_ids ) )
358 return false;
359
360 return implode( ',', friends_get_friend_user_ids( $user_id ) );
361}
362
363/**
364 * Get a user's friendship requests
365 *
366 * Note that we return a 0 if no pending requests are found. This is necessary because of the
367 * structure of the $include parameter in bp_has_members().
368 *
369 * @param int $user_id Defaults to displayed user
370 * @return mixed Returns an array of users if found, or a 0 if none are found
371 */
372function bp_get_friendship_requests( $user_id = 0 ) {
373 if ( !$user_id ) {
374 $user_id = bp_displayed_user_id();
375 }
376
377 if ( !$user_id ) {
378 return 0;
379 }
380
381 $requests = friends_get_friendship_request_user_ids( $user_id );
382
383 if ( !empty( $requests ) ) {
384 $requests = implode( ',', (array) $requests );
385 } else {
386 $requests = 0;
387 }
388
389 return apply_filters( 'bp_get_friendship_requests', $requests );
390}
391
392function bp_friend_friendship_id() {
393 echo bp_get_friend_friendship_id();
394}
395 function bp_get_friend_friendship_id() {
396 global $members_template;
397
398 if ( !$friendship_id = wp_cache_get( 'friendship_id_' . $members_template->member->id . '_' . bp_loggedin_user_id() ) ) {
399 $friendship_id = friends_get_friendship_id( $members_template->member->id, bp_loggedin_user_id() );
400 wp_cache_set( 'friendship_id_' . $members_template->member->id . '_' . bp_loggedin_user_id(), $friendship_id, 'bp' );
401 }
402
403 return apply_filters( 'bp_get_friend_friendship_id', $friendship_id );
404 }
405
406function bp_friend_accept_request_link() {
407 echo bp_get_friend_accept_request_link();
408}
409 function bp_get_friend_accept_request_link() {
410 global $members_template;
411
412 if ( !$friendship_id = wp_cache_get( 'friendship_id_' . $members_template->member->id . '_' . bp_loggedin_user_id() ) ) {
413 $friendship_id = friends_get_friendship_id( $members_template->member->id, bp_loggedin_user_id() );
414 wp_cache_set( 'friendship_id_' . $members_template->member->id . '_' . bp_loggedin_user_id(), $friendship_id, 'bp' );
415 }
416
417 return apply_filters( 'bp_get_friend_accept_request_link', wp_nonce_url( bp_loggedin_user_domain() . bp_get_friends_slug() . '/requests/accept/' . $friendship_id, 'friends_accept_friendship' ) );
418 }
419
420function bp_friend_reject_request_link() {
421 echo bp_get_friend_reject_request_link();
422}
423 function bp_get_friend_reject_request_link() {
424 global $members_template;
425
426 if ( !$friendship_id = wp_cache_get( 'friendship_id_' . $members_template->member->id . '_' . bp_loggedin_user_id() ) ) {
427 $friendship_id = friends_get_friendship_id( $members_template->member->id, bp_loggedin_user_id() );
428 wp_cache_set( 'friendship_id_' . $members_template->member->id . '_' . bp_loggedin_user_id(), $friendship_id, 'bp' );
429 }
430
431 return apply_filters( 'bp_get_friend_reject_request_link', wp_nonce_url( bp_loggedin_user_domain() . bp_get_friends_slug() . '/requests/reject/' . $friendship_id, 'friends_reject_friendship' ) );
432 }
433
434function bp_total_friend_count( $user_id = 0 ) {
435 echo bp_get_total_friend_count( $user_id );
436}
437 function bp_get_total_friend_count( $user_id = 0 ) {
438 return apply_filters( 'bp_get_total_friend_count', friends_get_total_friend_count( $user_id ) );
439 }
440 add_filter( 'bp_get_total_friend_count', 'bp_core_number_format' );
441
442function bp_friend_total_requests_count( $user_id = 0 ) {
443 echo bp_friend_get_total_requests_count( $user_id );
444}
445 function bp_friend_get_total_requests_count( $user_id = 0 ) {
446 if ( empty( $user_id ) )
447 $user_id = bp_loggedin_user_id();
448
449 return apply_filters( 'bp_friend_get_total_requests_count', count( BP_Friends_Friendship::get_friend_user_ids( $user_id, true ) ) );
450 }