| 1 | <?php
|
|---|
| 2 | /**
|
|---|
| 3 | * BuddyPress Friends Template Functions.
|
|---|
| 4 | *
|
|---|
| 5 | * @package BuddyPress
|
|---|
| 6 | * @subpackage FriendsTemplate
|
|---|
| 7 | * @since 1.5.0
|
|---|
| 8 | */
|
|---|
| 9 |
|
|---|
| 10 | // Exit if accessed directly.
|
|---|
| 11 | defined( 'ABSPATH' ) || exit;
|
|---|
| 12 |
|
|---|
| 13 | /**
|
|---|
| 14 | * Output the friends component slug.
|
|---|
| 15 | *
|
|---|
| 16 | * @since 1.5.0
|
|---|
| 17 | *
|
|---|
| 18 | */
|
|---|
| 19 | function bp_friends_slug() {
|
|---|
| 20 | echo bp_get_friends_slug();
|
|---|
| 21 | }
|
|---|
| 22 | /**
|
|---|
| 23 | * Return the friends component slug.
|
|---|
| 24 | *
|
|---|
| 25 | * @since 1.5.0
|
|---|
| 26 | *
|
|---|
| 27 | * @return string
|
|---|
| 28 | */
|
|---|
| 29 | function bp_get_friends_slug() {
|
|---|
| 30 |
|
|---|
| 31 | /**
|
|---|
| 32 | * Filters the friends component slug.
|
|---|
| 33 | *
|
|---|
| 34 | * @since 1.5.0
|
|---|
| 35 | *
|
|---|
| 36 | * @param string $value Friends component slug.
|
|---|
| 37 | */
|
|---|
| 38 | return apply_filters( 'bp_get_friends_slug', buddypress()->friends->slug );
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | /**
|
|---|
| 42 | * Output the friends component root slug.
|
|---|
| 43 | *
|
|---|
| 44 | * @since 1.5.0
|
|---|
| 45 | *
|
|---|
| 46 | */
|
|---|
| 47 | function bp_friends_root_slug() {
|
|---|
| 48 | echo bp_get_friends_root_slug();
|
|---|
| 49 | }
|
|---|
| 50 | /**
|
|---|
| 51 | * Return the friends component root slug.
|
|---|
| 52 | *
|
|---|
| 53 | * @since 1.5.0
|
|---|
| 54 | *
|
|---|
| 55 | * @return string
|
|---|
| 56 | */
|
|---|
| 57 | function bp_get_friends_root_slug() {
|
|---|
| 58 |
|
|---|
| 59 | /**
|
|---|
| 60 | * Filters the friends component root slug.
|
|---|
| 61 | *
|
|---|
| 62 | * @since 1.5.0
|
|---|
| 63 | *
|
|---|
| 64 | * @param string $value Friends component root slug.
|
|---|
| 65 | */
|
|---|
| 66 | return apply_filters( 'bp_get_friends_root_slug', buddypress()->friends->root_slug );
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | /**
|
|---|
| 70 | * Output a block of random friends.
|
|---|
| 71 | *
|
|---|
| 72 | * No longer used in BuddyPress.
|
|---|
| 73 | *
|
|---|
| 74 | * @todo Deprecate
|
|---|
| 75 | */
|
|---|
| 76 | function bp_friends_random_friends() {
|
|---|
| 77 |
|
|---|
| 78 | if ( !$friend_ids = wp_cache_get( 'friends_friend_ids_' . bp_displayed_user_id(), 'bp' ) ) {
|
|---|
| 79 | $friend_ids = BP_Friends_Friendship::get_random_friends( bp_displayed_user_id() );
|
|---|
| 80 | wp_cache_set( 'friends_friend_ids_' . bp_displayed_user_id(), $friend_ids, 'bp' );
|
|---|
| 81 | } ?>
|
|---|
| 82 |
|
|---|
| 83 | <div class="info-group">
|
|---|
| 84 | <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>
|
|---|
| 85 |
|
|---|
| 86 | <?php if ( $friend_ids ) { ?>
|
|---|
| 87 |
|
|---|
| 88 | <ul class="horiz-gallery">
|
|---|
| 89 |
|
|---|
| 90 | <?php for ( $i = 0, $count = count( $friend_ids ); $i < $count; ++$i ) { ?>
|
|---|
| 91 |
|
|---|
| 92 | <li>
|
|---|
| 93 | <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>
|
|---|
| 94 | <h5><?php echo bp_core_get_userlink($friend_ids[$i]) ?></h5>
|
|---|
| 95 | </li>
|
|---|
| 96 |
|
|---|
| 97 | <?php } ?>
|
|---|
| 98 |
|
|---|
| 99 | </ul>
|
|---|
| 100 |
|
|---|
| 101 | <?php } else { ?>
|
|---|
| 102 |
|
|---|
| 103 | <div id="message" class="info">
|
|---|
| 104 | <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>
|
|---|
| 105 | </div>
|
|---|
| 106 |
|
|---|
| 107 | <?php } ?>
|
|---|
| 108 |
|
|---|
| 109 | <div class="clear"></div>
|
|---|
| 110 | </div>
|
|---|
| 111 |
|
|---|
| 112 | <?php
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | /**
|
|---|
| 116 | * Pull up a group of random members, and display some profile data about them.
|
|---|
| 117 | *
|
|---|
| 118 | * This function is no longer used by BuddyPress core.
|
|---|
| 119 | *
|
|---|
| 120 | * @todo Deprecate
|
|---|
| 121 | *
|
|---|
| 122 | * @param int $total_members The number of members to retrieve.
|
|---|
| 123 | */
|
|---|
| 124 | function bp_friends_random_members( $total_members = 5 ) {
|
|---|
| 125 |
|
|---|
| 126 | if ( !$user_ids = wp_cache_get( 'friends_random_users', 'bp' ) ) {
|
|---|
| 127 | $user_ids = BP_Core_User::get_users( 'random', $total_members );
|
|---|
| 128 | wp_cache_set( 'friends_random_users', $user_ids, 'bp' );
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | ?>
|
|---|
| 132 |
|
|---|
| 133 | <?php if ( $user_ids['users'] ) { ?>
|
|---|
| 134 |
|
|---|
| 135 | <ul class="item-list" id="random-members-list">
|
|---|
| 136 |
|
|---|
| 137 | <?php for ( $i = 0, $count = count( $user_ids['users'] ); $i < $count; ++$i ) { ?>
|
|---|
| 138 |
|
|---|
| 139 | <li>
|
|---|
| 140 | <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>
|
|---|
| 141 | <h5><?php echo bp_core_get_userlink( $user_ids['users'][$i]->id ) ?></h5>
|
|---|
| 142 |
|
|---|
| 143 | <?php if ( bp_is_active( 'xprofile' ) ) { ?>
|
|---|
| 144 |
|
|---|
| 145 | <?php $random_data = xprofile_get_random_profile_data( $user_ids['users'][$i]->id, true ); ?>
|
|---|
| 146 |
|
|---|
| 147 | <div class="profile-data">
|
|---|
| 148 | <p class="field-name"><?php echo $random_data[0]->name ?></p>
|
|---|
| 149 |
|
|---|
| 150 | <?php echo $random_data[0]->value ?>
|
|---|
| 151 |
|
|---|
| 152 | </div>
|
|---|
| 153 |
|
|---|
| 154 | <?php } ?>
|
|---|
| 155 |
|
|---|
| 156 | <div class="action">
|
|---|
| 157 |
|
|---|
| 158 | <?php if ( bp_is_active( 'friends' ) ) { ?>
|
|---|
| 159 |
|
|---|
| 160 | <?php bp_add_friend_button( $user_ids['users'][$i]->id ) ?>
|
|---|
| 161 |
|
|---|
| 162 | <?php } ?>
|
|---|
| 163 |
|
|---|
| 164 | </div>
|
|---|
| 165 | </li>
|
|---|
| 166 |
|
|---|
| 167 | <?php } ?>
|
|---|
| 168 |
|
|---|
| 169 | </ul>
|
|---|
| 170 |
|
|---|
| 171 | <?php } else { ?>
|
|---|
| 172 |
|
|---|
| 173 | <div id="message" class="info">
|
|---|
| 174 | <p><?php _e( "There aren't enough site members to show a random sample just yet.", 'buddypress' ) ?></p>
|
|---|
| 175 | </div>
|
|---|
| 176 |
|
|---|
| 177 | <?php } ?>
|
|---|
| 178 | <?php
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | /**
|
|---|
| 182 | * Display a Friends search form.
|
|---|
| 183 | *
|
|---|
| 184 | * No longer used in BuddyPress.
|
|---|
| 185 | *
|
|---|
| 186 | * @todo Deprecate
|
|---|
| 187 | */
|
|---|
| 188 | function bp_friend_search_form() {
|
|---|
| 189 |
|
|---|
| 190 | $action = bp_displayed_user_domain() . bp_get_friends_slug() . '/my-friends/search/';
|
|---|
| 191 | $label = __( 'Filter Friends', 'buddypress' ); ?>
|
|---|
| 192 |
|
|---|
| 193 | <form action="<?php echo $action ?>" id="friend-search-form" method="post">
|
|---|
| 194 |
|
|---|
| 195 | <label for="friend-search-box" id="friend-search-label"><?php echo $label ?></label>
|
|---|
| 196 | <input type="search" name="friend-search-box" id="friend-search-box" value="<?php echo $value ?>"<?php echo $disabled ?> />
|
|---|
| 197 |
|
|---|
| 198 | <?php wp_nonce_field( 'friends_search', '_wpnonce_friend_search' ) ?>
|
|---|
| 199 |
|
|---|
| 200 | <input type="hidden" name="initiator" id="initiator" value="<?php echo esc_attr( bp_displayed_user_id() ) ?>" />
|
|---|
| 201 |
|
|---|
| 202 | </form>
|
|---|
| 203 |
|
|---|
| 204 | <?php
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 207 | /**
|
|---|
| 208 | * Output the "Add Friend" button in the member loop.
|
|---|
| 209 | *
|
|---|
| 210 | * @since 1.2.6
|
|---|
| 211 | */
|
|---|
| 212 | function bp_member_add_friend_button() {
|
|---|
| 213 | bp_add_friend_button( bp_get_member_user_id() );
|
|---|
| 214 | }
|
|---|
| 215 | add_action( 'bp_directory_members_actions', 'bp_member_add_friend_button' );
|
|---|
| 216 |
|
|---|
| 217 | /**
|
|---|
| 218 | * Output the friend count for the current member in the loop.
|
|---|
| 219 | *
|
|---|
| 220 | * @since 1.2.0
|
|---|
| 221 | */
|
|---|
| 222 | function bp_member_total_friend_count() {
|
|---|
| 223 | echo bp_get_member_total_friend_count();
|
|---|
| 224 | }
|
|---|
| 225 | /**
|
|---|
| 226 | * Return the friend count for the current member in the loop.
|
|---|
| 227 | *
|
|---|
| 228 | * Return value is a string of the form "x friends".
|
|---|
| 229 | *
|
|---|
| 230 | * @since 1.2.0
|
|---|
| 231 | *
|
|---|
| 232 | * @return string A string of the form "x friends".
|
|---|
| 233 | */
|
|---|
| 234 | function bp_get_member_total_friend_count() {
|
|---|
| 235 | global $members_template;
|
|---|
| 236 |
|
|---|
| 237 | if ( 1 == (int) $members_template->member->total_friend_count ) {
|
|---|
| 238 |
|
|---|
| 239 | /**
|
|---|
| 240 | * Filters text used to denote total friend count.
|
|---|
| 241 | *
|
|---|
| 242 | * @since 1.2.0
|
|---|
| 243 | *
|
|---|
| 244 | * @param string $value String of the form "x friends".
|
|---|
| 245 | * @param int $value Total friend count for current member in the loop.
|
|---|
| 246 | */
|
|---|
| 247 | return apply_filters( 'bp_get_member_total_friend_count', sprintf( __( '%d friend', 'buddypress' ), (int) $members_template->member->total_friend_count ) );
|
|---|
| 248 | } else {
|
|---|
| 249 |
|
|---|
| 250 | /** This filter is documented in bp-friends/bp-friends-template.php */
|
|---|
| 251 | return apply_filters( 'bp_get_member_total_friend_count', sprintf( __( '%d friends', 'buddypress' ), (int) $members_template->member->total_friend_count ) );
|
|---|
| 252 | }
|
|---|
| 253 | }
|
|---|
| 254 |
|
|---|
| 255 | /**
|
|---|
| 256 | * Output the ID of the current user in the friend request loop.
|
|---|
| 257 | *
|
|---|
| 258 | * @since 1.2.6
|
|---|
| 259 | *
|
|---|
| 260 | * @see bp_get_potential_friend_id() for a description of arguments.
|
|---|
| 261 | *
|
|---|
| 262 | * @param int $user_id See {@link bp_get_potential_friend_id()}.
|
|---|
| 263 | */
|
|---|
| 264 | function bp_potential_friend_id( $user_id = 0 ) {
|
|---|
| 265 | echo bp_get_potential_friend_id( $user_id );
|
|---|
| 266 | }
|
|---|
| 267 | /**
|
|---|
| 268 | * Return the ID of current user in the friend request loop.
|
|---|
| 269 | *
|
|---|
| 270 | * @since 1.2.6
|
|---|
| 271 | *
|
|---|
| 272 | * @global object $friends_template
|
|---|
| 273 | *
|
|---|
| 274 | * @param int $user_id Optional. If provided, the function will simply
|
|---|
| 275 | * return this value.
|
|---|
| 276 | * @return int ID of potential friend.
|
|---|
| 277 | */
|
|---|
| 278 | function bp_get_potential_friend_id( $user_id = 0 ) {
|
|---|
| 279 | global $friends_template;
|
|---|
| 280 |
|
|---|
| 281 | if ( empty( $user_id ) && isset( $friends_template->friendship->friend ) )
|
|---|
| 282 | $user_id = $friends_template->friendship->friend->id;
|
|---|
| 283 | elseif ( empty( $user_id ) && !isset( $friends_template->friendship->friend ) )
|
|---|
| 284 | $user_id = bp_displayed_user_id();
|
|---|
| 285 |
|
|---|
| 286 | /**
|
|---|
| 287 | * Filters the ID of current user in the friend request loop.
|
|---|
| 288 | *
|
|---|
| 289 | * @since 1.2.10
|
|---|
| 290 | *
|
|---|
| 291 | * @param int $user_id ID of current user in the friend request loop.
|
|---|
| 292 | */
|
|---|
| 293 | return apply_filters( 'bp_get_potential_friend_id', (int) $user_id );
|
|---|
| 294 | }
|
|---|
| 295 |
|
|---|
| 296 | /**
|
|---|
| 297 | * Check whether a given user is a friend of the logged-in user.
|
|---|
| 298 | *
|
|---|
| 299 | * Returns - 'is_friend', 'not_friends', 'pending'.
|
|---|
| 300 | *
|
|---|
| 301 | * @since 1.2.6
|
|---|
| 302 | *
|
|---|
| 303 | * @param int $user_id ID of the potential friend. Default: the value of
|
|---|
| 304 | * {@link bp_get_potential_friend_id()}.
|
|---|
| 305 | * @return string 'is_friend', 'not_friends', or 'pending'.
|
|---|
| 306 | */
|
|---|
| 307 | function bp_is_friend( $user_id = 0 ) {
|
|---|
| 308 |
|
|---|
| 309 | if ( !is_user_logged_in() )
|
|---|
| 310 | return false;
|
|---|
| 311 |
|
|---|
| 312 | if ( empty( $user_id ) )
|
|---|
| 313 | $user_id = bp_get_potential_friend_id( $user_id );
|
|---|
| 314 |
|
|---|
| 315 | if ( bp_loggedin_user_id() == $user_id )
|
|---|
| 316 | return false;
|
|---|
| 317 |
|
|---|
| 318 | /**
|
|---|
| 319 | * Filters the status of friendship between logged in user and given user.
|
|---|
| 320 | *
|
|---|
| 321 | * @since 1.2.10
|
|---|
| 322 | *
|
|---|
| 323 | * @param string $value String status of friendship. Possible values are 'is_friend', 'not_friends', 'pending'.
|
|---|
| 324 | */
|
|---|
| 325 | return apply_filters( 'bp_is_friend', friends_check_friendship_status( bp_loggedin_user_id(), $user_id ), $user_id );
|
|---|
| 326 | }
|
|---|
| 327 |
|
|---|
| 328 | /**
|
|---|
| 329 | * Output the Add Friend button.
|
|---|
| 330 | *
|
|---|
| 331 | * @since 1.0.0
|
|---|
| 332 | *
|
|---|
| 333 | * @see bp_get_add_friend_button() for information on arguments.
|
|---|
| 334 | *
|
|---|
| 335 | * @param int $potential_friend_id See {@link bp_get_add_friend_button()}.
|
|---|
| 336 | * @param int|bool $friend_status See {@link bp_get_add_friend_button()}.
|
|---|
| 337 | */
|
|---|
| 338 | function bp_add_friend_button( $potential_friend_id = 0, $friend_status = false ) {
|
|---|
| 339 | echo bp_get_add_friend_button( $potential_friend_id, $friend_status );
|
|---|
| 340 | }
|
|---|
| 341 | /**
|
|---|
| 342 | * Create the Add Friend button.
|
|---|
| 343 | *
|
|---|
| 344 | * @since 1.1.0
|
|---|
| 345 | *
|
|---|
| 346 | * @param int $potential_friend_id ID of the user to whom the button
|
|---|
| 347 | * applies. Default: value of {@link bp_get_potential_friend_id()}.
|
|---|
| 348 | * @param bool $friend_status Not currently used.
|
|---|
| 349 | * @return string HTML for the Add Friend button.
|
|---|
| 350 | */
|
|---|
| 351 | function bp_get_add_friend_button( $potential_friend_id = 0, $friend_status = false ) {
|
|---|
| 352 |
|
|---|
| 353 | if ( empty( $potential_friend_id ) )
|
|---|
| 354 | $potential_friend_id = bp_get_potential_friend_id( $potential_friend_id );
|
|---|
| 355 |
|
|---|
| 356 | $is_friend = bp_is_friend( $potential_friend_id );
|
|---|
| 357 |
|
|---|
| 358 | if ( empty( $is_friend ) )
|
|---|
| 359 | return false;
|
|---|
| 360 |
|
|---|
| 361 | switch ( $is_friend ) {
|
|---|
| 362 | case 'pending' :
|
|---|
| 363 | $button = array(
|
|---|
| 364 | 'id' => 'pending',
|
|---|
| 365 | 'component' => 'friends',
|
|---|
| 366 | 'must_be_logged_in' => true,
|
|---|
| 367 | 'block_self' => true,
|
|---|
| 368 | 'wrapper_class' => 'friendship-button pending_friend',
|
|---|
| 369 | 'wrapper_id' => 'friendship-button-' . $potential_friend_id,
|
|---|
| 370 | 'link_href' => wp_nonce_url( bp_loggedin_user_domain() . bp_get_friends_slug() . '/requests/cancel/' . $potential_friend_id . '/', 'friends_withdraw_friendship' ),
|
|---|
| 371 | 'link_text' => __( 'Cancel Friendship Request', 'buddypress' ),
|
|---|
| 372 | 'link_id' => 'friend-' . $potential_friend_id,
|
|---|
| 373 | 'link_rel' => 'remove',
|
|---|
| 374 | 'link_class' => 'friendship-button pending_friend requested'
|
|---|
| 375 | );
|
|---|
| 376 | break;
|
|---|
| 377 |
|
|---|
| 378 | case 'awaiting_response' :
|
|---|
| 379 | $button = array(
|
|---|
| 380 | 'id' => 'awaiting_response',
|
|---|
| 381 | 'component' => 'friends',
|
|---|
| 382 | 'must_be_logged_in' => true,
|
|---|
| 383 | 'block_self' => true,
|
|---|
| 384 | 'wrapper_class' => 'friendship-button awaiting_response_friend',
|
|---|
| 385 | 'wrapper_id' => 'friendship-button-' . $potential_friend_id,
|
|---|
| 386 | 'link_href' => bp_loggedin_user_domain() . bp_get_friends_slug() . '/requests/',
|
|---|
| 387 | 'link_text' => __( 'Friendship Requested', 'buddypress' ),
|
|---|
| 388 | 'link_id' => 'friend-' . $potential_friend_id,
|
|---|
| 389 | 'link_rel' => 'remove',
|
|---|
| 390 | 'link_class' => 'friendship-button awaiting_response_friend requested'
|
|---|
| 391 | );
|
|---|
| 392 | break;
|
|---|
| 393 |
|
|---|
| 394 | case 'is_friend' :
|
|---|
| 395 | $button = array(
|
|---|
| 396 | 'id' => 'is_friend',
|
|---|
| 397 | 'component' => 'friends',
|
|---|
| 398 | 'must_be_logged_in' => true,
|
|---|
| 399 | 'block_self' => false,
|
|---|
| 400 | 'wrapper_class' => 'friendship-button is_friend',
|
|---|
| 401 | 'wrapper_id' => 'friendship-button-' . $potential_friend_id,
|
|---|
| 402 | 'link_href' => wp_nonce_url( bp_loggedin_user_domain() . bp_get_friends_slug() . '/remove-friend/' . $potential_friend_id . '/', 'friends_remove_friend' ),
|
|---|
| 403 | 'link_text' => __( 'Cancel Friendship', 'buddypress' ),
|
|---|
| 404 | 'link_id' => 'friend-' . $potential_friend_id,
|
|---|
| 405 | 'link_rel' => 'remove',
|
|---|
| 406 | 'link_class' => 'friendship-button is_friend remove'
|
|---|
| 407 | );
|
|---|
| 408 | break;
|
|---|
| 409 |
|
|---|
| 410 | default:
|
|---|
| 411 | $button = array(
|
|---|
| 412 | 'id' => 'not_friends',
|
|---|
| 413 | 'component' => 'friends',
|
|---|
| 414 | 'must_be_logged_in' => true,
|
|---|
| 415 | 'block_self' => true,
|
|---|
| 416 | 'wrapper_class' => 'friendship-button not_friends',
|
|---|
| 417 | 'wrapper_id' => 'friendship-button-' . $potential_friend_id,
|
|---|
| 418 | 'link_href' => wp_nonce_url( bp_loggedin_user_domain() . bp_get_friends_slug() . '/add-friend/' . $potential_friend_id . '/', 'friends_add_friend' ),
|
|---|
| 419 | 'link_text' => __( 'Add Friend', 'buddypress' ),
|
|---|
| 420 | 'link_id' => 'friend-' . $potential_friend_id,
|
|---|
| 421 | 'link_rel' => 'add',
|
|---|
| 422 | 'link_class' => 'friendship-button not_friends add'
|
|---|
| 423 | );
|
|---|
| 424 | break;
|
|---|
| 425 | }
|
|---|
| 426 |
|
|---|
| 427 | /**
|
|---|
| 428 | * Filters the HTML for the add friend button.
|
|---|
| 429 | *
|
|---|
| 430 | * @since 1.1.0
|
|---|
| 431 | *
|
|---|
| 432 | * @param string $button HTML markup for add friend button.
|
|---|
| 433 | */
|
|---|
| 434 | return bp_get_button( apply_filters( 'bp_get_add_friend_button', $button ) );
|
|---|
| 435 | }
|
|---|
| 436 |
|
|---|
| 437 | /**
|
|---|
| 438 | * Get a comma-separated list of IDs of a user's friends.
|
|---|
| 439 | *
|
|---|
| 440 | * @since 1.2.0
|
|---|
| 441 | *
|
|---|
| 442 | * @param int $user_id Optional. Default: the displayed user's ID, or the
|
|---|
| 443 | * logged-in user's ID.
|
|---|
| 444 | * @return string|bool A comma-separated list of friend IDs if any are found,
|
|---|
| 445 | * otherwise false.
|
|---|
| 446 | */
|
|---|
| 447 | function bp_get_friend_ids( $user_id = 0 ) {
|
|---|
| 448 |
|
|---|
| 449 | if ( empty( $user_id ) )
|
|---|
| 450 | $user_id = ( bp_displayed_user_id() ) ? bp_displayed_user_id() : bp_loggedin_user_id();
|
|---|
| 451 |
|
|---|
| 452 | $friend_ids = friends_get_friend_user_ids( $user_id );
|
|---|
| 453 |
|
|---|
| 454 | if ( empty( $friend_ids ) )
|
|---|
| 455 | return false;
|
|---|
| 456 |
|
|---|
| 457 | return implode( ',', friends_get_friend_user_ids( $user_id ) );
|
|---|
| 458 | }
|
|---|
| 459 |
|
|---|
| 460 | /**
|
|---|
| 461 | * Get a user's friendship requests.
|
|---|
| 462 | *
|
|---|
| 463 | * Note that we return a 0 if no pending requests are found. This is necessary
|
|---|
| 464 | * because of the structure of the $include parameter in bp_has_members().
|
|---|
| 465 | *
|
|---|
| 466 | * @since 1.2.0
|
|---|
| 467 | *
|
|---|
| 468 | * @param int $user_id ID of the user whose requests are being retrieved.
|
|---|
| 469 | * Defaults to displayed user.
|
|---|
| 470 | * @return array|int An array of user IDs if found, or a 0 if none are found.
|
|---|
| 471 | */
|
|---|
| 472 | function bp_get_friendship_requests( $user_id = 0 ) {
|
|---|
| 473 | if ( !$user_id ) {
|
|---|
| 474 | $user_id = bp_displayed_user_id();
|
|---|
| 475 | }
|
|---|
| 476 |
|
|---|
| 477 | if ( !$user_id ) {
|
|---|
| 478 | return 0;
|
|---|
| 479 | }
|
|---|
| 480 |
|
|---|
| 481 | $requests = friends_get_friendship_request_user_ids( $user_id );
|
|---|
| 482 |
|
|---|
| 483 | if ( !empty( $requests ) ) {
|
|---|
| 484 | $requests = implode( ',', (array) $requests );
|
|---|
| 485 | } else {
|
|---|
| 486 | $requests = 0;
|
|---|
| 487 | }
|
|---|
| 488 |
|
|---|
| 489 | /**
|
|---|
| 490 | * Filters the total pending friendship requests for a user.
|
|---|
| 491 | *
|
|---|
| 492 | * @since 1.2.0
|
|---|
| 493 | * @since 2.6.0 Added the `$user_id` parameter.
|
|---|
| 494 | *
|
|---|
| 495 | * @param array|int $requests An array of user IDs if found, or a 0 if none are found.
|
|---|
| 496 | * @param int $user_id ID of the queried user.
|
|---|
| 497 | */
|
|---|
| 498 | return apply_filters( 'bp_get_friendship_requests', $requests, $user_id );
|
|---|
| 499 | }
|
|---|
| 500 |
|
|---|
| 501 | /**
|
|---|
| 502 | * Output the ID of the friendship between the logged-in user and the current user in the loop.
|
|---|
| 503 | *
|
|---|
| 504 | * @since 1.2.0
|
|---|
| 505 | */
|
|---|
| 506 | function bp_friend_friendship_id() {
|
|---|
| 507 | echo bp_get_friend_friendship_id();
|
|---|
| 508 | }
|
|---|
| 509 | /**
|
|---|
| 510 | * Return the ID of the friendship between the logged-in user and the current user in the loop.
|
|---|
| 511 | *
|
|---|
| 512 | * @since 1.2.0
|
|---|
| 513 | *
|
|---|
| 514 | * @return int ID of the friendship.
|
|---|
| 515 | */
|
|---|
| 516 | function bp_get_friend_friendship_id() {
|
|---|
| 517 | global $members_template;
|
|---|
| 518 |
|
|---|
| 519 | if ( !$friendship_id = wp_cache_get( 'friendship_id_' . $members_template->member->id . '_' . bp_loggedin_user_id() ) ) {
|
|---|
| 520 | $friendship_id = friends_get_friendship_id( $members_template->member->id, bp_loggedin_user_id() );
|
|---|
| 521 | wp_cache_set( 'friendship_id_' . $members_template->member->id . '_' . bp_loggedin_user_id(), $friendship_id, 'bp' );
|
|---|
| 522 | }
|
|---|
| 523 |
|
|---|
| 524 | /**
|
|---|
| 525 | * Filters the ID of the friendship between the logged in user and the current user in the loop.
|
|---|
| 526 | *
|
|---|
| 527 | * @since 1.2.0
|
|---|
| 528 | *
|
|---|
| 529 | * @param int $friendship_id ID of the friendship.
|
|---|
| 530 | */
|
|---|
| 531 | return apply_filters( 'bp_get_friend_friendship_id', $friendship_id );
|
|---|
| 532 | }
|
|---|
| 533 |
|
|---|
| 534 | /**
|
|---|
| 535 | * Output the URL for accepting the current friendship request in the loop.
|
|---|
| 536 | *
|
|---|
| 537 | * @since 1.0.0
|
|---|
| 538 | */
|
|---|
| 539 | function bp_friend_accept_request_link() {
|
|---|
| 540 | echo bp_get_friend_accept_request_link();
|
|---|
| 541 | }
|
|---|
| 542 | /**
|
|---|
| 543 | * Return the URL for accepting the current friendship request in the loop.
|
|---|
| 544 | *
|
|---|
| 545 | * @since 1.0.0
|
|---|
| 546 | *
|
|---|
| 547 | * @return string accept-friendship URL.
|
|---|
| 548 | */
|
|---|
| 549 | function bp_get_friend_accept_request_link() {
|
|---|
| 550 | global $members_template;
|
|---|
| 551 |
|
|---|
| 552 | if ( !$friendship_id = wp_cache_get( 'friendship_id_' . $members_template->member->id . '_' . bp_loggedin_user_id() ) ) {
|
|---|
| 553 | $friendship_id = friends_get_friendship_id( $members_template->member->id, bp_loggedin_user_id() );
|
|---|
| 554 | wp_cache_set( 'friendship_id_' . $members_template->member->id . '_' . bp_loggedin_user_id(), $friendship_id, 'bp' );
|
|---|
| 555 | }
|
|---|
| 556 |
|
|---|
| 557 | /**
|
|---|
| 558 | * Filters the URL for accepting the current friendship request in the loop.
|
|---|
| 559 | *
|
|---|
| 560 | * @since 1.0.0
|
|---|
| 561 | * @since 2.6.0 Added the `$friendship_id` parameter.
|
|---|
| 562 | *
|
|---|
| 563 | * @param string $value Accept-friendship URL.
|
|---|
| 564 | * @param int $friendship_id ID of the friendship.
|
|---|
| 565 | */
|
|---|
| 566 | /**
|
|---|
| 567 | * ORIGINAL SCRIPT
|
|---|
| 568 | */
|
|---|
| 569 | //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' ), $friendship_id );
|
|---|
| 570 |
|
|---|
| 571 | /**
|
|---|
| 572 | * UPDATED SCRIPT (23-FEB-2017)
|
|---|
| 573 | * To avoid the Accept request URL issue
|
|---|
| 574 | */
|
|---|
| 575 | 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' ), $friendship_id );
|
|---|
| 576 | }
|
|---|
| 577 |
|
|---|
| 578 | /**
|
|---|
| 579 | * Output the URL for rejecting the current friendship request in the loop.
|
|---|
| 580 | *
|
|---|
| 581 | * @since 1.0.0
|
|---|
| 582 | */
|
|---|
| 583 | function bp_friend_reject_request_link() {
|
|---|
| 584 | echo bp_get_friend_reject_request_link();
|
|---|
| 585 | }
|
|---|
| 586 | /**
|
|---|
| 587 | * Return the URL for rejecting the current friendship request in the loop.
|
|---|
| 588 | *
|
|---|
| 589 | * @since 1.0.0
|
|---|
| 590 | *
|
|---|
| 591 | * @return string reject-friendship URL.
|
|---|
| 592 | */
|
|---|
| 593 | function bp_get_friend_reject_request_link() {
|
|---|
| 594 | global $members_template;
|
|---|
| 595 |
|
|---|
| 596 | if ( !$friendship_id = wp_cache_get( 'friendship_id_' . $members_template->member->id . '_' . bp_loggedin_user_id() ) ) {
|
|---|
| 597 | $friendship_id = friends_get_friendship_id( $members_template->member->id, bp_loggedin_user_id() );
|
|---|
| 598 | wp_cache_set( 'friendship_id_' . $members_template->member->id . '_' . bp_loggedin_user_id(), $friendship_id, 'bp' );
|
|---|
| 599 | }
|
|---|
| 600 |
|
|---|
| 601 | /**
|
|---|
| 602 | * Filters the URL for rejecting the current friendship request in the loop.
|
|---|
| 603 | *
|
|---|
| 604 | * @since 1.0.0
|
|---|
| 605 | * @since 2.6.0 Added the `$friendship_id` parameter.
|
|---|
| 606 | *
|
|---|
| 607 | * @param string $value Reject-friendship URL.
|
|---|
| 608 | * @param int $friendship_id ID of the friendship.
|
|---|
| 609 | */
|
|---|
| 610 | 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' ), $friendship_id );
|
|---|
| 611 | }
|
|---|
| 612 |
|
|---|
| 613 | /**
|
|---|
| 614 | * Output the total friend count for a given user.
|
|---|
| 615 | *
|
|---|
| 616 | * @since 1.2.0
|
|---|
| 617 | *
|
|---|
| 618 | * @param int $user_id See {@link friends_get_total_friend_count()}.
|
|---|
| 619 | */
|
|---|
| 620 | function bp_total_friend_count( $user_id = 0 ) {
|
|---|
| 621 | echo bp_get_total_friend_count( $user_id );
|
|---|
| 622 | }
|
|---|
| 623 | /**
|
|---|
| 624 | * Return the total friend count for a given user.
|
|---|
| 625 | *
|
|---|
| 626 | * @since 1.2.0
|
|---|
| 627 | *
|
|---|
| 628 | * @param int $user_id See {@link friends_get_total_friend_count()}.
|
|---|
| 629 | * @return int Total friend count.
|
|---|
| 630 | */
|
|---|
| 631 | function bp_get_total_friend_count( $user_id = 0 ) {
|
|---|
| 632 |
|
|---|
| 633 | /**
|
|---|
| 634 | * Filters the total friend count for a given user.
|
|---|
| 635 | *
|
|---|
| 636 | * @since 1.2.0
|
|---|
| 637 | * @since 2.6.0 Added the `$user_id` parameter.
|
|---|
| 638 | *
|
|---|
| 639 | * @param int $value Total friend count.
|
|---|
| 640 | * @param int $user_id ID of the queried user.
|
|---|
| 641 | */
|
|---|
| 642 | return apply_filters( 'bp_get_total_friend_count', friends_get_total_friend_count( $user_id ), $user_id );
|
|---|
| 643 | }
|
|---|
| 644 | add_filter( 'bp_get_total_friend_count', 'bp_core_number_format' );
|
|---|
| 645 |
|
|---|
| 646 | /**
|
|---|
| 647 | * Output the total friendship request count for a given user.
|
|---|
| 648 | *
|
|---|
| 649 | * @since 1.2.0
|
|---|
| 650 | *
|
|---|
| 651 | * @see bp_friend_get_total_requests_count() for description of arguments.
|
|---|
| 652 | *
|
|---|
| 653 | * @param int $user_id See {@link bp_friend_get_total_requests_count().
|
|---|
| 654 | */
|
|---|
| 655 | function bp_friend_total_requests_count( $user_id = 0 ) {
|
|---|
| 656 | echo bp_friend_get_total_requests_count( $user_id );
|
|---|
| 657 | }
|
|---|
| 658 | /**
|
|---|
| 659 | * Return the total friendship request count for a given user.
|
|---|
| 660 | *
|
|---|
| 661 | * @since 1.2.0
|
|---|
| 662 | *
|
|---|
| 663 | * @param int $user_id ID of the user whose requests are being counted.
|
|---|
| 664 | * Default: ID of the logged-in user.
|
|---|
| 665 | * @return int Friend count.
|
|---|
| 666 | */
|
|---|
| 667 | function bp_friend_get_total_requests_count( $user_id = 0 ) {
|
|---|
| 668 | if ( empty( $user_id ) )
|
|---|
| 669 | $user_id = bp_loggedin_user_id();
|
|---|
| 670 |
|
|---|
| 671 | /**
|
|---|
| 672 | * Filters the total friendship request count for a given user.
|
|---|
| 673 | *
|
|---|
| 674 | * @since 1.2.0
|
|---|
| 675 | * @since 2.6.0 Added the `$user_id` parameter.
|
|---|
| 676 | *
|
|---|
| 677 | * @param int $value Friendship request count.
|
|---|
| 678 | * @param int $user_id ID of the queried user.
|
|---|
| 679 | */
|
|---|
| 680 | return apply_filters( 'bp_friend_get_total_requests_count', count( BP_Friends_Friendship::get_friend_user_ids( $user_id, true ) ), $user_id );
|
|---|
| 681 | }
|
|---|
| 682 |
|
|---|
| 683 | /** Stats **********************************************************************/
|
|---|
| 684 |
|
|---|
| 685 | /**
|
|---|
| 686 | * Display the number of friends in user's profile.
|
|---|
| 687 | *
|
|---|
| 688 | * @since 2.0.0
|
|---|
| 689 | *
|
|---|
| 690 | * @param array|string $args before|after|user_id.
|
|---|
| 691 | */
|
|---|
| 692 | function bp_friends_profile_stats( $args = '' ) {
|
|---|
| 693 | echo bp_friends_get_profile_stats( $args );
|
|---|
| 694 | }
|
|---|
| 695 | add_action( 'bp_members_admin_user_stats', 'bp_friends_profile_stats', 7, 1 );
|
|---|
| 696 |
|
|---|
| 697 | /**
|
|---|
| 698 | * Return the number of friends in user's profile.
|
|---|
| 699 | *
|
|---|
| 700 | * @since 2.0.0
|
|---|
| 701 | *
|
|---|
| 702 | * @param array|string $args before|after|user_id.
|
|---|
| 703 | * @return string HTML for stats output.
|
|---|
| 704 | */
|
|---|
| 705 | function bp_friends_get_profile_stats( $args = '' ) {
|
|---|
| 706 |
|
|---|
| 707 | // Parse the args.
|
|---|
| 708 | $r = bp_parse_args( $args, array(
|
|---|
| 709 | 'before' => '<li class="bp-friends-profile-stats">',
|
|---|
| 710 | 'after' => '</li>',
|
|---|
| 711 | 'user_id' => bp_displayed_user_id(),
|
|---|
| 712 | 'friends' => 0,
|
|---|
| 713 | 'output' => ''
|
|---|
| 714 | ), 'friends_get_profile_stats' );
|
|---|
| 715 |
|
|---|
| 716 | // Allow completely overloaded output.
|
|---|
| 717 | if ( empty( $r['output'] ) ) {
|
|---|
| 718 |
|
|---|
| 719 | // Only proceed if a user ID was passed.
|
|---|
| 720 | if ( ! empty( $r['user_id'] ) ) {
|
|---|
| 721 |
|
|---|
| 722 | // Get the user's friends.
|
|---|
| 723 | if ( empty( $r['friends'] ) ) {
|
|---|
| 724 | $r['friends'] = absint( friends_get_total_friend_count( $r['user_id'] ) );
|
|---|
| 725 | }
|
|---|
| 726 |
|
|---|
| 727 | // If friends exist, show some formatted output.
|
|---|
| 728 | $r['output'] = $r['before'] . sprintf( _n( '%s friend', '%s friends', $r['friends'], 'buddypress' ), '<strong>' . $r['friends'] . '</strong>' ) . $r['after'];
|
|---|
| 729 | }
|
|---|
| 730 | }
|
|---|
| 731 |
|
|---|
| 732 | /**
|
|---|
| 733 | * Filters the number of friends in user's profile.
|
|---|
| 734 | *
|
|---|
| 735 | * @since 2.0.0
|
|---|
| 736 | *
|
|---|
| 737 | * @param string $value Formatted string displaying total friends count.
|
|---|
| 738 | * @param array $r Array of arguments for string formatting and output.
|
|---|
| 739 | */
|
|---|
| 740 | return apply_filters( 'bp_friends_get_profile_stats', $r['output'], $r );
|
|---|
| 741 | }
|
|---|