bbp_move_topic_handler( int $topic_id, int $old_forum_id, int $new_forum_id )

Handle the moving of a topic from one forum to another. This includes walking up the old and new branches and updating the counts.

Description

Parameters

$topic_id

(Required) Topic id

$old_forum_id

(Required) Old forum id

$new_forum_id

(Required) New forum id

Source

File: bp-forums/topics/functions.php

1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
function bbp_move_topic_handler( $topic_id, $old_forum_id, $new_forum_id ) {
 
    // Validate parameters
    $topic_id     = bbp_get_topic_id( $topic_id     );
    $old_forum_id = bbp_get_forum_id( $old_forum_id );
    $new_forum_id = bbp_get_forum_id( $new_forum_id );
 
    // Update topic forum's ID
    bbp_update_topic_forum_id( $topic_id, $new_forum_id );
 
    /** Stickies **************************************************************/
 
    // Get forum stickies
    $old_stickies = bbp_get_stickies( $old_forum_id );
 
    // Only proceed if stickies are found
    if ( !empty( $old_stickies ) ) {
 
        // Define local variables
        $updated_stickies = array();
 
        // Loop through stickies of forum and add misses to the updated array
        foreach ( (array) $old_stickies as $sticky_topic_id ) {
            if ( $topic_id !== $sticky_topic_id ) {
                $updated_stickies[] = $sticky_topic_id;
            }
        }
 
        // If stickies are different, update or delete them
        if ( $updated_stickies !== $old_stickies ) {
 
            // No more stickies so delete the meta
            if ( empty( $updated_stickies ) ) {
                delete_post_meta( $old_forum_id, '_bbp_sticky_topics' );
 
            // Still stickies so update the meta
            } else {
                update_post_meta( $old_forum_id, '_bbp_sticky_topics', $updated_stickies );
            }
 
            // Topic was sticky, so restick in new forum
            bbp_stick_topic( $topic_id );
        }
    }
 
    /** Topic Replies *********************************************************/
 
    // Get the topics replies
    $replies = bbp_get_all_child_ids( $topic_id, bbp_get_reply_post_type() );
 
    // Update the forum_id of all replies in the topic
    foreach ( $replies as $reply_id ) {
        bbp_update_reply_forum_id( $reply_id, $new_forum_id );
    }
 
    /** Old forum_id **********************************************************/
 
    // Get topic ancestors
    $old_forum_ancestors = array_values( array_unique( array_merge( array( $old_forum_id ), (array) get_post_ancestors( $old_forum_id ) ) ) );
 
    // Loop through ancestors and update them
    if ( !empty( $old_forum_ancestors ) ) {
        foreach ( $old_forum_ancestors as $ancestor ) {
            if ( bbp_is_forum( $ancestor ) ) {
                bbp_update_forum( array(
                    'forum_id' => $ancestor,
                ) );
            }
        }
    }
 
    /** New forum_id **********************************************************/
 
    // Make sure we're not walking twice
    if ( !in_array( $new_forum_id, $old_forum_ancestors ) ) {
 
        // Get topic ancestors
        $new_forum_ancestors = array_values( array_unique( array_merge( array( $new_forum_id ), (array) get_post_ancestors( $new_forum_id ) ) ) );
 
        // Make sure we're not walking twice
        $new_forum_ancestors = array_diff( $new_forum_ancestors, $old_forum_ancestors );
 
        // Loop through ancestors and update them
        if ( !empty( $new_forum_ancestors ) ) {
            foreach ( $new_forum_ancestors as $ancestor ) {
                if ( bbp_is_forum( $ancestor ) ) {
                    bbp_update_forum( array(
                        'forum_id' => $ancestor,
                    ) );
                }
            }
        }
    }
}

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.