bbp_toggle_topic_handler

Handles the front end opening/closing, spamming/unspamming, sticking/unsticking and trashing/untrashing/deleting of topics.

Description

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

Parameters (1)

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

Usage

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

Defined (1)

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

/includes/topics/functions.php  
  1. function bbp_toggle_topic_handler( $action = '' ) { 
  2.  
  3. // Bail if required GET actions aren't passed 
  4. if ( empty( $_GET['topic_id'] ) ) 
  5. return; 
  6.  
  7. // Setup possible get actions 
  8. $possible_actions = array( 
  9. 'bbp_toggle_topic_close',  
  10. 'bbp_toggle_topic_stick',  
  11. 'bbp_toggle_topic_spam',  
  12. 'bbp_toggle_topic_trash' 
  13. ); 
  14.  
  15. // Bail if actions aren't meant for this function 
  16. if ( !in_array( $action, $possible_actions ) ) 
  17. return; 
  18.  
  19. $failure = ''; // Empty failure string 
  20. $view_all = false; // Assume not viewing all 
  21. $topic_id = (int) $_GET['topic_id']; // What's the topic id? 
  22. $success = false; // Flag 
  23. $post_data = array( 'ID' => $topic_id ); // Prelim array 
  24. $redirect = ''; // Empty redirect URL 
  25.  
  26. // Make sure topic exists 
  27. $topic = bbp_get_topic( $topic_id ); 
  28. if ( empty( $topic ) ) 
  29. return; 
  30.  
  31. // What is the user doing here? 
  32. if ( !current_user_can( 'edit_topic', $topic->ID ) || ( 'bbp_toggle_topic_trash' === $action && !current_user_can( 'delete_topic', $topic->ID ) ) ) { 
  33. bbp_add_error( 'bbp_toggle_topic_permission', __( '<strong>ERROR:</strong> You do not have the permission to do that.', bbpress ) ); 
  34. return; 
  35.  
  36. // What action are we trying to perform? 
  37. switch ( $action ) { 
  38.  
  39. // Toggle open/close 
  40. case 'bbp_toggle_topic_close' : 
  41. check_ajax_referer( 'close-topic_' . $topic_id ); 
  42.  
  43. $is_open = bbp_is_topic_open( $topic_id ); 
  44. $success = true === $is_open ? bbp_close_topic( $topic_id ) : bbp_open_topic( $topic_id ); 
  45. $failure = true === $is_open ? __( '<strong>ERROR</strong>: There was a problem closing the topic.', bbpress ) : __( '<strong>ERROR</strong>: There was a problem opening the topic.', bbpress ); 
  46.  
  47. break; 
  48.  
  49. // Toggle sticky/super-sticky/unstick 
  50. case 'bbp_toggle_topic_stick' : 
  51. check_ajax_referer( 'stick-topic_' . $topic_id ); 
  52.  
  53. $is_sticky = bbp_is_topic_sticky( $topic_id ); 
  54. $is_super = false === $is_sticky && !empty( $_GET['super'] ) && ( "1" === $_GET['super'] ) ? true : false; 
  55. $success = true === $is_sticky ? bbp_unstick_topic( $topic_id ) : bbp_stick_topic( $topic_id, $is_super ); 
  56. $failure = true === $is_sticky ? __( '<strong>ERROR</strong>: There was a problem unsticking the topic.', bbpress ) : __( '<strong>ERROR</strong>: There was a problem sticking the topic.', bbpress ); 
  57.  
  58. break; 
  59.  
  60. // Toggle spam 
  61. case 'bbp_toggle_topic_spam' : 
  62. check_ajax_referer( 'spam-topic_' . $topic_id ); 
  63.  
  64. $is_spam = bbp_is_topic_spam( $topic_id ); 
  65. $success = true === $is_spam ? bbp_unspam_topic( $topic_id ) : bbp_spam_topic( $topic_id ); 
  66. $failure = true === $is_spam ? __( '<strong>ERROR</strong>: There was a problem unmarking the topic as spam.', bbpress ) : __( '<strong>ERROR</strong>: There was a problem marking the topic as spam.', bbpress ); 
  67. $view_all = !$is_spam; 
  68.  
  69. break; 
  70.  
  71. // Toggle trash 
  72. case 'bbp_toggle_topic_trash' : 
  73.  
  74. $sub_action = !empty( $_GET['sub_action'] ) && in_array( $_GET['sub_action'], array( 'trash', 'untrash', 'delete' ) ) ? $_GET['sub_action'] : false; 
  75.  
  76. if ( empty( $sub_action ) ) 
  77. break; 
  78.  
  79. switch ( $sub_action ) { 
  80. case 'trash': 
  81. check_ajax_referer( 'trash-' . bbp_get_topic_post_type() . '_' . $topic_id ); 
  82.  
  83. $view_all = true; 
  84. $success = wp_trash_post( $topic_id ); 
  85. $failure = __( '<strong>ERROR</strong>: There was a problem trashing the topic.', bbpress ); 
  86.  
  87. break; 
  88.  
  89. case 'untrash': 
  90. check_ajax_referer( 'untrash-' . bbp_get_topic_post_type() . '_' . $topic_id ); 
  91.  
  92. $success = wp_untrash_post( $topic_id ); 
  93. $failure = __( '<strong>ERROR</strong>: There was a problem untrashing the topic.', bbpress ); 
  94.  
  95. break; 
  96.  
  97. case 'delete': 
  98. check_ajax_referer( 'delete-' . bbp_get_topic_post_type() . '_' . $topic_id ); 
  99.  
  100. $success = wp_delete_post( $topic_id ); 
  101. $failure = __( '<strong>ERROR</strong>: There was a problem deleting the topic.', bbpress ); 
  102.  
  103. break; 
  104.  
  105. break; 
  106.  
  107. // Do additional topic toggle actions 
  108. do_action( 'bbp_toggle_topic_handler', $success, $post_data, $action ); 
  109.  
  110. // No errors 
  111. if ( false !== $success && !is_wp_error( $success ) ) { 
  112.  
  113. // Redirect back to the topic's forum 
  114. if ( isset( $sub_action ) && ( 'delete' === $sub_action ) ) { 
  115. $redirect = bbp_get_forum_permalink( $success->post_parent ); 
  116.  
  117. // Redirect back to the topic 
  118. } else { 
  119.  
  120. // Get the redirect detination 
  121. $permalink = bbp_get_topic_permalink( $topic_id ); 
  122. $redirect = bbp_add_view_all( $permalink, $view_all ); 
  123.  
  124. wp_safe_redirect( $redirect ); 
  125.  
  126. // For good measure 
  127. exit(); 
  128.  
  129. // Handle errors 
  130. } else { 
  131. bbp_add_error( 'bbp_toggle_topic', $failure );