bbp_merge_topic_handler( string $action = '' )

Merge topic handler

Description

Handles the front end merge topic submission

Parameters

$action

(Optional) The requested action to compare this function to

Default value: ''

Source

File: bp-forums/topics/functions.php

1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
function bbp_merge_topic_handler( $action = '' ) {
 
    // Bail if action is not bbp-merge-topic
    if ( 'bbp-merge-topic' !== $action )
        return;
 
    // Define local variable(s)
    $source_topic_id = $destination_topic_id = 0;
    $source_topic = $destination_topic = 0;
    $subscribers = $favoriters = $replies = array();
 
    /** Source Topic **********************************************************/
 
    // Topic id
    if ( empty( $_POST['bbp_topic_id'] ) ) {
        bbp_add_error( 'bbp_merge_topic_source_id', __( '<strong>ERROR</strong>: Discussion ID not found.', 'buddyboss' ) );
    } else {
        $source_topic_id = (int) $_POST['bbp_topic_id'];
    }
 
    // Nonce check
    if ( ! bbp_verify_nonce_request( 'bbp-merge-topic_' . $source_topic_id ) ) {
        bbp_add_error( 'bbp_merge_topic_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'buddyboss' ) );
        return;
 
    // Source topic not found
    } elseif ( !$source_topic = bbp_get_topic( $source_topic_id ) ) {
        bbp_add_error( 'bbp_merge_topic_source_not_found', __( '<strong>ERROR</strong>: The discussion you want to merge was not found.', 'buddyboss' ) );
        return;
    }
 
    // Cannot edit source topic
    if ( !current_user_can( 'edit_topic', $source_topic->ID ) ) {
        bbp_add_error( 'bbp_merge_topic_source_permission', __( '<strong>ERROR</strong>: You do not have the permissions to edit the source discussion.', 'buddyboss' ) );
        return;
    }
 
    /** Destination Topic *****************************************************/
 
    // Topic id
    if ( empty( $_POST['bbp_destination_topic'] ) )
        bbp_add_error( 'bbp_merge_topic_destination_id', __( '<strong>ERROR</strong>: Destination discussion ID not found.', 'buddyboss' ) );
    else
        $destination_topic_id = (int) $_POST['bbp_destination_topic'];
 
    // Destination topic not found
    if ( !$destination_topic = bbp_get_topic( $destination_topic_id ) )
        bbp_add_error( 'bbp_merge_topic_destination_not_found', __( '<strong>ERROR</strong>: The discussion you want to merge to was not found.', 'buddyboss' ) );
 
    // Cannot edit destination topic
    if ( !current_user_can( 'edit_topic', $destination_topic->ID ) )
        bbp_add_error( 'bbp_merge_topic_destination_permission', __( '<strong>ERROR</strong>: You do not have the permissions to edit the destination discussion.', 'buddyboss' ) );
 
    // Bail if errors
    if ( bbp_has_errors() )
        return;
 
    /** No Errors *************************************************************/
 
    // Update counts, etc...
    do_action( 'bbp_merge_topic', $destination_topic->ID, $source_topic->ID );
 
    /** Date Check ************************************************************/
 
    // Check if the destination topic is older than the source topic
    if ( strtotime( $source_topic->post_date ) < strtotime( $destination_topic->post_date ) ) {
 
        // Set destination topic post_date to 1 second before source topic
        $destination_post_date = date( 'Y-m-d H:i:s', strtotime( $source_topic->post_date ) - 1 );
 
        // Update destination topic
        wp_update_post( array(
            'ID'            => $destination_topic_id,
            'post_date'     => $destination_post_date,
            'post_date_gmt' => get_gmt_from_date( $destination_post_date )
        ) );
    }
 
    /** Subscriptions *********************************************************/
 
    // Get subscribers from source topic
    $subscribers = bbp_get_topic_subscribers( $source_topic->ID );
 
    // Remove the topic from everybody's subscriptions
    if ( !empty( $subscribers ) ) {
 
        // Loop through each user
        foreach ( (array) $subscribers as $subscriber ) {
 
            // Shift the subscriber if told to
            if ( !empty( $_POST['bbp_topic_subscribers'] ) && ( "1" === $_POST['bbp_topic_subscribers'] ) && bbp_is_subscriptions_active() )
                bbp_add_user_subscription( $subscriber, $destination_topic->ID );
 
            // Remove old subscription
            bbp_remove_user_subscription( $subscriber, $source_topic->ID );
        }
    }
 
    /** Favorites *************************************************************/
 
    // Get favoriters from source topic
    $favoriters = bbp_get_topic_favoriters( $source_topic->ID );
 
    // Remove the topic from everybody's favorites
    if ( !empty( $favoriters ) ) {
 
        // Loop through each user
        foreach ( (array) $favoriters as $favoriter ) {
 
            // Shift the favoriter if told to
            if ( !empty( $_POST['bbp_topic_favoriters'] ) && "1" === $_POST['bbp_topic_favoriters'] )
                bbp_add_user_favorite( $favoriter, $destination_topic->ID );
 
            // Remove old favorite
            bbp_remove_user_favorite( $favoriter, $source_topic->ID );
        }
    }
 
    /** Tags ******************************************************************/
 
    // Get the source topic tags
    $source_topic_tags = wp_get_post_terms( $source_topic->ID, bbp_get_topic_tag_tax_id(), array( 'fields' => 'names' ) );
 
    // Tags to possibly merge
    if ( !empty( $source_topic_tags ) && !is_wp_error( $source_topic_tags ) ) {
 
        // Shift the tags if told to
        if ( !empty( $_POST['bbp_topic_tags'] ) && ( "1" === $_POST['bbp_topic_tags'] ) )
            wp_set_post_terms( $destination_topic->ID, $source_topic_tags, bbp_get_topic_tag_tax_id(), true );
 
        // Delete the tags from the source topic
        wp_delete_object_term_relationships( $source_topic->ID, bbp_get_topic_tag_tax_id() );
    }
 
    /** Source Topic **********************************************************/
 
    // Status
    bbp_open_topic( $source_topic->ID );
 
    // Sticky
    bbp_unstick_topic( $source_topic->ID );
 
    // Get the replies of the source topic
    $replies = (array) get_posts( array(
        'post_parent'    => $source_topic->ID,
        'post_type'      => bbp_get_reply_post_type(),
        'posts_per_page' => -1,
        'order'          => 'ASC'
    ) );
 
    // Prepend the source topic to its replies array for processing
    array_unshift( $replies, $source_topic );
 
    if ( !empty( $replies ) ) {
 
        /** Merge Replies *****************************************************/
 
        // Change the post_parent of each reply to the destination topic id
        foreach ( $replies as $reply ) {
 
            // Update the reply
            wp_update_post( array(
                'ID'          => $reply->ID,
                'post_title'  => sprintf( __( 'Reply To: %s', 'buddyboss' ), $destination_topic->post_title ),
                'post_name'   => false,
                'post_type'   => bbp_get_reply_post_type(),
                'post_parent' => $destination_topic->ID,
                'guid'        => ''
            ) );
 
            // Adjust reply meta values
            bbp_update_reply_topic_id( $reply->ID, $destination_topic->ID                           );
            bbp_update_reply_forum_id( $reply->ID, bbp_get_topic_forum_id( $destination_topic->ID ) );
 
            // Adjust reply to values
            $reply_to = bbp_get_reply_to( $reply->ID );
            if ( empty( $reply_to ) ) {
                bbp_update_reply_to( $reply->ID, $source_topic->ID );
            }
 
            // Do additional actions per merged reply
            do_action( 'bbp_merged_topic_reply', $reply->ID, $destination_topic->ID );
        }
    }
 
    /** Successful Merge ******************************************************/
 
    // Update topic's last meta data
    bbp_update_topic_last_reply_id   ( $destination_topic->ID );
    bbp_update_topic_last_active_id  ( $destination_topic->ID );
    bbp_update_topic_last_active_time( $destination_topic->ID );
 
    // Send the post parent of the source topic as it has been shifted
    // (possibly to a new forum) so we need to update the counts of the
    // old forum as well as the new one
    do_action( 'bbp_merged_topic', $destination_topic->ID, $source_topic->ID, $source_topic->post_parent );
 
    // Redirect back to new topic
    wp_safe_redirect( bbp_get_topic_permalink( $destination_topic->ID ) );
 
    // For good measure
    exit();
}

Changelog

Changelog
Version Description
bbPress (r2756) 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.