bbp_get_user_favorites_link( mixed $args = '', int $user_id, bool $wrap = true )
User favorites link
Description
Return the link to make a topic favorite/remove a topic from favorites
Parameters
- $args
-
(Optional) This function supports these arguments: - subscribe: Favorite text - unsubscribe: Unfavorite text - user_id: User id - topic_id: Topic id - before: Before the link - after: After the link
Default value: ''
- $user_id
-
(Optional) User id
- $topic_id
-
(Optional) Topic id
- $wrap
-
(Optional) If you want to wrap the link in <span id="favorite-toggle">. See ajax_favorite()
Default value: true
Return
(string) User favorites link
Source
File: bp-forums/users/template.php
function bbp_get_user_favorites_link( $args = '', $user_id = 0, $wrap = true ) {
if ( ! bbp_is_favorites_active() ) {
return false;
}
// Parse arguments against default values
$r = bbp_parse_args( $args, array(
'favorite' => __( 'Favorite', 'buddyboss' ),
'favorited' => __( 'Unfavorite', 'buddyboss' ),
'user_id' => 0,
'topic_id' => 0,
'before' => '',
'after' => ''
), 'get_user_favorites_link' );
// Validate user and topic ID's
$user_id = bbp_get_user_id( $r['user_id'], true, true );
$topic_id = bbp_get_topic_id( $r['topic_id'] );
if ( empty( $user_id ) || empty( $topic_id ) ) {
return false;
}
// No link if you can't edit yourself
if ( ! current_user_can( 'edit_user', (int) $user_id ) ) {
return false;
}
// Decide which link to show
$is_fav = bbp_is_user_favorite( $user_id, $topic_id );
if ( ! empty( $is_fav ) ) {
$text = $r['favorited'];
$query_args = array( 'action' => 'bbp_favorite_remove', 'topic_id' => $topic_id );
} else {
$text = $r['favorite'];
$query_args = array( 'action' => 'bbp_favorite_add', 'topic_id' => $topic_id );
}
// Create the link based where the user is and if the topic is
// already the user's favorite
if ( bbp_is_favorites() ) {
$permalink = bbp_get_favorites_permalink( $user_id );
} elseif ( bbp_is_single_topic() || bbp_is_single_reply() ) {
$permalink = bbp_get_topic_permalink( $topic_id );
} else {
$permalink = get_permalink();
}
$url = esc_url( wp_nonce_url( add_query_arg( $query_args, $permalink ), 'toggle-favorite_' . $topic_id ) );
$sub = $is_fav ? ' class="is-favorite"' : '';
$html = sprintf( '%s<span id="favorite-%d" %s><a href="%s" class="favorite-toggle" data-topic="%d">%s</a></span>%s', $r['before'], $topic_id, $sub, $url, $topic_id, $text, $r['after'] );
// Initial output is wrapped in a span, ajax output is hooked to this
if ( ! empty( $wrap ) ) {
$html = '<span id="favorite-toggle">' . $html . '</span>';
}
// Return the link
return apply_filters( 'bbp_get_user_favorites_link', $html, $r, $user_id, $topic_id );
}
Changelog
| Version | Description |
|---|---|
| bbPress (r2652) | Introduced. |
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.