bbp_subscriptions_handler

Handles the front end subscribing and unsubscribing topics.

Description

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

Parameters (1)

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

Usage

  1. if ( !function_exists( 'bbp_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_subscriptions_handler($action); 
  9.  

Defined (1)

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

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