Skip to:
Content

BuddyPress.org

Changeset 13344


Ignore:
Timestamp:
10/25/2022 07:33:13 PM (19 months ago)
Author:
imath
Message:

Improve the activity loop to be able to get items below a max. ID

The $filter parameter of this loop now includes a new $offset_lower
argument making it possible to only retrieve activities having an ID
lower than this argument provided value.

We are using this filter to avoid a possible activity duplicate when
clicking on the "Load More" link.

Closes https://github.com/buddypress/buddypress/pull/29
Fixes #4535

Location:
trunk/src
Files:
6 edited

Legend:

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

    r13326 r13344  
    273273            'secondary_id'      => false,        // Secondary object ID to filter on e.g. a post_id.
    274274            'offset'            => false,        // Return only items >= this ID.
     275            'offset_lower'      => false,        // Return only items < this ID.
    275276            'since'             => false,        // Return only items recorded since this Y-m-d H:i:s date.
    276277
     
    327328            'object' => $_GET['afilter']
    328329        );
    329     } elseif ( ! empty( $r['user_id'] ) || ! empty( $r['object'] ) || ! empty( $r['action'] ) || ! empty( $r['primary_id'] ) || ! empty( $r['secondary_id'] ) || ! empty( $r['offset'] ) || ! empty( $r['since'] ) ) {
     330    } elseif ( ! empty( $r['user_id'] ) || ! empty( $r['object'] ) || ! empty( $r['action'] ) || ! empty( $r['primary_id'] ) || ! empty( $r['secondary_id'] ) || ! empty( $r['offset'] ) || ! empty( $r['offset_lower'] ) || ! empty( $r['since'] ) ) {
    330331        $r['filter'] = array(
    331332            'user_id'      => $r['user_id'],
     
    335336            'secondary_id' => $r['secondary_id'],
    336337            'offset'       => $r['offset'],
     338            'offset_lower' => $r['offset_lower'],
    337339            'since'        => $r['since']
    338340        );
     
    411413        global $activities_template;
    412414
    413         $url  = bp_get_requested_url();
    414         $link = add_query_arg( $activities_template->pag_arg, $activities_template->pag_page + 1, $url );
     415        $url            = bp_get_requested_url();
     416        $load_more_args = array(
     417            $activities_template->pag_arg => $activities_template->pag_page + 1,
     418        );
     419
     420        // Try to include the offset arg.
     421        $last_displayed_activity = reset( $activities_template->activities );
     422        if ( isset( $last_displayed_activity->id ) && $last_displayed_activity->id ) {
     423            $load_more_args['offset_lower'] = (int) $last_displayed_activity->id;
     424        }
     425
     426        $link = add_query_arg( $load_more_args, $url );
    415427
    416428        /**
  • trunk/src/bp-activity/classes/class-bp-activity-activity.php

    r13337 r13344  
    19231923     *     @type int              $offset       Return only those items with an ID greater
    19241924     *                                          than the offset value.
     1925     *     @type int              $offset_lower Return only those items with an ID lower
     1926     *                                          than the offset value.
    19251927     *     @type string           $since        Return only those items that have a
    19261928     *                                          date_recorded value greater than a
     
    19661968            $sid_sql = absint( $filter_array['offset'] );
    19671969            $filter_sql[] = "a.id >= {$sid_sql}";
     1970        }
     1971
     1972        if ( ! empty( $filter_array['offset_lower'] ) ) {
     1973            $sid_sql = absint( $filter_array['offset_lower'] );
     1974            $filter_sql[] = "a.id < {$sid_sql}";
    19681975        }
    19691976
  • trunk/src/bp-templates/bp-legacy/buddypress-functions.php

    r13306 r13344  
    766766    }
    767767
     768    if ( ! empty( $_POST['offset_lower'] ) ) {
     769        $qs[] = 'offset_lower=' . intval( $_POST['offset_lower'] );
     770    }
     771
    768772    $object_search_text = bp_get_search_default_text( $object );
    769773    if ( ! empty( $_POST['search_terms'] ) && is_string( $_POST['search_terms'] ) && $object_search_text != $_POST['search_terms'] && 'false' != $_POST['search_terms'] && 'undefined' != $_POST['search_terms'] )
  • trunk/src/bp-templates/bp-legacy/js/buddypress.js

    r13190 r13344  
    432432        /* Load more updates at the end of the page */
    433433        if ( target.parent().hasClass('load-more') ) {
     434            var loadMoreLink = new URL( jq( target ).prop( 'href' ) ),
     435                offsetLower = parseInt( loadMoreLink.searchParams.get( 'offset_lower' ), 10 ) || 0;
     436
    434437            if ( bp_ajax_request ) {
    435438                bp_ajax_request.abort();
     
    449452                'cookie': bp_get_cookies(),
    450453                'page': oldest_page,
     454                'offset_lower': offsetLower,
    451455                'exclude_just_posted': just_posted.join(',')
    452456            };
  • trunk/src/bp-templates/bp-nouveau/includes/functions.php

    r13312 r13344  
    116116    if ( ! empty( $post_query['offset'] ) ) {
    117117        $qs[] = 'offset=' . intval( $post_query['offset'] );
     118    }
     119
     120    if ( ! empty( $post_query['offset_lower'] ) ) {
     121        $qs[] = 'offset_lower=' . intval( $post_query['offset_lower'] );
    118122    }
    119123
  • trunk/src/bp-templates/bp-nouveau/js/buddypress-activity.js

    r13114 r13344  
    274274            // Load more activities
    275275            } else if ( $( event.currentTarget ).hasClass( 'load-more' ) ) {
    276                 var next_page = ( Number( this.current_page ) * 1 ) + 1, self = this, search_terms = '';
     276                var next_page = ( Number( this.current_page ) * 1 ) + 1, self = this, search_terms = '',
     277                    loadMoreLink = $( event.currentTarget ).children().first(),
     278                    offsetLower  = loadMoreLink ? bp.Nouveau.getLinkParams( loadMoreLink.prop( 'href' ), 'offset_lower' ) : 0;
    277279
    278280                // Stop event propagation
    279281                event.preventDefault();
    280282
    281                 $( event.currentTarget ).find( 'a' ).first().addClass( 'loading' );
     283                loadMoreLink.addClass( 'loading' );
    282284
    283285                // reset the just posted
     
    301303                    method              : 'append',
    302304                    exclude_just_posted : this.just_posted.join( ',' ),
     305                    offset_lower        : offsetLower,
    303306                    target              : '#buddypress [data-bp-list] ul.bp-list'
    304307                } ).done( function( response ) {
Note: See TracChangeset for help on using the changeset viewer.