Skip to:
Content

bbPress.org


Ignore:
Timestamp:
11/09/2019 05:35:42 AM (5 years ago)
Author:
johnjamesjacoby
Message:

Counts (meta-data): full audit of forum/topic/reply, public/non-public counts.

This commit is the result of a full count audit, exposing multiple inconsistencies and voids in relation to how public and non-public counts are (re)calculated.

For instance, hidden forum replies are not counted at all, until now. By introducing a new Repair tool, hidden forum reply counts are now counted.

In addition, there were multiple bugs with topic & reply moderation, where the act of approving or unapproving topics or replies would cause the numbers to be inaccurate, or where topics & replies being caught in moderation were still increasing public counts.

It was also possible to, as a Key Master, publicly reply to unapproved topics, which was a completely unanticipated side-effect of allowing Key Masters to do pretty much anything. Going forward, the default reply status is the topic status, but is still beholden to all existing moderation settings and user role capabilities. This results in a more sane user experience, and prevents the unusual circumstance of there being "0 topics and 30 replies" in public-facing forums.

Certain count increase/decrease actions have been reprioritized to avoid collisions and race conditions, proving once again that ya gotta get up to get down.

See #2838. Fixes #1799.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/includes/topics/functions.php

    r6922 r6923  
    844844/**
    845845 * Walks up the post_parent tree from the current topic_id, and updates the
    846  * counts of forums above it. This calls a few internal functions that all run
     846 * meta data of forums above it. This calls several functions that all run
    847847 * manual queries against the database to get their results. As such, this
    848848 * function can be costly to run but is necessary to keep everything accurate.
     
    994994    $old_forum_ancestors = array_values( array_unique( array_merge( array( $old_forum_id ), (array) get_post_ancestors( $old_forum_id ) ) ) );
    995995
    996     // Get reply count.
    997     $public_reply_count = bbp_get_public_child_count( $topic_id, bbp_get_reply_post_type() );
    998 
    999     // Topic status.
    1000     $topic_status = get_post_field( 'post_status', $topic_id );
    1001 
    1002     // Update old/new forum counts.
    1003     if ( $topic_status === bbp_get_public_status_id() ) {
     996    // Public counts
     997    if ( bbp_is_topic_public( $topic_id ) ) {
    1004998
    1005999        // Update old forum counts.
    10061000        bbp_decrease_forum_topic_count( $old_forum_id );
    1007         bbp_bump_forum_reply_count( $old_forum_id, -$public_reply_count );
    10081001
    10091002        // Update new forum counts.
    10101003        bbp_increase_forum_topic_count( $new_forum_id );
    1011         bbp_bump_forum_reply_count( $new_forum_id, $public_reply_count );
     1004
     1005    // Non-public counts
    10121006    } else {
    10131007
     
    10181012        bbp_increase_forum_topic_count_hidden( $new_forum_id );
    10191013    }
     1014
     1015    // Get reply counts.
     1016    $public_reply_count = bbp_get_public_child_count( $topic_id, bbp_get_reply_post_type() );
     1017    $hidden_reply_count = bbp_get_non_public_child_count( $topic_id, bbp_get_reply_post_type() );
     1018
     1019    // Bump reply counts.
     1020    bbp_bump_forum_reply_count( $old_forum_id, -$public_reply_count );
     1021    bbp_bump_forum_reply_count( $new_forum_id, $public_reply_count );
     1022    bbp_bump_forum_reply_count_hidden( $old_forum_id, -$hidden_reply_count );
     1023    bbp_bump_forum_reply_count_hidden( $new_forum_id, $hidden_reply_count );
    10201024
    10211025    // Loop through ancestors and update them
     
    23192323        $topic_id = bbp_get_reply_topic_id( $reply_id );
    23202324
    2321         // If this is a new, unpublished, reply, update hidden count and bail.
    2322         if ( ! bbp_is_reply_published( $reply_id ) ) {
     2325        // Update inverse based on item status
     2326        if ( ! bbp_is_reply_public( $reply_id ) ) {
    23232327            bbp_increase_topic_reply_count_hidden( $topic_id );
    23242328            return;
     
    23262330    }
    23272331
     2332    // Bump up
    23282333    bbp_bump_topic_reply_count( $topic_id );
    23292334}
     
    23472352    // If it's a reply, get the topic id.
    23482353    if ( bbp_is_reply( $topic_id ) ) {
    2349         $topic_id = bbp_get_reply_topic_id( $topic_id );
    2350     }
    2351 
     2354        $reply_id = $topic_id;
     2355        $topic_id = bbp_get_reply_topic_id( $reply_id );
     2356
     2357        // Update inverse based on item status
     2358        if ( ! bbp_is_reply_public( $reply_id ) ) {
     2359            bbp_decrease_topic_reply_count_hidden( $topic_id );
     2360            return;
     2361        }
     2362    }
     2363
     2364    // Bump down
    23522365    bbp_bump_topic_reply_count( $topic_id, -1 );
    23532366}
     
    24002413    // If it's a reply, get the topic id.
    24012414    if ( bbp_is_reply( $topic_id ) ) {
    2402         $topic_id = bbp_get_reply_topic_id( $topic_id );
    2403     }
    2404 
     2415        $reply_id = $topic_id;
     2416        $topic_id = bbp_get_reply_topic_id( $reply_id );
     2417
     2418        // Update inverse based on item status
     2419        if ( bbp_is_reply_public( $reply_id ) ) {
     2420            bbp_increase_topic_reply_count( $topic_id );
     2421            return;
     2422        }
     2423    }
     2424
     2425    // Bump up
    24052426    bbp_bump_topic_reply_count_hidden( $topic_id );
    24062427}
     
    24242445    // If it's a reply, get the topic id.
    24252446    if ( bbp_is_reply( $topic_id ) ) {
    2426         $topic_id = bbp_get_reply_topic_id( $topic_id );
    2427     }
    2428 
     2447        $reply_id = $topic_id;
     2448        $topic_id = bbp_get_reply_topic_id( $reply_id );
     2449
     2450        // Update inverse based on item status
     2451        if ( bbp_is_reply_public( $reply_id ) ) {
     2452            bbp_decrease_topic_reply_count( $topic_id );
     2453            return;
     2454        }
     2455    }
     2456
     2457    // Bump down
    24292458    bbp_bump_topic_reply_count_hidden( $topic_id, -1 );
    24302459}
     
    25092538 * @return int Topic reply count
    25102539 */
    2511 function bbp_update_topic_reply_count( $topic_id = 0, $reply_count = 0 ) {
     2540function bbp_update_topic_reply_count( $topic_id = 0, $reply_count = false ) {
    25122541
    25132542    // If it's a reply, then get the parent (topic id)
     
    25172546
    25182547    // Get replies of topic if not passed
    2519     $reply_count = empty( $reply_count )
     2548    $reply_count = ! is_int( $reply_count )
    25202549        ? bbp_get_public_child_count( $topic_id, bbp_get_reply_post_type() )
    25212550        : (int) $reply_count;
     
    25372566 * @return int Topic hidden reply count
    25382567 */
    2539 function bbp_update_topic_reply_count_hidden( $topic_id = 0, $reply_count = 0 ) {
     2568function bbp_update_topic_reply_count_hidden( $topic_id = 0, $reply_count = false ) {
    25402569
    25412570    // If it's a reply, then get the parent (topic id)
     
    25452574
    25462575    // Get replies of topic
    2547     $reply_count = empty( $reply_count )
     2576    $reply_count = ! is_int( $reply_count )
    25482577        ? bbp_get_non_public_child_count( $topic_id, bbp_get_reply_post_type() )
    25492578        : (int) $reply_count;
     
    25842613
    25852614    // Update only if published
    2586     if ( bbp_get_public_status_id() === get_post_status( $active_id ) ) {
    2587         update_post_meta( $topic_id, '_bbp_last_active_id', $active_id );
    2588     }
     2615    update_post_meta( $topic_id, '_bbp_last_active_id', $active_id );
    25892616
    25902617    // Filter & return
     
    26542681
    26552682    // Update if reply is published
    2656     if ( bbp_is_reply_published( $reply_id ) ) {
    2657         update_post_meta( $topic_id, '_bbp_last_reply_id', $reply_id );
    2658     }
     2683    update_post_meta( $topic_id, '_bbp_last_reply_id', $reply_id );
    26592684
    26602685    // Filter & return
Note: See TracChangeset for help on using the changeset viewer.