bbp_toggle_reply_handler

Handles the front end spamming/unspamming and trashing/untrashing/deleting of replies.

Description

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

Parameters (1)

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

Usage

  1. if ( !function_exists( 'bbp_toggle_reply_handler' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'bbpress/includes/replies/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_toggle_reply_handler($action); 
  9.  

Defined (1)

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

/includes/replies/functions.php  
  1. function bbp_toggle_reply_handler( $action = '' ) { 
  2.  
  3. // Bail if required GET actions aren't passed 
  4. if ( empty( $_GET['reply_id'] ) ) 
  5. return; 
  6.  
  7. // Setup possible get actions 
  8. $possible_actions = array( 
  9. 'bbp_toggle_reply_spam',  
  10. 'bbp_toggle_reply_trash' 
  11. ); 
  12.  
  13. // Bail if actions aren't meant for this function 
  14. if ( !in_array( $action, $possible_actions ) ) 
  15. return; 
  16.  
  17. $failure = ''; // Empty failure string 
  18. $view_all = false; // Assume not viewing all 
  19. $reply_id = (int) $_GET['reply_id']; // What's the reply id? 
  20. $success = false; // Flag 
  21. $post_data = array( 'ID' => $reply_id ); // Prelim array 
  22.  
  23. // Make sure reply exists 
  24. $reply = bbp_get_reply( $reply_id ); 
  25. if ( empty( $reply ) ) 
  26. return; 
  27.  
  28. // What is the user doing here? 
  29. if ( !current_user_can( 'edit_reply', $reply->ID ) || ( 'bbp_toggle_reply_trash' === $action && !current_user_can( 'delete_reply', $reply->ID ) ) ) { 
  30. bbp_add_error( 'bbp_toggle_reply_permission', __( '<strong>ERROR:</strong> You do not have the permission to do that!', bbpress ) ); 
  31. return; 
  32.  
  33. // What action are we trying to perform? 
  34. switch ( $action ) { 
  35.  
  36. // Toggle spam 
  37. case 'bbp_toggle_reply_spam' : 
  38. check_ajax_referer( 'spam-reply_' . $reply_id ); 
  39.  
  40. $is_spam = bbp_is_reply_spam( $reply_id ); 
  41. $success = $is_spam ? bbp_unspam_reply( $reply_id ) : bbp_spam_reply( $reply_id ); 
  42. $failure = $is_spam ? __( '<strong>ERROR</strong>: There was a problem unmarking the reply as spam!', bbpress ) : __( '<strong>ERROR</strong>: There was a problem marking the reply as spam!', bbpress ); 
  43. $view_all = !$is_spam; 
  44.  
  45. break; 
  46.  
  47. // Toggle trash 
  48. case 'bbp_toggle_reply_trash' : 
  49.  
  50. $sub_action = in_array( $_GET['sub_action'], array( 'trash', 'untrash', 'delete' ) ) ? $_GET['sub_action'] : false; 
  51.  
  52. if ( empty( $sub_action ) ) 
  53. break; 
  54.  
  55. switch ( $sub_action ) { 
  56. case 'trash': 
  57. check_ajax_referer( 'trash-' . bbp_get_reply_post_type() . '_' . $reply_id ); 
  58.  
  59. $view_all = true; 
  60. $success = wp_trash_post( $reply_id ); 
  61. $failure = __( '<strong>ERROR</strong>: There was a problem trashing the reply!', bbpress ); 
  62.  
  63. break; 
  64.  
  65. case 'untrash': 
  66. check_ajax_referer( 'untrash-' . bbp_get_reply_post_type() . '_' . $reply_id ); 
  67.  
  68. $success = wp_untrash_post( $reply_id ); 
  69. $failure = __( '<strong>ERROR</strong>: There was a problem untrashing the reply!', bbpress ); 
  70.  
  71. break; 
  72.  
  73. case 'delete': 
  74. check_ajax_referer( 'delete-' . bbp_get_reply_post_type() . '_' . $reply_id ); 
  75.  
  76. $success = wp_delete_post( $reply_id ); 
  77. $failure = __( '<strong>ERROR</strong>: There was a problem deleting the reply!', bbpress ); 
  78.  
  79. break; 
  80.  
  81. break; 
  82.  
  83. // Do additional reply toggle actions 
  84. do_action( 'bbp_toggle_reply_handler', $success, $post_data, $action ); 
  85.  
  86. // No errors 
  87. if ( ( false !== $success ) && !is_wp_error( $success ) ) { 
  88.  
  89. /** Redirect **********************************************************/ 
  90.  
  91. // Redirect to 
  92. $redirect_to = bbp_get_redirect_to(); 
  93.  
  94. // Get the reply URL 
  95. $reply_url = bbp_get_reply_url( $reply_id, $redirect_to ); 
  96.  
  97. // Add view all if needed 
  98. if ( !empty( $view_all ) ) 
  99. $reply_url = bbp_add_view_all( $reply_url, true ); 
  100.  
  101. // Redirect back to reply 
  102. wp_safe_redirect( $reply_url ); 
  103.  
  104. // For good measure 
  105. exit(); 
  106.  
  107. // Handle errors 
  108. } else { 
  109. bbp_add_error( 'bbp_toggle_reply', $failure );