bbPress Plugin Browser »

Forum Last Poster (0.0.6)

Download

Version: 0.0.6

Last Updated: 2011-1-25

Requires bbPress Version: 0.8.2 or higher

Compatible up to: 0.9

Author Homepage »

Plugin Homepage »

Donate to this plugin »

Average Rating

5 stars
4 stars
3 stars
2 stars
1 star
(2)

Your Rating

Author: _ck_


  1. Thanks for reporting that simon. I think I slapped this together in a few hours so it definitely could use some optimization. bbPress 0.9 never had forum meta so I got lazy and did not store the result, doing it each time.

    The loop you are taking about is not that bad, it will only be one item per forum you have, so unless you have hundreds of forums, it will be fine. I think the problem is the query where the SELECT MAX because topic_last_post_id does not have an index if I recall correctly, I don't see why it would. So mysql probably has to scan the entire column, especially since the topic_status has to be checked as well. If you know how to do that (ie. via phpmyadmin) you might want to give that a try temporarily.

    A lazy, temporary workaround would probably be to have mysql make an index on topic_last_post_id in the topic table.

    Posted: 2 years ago #
  2. 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' );
    }
    
    ?>
    Posted: 2 years ago #
  3. I wish you had used a pastebin instead of deciding to post the entire plugin here.

    Apparently I forgot that I did indeed create caching of the results at some point.
    All I needed to do is further optimize it so it only updates on a per-forum basis once the entire dataset is created. I believe that can be done in two lines or so.

    (your admin code literally doubles the size of the plugin and is always loaded, yet not needed by 99% of the users 99% of the time, this is the #1 problem of both WP and bbPress plugins)

    Posted: 2 years ago #
  4. Thanks for your comments.

    The additional code runs only in the admin area, and performs pretty speedy checks to ensure it's needed. I figured this was a reasonable tradeoff. I did also consider creating the cache on plugin activation.

    Your point is a fair one, but I prefered to keep long operations outside the "public" area of the website.

    Thanks again.

    P.S. Pastebins are pretty transient, so I preferred to paste in here.

    Posted: 2 years ago #
  5. Hey!

    I tried using this plugin but for some reason when I activiate it, its freezing up the forums. It wont even let you visit the forums after its installed. I had to remove it. Granted there are posts on this forum that are 4 years old. So there are many, many topics. I am also using 0.9.0.4 with other plugins such as Avatar upload, bbsmilies, postcount/dynamic titles, simple online list, post notification, bb attachments and accurate post time.

    Posted: 2 years ago #
  6. circulartrend, it should work on your forum, the number of topics should not be a problem

    you are using 0.0.4 of the plugin downloaded from the top?

    The admin section freezes or just the regular forum pages?

    Are you running on a windows or linux server? What version of PHP/mysql ?

    I just posted 0.0.5 which I had sitting on my computer but I don't remember when I changed it. The changes really should not affect anything but give it a try anyway.

    Posted: 2 years ago #
  7. Thank you! That worked, it doesn't freeze anymore! :)

    Posted: 2 years ago #
  8. Actually,

    After hours of removing all plugins accept the ones by CK :) I finally got to the end where I only had one plugin not by CK...accept avatar upload. I tried to add this plugin and it locks everything up. I can't get to the boards or the admin menu. I upgraded from 0.9.0.4 to 0.9.0.6 too! I am not sure what the issue is.

    I am using avatar upload, bbsmilies, post count plus, members online, my views - started/participated topics, subscribe to topic, bbpress attachments, bbcode lite, bbcode buttons toolbar. All are CK accept avatar upload. Not sure what to do. I will be happy to give you any additional info you may need.

    Posted: 2 years ago #
  9. I have changed the query used by this plugin to get around a mysql bug and it's now over 100 times faster, which should help significantly on large forums.

    Posted: 2 years ago #
  10. _ck_, I want to display the exact time/date of the last posts rather than "1 day ago" or "2 months ago".. where's the part of your code should I change?

    I've successfully edited the Freshness on "Latest Discussion" with the exact time/date using this code

    <?php echo(topic_time('j M Y')); ?><br /><?php echo(topic_time('H:i')); ?>

    I tried using the same code to replace forum_time() but it didn't work obviously..

    thanks for the code..

    Posted: 2 years ago #
  11. IanKwok

    Member

    _ck_, I am using this plugin but my forum will have to undergo modification before any topic or post gets posted, however, your plugin will do a count of the topics and update the cache every time someone add a new post. In view of modification, how can I activate this plugin so that the count does not include that of the posts still being queued for modification?

    I am also using another plugin called bb-moderation-hold.

    Thanks

    Posted: 2 years ago #
  12. paamayim

    Member

    The last post link of the first discussione is empty, all other last posts are correct.

    Do you have any idea why the first result returned by forum_last_post_link() is something like this?

    http://example/forum/topic/#post-

    (toppic slug and post id are missing)

    Posted: 2 years ago #
  13. ph23man

    Member

    Paamayim, I'm having the same issue on bbpress 1.0.2, the first forum of a sub-forum listing doesn't show the last post properly. Not really sure why it's happening. It's getting the time posted and poster's username but not the link.

    Posted: 1 year ago #
  14. miruru

    Member

    This plugin works great, however, is it possible to have the forum last poster to include subsubforums? It works for subforums.

    Posted: 1 year ago #
  15. What do I put when there isn't anything in the forum?

    Posted: 1 year ago #

RSS feed for this topic

Add a Comment

You must log in to post.