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 | } |
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 ); |
| 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 | /** |