bbp_favorites_handler

Handles the front end adding and removing of favorite topics.

Description

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

Parameters (1)

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

Usage

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

Defined (1)

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

/includes/users/functions.php  
  1. function bbp_favorites_handler( $action = '' ) { 
  2.  
  3. if ( !bbp_is_favorites_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_favorite_add',  
  13. 'bbp_favorite_remove',  
  14. ); 
  15.  
  16. // Bail if actions aren't meant for this function 
  17. if ( !in_array( $action, $possible_actions ) ) 
  18. return; 
  19.  
  20. // What action is taking place? 
  21. $topic_id = intval( $_GET['topic_id'] ); 
  22. $user_id = bbp_get_user_id( 0, true, true ); 
  23.  
  24. // Check for empty topic 
  25. if ( empty( $topic_id ) ) { 
  26. bbp_add_error( 'bbp_favorite_topic_id', __( '<strong>ERROR</strong>: No topic was found! Which topic are you marking/unmarking as favorite?', bbpress ) ); 
  27.  
  28. // Check nonce 
  29. } elseif ( ! bbp_verify_nonce_request( 'toggle-favorite_' . $topic_id ) ) { 
  30. bbp_add_error( 'bbp_favorite_nonce', __( '<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_favorite_permissions', __( '<strong>ERROR</strong>: You don\'t have the permission to edit favorites of that user!', bbpress ) ); 
  35.  
  36. // Bail if errors 
  37. if ( bbp_has_errors() ) 
  38. return; 
  39.  
  40. /** No errors *************************************************************/ 
  41.  
  42. $is_favorite = bbp_is_user_favorite( $user_id, $topic_id ); 
  43. $success = false; 
  44.  
  45. if ( true === $is_favorite && 'bbp_favorite_remove' === $action ) 
  46. $success = bbp_remove_user_favorite( $user_id, $topic_id ); 
  47. elseif ( false === $is_favorite && 'bbp_favorite_add' === $action ) 
  48. $success = bbp_add_user_favorite( $user_id, $topic_id ); 
  49.  
  50. // Do additional favorites actions 
  51. do_action( 'bbp_favorites_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_favorites() ) { 
  58. $redirect = bbp_get_favorites_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_favorite && 'bbp_favorite_remove' === $action ) { 
  75. bbp_add_error( 'bbp_favorite_remove', __( '<strong>ERROR</strong>: There was a problem removing that topic from favorites!', bbpress ) ); 
  76. } elseif ( false === $is_favorite && 'bbp_favorite_add' === $action ) { 
  77. bbp_add_error( 'bbp_favorite_add', __( '<strong>ERROR</strong>: There was a problem favoriting that topic!', bbpress ) );