Skip to:
Content

BuddyPress.org

Changeset 13724


Ignore:
Timestamp:
02/14/2024 06:26:17 AM (2 years ago)
Author:
imath
Message:

Activity: make sure streams are paginated the right way

In [13344] we made sure to avoid the last entry(ies) of the stream to be duplicated if some other members published other entry(ies) just before a user clicked on the "Load More" link.

2 mistakes were made while generating the BP_Activity_Activity MySQL query clause as well as the Load More link query variable causing a regression as some activity entries were wrongly skipped from display each time a user was clicking on this link.

  1. The MySQL query clause built in BP_Activity_Activity::get_filter_sql() needs to also include the last inserted Activity ID using the <= operator instead of the < one.
  2. The query variable added to the Load More link needs to remain the last inserted Activity ID at the time the stream is first displayed.

Props testovac
Antiprops imath

See #9094
Closes https://github.com/buddypress/buddypress/pull/229

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-activity/bp-activity-template.php

    r13520 r13724  
    426426                );
    427427
    428                 // Try to include the offset arg.
    429                 $last_displayed_activity = reset( $activities_template->activities );
    430                 if ( isset( $last_displayed_activity->id ) && $last_displayed_activity->id ) {
    431                         $load_more_args['offset_lower'] = (int) $last_displayed_activity->id;
     428                // Use the first posted offset arg to transport it for each following page links.
     429                if ( isset( $_POST['offset_lower'] ) && $_POST['offset_lower'] ) {
     430                        $load_more_args['offset_lower'] = (int) wp_unslash( $_POST['offset_lower'] );
     431
     432                        // Try to include the offset arg to the second page link.
     433                } elseif ( 1 === $activities_template->pag_page ) {
     434                        $last_displayed_activity = reset( $activities_template->activities );
     435                        if ( isset( $last_displayed_activity->id ) && $last_displayed_activity->id ) {
     436                                $load_more_args['offset_lower'] = (int) $last_displayed_activity->id;
     437                        }
    432438                }
    433439
  • trunk/src/bp-activity/classes/class-bp-activity-activity.php

    r13441 r13724  
    19791979                if ( ! empty( $filter_array['offset_lower'] ) ) {
    19801980                        $sid_sql = absint( $filter_array['offset_lower'] );
    1981                         $filter_sql[] = "a.id < {$sid_sql}";
     1981                        $filter_sql[] = "a.id <= {$sid_sql}";
    19821982                }
    19831983
  • trunk/tests/phpunit/testcases/activity/class.BP_Activity_Activity.php

    r13436 r13724  
    429429        /**
    430430         * @group get
     431         * @ticket BP9094
     432         */
     433        public function test_get_with_offset_lower() {
     434                $now = time();
     435                $a1  = self::factory()->activity->create(
     436                        array(
     437                                'content' => 'Happy Valentine’s day',
     438                                'recorded_time' => date( 'Y-m-d H:i:s', $now - 100 ),
     439                        )
     440                );
     441                $a2  = self::factory()->activity->create(
     442                        array(
     443                                'content' => 'Happy new year',
     444                                'recorded_time' => date( 'Y-m-d H:i:s', $now - 50 ),
     445                        )
     446                );
     447                $a3  = self::factory()->activity->create(
     448                        array(
     449                                'content' => 'Happy days',
     450                                'recorded_time' => date( 'Y-m-d H:i:s', $now - 10 ),
     451                        )
     452                );
     453
     454                $activity = BP_Activity_Activity::get(
     455                        array(
     456                                'filter' => array(
     457                                        'offset_lower' => $a2,
     458                                ),
     459                        )
     460                );
     461
     462                $ids = wp_list_pluck( $activity['activities'], 'id' );
     463                $this->assertEquals( array( $a2, $a1 ), $ids );
     464        }
     465
     466        /**
     467         * @group get
     468         * @ticket BP9094
     469         */
     470        public function test_get_with_offset_lower_with_pagination() {
     471                $a = self::factory()->activity->create_many(
     472                        3,
     473                        array(
     474                                'type' => 'activity_update',
     475                        )
     476                );
     477
     478                $stream = BP_Activity_Activity::get(
     479                        array(
     480                                'page'              => 1,
     481                                'per_page'          => 2,
     482                                'filter' => array(
     483                                        'offset_lower' => end( $a ),
     484                                ),
     485                        )
     486                );
     487
     488                $page_one_ids                 = wp_list_pluck( $stream['activities'], 'id' );
     489                $last_displayed_on_first_page = end( $page_one_ids );
     490
     491                $a1 = self::factory()->activity->create(
     492                        array(
     493                                'content' => 'Ouch this is an Auto Refresh simulation',
     494                        )
     495                );
     496
     497                $stream = BP_Activity_Activity::get(
     498                        array(
     499                                'page'              => 2,
     500                                'per_page'          => 2,
     501                        )
     502                );
     503
     504                $page_two_ids                = wp_list_pluck( $stream['activities'], 'id' );
     505                $first_displayed_on_page_two = reset( $page_two_ids );
     506                $this->assertEquals( $first_displayed_on_page_two, $last_displayed_on_first_page );
     507
     508                $stream = BP_Activity_Activity::get(
     509                        array(
     510                                'page'              => 2,
     511                                'per_page'          => 2,
     512                                'filter' => array(
     513                                        'offset_lower' => end( $a ),
     514                                ),
     515                        )
     516                );
     517
     518                $without_auto_refresh = wp_list_pluck( $stream['activities'], 'id' );
     519                $this->assertFalse( in_array( $last_displayed_on_first_page, $without_auto_refresh, true ) );
     520                $this->assertSame( array_reverse( $a ), array_merge( $page_one_ids, $without_auto_refresh ) );
     521        }
     522
     523        /**
     524         * @group get
    431525         */
    432526        public function test_get_with_user_id__in() {
Note: See TracChangeset for help on using the changeset viewer.