Ticket #6426: 6426.01.patch
File 6426.01.patch, 10.9 KB (added by , 9 years ago) |
---|
-
src/bp-activity/bp-activity-functions.php
1454 1454 'max' => false, // Maximum number of results to return 1455 1455 'page' => 1, // page 1 without a per_page will result in no pagination. 1456 1456 'per_page' => false, // results per page 1457 'fields' => '', // Fields to return 1457 1458 'sort' => 'DESC', // sort ASC or DESC 1458 1459 'display_comments' => false, // false for no comments. 'stream' for within stream display, 'threaded' for below each activity item 1459 1460 … … 1483 1484 ) ); 1484 1485 1485 1486 // Attempt to return a cached copy of the first page of sitewide activity. 1486 if ( ( 1 === (int) $r['page'] ) && empty( $r['max'] ) && empty( $r[' search_terms'] ) && empty( $r['meta_query'] ) && empty( $r['date_query'] ) && empty( $r['filter_query'] ) && empty( $r['filter'] ) && empty( $r['scope'] )&& empty( $r['exclude'] ) && empty( $r['in'] ) && ( 'DESC' === $r['sort'] ) && empty( $r['exclude'] ) && ( 'ham_only' === $r['spam'] ) ) {1487 if ( ( 1 === (int) $r['page'] ) && empty( $r['max'] ) && empty( $r['fields'] ) && empty( $r['search_terms'] ) && empty( $r['meta_query'] ) && empty( $r['date_query'] ) && empty( $r['filter_query'] ) && empty( $r['filter'] ) && empty( $r['scope'] )&& empty( $r['exclude'] ) && empty( $r['in'] ) && ( 'DESC' === $r['sort'] ) && empty( $r['exclude'] ) && ( 'ham_only' === $r['spam'] ) ) { 1487 1488 1488 1489 $activity = wp_cache_get( 'bp_activity_sitewide_front', 'bp' ); 1489 1490 if ( false === $activity ) { … … 1492 1493 'page' => $r['page'], 1493 1494 'per_page' => $r['per_page'], 1494 1495 'max' => $r['max'], 1496 'fields' => $r['fields'], 1495 1497 'sort' => $r['sort'], 1496 1498 'search_terms' => $r['search_terms'], 1497 1499 'meta_query' => $r['meta_query'], … … 1514 1516 'page' => $r['page'], 1515 1517 'per_page' => $r['per_page'], 1516 1518 'max' => $r['max'], 1519 'fields' => $r['fields'], 1517 1520 'sort' => $r['sort'], 1518 1521 'search_terms' => $r['search_terms'], 1519 1522 'meta_query' => $r['meta_query'], … … 1565 1568 'activity_ids' => false, // A single activity_id or array of IDs. 1566 1569 'display_comments' => false, // true or false to display threaded comments for these specific activity items 1567 1570 'max' => false, // Maximum number of results to return 1571 'fields' => '', // Fields to return 1568 1572 'page' => 1, // page 1 without a per_page will result in no pagination. 1569 1573 'per_page' => false, // results per page 1570 1574 'show_hidden' => true, // When fetching specific items, show all … … 1577 1581 'display_comments' => $r['display_comments'], 1578 1582 'in' => $r['activity_ids'], 1579 1583 'max' => $r['max'], 1584 'fields' => $r['fields'], 1580 1585 'page' => $r['page'], 1581 1586 'per_page' => $r['per_page'], 1582 1587 'show_hidden' => $r['show_hidden'], -
src/bp-activity/classes/class-bp-activity-activity.php
272 272 * in no pagination. Default: 1. 273 273 * @type int|bool $per_page Number of results per page. Default: 25. 274 274 * @type int|bool $max Maximum number of results to return. Default: false (unlimited). 275 * @type string|array $fields Fields to return. Any column from the activity DB table is acceptable. Can 276 * be comma-delimited string or array. 'ids' returns only activity IDs. 275 277 * @type string $sort ASC or DESC. Default: 'DESC'. 276 278 * @type array $exclude Array of activity IDs to exclude. Default: false. 277 279 * @type array $in Array of ids to limit query by (IN). Default: false. … … 323 325 'page' => 1, // The current page 324 326 'per_page' => 25, // Activity items per page 325 327 'max' => false, // Max number of items to return 328 'fields' => '', // Fields to return 326 329 'sort' => 'DESC', // ASC or DESC 327 330 'exclude' => false, // Array of ids to exclude 328 331 'in' => false, // Array of ids to limit query by (IN) … … 544 547 // Query first for activity IDs 545 548 $activity_ids_sql = "{$select_sql} {$from_sql} {$join_sql} {$where_sql} ORDER BY a.date_recorded {$sort}"; 546 549 547 if ( ! empty( $per_page ) && ! empty( $page ) ) {550 if ( ! empty( $per_page ) && ! empty( $page ) && 'ids' !== $r['fields'] ) { 548 551 // We query for $per_page + 1 items in order to 549 552 // populate the has_more_items flag 550 553 $activity_ids_sql .= $wpdb->prepare( " LIMIT %d, %d", absint( ( $page - 1 ) * $per_page ), $per_page + 1 ); … … 562 565 563 566 $activity_ids = $wpdb->get_col( $activity_ids_sql ); 564 567 568 // Return activity IDs only 569 if ( 'ids' === $r['fields'] ) { 570 return $activity_ids; 571 } 572 565 573 $retval['has_more_items'] = ! empty( $per_page ) && count( $activity_ids ) > $per_page; 566 574 567 575 // If we've fetched more than the $per_page value, we … … 570 578 array_pop( $activity_ids ); 571 579 } 572 580 573 $activities = self::get_activity_data( $activity_ids ); 581 $activities = self::get_activity_data( $activity_ids, $r['fields'] ); 582 583 // Return activity data with custom fields now 584 if ( ! empty( $r['fields'] ) ) { 585 return $activities; 586 } 574 587 } 575 588 576 589 // Get the fullnames of users so we don't have to query in the loop … … 630 643 * 631 644 * @since BuddyPress (2.0.0) 632 645 * 633 * @param array $activity_ids Array of activity IDs. 646 * @param array $activity_ids Array of activity IDs. 647 * @param string|array $fields Fields to return. Either comma-delimited string or array. Default: ''. 634 648 * 635 649 * @return array 636 650 */ 637 protected static function get_activity_data( $activity_ids = array() ) {651 protected static function get_activity_data( $activity_ids = array(), $fields = '' ) { 638 652 global $wpdb; 639 653 640 654 // Bail if no activity ID's passed … … 663 677 } 664 678 } 665 679 680 // Explode comma-delimited $fields variable to array 681 if ( ! empty( $fields ) ) { 682 $fields = is_array( $fields ) ? $fields : explode( ',', $fields ); 683 } 684 666 685 // Now fetch data from the cache 667 686 foreach ( $activity_ids as $activity_id ) { 668 687 $activities[] = wp_cache_get( $activity_id, 'bp_activity' ); 669 688 } 670 689 671 // Then fetch user data 672 $user_query = new BP_User_Query( array( 673 'user_ids' => wp_list_pluck( $activities, 'user_id' ), 674 'populate_extras' => false, 675 ) ); 690 // Grab all fields and fetch usernames 691 if ( empty( $fields ) ) { 692 // Then fetch user data 693 $user_query = new BP_User_Query( array( 694 'user_ids' => wp_list_pluck( $activities, 'user_id' ), 695 'populate_extras' => false, 696 ) ); 697 698 // Associated located user data with activity items 699 foreach ( $activities as $a_index => $a_item ) { 700 $a_user_id = intval( $a_item->user_id ); 701 $a_user = isset( $user_query->results[ $a_user_id ] ) ? $user_query->results[ $a_user_id ] : ''; 702 703 if ( ! empty( $a_user ) ) { 704 $activities[ $a_index ]->user_email = $a_user->user_email; 705 $activities[ $a_index ]->user_nicename = $a_user->user_nicename; 706 $activities[ $a_index ]->user_login = $a_user->user_login; 707 $activities[ $a_index ]->display_name = $a_user->display_name; 708 } 709 } 710 711 // Only return custom fields 712 } else { 713 $filtered_activities = array(); 714 foreach ( $activities as $a_index => $a_item ) { 715 $filtered_activities[ $a_index ] = new stdClass; 676 716 677 // Associated located user data with activity items 678 foreach ( $activities as $a_index => $a_item ) { 679 $a_user_id = intval( $a_item->user_id ); 680 $a_user = isset( $user_query->results[ $a_user_id ] ) ? $user_query->results[ $a_user_id ] : ''; 717 // Always add the activity ID 718 $filtered_activities[ $a_index ]->id = $a_item->id; 681 719 682 if ( !empty( $a_user ) ) { 683 $activities[ $a_index ]->user_email = $a_user->user_email; 684 $activities[ $a_index ]->user_nicename = $a_user->user_nicename; 685 $activities[ $a_index ]->user_login = $a_user->user_login; 686 $activities[ $a_index ]->display_name = $a_user->display_name; 720 // Add each custom field 721 foreach ( $fields as $field ) { 722 if ( isset( $a_item->$field ) ) { 723 $filtered_activities[ $a_index ]->$field = $a_item->$field; 724 } 725 } 687 726 } 727 728 $activities = $filtered_activities; 688 729 } 689 730 690 731 return $activities; -
tests/phpunit/testcases/activity/class.BP_Activity_Activity.php
5 5 class BP_Tests_Activity_Class extends BP_UnitTestCase { 6 6 7 7 /** 8 * @group get 9 * @group fields 10 */ 11 public function test_get_with_fields_parameter_by_id() { 12 $a = $this->factory->activity->create_many( 3, array( 13 'type' => 'activity_update', 14 ) ); 15 16 // invalid activity items 17 $invalid_aids = array( $a[2] + 1, $a[2] + 2 ); 18 19 $result = BP_Activity_Activity::get( array( 20 'fields' => 'ids', 21 'in' => array_merge( $a, $invalid_aids ), 22 ) ); 23 24 $this->assertEquals( $a, $result ); 25 } 26 27 /** 28 * @group get 29 * @group fields 30 */ 31 public function test_get_with_fields_parameter_by_column_names() { 32 $now = time(); 33 $a1 = $this->factory->activity->create( array( 34 'component' => 'activity', 35 'type' => 'activity_update', 36 'recorded_time' => date( 'Y-m-d H:i:s', $now ), 37 ) ); 38 $a2 = $this->factory->activity->create( array( 39 'component' => 'blogs', 40 'type' => 'new_blog_post', 41 'recorded_time' => date( 'Y-m-d H:i:s', $now - 60 ), 42 ) ); 43 $a3 = $this->factory->activity->create( array( 44 'component' => 'activity', 45 'type' => 'activity_update', 46 'recorded_time' => date( 'Y-m-d H:i:s', $now - 120 ), 47 ) ); 48 49 // Grab only 'component' and 'type' fields 50 $result = BP_Activity_Activity::get( array( 51 'fields' => 'component,type', 52 ) ); 53 54 $expected = array(); 55 $expected[0] = (object) array( 56 'id' => $a1, 57 'component' => 'activity', 58 'type' => 'activity_update' 59 ); 60 $expected[1] = (object) array( 61 'id' => $a2, 62 'component' => 'blogs', 63 'type' => 'new_blog_post' 64 ); 65 $expected[2] = (object) array( 66 'id' => $a3, 67 'component' => 'activity', 68 'type' => 'activity_update' 69 ); 70 71 $this->assertEquals( $expected, $result ); 72 } 73 74 /** 8 75 * @group check_exists_by_content 9 76 */ 10 77 public function test_check_exists_by_content() {