bbp_admin_repair_freshness()

Recaches the last post in every topic and forum

Description

Return

(array) An array of the status code and the message

Source

File: bp-forums/admin/tools.php

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
function bbp_admin_repair_freshness() {
    global $wpdb;
 
    $statement = __( 'Recomputing latest post in every discussion and forum… %s', 'buddyboss' );
    $result    = __( 'Failed!', 'buddyboss' );
 
    // First, delete everything.
    if ( is_wp_error( $wpdb->query( "DELETE FROM `$wpdb->postmeta` WHERE `meta_key` IN ( '_bbp_last_reply_id', '_bbp_last_topic_id', '_bbp_last_active_id', '_bbp_last_active_time' );" ) ) )
        return array( 1, sprintf( $statement, $result ) );
 
    // Next, give all the topics with replies the ID their last reply.
    if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`)
            ( SELECT `topic`.`ID`, '_bbp_last_reply_id', MAX( `reply`.`ID` )
            FROM `$wpdb->posts` AS `topic` INNER JOIN `$wpdb->posts` AS `reply` ON `topic`.`ID` = `reply`.`post_parent`
            WHERE `reply`.`post_status` IN ( '" . bbp_get_public_status_id() . "' ) AND `topic`.`post_type` = 'topic' AND `reply`.`post_type` = 'reply'
            GROUP BY `topic`.`ID` );" ) ) )
        return array( 2, sprintf( $statement, $result ) );
 
    // For any remaining topics, give a reply ID of 0.
    if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`)
            ( SELECT `ID`, '_bbp_last_reply_id', 0
            FROM `$wpdb->posts` AS `topic` LEFT JOIN `$wpdb->postmeta` AS `reply`
            ON `topic`.`ID` = `reply`.`post_id` AND `reply`.`meta_key` = '_bbp_last_reply_id'
            WHERE `reply`.`meta_id` IS NULL AND `topic`.`post_type` = 'topic' );" ) ) )
        return array( 3, sprintf( $statement, $result ) );
 
    // Now we give all the forums with topics the ID their last topic.
    if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`)
            ( SELECT `forum`.`ID`, '_bbp_last_topic_id', `topic`.`ID`
            FROM `$wpdb->posts` AS `forum` INNER JOIN `$wpdb->posts` AS `topic` ON `forum`.`ID` = `topic`.`post_parent`
            WHERE `topic`.`post_status` IN ( '" . bbp_get_public_status_id() . "' ) AND `forum`.`post_type` = 'forum' AND `topic`.`post_type` = 'topic'
            GROUP BY `forum`.`ID` );" ) ) )
        return array( 4, sprintf( $statement, $result ) );
 
    // For any remaining forums, give a topic ID of 0.
    if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`)
            ( SELECT `ID`, '_bbp_last_topic_id', 0
            FROM `$wpdb->posts` AS `forum` LEFT JOIN `$wpdb->postmeta` AS `topic`
            ON `forum`.`ID` = `topic`.`post_id` AND `topic`.`meta_key` = '_bbp_last_topic_id'
            WHERE `topic`.`meta_id` IS NULL AND `forum`.`post_type` = 'forum' );" ) ) )
        return array( 5, sprintf( $statement, $result ) );
 
    // After that, we give all the topics with replies the ID their last reply (again, this time for a different reason).
    if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`)
            ( SELECT `topic`.`ID`, '_bbp_last_active_id', MAX( `reply`.`ID` )
            FROM `$wpdb->posts` AS `topic` INNER JOIN `$wpdb->posts` AS `reply` ON `topic`.`ID` = `reply`.`post_parent`
            WHERE `reply`.`post_status` IN ( '" . bbp_get_public_status_id() . "' ) AND `topic`.`post_type` = 'topic' AND `reply`.`post_type` = 'reply'
            GROUP BY `topic`.`ID` );" ) ) )
        return array( 6, sprintf( $statement, $result ) );
 
    // For any remaining topics, give a reply ID of themself.
    if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`)
            ( SELECT `ID`, '_bbp_last_active_id', `ID`
            FROM `$wpdb->posts` AS `topic` LEFT JOIN `$wpdb->postmeta` AS `reply`
            ON `topic`.`ID` = `reply`.`post_id` AND `reply`.`meta_key` = '_bbp_last_active_id'
            WHERE `reply`.`meta_id` IS NULL AND `topic`.`post_type` = 'topic' );" ) ) )
        return array( 7, sprintf( $statement, $result ) );
 
    // Give topics with replies their last update time.
    if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`)
            ( SELECT `topic`.`ID`, '_bbp_last_active_time', MAX( `reply`.`post_date` )
            FROM `$wpdb->posts` AS `topic` INNER JOIN `$wpdb->posts` AS `reply` ON `topic`.`ID` = `reply`.`post_parent`
            WHERE `reply`.`post_status` IN ( '" . bbp_get_public_status_id() . "' ) AND `topic`.`post_type` = 'topic' AND `reply`.`post_type` = 'reply'
            GROUP BY `topic`.`ID` );" ) ) )
        return array( 8, sprintf( $statement, $result ) );
 
    // Give topics without replies their last update time.
    if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`)
            ( SELECT `ID`, '_bbp_last_active_time', `post_date`
            FROM `$wpdb->posts` AS `topic` LEFT JOIN `$wpdb->postmeta` AS `reply`
            ON `topic`.`ID` = `reply`.`post_id` AND `reply`.`meta_key` = '_bbp_last_active_time'
            WHERE `reply`.`meta_id` IS NULL AND `topic`.`post_type` = 'topic' );" ) ) )
        return array( 9, sprintf( $statement, $result ) );
 
    // Forums need to know what their last active item is as well. Now it gets a bit more complex to do in the database.
    $forums = $wpdb->get_col( "SELECT `ID` FROM `$wpdb->posts` WHERE `post_type` = 'forum' and `post_status` != 'auto-draft';" );
    if ( is_wp_error( $forums ) )
        return array( 10, sprintf( $statement, $result ) );
 
    // Loop through forums
    foreach ( $forums as $forum_id ) {
        if ( !bbp_is_forum_category( $forum_id ) ) {
            bbp_update_forum( array( 'forum_id' => $forum_id ) );
        }
    }
 
    // Loop through categories when forums are done
    foreach ( $forums as $forum_id ) {
        if ( bbp_is_forum_category( $forum_id ) ) {
            bbp_update_forum( array( 'forum_id' => $forum_id ) );
        }
    }
 
    // Complete results
    return array( 0, sprintf( $statement, __( 'Complete!', 'buddyboss' ) ) );
}

Changelog

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