bbp_spam_topic

Marks a topic as spam.

Description

bbp_spam_topic( (int) $topic_id = 0 ); 

Parameters (1)

0. $topic_id — Optional. (int)
The topic id.

Usage

  1. if ( !function_exists( 'bbp_spam_topic' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'bbpress/includes/topics/functions.php'; 
  3.  
  4. // The topic id. 
  5. $topic_id = -1; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = bbp_spam_topic($topic_id); 
  9.  

Defined (1)

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

/includes/topics/functions.php  
  1. function bbp_spam_topic( $topic_id = 0 ) { 
  2.  
  3. // Get the topic 
  4. $topic = bbp_get_topic( $topic_id ); 
  5. if ( empty( $topic ) ) 
  6. return $topic; 
  7.  
  8. // Bail if topic is spam 
  9. if ( bbp_get_spam_status_id() === $topic->post_status ) 
  10. return false; 
  11.  
  12. // Execute pre spam code 
  13. do_action( 'bbp_spam_topic', $topic_id ); 
  14.  
  15. /** Trash Replies *********************************************************/ 
  16.  
  17. // Topic is being spammed, so its replies are trashed 
  18. $replies = new WP_Query( array( 
  19. 'suppress_filters' => true,  
  20. 'post_type' => bbp_get_reply_post_type(),  
  21. 'post_status' => bbp_get_public_status_id(),  
  22. 'post_parent' => $topic_id,  
  23. 'posts_per_page' => -1,  
  24. 'nopaging' => true,  
  25. 'fields' => 'id=>parent' 
  26. ) ); 
  27.  
  28. if ( !empty( $replies->posts ) ) { 
  29.  
  30. // Prevent debug notices 
  31. $pre_spammed_replies = array(); 
  32.  
  33. // Loop through replies, trash them, and add them to array 
  34. foreach ( $replies->posts as $reply ) { 
  35. wp_trash_post( $reply->ID ); 
  36. $pre_spammed_replies[] = $reply->ID; 
  37.  
  38. // Set a post_meta entry of the replies that were trashed by this action. 
  39. // This is so we can possibly untrash them, without untrashing replies 
  40. // that were purposefully trashed before. 
  41. update_post_meta( $topic_id, '_bbp_pre_spammed_replies', $pre_spammed_replies ); 
  42.  
  43. // Reset the $post global 
  44. wp_reset_postdata(); 
  45.  
  46. // Cleanup 
  47. unset( $replies ); 
  48.  
  49. /** Topic Tags ************************************************************/ 
  50.  
  51. // Add the original post status as post meta for future restoration 
  52. add_post_meta( $topic_id, '_bbp_spam_meta_status', $topic->post_status ); 
  53.  
  54. // Get topic tags 
  55. $terms = get_the_terms( $topic_id, bbp_get_topic_tag_tax_id() ); 
  56.  
  57. // Define local variable(s) 
  58. $term_names = array(); 
  59.  
  60. // Topic has tags 
  61. if ( !empty( $terms ) ) { 
  62.  
  63. // Loop through and collect term names 
  64. foreach ( $terms as $term ) { 
  65. $term_names[] = $term->name; 
  66.  
  67. // Topic terms have slugs 
  68. if ( !empty( $term_names ) ) { 
  69.  
  70. // Add the original post status as post meta for future restoration 
  71. add_post_meta( $topic_id, '_bbp_spam_topic_tags', $term_names ); 
  72.  
  73. // Empty the topic of its tags 
  74. $topic->tax_input = array( bbp_get_topic_tag_tax_id() => '' ); 
  75.  
  76. // Set post status to spam 
  77. $topic->post_status = bbp_get_spam_status_id(); 
  78.  
  79. // No revisions 
  80. remove_action( 'pre_post_update', 'wp_save_post_revision' ); 
  81.  
  82. // Update the topic 
  83. $topic_id = wp_update_post( $topic ); 
  84.  
  85. // Execute post spam code 
  86. do_action( 'bbp_spammed_topic', $topic_id ); 
  87.  
  88. // Return topic_id 
  89. return $topic_id;