Skip to:
Content

BuddyPress.org

Changeset 6948


Ignore:
Timestamp:
04/26/2013 05:46:44 PM (7 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

Location:
trunk
Files:
5 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
  • trunk/bp-activity/bp-activity-functions.php

    r6925 r6948  
    882882
    883883        'search_terms'     => false,        // Pass search terms as a string
     884        'meta_query'       => false,        // Filter by activity meta. See WP_Meta_Query for format
    884885        'show_hidden'      => false,        // Show activity items that are hidden site-wide?
    885886        'exclude'          => false,        // Comma-separated list of activity IDs to exclude
     
    903904
    904905    // 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 ) {
    906907        if ( !$activity = wp_cache_get( 'bp_activity_sitewide_front', 'bp' ) ) {
    907908            $args = array(
     
    911912                'sort'             => $sort,
    912913                'search_terms'     => $search_terms,
     914                'meta_query'       => $meta_query,
    913915                'filter'           => $filter,
    914916                'display_comments' => $display_comments,
     
    927929            'sort'             => $sort,
    928930            'search_terms'     => $search_terms,
     931            'meta_query'       => $meta_query,
    929932            'filter'           => $filter,
    930933            'display_comments' => $display_comments,
  • trunk/bp-activity/bp-activity-template.php

    r6926 r6948  
    151151            'filter'           => false,
    152152            'search_terms'     => false,
     153            'meta_query'       => false,
    153154            'display_comments' => 'threaded',
    154155            'show_hidden'      => false,
     
    173174        // Fetch all activity items
    174175        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 ) );
    176177
    177178        if ( !$max || $max >= (int) $this->activities['total'] )
     
    362363        'secondary_id'     => false,        // secondary object ID to filter on e.g. a post_id
    363364
     365        'meta_query'       => false,        // filter on activity meta. See WP_Meta_Query for format
     366
    364367        // Searching
    365368        'search_terms'     => false         // specify terms to search on
     
    455458        'filter'           => $filter,
    456459        'search_terms'     => $search_terms,
     460        'meta_query'       => $meta_query,
    457461        'display_comments' => $display_comments,
    458462        'show_hidden'      => $show_hidden,
  • trunk/tests/testcases/activity/class.BP_Activity_Activity.php

    r6907 r6948  
    7373        $this->assertEquals( $activity['activities'][0]->hide_sitewide, 1 );
    7474    }
     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    }
    7592}
  • trunk/tests/testcases/activity/template.php

    r6926 r6948  
    111111        $activities_template = null;
    112112    }
     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    }
    113135}
Note: See TracChangeset for help on using the changeset viewer.