bbp_get_user_favorites_link( array $args = array(), int $user_id, bool $wrap = true )
User favorites link
Description Description
Return the link to make a topic favorite/remove a topic from favorites
Parameters 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: array()
- $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 Return
(string) User favorites link
Source Source
File: includes/users/template.php
function bbp_get_user_favorites_link( $args = array(), $user_id = 0, $wrap = true ) {
// Bail if favorites are inactive
if ( ! bbp_is_favorites_active() ) {
return false;
}
// Parse arguments against default values
$r = bbp_parse_args( $args, array(
'favorite' => esc_html__( 'Favorite', 'bbpress' ),
'favorited' => esc_html__( 'Unfavorite', 'bbpress' ),
'user_id' => 0,
'object_id' => 0,
'object_type' => 'post',
'before' => '',
'after' => '',
'redirect_to' => '',
// Deprecated. Use object_id.
'forum_id' => 0,
'topic_id' => 0
), 'get_user_favorites_link' );
// Validate user and object ID's
$user_id = bbp_get_user_id( $r['user_id'], true, true );
$object_type = sanitize_key( $r['object_type'] );
// Back-compat for deprecated arguments
if ( ! empty( $r['topic_id'] ) ) {
$object_id = absint( $r['topic_id'] );
} elseif ( ! empty( $r['forum_id'] ) ) {
$object_id = absint( $r['forum_id'] );
} else {
$object_id = absint( $r['object_id'] );
}
// Bail if empty
if ( empty( $user_id ) || empty( $object_id ) || empty( $object_type ) ) {
return false;
}
// No link if you can't edit yourself
if ( ! current_user_can( 'edit_user', $user_id ) ) {
return false;
}
// Decide which link to show
$is_fav = bbp_is_user_favorite( $user_id, $object_id );
if ( ! empty( $is_fav ) ) {
$text = $r['favorited'];
$q_args = array(
'action' => 'bbp_favorite_remove',
'object_id' => $object_id
);
} else {
$text = $r['favorite'];
$q_args = array(
'action' => 'bbp_favorite_add',
'object_id' => $object_id
);
}
// Custom redirect
if ( ! empty( $r['redirect_to'] ) ) {
$q_args['redirect_to'] = urlencode( $r['redirect_to'] );
}
// URL
$url = esc_url( wp_nonce_url( add_query_arg( $q_args ), 'toggle-favorite_' . $object_id ) );
$sub = $is_fav ? ' class="is-favorite"' : '';
$html = sprintf( '%s<span id="favorite-%d" %s><a href="%s" class="favorite-toggle" data-bbp-object-id="%d" data-bbp-object-type="%s" data-bbp-nonce="%s">%s</a></span>%s', $r['before'], $object_id, $sub, $url, $object_id, $object_type, wp_create_nonce( 'toggle-favorite_' . $object_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>';
}
// Filter & return
return apply_filters( 'bbp_get_user_favorites_link', $html, $r, $user_id, $object_id );
}
Changelog Changelog
| Version | Description |
|---|---|
| 2.6.0 | bbPress (r6308) Add 'redirect_to' support |
| 2.0.0 | Introduced. |