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
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
| 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.