bbp_forum_subscriptions_handler

Handles the front end subscribing and unsubscribing forums.

Description

bbp_forum_subscriptions_handler( (string) $action = '' ); 

Parameters (1)

0. $action — Optional. (string) => ''
The requested action to compare this function to

Usage

  1. if ( !function_exists( 'bbp_forum_subscriptions_handler' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'bbpress/includes/users/functions.php'; 
  3.  
  4. // The requested action to compare this function to 
  5. $action = ''; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = bbp_forum_subscriptions_handler($action); 
  9.  

Defined (1)

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

/includes/users/functions.php  
  1. function bbp_forum_subscriptions_handler( $action = '' ) { 
  2.  
  3. return false; 
  4.  
  5. // Bail if no forum ID is passed 
  6. if ( empty( $_GET['forum_id'] ) ) { 
  7. return; 
  8.  
  9. // Setup possible get actions 
  10. $possible_actions = array( 
  11. 'bbp_subscribe',  
  12. 'bbp_unsubscribe',  
  13. ); 
  14.  
  15. // Bail if actions aren't meant for this function 
  16. if ( ! in_array( $action, $possible_actions ) ) { 
  17. return; 
  18.  
  19. // Get required data 
  20. $user_id = bbp_get_user_id( 0, true, true ); 
  21. $forum_id = intval( $_GET['forum_id'] ); 
  22.  
  23. // Check for empty forum 
  24. if ( empty( $forum_id ) ) { 
  25. bbp_add_error( 'bbp_subscription_forum_id', __( '<strong>ERROR</strong>: No forum was found! Which forum are you subscribing/unsubscribing to?', bbpress ) ); 
  26.  
  27. // Check nonce 
  28. } elseif ( ! bbp_verify_nonce_request( 'toggle-subscription_' . $forum_id ) ) { 
  29. bbp_add_error( 'bbp_subscription_forum_id', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', bbpress ) ); 
  30.  
  31. // Check current user's ability to edit the user 
  32. } elseif ( !current_user_can( 'edit_user', $user_id ) ) { 
  33. bbp_add_error( 'bbp_subscription_permissions', __( '<strong>ERROR</strong>: You don\'t have the permission to edit favorites of that user!', bbpress ) ); 
  34.  
  35. // Bail if we have errors 
  36. if ( bbp_has_errors() ) { 
  37. return; 
  38.  
  39. /** No errors *************************************************************/ 
  40.  
  41. $is_subscription = bbp_is_user_subscribed( $user_id, $forum_id ); 
  42. $success = false; 
  43.  
  44. if ( true === $is_subscription && 'bbp_unsubscribe' === $action ) { 
  45. $success = bbp_remove_user_subscription( $user_id, $forum_id ); 
  46. } elseif ( false === $is_subscription && 'bbp_subscribe' === $action ) { 
  47. $success = bbp_add_user_subscription( $user_id, $forum_id ); 
  48.  
  49. // Do additional subscriptions actions 
  50. do_action( 'bbp_subscriptions_handler', $success, $user_id, $forum_id, $action ); 
  51.  
  52. // Success! 
  53. if ( true === $success ) { 
  54.  
  55. // Redirect back from whence we came 
  56. if ( bbp_is_subscriptions() ) { 
  57. $redirect = bbp_get_subscriptions_permalink( $user_id ); 
  58. } elseif ( bbp_is_single_user() ) { 
  59. $redirect = bbp_get_user_profile_url(); 
  60. } elseif ( is_singular( bbp_get_forum_post_type() ) ) { 
  61. $redirect = bbp_get_forum_permalink( $forum_id ); 
  62. } elseif ( is_single() || is_page() ) { 
  63. $redirect = get_permalink(); 
  64. } else { 
  65. $redirect = get_permalink( $forum_id ); 
  66.  
  67. wp_safe_redirect( $redirect ); 
  68.  
  69. // For good measure 
  70. exit(); 
  71.  
  72. // Fail! Handle errors 
  73. } elseif ( true === $is_subscription && 'bbp_unsubscribe' === $action ) { 
  74. bbp_add_error( 'bbp_unsubscribe', __( '<strong>ERROR</strong>: There was a problem unsubscribing from that forum!', bbpress ) ); 
  75. } elseif ( false === $is_subscription && 'bbp_subscribe' === $action ) { 
  76. bbp_add_error( 'bbp_subscribe', __( '<strong>ERROR</strong>: There was a problem subscribing to that forum!', bbpress ) );