Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
04/26/2013 05:46:44 PM (13 years ago)
Author:
boonebgorges
Message:

Introduces meta_query paramater for bp_has_activities() stack

This will allow plugin and theme authors to filter the contents of the activity
stream based on information stored in the activitymeta table, using the
powerful meta_query syntax familiar from the 'meta_query' WP_Query param.

Also adds unit test for 'meta_query' in BP_Activity_Activity::get(), and
integration test for bp_has_activities(). These tests are primarily designed
to ensure that our param gets passed properly down the stack; we rely on WP
to get the meta_query logic right.

See #3521

Props ericlewis

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-activity/bp-activity-classes.php

    r6592 r6948  
    145145                        'exclude'          => false,      // Array of ids to exclude
    146146                        'in'               => false,      // Array of ids to limit query by (IN)
     147                        'meta_query'       => false,      // Filter by activitymeta
    147148                        'filter'           => false,      // See self::get_filter_sql()
    148149                        'search_terms'     => false,      // Terms to search by
     
    159160                $from_sql = " FROM {$bp->activity->table_name} a LEFT JOIN {$wpdb->users} u ON a.user_id = u.ID";
    160161
     162                $join_sql = '';
     163
    161164                // Where conditions
    162165                $where_conditions = array();
     
    198201                }
    199202
     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
    200214                // Alter the query based on whether we want to show activity item
    201215                // comments in the stream like normal comments or threaded below
     
    229243
    230244                        $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 ) );
    232246                } 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 );
    237251
    238252                $total_activities = $wpdb->get_var( $total_activities_sql );
     
    283297
    284298                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;
    285339        }
    286340
Note: See TracChangeset for help on using the changeset viewer.