bbp_spam_topic( int $topic_id )

Marks a topic as spam

Description

Parameters

$topic_id

(Required) Topic id

Return

(mixed) False or WP_Error on failure, topic id on success

Source

File: bp-forums/topics/functions.php

2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
function bbp_spam_topic( $topic_id = 0 ) {
 
    // Get the topic
    $topic = bbp_get_topic( $topic_id );
    if ( empty( $topic ) )
        return $topic;
 
    // Bail if topic is spam
    if ( bbp_get_spam_status_id() === $topic->post_status )
        return false;
 
    // Execute pre spam code
    do_action( 'bbp_spam_topic', $topic_id );
 
    /** Trash Replies *********************************************************/
 
    // Topic is being spammed, so its replies are trashed
    $replies = new WP_Query( array(
        'suppress_filters' => true,
        'post_type'        => bbp_get_reply_post_type(),
        'post_status'      => bbp_get_public_status_id(),
        'post_parent'      => $topic_id,
        'posts_per_page'   => -1,
        'nopaging'         => true,
        'fields'           => 'id=>parent'
    ) );
 
    if ( !empty( $replies->posts ) ) {
 
        // Prevent debug notices
        $pre_spammed_replies = array();
 
        // Loop through replies, trash them, and add them to array
        foreach ( $replies->posts as $reply ) {
            wp_trash_post( $reply->ID );
            $pre_spammed_replies[] = $reply->ID;
        }
 
        // Set a post_meta entry of the replies that were trashed by this action.
        // This is so we can possibly untrash them, without untrashing replies
        // that were purposefully trashed before.
        update_post_meta( $topic_id, '_bbp_pre_spammed_replies', $pre_spammed_replies );
 
        // Reset the $post global
        wp_reset_postdata();
    }
 
    // Cleanup
    unset( $replies );
 
    /** Topic Tags ************************************************************/
 
    // Add the original post status as post meta for future restoration
    add_post_meta( $topic_id, '_bbp_spam_meta_status', $topic->post_status );
 
    // Get topic tags
    $terms = get_the_terms( $topic_id, bbp_get_topic_tag_tax_id() );
 
    // Define local variable(s)
    $term_names = array();
 
    // Topic has tags
    if ( !empty( $terms ) ) {
 
        // Loop through and collect term names
        foreach ( $terms as $term ) {
            $term_names[] = $term->name;
        }
 
        // Topic terms have slugs
        if ( !empty( $term_names ) ) {
 
            // Add the original post status as post meta for future restoration
            add_post_meta( $topic_id, '_bbp_spam_topic_tags', $term_names );
 
            // Empty the topic of its tags
            $topic->tax_input = array( bbp_get_topic_tag_tax_id() => '' );
        }
    }
 
    // Set post status to spam
    $topic->post_status = bbp_get_spam_status_id();
 
    // No revisions
    remove_action( 'pre_post_update', 'wp_save_post_revision' );
 
    // Update the topic
    $topic_id = wp_update_post( $topic );
 
    // Execute post spam code
    do_action( 'bbp_spammed_topic', $topic_id );
 
    // Return topic_id
    return $topic_id;
}

Changelog

Changelog
Version Description
bbPress (r2740) Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.