Skip to:
Content

bbPress.org

Changeset 7454


Ignore:
Timestamp:
09/07/2026 05:02:50 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.

Merges r7453 to 2.6.

In 2.6, for 2.6.16.

See #2533, #2586, #2649, #2664, #3678.

Location:
branches/2.6
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • branches/2.6/CHANGELOG.md

    r7451 r7454  
    1919- Added Block Theme support while continuing to use bbPress PHP templates for
    2020  classic and hybrid themes.
     21- Added `bbp_post_updated` with the post ID and before/after post objects.
    2122
    2223### Changed
    … …  
    3233- Kept public, hidden, and author contribution counts accurate when topics and
    3334  replies change moderation status or are permanently deleted.
     35- Kept subforum counts accurate across public, private, hidden, trashed, moved,
     36  restored, and permanently deleted forums.
     37- Corrected count repair tools to clear stale metadata caches, exclude anonymous
     38  authors from voice counts, honor reply-status filters, rebuild private and
     39  hidden forums, and preserve similarly named metadata on other post types.
    3440- Prevented BuddyPress integration from loading bbPress translations before
    3541  WordPress initialization.
  • branches/2.6/src/includes/admin/tools/repair.php

    r7436 r7454  
    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/**
    … …  
    225247
    226248        // Post types and status
    227         $tpt = bbp_get_topic_post_type();
    228         $rpt = bbp_get_reply_post_type();
    229         $pps = bbp_get_public_status_id();
    230         $cps = bbp_get_closed_status_id();
     249        $tpt                = bbp_get_topic_post_type();
     250        $rpt                = bbp_get_reply_post_type();
     251        $sql_topic_statuses = "'" . implode( "','", bbp_get_public_topic_statuses() ) . "'";
     252        $sql_reply_statuses = "'" . implode( "','", bbp_get_public_reply_statuses() ) . "'";
    231253
    232254        // Delete the meta key _bbp_reply_count for each topic
    … …  
    236258                                                AND `postmeta`.`meta_key` = '_bbp_reply_count'";
    237259
    238         if ( is_wp_error( $bbp_db->query( $sql_delete ) ) ) {
     260        if ( false === $bbp_db->query( $sql_delete ) ) {
    239261                return array( 1, sprintf( $statement, $result ) );
    240262        }
    … …  
    246268                                        LEFT JOIN `{$bbp_db->posts}` as `replies`
    247269                                                ON  `replies`.`post_parent` = `topics`.`ID`
    248                                                 AND `replies`.`post_status` = '{$pps}'
     270                                                AND `replies`.`post_status` IN ( {$sql_reply_statuses} )
    249271                                                AND `replies`.`post_type`   = '{$rpt}'
    250272                                WHERE `topics`.`post_type` = '{$tpt}'
    251                                         AND `topics`.`post_status` IN ( '{$pps}', '{$cps}' )
     273                                        AND `topics`.`post_status` IN ( {$sql_topic_statuses} )
    252274                                GROUP BY `topics`.`ID`)";
    253275
    254         if ( is_wp_error( $bbp_db->query( $sql ) ) ) {
     276        if ( false === $bbp_db->query( $sql ) ) {
    255277                return array( 2, sprintf( $statement, $result ) );
    256278        }
     279
     280        bbp_admin_repair_clean_post_meta_cache( $tpt );
    257281
    258282        return array( 0, sprintf( $statement, esc_html__( 'Complete!', 'bbpress' ) ) );
    … …  
    274298        $result    = esc_html__( 'Failed!', 'bbpress' );
    275299
    276         $sql_delete = "DELETE FROM {$bbp_db->postmeta} WHERE meta_key IN ('_bbp_voice_count', '_bbp_engagement')";
    277         if ( is_wp_error( $bbp_db->query( $sql_delete ) ) ) {
     300        $tpt = bbp_get_topic_post_type();
     301
     302        $sql_delete = "DELETE `postmeta` FROM `{$bbp_db->postmeta}` AS `postmeta`
     303                                                LEFT JOIN `{$bbp_db->posts}` AS `posts` ON `posts`.`ID` = `postmeta`.`post_id`
     304                                                WHERE `posts`.`post_type` = '{$tpt}'
     305                                                AND `postmeta`.`meta_key` IN ( '_bbp_voice_count', '_bbp_engagement' )";
     306        if ( false === $bbp_db->query( $sql_delete ) ) {
    278307                return array( 1, sprintf( $statement, $result ) );
    279308        }
    280309
    281310        // Post types and status
    282         $tpt = bbp_get_topic_post_type();
    283         $rpt = bbp_get_reply_post_type();
    284         $pps = bbp_get_public_status_id();
    285         $cps = bbp_get_closed_status_id();
     311        $rpt                = bbp_get_reply_post_type();
     312        $sql_topic_statuses = "'" . implode( "','", bbp_get_public_topic_statuses() ) . "'";
     313        $sql_reply_statuses = "'" . implode( "','", bbp_get_public_reply_statuses() ) . "'";
    286314
    287315        $engagements_sql = $bbp_db->prepare( "INSERT INTO {$bbp_db->postmeta} (post_id, meta_key, meta_value) (
    288                 SELECT postmeta.meta_value, '_bbp_engagement', posts.post_author
     316                SELECT topics.ID, '_bbp_engagement', posts.post_author
    289317                        FROM {$bbp_db->posts} AS posts
    290                         LEFT JOIN {$bbp_db->postmeta} AS postmeta
     318                        INNER JOIN {$bbp_db->postmeta} AS postmeta
    291319                                ON posts.ID = postmeta.post_id
    292320                                AND postmeta.meta_key = '_bbp_topic_id'
    293                         WHERE posts.post_type IN (%s, %s)
    294                                 AND posts.post_status IN (%s, %s)
    295                         GROUP BY postmeta.meta_value, posts.post_author)",
     321                        INNER JOIN {$bbp_db->posts} AS topics
     322                                ON topics.ID = postmeta.meta_value
     323                                AND topics.post_type = %s
     324                        WHERE (
     325                                        ( posts.post_type = %s AND posts.post_status IN ({$sql_topic_statuses}) )
     326                                        OR ( posts.post_type = %s AND posts.post_status IN ({$sql_reply_statuses}) )
     327                                )
     328                                AND posts.post_author > 0
     329                        GROUP BY topics.ID, posts.post_author)",
    296330                $tpt,
    297                 $rpt,
    298                 $pps,
    299                 $cps
     331                $tpt,
     332                $rpt
    300333        );
    301334
    302         if ( is_wp_error( $bbp_db->query( $engagements_sql ) ) ) {
     335        if ( false === $bbp_db->query( $engagements_sql ) ) {
    303336                return array( 2, sprintf( $statement, $result ) );
    304337        }
    305338
    306         $voice_count_sql = "INSERT INTO {$bbp_db->postmeta} (post_id, meta_key, meta_value) (
    307                 SELECT post_id, '_bbp_voice_count', COUNT(DISTINCT meta_value)
    308                         FROM {$bbp_db->postmeta}
    309                         WHERE meta_key = '_bbp_engagement'
    310                         GROUP BY post_id)";
    311 
    312         if ( is_wp_error( $bbp_db->query( $voice_count_sql ) ) ) {
     339        $voice_count_sql = $bbp_db->prepare( "INSERT INTO {$bbp_db->postmeta} (post_id, meta_key, meta_value) (
     340                SELECT engagements.post_id, '_bbp_voice_count', COUNT(DISTINCT engagements.meta_value)
     341                        FROM {$bbp_db->postmeta} AS engagements
     342                        INNER JOIN {$bbp_db->posts} AS topics
     343                                ON topics.ID = engagements.post_id
     344                                AND topics.post_type = %s
     345                        WHERE engagements.meta_key = '_bbp_engagement'
     346                        GROUP BY engagements.post_id)",
     347                $tpt
     348        );
     349
     350        if ( false === $bbp_db->query( $voice_count_sql ) ) {
    313351                return array( 3, sprintf( $statement, $result ) );
    314352        }
     353
     354        bbp_admin_repair_clean_post_meta_cache( $tpt );
    315355
    316356        return array( 0, sprintf( $statement, esc_html__( 'Complete!', 'bbpress' ) ) );
    … …  
    332372        $result    = esc_html__( 'Failed!', 'bbpress' );
    333373
    334         $sql_delete = "DELETE FROM `{$bbp_db->postmeta}` WHERE `meta_key` = '_bbp_reply_count_hidden'";
    335         if ( is_wp_error( $bbp_db->query( $sql_delete ) ) ) {
    336                 return array( 1, sprintf( $statement, $result ) );
    337         }
    338 
    339         // Post types and status
     374        $tpt = bbp_get_topic_post_type();
    340375        $rpt = bbp_get_reply_post_type();
    341         $sta = bbp_get_non_public_topic_statuses();
     376
     377        $sql_delete = "DELETE `postmeta` FROM `{$bbp_db->postmeta}` AS `postmeta`
     378                                                LEFT JOIN `{$bbp_db->posts}` AS `posts` ON `posts`.`ID` = `postmeta`.`post_id`
     379                                                WHERE `posts`.`post_type` = '{$tpt}'
     380                                                AND `postmeta`.`meta_key` = '_bbp_reply_count_hidden'";
     381        if ( false === $bbp_db->query( $sql_delete ) ) {
     382                return array( 1, sprintf( $statement, $result ) );
     383        }
     384
     385        // Reply statuses
     386        $sta = bbp_get_non_public_reply_statuses();
    342387
    343388        // SQL
    344389        $sql_status = "'" . implode( "','", $sta ) . "'";
    345390
    346         $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`)";
    347         if ( is_wp_error( $bbp_db->query( $sql ) ) ) {
     391        $sql = "INSERT INTO `{$bbp_db->postmeta}` (`post_id`, `meta_key`, `meta_value`) (
     392                SELECT `topics`.`ID`, '_bbp_reply_count_hidden', COUNT(`replies`.`ID`) AS `meta_value`
     393                        FROM `{$bbp_db->posts}` AS `replies`
     394                        INNER JOIN `{$bbp_db->posts}` AS `topics`
     395                                ON `topics`.`ID` = `replies`.`post_parent`
     396                                AND `topics`.`post_type` = '{$tpt}'
     397                        WHERE `replies`.`post_type` = '{$rpt}'
     398                                AND `replies`.`post_status` IN ({$sql_status})
     399                        GROUP BY `topics`.`ID`)";
     400        if ( false === $bbp_db->query( $sql ) ) {
    348401                return array( 2, sprintf( $statement, $result ) );
    349402        }
     403
     404        bbp_admin_repair_clean_post_meta_cache( $tpt );
    350405
    351406        return array( 0, sprintf( $statement, esc_html__( 'Complete!', 'bbpress' ) ) );
    … …  
    367422        $result    = esc_html__( 'Failed!', 'bbpress' );
    368423
    369         $sql_delete = "DELETE FROM {$bbp_db->postmeta} WHERE meta_key IN ( '_bbp_topic_count', '_bbp_total_topic_count', '_bbp_topic_count_hidden' )";
    370         if ( is_wp_error( $bbp_db->query( $sql_delete ) ) ) {
     424        $fpt = bbp_get_forum_post_type();
     425
     426        $sql_delete = "DELETE `postmeta` FROM `{$bbp_db->postmeta}` AS `postmeta`
     427                                                LEFT JOIN `{$bbp_db->posts}` AS `posts` ON `posts`.`ID` = `postmeta`.`post_id`
     428                                                WHERE `posts`.`post_type` = '{$fpt}'
     429                                                AND `postmeta`.`meta_key` IN ( '_bbp_topic_count', '_bbp_total_topic_count', '_bbp_topic_count_hidden' )";
     430        if ( false === $bbp_db->query( $sql_delete ) ) {
    371431                return array( 1, sprintf( $statement, $result ) );
    372432        }
    … …  
    374434        $forums = get_posts(
    375435                array(
    376                         'post_type'   => bbp_get_forum_post_type(),
     436                        'fields'      => 'ids',
     437                        'post_type'   => $fpt,
     438                        'post_status' => bbp_get_countable_forum_statuses(),
    377439                        'numberposts' => -1
    378440                )
    … …  
    380442
    381443        if ( ! empty( $forums ) ) {
    382                 foreach ( $forums as $forum ) {
    383                         bbp_update_forum_topic_count( $forum->ID );
    384                         bbp_update_forum_topic_count_hidden( $forum->ID );
     444                foreach ( $forums as $forum_id ) {
     445                        bbp_update_forum_topic_count( $forum_id );
     446                        bbp_update_forum_topic_count_hidden( $forum_id );
    385447                }
    386448        } else {
    … …  
    465527                                                LEFT JOIN `{$bbp_db->posts}` AS `posts` ON `posts`.`ID` = `postmeta`.`post_id`
    466528                                                WHERE `posts`.`post_type` = '{$fpt}'
    467                                                 AND `postmeta`.`meta_key` = '_bbp_reply_count'
    468                                                 OR `postmeta`.`meta_key` = '_bbp_total_reply_count'";
    469 
    470         if ( is_wp_error( $bbp_db->query( $sql_delete ) ) ) {
     529                                                AND `postmeta`.`meta_key` IN ( '_bbp_reply_count', '_bbp_total_reply_count' )";
     530
     531        if ( false === $bbp_db->query( $sql_delete ) ) {
    471532                return array( 1, sprintf( $statement, $result ) );
    472533        }
    … …  
    475536        $forums = get_posts(
    476537                array(
    477                         'post_type'   => bbp_get_forum_post_type(),
     538                        'fields'      => 'ids',
     539                        'post_type'   => $fpt,
     540                        'post_status' => bbp_get_countable_forum_statuses(),
    478541                        'numberposts' => -1
    479542                )
    … …  
    481544
    482545        if ( ! empty( $forums ) ) {
    483                 foreach ( $forums as $forum ) {
    484                         bbp_update_forum_reply_count( $forum->ID );
     546                foreach ( $forums as $forum_id ) {
     547                        bbp_update_forum_reply_count( $forum_id );
    485548                }
    486549        } else {
    … …  
    514577                                                LEFT JOIN `{$bbp_db->posts}` AS `posts` ON `posts`.`ID` = `postmeta`.`post_id`
    515578                                                WHERE `posts`.`post_type` = '{$fpt}'
    516                                                 AND `postmeta`.`meta_key` = '_bbp_reply_count_hidden'
    517                                                 OR `postmeta`.`meta_key` = '_bbp_total_reply_count_hidden'";
    518 
    519         if ( is_wp_error( $bbp_db->query( $sql_delete ) ) ) {
     579                                                AND `postmeta`.`meta_key` IN ( '_bbp_reply_count_hidden', '_bbp_total_reply_count_hidden' )";
     580
     581        if ( false === $bbp_db->query( $sql_delete ) ) {
    520582                return array( 1, sprintf( $statement, $result ) );
    521583        }
    … …  
    524586        $forums = get_posts(
    525587                array(
    526                         'post_type'   => bbp_get_forum_post_type(),
     588                        'fields'      => 'ids',
     589                        'post_type'   => $fpt,
     590                        'post_status' => bbp_get_countable_forum_statuses(),
    527591                        'numberposts' => -1
    528592                )
    … …  
    530594
    531595        if ( ! empty( $forums ) ) {
    532                 foreach ( $forums as $forum ) {
    533                         bbp_update_forum_reply_count_hidden( $forum->ID );
     596                foreach ( $forums as $forum_id ) {
     597                        bbp_update_forum_reply_count_hidden( $forum_id );
    534598                }
    535599        } else {
    … …  
    557621        $sql_type    = bbp_get_topic_post_type();
    558622        $sql_status  = "'" . implode( "','", bbp_get_public_topic_statuses() ) . "'";
    559         $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`";
     623        $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`";
    560624        $insert_rows = $bbp_db->get_results( $sql_select );
    561625
    562         if ( is_wp_error( $insert_rows ) ) {
     626        if ( ! empty( $bbp_db->last_error ) ) {
    563627                return array( 1, sprintf( $statement, $result ) );
    564628        }
    565629
    566630        $key           = $bbp_db->prefix . '_bbp_topic_count';
     631        $user_ids      = (array) $bbp_db->get_col( "SELECT `user_id` FROM `{$bbp_db->usermeta}` WHERE `meta_key` = '{$key}'" );
    567632        $insert_values = array();
     633
     634        if ( ! empty( $bbp_db->last_error ) ) {
     635                return array( 2, sprintf( $statement, $result ) );
     636        }
     637
    568638        foreach ( $insert_rows as $insert_row ) {
    569                 $insert_values[] = "('{$insert_row->post_author}', '{$key}', '{$insert_row->_count}')";
    570         }
    571 
    572         if ( ! count( $insert_values ) ) {
    573                 return array( 2, sprintf( $statement, $result ) );
     639                $user_id         = (int) $insert_row->post_author;
     640                $user_ids[]      = $user_id;
     641                $insert_values[] = $bbp_db->prepare( '(%d, %s, %d)', $user_id, $key, (int) $insert_row->_count );
    574642        }
    575643
    576644        $sql_delete = "DELETE FROM `{$bbp_db->usermeta}` WHERE `meta_key` = '{$key}'";
    577         if ( is_wp_error( $bbp_db->query( $sql_delete ) ) ) {
     645        if ( false === $bbp_db->query( $sql_delete ) ) {
    578646                return array( 3, sprintf( $statement, $result ) );
    579647        }
    580648
     649        $insert_error = false;
    581650        foreach ( array_chunk( $insert_values, 10000 ) as $chunk ) {
    582651                $chunk = "\n" . implode( ",\n", $chunk );
    583652                $sql_insert = "INSERT INTO `{$bbp_db->usermeta}` (`user_id`, `meta_key`, `meta_value`) VALUES {$chunk}";
    584653
    585                 if ( is_wp_error( $bbp_db->query( $sql_insert ) ) ) {
    586                         return array( 4, sprintf( $statement, $result ) );
    587                 }
     654                if ( false === $bbp_db->query( $sql_insert ) ) {
     655                        $insert_error = true;
     656                        break;
     657                }
     658        }
     659
     660        // Clear cached counts for users affected by the direct database update
     661        $user_ids = wp_parse_id_list( $user_ids );
     662        if ( ! empty( $user_ids ) ) {
     663                wp_cache_delete_multiple( $user_ids, 'user_meta' );
     664        }
     665
     666        if ( $insert_error ) {
     667                return array( 4, sprintf( $statement, $result ) );
    588668        }
    589669
    … …  
    608688        $sql_type    = bbp_get_reply_post_type();
    609689        $sql_status  = "'" . implode( "','", bbp_get_public_reply_statuses() ) . "'";
    610         $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`";
     690        $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`";
    611691        $insert_rows = $bbp_db->get_results( $sql_select );
    612692
    613         if ( is_wp_error( $insert_rows ) ) {
     693        if ( ! empty( $bbp_db->last_error ) ) {
    614694                return array( 1, sprintf( $statement, $result ) );
    615695        }
    616696
    617697        $key           = $bbp_db->prefix . '_bbp_reply_count';
     698        $user_ids      = (array) $bbp_db->get_col( "SELECT `user_id` FROM `{$bbp_db->usermeta}` WHERE `meta_key` = '{$key}'" );
    618699        $insert_values = array();
     700
     701        if ( ! empty( $bbp_db->last_error ) ) {
     702                return array( 2, sprintf( $statement, $result ) );
     703        }
     704
    619705        foreach ( $insert_rows as $insert_row ) {
    620                 $insert_values[] = "('{$insert_row->post_author}', '{$key}', '{$insert_row->_count}')";
    621         }
    622 
    623         if ( ! count( $insert_values ) ) {
    624                 return array( 2, sprintf( $statement, $result ) );
     706                $user_id         = (int) $insert_row->post_author;
     707                $user_ids[]      = $user_id;
     708                $insert_values[] = $bbp_db->prepare( '(%d, %s, %d)', $user_id, $key, (int) $insert_row->_count );
    625709        }
    626710
    627711        $sql_delete = "DELETE FROM `{$bbp_db->usermeta}` WHERE `meta_key` = '{$key}'";
    628         if ( is_wp_error( $bbp_db->query( $sql_delete ) ) ) {
     712        if ( false === $bbp_db->query( $sql_delete ) ) {
    629713                return array( 3, sprintf( $statement, $result ) );
    630714        }
    631715
     716        $insert_error = false;
    632717        foreach ( array_chunk( $insert_values, 10000 ) as $chunk ) {
    633718                $chunk = "\n" . implode( ",\n", $chunk );
    634719                $sql_insert = "INSERT INTO `{$bbp_db->usermeta}` (`user_id`, `meta_key`, `meta_value`) VALUES {$chunk}";
    635720
    636                 if ( is_wp_error( $bbp_db->query( $sql_insert ) ) ) {
    637                         return array( 4, sprintf( $statement, $result ) );
    638                 }
     721                if ( false === $bbp_db->query( $sql_insert ) ) {
     722                        $insert_error = true;
     723                        break;
     724                }
     725        }
     726
     727        // Clear cached counts for users affected by the direct database update
     728        $user_ids = wp_parse_id_list( $user_ids );
     729        if ( ! empty( $user_ids ) ) {
     730                wp_cache_delete_multiple( $user_ids, 'user_meta' );
     731        }
     732
     733        if ( $insert_error ) {
     734                return array( 4, sprintf( $statement, $result ) );
    639735        }
    640736
  • branches/2.6/src/includes/core/actions.php

    r7451 r7454  
    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/**
    … …  
    179180add_action( 'trashed_post',   'bbp_trashed_forum'   );
    180181add_action( 'untrashed_post', 'bbp_untrashed_forum' );
    181 add_action( 'deleted_post',   'bbp_deleted_forum'  );
     182add_action( 'deleted_post',   'bbp_deleted_forum', 10, 2 );
    182183
    183184// Auto trash/untrash/delete a forums topics
    … …  
    273274add_action( 'bbp_unapproved_reply', 'bbp_update_reply_walker' );
    274275
    275 // Update counts when topics and replies are created or change status
     276// Update counts from persisted post status changes
    276277add_action( 'bbp_transition_post_status', 'bbp_update_counts_on_transition_post_status', 10, 3 );
     278add_action( 'bbp_transition_post_status', 'bbp_update_forum_subforum_count_on_transition_post_status', 10, 3 );
     279
     280// Update parent subforum counts after updates and permanent deletion
     281add_action( 'bbp_deleted_forum',   'bbp_reparent_forum_subforums', 9, 2 );
     282add_action( 'bbp_deleted_forum',   'bbp_update_parent_forum_subforum_count', 10, 2 );
     283add_action( 'bbp_post_updated',    'bbp_update_forum_subforum_counts_on_post_updated', 10, 3 );
    277284
    278285// Update user topic & reply counts
  • branches/2.6/src/includes/core/sub-actions.php

    r7081 r7454  
    288288        // Do the action
    289289        do_action( 'bbp_transition_post_status', $new_status, $old_status, $post );
     290}
     291
     292/**
     293 * Add the bbPress-specific post updated action.
     294 *
     295 * @since 2.6.16
     296 *
     297 * @param int     $post_id     Post ID.
     298 * @param WP_Post $post_after  Post object following the update.
     299 * @param WP_Post $post_before Post object before the update.
     300 */
     301function bbp_post_updated( $post_id = 0, $post_after = false, $post_before = false ) {
     302        $post_types = bbp_get_post_types();
     303
     304        // Bail if neither version is a bbPress post type
     305        if ( ! in_array( $post_after->post_type, $post_types, true ) && ! in_array( $post_before->post_type, $post_types, true ) ) {
     306                return;
     307        }
     308
     309        // Do the action
     310        do_action( 'bbp_post_updated', $post_id, $post_after, $post_before );
    290311}
    291312
  • branches/2.6/src/includes/forums/functions.php

    r7436 r7454  
    18371837 *
    18381838 * @since 2.0.0 bbPress (r2625)
    1839  *
    1840  * @param int $forum_id Optional. Forum id
    1841  * @param int $subforums Optional. Number of subforums
    1842  * @return bool True on success, false on failure
    1843  */
     1839 * @since 2.6.16 Count supported forum visibilities from the post hierarchy.
     1840 *
     1841 * @param int      $forum_id Optional. Forum ID.
     1842 * @param int|bool $subforums Optional. Number of subforums, or false to query.
     1843         * @return int|false Number of subforums, or false on query failure.
     1844*/
    18441845function bbp_update_forum_subforum_count( $forum_id = 0, $subforums = false ) {
    18451846        $forum_id = bbp_get_forum_id( $forum_id );
    18461847
    18471848        // Maybe query for counts
    1848         $subforums = ! is_int( $subforums )
    1849                 ? bbp_get_public_child_count( $forum_id, bbp_get_forum_post_type() )
    1850                 : (int) $subforums;
     1849        if ( ! is_int( $subforums ) ) {
     1850                $bbp_db    = bbp_db();
     1851                $post_type = bbp_get_forum_post_type();
     1852                $statuses  = bbp_get_countable_forum_statuses();
     1853
     1854                if ( ! empty( $statuses ) ) {
     1855                        $placeholders = implode( ', ', array_fill( 0, count( $statuses ), '%s' ) );
     1856                        $sql           = "SELECT COUNT(*) FROM {$bbp_db->posts} WHERE post_parent = %d AND post_type = %s AND post_status IN ({$placeholders})";
     1857                        $query         = $bbp_db->prepare( $sql, array_merge( array( $forum_id, $post_type ), $statuses ) );
     1858                        $subforums     = $bbp_db->get_var( $query );
     1859
     1860                        // Bail if the count query failed
     1861                        if ( ! empty( $bbp_db->last_error ) ) {
     1862                                return false;
     1863                        }
     1864
     1865                        $subforums = bbp_number_not_negative( $subforums );
     1866                } else {
     1867                        $subforums = 0;
     1868                }
     1869        }
    18511870
    18521871        update_post_meta( $forum_id, '_bbp_forum_subforum_count', $subforums );
    … …  
    18571876
    18581877/**
    1859  * Adjust the total topic count of a forum
    1860  *
     1878 * Synchronize child-forum metadata after WordPress reparents deleted children.
     1879 *
     1880 * @since 2.6.16
     1881 *
     1882 * @param int     $forum_id Deleted forum ID.
     1883 * @param WP_Post $forum    Deleted forum post object.
     1884 */
     1885function bbp_reparent_forum_subforums( $forum_id = 0, $forum = false ) {
     1886        $bbp_db    = bbp_db();
     1887        $post_type = bbp_get_forum_post_type();
     1888        $meta_key  = '_bbp_forum_id';
     1889        $sql       = "SELECT posts.ID
     1890                FROM {$bbp_db->posts} AS posts
     1891                INNER JOIN {$bbp_db->postmeta} AS postmeta
     1892                        ON posts.ID = postmeta.post_id
     1893                        AND postmeta.meta_key = %s
     1894                WHERE posts.post_type = %s
     1895                        AND posts.post_parent = %d
     1896                        AND postmeta.meta_value = %d";
     1897        $query     = $bbp_db->prepare( $sql, $meta_key, $post_type, $forum->post_parent, $forum_id );
     1898
     1899        foreach ( wp_parse_id_list( $bbp_db->get_col( $query ) ) as $subforum_id ) {
     1900                bbp_update_forum_id( $subforum_id, $forum->post_parent );
     1901        }
     1902}
     1903
     1904/**
     1905 * Update a parent forum's subforum count after a child is permanently deleted.
     1906 *
     1907 * @since 2.6.16
     1908 *
     1909 * @param int          $forum_id Forum ID.
     1910 * @param WP_Post|bool $forum    Optional. Forum post object.
     1911 * @return int|false Updated subforum count, or false if there is no parent.
     1912 */
     1913function bbp_update_parent_forum_subforum_count( $forum_id = 0, $forum = false ) {
     1914        $forum = ( $forum instanceof WP_Post )
     1915                ? $forum
     1916                : get_post( $forum_id );
     1917
     1918        // Bail if the forum has no parent
     1919        if ( empty( $forum ) || empty( $forum->post_parent ) ) {
     1920                return false;
     1921        }
     1922
     1923        return bbp_update_forum_subforum_count( $forum->post_parent );
     1924}
     1925
     1926/**
     1927 * Update a parent forum's subforum count after a child changes count status.
     1928 *
     1929 * @since 2.6.16
     1930 *
     1931 * @param string  $new_status New post status.
     1932 * @param string  $old_status Old post status.
     1933 * @param WP_Post $forum      Forum post object.
     1934 * @return int|false Updated subforum count, or false when no update is needed.
     1935 */
     1936function bbp_update_forum_subforum_count_on_transition_post_status( $new_status = '', $old_status = '', $forum = false ) {
     1937
     1938        // Bail if this is not a child forum
     1939        if ( ( bbp_get_forum_post_type() !== $forum->post_type ) || empty( $forum->post_parent ) ) {
     1940                return false;
     1941        }
     1942
     1943        $statuses    = bbp_get_countable_forum_statuses();
     1944        $was_counted = in_array( $old_status, $statuses, true );
     1945        $is_counted  = in_array( $new_status, $statuses, true );
     1946
     1947        // Bail if subforum count membership did not change
     1948        if ( $was_counted === $is_counted ) {
     1949                return false;
     1950        }
     1951
     1952        return bbp_update_forum_subforum_count( $forum->post_parent );
     1953}
     1954
     1955/**
     1956 * Update subforum counts after a forum changes parent or post type.
     1957 *
     1958 * @since 2.6.16
     1959 *
     1960 * @param int     $forum_id     Forum ID.
     1961 * @param WP_Post $forum_after  Forum object following the update.
     1962 * @param WP_Post $forum_before Forum object before the update.
     1963 */
     1964function bbp_update_forum_subforum_counts_on_post_updated( $forum_id = 0, $forum_after = false, $forum_before = false ) {
     1965        $post_type    = bbp_get_forum_post_type();
     1966        $parent_moved = ( $forum_after->post_parent !== $forum_before->post_parent );
     1967
     1968        // Bail if forum hierarchy membership did not change
     1969        if ( ! $parent_moved && ( $forum_after->post_type === $forum_before->post_type ) ) {
     1970                return false;
     1971        }
     1972
     1973        // Synchronize parent metadata when the updated post remains a forum
     1974        if ( ( $post_type === $forum_after->post_type ) && ( $parent_moved || ( $post_type !== $forum_before->post_type ) ) ) {
     1975                bbp_update_forum_id( $forum_id, $forum_after->post_parent );
     1976        }
     1977
     1978        $statuses    = bbp_get_countable_forum_statuses();
     1979        $was_counted = ( $post_type === $forum_before->post_type ) && in_array( $forum_before->post_status, $statuses, true );
     1980        $is_counted  = ( $post_type === $forum_after->post_type ) && in_array( $forum_after->post_status, $statuses, true );
     1981
     1982        // Include the previous parent when it formerly contained this subforum
     1983        $parent_ids = $was_counted
     1984                ? array( $forum_before->post_parent )
     1985                : array();
     1986
     1987        // Include the new parent when it now contains this subforum
     1988        if ( $is_counted ) {
     1989                $parent_ids[] = $forum_after->post_parent;
     1990        }
     1991
     1992        // Recount each affected parent once
     1993        foreach ( array_filter( array_unique( $parent_ids ) ) as $parent_id ) {
     1994                bbp_update_forum_subforum_count( $parent_id );
     1995        }
     1996}
     1997
     1998/**
     1999 * Adjust the total topic count of a forum.
     2000*
    18612001 * @since 2.0.0 bbPress (r2464)
    18622002 *
    … …  
    22192359        // Filter & return
    22202360        return (array) apply_filters( 'bbp_get_non_public_forum_statuses', $statuses );
     2361}
     2362
     2363/**
     2364 * Return forum statuses included in forum and subforum counts.
     2365 *
     2366 * @since 2.6.16
     2367 *
     2368 * @return array
     2369 */
     2370function bbp_get_countable_forum_statuses() {
     2371        $statuses = array_values( array_unique( array_merge( bbp_get_public_forum_statuses(), bbp_get_non_public_forum_statuses() ) ) );
     2372
     2373        // Filter & return
     2374        return (array) apply_filters( 'bbp_get_countable_forum_statuses', $statuses );
    22212375}
    22222376
    … …  
    29853139 * @since 2.1.0 bbPress (r3668)
    29863140 * @since 2.6.0 bbPress (r6526) Not recommend for usage
    2987  */
    2988 function bbp_deleted_forum( $forum_id = 0 ) {
     3141 * @since 2.6.16 Added the `$forum` parameter and passed it to the action.
     3142 *
     3143 * @param int          $forum_id Forum ID.
     3144 * @param WP_Post|bool $forum    Optional. Deleted forum post object.
     3145 * @return false|null False if the post is not a forum, otherwise null.
     3146 */
     3147function bbp_deleted_forum( $forum_id = 0, $forum = false ) {
     3148        $forum_id = bbp_get_forum_id( $forum_id );
     3149        $forum    = ( $forum instanceof WP_Post )
     3150                ? $forum
     3151                : get_post( $forum_id );
     3152
     3153        if ( empty( $forum_id ) || ! $forum || ( bbp_get_forum_post_type() !== $forum->post_type ) ) {
     3154                return false;
     3155        }
     3156
     3157        do_action( 'bbp_deleted_forum', $forum_id, $forum );
     3158}
     3159
     3160/**
     3161 * Called after trashing a forum
     3162 *
     3163 * @since 2.1.0 bbPress (r3668)
     3164 */
     3165function bbp_trashed_forum( $forum_id = 0 ) {
    29893166        $forum_id = bbp_get_forum_id( $forum_id );
    29903167
    … …  
    29933170        }
    29943171
    2995         do_action( 'bbp_deleted_forum', $forum_id );
    2996 }
    2997 
    2998 /**
    2999  * Called after trashing a forum
     3172        do_action( 'bbp_trashed_forum', $forum_id );
     3173}
     3174
     3175/**
     3176 * Called after untrashing a forum
    30003177 *
    30013178 * @since 2.1.0 bbPress (r3668)
    30023179 */
    3003 function bbp_trashed_forum( $forum_id = 0 ) {
     3180function bbp_untrashed_forum( $forum_id = 0 ) {
    30043181        $forum_id = bbp_get_forum_id( $forum_id );
    30053182
    … …  
    30083185        }
    30093186
    3010         do_action( 'bbp_trashed_forum', $forum_id );
    3011 }
    3012 
    3013 /**
    3014  * Called after untrashing a forum
    3015  *
    3016  * @since 2.1.0 bbPress (r3668)
    3017  */
    3018 function bbp_untrashed_forum( $forum_id = 0 ) {
    3019         $forum_id = bbp_get_forum_id( $forum_id );
    3020 
    3021         if ( empty( $forum_id ) || ! bbp_is_forum( $forum_id ) ) {
    3022                 return false;
    3023         }
    3024 
    30253187        do_action( 'bbp_untrashed_forum', $forum_id );
    30263188}
  • branches/2.6/src/readme.txt

    r7416 r7454  
    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.
  • branches/2.6/tests/phpunit/testcases/admin/tools.php

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

    r7451 r7454  
    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.