Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
04/26/2013 06:50:47 PM (13 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

File:
1 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 ) {
Note: See TracChangeset for help on using the changeset viewer.