[buddypress-trac] [BuddyPress] #4535: Load More Button loads duplicates

buddypress-trac at lists.automattic.com buddypress-trac at lists.automattic.com
Mon Sep 17 21:20:41 UTC 2012


#4535: Load More Button loads duplicates
--------------------------+-----------------------------
 Reporter:  jtymann       |      Owner:
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  Activity      |    Version:
 Severity:  normal        |   Keywords:
--------------------------+-----------------------------
 In the default buddypress theme/Activity Core there is a bug with the
 activity stream. If you load the activity stream. Then open up a new
 window, go to the site, and perform a few actions that create activities.
 If you go back to the original window and click 'load more' you will
 receive duplicate posts. This is highly noticeable on a site with multiple
 concurrent users creating activity.

 Below is how I fixed the issue in a child theme of mine:

 The concept of my changes is pretty simple, in addition to passing which
 page the user is on the id of the first post on the users page is passed
 as well. Then the sql is altered to take this into account.

 javascript changes:
 I added the line "'pag_first_post' : $(".activity-
 item:first").attr("id").split("-")[1]" (and a comma) to the following
 code. This passes the post id along with the load more request

 {{{
 /* Load more updates at the end of the page */
                 if ( target.parent().hasClass('load-more') ) {
                         jq("#content li.load-more").addClass('loading');

                         if ( null == jq.cookie('bp-activity-oldestpage') )
                                 jq.cookie('bp-activity-oldestpage', 1, {
                                         path: '/'
                                 } );

                         var oldest_page = ( jq.cookie('bp-activity-
 oldestpage') * 1 ) + 1;

                         jq.post( ajaxurl, {
                                 action: 'activity_get_older_updates',
                                 'cookie':
 encodeURIComponent(document.cookie),
                                 'page': oldest_page,
                                 'pag_first_post' : $(".activity-
 item:first").attr("id").split("-")[1]
                         },
                         function(response)
                         {
                                 jq("#content li.load-
 more").removeClass('loading');
                                 jq.cookie( 'bp-activity-oldestpage',
 oldest_page, {
                                         path: '/'
                                 } );
                                 jq("#content ul.activity-
 list").append(response.contents);

                                 target.parent().hide();
                         }, 'json' );

                         return false;
                 }
 }}}
 ajax.php changes:
 I added support to the function bp_dtheme_ajax_querystring to now handle
 this new parameter.

 I added the lines:
 {{{
         if ( ! empty( $_POST['pag_first_post'] ) && '-1' !=
 $_POST['pag_first_post'] )
                 $qs[] = 'pag_first_post=' . $_POST['pag_first_post'];
 }}}
 After the code
 {{{
         if ( ! empty( $_POST['page'] ) && '-1' != $_POST['page'] )
                 $qs[] = 'page=' . $_POST['page'];
 }}}
 Finally I added the following code to my functions.php file:
 {{{
 //Fix to correct load more duplicates
 function ofa_activity_get_load_more_fix($prepared_sql, $select_sql,
 $from_sql, $where_sql, $sort, $pag_sql=false) {
         global $bp, $wpdb;
         $args = wp_parse_args(bp_ajax_querystring( 'activity' ));
         if($args['pag_first_post'] && $post_seed =
 (int)$args['pag_first_post']){
                 if($pag_sql){
                         $where_sql .= " AND a.id <= $post_seed";
                         return $wpdb->prepare( "{$select_sql} {$from_sql}
 {$where_sql} ORDER BY a.date_recorded {$sort} {$pag_sql}");
                 }
         }
         return $prepared_sql;
 }
 add_filter( 'bp_activity_get_user_join_filter',
 'ofa_activity_get_load_more_fix', 10, 6 );

 function ofa_activity_get_total_load_more_fix($prepared_sql, $select_sql,
 $from_sql, $where_sql, $sort) {
         global $bp, $wpdb;
         $args = wp_parse_args(bp_ajax_querystring( 'activity' ));
         if($args['pag_first_post'] && $post_seed =
 (int)$args['pag_first_post']){
                 if($pag_sql){
                         $where_sql .= " AND a.id <= $post_seed";
                         return $wpdb->prepare( "SELECT count(a.id) FROM
 {$bp->activity->table_name} a {$index_hint_sql} {$where_sql} ORDER BY
 a.date_recorded {$sort}" );
                 }
         }
         return $prepared_sql;
 }
 add_filter( 'ofa_activity_get_total_load_more_fix', '', 10, 5 );
 }}}

-- 
Ticket URL: <https://buddypress.trac.wordpress.org/ticket/4535>
BuddyPress <http://buddypress.org/>
BuddyPress


More information about the buddypress-trac mailing list