Ticket #6426: 6426.02.patch
File 6426.02.patch, 22.0 KB (added by , 9 years ago) |
---|
-
src/bp-activity/bp-activity-functions.php
1304 1304 * @param BP_Activity_Activity $action Action string being requested. 1305 1305 * @param BP_Activity_Activity $activity Activity item object. 1306 1306 */ 1307 $action = apply_filters( 'bp_activity_generate_action_string', $activity->action, $activity );1307 $action = apply_filters( 'bp_activity_generate_action_string', isset( $activity->action ) ? $activity->action : false, $activity ); 1308 1308 1309 1309 // Remove the filter for future activity items 1310 1310 remove_filter( 'bp_activity_generate_action_string', $actions->{$activity->component}->{$activity->type}['format_callback'], 10, 2 ); … … 1320 1320 * @param string $action Static activity action. 1321 1321 * @param object $activity Activity data object. 1322 1322 * 1323 * @return string 1323 * @return string|bool Generated string on success; boolean false on failure. 1324 1324 */ 1325 1325 function bp_activity_format_activity_action_activity_update( $action, $activity ) { 1326 if ( false === isset( $activity->user_id ) ) { 1327 return $action; 1328 } 1329 1326 1330 $action = sprintf( __( '%s posted an update', 'buddypress' ), bp_core_get_userlink( $activity->user_id ) ); 1327 1331 1328 1332 /** … … 1344 1348 * @param string $action Static activity action. 1345 1349 * @param object $activity Activity data object. 1346 1350 * 1347 * @return string 1351 * @return string|bool Generated string on success; boolean false on failure. 1348 1352 */ 1349 1353 function bp_activity_format_activity_action_activity_comment( $action, $activity ) { 1354 if ( false === isset( $activity->user_id ) ) { 1355 return $action; 1356 } 1357 1350 1358 $action = sprintf( __( '%s posted a new activity comment', 'buddypress' ), bp_core_get_userlink( $activity->user_id ) ); 1351 1359 1352 1360 /** … … 1378 1386 $bp->activity->track = bp_activity_get_post_types_tracking_args(); 1379 1387 } 1380 1388 1381 if ( empty( $activity->type ) || empty( $bp->activity->track[ $activity->type ] ) ) {1389 if ( empty( $activity->type ) || empty( $bp->activity->track[ $activity->type ] ) || false === isset( $activity->user_id ) || false === isset( $activity->item_id ) ) { 1382 1390 return $action; 1383 1391 } 1384 1392 … … 1454 1462 'max' => false, // Maximum number of results to return 1455 1463 'page' => 1, // page 1 without a per_page will result in no pagination. 1456 1464 'per_page' => false, // results per page 1465 'fields' => '', // Fields to return 1457 1466 'sort' => 'DESC', // sort ASC or DESC 1458 1467 'display_comments' => false, // false for no comments. 'stream' for within stream display, 'threaded' for below each activity item 1459 1468 … … 1483 1492 ) ); 1484 1493 1485 1494 // 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'] ) ) {1495 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 1496 1488 1497 $activity = wp_cache_get( 'bp_activity_sitewide_front', 'bp' ); 1489 1498 if ( false === $activity ) { … … 1492 1501 'page' => $r['page'], 1493 1502 'per_page' => $r['per_page'], 1494 1503 'max' => $r['max'], 1504 'fields' => $r['fields'], 1495 1505 'sort' => $r['sort'], 1496 1506 'search_terms' => $r['search_terms'], 1497 1507 'meta_query' => $r['meta_query'], … … 1514 1524 'page' => $r['page'], 1515 1525 'per_page' => $r['per_page'], 1516 1526 'max' => $r['max'], 1527 'fields' => $r['fields'], 1517 1528 'sort' => $r['sort'], 1518 1529 'search_terms' => $r['search_terms'], 1519 1530 'meta_query' => $r['meta_query'], … … 1565 1576 'activity_ids' => false, // A single activity_id or array of IDs. 1566 1577 'display_comments' => false, // true or false to display threaded comments for these specific activity items 1567 1578 'max' => false, // Maximum number of results to return 1579 'fields' => '', // Fields to return 1568 1580 'page' => 1, // page 1 without a per_page will result in no pagination. 1569 1581 'per_page' => false, // results per page 1570 1582 'show_hidden' => true, // When fetching specific items, show all … … 1577 1589 'display_comments' => $r['display_comments'], 1578 1590 'in' => $r['activity_ids'], 1579 1591 'max' => $r['max'], 1592 'fields' => $r['fields'], 1580 1593 'page' => $r['page'], 1581 1594 'per_page' => $r['per_page'], 1582 1595 '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) … … 393 396 $where_conditions['search_sql'] = $wpdb->prepare( 'a.content LIKE %s', $search_terms_like ); 394 397 } 395 398 399 // Custom fields 400 if ( ! empty( $r['fields'] ) ) { 401 $fields = is_array( $r['fields'] ) ? $r['fields'] : explode( ',', $r['fields'] ); 402 } else { 403 $fields = array(); 404 } 405 396 406 // Sorting 397 407 $sort = $r['sort']; 398 408 if ( $sort != 'ASC' && $sort != 'DESC' ) { … … 562 572 563 573 $activity_ids = $wpdb->get_col( $activity_ids_sql ); 564 574 575 // Return activity IDs only 576 if ( 'ids' === $r['fields'] ) { 577 return $activity_ids; 578 } 579 565 580 $retval['has_more_items'] = ! empty( $per_page ) && count( $activity_ids ) > $per_page; 566 581 567 582 // If we've fetched more than the $per_page value, we … … 570 585 array_pop( $activity_ids ); 571 586 } 572 587 573 $activities = self::get_activity_data( $activity_ids );588 $activities = self::get_activity_data( $activity_ids, $fields ); 574 589 } 575 590 576 591 // Get the fullnames of users so we don't have to query in the loop 577 $activities = self::append_user_fullnames( $activities ); 592 if ( empty( $fields ) || in_array( 'user_id', $fields, true ) ) { 593 $activities = self::append_user_fullnames( $activities ); 594 } 578 595 579 596 // Get activity meta 580 597 $activity_ids = array(); … … 594 611 BP_Activity_Activity::prefetch_object_data( $activities ); 595 612 596 613 // Generate action strings 597 $activities = BP_Activity_Activity::generate_action_strings( $activities ); 614 if ( empty( $fields ) || in_array( 'action', $fields, true ) ) { 615 $activities = BP_Activity_Activity::generate_action_strings( $activities ); 616 } 598 617 599 618 $retval['activities'] = $activities; 600 619 … … 631 650 * @since BuddyPress (2.0.0) 632 651 * 633 652 * @param array $activity_ids Array of activity IDs. 653 * @param array $fields Fields to return. 634 654 * 635 655 * @return array 636 656 */ 637 protected static function get_activity_data( $activity_ids = array() ) {657 protected static function get_activity_data( $activity_ids = array(), $fields = array() ) { 638 658 global $wpdb; 639 659 640 660 // Bail if no activity ID's passed … … 668 688 $activities[] = wp_cache_get( $activity_id, 'bp_activity' ); 669 689 } 670 690 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 ) ); 691 // Grab all fields and fetch usernames 692 if ( empty( $fields ) ) { 693 // Then fetch user data 694 $user_query = new BP_User_Query( array( 695 'user_ids' => wp_list_pluck( $activities, 'user_id' ), 696 'populate_extras' => false, 697 ) ); 698 699 // Associated located user data with activity items 700 foreach ( $activities as $a_index => $a_item ) { 701 $a_user_id = intval( $a_item->user_id ); 702 $a_user = isset( $user_query->results[ $a_user_id ] ) ? $user_query->results[ $a_user_id ] : ''; 703 704 if ( ! empty( $a_user ) ) { 705 $activities[ $a_index ]->user_email = $a_user->user_email; 706 $activities[ $a_index ]->user_nicename = $a_user->user_nicename; 707 $activities[ $a_index ]->user_login = $a_user->user_login; 708 $activities[ $a_index ]->display_name = $a_user->display_name; 709 } 710 } 711 712 // Only return custom fields 713 } else { 714 $filtered_activities = array(); 715 foreach ( $activities as $a_index => $a_item ) { 716 $filtered_activities[ $a_index ] = new stdClass; 676 717 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 ] : ''; 718 // Always add the activity ID 719 $filtered_activities[ $a_index ]->id = $a_item->id; 681 720 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; 721 // Add each custom field 722 foreach ( $fields as $field ) { 723 if ( isset( $a_item->$field ) ) { 724 $filtered_activities[ $a_index ]->$field = $a_item->$field; 725 } 726 } 687 727 } 728 729 $activities = $filtered_activities; 688 730 } 689 731 690 732 return $activities; -
src/bp-blogs/bp-blogs-activity.php
64 64 * 65 65 * @since BuddyPress (2.0.0) 66 66 * 67 * @param string $action Static activity action.68 * @param object $activity Activity data object.67 * @param string|bool $action Static activity action. 68 * @param object $activity Activity data object. 69 69 * 70 * @return string 70 * @return string|bool Constructed activity action. 71 71 */ 72 72 function bp_blogs_format_activity_action_new_blog( $action, $activity ) { 73 if ( false === isset( $activity->item_id ) || false === isset( $activity->user_id ) ) { 74 return $action; 75 } 76 73 77 $blog_url = bp_blogs_get_blogmeta( $activity->item_id, 'url' ); 74 78 $blog_name = bp_blogs_get_blogmeta( $activity->item_id, 'name' ); 75 79 … … 103 107 * 104 108 * @since BuddyPress (2.0.0) 105 109 * 106 * @param string $action Static activity action.107 * @param object $activity Activity data object.110 * @param string|bool $action Static activity action. 111 * @param object $activity Activity data object. 108 112 * 109 * @return string Constructed activity action.113 * @return string|bool Constructed activity action. 110 114 */ 111 115 function bp_blogs_format_activity_action_new_blog_post( $action, $activity ) { 116 if ( false === isset( $activity->item_id ) || false === isset( $activity->secondary_item_id ) || false === isset( $activity->user_id ) ) { 117 return $action; 118 } 119 112 120 $blog_url = bp_blogs_get_blogmeta( $activity->item_id, 'url' ); 113 121 $blog_name = bp_blogs_get_blogmeta( $activity->item_id, 'name' ); 114 122 … … 215 223 * 216 224 * @since BuddyPress (2.0.0) 217 225 * 218 * @param string $action Static activity action.219 * @param object $activity Activity data object.226 * @param string|bool $action Static activity action. 227 * @param object $activity Activity data object. 220 228 * 221 * @return string Constructed activity action.229 * @return string|bool Constructed activity action. 222 230 */ 223 231 function bp_blogs_format_activity_action_new_blog_comment( $action, $activity ) { 232 if ( false === isset( $activity->item_id ) || false === isset( $activity->user_id ) ) { 233 return $action; 234 } 235 224 236 $blog_url = bp_blogs_get_blogmeta( $activity->item_id, 'url' ); 225 237 $blog_name = bp_blogs_get_blogmeta( $activity->item_id, 'name' ); 226 238 … … 310 322 continue; 311 323 } 312 324 313 $blog_ids[] = $activity->item_id; 325 if ( isset( $activity->item_id ) ) { 326 $blog_ids[] = $activity->item_id; 327 } 314 328 } 315 329 316 330 if ( ! empty( $blog_ids ) ) { -
src/bp-friends/bp-friends-activity.php
127 127 * 128 128 * @since BuddyPress (2.0.0) 129 129 * 130 * @param string $action Activity action string.131 * @param object $activity Activity data.130 * @param string|bool $action Activity action string. 131 * @param object $activity Activity data. 132 132 * 133 * @return string $action Formatted activity action.133 * @return string|bool $action Formatted activity action. 134 134 */ 135 135 function bp_friends_format_activity_action_friendship_accepted( $action, $activity ) { 136 if ( false === isset( $activity->user_id ) || false === isset( $activity->secondary_item_id ) ) { 137 return $action; 138 } 139 136 140 $initiator_link = bp_core_get_userlink( $activity->user_id ); 137 141 $friend_link = bp_core_get_userlink( $activity->secondary_item_id ); 138 142 … … 162 166 * 163 167 * @since BuddyPress (2.0.0) 164 168 * 165 * @param string $action Static activity action.166 * @param object $activity Activity data.169 * @param string|bool $action Static activity action. 170 * @param object $activity Activity data. 167 171 * 168 * @return string $action Formatted activity action.172 * @return string|bool $action Formatted activity action. 169 173 */ 170 174 function bp_friends_format_activity_action_friendship_created( $action, $activity ) { 175 if ( false === isset( $activity->user_id ) || false === isset( $activity->secondary_item_id ) ) { 176 return $action; 177 } 178 171 179 $initiator_link = bp_core_get_userlink( $activity->user_id ); 172 180 $friend_link = bp_core_get_userlink( $activity->secondary_item_id ); 173 181 … … 215 223 continue; 216 224 } 217 225 218 $friend_ids[] = $activity->secondary_item_id; 226 if ( isset( $activity->secondary_item_id ) ) { 227 $friend_ids[] = $activity->secondary_item_id; 228 } 219 229 } 220 230 221 231 if ( ! empty( $friend_ids ) ) { -
src/bp-groups/bp-groups-activity.php
89 89 * 90 90 * @since BuddyPress (2.0.0) 91 91 * 92 * @param string $action Static activity action.93 * @param object $activity Activity data object.92 * @param string|bool $action Static activity action. 93 * @param object $activity Activity data object. 94 94 * 95 * @return string 95 * @return string|bool Constructed activity action. 96 96 */ 97 97 function bp_groups_format_activity_action_created_group( $action, $activity ) { 98 if ( false === isset( $activity->user_id ) || false === isset( $activity->item_id ) ) { 99 return $action; 100 } 101 98 102 $user_link = bp_core_get_userlink( $activity->user_id ); 99 103 100 104 $group = groups_get_group( array( … … 121 125 * 122 126 * @since BuddyPress (2.0.0) 123 127 * 124 * @param string $action Static activity action.125 * @param object $activity Activity data object.128 * @param string|bool $action Static activity action. 129 * @param object $activity Activity data object. 126 130 * 127 * @return string 131 * @return string|bool Constructed activity action. 128 132 */ 129 133 function bp_groups_format_activity_action_joined_group( $action, $activity ) { 134 if ( false === isset( $activity->user_id ) || false === isset( $activity->item_id ) ) { 135 return $action; 136 } 137 130 138 $user_link = bp_core_get_userlink( $activity->user_id ); 131 139 132 140 $group = groups_get_group( array( … … 164 172 * 165 173 * @since BuddyPress (2.2.0) 166 174 * 167 * @param string$action Static activity action.168 * @param object$activity Activity data object.175 * @param string|bool $action Static activity action. 176 * @param object $activity Activity data object. 169 177 * 170 * @return string 178 * @return string|bool Constructed activity action. 171 179 */ 172 180 function bp_groups_format_activity_action_group_details_updated( $action, $activity ) { 181 if ( false === isset( $activity->user_id ) || false === isset( $activity->item_id ) || false === isset( $activity->date_recorded ) ) { 182 return $action; 183 } 184 173 185 $user_link = bp_core_get_userlink( $activity->user_id ); 174 186 175 187 $group = groups_get_group( array( … … 236 248 continue; 237 249 } 238 250 239 $group_ids[] = $activity->item_id; 251 if ( isset( $activity->item_id ) ) { 252 $group_ids[] = $activity->item_id; 253 } 240 254 } 241 255 242 256 if ( ! empty( $group_ids ) ) { -
src/bp-members/bp-members-activity.php
43 43 * 44 44 * @since BuddyPress (2.2.0) 45 45 * 46 * @param string $action Static activity action.47 * @param object $activity Activityobject.46 * @param string|bool $action Static activity action. 47 * @param object $activity Activity data object. 48 48 * 49 * @return string 49 * @return string|bool Constructed activity action. 50 50 */ 51 51 function bp_members_format_activity_action_new_member( $action, $activity ) { 52 if ( false === isset( $activity->user_id ) ) { 53 return $action; 54 } 55 52 56 $userlink = bp_core_get_userlink( $activity->user_id ); 53 57 $action = sprintf( __( '%s became a registered member', 'buddypress' ), $userlink ); 54 58 -
src/bp-xprofile/bp-xprofile-activity.php
56 56 * 57 57 * @since BuddyPress (2.0.0) 58 58 * 59 * @param string $action Static activity action. 60 * @param object $activity Activity object. 61 * @return string 59 * @param string|bool $action Static activity action. 60 * @param object $activity Activity data object. 61 * 62 * @return string|bool Constructed activity action. 62 63 */ 63 64 function bp_xprofile_format_activity_action_new_avatar( $action, $activity ) { 65 if ( false === isset( $activity->user_id ) ) { 66 return $action; 67 } 68 64 69 $userlink = bp_core_get_userlink( $activity->user_id ); 65 70 $action = sprintf( __( '%s changed their profile picture', 'buddypress' ), $userlink ); 66 71 … … 85 90 * 86 91 * @since BuddyPress (2.0.0) 87 92 * 88 * @param string $action Static activity action. 89 * @param object $activity Activity object. 90 * @return string 93 * @param string|bool $action Static activity action. 94 * @param object $activity Activity data object. 95 * 96 * @return string|bool Constructed activity action. 91 97 */ 92 98 function bp_xprofile_format_activity_action_updated_profile( $action, $activity ) { 99 if ( false === isset( $activity->user_id ) ) { 100 return $action; 101 } 93 102 94 103 // Note for translators: The natural phrasing in English, "Joe updated 95 104 // his profile", requires that we know Joe's gender, which we don't. If -
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['activities'][0] = (object) array( 56 'id' => $a1, 57 'component' => 'activity', 58 'type' => 'activity_update' 59 ); 60 $expected['activities'][1] = (object) array( 61 'id' => $a2, 62 'component' => 'blogs', 63 'type' => 'new_blog_post' 64 ); 65 $expected['activities'][2] = (object) array( 66 'id' => $a3, 67 'component' => 'activity', 68 'type' => 'activity_update' 69 ); 70 $expected['total'] = null; 71 $expected['has_more_items'] = false; 72 73 $this->assertEquals( $expected, $result ); 74 } 75 76 /** 8 77 * @group check_exists_by_content 9 78 */ 10 79 public function test_check_exists_by_content() {