Skip to:
Content

BuddyPress.org

Ticket #5349: 5349.patch

File 5349.patch, 4.8 KB (added by boonebgorges, 8 years ago)
  • bp-activity/bp-activity-classes.php

    diff --git bp-activity/bp-activity-classes.php bp-activity/bp-activity-classes.php
    index a05f2e0..44c9307 100644
    class BP_Activity_Activity { 
    292292                extract( $r );
    293293
    294294                // Select conditions
    295                 $select_sql = "SELECT DISTINCT a.*, u.user_email, u.user_nicename, u.user_login, u.display_name";
     295                $select_sql = "SELECT DISTINCT a.id";
    296296
    297                 $from_sql = " FROM {$bp->activity->table_name} a LEFT JOIN {$wpdb->users} u ON a.user_id = u.ID";
     297                $from_sql = " FROM {$bp->activity->table_name} a";
    298298
    299299                $join_sql = '';
    300300
    class BP_Activity_Activity { 
    377377                        $index_hint_sql = '';
    378378                }
    379379
    380                 if ( !empty( $per_page ) && !empty( $page ) ) {
     380                // Sanitize page and per_page parameters
     381                $page     = absint( $page     );
     382                $per_page = absint( $per_page );
    381383
    382                         // Make sure page values are absolute integers
    383                         $page     = absint( $page     );
    384                         $per_page = absint( $per_page );
     384                // Filter and return true to use the legacy query structure (not recommended)
     385                if ( apply_filters( 'bp_use_legacy_activity_query', false, __METHOD__, $r ) ) {
     386
     387                        // Legacy queries joined against the user table
     388                        $select_sql = "SELECT DISTINCT a.*, u.user_email, u.user_nicename, u.user_login, u.display_name";
     389                        $from_sql   = " FROM {$bp->activity->table_name} a LEFT JOIN {$wpdb->users} u ON a.user_id = u.ID";
     390
     391                        if ( ! empty( $page ) && ! empty( $per_page ) ) {
     392                                $pag_sql = $wpdb->prepare( "LIMIT %d, %d", absint( ( $page - 1 ) * $per_page ), $per_page );
     393                                $activities = $wpdb->get_results( apply_filters( 'bp_activity_get_user_join_filter', "{$select_sql} {$from_sql} {$join_sql} {$where_sql} ORDER BY a.date_recorded {$sort} {$pag_sql}", $select_sql, $from_sql, $where_sql, $sort, $pag_sql ) );
     394                        } else {
     395                                $activities = $wpdb->get_results( apply_filters( 'bp_activity_get_user_join_filter', "{$select_sql} {$from_sql} {$join_sql} {$where_sql} ORDER BY a.date_recorded {$sort}", $select_sql, $from_sql, $where_sql, $sort ) );
     396                        }
    385397
    386                         $pag_sql    = $wpdb->prepare( "LIMIT %d, %d", absint( ( $page - 1 ) * $per_page ), $per_page );
    387                         $activities = $wpdb->get_results( apply_filters( 'bp_activity_get_user_join_filter', "{$select_sql} {$from_sql} {$join_sql} {$where_sql} ORDER BY a.date_recorded {$sort} {$pag_sql}", $select_sql, $from_sql, $where_sql, $sort, $pag_sql ) );
    388398                } else {
    389                         $activities = $wpdb->get_results( apply_filters( 'bp_activity_get_user_join_filter', "{$select_sql} {$from_sql} {$join_sql} {$where_sql} ORDER BY a.date_recorded {$sort}", $select_sql, $from_sql, $where_sql, $sort ) );
     399                        // Query first for activity IDs
     400                        $activity_ids_sql = "{$select_sql} {$from_sql} {$join_sql} {$where_sql} ORDER BY a.date_recorded {$sort}";
     401
     402                        if ( ! empty( $per_page ) && ! empty( $page ) ) {
     403                                $activity_ids_sql .= $wpdb->prepare( " LIMIT %d, %d", absint( ( $page - 1 ) * $per_page ), $per_page );
     404                        }
     405
     406                        $activity_ids = $wpdb->get_col( $activity_ids_sql );
     407
     408                        $activities = self::get_activity_data( $activity_ids );
    390409                }
    391410
    392411                $total_activities_sql = apply_filters( 'bp_activity_total_activities_sql', "SELECT count(DISTINCT a.id) FROM {$bp->activity->table_name} a {$index_hint_sql} {$join_sql} {$where_sql} ORDER BY a.date_recorded {$sort}", $where_sql, $sort );
    class BP_Activity_Activity { 
    437456        }
    438457
    439458        /**
     459         * Convert activity IDs to activity objects, as expected in template loop.
     460         *
     461         * @since 2.0
     462         *
     463         * @param array $activity_ids Array of activity IDs.
     464         * @return array
     465         */
     466        protected static function get_activity_data( $activity_ids ) {
     467                global $wpdb, $bp;
     468
     469                if ( empty( $activity_ids ) ) {
     470                        return array();
     471                }
     472
     473                $activity_ids_sql = implode( ',', wp_parse_id_list( $activity_ids ) );
     474
     475                // First fetch data from activity table, preserving order
     476                $activities = $wpdb->get_results( "SELECT * FROM {$bp->activity->table_name} WHERE id IN ({$activity_ids_sql}) ORDER BY FIELD( id, {$activity_ids_sql} )");
     477
     478                // Then fetch user data
     479                $user_query = new BP_User_Query( array(
     480                        'user_ids'        => wp_list_pluck( $activities, 'user_id' ),
     481                        'populate_extras' => false,
     482                ) );
     483
     484                // Associated located user data with activity items
     485                foreach ( $activities as $a_index => $a_item ) {
     486                        $a_user_id = intval( $a_item->user_id );
     487                        $a_user = isset( $user_query->results[ $a_user_id ] ) ? $user_query->results[ $a_user_id ] : '';
     488
     489                        if ( $a_user ) {
     490                                $activities[ $a_index ]->user_email = $a_user->user_email;
     491                                $activities[ $a_index ]->user_nicename = $a_user->user_nicename;
     492                                $activities[ $a_index ]->user_login = $a_user->user_login;
     493                                $activities[ $a_index ]->display_name = $a_user->display_name;
     494                        }
     495                }
     496
     497                return $activities;
     498        }
     499
     500        /**
    440501         * Get the SQL for the 'meta_query' param in BP_Activity_Activity::get().
    441502         *
    442503         * We use WP_Meta_Query to do the heavy lifting of parsing the