Skip to:
Content

BuddyPress.org

Changeset 6950


Ignore:
Timestamp:
04/26/2013 06:50:47 PM (7 years ago)
Author:
boonebgorges
Message:

Introduces a 'meta_query' parameter for the bp_has_groups() stack

This will allow plugin and theme authors to customize their group directories
based on data stored in the groupmeta table, using a powerful syntax familiar
from WP_Query.

Also adds unit tests for BP_Groups_Group::get() for the meta_query parameter,
as well as an integration test for the entire bp_has_groups() chain of
functions.

Fixes #3521

Location:
trunk
Files:
3 added
3 edited

Legend:

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

    r6712 r6950  
    335335            'user_id'         => 0,
    336336            'search_terms'    => false,
     337            'meta_query'      => false,
    337338            'include'         => false,
    338339            'populate_extras' => true,
     
    366367            $search_terms = like_escape( $wpdb->escape( $search_terms ) );
    367368            $sql['search'] = " AND ( g.name LIKE '%%{$search_terms}%%' OR g.description LIKE '%%{$search_terms}%%' )";
     369        }
     370
     371        $meta_query_sql = self::get_meta_query_sql( $meta_query );
     372
     373        if ( ! empty( $meta_query_sql['join'] ) ) {
     374            $sql['from'] .= $meta_query_sql['join'];
     375            $total_sql['select'] .= $meta_query_sql['join_total'];
     376        }
     377
     378        if ( ! empty( $meta_query_sql['where'] ) ) {
     379            $sql['meta'] = $meta_query_sql['where'];
    368380        }
    369381
     
    465477        return array( 'groups' => $paged_groups, 'total' => $total_groups );
    466478    }
     479
     480    /**
     481     * Get the SQL for the 'meta_query' param in BP_Activity_Activity::get()
     482     *
     483     * We use WP_Meta_Query to do the heavy lifting of parsing the
     484     * meta_query array and creating the necessary SQL clauses. However,
     485     * since BP_Activity_Activity::get() builds its SQL differently than
     486     * WP_Query, we have to alter the return value (stripping the leading
     487     * AND keyword from the 'where' clause).
     488     *
     489     * @since 1.8
     490     *
     491     * @param array $meta_query An array of meta_query filters. See the
     492     *   documentation for WP_Meta_Query for details.
     493     * @return array $sql_array 'join' and 'where' clauses
     494     */
     495    public static function get_meta_query_sql( $meta_query = array() ) {
     496        global $wpdb;
     497
     498        $sql_array = array(
     499            'join'  => '',
     500            'where' => '',
     501        );
     502
     503        if ( ! empty( $meta_query ) ) {
     504            $groups_meta_query = new WP_Meta_Query( $meta_query );
     505
     506            // WP_Meta_Query expects the table name at
     507            // $wpdb->group
     508            $wpdb->groupmeta = buddypress()->groups->table_name_groupmeta;
     509
     510            $meta_sql = $groups_meta_query->get_sql( 'group', 'g', 'id' );
     511
     512            // BP_Groups_Group::get uses the comma syntax for table
     513            // joins, which means that we have to do some regex to
     514            // convert the INNER JOIN and move the ON clause to a
     515            // WHERE condition
     516            //
     517            // @todo It may be better in the long run to refactor
     518            // the more general query syntax to accord better with
     519            // BP/WP convention
     520            preg_match( '/INNER JOIN (.*) ON/', $meta_sql['join'], $matches_a );
     521            preg_match( '/ON \((.*)\)$/', $meta_sql['join'], $matches_b );
     522            if ( ! empty( $matches_a[1] ) && ! empty( $matches_b[1] ) ) {
     523                $sql_array['join']  = $matches_a[1] . ', ';
     524                $sql_array['where'] = preg_replace( '/^(\sAND\s+[\(\s]+)/', '$1' . $matches_b[1] . ' AND ', $meta_sql['where'] );
     525            }
     526        }
     527
     528        return $sql_array;
     529    }
     530
    467531
    468532    function get_by_most_forum_topics( $limit = null, $page = null, $user_id = 0, $search_terms = false, $populate_extras = true, $exclude = false ) {
  • trunk/bp-groups/bp-groups-functions.php

    r6711 r6950  
    419419        'exclude'         => false,    // Do not include these specific groups (group_ids)
    420420        'search_terms'    => false,    // Limit to groups that match these search terms
     421        'meta_query'      => false,    // Filter by groupmeta. See WP_Meta_Query for syntax
    421422        'show_hidden'     => false,    // Show hidden groups to non-admins
    422423        'per_page'        => 20,       // The number of results to return per page
     
    433434        'exclude'         => $r['exclude'],
    434435        'search_terms'    => $r['search_terms'],
     436        'meta_query'      => $r['meta_query'],
    435437        'show_hidden'     => $r['show_hidden'],
    436438        'per_page'        => $r['per_page'],
  • trunk/bp-groups/bp-groups-template.php

    r6777 r6950  
    145145            'exclude'         => false,
    146146            'search_terms'    => '',
     147            'meta_query'      => false,
    147148            'populate_extras' => true
    148149        );
     
    170171                'user_id'         => $user_id,
    171172                'search_terms'    => $search_terms,
     173                'meta_query'      => $meta_query,
    172174                'include'         => $include,
    173175                'exclude'         => $exclude,
     
    308310        'slug'            => $slug,    // Pass a group slug to only return that group
    309311        'search_terms'    => '',       // Pass search terms to return only matching groups
     312        'meta_query'      => false,    // Filter by groupmeta. See WP_Meta_Query for format
    310313        'include'         => false,    // Pass comma separated list or array of group ID's to return only these groups
    311314        'exclude'         => false,    // Pass comma separated list or array of group ID's to exclude these groups
     
    335338        'slug'            => $r['slug'],
    336339        'search_terms'    => $r['search_terms'],
     340        'meta_query'      => $r['meta_query'],
    337341        'include'         => $r['include'],
    338342        'exclude'         => $r['exclude'],
Note: See TracChangeset for help on using the changeset viewer.