bbp_get_user_subscribe_link

Return the link to subscribe/unsubscribe from a forum or topic.

Description

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

Returns (string)

Permanent link to topic

Parameters (3)

0. $args — Optional. (string) => ''
This function supports these arguments: - subscribe: Subscribe text - unsubscribe: Unsubscribe text - user_id: User id - topic_id: Topic id - forum_id: Forum id - before: Before the link - after: After the link
1. $user_id — Optional. (int)
User id
2. $wrap — Optional. (bool) => true
If you want to wrap the link in .

Usage

  1. if ( !function_exists( 'bbp_get_user_subscribe_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. If you want to wrap the link in . 
  9. $wrap = true; 
  10.  
  11. // NOTICE! Understand what this does before running. 
  12. $result = bbp_get_user_subscribe_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_subscribe_link( $args = '', $user_id = 0, $wrap = true ) { 
  2. return; 
  3.  
  4. // Parse arguments against default values 
  5. $r = bbp_parse_args( $args, array( 
  6. 'subscribe' => __( 'Subscribe', bbpress ),  
  7. 'unsubscribe' => __( 'Unsubscribe', bbpress ),  
  8. 'user_id' => 0,  
  9. 'topic_id' => 0,  
  10. 'forum_id' => 0,  
  11. 'before' => ' | ',  
  12. 'after' => '' 
  13. ), 'get_user_subscribe_link' ); 
  14.  
  15. // Validate user and object 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. $forum_id = bbp_get_forum_id( $r['forum_id'] ); 
  19. if ( empty( $user_id ) || ( empty( $topic_id ) && empty( $forum_id ) ) ) { 
  20. return false; 
  21.  
  22. // No link if you can't edit yourself 
  23. if ( ! current_user_can( 'edit_user', (int) $user_id ) ) { 
  24. return false; 
  25.  
  26. // Check if viewing a single forum 
  27. if ( empty( $topic_id ) && ! empty( $forum_id ) ) { 
  28.  
  29. // Decide which link to show 
  30. $is_subscribed = bbp_is_user_subscribed_to_forum( $user_id, $forum_id ); 
  31. if ( ! empty( $is_subscribed ) ) { 
  32. $text = $r['unsubscribe']; 
  33. $query_args = array( 'action' => 'bbp_unsubscribe', 'forum_id' => $forum_id ); 
  34. } else { 
  35. $text = $r['subscribe']; 
  36. $query_args = array( 'action' => 'bbp_subscribe', 'forum_id' => $forum_id ); 
  37.  
  38. // Create the link based where the user is and if the user is 
  39. // subscribed already 
  40. if ( bbp_is_subscriptions() ) { 
  41. $permalink = bbp_get_subscriptions_permalink( $user_id ); 
  42. } elseif ( bbp_is_single_forum() || bbp_is_single_reply() ) { 
  43. $permalink = bbp_get_forum_permalink( $forum_id ); 
  44. } else { 
  45. $permalink = get_permalink(); 
  46.  
  47. $url = esc_url( wp_nonce_url( add_query_arg( $query_args, $permalink ), 'toggle-subscription_' . $forum_id ) ); 
  48. $sub = $is_subscribed ? ' class="is-subscribed"' : ''; 
  49. $html = sprintf( '%s<span id="subscribe-%d" %s><a href="%s" class="subscription-toggle" data-forum="%d">%s</a></span>%s', $r['before'], $forum_id, $sub, $url, $forum_id, $text, $r['after'] ); 
  50.  
  51. // Initial output is wrapped in a span, ajax output is hooked to this 
  52. if ( !empty( $wrap ) ) { 
  53. $html = '<span id="subscription-toggle">' . $html . '</span>'; 
  54.  
  55. } else { 
  56.  
  57. // Decide which link to show 
  58. $is_subscribed = bbp_is_user_subscribed_to_topic( $user_id, $topic_id ); 
  59. if ( ! empty( $is_subscribed ) ) { 
  60. $text = $r['unsubscribe']; 
  61. $query_args = array( 'action' => 'bbp_unsubscribe', 'topic_id' => $topic_id ); 
  62. } else { 
  63. $text = $r['subscribe']; 
  64. $query_args = array( 'action' => 'bbp_subscribe', 'topic_id' => $topic_id ); 
  65.  
  66. // Create the link based where the user is and if the user is 
  67. // subscribed already 
  68. if ( bbp_is_subscriptions() ) { 
  69. $permalink = bbp_get_subscriptions_permalink( $user_id ); 
  70. } elseif ( bbp_is_single_topic() || bbp_is_single_reply() ) { 
  71. $permalink = bbp_get_topic_permalink( $topic_id ); 
  72. } else { 
  73. $permalink = get_permalink(); 
  74.  
  75. $url = esc_url( wp_nonce_url( add_query_arg( $query_args, $permalink ), 'toggle-subscription_' . $topic_id ) ); 
  76. $sub = $is_subscribed ? ' class="is-subscribed"' : ''; 
  77. $html = sprintf( '%s<span id="subscribe-%d" %s><a href="%s" class="subscription-toggle" data-topic="%d">%s</a></span>%s', $r['before'], $topic_id, $sub, $url, $topic_id, $text, $r['after'] ); 
  78.  
  79. // Initial output is wrapped in a span, ajax output is hooked to this 
  80. if ( !empty( $wrap ) ) { 
  81. $html = '<span id="subscription-toggle">' . $html . '</span>'; 
  82.  
  83. // Return the link 
  84. return apply_filters( 'bbp_get_user_subscribe_link', $html, $r, $user_id, $topic_id );