Changeset 6948
- Timestamp:
- 04/26/2013 05:46:44 PM (11 years ago)
- Location:
- trunk
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/bp-activity/bp-activity-classes.php
r6592 r6948 145 145 'exclude' => false, // Array of ids to exclude 146 146 'in' => false, // Array of ids to limit query by (IN) 147 'meta_query' => false, // Filter by activitymeta 147 148 'filter' => false, // See self::get_filter_sql() 148 149 'search_terms' => false, // Terms to search by … … 159 160 $from_sql = " FROM {$bp->activity->table_name} a LEFT JOIN {$wpdb->users} u ON a.user_id = u.ID"; 160 161 162 $join_sql = ''; 163 161 164 // Where conditions 162 165 $where_conditions = array(); … … 198 201 } 199 202 203 // Process meta_query into SQL 204 $meta_query_sql = self::get_meta_query_sql( $meta_query ); 205 206 if ( ! empty( $meta_query_sql['join'] ) ) { 207 $join_sql .= $meta_query_sql['join']; 208 } 209 210 if ( ! empty( $meta_query_sql['where'] ) ) { 211 $where_conditions[] = $meta_query_sql['where']; 212 } 213 200 214 // Alter the query based on whether we want to show activity item 201 215 // comments in the stream like normal comments or threaded below … … 229 243 230 244 $pag_sql = $wpdb->prepare( "LIMIT %d, %d", absint( ( $page - 1 ) * $per_page ), $per_page ); 231 $activities = $wpdb->get_results( apply_filters( 'bp_activity_get_user_join_filter', "{$select_sql} {$from_sql} {$ where_sql} ORDER BY a.date_recorded {$sort} {$pag_sql}", $select_sql, $from_sql, $where_sql, $sort, $pag_sql ) );245 $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 ) ); 232 246 } else { 233 $activities = $wpdb->get_results( apply_filters( 'bp_activity_get_user_join_filter', "{$select_sql} {$from_sql} {$ where_sql} ORDER BY a.date_recorded {$sort}", $select_sql, $from_sql, $where_sql, $sort ) );234 } 235 236 $total_activities_sql = apply_filters( 'bp_activity_total_activities_sql', "SELECT count(a.id) FROM {$bp->activity->table_name} a {$index_hint_sql} {$ where_sql} ORDER BY a.date_recorded {$sort}", $where_sql, $sort );247 $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 ) ); 248 } 249 250 $total_activities_sql = apply_filters( 'bp_activity_total_activities_sql', "SELECT count(a.id) FROM {$bp->activity->table_name} a {$index_hint_sql} {$join_sql} {$where_sql} ORDER BY a.date_recorded {$sort}", $where_sql, $sort ); 237 251 238 252 $total_activities = $wpdb->get_var( $total_activities_sql ); … … 283 297 284 298 return array( 'activities' => $activities, 'total' => (int) $total_activities ); 299 } 300 301 /** 302 * Get the SQL for the 'meta_query' param in BP_Activity_Activity::get() 303 * 304 * We use WP_Meta_Query to do the heavy lifting of parsing the 305 * meta_query array and creating the necessary SQL clauses. However, 306 * since BP_Activity_Activity::get() builds its SQL differently than 307 * WP_Query, we have to alter the return value (stripping the leading 308 * AND keyword from the 'where' clause). 309 * 310 * @since 1.8 311 * 312 * @param array $meta_query An array of meta_query filters. See the 313 * documentation for WP_Meta_Query for details. 314 * @return array $sql_array 'join' and 'where' clauses 315 */ 316 public static function get_meta_query_sql( $meta_query = array() ) { 317 global $wpdb; 318 319 $sql_array = array( 320 'join' => '', 321 'where' => '', 322 ); 323 324 if ( ! empty( $meta_query ) ) { 325 $activity_meta_query = new WP_Meta_Query( $meta_query ); 326 327 // WP_Meta_Query expects the table name at 328 // $wpdb->activitymeta 329 $wpdb->activitymeta = buddypress()->activity->table_name_meta; 330 331 $meta_sql = $activity_meta_query->get_sql( 'activity', 'a', 'id' ); 332 333 // Strip the leading AND - BP handles it in get() 334 $sql_array['where'] = preg_replace( '/^\sAND/', '', $meta_sql['where'] ); 335 $sql_array['join'] = $meta_sql['join']; 336 } 337 338 return $sql_array; 285 339 } 286 340 -
trunk/bp-activity/bp-activity-functions.php
r6925 r6948 882 882 883 883 'search_terms' => false, // Pass search terms as a string 884 'meta_query' => false, // Filter by activity meta. See WP_Meta_Query for format 884 885 'show_hidden' => false, // Show activity items that are hidden site-wide? 885 886 'exclude' => false, // Comma-separated list of activity IDs to exclude … … 903 904 904 905 // Attempt to return a cached copy of the first page of sitewide activity. 905 if ( 1 == (int) $page && empty( $max ) && empty( $search_terms ) && empty( $ filter ) && empty( $exclude ) && empty( $in ) && 'DESC' == $sort && empty( $exclude ) && 'ham_only' == $spam ) {906 if ( 1 == (int) $page && empty( $max ) && empty( $search_terms ) && empty( $meta_query ) && empty( $filter ) && empty( $exclude ) && empty( $in ) && 'DESC' == $sort && empty( $exclude ) && 'ham_only' == $spam ) { 906 907 if ( !$activity = wp_cache_get( 'bp_activity_sitewide_front', 'bp' ) ) { 907 908 $args = array( … … 911 912 'sort' => $sort, 912 913 'search_terms' => $search_terms, 914 'meta_query' => $meta_query, 913 915 'filter' => $filter, 914 916 'display_comments' => $display_comments, … … 927 929 'sort' => $sort, 928 930 'search_terms' => $search_terms, 931 'meta_query' => $meta_query, 929 932 'filter' => $filter, 930 933 'display_comments' => $display_comments, -
trunk/bp-activity/bp-activity-template.php
r6926 r6948 151 151 'filter' => false, 152 152 'search_terms' => false, 153 'meta_query' => false, 153 154 'display_comments' => 'threaded', 154 155 'show_hidden' => false, … … 173 174 // Fetch all activity items 174 175 else 175 $this->activities = bp_activity_get( array( 'display_comments' => $display_comments, 'max' => $max, 'per_page' => $this->pag_num, 'page' => $this->pag_page, 'sort' => $sort, 'search_terms' => $search_terms, ' filter' => $filter, 'show_hidden' => $show_hidden, 'exclude' => $exclude, 'in' => $in, 'spam' => $spam ) );176 $this->activities = bp_activity_get( array( 'display_comments' => $display_comments, 'max' => $max, 'per_page' => $this->pag_num, 'page' => $this->pag_page, 'sort' => $sort, 'search_terms' => $search_terms, 'meta_query' => $meta_query, 'filter' => $filter, 'show_hidden' => $show_hidden, 'exclude' => $exclude, 'in' => $in, 'spam' => $spam ) ); 176 177 177 178 if ( !$max || $max >= (int) $this->activities['total'] ) … … 362 363 'secondary_id' => false, // secondary object ID to filter on e.g. a post_id 363 364 365 'meta_query' => false, // filter on activity meta. See WP_Meta_Query for format 366 364 367 // Searching 365 368 'search_terms' => false // specify terms to search on … … 455 458 'filter' => $filter, 456 459 'search_terms' => $search_terms, 460 'meta_query' => $meta_query, 457 461 'display_comments' => $display_comments, 458 462 'show_hidden' => $show_hidden, -
trunk/tests/testcases/activity/class.BP_Activity_Activity.php
r6907 r6948 73 73 $this->assertEquals( $activity['activities'][0]->hide_sitewide, 1 ); 74 74 } 75 76 public function test_get_meta_query() { 77 $a1 = $this->factory->activity->create(); 78 $a2 = $this->factory->activity->create(); 79 bp_activity_update_meta( $a1->id, 'foo', 'bar' ); 80 81 $activity = BP_Activity_Activity::get( array( 82 'meta_query' => array( 83 array( 84 'key' => 'foo', 85 'value' => 'bar', 86 ), 87 ), 88 ) ); 89 $ids = wp_list_pluck( $activity['activities'], 'id' ); 90 $this->assertEquals( $ids, array( $a1->id ) ); 91 } 75 92 } -
trunk/tests/testcases/activity/template.php
r6926 r6948 111 111 $activities_template = null; 112 112 } 113 114 /** 115 * Integration test for 'meta_query' param 116 */ 117 function test_bp_has_activities_with_meta_query() { 118 $a1 = $this->factory->activity->create(); 119 $a2 = $this->factory->activity->create(); 120 bp_activity_update_meta( $a1->id, 'foo', 'bar' ); 121 122 global $activities_template; 123 bp_has_activities( array( 124 'meta_query' => array( 125 array( 126 'key' => 'foo', 127 'value' => 'bar', 128 ), 129 ), 130 ) ); 131 132 $ids = wp_list_pluck( $activities_template->activities, 'id' ); 133 $this->assertEquals( $ids, array( $a1->id ) ); 134 } 113 135 }
Note: See TracChangeset
for help on using the changeset viewer.