Skip to:
Content

bbPress.org

Changeset 7478


Ignore:
Timestamp:
09/10/2026 09:23:18 PM (2 months ago)
Author:
johnjamesjacoby
Message:

Counts: avoid forum-wide reply recounts.

Apply a topic's stored public reply count as an atomic forum difference when the topic enters or leaves the public status set. Add a short-circuit filter for integrations that replace the complete persisted transition count operation.

Add coverage for custom hidden statuses, moderation cascades, and the replacement lifecycle.

Merges [7475] to 2.6.

In 2.6, for 2.6.16.

See #3678.

Location:
branches/2.6
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • branches/2.6/CHANGELOG.md

    r7474 r7478  
    2525- Updated the 2.6 build, lint, test, translation, and installed-package smoke
    2626  test tooling.
     27- Moved built-in topic, reply, forum, and user count synchronization to
     28  `bbp_transition_post_status`. Existing creation and moderation actions still
     29  fire, but no longer host bbPress's core count callbacks. Topic transitions
     30  apply the stored reply-count difference to forum aggregates instead of
     31  recounting every reply. Extensions that need finalized counts should use the
     32  transition action at priority 11 or later; permanent deletion continues
     33  through `bbp_deleted_topic` and `bbp_deleted_reply`.
    2734
    2835### Security
  • branches/2.6/src/includes/common/functions.php

    r7468 r7478  
    233233function bbp_update_counts_on_transition_post_status( $new_status = '', $old_status = '', $post = false ) {
    234234
     235        /**
     236         * Short-circuits count updates for a persisted post-status transition.
     237         *
     238         * Returning a non-null value prevents the normal topic and reply count
     239         * updates. This allows integrations with custom post-status lifecycles or
     240         * count storage to replace the complete transition operation.
     241         *
     242         * @since 2.6.16
     243         *
     244         * @param null|bool $check      Whether to short-circuit count updates.
     245         * @param string    $new_status New post status.
     246         * @param string    $old_status Old post status.
     247         * @param WP_Post   $post       Post object.
     248         */
     249        $check = apply_filters( 'bbp_pre_update_counts_on_transition_post_status', null, $new_status, $old_status, $post );
     250        if ( null !== $check ) {
     251                return (bool) $check;
     252        }
     253
    235254        // Bail if the status did not change
    236255        if ( $new_status === $old_status ) {
    … …  
    270289                        }
    271290
    272                         // Recount replies after their parent topic crosses the public boundary
     291                        // Apply every public reply when its topic crosses the public boundary
    273292                        if ( ! $is_new && ! empty( $forum_id ) && ! empty( $public_difference ) ) {
    274                                 bbp_update_forum_reply_count( $forum_id, true );
     293                                $reply_count      = bbp_get_topic_reply_count( $post->ID, true );
     294                                $reply_difference = $reply_count * $public_difference;
     295
     296                                if ( ! empty( $reply_difference ) ) {
     297                                        bbp_bump_forum_reply_count( $forum_id, $reply_difference );
     298                                }
    275299                        }
    276300                }
  • branches/2.6/src/readme.txt

    r7472 r7478  
    4848= Count updates in 2.6.16 =
    4949
    50 bbPress 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.
     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. When a topic crosses the public status boundary, its stored public reply count is applied to the forum aggregate instead of recounting every reply. The action receives the new status, old status, and `WP_Post` object. The `bbp_pre_update_counts_on_transition_post_status` filter can short-circuit the complete topic or reply count operation for custom status lifecycles or count storage.
    5151
    5252The 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.
  • branches/2.6/tests/phpunit/testcases/forums/functions/counts.php

    r7468 r7478  
    300300
    301301        /**
     302         * @covers ::bbp_update_counts_on_transition_post_status
     303         * @ticket BBP3678
     304         */
     305        public function test_bbp_status_transition_counts_can_be_short_circuited() {
     306                $user_id  = $this->factory->user->create();
     307                $forum_id = $this->factory->forum->create();
     308                $topic_id = $this->factory->topic->create( array(
     309                        'post_author' => $user_id,
     310                        'post_parent' => $forum_id,
     311                        'topic_meta'  => array( 'forum_id' => $forum_id ),
     312                ) );
     313                $this->factory->reply->create( array(
     314                        'post_parent' => $topic_id,
     315                        'reply_meta'  => array(
     316                                'forum_id' => $forum_id,
     317                                'topic_id' => $topic_id,
     318                        ),
     319                ) );
     320
     321                $this->assertSame( 1, bbp_get_forum_reply_count( $forum_id, false, true ) );
     322                $short_circuit = '__return_false';
     323                add_filter( 'bbp_pre_update_counts_on_transition_post_status', $short_circuit );
     324
     325                try {
     326                        bbp_unapprove_topic( $topic_id );
     327                } finally {
     328                        remove_filter( 'bbp_pre_update_counts_on_transition_post_status', $short_circuit );
     329                }
     330
     331                $this->assertSame( 1, bbp_get_forum_topic_count( $forum_id, false, true ) );
     332                $this->assertSame( 0, bbp_get_forum_topic_count_hidden( $forum_id, false, true ) );
     333                $this->assertSame( 1, bbp_get_forum_reply_count( $forum_id, false, true ) );
     334                $this->assertSame( 1, bbp_get_user_topic_count( $user_id, true ) );
     335        }
     336
     337        /**
    302338         * Generic function to test the forum counts on a approved/unapproved topic
    303339         */
  • branches/2.6/tests/phpunit/testcases/replies/functions/status.php

    r7451 r7478  
    129129
    130130        /**
     131         * @covers ::bbp_update_counts_on_transition_post_status
     132         * @ticket BBP3678
     133         */
     134        public function test_custom_non_public_status_updates_reply_counts() {
     135                $user_id  = $this->factory->user->create();
     136                $forum_id = $this->factory->forum->create();
     137                $topic_id = $this->factory->topic->create( array(
     138                        'post_parent' => $forum_id,
     139                        'topic_meta'  => array( 'forum_id' => $forum_id ),
     140                ) );
     141                $reply_id = $this->factory->reply->create( array(
     142                        'post_author' => $user_id,
     143                        'post_parent' => $topic_id,
     144                        'reply_meta'  => array(
     145                                'forum_id' => $forum_id,
     146                                'topic_id' => $topic_id,
     147                        ),
     148                ) );
     149                $add_archived = function( $statuses ) {
     150                        $statuses[] = 'archived';
     151
     152                        return $statuses;
     153                };
     154
     155                add_filter( 'bbp_get_non_public_reply_statuses', $add_archived );
     156
     157                try {
     158                        wp_update_post( array(
     159                                'ID'          => $reply_id,
     160                                'post_status' => 'archived',
     161                        ) );
     162
     163                        $this->assertSame( 0, bbp_get_topic_reply_count( $topic_id, true ) );
     164                        $this->assertSame( 1, bbp_get_topic_reply_count_hidden( $topic_id, true ) );
     165                        $this->assertSame( 0, bbp_get_forum_reply_count( $forum_id, false, true ) );
     166                        $this->assertSame( 1, bbp_get_forum_reply_count_hidden( $forum_id, false, true ) );
     167                        $this->assertSame( 0, bbp_get_user_reply_count( $user_id, true ) );
     168
     169                        wp_update_post( array(
     170                                'ID'          => $reply_id,
     171                                'post_status' => bbp_get_public_status_id(),
     172                        ) );
     173
     174                        $this->assertSame( 1, bbp_get_topic_reply_count( $topic_id, true ) );
     175                        $this->assertSame( 0, bbp_get_topic_reply_count_hidden( $topic_id, true ) );
     176                        $this->assertSame( 1, bbp_get_forum_reply_count( $forum_id, false, true ) );
     177                        $this->assertSame( 0, bbp_get_forum_reply_count_hidden( $forum_id, false, true ) );
     178                        $this->assertSame( 1, bbp_get_user_reply_count( $user_id, true ) );
     179                } finally {
     180                        remove_filter( 'bbp_get_non_public_reply_statuses', $add_archived );
     181                }
     182        }
     183
     184        /**
    131185         * @covers ::bbp_update_reply_walker
    132186         */
  • branches/2.6/tests/phpunit/testcases/topics/functions/status.php

    r7451 r7478  
    7878        /**
    7979         * @covers ::bbp_spam_topic
     80         * @covers ::bbp_update_counts_on_transition_post_status
    8081         */
    8182        public function test_bbp_spam_topic() {
    … …  
    237238
    238239        /**
     240         * @covers ::bbp_update_counts_on_transition_post_status
     241         * @ticket BBP3678
     242         */
     243        public function test_custom_non_public_status_updates_topic_and_forum_counts() {
     244                $user_id  = $this->factory->user->create();
     245                $forum_id = $this->factory->forum->create();
     246                $topic_id = $this->factory->topic->create( array(
     247                        'post_author' => $user_id,
     248                        'post_parent' => $forum_id,
     249                        'topic_meta'  => array( 'forum_id' => $forum_id ),
     250                ) );
     251                $this->factory->reply->create( array(
     252                        'post_parent' => $topic_id,
     253                        'reply_meta'  => array(
     254                                'forum_id' => $forum_id,
     255                                'topic_id' => $topic_id,
     256                        ),
     257                ) );
     258                $add_archived = function( $statuses ) {
     259                        $statuses[] = 'archived';
     260
     261                        return $statuses;
     262                };
     263
     264                add_filter( 'bbp_get_non_public_topic_statuses', $add_archived );
     265
     266                try {
     267                        wp_update_post( array(
     268                                'ID'          => $topic_id,
     269                                'post_status' => 'archived',
     270                        ) );
     271
     272                        $this->assertSame( 0, bbp_get_forum_topic_count( $forum_id, false, true ) );
     273                        $this->assertSame( 1, bbp_get_forum_topic_count_hidden( $forum_id, false, true ) );
     274                        $this->assertSame( 0, bbp_get_forum_reply_count( $forum_id, false, true ) );
     275                        $this->assertSame( 0, bbp_get_user_topic_count( $user_id, true ) );
     276
     277                        wp_update_post( array(
     278                                'ID'          => $topic_id,
     279                                'post_status' => bbp_get_public_status_id(),
     280                        ) );
     281
     282                        $this->assertSame( 1, bbp_get_forum_topic_count( $forum_id, false, true ) );
     283                        $this->assertSame( 0, bbp_get_forum_topic_count_hidden( $forum_id, false, true ) );
     284                        $this->assertSame( 1, bbp_get_forum_reply_count( $forum_id, false, true ) );
     285                        $this->assertSame( 1, bbp_get_user_topic_count( $user_id, true ) );
     286                } finally {
     287                        remove_filter( 'bbp_get_non_public_topic_statuses', $add_archived );
     288                }
     289        }
     290
     291        /**
    239292         * @covers ::bbp_spam_topic_replies
    240293         */
    … …  
    310363        /**
    311364         * @covers ::bbp_unspam_topic
     365         * @covers ::bbp_update_counts_on_transition_post_status
    312366         */
    313367        public function test_bbp_unspam_topic() {
    … …  
    388442
    389443        /**
     444         * @covers ::bbp_update_counts_on_transition_post_status
     445         * @ticket BBP3678
     446         */
     447        public function test_repeated_topic_status_cascades_do_not_drift_forum_reply_count() {
     448                $forum_id = $this->factory->forum->create();
     449                $topic_id = $this->factory->topic->create( array(
     450                        'post_parent' => $forum_id,
     451                        'topic_meta'  => array( 'forum_id' => $forum_id ),
     452                ) );
     453                $this->factory->reply->create_many( 2, array(
     454                        'post_parent' => $topic_id,
     455                        'reply_meta'  => array(
     456                                'forum_id' => $forum_id,
     457                                'topic_id' => $topic_id,
     458                        ),
     459                ) );
     460
     461                for ( $i = 0; $i < 2; $i++ ) {
     462                        bbp_spam_topic( $topic_id );
     463                        $this->assertSame( 0, bbp_get_forum_reply_count( $forum_id, false, true ) );
     464
     465                        bbp_unspam_topic( $topic_id );
     466                        $this->assertSame( 2, bbp_get_forum_reply_count( $forum_id, false, true ) );
     467
     468                        wp_trash_post( $topic_id );
     469                        $this->assertSame( 0, bbp_get_forum_reply_count( $forum_id, false, true ) );
     470
     471                        wp_untrash_post( $topic_id );
     472                        $this->assertSame( 2, bbp_get_forum_reply_count( $forum_id, false, true ) );
     473                }
     474        }
     475
     476        /**
    390477         * @covers ::bbp_unspam_topic_replies
    391478         */
Note: See TracChangeset for help on using the changeset viewer.