Opened 13 years ago
Closed 10 years ago
#1694 closed enhancement (fixed)
Add user counter functions for topics and replies
Reported by: | anointed | Owned by: | johnjamesjacoby |
---|---|---|---|
Milestone: | 2.6 | Priority: | normal |
Severity: | normal | Version: | 2.0 |
Component: | Component - Users | Keywords: | has-patch dev-feedback |
Cc: | stephen@…, djpaul@…, jared@…, wordpress@…, mail@…, nashwan.doaqan@… |
Description
I wanted an easy way to display the total number of topics and replies created by my users. This part was rather easy but I have a few issues with what to do when we're allowing a guest to post topics. The counters add up all the topics for all the users and apply it to each user.
Here is my attempt at this script. Please be kind as I know it doesn't work right for guests and there may be much better ways of doing it.
Attachments (5)
Change History (40)
#4
@
13 years ago
- Milestone changed from 2.1 to 2.2
Functions are in, but are not being used anywhere. No time to finish this for 2.1. Punting to 2.2.
#7
@
12 years ago
I know this is a work in progress and, therefore, there may be code on it's way that hasn't been committed yet, but I think the topic count should include closed topics, ie where post_status is "closed" as well as "publish". Otherwise, one could have a situation where a user posts his first topic which is later closed by an admin/mod, resulting in the OP's topic count being zero - which doesn't make much sense to me.
To accommodate this, tweaks would be needed to bbp_admin_repair_user_reply_count() and bbp_admin_repair_user_topic_count() in bbp-tools.php.
#8
@
12 years ago
- Cc jared@… added
@johnjamesjacoby - this still on track for 2.2?
If so, what's needed? I know the bulk of the functions that handle this are already in, do they just need to be hooked up?
#10
@
12 years ago
- Cc wordpress@… added
I'm using user_meta as a cache for the counts. I'm hooking into all places I care like new reply, new thread, delete reply, delete thread, etc. to keep the user_meta in sync. But had to do this queries to get the counts in a batch (first time, or in the "recounts" menu, in case for some reason the user_meta goes out of sync)
private function batch_fill_replies() { global $wpdb; $sql_delete = "DELETE FROM `{$wpdb->usermeta}` WHERE `meta_key` = '%s';"; $sql_delete = $wpdb->prepare( $sql_delete, self::REPLY_COUNT_KEY ); if ( $wpdb->query( $sql_delete ) === false ) return false; $sql = "INSERT INTO `{$wpdb->usermeta}` (user_id, meta_key, meta_value) SELECT post_author, '%s', COUNT(id) FROM `{$wpdb->posts}` WHERE post_type IN ('reply') GROUP BY post_author HAVING COUNT(id) > 0"; $sql = $wpdb->prepare( $sql, self::REPLY_COUNT_KEY ); if ( $wpdb->query( $sql ) === false ) return false; return true; } private function batch_fill_topics() { global $wpdb; $sql_delete = "DELETE FROM `{$wpdb->usermeta}` WHERE `meta_key` = '%s';"; $sql_delete = $wpdb->prepare( $sql_delete, self::TOPIC_COUNT_KEY ); if ( $wpdb->query( $sql_delete ) === false ) return false; $sql = "INSERT INTO `{$wpdb->usermeta}` (user_id, meta_key, meta_value) SELECT post_author, '%s', COUNT(id) FROM `{$wpdb->posts}` WHERE post_type IN ('topic') GROUP BY post_author HAVING COUNT(id) > 0"; $sql = $wpdb->prepare( $sql, self::TOPIC_COUNT_KEY ); if ( $wpdb->query( $sql ) === false ) return false; return true; }
#11
@
12 years ago
The tool recounts does something similar, breaking the queries up into 10000 user batches.
Are you able to patch the bump function into the correct hooks?
#12
@
12 years ago
There, I implemented the functions and hooked them in.
Had to fix a little issue in bbp_delete_topic. The child replies were not being deleted.
#13
@
12 years ago
Actually, it's still not deleting the replies. Checking the actual mysql query for that WP_Query in bbp_delete_topic, I see the 'post_status' => 'any' is not being honoured, not sure why. Just reported in #2059
#18
@
12 years ago
- Keywords has-patch removed
- Milestone changed from 2.3 to 2.4
Punting to 2.4, since this needs testing and the patch isn't exactly right.
#20
@
11 years ago
Definitely want this for 2.5
@jjj what's left here to do or isn't quite right? I can see what I can do :)
#24
follow-up:
↓ 28
@
11 years ago
- Keywords has-patch added
Refreshed 1694.3.diff
- Use
update_user_option
to update the counts (mulsisite compat) rather thanupdate_user_meta
- Also fixes incorrect
bbp_get_user_reply_count
$filter variable
Testing:
- New Topic increases
wp__bbp_topic_count
usermeta by1
(Wherewp_
is the current $WPDB $Prefix) - New Reply increases
wp__bbp_reply_count
usermeta by1
(Wherewp_
is the current $WPDB $Prefix)
- Trash Topic
wp__bbp_topic_count
is not updated - Trash Reply
wp__bbp_reply_count
is not updated
- Spam Topic
wp__bbp_topic_count
is not updated - Spam Reply
wp__bbp_reply_count
is not updated
- Delete Topic decreases
wp__bbp_topic_count
usermeta by1
(Wherewp_
is the current $WPDB $Prefix) - Delete Reply decreases
wp__bbp_reply_count
usermeta by1
(Wherewp_
is the current $WPDB $Prefix)
Presuming we should switch to subtracting the count when a topic or reply is trashed or marked as spam otherwise the spam and trash topics remain counted in the user counts.
#27
@
11 years ago
Replying to netweb:
Also 'Forum Freshness' is not updated when using
1694.3.diff
.
Ignore this, I'll create a separate ticket for this once I can explain it clearly.
#28
in reply to:
↑ 24
@
11 years ago
- Keywords has-patch added; needs-testing removed
Replying to netweb:
Presuming we should switch to subtracting the count when a topic or reply is trashed or marked as spam otherwise the spam and trash topics remain counted in the user counts.
The above in 1694.4.diff switched to decreasing the count when a topic or reply is spammed or trashed.
Testing:
- New Topic increases
wp__bbp_topic_count
usermeta by1
(Wherewp_
is the current $WPDB $Prefix) - New Reply increases
wp__bbp_reply_count
usermeta by1
(Wherewp_
is the current $WPDB $Prefix)
- Trash Topic decreases
wp__bbp_topic_count
usermeta by1
(Wherewp_
is the current $WPDB $Prefix) - Trash Reply decreases
wp__bbp_reply_count
usermeta by1
(Wherewp_
is the current $WPDB $Prefix)
- Spam Topic decreases
wp__bbp_topic_count
usermeta by1
(Wherewp_
is the current $WPDB $Prefix) - Spam Reply decreases
wp__bbp_reply_count
usermeta by1
(Wherewp_
is the current $WPDB $Prefix)
- Delete Topic
wp__bbp_topic_count
is not updated - Delete Reply
wp__bbp_reply_count
is not updated
#30
follow-up:
↓ 32
@
10 years ago
- Keywords dev-feedback added
- Resolution fixed deleted
- Status changed from closed to reopened
If a user posts his very first post, let's say a reply, it will set his post count to 2 instead of 1, because:
bbp_bump_user_reply_count() bumps it by one. But since
$count = bbp_get_user_reply_count( $user_id, true );
returns 0 for a new user (usermeta _bbp_reply_count not available), it counts the existing posts:
if ( empty( $count ) ) { $count = bbp_get_user_reply_count_raw( $user_id ); }
which is 1 at this point. This gets increased by one afterwards resulting in a post count of 2.
( (int) $count + (int) $difference )
But this isn't a "first post bug". If for any reason the usermeta (_bbp_reply_count or _bbp_topic_count) gets deleted, it will miscalculate again, as far as I can tell.
#32
in reply to:
↑ 30
@
10 years ago
Replying to wpdennis:
If a user posts his very first post, let's say a reply, it will set his post count to 2 instead of 1, because:
bbp_bump_user_reply_count() bumps it by one. But since
$count = bbp_get_user_reply_count( $user_id, true );
returns 0 for a new user (usermeta _bbp_reply_count not available), it counts the existing posts:
if ( empty( $count ) ) { $count = bbp_get_user_reply_count_raw( $user_id ); }which is 1 at this point. This gets increased by one afterwards resulting in a post count of 2.
( (int) $count + (int) $difference )
Confirmed
But this isn't a "first post bug". If for any reason the usermeta (_bbp_reply_count or _bbp_topic_count) gets deleted, it will miscalculate again, as far as I can tell.
This is pretty much true for any of bbPress counts as we rely on storing counts in post meta, these are what the repair tools are for.
This ticket was mentioned in Slack in #bbpress by netweb. View the logs.
10 years ago
#35
@
10 years ago
- Resolution set to fixed
- Status changed from reopened to closed
Marking as fixed, the 1799.5.patch in #1799 resolves the outstanding issue above, and related discussion in Slack.
topic and reply counter functions