- Timestamp:
- 10/12/2015 05:45:14 AM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-activity/classes/class-bp-activity-activity.php
r10217 r10253 7 7 */ 8 8 9 // Exit if accessed directly 9 // Exit if accessed directly. 10 10 defined( 'ABSPATH' ) || exit; 11 11 … … 164 164 } 165 165 166 // Generate dynamic 'action' when possible 166 // Generate dynamic 'action' when possible. 167 167 $action = bp_activity_generate_action_string( $this ); 168 168 if ( false !== $action ) { … … 170 170 171 171 // If no callback is available, use the literal string from 172 // the database row 172 // the database row. 173 173 } elseif ( ! empty( $row->action ) ) { 174 174 $this->action = $row->action; 175 175 176 // Provide a fallback to avoid PHP notices 176 // Provide a fallback to avoid PHP notices. 177 177 } else { 178 178 $this->action = ''; … … 235 235 } 236 236 237 // If this is a new activity item, set the $id property 237 // If this is a new activity item, set the $id property. 238 238 if ( empty( $this->id ) ) { 239 239 $this->id = $wpdb->insert_id; … … 271 271 * @param array $args { 272 272 * An array of arguments. All items are optional. 273 *274 273 * @type int $page Which page of results to fetch. Using page=1 without per_page will result 275 274 * in no pagination. Default: 1. … … 296 295 * } 297 296 * @return array The array returned has two keys: 298 * - 'total' is the count of located activities299 * - 'activities' is an array of the located activities297 * - 'total' is the count of located activities 298 * - 'activities' is an array of the located activities 300 299 */ 301 300 public static function get( $args = array() ) { 302 301 global $wpdb; 303 302 304 // Backward compatibility with old method of passing arguments 303 // Backward compatibility with old method of passing arguments. 305 304 if ( !is_array( $args ) || func_num_args() > 1 ) { 306 305 _deprecated_argument( __METHOD__, '1.6', sprintf( __( 'Arguments passed to %1$s should be in an associative array. See the inline documentation at %2$s for more details.', 'buddypress' ), __METHOD__, __FILE__ ) ); … … 326 325 $bp = buddypress(); 327 326 $r = wp_parse_args( $args, array( 328 'page' => 1, // The current page 329 'per_page' => 25, // Activity items per page 330 'max' => false, // Max number of items to return 331 'fields' => 'all', 332 'sort' => 'DESC', // ASC or DESC 333 'exclude' => false, // Array of ids to exclude 334 'in' => false, // Array of ids to limit query by (IN) 335 'meta_query' => false, // Filter by activitymeta 336 'date_query' => false, // Filter by date 337 'filter_query' => false, // Advanced filtering - see BP_Activity_Query 338 'filter' => false, // See self::get_filter_sql() 339 'scope' => false, // Preset activity arguments 340 'search_terms' => false, // Terms to search by 341 'display_comments' => false, // Whether to include activity comments 342 'show_hidden' => false, // Show items marked hide_sitewide 343 'spam' => 'ham_only', // Spam status 344 'update_meta_cache' => true, 345 'count_total' => false, 327 'page' => 1, // The current page. 328 'per_page' => 25, // Activity items per page. 329 'max' => false, // Max number of items to return. 330 'fields' => 'all', // Fields to include. 331 'sort' => 'DESC', // ASC or DESC. 332 'exclude' => false, // Array of ids to exclude. 333 'in' => false, // Array of ids to limit query by (IN). 334 'meta_query' => false, // Filter by activitymeta. 335 'date_query' => false, // Filter by date. 336 'filter_query' => false, // Advanced filtering - see BP_Activity_Query. 337 'filter' => false, // See self::get_filter_sql(). 338 'scope' => false, // Preset activity arguments. 339 'search_terms' => false, // Terms to search by. 340 'display_comments' => false, // Whether to include activity comments. 341 'show_hidden' => false, // Show items marked hide_sitewide. 342 'spam' => 'ham_only', // Spam status. 343 'update_meta_cache' => true, // Whether or not to update meta cache. 344 'count_total' => false, // Whether or not to use count_total. 346 345 ) ); 347 346 348 // Select conditions 347 // Select conditions. 349 348 $select_sql = "SELECT DISTINCT a.id"; 350 349 … … 353 352 $join_sql = ''; 354 353 355 // Where conditions 354 // Where conditions. 356 355 $where_conditions = array(); 357 356 358 // Excluded types 357 // Excluded types. 359 358 $excluded_types = array(); 360 359 361 // Scope takes precedence 360 // Scope takes precedence. 362 361 if ( ! empty( $r['scope'] ) ) { 363 362 $scope_query = self::get_scope_query_sql( $r['scope'], $r ); 364 363 365 // Add our SQL conditions if matches were found 364 // Add our SQL conditions if matches were found. 366 365 if ( ! empty( $scope_query['sql'] ) ) { 367 366 $where_conditions['scope_query_sql'] = $scope_query['sql']; 368 367 } 369 368 370 // override some arguments if needed369 // Override some arguments if needed. 371 370 if ( ! empty( $scope_query['override'] ) ) { 372 371 $r = self::array_replace_recursive( $r, $scope_query['override'] ); 373 372 } 374 373 375 // Advanced filtering 374 // Advanced filtering. 376 375 } elseif ( ! empty( $r['filter_query'] ) ) { 377 376 $filter_query = new BP_Activity_Query( $r['filter_query'] ); … … 382 381 } 383 382 384 // Regular filtering 383 // Regular filtering. 385 384 if ( $r['filter'] && $filter_sql = BP_Activity_Activity::get_filter_sql( $r['filter'] ) ) { 386 385 $where_conditions['filter_sql'] = $filter_sql; 387 386 } 388 387 389 // Spam 388 // Spam. 390 389 if ( 'ham_only' == $r['spam'] ) { 391 390 $where_conditions['spam_sql'] = 'a.is_spam = 0'; … … 394 393 } 395 394 396 // Searching 395 // Searching. 397 396 if ( $r['search_terms'] ) { 398 397 $search_terms_like = '%' . bp_esc_like( $r['search_terms'] ) . '%'; … … 400 399 } 401 400 402 // Sorting 401 // Sorting. 403 402 $sort = $r['sort']; 404 403 if ( $sort != 'ASC' && $sort != 'DESC' ) { … … 411 410 } 412 411 413 // Exclude specified items 412 // Exclude specified items. 414 413 if ( ! empty( $r['exclude'] ) ) { 415 414 $exclude = implode( ',', wp_parse_id_list( $r['exclude'] ) ); … … 417 416 } 418 417 419 // The specific ids to which you want to limit the query 418 // The specific ids to which you want to limit the query. 420 419 if ( ! empty( $r['in'] ) ) { 421 420 $in = implode( ',', wp_parse_id_list( $r['in'] ) ); … … 423 422 } 424 423 425 // Process meta_query into SQL 424 // Process meta_query into SQL. 426 425 $meta_query_sql = self::get_meta_query_sql( $r['meta_query'] ); 427 426 … … 434 433 } 435 434 436 // Process date_query into SQL 435 // Process date_query into SQL. 437 436 $date_query_sql = self::get_date_query_sql( $r['date_query'] ); 438 437 … … 449 448 450 449 // Exclude 'last_activity' items unless the 'action' filter has 451 // been explicitly set 450 // been explicitly set. 452 451 if ( empty( $r['filter']['object'] ) ) { 453 452 $excluded_types[] = 'last_activity'; 454 453 } 455 454 456 // Build the excluded type sql part 455 // Build the excluded type sql part. 457 456 if ( ! empty( $excluded_types ) ) { 458 457 $not_in = "'" . implode( "', '", esc_sql( $excluded_types ) ) . "'"; … … 473 472 $where_conditions = apply_filters( 'bp_activity_get_where_conditions', $where_conditions, $r, $select_sql, $from_sql, $join_sql ); 474 473 475 // Join the where conditions together 474 // Join the where conditions together. 476 475 $where_sql = 'WHERE ' . join( ' AND ', $where_conditions ); 477 476 … … 488 487 if ( false !== strpos( $where_sql, $index ) ) { 489 488 $the_index = $index; 490 break; // Take the first one we find 489 break; // Take the first one we find. 491 490 } 492 491 } … … 498 497 } 499 498 500 // Sanitize page and per_page parameters 499 // Sanitize page and per_page parameters. 501 500 $page = absint( $r['page'] ); 502 501 $per_page = absint( $r['per_page'] ); … … 521 520 if ( apply_filters( 'bp_use_legacy_activity_query', false, __METHOD__, $r ) ) { 522 521 523 // Legacy queries joined against the user table 522 // Legacy queries joined against the user table. 524 523 $select_sql = "SELECT DISTINCT a.*, u.user_email, u.user_nicename, u.user_login, u.display_name"; 525 524 $from_sql = " FROM {$bp->activity->table_name} a LEFT JOIN {$wpdb->users} u ON a.user_id = u.ID"; … … 528 527 $pag_sql = $wpdb->prepare( "LIMIT %d, %d", absint( ( $page - 1 ) * $per_page ), $per_page ); 529 528 530 /** this filter is documented in bp-activity/bp-activity-classes.php */529 /** This filter is documented in bp-activity/bp-activity-classes.php */ 531 530 $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 ) ); 532 531 } else { … … 548 547 549 548 } else { 550 // Query first for activity IDs 549 // Query first for activity IDs. 551 550 $activity_ids_sql = "{$select_sql} {$from_sql} {$join_sql} {$where_sql} ORDER BY a.date_recorded {$sort}"; 552 551 553 552 if ( ! empty( $per_page ) && ! empty( $page ) ) { 554 553 // We query for $per_page + 1 items in order to 555 // populate the has_more_items flag 554 // populate the has_more_items flag. 556 555 $activity_ids_sql .= $wpdb->prepare( " LIMIT %d, %d", absint( ( $page - 1 ) * $per_page ), $per_page + 1 ); 557 556 } … … 572 571 573 572 // If we've fetched more than the $per_page value, we 574 // can discard the extra now 573 // can discard the extra now. 575 574 if ( ! empty( $per_page ) && count( $activity_ids ) === $per_page + 1 ) { 576 575 array_pop( $activity_ids ); … … 585 584 586 585 if ( 'ids' !== $r['fields'] ) { 587 // Get the fullnames of users so we don't have to query in the loop 586 // Get the fullnames of users so we don't have to query in the loop. 588 587 $activities = self::append_user_fullnames( $activities ); 589 588 590 // Get activity meta 589 // Get activity meta. 591 590 $activity_ids = array(); 592 591 foreach ( (array) $activities as $activity ) { … … 602 601 } 603 602 604 // Pre-fetch data associated with activity users and other objects 603 // Pre-fetch data associated with activity users and other objects. 605 604 BP_Activity_Activity::prefetch_object_data( $activities ); 606 605 607 // Generate action strings 606 // Generate action strings. 608 607 $activities = BP_Activity_Activity::generate_action_strings( $activities ); 609 608 } … … 611 610 $retval['activities'] = $activities; 612 611 613 // If $max is set, only return up to the max results 612 // If $max is set, only return up to the max results. 614 613 if ( ! empty( $r['count_total'] ) ) { 615 614 … … 644 643 * 645 644 * @param array $activity_ids Array of activity IDs. 646 *647 645 * @return array 648 646 */ … … 650 648 global $wpdb; 651 649 652 // Bail if no activity ID's passed 650 // Bail if no activity ID's passed. 653 651 if ( empty( $activity_ids ) ) { 654 652 return array(); 655 653 } 656 654 657 // Get BuddyPress 655 // Get BuddyPress. 658 656 $bp = buddypress(); 659 657 … … 661 659 $uncached_ids = bp_get_non_cached_ids( $activity_ids, 'bp_activity' ); 662 660 663 // Prime caches as necessary 661 // Prime caches as necessary. 664 662 if ( ! empty( $uncached_ids ) ) { 665 // Format the activity ID's for use in the query below 663 // Format the activity ID's for use in the query below. 666 664 $uncached_ids_sql = implode( ',', wp_parse_id_list( $uncached_ids ) ); 667 665 668 // Fetch data from activity table, preserving order 666 // Fetch data from activity table, preserving order. 669 667 $queried_adata = $wpdb->get_results( "SELECT * FROM {$bp->activity->table_name} WHERE id IN ({$uncached_ids_sql})"); 670 668 671 669 // Put that data into the placeholders created earlier, 672 // and add it to the cache 670 // and add it to the cache. 673 671 foreach ( (array) $queried_adata as $adata ) { 674 672 wp_cache_set( $adata->id, $adata, 'bp_activity' ); … … 676 674 } 677 675 678 // Now fetch data from the cache 676 // Now fetch data from the cache. 679 677 foreach ( $activity_ids as $activity_id ) { 680 678 $activities[] = wp_cache_get( $activity_id, 'bp_activity' ); 681 679 } 682 680 683 // Then fetch user data 681 // Then fetch user data. 684 682 $user_query = new BP_User_Query( array( 685 683 'user_ids' => wp_list_pluck( $activities, 'user_id' ), … … 687 685 ) ); 688 686 689 // Associated located user data with activity items 687 // Associated located user data with activity items. 690 688 foreach ( $activities as $a_index => $a_item ) { 691 689 $a_user_id = intval( $a_item->user_id ); … … 709 707 * 710 708 * @param array $activities Activities array. 711 *712 709 * @return array 713 710 */ … … 750 747 * 751 748 * @param array $activities Array of activities. 752 *753 749 * @return array $activities Array of activities. 754 750 */ … … 776 772 * 777 773 * @param array $activities Array of activities. 778 *779 774 * @return array 780 775 */ … … 805 800 * @param array $meta_query An array of meta_query filters. See the 806 801 * documentation for WP_Meta_Query for details. 807 *808 802 * @return array $sql_array 'join' and 'where' clauses. 809 803 */ … … 820 814 821 815 // WP_Meta_Query expects the table name at 822 // $wpdb->activitymeta 816 // $wpdb->activitymeta. 823 817 $wpdb->activitymeta = buddypress()->activity->table_name_meta; 824 818 825 819 $meta_sql = $activity_meta_query->get_sql( 'activity', 'a', 'id' ); 826 820 827 // Strip the leading AND - BP handles it in get() 821 // Strip the leading AND - BP handles it in get(). 828 822 $sql_array['where'] = preg_replace( '/^\sAND/', '', $meta_sql['where'] ); 829 823 $sql_array['join'] = $meta_sql['join']; … … 846 840 * @param array $date_query An array of date_query parameters. See the 847 841 * documentation for the first parameter of WP_Date_Query. 848 *849 842 * @return string 850 843 */ … … 852 845 $sql = ''; 853 846 854 // Date query 847 // Date query. 855 848 if ( ! empty( $date_query ) && is_array( $date_query ) ) { 856 849 $date_query = new BP_Date_Query( $date_query, 'date_recorded' ); … … 874 867 * @param array $r Current activity arguments. Same as those of BP_Activity_Activity::get(), 875 868 * but merged with defaults. 876 *877 869 * @return array 'sql' WHERE SQL string and 'override' activity args. 878 870 */ 879 871 public static function get_scope_query_sql( $scope = false, $r = array() ) { 880 872 881 // Define arrays for future use 873 // Define arrays for future use. 882 874 $query_args = array(); 883 875 $override = array(); 884 876 $retval = array(); 885 877 886 // Check for array of scopes 878 // Check for array of scopes. 887 879 if ( is_array( $scope ) ) { 888 880 $scopes = $scope; 889 881 890 // Explode a comma separated string of scopes 882 // Explode a comma separated string of scopes. 891 883 } elseif ( is_string( $scope ) ) { 892 884 $scopes = explode( ',', $scope ); 893 885 } 894 886 895 // Bail if no scope passed 887 // Bail if no scope passed. 896 888 if ( empty( $scopes ) ) { 897 889 return false; 898 890 } 899 891 900 // Helper to easily grab the 'user_id' 892 // Helper to easily grab the 'user_id'. 901 893 if ( ! empty( $r['filter']['user_id'] ) ) { 902 894 $r['user_id'] = $r['filter']['user_id']; 903 895 } 904 896 905 // parse each scope; yes! we handle multiples!897 // Parse each scope; yes! we handle multiples! 906 898 foreach ( $scopes as $scope ) { 907 899 $scope_args = array(); … … 922 914 * @param array { 923 915 * Activity query clauses. 924 *925 916 * @type array { 926 917 * Activity arguments for your custom scope. … … 934 925 935 926 if ( ! empty( $scope_args ) ) { 936 // merge override properties from other scopes927 // Merge override properties from other scopes 937 928 // this might be a problem... 938 929 if ( ! empty( $scope_args['override'] ) ) { … … 941 932 } 942 933 943 // save scope args934 // Save scope args. 944 935 if ( ! empty( $scope_args ) ) { 945 936 $query_args[] = $scope_args; … … 949 940 950 941 if ( ! empty( $query_args ) ) { 951 // set relation to OR942 // Set relation to OR. 952 943 $query_args['relation'] = 'OR'; 953 944 … … 982 973 * @param string $sort MySQL column sort; ASC or DESC. (Optional; default is DESC). 983 974 * @param bool $display_comments Retrieve an activity item's associated comments or not. (Optional; default is false). 984 *985 975 * @return array 986 976 */ … … 992 982 /** 993 983 * Get the first activity ID that matches a set of criteria. 984 * 985 * @todo Should parameters be optional? 994 986 * 995 987 * @param int $user_id User ID to filter by. … … 1001 993 * @param string $content Content to filter by. 1002 994 * @param string $date_recorded Date to filter by. 1003 *1004 * @todo Should parameters be optional?1005 *1006 995 * @return int|bool Activity ID on success, false if none is found. 1007 996 */ … … 1074 1063 * @int $hide_sitewide Optional. Default: false. 1075 1064 * } 1076 *1077 1065 * @return array|bool An array of deleted activity IDs on success, false on failure. 1078 1066 */ … … 1095 1083 ) ); 1096 1084 1097 // Setup empty array from where query arguments 1085 // Setup empty array from where query arguments. 1098 1086 $where_args = array(); 1099 1087 1100 // ID 1088 // ID. 1101 1089 if ( ! empty( $r['id'] ) ) { 1102 1090 $where_args[] = $wpdb->prepare( "id = %d", $r['id'] ); 1103 1091 } 1104 1092 1105 // User ID 1093 // User ID. 1106 1094 if ( ! empty( $r['user_id'] ) ) { 1107 1095 $where_args[] = $wpdb->prepare( "user_id = %d", $r['user_id'] ); 1108 1096 } 1109 1097 1110 // Action 1098 // Action. 1111 1099 if ( ! empty( $r['action'] ) ) { 1112 1100 $where_args[] = $wpdb->prepare( "action = %s", $r['action'] ); 1113 1101 } 1114 1102 1115 // Content 1103 // Content. 1116 1104 if ( ! empty( $r['content'] ) ) { 1117 1105 $where_args[] = $wpdb->prepare( "content = %s", $r['content'] ); 1118 1106 } 1119 1107 1120 // Component 1108 // Component. 1121 1109 if ( ! empty( $r['component'] ) ) { 1122 1110 $where_args[] = $wpdb->prepare( "component = %s", $r['component'] ); 1123 1111 } 1124 1112 1125 // Type 1113 // Type. 1126 1114 if ( ! empty( $r['type'] ) ) { 1127 1115 $where_args[] = $wpdb->prepare( "type = %s", $r['type'] ); 1128 1116 } 1129 1117 1130 // Primary Link 1118 // Primary Link. 1131 1119 if ( ! empty( $r['primary_link'] ) ) { 1132 1120 $where_args[] = $wpdb->prepare( "primary_link = %s", $r['primary_link'] ); 1133 1121 } 1134 1122 1135 // Item ID 1123 // Item ID. 1136 1124 if ( ! empty( $r['item_id'] ) ) { 1137 1125 $where_args[] = $wpdb->prepare( "item_id = %d", $r['item_id'] ); 1138 1126 } 1139 1127 1140 // Secondary item ID 1128 // Secondary item ID. 1141 1129 if ( ! empty( $r['secondary_item_id'] ) ) { 1142 1130 $where_args[] = $wpdb->prepare( "secondary_item_id = %d", $r['secondary_item_id'] ); 1143 1131 } 1144 1132 1145 // Date Recorded 1133 // Date Recorded. 1146 1134 if ( ! empty( $r['date_recorded'] ) ) { 1147 1135 $where_args[] = $wpdb->prepare( "date_recorded = %s", $r['date_recorded'] ); 1148 1136 } 1149 1137 1150 // Hidden sitewide 1138 // Hidden sitewide. 1151 1139 if ( ! empty( $r['hide_sitewide'] ) ) { 1152 1140 $where_args[] = $wpdb->prepare( "hide_sitewide = %d", $r['hide_sitewide'] ); 1153 1141 } 1154 1142 1155 // Bail if no where arguments 1143 // Bail if no where arguments. 1156 1144 if ( empty( $where_args ) ) { 1157 1145 return false; 1158 1146 } 1159 1147 1160 // Join the where arguments for querying 1148 // Join the where arguments for querying. 1161 1149 $where_sql = 'WHERE ' . join( ' AND ', $where_args ); 1162 1150 1163 // Fetch all activities being deleted so we can perform more actions 1151 // Fetch all activities being deleted so we can perform more actions. 1164 1152 $activities = $wpdb->get_results( "SELECT * FROM {$bp->activity->table_name} {$where_sql}" ); 1165 1153 … … 1174 1162 do_action_ref_array( 'bp_activity_before_delete', array( $activities, $r ) ); 1175 1163 1176 // Attempt to delete activities from the database 1164 // Attempt to delete activities from the database. 1177 1165 $deleted = $wpdb->query( "DELETE FROM {$bp->activity->table_name} {$where_sql}" ); 1178 1166 1179 // Bail if nothing was deleted 1167 // Bail if nothing was deleted. 1180 1168 if ( empty( $deleted ) ) { 1181 1169 return false; … … 1192 1180 do_action_ref_array( 'bp_activity_after_delete', array( $activities, $r ) ); 1193 1181 1194 // Pluck the activity IDs out of the $activities array 1182 // Pluck the activity IDs out of the $activities array. 1195 1183 $activity_ids = wp_parse_id_list( wp_list_pluck( $activities, 'id' ) ); 1196 1184 1197 // Handle accompanying activity comments and meta deletion 1185 // Handle accompanying activity comments and meta deletion. 1198 1186 if ( ! empty( $activity_ids ) ) { 1199 1187 1200 // Delete all activity meta entries for activity items 1188 // Delete all activity meta entries for activity items. 1201 1189 BP_Activity_Activity::delete_activity_meta_entries( $activity_ids ); 1202 1190 1203 // Setup empty array for comments 1191 // Setup empty array for comments. 1204 1192 $comment_ids = array(); 1205 1193 1206 // Loop through activity ids and attempt to delete comments 1194 // Loop through activity ids and attempt to delete comments. 1207 1195 foreach ( $activity_ids as $activity_id ) { 1208 1196 1209 // Attempt to delete comments 1197 // Attempt to delete comments. 1210 1198 $comments = BP_Activity_Activity::delete( array( 1211 1199 'type' => 'activity_comment', … … 1213 1201 ) ); 1214 1202 1215 // Merge IDs together 1203 // Merge IDs together. 1216 1204 if ( ! empty( $comments ) ) { 1217 1205 $comment_ids = array_merge( $comment_ids, $comments ); … … 1219 1207 } 1220 1208 1221 // Merge activity IDs with any deleted comment IDs 1209 // Merge activity IDs with any deleted comment IDs. 1222 1210 if ( ! empty( $comment_ids ) ) { 1223 1211 $activity_ids = array_unique( array_merge( $activity_ids, $comment_ids ) ); … … 1238 1226 * 1239 1227 * @param array $activity_ids Activity IDs whose comments should be deleted. 1240 * @param bool $delete_meta Should we delete the activity meta items for these comments ?1228 * @param bool $delete_meta Should we delete the activity meta items for these comments. 1241 1229 * 1242 1230 * @return bool True on success. … … 1251 1239 1252 1240 if ( $delete_meta ) { 1253 // Fetch the activity comment IDs for our deleted activity items 1241 // Fetch the activity comment IDs for our deleted activity items. 1254 1242 $activity_comment_ids = $wpdb->get_col( "SELECT id FROM {$bp->activity->table_name} WHERE type = 'activity_comment' AND item_id IN ({$activity_ids})" ); 1255 1243 … … 1268 1256 * 1269 1257 * @param array $activity_ids Activity IDs whose meta should be deleted. 1270 *1271 1258 * @return bool True on success. 1272 1259 */ … … 1290 1277 * @param array $activities Activities to fetch comments for. 1291 1278 * @param string $spam Optional. 'ham_only' (default), 'spam_only' or 'all'. 1292 *1293 1279 * @return array The updated activities with nested comments. 1294 1280 */ … … 1302 1288 } 1303 1289 1304 // Merge the comments with the activity items 1290 // Merge the comments with the activity items. 1305 1291 foreach ( (array) $activities as $key => $activity ) { 1306 1292 if ( isset( $activity_comments[$activity->id] ) ) { … … 1324 1310 * @param string $spam Optional. 'ham_only' (default), 'spam_only' or 'all'. 1325 1311 * @param int $top_level_parent_id Optional. The id of the root-level parent activity item. 1326 *1327 1312 * @return array The updated activities with nested comments. 1328 1313 */ … … 1337 1322 1338 1323 // We store the string 'none' to cache the fact that the 1339 // activity item has no comments 1324 // activity item has no comments. 1340 1325 if ( 'none' === $comments ) { 1341 1326 $comments = false; 1342 1327 1343 // A true cache miss 1328 // A true cache miss. 1344 1329 } elseif ( empty( $comments ) ) { 1345 1330 1346 1331 $bp = buddypress(); 1347 1332 1348 // Select the user's fullname with the query 1333 // Select the user's fullname with the query. 1349 1334 if ( bp_is_active( 'xprofile' ) ) { 1350 1335 $fullname_select = ", pd.value as user_fullname"; … … 1352 1337 $fullname_where = "AND pd.user_id = a.user_id AND pd.field_id = 1"; 1353 1338 1354 // Prevent debug errors 1339 // Prevent debug errors. 1355 1340 } else { 1356 1341 $fullname_select = $fullname_from = $fullname_where = ''; 1357 1342 } 1358 1343 1359 // Don't retrieve activity comments marked as spam 1344 // Don't retrieve activity comments marked as spam. 1360 1345 if ( 'ham_only' == $spam ) { 1361 1346 $spam_sql = 'AND a.is_spam = 0'; … … 1366 1351 } 1367 1352 1368 // Legacy query - not recommended 1353 // Legacy query - not recommended. 1369 1354 $func_args = func_get_args(); 1370 1355 … … 1407 1392 $ref = array(); 1408 1393 1409 // Loop descendants and build an assoc array 1394 // Loop descendants and build an assoc array. 1410 1395 foreach ( (array) $descendants as $d ) { 1411 1396 $d->children = array(); 1412 1397 1413 // If we have a reference on the parent 1398 // If we have a reference on the parent. 1414 1399 if ( isset( $ref[ $d->secondary_item_id ] ) ) { 1415 1400 $ref[ $d->secondary_item_id ]->children[ $d->id ] = $d; 1416 1401 $ref[ $d->id ] =& $ref[ $d->secondary_item_id ]->children[ $d->id ]; 1417 1402 1418 // If we don't have a reference on the parent, put in the root level 1403 // If we don't have a reference on the parent, put in the root level. 1419 1404 } else { 1420 1405 $comments[ $d->id ] = $d; … … 1423 1408 } 1424 1409 1425 // Calculate depth for each item 1410 // Calculate depth for each item. 1426 1411 foreach ( $ref as &$r ) { 1427 1412 $depth = 1; … … 1432 1417 1433 1418 // When display_comments=stream, the parent comment may not be part of the 1434 // returned results, so we manually fetch it 1419 // returned results, so we manually fetch it. 1435 1420 if ( empty( $ref[ $parent_id ] ) ) { 1436 1421 $direct_parent = new BP_Activity_Activity( $parent_id ); 1437 1422 if ( isset( $direct_parent->secondary_item_id ) ) { 1438 1423 // If the direct parent is not an activity update, that means we've reached 1439 // the parent activity item (eg. new_blog_post) 1424 // the parent activity item (eg. new_blog_post). 1440 1425 if ( 'activity_update' !== $direct_parent->type ) { 1441 1426 $parent_id = $r->item_id; … … 1446 1431 1447 1432 } else { 1448 // Something went wrong. Short-circuit the depth calculation 1433 // Something went wrong. Short-circuit the depth calculation. 1449 1434 $parent_id = $r->item_id; 1450 1435 } … … 1488 1473 $bp = buddypress(); 1489 1474 1490 // The right value of this node is the left value + 1 1475 // The right value of this node is the left value + 1. 1491 1476 $right = intval( $left + 1 ); 1492 1477 1493 // Get all descendants of this node 1478 // Get all descendants of this node. 1494 1479 $comments = BP_Activity_Activity::get_child_comments( $parent_id ); 1495 1480 $descendants = wp_list_pluck( $comments, 'id' ); 1496 1481 1497 // Loop the descendants and recalculate the left and right values 1482 // Loop the descendants and recalculate the left and right values. 1498 1483 foreach ( (array) $descendants as $descendant_id ) { 1499 1484 $right = BP_Activity_Activity::rebuild_activity_comment_tree( $descendant_id, $right ); … … 1501 1486 1502 1487 // We've got the left value, and now that we've processed the children 1503 // of this node we also know the right value 1488 // of this node we also know the right value. 1504 1489 if ( 1 === $left ) { 1505 1490 $wpdb->query( $wpdb->prepare( "UPDATE {$bp->activity->table_name} SET mptt_left = %d, mptt_right = %d WHERE id = %d", $left, $right, $parent_id ) ); … … 1508 1493 } 1509 1494 1510 // Return the right value of this node + 1 1495 // Return the right value of this node + 1. 1511 1496 return intval( $right + 1 ); 1512 1497 } … … 1520 1505 * 1521 1506 * @param int $parent_id ID of an activity or activity comment. 1522 *1523 1507 * @return object Numerically indexed array of child comments. 1524 1508 */ … … 1535 1519 * 1536 1520 * @param bool $skip_last_activity If true, components will not be 1537 * included if the only activity type associated with them is 1538 * 'last_activity'. (Since 2.0.0, 'last_activity' is stored in 1539 * the activity table, but these items are not full-fledged 1540 * activity items.) Default: true. 1541 * 1521 * included if the only activity type associated with them is 1522 * 'last_activity'. (Since 2.0.0, 'last_activity' is stored in 1523 * the activity table, but these items are not full-fledged 1524 * activity items.) Default: true. 1542 1525 * @return array List of component names. 1543 1526 */ … … 1560 1543 * 1561 1544 * @param int $limit Optional. Number of items to fetch. Default: 35. 1562 *1563 1545 * @return array $activity_feed List of activity items, with RSS data added. 1564 1546 */ … … 1587 1569 * @param string $field The database field. 1588 1570 * @param array|bool $items The values for the IN clause, or false when none are found. 1589 *1590 1571 * @return string|bool 1591 1572 */ … … 1593 1574 global $wpdb; 1594 1575 1595 // split items at the comma1576 // Split items at the comma. 1596 1577 if ( ! is_array( $items ) ) { 1597 1578 $items = explode( ',', $items ); 1598 1579 } 1599 1580 1600 // array of prepared integers or quoted strings1581 // Array of prepared integers or quoted strings. 1601 1582 $items_prepared = array(); 1602 1583 1603 // clean up and format each item1584 // Clean up and format each item. 1604 1585 foreach ( $items as $item ) { 1605 // clean up the string1586 // Clean up the string. 1606 1587 $item = trim( $item ); 1607 // pass everything through prepare for security and to safely quote strings1588 // Pass everything through prepare for security and to safely quote strings. 1608 1589 $items_prepared[] = ( is_numeric( $item ) ) ? $wpdb->prepare( '%d', $item ) : $wpdb->prepare( '%s', $item ); 1609 1590 } 1610 1591 1611 // build IN operator sql syntax1592 // Build IN operator sql syntax. 1612 1593 if ( count( $items_prepared ) ) 1613 1594 return sprintf( '%s IN ( %s )', trim( $field ), implode( ',', $items_prepared ) ); … … 1639 1620 * given MySQL-formatted date. 1640 1621 * } 1641 *1642 1622 * @return string The filter clause, for use in a SQL query. 1643 1623 */ … … 1682 1662 1683 1663 if ( ! empty( $filter_array['since'] ) ) { 1684 // Validate that this is a proper Y-m-d H:i:s date 1685 // Trick: parse to UNIX date then translate back 1664 // Validate that this is a proper Y-m-d H:i:s date. 1665 // Trick: parse to UNIX date then translate back. 1686 1666 $translated_date = date( 'Y-m-d H:i:s', strtotime( $filter_array['since'] ) ); 1687 1667 if ( $translated_date === $filter_array['since'] ) { … … 1717 1697 * 1718 1698 * @param int $user_id The ID of the user whose favorites you're counting. 1719 *1720 1699 * @return int $value A count of the user's favorites. 1721 1700 */ 1722 1701 public static function total_favorite_count( $user_id ) { 1723 1702 1724 // Get activities from user meta 1703 // Get activities from user meta. 1725 1704 $favorite_activity_entries = bp_get_user_meta( $user_id, 'bp_favorite_activities', true ); 1726 1705 if ( ! empty( $favorite_activity_entries ) ) { … … 1728 1707 } 1729 1708 1730 // No favorites 1709 // No favorites. 1731 1710 return 0; 1732 1711 } … … 1736 1715 * 1737 1716 * @param string $content The content to filter by. 1738 *1739 1717 * @return int|bool The ID of the first matching item if found, otherwise false. 1740 1718 */ … … 1751 1729 * 1752 1730 * @param int $user_id The ID of the user whose activity you want to mark hidden. 1753 *1754 1731 * @return mixed 1755 1732 */ … … 1765 1742 * PHP-agnostic version of {@link array_replace_recursive()}. 1766 1743 * 1767 * array_replace_recursive()is a PHP 5.3 function. BuddyPress (and1744 * The array_replace_recursive() function is a PHP 5.3 function. BuddyPress (and 1768 1745 * WordPress) currently supports down to PHP 5.2, so this method is a workaround 1769 1746 * for PHP 5.2. … … 1780 1757 * @param array $base Array with keys needing to be replaced. 1781 1758 * @param array $replacements Array with the replaced keys. 1782 *1783 1759 * @return array 1784 1760 */ … … 1789 1765 1790 1766 // PHP 5.2-compatible version 1791 // http://php.net/manual/en/function.array-replace-recursive.php#109390 1767 // http://php.net/manual/en/function.array-replace-recursive.php#109390. 1792 1768 foreach ( array_slice( func_get_args(), 1 ) as $replacements ) { 1793 1769 $bref_stack = array( &$base );
Note:
See TracChangeset
for help on using the changeset viewer.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)