Skip to:
Content

bbPress.org

Changeset 7453


Ignore:
Timestamp:
09/07/2026 05:01:34 PM (2 months ago)
Author:
johnjamesjacoby
Message:

Counts: improve repair and subforum integrity.

Count public, private, and hidden subforums from the post hierarchy, and synchronize parent counts when forums change visibility, parent, post type, or are permanently deleted. Keep child relationship metadata aligned when WordPress reparents forums.

Correct count repair queries to target bbPress post types, honor filtered statuses, exclude anonymous authors, preserve unrelated metadata, repair empty user counts, clear metadata caches after direct database writes, and detect database failures from wpdb return values.

In trunk, for 2.7.

Props netweb, GDragoN.
Fixes #2649.
Fixes #2664.
See #2533, #2586, #3678.

Location:
trunk
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/CHANGELOG.md

    r7449 r7453  
    2626- Added hooks after bbPress administration classes load and dedicated hooks
    2727  for canonical redirects.
     28- Added `bbp_post_updated` with the post ID and before/after post objects.
    2829- Added contributor, security, support, governance, and repository guidance.
    2930
    … …  
    3132
    3233- Raised the minimum requirements to WordPress 6.0 and PHP 7.2.
     34- Moved built-in topic, reply, forum, and user count synchronization to
     35  `bbp_transition_post_status`. Existing creation and moderation actions still
     36  fire, but no longer host bbPress's core count callbacks. Extensions that need
     37  finalized counts should use the transition action at priority 11 or later;
     38  permanent deletion continues through `bbp_deleted_topic` and
     39  `bbp_deleted_reply`.
    3340- Kept the classic editor for forums, topics, and replies while making bbPress
    3441  blocks available in Posts and Pages.
    … …  
    5360- Kept public, hidden, and author contribution counts accurate when topics and
    5461  replies change moderation status or are permanently deleted.
     62- Kept subforum counts accurate across public, private, hidden, trashed, moved,
     63  restored, and permanently deleted forums.
     64- Corrected count repair tools to clear stale metadata caches, exclude anonymous
     65  authors from voice counts, honor reply-status filters, rebuild private and
     66  hidden forums, and preserve similarly named metadata on other post types.
    5567
    5668- Cache a distinct count of current-site forum-role holders for forum statistics,
  • trunk/src/includes/admin/tools/repair.php

    r7380 r7453  
    1010// Exit if accessed directly
    1111defined( 'ABSPATH' ) || exit;
     12
     13/** Helpers *******************************************************************/
     14
     15/**
     16 * Clear cached metadata for a repaired bbPress post type.
     17 *
     18 * Repair tools update metadata directly in the database, bypassing the cache
     19 * invalidation performed by the WordPress metadata API.
     20 *
     21 * @since 2.6.16
     22 *
     23 * @param string $post_type Post type name.
     24 */
     25function bbp_admin_repair_clean_post_meta_cache( $post_type = '' ) {
     26        $bbp_db   = bbp_db();
     27        $query    = $bbp_db->prepare( "SELECT ID FROM {$bbp_db->posts} WHERE post_type = %s", $post_type );
     28        $post_ids = wp_parse_id_list( $bbp_db->get_col( $query ) );
     29
     30        if ( ! empty( $post_ids ) ) {
     31                wp_cache_delete_multiple( $post_ids, 'post_meta' );
     32        }
     33}
    1234
    1335/**
    … …  
    242264
    243265        // Post types and status
    244         $tpt = bbp_get_topic_post_type();
    245         $rpt = bbp_get_reply_post_type();
    246         $pps = bbp_get_public_status_id();
    247         $cps = bbp_get_closed_status_id();
     266        $tpt                = bbp_get_topic_post_type();
     267        $rpt                = bbp_get_reply_post_type();
     268        $sql_topic_statuses = "'" . implode( "','", bbp_get_public_topic_statuses() ) . "'";
     269        $sql_reply_statuses = "'" . implode( "','", bbp_get_public_reply_statuses() ) . "'";
    248270
    249271        // Delete the meta key _bbp_reply_count for each topic
    … …  
    253275                                                AND `postmeta`.`meta_key` = '_bbp_reply_count'";
    254276
    255         if ( is_wp_error( $bbp_db->query( $sql_delete ) ) ) {
     277        if ( false === $bbp_db->query( $sql_delete ) ) {
    256278                return array( 1, sprintf( $statement, $result ) );
    257279        }
    … …  
    263285                                        LEFT JOIN `{$bbp_db->posts}` as `replies`
    264286                                                ON  `replies`.`post_parent` = `topics`.`ID`
    265                                                 AND `replies`.`post_status` = '{$pps}'
     287                                                AND `replies`.`post_status` IN ( {$sql_reply_statuses} )
    266288                                                AND `replies`.`post_type`   = '{$rpt}'
    267289                                WHERE `topics`.`post_type` = '{$tpt}'
    268                                         AND `topics`.`post_status` IN ( '{$pps}', '{$cps}' )
     290                                        AND `topics`.`post_status` IN ( {$sql_topic_statuses} )
    269291                                GROUP BY `topics`.`ID`)";
    270292
    271         if ( is_wp_error( $bbp_db->query( $sql ) ) ) {
     293        if ( false === $bbp_db->query( $sql ) ) {
    272294                return array( 2, sprintf( $statement, $result ) );
    273295        }
     296
     297        bbp_admin_repair_clean_post_meta_cache( $tpt );
    274298
    275299        return array( 0, sprintf( $statement, esc_html__( 'Complete!', 'bbpress' ) ) );
    … …  
    291315        $result    = esc_html__( 'Failed!', 'bbpress' );
    292316
    293         $sql_delete = "DELETE FROM {$bbp_db->postmeta} WHERE meta_key IN ('_bbp_voice_count', '_bbp_engagement')";
    294         if ( is_wp_error( $bbp_db->query( $sql_delete ) ) ) {
     317        $tpt = bbp_get_topic_post_type();
     318
     319        $sql_delete = "DELETE `postmeta` FROM `{$bbp_db->postmeta}` AS `postmeta`
     320                                                LEFT JOIN `{$bbp_db->posts}` AS `posts` ON `posts`.`ID` = `postmeta`.`post_id`
     321                                                WHERE `posts`.`post_type` = '{$tpt}'
     322                                                AND `postmeta`.`meta_key` IN ( '_bbp_voice_count', '_bbp_engagement' )";
     323        if ( false === $bbp_db->query( $sql_delete ) ) {
    295324                return array( 1, sprintf( $statement, $result ) );
    296325        }
    297326
    298327        // Post types and status
    299         $tpt = bbp_get_topic_post_type();
    300         $rpt = bbp_get_reply_post_type();
    301         $pps = bbp_get_public_status_id();
    302         $cps = bbp_get_closed_status_id();
     328        $rpt                = bbp_get_reply_post_type();
     329        $sql_topic_statuses = "'" . implode( "','", bbp_get_public_topic_statuses() ) . "'";
     330        $sql_reply_statuses = "'" . implode( "','", bbp_get_public_reply_statuses() ) . "'";
    303331
    304332        $engagements_sql = $bbp_db->prepare( "INSERT INTO {$bbp_db->postmeta} (post_id, meta_key, meta_value) (
    305                 SELECT postmeta.meta_value, '_bbp_engagement', posts.post_author
     333                SELECT topics.ID, '_bbp_engagement', posts.post_author
    306334                        FROM {$bbp_db->posts} AS posts
    307                         LEFT JOIN {$bbp_db->postmeta} AS postmeta
     335                        INNER JOIN {$bbp_db->postmeta} AS postmeta
    308336                                ON posts.ID = postmeta.post_id
    309337                                AND postmeta.meta_key = '_bbp_topic_id'
    310                         WHERE posts.post_type IN (%s, %s)
    311                                 AND posts.post_status IN (%s, %s)
    312                         GROUP BY postmeta.meta_value, posts.post_author)",
     338                        INNER JOIN {$bbp_db->posts} AS topics
     339                                ON topics.ID = postmeta.meta_value
     340                                AND topics.post_type = %s
     341                        WHERE (
     342                                        ( posts.post_type = %s AND posts.post_status IN ({$sql_topic_statuses}) )
     343                                        OR ( posts.post_type = %s AND posts.post_status IN ({$sql_reply_statuses}) )
     344                                )
     345                                AND posts.post_author > 0
     346                        GROUP BY topics.ID, posts.post_author)",
    313347                $tpt,
    314                 $rpt,
    315                 $pps,
    316                 $cps
     348                $tpt,
     349                $rpt
    317350        );
    318351
    319         if ( is_wp_error( $bbp_db->query( $engagements_sql ) ) ) {
     352        if ( false === $bbp_db->query( $engagements_sql ) ) {
    320353                return array( 2, sprintf( $statement, $result ) );
    321354        }
    322355
    323         $voice_count_sql = "INSERT INTO {$bbp_db->postmeta} (post_id, meta_key, meta_value) (
    324                 SELECT post_id, '_bbp_voice_count', COUNT(DISTINCT meta_value)
    325                         FROM {$bbp_db->postmeta}
    326                         WHERE meta_key = '_bbp_engagement'
    327                         GROUP BY post_id)";
    328 
    329         if ( is_wp_error( $bbp_db->query( $voice_count_sql ) ) ) {
     356        $voice_count_sql = $bbp_db->prepare( "INSERT INTO {$bbp_db->postmeta} (post_id, meta_key, meta_value) (
     357                SELECT engagements.post_id, '_bbp_voice_count', COUNT(DISTINCT engagements.meta_value)
     358                        FROM {$bbp_db->postmeta} AS engagements
     359                        INNER JOIN {$bbp_db->posts} AS topics
     360                                ON topics.ID = engagements.post_id
     361                                AND topics.post_type = %s
     362                        WHERE engagements.meta_key = '_bbp_engagement'
     363                        GROUP BY engagements.post_id)",
     364                $tpt
     365        );
     366
     367        if ( false === $bbp_db->query( $voice_count_sql ) ) {
    330368                return array( 3, sprintf( $statement, $result ) );
    331369        }
     370
     371        bbp_admin_repair_clean_post_meta_cache( $tpt );
    332372
    333373        return array( 0, sprintf( $statement, esc_html__( 'Complete!', 'bbpress' ) ) );
    … …  
    349389        $result    = esc_html__( 'Failed!', 'bbpress' );
    350390
    351         $sql_delete = "DELETE FROM `{$bbp_db->postmeta}` WHERE `meta_key` = '_bbp_reply_count_hidden'";
    352         if ( is_wp_error( $bbp_db->query( $sql_delete ) ) ) {
    353                 return array( 1, sprintf( $statement, $result ) );
    354         }
    355 
    356         // Post types and status
     391        $tpt = bbp_get_topic_post_type();
    357392        $rpt = bbp_get_reply_post_type();
    358         $sta = bbp_get_non_public_topic_statuses();
     393
     394        $sql_delete = "DELETE `postmeta` FROM `{$bbp_db->postmeta}` AS `postmeta`
     395                                                LEFT JOIN `{$bbp_db->posts}` AS `posts` ON `posts`.`ID` = `postmeta`.`post_id`
     396                                                WHERE `posts`.`post_type` = '{$tpt}'
     397                                                AND `postmeta`.`meta_key` = '_bbp_reply_count_hidden'";
     398        if ( false === $bbp_db->query( $sql_delete ) ) {
     399                return array( 1, sprintf( $statement, $result ) );
     400        }
     401
     402        // Reply statuses
     403        $sta = bbp_get_non_public_reply_statuses();
    359404
    360405        // SQL
    361406        $sql_status = "'" . implode( "','", $sta ) . "'";
    362407
    363         $sql = "INSERT INTO `{$bbp_db->postmeta}` (`post_id`, `meta_key`, `meta_value`) (SELECT `post_parent`, '_bbp_reply_count_hidden', COUNT(`post_status`) as `meta_value` FROM `{$bbp_db->posts}` WHERE `post_type` = '{$rpt}' AND `post_status` IN ({$sql_status}) GROUP BY `post_parent`)";
    364         if ( is_wp_error( $bbp_db->query( $sql ) ) ) {
     408        $sql = "INSERT INTO `{$bbp_db->postmeta}` (`post_id`, `meta_key`, `meta_value`) (
     409                SELECT `topics`.`ID`, '_bbp_reply_count_hidden', COUNT(`replies`.`ID`) AS `meta_value`
     410                        FROM `{$bbp_db->posts}` AS `replies`
     411                        INNER JOIN `{$bbp_db->posts}` AS `topics`
     412                                ON `topics`.`ID` = `replies`.`post_parent`
     413                                AND `topics`.`post_type` = '{$tpt}'
     414                        WHERE `replies`.`post_type` = '{$rpt}'
     415                                AND `replies`.`post_status` IN ({$sql_status})
     416                        GROUP BY `topics`.`ID`)";
     417        if ( false === $bbp_db->query( $sql ) ) {
    365418                return array( 2, sprintf( $statement, $result ) );
    366419        }
     420
     421        bbp_admin_repair_clean_post_meta_cache( $tpt );
    367422
    368423        return array( 0, sprintf( $statement, esc_html__( 'Complete!', 'bbpress' ) ) );
    … …  
    384439        $result    = esc_html__( 'Failed!', 'bbpress' );
    385440
    386         $sql_delete = "DELETE FROM {$bbp_db->postmeta} WHERE meta_key IN ( '_bbp_topic_count', '_bbp_total_topic_count', '_bbp_topic_count_hidden' )";
    387         if ( is_wp_error( $bbp_db->query( $sql_delete ) ) ) {
     441        $fpt = bbp_get_forum_post_type();
     442
     443        $sql_delete = "DELETE `postmeta` FROM `{$bbp_db->postmeta}` AS `postmeta`
     444                                                LEFT JOIN `{$bbp_db->posts}` AS `posts` ON `posts`.`ID` = `postmeta`.`post_id`
     445                                                WHERE `posts`.`post_type` = '{$fpt}'
     446                                                AND `postmeta`.`meta_key` IN ( '_bbp_topic_count', '_bbp_total_topic_count', '_bbp_topic_count_hidden' )";
     447        if ( false === $bbp_db->query( $sql_delete ) ) {
    388448                return array( 1, sprintf( $statement, $result ) );
    389449        }
    … …  
    391451        $forums = get_posts(
    392452                array(
    393                         'post_type'   => bbp_get_forum_post_type(),
     453                        'fields'      => 'ids',
     454                        'post_type'   => $fpt,
     455                        'post_status' => bbp_get_countable_forum_statuses(),
    394456                        'numberposts' => -1
    395457                )
    … …  
    397459
    398460        if ( ! empty( $forums ) ) {
    399                 foreach ( $forums as $forum ) {
    400                         bbp_update_forum_topic_count( $forum->ID );
    401                         bbp_update_forum_topic_count_hidden( $forum->ID );
     461                foreach ( $forums as $forum_id ) {
     462                        bbp_update_forum_topic_count( $forum_id );
     463                        bbp_update_forum_topic_count_hidden( $forum_id );
    402464                }
    403465        } else {
    … …  
    482544                                                LEFT JOIN `{$bbp_db->posts}` AS `posts` ON `posts`.`ID` = `postmeta`.`post_id`
    483545                                                WHERE `posts`.`post_type` = '{$fpt}'
    484                                                 AND `postmeta`.`meta_key` = '_bbp_reply_count'
    485                                                 OR `postmeta`.`meta_key` = '_bbp_total_reply_count'";
    486 
    487         if ( is_wp_error( $bbp_db->query( $sql_delete ) ) ) {
     546                                                AND `postmeta`.`meta_key` IN ( '_bbp_reply_count', '_bbp_total_reply_count' )";
     547
     548        if ( false === $bbp_db->query( $sql_delete ) ) {
    488549                return array( 1, sprintf( $statement, $result ) );
    489550        }
    … …  
    492553        $forums = get_posts(
    493554                array(
    494                         'post_type'   => bbp_get_forum_post_type(),
     555                        'fields'      => 'ids',
     556                        'post_type'   => $fpt,
     557                        'post_status' => bbp_get_countable_forum_statuses(),
    495558                        'numberposts' => -1
    496559                )
    … …  
    498561
    499562        if ( ! empty( $forums ) ) {
    500                 foreach ( $forums as $forum ) {
    501                         bbp_update_forum_reply_count( $forum->ID );
     563                foreach ( $forums as $forum_id ) {
     564                        bbp_update_forum_reply_count( $forum_id );
    502565                }
    503566        } else {
    … …  
    531594                                                LEFT JOIN `{$bbp_db->posts}` AS `posts` ON `posts`.`ID` = `postmeta`.`post_id`
    532595                                                WHERE `posts`.`post_type` = '{$fpt}'
    533                                                 AND `postmeta`.`meta_key` = '_bbp_reply_count_hidden'
    534                                                 OR `postmeta`.`meta_key` = '_bbp_total_reply_count_hidden'";
    535 
    536         if ( is_wp_error( $bbp_db->query( $sql_delete ) ) ) {
     596                                                AND `postmeta`.`meta_key` IN ( '_bbp_reply_count_hidden', '_bbp_total_reply_count_hidden' )";
     597
     598        if ( false === $bbp_db->query( $sql_delete ) ) {
    537599                return array( 1, sprintf( $statement, $result ) );
    538600        }
    … …  
    541603        $forums = get_posts(
    542604                array(
    543                         'post_type'   => bbp_get_forum_post_type(),
     605                        'fields'      => 'ids',
     606                        'post_type'   => $fpt,
     607                        'post_status' => bbp_get_countable_forum_statuses(),
    544608                        'numberposts' => -1
    545609                )
    … …  
    547611
    548612        if ( ! empty( $forums ) ) {
    549                 foreach ( $forums as $forum ) {
    550                         bbp_update_forum_reply_count_hidden( $forum->ID );
     613                foreach ( $forums as $forum_id ) {
     614                        bbp_update_forum_reply_count_hidden( $forum_id );
    551615                }
    552616        } else {
    … …  
    574638        $sql_type    = bbp_get_topic_post_type();
    575639        $sql_status  = "'" . implode( "','", bbp_get_public_topic_statuses() ) . "'";
    576         $sql_select  = "SELECT `post_author`, COUNT(DISTINCT `ID`) as `_count` FROM `{$bbp_db->posts}` WHERE `post_type` = '{$sql_type}' AND `post_status` IN ({$sql_status}) GROUP BY `post_author`";
     640        $sql_select  = "SELECT `post_author`, COUNT(*) as `_count` FROM `{$bbp_db->posts}` WHERE `post_type` = '{$sql_type}' AND `post_status` IN ({$sql_status}) AND `post_author` > 0 GROUP BY `post_author`";
    577641        $insert_rows = $bbp_db->get_results( $sql_select );
    578642
    579         if ( is_wp_error( $insert_rows ) ) {
     643        if ( ! empty( $bbp_db->last_error ) ) {
    580644                return array( 1, sprintf( $statement, $result ) );
    581645        }
    582646
    583647        $key           = $bbp_db->prefix . '_bbp_topic_count';
     648        $user_ids      = (array) $bbp_db->get_col( "SELECT `user_id` FROM `{$bbp_db->usermeta}` WHERE `meta_key` = '{$key}'" );
    584649        $insert_values = array();
     650
     651        if ( ! empty( $bbp_db->last_error ) ) {
     652                return array( 2, sprintf( $statement, $result ) );
     653        }
     654
    585655        foreach ( $insert_rows as $insert_row ) {
    586                 $insert_values[] = "('{$insert_row->post_author}', '{$key}', '{$insert_row->_count}')";
    587         }
    588 
    589         if ( ! count( $insert_values ) ) {
    590                 return array( 2, sprintf( $statement, $result ) );
     656                $user_id         = (int) $insert_row->post_author;
     657                $user_ids[]      = $user_id;
     658                $insert_values[] = $bbp_db->prepare( '(%d, %s, %d)', $user_id, $key, (int) $insert_row->_count );
    591659        }
    592660
    593661        $sql_delete = "DELETE FROM `{$bbp_db->usermeta}` WHERE `meta_key` = '{$key}'";
    594         if ( is_wp_error( $bbp_db->query( $sql_delete ) ) ) {
     662        if ( false === $bbp_db->query( $sql_delete ) ) {
    595663                return array( 3, sprintf( $statement, $result ) );
    596664        }
    597665
     666        $insert_error = false;
    598667        foreach ( array_chunk( $insert_values, 10000 ) as $chunk ) {
    599668                $chunk = "\n" . implode( ",\n", $chunk );
    600669                $sql_insert = "INSERT INTO `{$bbp_db->usermeta}` (`user_id`, `meta_key`, `meta_value`) VALUES {$chunk}";
    601670
    602                 if ( is_wp_error( $bbp_db->query( $sql_insert ) ) ) {
    603                         return array( 4, sprintf( $statement, $result ) );
    604                 }
     671                if ( false === $bbp_db->query( $sql_insert ) ) {
     672                        $insert_error = true;
     673                        break;
     674                }
     675        }
     676
     677        // Clear cached counts for users affected by the direct database update
     678        $user_ids = wp_parse_id_list( $user_ids );
     679        if ( ! empty( $user_ids ) ) {
     680                wp_cache_delete_multiple( $user_ids, 'user_meta' );
     681        }
     682
     683        if ( $insert_error ) {
     684                return array( 4, sprintf( $statement, $result ) );
    605685        }
    606686
    … …  
    625705        $sql_type    = bbp_get_reply_post_type();
    626706        $sql_status  = "'" . implode( "','", bbp_get_public_reply_statuses() ) . "'";
    627         $sql_select  = "SELECT `post_author`, COUNT(DISTINCT `ID`) as `_count` FROM `{$bbp_db->posts}` WHERE `post_type` = '{$sql_type}' AND `post_status` IN ({$sql_status}) GROUP BY `post_author`";
     707        $sql_select  = "SELECT `post_author`, COUNT(*) as `_count` FROM `{$bbp_db->posts}` WHERE `post_type` = '{$sql_type}' AND `post_status` IN ({$sql_status}) AND `post_author` > 0 GROUP BY `post_author`";
    628708        $insert_rows = $bbp_db->get_results( $sql_select );
    629709
    630         if ( is_wp_error( $insert_rows ) ) {
     710        if ( ! empty( $bbp_db->last_error ) ) {
    631711                return array( 1, sprintf( $statement, $result ) );
    632712        }
    633713
    634714        $key           = $bbp_db->prefix . '_bbp_reply_count';
     715        $user_ids      = (array) $bbp_db->get_col( "SELECT `user_id` FROM `{$bbp_db->usermeta}` WHERE `meta_key` = '{$key}'" );
    635716        $insert_values = array();
     717
     718        if ( ! empty( $bbp_db->last_error ) ) {
     719                return array( 2, sprintf( $statement, $result ) );
     720        }
     721
    636722        foreach ( $insert_rows as $insert_row ) {
    637                 $insert_values[] = "('{$insert_row->post_author}', '{$key}', '{$insert_row->_count}')";
    638         }
    639 
    640         if ( ! count( $insert_values ) ) {
    641                 return array( 2, sprintf( $statement, $result ) );
     723                $user_id         = (int) $insert_row->post_author;
     724                $user_ids[]      = $user_id;
     725                $insert_values[] = $bbp_db->prepare( '(%d, %s, %d)', $user_id, $key, (int) $insert_row->_count );
    642726        }
    643727
    644728        $sql_delete = "DELETE FROM `{$bbp_db->usermeta}` WHERE `meta_key` = '{$key}'";
    645         if ( is_wp_error( $bbp_db->query( $sql_delete ) ) ) {
     729        if ( false === $bbp_db->query( $sql_delete ) ) {
    646730                return array( 3, sprintf( $statement, $result ) );
    647731        }
    648732
     733        $insert_error = false;
    649734        foreach ( array_chunk( $insert_values, 10000 ) as $chunk ) {
    650735                $chunk = "\n" . implode( ",\n", $chunk );
    651736                $sql_insert = "INSERT INTO `{$bbp_db->usermeta}` (`user_id`, `meta_key`, `meta_value`) VALUES {$chunk}";
    652737
    653                 if ( is_wp_error( $bbp_db->query( $sql_insert ) ) ) {
    654                         return array( 4, sprintf( $statement, $result ) );
    655                 }
     738                if ( false === $bbp_db->query( $sql_insert ) ) {
     739                        $insert_error = true;
     740                        break;
     741                }
     742        }
     743
     744        // Clear cached counts for users affected by the direct database update
     745        $user_ids = wp_parse_id_list( $user_ids );
     746        if ( ! empty( $user_ids ) ) {
     747                wp_cache_delete_multiple( $user_ids, 'user_meta' );
     748        }
     749
     750        if ( $insert_error ) {
     751                return array( 4, sprintf( $statement, $result ) );
    656752        }
    657753
  • trunk/src/includes/core/actions.php

    r7449 r7453  
    5555add_action( 'wp_footer',                'bbp_footer',                 10    );
    5656add_action( 'transition_post_status',   'bbp_transition_post_status', 10, 3 );
     57add_action( 'post_updated',             'bbp_post_updated',           10, 3 );
    5758
    5859/**
    … …  
    181182add_action( 'trashed_post',   'bbp_trashed_forum'   );
    182183add_action( 'untrashed_post', 'bbp_untrashed_forum' );
    183 add_action( 'deleted_post',   'bbp_deleted_forum'  );
     184add_action( 'deleted_post',   'bbp_deleted_forum', 10, 2 );
    184185
    185186// Auto trash/untrash/delete a forums topics
    … …  
    275276add_action( 'bbp_unapproved_reply', 'bbp_update_reply_walker' );
    276277
    277 // Update counts when topics and replies are created or change status
     278// Update counts from persisted post status changes
    278279add_action( 'bbp_transition_post_status', 'bbp_update_counts_on_transition_post_status', 10, 3 );
     280add_action( 'bbp_transition_post_status', 'bbp_update_forum_subforum_count_on_transition_post_status', 10, 3 );
     281
     282// Update parent subforum counts after updates and permanent deletion
     283add_action( 'bbp_deleted_forum',   'bbp_reparent_forum_subforums', 9, 2 );
     284add_action( 'bbp_deleted_forum',   'bbp_update_parent_forum_subforum_count', 10, 2 );
     285add_action( 'bbp_post_updated',    'bbp_update_forum_subforum_counts_on_post_updated', 10, 3 );
    279286
    280287// Update user topic & reply counts
  • trunk/src/includes/core/sub-actions.php

    r7383 r7453  
    297297        // Do the action
    298298        do_action( 'bbp_transition_post_status', $new_status, $old_status, $post );
     299}
     300
     301/**
     302 * Add the bbPress-specific post updated action.
     303 *
     304 * @since 2.6.16
     305 *
     306 * @param int     $post_id     Post ID.
     307 * @param WP_Post $post_after  Post object following the update.
     308 * @param WP_Post $post_before Post object before the update.
     309 */
     310function bbp_post_updated( $post_id = 0, $post_after = false, $post_before = false ) {
     311        $post_types = bbp_get_post_types();
     312
     313        // Bail if neither version is a bbPress post type
     314        if ( ! in_array( $post_after->post_type, $post_types, true ) && ! in_array( $post_before->post_type, $post_types, true ) ) {
     315                return;
     316        }
     317
     318        // Do the action
     319        do_action( 'bbp_post_updated', $post_id, $post_after, $post_before );
    299320}
    300321
  • trunk/src/includes/forums/functions.php

    r7423 r7453  
    18151815 *
    18161816 * @since 2.0.0 bbPress (r2625)
    1817  *
    1818  * @param int $forum_id Optional. Forum id.
    1819  * @param int $subforums Optional. Number of subforums.
    1820  * @return bool True on success, false on failure.
     1817 * @since 2.6.16 Count supported forum visibilities from the post hierarchy.
     1818 *
     1819 * @param int      $forum_id Optional. Forum ID.
     1820 * @param int|bool $subforums Optional. Number of subforums, or false to query.
     1821         * @return int|false Number of subforums, or false on query failure.
    18211822 */
    18221823function bbp_update_forum_subforum_count( $forum_id = 0, $subforums = false ) {
    … …  
    18241825
    18251826        // Maybe query for counts
    1826         $subforums = ! is_int( $subforums )
    1827                 ? bbp_get_public_child_count( $forum_id, bbp_get_forum_post_type() )
    1828                 : (int) $subforums;
     1827        if ( ! is_int( $subforums ) ) {
     1828                $bbp_db    = bbp_db();
     1829                $post_type = bbp_get_forum_post_type();
     1830                $statuses  = bbp_get_countable_forum_statuses();
     1831
     1832                if ( ! empty( $statuses ) ) {
     1833                        $placeholders = implode( ', ', array_fill( 0, count( $statuses ), '%s' ) );
     1834                        $sql           = "SELECT COUNT(*) FROM {$bbp_db->posts} WHERE post_parent = %d AND post_type = %s AND post_status IN ({$placeholders})";
     1835                        $query         = $bbp_db->prepare( $sql, array_merge( array( $forum_id, $post_type ), $statuses ) );
     1836                        $subforums     = $bbp_db->get_var( $query );
     1837
     1838                        // Bail if the count query failed
     1839                        if ( ! empty( $bbp_db->last_error ) ) {
     1840                                return false;
     1841                        }
     1842
     1843                        $subforums = bbp_number_not_negative( $subforums );
     1844                } else {
     1845                        $subforums = 0;
     1846                }
     1847        }
    18291848
    18301849        update_post_meta( $forum_id, '_bbp_forum_subforum_count', $subforums );
    … …  
    18321851        // Filter & return
    18331852        return (int) apply_filters( 'bbp_update_forum_subforum_count', $subforums, $forum_id );
     1853}
     1854
     1855/**
     1856 * Synchronize child-forum metadata after WordPress reparents deleted children.
     1857 *
     1858 * @since 2.6.16
     1859 *
     1860 * @param int     $forum_id Deleted forum ID.
     1861 * @param WP_Post $forum    Deleted forum post object.
     1862 */
     1863function bbp_reparent_forum_subforums( $forum_id = 0, $forum = false ) {
     1864        $bbp_db    = bbp_db();
     1865        $post_type = bbp_get_forum_post_type();
     1866        $meta_key  = '_bbp_forum_id';
     1867        $sql       = "SELECT posts.ID
     1868                FROM {$bbp_db->posts} AS posts
     1869                INNER JOIN {$bbp_db->postmeta} AS postmeta
     1870                        ON posts.ID = postmeta.post_id
     1871                        AND postmeta.meta_key = %s
     1872                WHERE posts.post_type = %s
     1873                        AND posts.post_parent = %d
     1874                        AND postmeta.meta_value = %d";
     1875        $query     = $bbp_db->prepare( $sql, $meta_key, $post_type, $forum->post_parent, $forum_id );
     1876
     1877        foreach ( wp_parse_id_list( $bbp_db->get_col( $query ) ) as $subforum_id ) {
     1878                bbp_update_forum_id( $subforum_id, $forum->post_parent );
     1879        }
     1880}
     1881
     1882/**
     1883 * Update a parent forum's subforum count after a child is permanently deleted.
     1884 *
     1885 * @since 2.6.16
     1886 *
     1887 * @param int          $forum_id Forum ID.
     1888 * @param WP_Post|bool $forum    Optional. Forum post object.
     1889 * @return int|false Updated subforum count, or false if there is no parent.
     1890 */
     1891function bbp_update_parent_forum_subforum_count( $forum_id = 0, $forum = false ) {
     1892        $forum = ( $forum instanceof WP_Post )
     1893                ? $forum
     1894                : get_post( $forum_id );
     1895
     1896        // Bail if the forum has no parent
     1897        if ( empty( $forum ) || empty( $forum->post_parent ) ) {
     1898                return false;
     1899        }
     1900
     1901        return bbp_update_forum_subforum_count( $forum->post_parent );
     1902}
     1903
     1904/**
     1905 * Update a parent forum's subforum count after a child changes count status.
     1906 *
     1907 * @since 2.6.16
     1908 *
     1909 * @param string  $new_status New post status.
     1910 * @param string  $old_status Old post status.
     1911 * @param WP_Post $forum      Forum post object.
     1912 * @return int|false Updated subforum count, or false when no update is needed.
     1913 */
     1914function bbp_update_forum_subforum_count_on_transition_post_status( $new_status = '', $old_status = '', $forum = false ) {
     1915
     1916        // Bail if this is not a child forum
     1917        if ( ( bbp_get_forum_post_type() !== $forum->post_type ) || empty( $forum->post_parent ) ) {
     1918                return false;
     1919        }
     1920
     1921        $statuses    = bbp_get_countable_forum_statuses();
     1922        $was_counted = in_array( $old_status, $statuses, true );
     1923        $is_counted  = in_array( $new_status, $statuses, true );
     1924
     1925        // Bail if subforum count membership did not change
     1926        if ( $was_counted === $is_counted ) {
     1927                return false;
     1928        }
     1929
     1930        return bbp_update_forum_subforum_count( $forum->post_parent );
     1931}
     1932
     1933/**
     1934 * Update subforum counts after a forum changes parent or post type.
     1935 *
     1936 * @since 2.6.16
     1937 *
     1938 * @param int     $forum_id     Forum ID.
     1939 * @param WP_Post $forum_after  Forum object following the update.
     1940 * @param WP_Post $forum_before Forum object before the update.
     1941 */
     1942function bbp_update_forum_subforum_counts_on_post_updated( $forum_id = 0, $forum_after = false, $forum_before = false ) {
     1943        $post_type    = bbp_get_forum_post_type();
     1944        $parent_moved = ( $forum_after->post_parent !== $forum_before->post_parent );
     1945
     1946        // Bail if forum hierarchy membership did not change
     1947        if ( ! $parent_moved && ( $forum_after->post_type === $forum_before->post_type ) ) {
     1948                return false;
     1949        }
     1950
     1951        // Synchronize parent metadata when the updated post remains a forum
     1952        if ( ( $post_type === $forum_after->post_type ) && ( $parent_moved || ( $post_type !== $forum_before->post_type ) ) ) {
     1953                bbp_update_forum_id( $forum_id, $forum_after->post_parent );
     1954        }
     1955
     1956        $statuses    = bbp_get_countable_forum_statuses();
     1957        $was_counted = ( $post_type === $forum_before->post_type ) && in_array( $forum_before->post_status, $statuses, true );
     1958        $is_counted  = ( $post_type === $forum_after->post_type ) && in_array( $forum_after->post_status, $statuses, true );
     1959
     1960        // Include the previous parent when it formerly contained this subforum
     1961        $parent_ids = $was_counted
     1962                ? array( $forum_before->post_parent )
     1963                : array();
     1964
     1965        // Include the new parent when it now contains this subforum
     1966        if ( $is_counted ) {
     1967                $parent_ids[] = $forum_after->post_parent;
     1968        }
     1969
     1970        // Recount each affected parent once
     1971        foreach ( array_filter( array_unique( $parent_ids ) ) as $parent_id ) {
     1972                bbp_update_forum_subforum_count( $parent_id );
     1973        }
    18341974}
    18351975
    … …  
    21972337        // Filter & return
    21982338        return (array) apply_filters( 'bbp_get_non_public_forum_statuses', $statuses );
     2339}
     2340
     2341/**
     2342 * Return forum statuses included in forum and subforum counts.
     2343 *
     2344 * @since 2.6.16
     2345 *
     2346 * @return array
     2347 */
     2348function bbp_get_countable_forum_statuses() {
     2349        $statuses = array_values( array_unique( array_merge( bbp_get_public_forum_statuses(), bbp_get_non_public_forum_statuses() ) ) );
     2350
     2351        // Filter & return
     2352        return (array) apply_filters( 'bbp_get_countable_forum_statuses', $statuses );
    21992353}
    22002354
    … …  
    29573111 * @since 2.1.0 bbPress (r3668)
    29583112 * @since 2.6.0 bbPress (r6526) Not recommend for usage
    2959  */
    2960 function bbp_deleted_forum( $forum_id = 0 ) {
     3113 * @since 2.6.16 Added the `$forum` parameter and passed it to the action.
     3114 *
     3115 * @param int          $forum_id Forum ID.
     3116 * @param WP_Post|bool $forum    Optional. Deleted forum post object.
     3117 * @return false|null False if the post is not a forum, otherwise null.
     3118 */
     3119function bbp_deleted_forum( $forum_id = 0, $forum = false ) {
     3120        $forum_id = bbp_get_forum_id( $forum_id );
     3121        $forum    = ( $forum instanceof WP_Post )
     3122                ? $forum
     3123                : get_post( $forum_id );
     3124
     3125        if ( empty( $forum_id ) || ! $forum || ( bbp_get_forum_post_type() !== $forum->post_type ) ) {
     3126                return false;
     3127        }
     3128
     3129        do_action( 'bbp_deleted_forum', $forum_id, $forum );
     3130}
     3131
     3132/**
     3133 * Called after trashing a forum.
     3134 *
     3135 * @since 2.1.0 bbPress (r3668)
     3136 */
     3137function bbp_trashed_forum( $forum_id = 0 ) {
    29613138        $forum_id = bbp_get_forum_id( $forum_id );
    29623139
    … …  
    29653142        }
    29663143
    2967         do_action( 'bbp_deleted_forum', $forum_id );
    2968 }
    2969 
    2970 /**
    2971  * Called after trashing a forum.
     3144        do_action( 'bbp_trashed_forum', $forum_id );
     3145}
     3146
     3147/**
     3148 * Called after untrashing a forum.
    29723149 *
    29733150 * @since 2.1.0 bbPress (r3668)
    29743151 */
    2975 function bbp_trashed_forum( $forum_id = 0 ) {
     3152function bbp_untrashed_forum( $forum_id = 0 ) {
    29763153        $forum_id = bbp_get_forum_id( $forum_id );
    29773154
    … …  
    29803157        }
    29813158
    2982         do_action( 'bbp_trashed_forum', $forum_id );
    2983 }
    2984 
    2985 /**
    2986  * Called after untrashing a forum.
    2987  *
    2988  * @since 2.1.0 bbPress (r3668)
    2989  */
    2990 function bbp_untrashed_forum( $forum_id = 0 ) {
    2991         $forum_id = bbp_get_forum_id( $forum_id );
    2992 
    2993         if ( empty( $forum_id ) || ! bbp_is_forum( $forum_id ) ) {
    2994                 return false;
    2995         }
    2996 
    29973159        do_action( 'bbp_untrashed_forum', $forum_id );
    29983160}
  • trunk/src/readme.txt

    r7418 r7453  
    4444* bbPress also comes with built-in support for Akismet and BuddyPress, two very popular and very powerful WordPress plugins. If you're using either, visit your Forum Settings page and ensure that integration appears correct.
    4545
     46== Developer Notes ==
     47
     48= Count updates in 2.6.16 =
     49
     50bbPress now synchronizes its built-in public and hidden topic and reply counts, aggregate forum counts, and user contribution counts on `bbp_transition_post_status` at priority 10, after WordPress persists the new post status. This action receives the new status, old status, and `WP_Post` object.
     51
     52The existing `bbp_new_*`, `bbp_insert_*`, `bbp_trash_*`, `bbp_untrash_*`, `bbp_spam_*`, `bbp_unspam_*`, `bbp_approve_*`, and `bbp_unapprove_*` topic and reply actions continue to fire with their existing arguments and timing. Their corresponding completed actions, such as `bbp_trashed_*`, also remain available. The public count helper functions also remain callable.
     53
     54bbPress no longer attaches its built-in increase, decrease, and insertion count callbacks to those creation and moderation actions. Manually firing one of those actions without changing the post status therefore no longer updates counts. Extensions that need finalized counts after creation or moderation should use `bbp_transition_post_status` at priority 11 or later.
     55
     56Permanent deletion does not produce a post-status transition. Its count maintenance continues through the existing `bbp_deleted_topic` and `bbp_deleted_reply` actions.
     57
     58= Subforum counts in 2.6.16 =
     59
     60bbPress now maintains subforum counts when forums are trashed, restored, permanently deleted, or moved between parents. The new `bbp_post_updated` action receives the post ID, the updated `WP_Post` object, and the previous `WP_Post` object after any bbPress post type is updated.
     61
    4662== Screenshots ==
    4763
    … …  
    5773
    5874Check out the [releases page](https://codex.bbpress.org/releases/)
     75
     76== Upgrade Notice ==
     77
     78= 2.6.16 =
     79
     80Count maintenance now uses the bbPress post-status transition action. Extensions that customize topic or reply counts should review the Developer Notes.
  • trunk/tests/phpunit/testcases/admin/tools.php

    r7365 r7453  
    108108        /**
    109109         * @covers ::bbp_admin_repair_topic_reply_count
    110          * @todo   Implement test_bbp_admin_repair_topic_reply_count().
     110         * @ticket BBP2664
    111111         */
    112112        public function test_bbp_admin_repair_topic_reply_count() {
    113                 // Remove the following lines when you implement this test.
    114                 $this->markTestIncomplete(
    115                         'This test has not been implemented yet.'
    116                 );
     113                $topic_id = $this->factory->topic->create();
     114
     115                $this->factory->reply->create_many( 2, array( 'post_parent' => $topic_id ) );
     116                $this->factory->reply->create( array(
     117                        'post_parent' => $topic_id,
     118                        'post_status' => bbp_get_pending_status_id(),
     119                ) );
     120                bbp_update_topic_reply_count( $topic_id, 9 );
     121
     122                $public_statuses = function ( $statuses ) {
     123                        $statuses[] = bbp_get_pending_status_id();
     124                        return $statuses;
     125                };
     126                add_filter( 'bbp_get_public_reply_statuses', $public_statuses );
     127                $result = bbp_admin_repair_topic_reply_count();
     128                remove_filter( 'bbp_get_public_reply_statuses', $public_statuses );
     129
     130                $this->assertSame( 0, $result[0] );
     131                $this->assertSame( 3, bbp_get_topic_reply_count( $topic_id, true ) );
    117132        }
    118133
    119134        /**
    120135         * @covers ::bbp_admin_repair_topic_voice_count
     136         * @ticket BBP2664
    121137         */
    122138        public function test_bbp_admin_repair_topic_voice_count() {
    … …  
    153169                $this->assertSame( '3', $count );
    154170
     171                $pending_user_id = $this->factory->user->create();
     172                $this->factory->reply->create( array(
     173                        'post_author' => $pending_user_id,
     174                        'post_parent' => $t,
     175                        'post_status' => bbp_get_pending_status_id(),
     176                        'reply_meta'  => array(
     177                                'forum_id' => $f,
     178                                'topic_id' => $t,
     179                        ),
     180                ) );
     181
    155182                // Delete the topic _bbp_voice_count meta key.
    156183                $this->assertTrue( delete_post_meta_by_key( '_bbp_voice_count' ) );
    … …  
    160187
    161188                // Repair the topic voice count meta.
     189                $public_statuses = function ( $statuses ) {
     190                        $statuses[] = bbp_get_pending_status_id();
     191                        return $statuses;
     192                };
     193                add_filter( 'bbp_get_public_reply_statuses', $public_statuses );
    162194                bbp_admin_repair_topic_voice_count();
    163 
    164                 clean_post_cache( $t );
     195                remove_filter( 'bbp_get_public_reply_statuses', $public_statuses );
    165196
    166197                $count = bbp_get_topic_voice_count( $t );
    167                 $this->assertSame( '3', $count );
     198                $this->assertSame( '4', $count );
     199        }
     200
     201        /**
     202         * @covers ::bbp_admin_repair_topic_voice_count
     203         */
     204        public function test_bbp_admin_repair_topic_voice_count_excludes_anonymous_authors() {
     205                $forum_id = $this->factory->forum->create();
     206                $topic_id = $this->factory->topic->create( array(
     207                        'post_author' => 0,
     208                        'post_parent' => $forum_id,
     209                        'topic_meta'  => array(
     210                                'forum_id' => $forum_id,
     211                        ),
     212                ) );
     213
     214                $this->factory->reply->create( array(
     215                        'post_author' => 0,
     216                        'post_parent' => $topic_id,
     217                        'reply_meta'  => array(
     218                                'forum_id' => $forum_id,
     219                                'topic_id' => $topic_id,
     220                        ),
     221                ) );
     222
     223                bbp_admin_repair_topic_voice_count();
     224
     225                $this->assertSame( 0, bbp_get_topic_voice_count( $topic_id, true ) );
    168226        }
    169227
    170228        /**
    171229         * @covers ::bbp_admin_repair_topic_hidden_reply_count
     230         * @ticket BBP2664
    172231         */
    173232        public function test_bbp_admin_repair_topic_hidden_reply_count() {
    … …  
    214273
    215274                // Repair the topic hidden reply count meta.
     275                add_filter( 'bbp_get_non_public_topic_statuses', '__return_empty_array' );
    216276                bbp_admin_repair_topic_hidden_reply_count();
    217 
    218                 clean_post_cache( $t );
     277                remove_filter( 'bbp_get_non_public_topic_statuses', '__return_empty_array' );
    219278
    220279                $count = bbp_get_topic_reply_count_hidden( $t, true );
    … …  
    512571
    513572        /**
     573         * @covers ::bbp_admin_repair_forum_topic_count
     574         * @covers ::bbp_admin_repair_forum_reply_count
     575         * @covers ::bbp_admin_repair_forum_hidden_reply_count
     576         */
     577        public function test_forum_count_repairs_include_private_and_hidden_forums() {
     578                $forum_ids = array();
     579
     580                foreach ( array( bbp_get_private_status_id(), bbp_get_hidden_status_id() ) as $status ) {
     581                        $forum_id   = $this->factory->forum->create( array( 'post_status' => $status ) );
     582                        $forum_ids[] = $forum_id;
     583                        $topic_id   = $this->factory->topic->create( array(
     584                                'post_parent' => $forum_id,
     585                                'topic_meta'  => array( 'forum_id' => $forum_id ),
     586                        ) );
     587
     588                        $this->factory->reply->create( array(
     589                                'post_parent' => $topic_id,
     590                                'reply_meta'  => array(
     591                                        'forum_id' => $forum_id,
     592                                        'topic_id' => $topic_id,
     593                                ),
     594                        ) );
     595                        $this->factory->reply->create( array(
     596                                'post_parent' => $topic_id,
     597                                'post_status' => bbp_get_spam_status_id(),
     598                                'reply_meta'  => array(
     599                                        'forum_id' => $forum_id,
     600                                        'topic_id' => $topic_id,
     601                                ),
     602                        ) );
     603                }
     604
     605                bbp_admin_repair_forum_topic_count();
     606                bbp_admin_repair_forum_reply_count();
     607                bbp_admin_repair_forum_hidden_reply_count();
     608
     609                foreach ( $forum_ids as $forum_id ) {
     610                        $this->assertSame( 1, bbp_get_forum_topic_count( $forum_id, true, true ) );
     611                        $this->assertSame( 1, bbp_get_forum_reply_count( $forum_id, true, true ) );
     612                        $this->assertSame( 1, bbp_get_forum_reply_count_hidden( $forum_id, true, true ) );
     613                }
     614        }
     615
     616        /**
     617         * @covers ::bbp_admin_repair_topic_voice_count
     618         * @covers ::bbp_admin_repair_topic_hidden_reply_count
     619         * @covers ::bbp_admin_repair_forum_topic_count
     620         * @covers ::bbp_admin_repair_forum_reply_count
     621         * @covers ::bbp_admin_repair_forum_hidden_reply_count
     622         * @ticket BBP2586
     623         */
     624        public function test_count_repairs_preserve_non_bbp_post_meta() {
     625                $this->factory->forum->create();
     626                $post_id  = $this->factory->post->create();
     627                $reply_id = $this->factory->post->create( array(
     628                        'post_author' => $this->factory->user->create(),
     629                        'post_parent' => $post_id,
     630                        'post_status' => bbp_get_public_status_id(),
     631                        'post_type'   => bbp_get_reply_post_type(),
     632                ) );
     633
     634                update_post_meta( $reply_id, '_bbp_topic_id', $post_id );
     635                $this->factory->post->create( array(
     636                        'post_parent' => $post_id,
     637                        'post_status' => bbp_get_spam_status_id(),
     638                        'post_type'   => bbp_get_reply_post_type(),
     639                ) );
     640
     641                $metadata = array(
     642                        '_bbp_reply_count_hidden'       => 4,
     643                        '_bbp_voice_count'              => 5,
     644                        '_bbp_engagement'               => 6,
     645                        '_bbp_total_reply_count'        => 7,
     646                        '_bbp_total_reply_count_hidden' => 8,
     647                        '_bbp_topic_count'              => 9,
     648                        '_bbp_total_topic_count'        => 10,
     649                        '_bbp_topic_count_hidden'       => 11,
     650                );
     651
     652                foreach ( $metadata as $key => $value ) {
     653                        update_post_meta( $post_id, $key, $value );
     654                }
     655
     656                bbp_admin_repair_topic_voice_count();
     657                bbp_admin_repair_topic_hidden_reply_count();
     658                bbp_admin_repair_forum_topic_count();
     659                bbp_admin_repair_forum_reply_count();
     660                bbp_admin_repair_forum_hidden_reply_count();
     661
     662                foreach ( $metadata as $key => $value ) {
     663                        $this->assertSame( array( (string) $value ), get_post_meta( $post_id, $key, false ) );
     664                }
     665        }
     666
     667        /**
    514668         * @covers ::bbp_admin_repair_user_topic_count
    515          * @todo   Implement test_bbp_admin_repair_user_topic_count().
     669         * @ticket BBP2664
    516670         */
    517671        public function test_bbp_admin_repair_user_topic_count() {
    518                 // Remove the following lines when you implement this test.
    519                 $this->markTestIncomplete(
    520                         'This test has not been implemented yet.'
    521                 );
     672                $user_id = $this->factory->user->create();
     673
     674                $this->factory->topic->create_many( 2, array( 'post_author' => $user_id ) );
     675                $this->factory->topic->create( array(
     676                        'post_author' => $user_id,
     677                        'post_status' => bbp_get_spam_status_id(),
     678                ) );
     679                bbp_update_user_topic_count( $user_id, 9 );
     680                $this->assertSame( 9, bbp_get_user_topic_count( $user_id, true ) );
     681
     682                $result = bbp_admin_repair_user_topic_count();
     683
     684                $this->assertSame( 0, $result[0] );
     685                $this->assertSame( 2, bbp_get_user_topic_count( $user_id, true ) );
    522686        }
    523687
    524688        /**
    525689         * @covers ::bbp_admin_repair_user_reply_count
    526          * @todo   Implement test_bbp_admin_repair_user_reply_count().
     690         * @ticket BBP2664
    527691         */
    528692        public function test_bbp_admin_repair_user_reply_count() {
    529                 // Remove the following lines when you implement this test.
    530                 $this->markTestIncomplete(
    531                         'This test has not been implemented yet.'
    532                 );
     693                $user_id = $this->factory->user->create();
     694                $topic_id = $this->factory->topic->create();
     695
     696                $this->factory->reply->create_many( 2, array(
     697                        'post_author' => $user_id,
     698                        'post_parent' => $topic_id,
     699                ) );
     700                $this->factory->reply->create( array(
     701                        'post_author' => $user_id,
     702                        'post_parent' => $topic_id,
     703                        'post_status' => bbp_get_spam_status_id(),
     704                ) );
     705                bbp_update_user_reply_count( $user_id, 9 );
     706                $this->assertSame( 9, bbp_get_user_reply_count( $user_id, true ) );
     707
     708                $result = bbp_admin_repair_user_reply_count();
     709
     710                $this->assertSame( 0, $result[0] );
     711                $this->assertSame( 2, bbp_get_user_reply_count( $user_id, true ) );
     712        }
     713
     714        /**
     715         * @covers ::bbp_admin_repair_user_topic_count
     716         * @covers ::bbp_admin_repair_user_reply_count
     717         * @ticket BBP2664
     718         */
     719        public function test_user_count_repairs_clear_stale_counts_without_public_posts() {
     720                $user_id = $this->factory->user->create();
     721
     722                bbp_update_user_topic_count( $user_id, 7 );
     723                bbp_update_user_reply_count( $user_id, 8 );
     724                $this->assertSame( 7, bbp_get_user_topic_count( $user_id, true ) );
     725                $this->assertSame( 8, bbp_get_user_reply_count( $user_id, true ) );
     726
     727                $topic_result = bbp_admin_repair_user_topic_count();
     728                $reply_result = bbp_admin_repair_user_reply_count();
     729
     730                $this->assertSame( 0, $topic_result[0] );
     731                $this->assertSame( 0, $reply_result[0] );
     732                $this->assertSame( 0, bbp_get_user_topic_count( $user_id, true ) );
     733                $this->assertSame( 0, bbp_get_user_reply_count( $user_id, true ) );
    533734        }
    534735
  • trunk/tests/phpunit/testcases/forums/functions/counts.php

    r7449 r7453  
    1111
    1212        /**
     13         * @covers ::bbp_get_countable_forum_statuses
     14         */
     15        public function test_bbp_get_countable_forum_statuses() {
     16                $include_private = function ( $statuses ) {
     17                        $statuses[] = bbp_get_private_status_id();
     18                        return $statuses;
     19                };
     20
     21                add_filter( 'bbp_get_public_forum_statuses', $include_private );
     22                $statuses = bbp_get_countable_forum_statuses();
     23                remove_filter( 'bbp_get_public_forum_statuses', $include_private );
     24
     25                $this->assertSame(
     26                        array(
     27                                bbp_get_public_status_id(),
     28                                bbp_get_private_status_id(),
     29                                bbp_get_hidden_status_id()
     30                        ),
     31                        $statuses
     32                );
     33        }
     34
     35        /**
    1336         * Generic function to test the forum counts with a new topic
    1437         *
    … …  
    417440        /**
    418441         * @covers ::bbp_update_forum_subforum_count
     442         * @ticket BBP2649
    419443         */
    420444        public function test_bbp_update_forum_subforum_count() {
    … …  
    440464                $count = bbp_get_forum_subforum_count( $f1, true );
    441465                $this->assertSame( 3, $count );
     466        }
     467
     468        /**
     469         * @covers ::bbp_update_forum_subforum_count
     470         * @ticket BBP2649
     471         */
     472        public function test_bbp_update_forum_subforum_count_includes_supported_visibilities() {
     473                $forum_id = $this->factory->forum->create();
     474
     475                foreach ( array( bbp_get_public_status_id(), bbp_get_private_status_id(), bbp_get_hidden_status_id() ) as $status ) {
     476                        $subforum_id = $this->factory->forum->create( array(
     477                                'post_parent' => $forum_id,
     478                                'post_status' => $status,
     479                        ) );
     480                }
     481
     482                // Count the canonical hierarchy once, even if relationship meta is duplicated
     483                add_post_meta( $subforum_id, '_bbp_forum_id', $forum_id );
     484
     485                $trashed_id = $this->factory->forum->create( array( 'post_parent' => $forum_id ) );
     486                wp_trash_post( $trashed_id );
     487
     488                bbp_update_forum_subforum_count( $forum_id );
     489
     490                $this->assertSame( 3, bbp_get_forum_subforum_count( $forum_id, true ) );
     491        }
     492
     493        /**
     494         * @covers ::bbp_update_parent_forum_subforum_count
     495         * @covers ::bbp_update_forum_subforum_count_on_transition_post_status
     496         * @ticket BBP2649
     497         */
     498        public function test_subforum_count_tracks_trash_restore_and_deletion() {
     499                $forum_id    = $this->factory->forum->create();
     500                $subforum_id = $this->factory->forum->create( array( 'post_parent' => $forum_id ) );
     501
     502                $this->assertSame( 1, bbp_get_forum_subforum_count( $forum_id, true ) );
     503
     504                wp_trash_post( $subforum_id );
     505                $this->assertSame( 0, bbp_get_forum_subforum_count( $forum_id, true ) );
     506
     507                wp_untrash_post( $subforum_id );
     508                $this->assertSame( 1, bbp_get_forum_subforum_count( $forum_id, true ) );
     509
     510                wp_update_post( array(
     511                        'ID'          => $subforum_id,
     512                        'post_status' => 'draft',
     513                ) );
     514                $this->assertSame( 0, bbp_get_forum_subforum_count( $forum_id, true ) );
     515
     516                wp_publish_post( $subforum_id );
     517                $this->assertSame( 1, bbp_get_forum_subforum_count( $forum_id, true ) );
     518
     519                wp_delete_post( $subforum_id, true );
     520                $this->assertSame( 0, bbp_get_forum_subforum_count( $forum_id, true ) );
     521        }
     522
     523        /**
     524         * @covers ::bbp_reparent_forum_subforums
     525         * @covers ::bbp_update_parent_forum_subforum_count
     526         * @ticket BBP2649
     527         */
     528        public function test_deleting_forum_reparents_child_forum_metadata_and_count() {
     529                $grandparent_id = $this->factory->forum->create();
     530                $parent_id      = $this->factory->forum->create( array( 'post_parent' => $grandparent_id ) );
     531                $child_id       = $this->factory->forum->create( array( 'post_parent' => $parent_id ) );
     532
     533                wp_delete_post( $parent_id, true );
     534
     535                $this->assertSame( $grandparent_id, (int) get_post_field( 'post_parent', $child_id ) );
     536                $this->assertSame( $grandparent_id, bbp_get_forum_parent_id( $child_id ) );
     537                $this->assertSame( 1, bbp_get_forum_subforum_count( $grandparent_id, true ) );
     538        }
     539
     540        /**
     541         * @covers ::bbp_update_forum_subforum_counts_on_post_updated
     542         * @ticket BBP2649
     543         */
     544        public function test_subforum_count_tracks_parent_changes() {
     545                $old_parent_id = $this->factory->forum->create();
     546                $new_parent_id = $this->factory->forum->create();
     547                $subforum_id   = $this->factory->forum->create( array( 'post_parent' => $old_parent_id ) );
     548
     549                $this->assertSame( 1, bbp_get_forum_subforum_count( $old_parent_id, true ) );
     550                $this->assertSame( 0, bbp_get_forum_subforum_count( $new_parent_id, true ) );
     551
     552                wp_update_post( array(
     553                        'ID'          => $subforum_id,
     554                        'post_parent' => $new_parent_id,
     555                ) );
     556
     557                $this->assertSame( 0, bbp_get_forum_subforum_count( $old_parent_id, true ) );
     558                $this->assertSame( 1, bbp_get_forum_subforum_count( $new_parent_id, true ) );
     559        }
     560
     561        /**
     562         * @covers ::bbp_update_forum_subforum_counts_on_post_updated
     563         * @ticket BBP2649
     564         */
     565        public function test_subforum_count_tracks_post_type_changes() {
     566                $parent_id   = $this->factory->forum->create();
     567                $subforum_id = $this->factory->forum->create( array( 'post_parent' => $parent_id ) );
     568
     569                $this->assertSame( 1, bbp_get_forum_subforum_count( $parent_id, true ) );
     570
     571                wp_update_post( array(
     572                        'ID'        => $subforum_id,
     573                        'post_type' => 'post',
     574                ) );
     575                $this->assertSame( 0, bbp_get_forum_subforum_count( $parent_id, true ) );
     576
     577                wp_update_post( array(
     578                        'ID'        => $subforum_id,
     579                        'post_type' => bbp_get_forum_post_type(),
     580                ) );
     581                $this->assertSame( $parent_id, bbp_get_forum_parent_id( $subforum_id ) );
     582                $this->assertSame( 1, bbp_get_forum_subforum_count( $parent_id, true ) );
    442583        }
    443584
Note: See TracChangeset for help on using the changeset viewer.