bbp_get_user_favorites_link

User favorites link.

Description

(string) bbp_get_user_favorites_link( (string) $args = '', (int) $user_id = 0, (bool) $wrap = true ); 

Return the link to make a topic favorite/remove a topic from favorites

Returns (string)

User favorites link

Parameters (3)

0. $args — Optional. (string) => ''
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
1. $user_id — Optional. (int)
User id
2. $wrap — Optional. (bool) => true
Topic id

Usage

  1. if ( !function_exists( 'bbp_get_user_favorites_link' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'bbpress/includes/users/template.php'; 
  3. $args = ''; 
  4.  
  5. // Optional. User id 
  6. $user_id = -1; 
  7.  
  8. // Optional. Topic id 
  9. $wrap = true; 
  10.  
  11. // NOTICE! Understand what this does before running. 
  12. $result = bbp_get_user_favorites_link($args, $user_id, $wrap); 
  13.  

Defined (1)

The function is defined in the following location(s).

/includes/users/template.php  
  1. function bbp_get_user_favorites_link( $args = '', $user_id = 0, $wrap = true ) { 
  2. if ( ! bbp_is_favorites_active() ) { 
  3. return false; 
  4.  
  5. // Parse arguments against default values 
  6. $r = bbp_parse_args( $args, array( 
  7. 'favorite' => __( 'Favorite', bbpress ),  
  8. 'favorited' => __( 'Favorited', bbpress ),  
  9. 'user_id' => 0,  
  10. 'topic_id' => 0,  
  11. 'before' => '',  
  12. 'after' => '' 
  13. ), 'get_user_favorites_link' ); 
  14.  
  15. // Validate user and topic ID's 
  16. $user_id = bbp_get_user_id( $r['user_id'], true, true ); 
  17. $topic_id = bbp_get_topic_id( $r['topic_id'] ); 
  18. if ( empty( $user_id ) || empty( $topic_id ) ) { 
  19. return false; 
  20.  
  21. // No link if you can't edit yourself 
  22. if ( ! current_user_can( 'edit_user', (int) $user_id ) ) { 
  23. return false; 
  24.  
  25. // Decide which link to show 
  26. $is_fav = bbp_is_user_favorite( $user_id, $topic_id ); 
  27. if ( ! empty( $is_fav ) ) { 
  28. $text = $r['favorited']; 
  29. $query_args = array( 'action' => 'bbp_favorite_remove', 'topic_id' => $topic_id ); 
  30. } else { 
  31. $text = $r['favorite']; 
  32. $query_args = array( 'action' => 'bbp_favorite_add', 'topic_id' => $topic_id ); 
  33.  
  34. // Create the link based where the user is and if the topic is 
  35. // already the user's favorite 
  36. if ( bbp_is_favorites() ) { 
  37. $permalink = bbp_get_favorites_permalink( $user_id ); 
  38. } elseif ( bbp_is_single_topic() || bbp_is_single_reply() ) { 
  39. $permalink = bbp_get_topic_permalink( $topic_id ); 
  40. } else { 
  41. $permalink = get_permalink(); 
  42.  
  43. $url = esc_url( wp_nonce_url( add_query_arg( $query_args, $permalink ), 'toggle-favorite_' . $topic_id ) ); 
  44. $sub = $is_fav ? ' class="is-favorite"' : ''; 
  45. $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'] ); 
  46.  
  47. // Initial output is wrapped in a span, ajax output is hooked to this 
  48. if ( ! empty( $wrap ) ) { 
  49. $html = '<span id="favorite-toggle">' . $html . '</span>'; 
  50.  
  51. // Return the link 
  52. return apply_filters( 'bbp_get_user_favorites_link', $html, $r, $user_id, $topic_id );