Thanks __ck__. I've made some amendments to the plugin, and it seems to still run (result!). The amends limit the parts of the cache recreated when posts are changed, it also prompts the admin to create a whole cache when the plugin is installed (message in the admin area) and this prompt will reappear if the cache ever appears inadequate (only basic checks, and only done in the admin area).
I've included the amends below in case they are useful to you:
<?php
/*
Plugin Name: Forum Last Poster
Description: Adds <code>forum_last_poster()</code>, <code>forum_time()</code>, <code>forum_last_post_link()</code> and other functions to bbPress to mimic the topic tables' FRESHNESS column. Requires simple template edits.
Plugin URI: http://bbpress.org/plugins/topic/forum-last-poster
Author: _ck_
Author URI: http://bbShowcase.org
Version: 0.0.4
*/
function forum_last_poster($id=0) {topic_last_poster(forum_last_topic_id($id));}
function get_forum_last_poster($id=0) {return get_topic_last_poster(forum_last_topic_id($id));}
function forum_last_poster_link( $id = 0 ) { topic_last_poster_link( forum_last_topic_id( $id ) ); }
function forum_time($id=0) {topic_time(forum_last_topic_id($id));}
function get_forum_time($id=0) {return get_topic_time(forum_last_topic_id($id));}
function get_forum_timestamp($id=0) {return get_topic_time( array( 'format' => 'U', 'id' => $id ) );}
function forum_last_post_link($id=0) {topic_last_post_link(forum_last_topic_id($id));}
function get_forum_last_post_link($id=0) {return get_topic_last_post_link(forum_last_topic_id($id));}
add_action( 'bb_new_post', 'forum_last_poster_new');
add_action( 'bb_delete_post','forum_last_poster_deleted');
// I suppose both functions could just be replaced with the one below,
// but this one is marginally faster and as it is the most common case
// it seems worth the optimisation vs code duplication/growth.
function forum_last_poster_new( $post_id = 0 )
{
// We can get all the info we need from the post object as
// we assume it (having just been posted) is the most recent.
$post = bb_get_post( $post_id );
$forum_last_poster = bb_get_option( 'forum_last_poster' );
$forum_last_poster[ $post->forum_id ] = $post->topic_id;
bb_update_option('forum_last_poster',$forum_last_poster);
}
function forum_last_poster_deleted( $post_id = 0 )
{
global $bbdb;
$post = bb_get_post( $post_id );
// We might have just deleted the latest post in this forum, so query DB to
// return the posted to topic in this forum
$sql = " SELECT topic_id FROM $bbdb->topics WHERE forum_id = %d AND topic_last_post_id IN ( SELECT MAX( topic_last_post_id ) FROM $bbdb->topics WHERE topic_status=0 GROUP BY forum_id ) ";
$prepared_sql = $bbdb->prepare( $sql, $post->forum_id );
$latest = $bbdb->get_var( $prepared_sql );
// Now update the array
$forum_last_poster = bb_get_option( 'forum_last_poster' );
$forum_last_poster[ $post->forum_id ] = $latest->topic_id;
bb_update_option('forum_last_poster',$forum_last_poster);
}
function forum_last_posters_recalc()
{
global $bbdb;
$sql = " SELECT forum_id, topic_id FROM $bbdb->topics ";
$sql .= " WHERE topic_last_post_id IN (SELECT MAX(topic_last_post_id) FROM $bbdb->topics WHERE topic_status=0 GROUP BY forum_id) ";
// On this occasion there's no need to prepare the SQL as there's no user input
$last_topics = $bbdb->get_results( $sql );
foreach ($last_topics as $last_topic) {$forum_last_poster[$last_topic->forum_id]=$last_topic->topic_id;}
bb_update_option('forum_last_poster',$forum_last_poster);
}
function forum_last_topic_id($id = 0) {
global $forums_last_topic_id, $forum, $bb_topic_cache, $wp_object_cache;
if (!$id) {$id=$forum->forum_id;}
if (isset($forums_last_topic_id)) { return $forums_last_topic_id[$id]; }
$last_topics=bb_get_option('forum_last_poster');
if (empty($last_topics)) { return; // Shouldn't happen if the admin creates the cache as instructed }
foreach ($last_topics as $temp=>$id) {
$forums_last_topic_id[$temp]=$id;
if (!isset($bb_topic_cache[$id]) && !isset($wp_object_cache->cache['bb_topic'][$id])) {$add_cache[]->topic_id=$id;}
}
if (!empty($add_cache)) {bb_cache_post_topics($add_cache);} // cache topics not already in cache
return $forums_last_topic_id[$id];
}
add_action( 'bb_admin-header.php', 'forum_last_poster_check' );
function forum_last_poster_check()
{
// OK. Are we being asked to recalc the cache?
$recalc = (bool) @ $_GET[ 'forum_last_posters_recalc' ];
if ( $recalc ) {
// Are we authorised to recalc the cache?
bb_check_admin_referer( 'forum_last_posters_recalc' );
// Everything seems in order, GO GO GO
forum_last_posters_recalc();
// Redirect with a note
global $bb;
$args = array( 'forum_last_posters_recalced' => 1 );
$url = add_query_arg( $args, trailingslashit( $bb->uri ) . 'bb-admin/' );
bb_safe_redirect( $url );
exit;
}
// So maybe we've just recalced the cache?
$done_recalc = (bool) @ $_GET[ 'forum_last_posters_recalced' ];
if ( $done_recalc ) {
// Let the user know everything is OK, hopefully they will sleep better
return forum_last_poster_recached_note();
}
// Does the cache exist?
$cache = bb_get_option( 'forum_last_poster' );
if ( ! $cache ) return forum_last_poster_cache_note();
global $bbdb;
$sql = " SELECT COUNT( forum_id ) FROM $bbdb->forums ";
// Again, on this occasion there's no need to prepare the SQL as there's no user input
$num_forums = $bbdb->get_var( $sql );
// Check there's the right number of forums in the cache
if ( count( $cache ) != $num_forums ) return forum_last_poster_cache_note();
}
function forum_last_poster_cache_note()
{
global $bb;
$args = array( 'forum_last_posters_recalc' => 1 );
$url = add_query_arg( $args, trailingslashit( $bb->uri ) . 'bb-admin/' );
$url = bb_nonce_url( $url, 'forum_last_posters_recalc' );
$message = " For the Forum Last Poster plugin to work a cache needs to be created. Cache creation may take a little while (particularly with large forums). <a href='$url' style='-moz-border-radius:10px; -khtml-border-radius: 10px; -webkit-border-radius: 10px; border-radius: 10px; background:#F2F2F2 url(images/white-grad.png) repeat-x scroll 0 0; border:1px solid #BBBBBB; color:#464646; cursor:pointer; font-size:1em; font-weight:normal; line-height:1.2em; padding:4px 10px;'>Create Cache</a> ";
bb_admin_notice( $message, 'updated' );
return true;
}
function forum_last_poster_recached_note()
{
$message = " Thanks again for using the Forum Last Poster plugin. Everything went well creating the cache, and you shouldn't see this notice again. Bye! ";
bb_admin_notice( $message, 'updated' );
}
?>