Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
05/15/2016 02:21:54 AM (9 years ago)
Author:
boonebgorges
Message:

Introduce Group Types API.

Modeled on the Member Types API, Group Types allow developers to register
arbitrary group categorizations, and assign one or more of these types to a
given group. BuddyPress stores this information in a custom taxonomy on the
root blog.

Group queries can be filtered by group type, by using the group_type,
group_type__in, and group_type__not_in parameters in the bp_has_groups()
template loop function stack.

Props Mamaduka, boonebgorges, dcavins.
See #6784.

File:
1 edited

Legend:

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

    r10487 r10766  
    684684     *
    685685     * @since 1.6.0
     686     * @since 2.6.0 Added `$group_type`, `$group_type__in`, and `$group_type__not_in` parameters.
    686687     *
    687688     * @param array $args {
     
    705706     *                                           or descriptions match the search terms will be
    706707     *                                           returned. Default: false.
     708     *     @type array|string $group_type         Array or comma-separated list of group types to limit results to.
     709     *     @type array|string $group_type__in     Array or comma-separated list of group types to limit results to.
     710     *     @type array|string $group_type__not_in Array or comma-separated list of group types that will be
     711     *                                            excluded from results.
    707712     *     @type array        $meta_query        Optional. An array of meta_query conditions.
    708713     *                                           See {@link WP_Meta_Query::queries} for description.
     
    756761            'user_id'           => 0,
    757762            'search_terms'      => false,
     763            'group_type'         => '',
     764            'group_type__in'     => '',
     765            'group_type__not_in' => '',
    758766            'meta_query'        => false,
    759767            'include'           => false,
     
    803811        if ( ! empty( $meta_query_sql['where'] ) ) {
    804812            $sql['meta'] = $meta_query_sql['where'];
     813        }
     814
     815        // Only use 'group_type__in', if 'group_type' is not set.
     816        if ( empty( $r['group_type'] ) && ! empty( $r['group_type__in']) ) {
     817            $r['group_type'] = $r['group_type__in'];
     818        }
     819
     820        // Group types to exclude. This has priority over inclusions.
     821        if ( ! empty( $r['group_type__not_in'] ) ) {
     822            $group_type_clause = self::get_sql_clause_for_group_types( $r['group_type__not_in'], 'NOT IN' );
     823
     824        // Group types to include.
     825        } elseif ( ! empty( $r['group_type'] ) ) {
     826            $group_type_clause = self::get_sql_clause_for_group_types( $r['group_type'], 'IN' );
     827        }
     828
     829        if ( ! empty( $group_type_clause ) ) {
     830            $sql['group_type'] = $group_type_clause;
    805831        }
    806832
     
    914940            $meta_query_clause = preg_replace( '/^\s*AND/', '', $meta_query_sql['where'] );
    915941            $total_sql['where'][] = $meta_query_clause;
     942        }
     943
     944        // Trim leading 'AND' to match `$total_sql` query style.
     945        if ( ! empty( $group_type_clause ) ) {
     946            $total_sql['where'][] = preg_replace( '/^\s*AND\s*/', '', $group_type_clause );
    916947        }
    917948
     
    16251656        return $ids;
    16261657    }
     1658
     1659    /**
     1660     * Get SQL clause for group type(s).
     1661     *
     1662     * @since 2.6.0
     1663     *
     1664     * @param  string|array $group_types Group type(s).
     1665     * @param  string       $operator    'IN' or 'NOT IN'.
     1666     * @return string       $clause      SQL clause.
     1667     */
     1668    protected static function get_sql_clause_for_group_types( $group_types, $operator ) {
     1669        global $wpdb;
     1670
     1671        // Sanitize operator.
     1672        if ( 'NOT IN' !== $operator ) {
     1673            $operator = 'IN';
     1674        }
     1675
     1676        // Parse and sanitize types.
     1677        if ( ! is_array( $group_types ) ) {
     1678            $group_types = preg_split( '/[,\s+]/', $group_types );
     1679        }
     1680
     1681        $types = array();
     1682        foreach ( $group_types as $gt ) {
     1683            if ( bp_groups_get_group_type_object( $gt ) ) {
     1684                $types[] = $gt;
     1685            }
     1686        }
     1687
     1688        $tax_query = new WP_Tax_Query( array(
     1689            array(
     1690                'taxonomy' => 'bp_group_type',
     1691                'field'    => 'name',
     1692                'operator' => $operator,
     1693                'terms'    => $types,
     1694            ),
     1695        ) );
     1696
     1697        $switched = false;
     1698        if ( ! bp_is_root_blog() ) {
     1699            switch_to_blog( bp_get_root_blog_id() );
     1700            $switched = true;
     1701        }
     1702
     1703        $sql_clauses = $tax_query->get_sql( 'g', 'id' );
     1704
     1705        if ( $switched ) {
     1706            restore_current_blog();
     1707        }
     1708
     1709        $clause = '';
     1710
     1711        // The no_results clauses are the same between IN and NOT IN.
     1712        if ( false !== strpos( $sql_clauses['where'], '0 = 1' ) ) {
     1713            $clause = $sql_clauses['where'];
     1714
     1715        // The tax_query clause generated for NOT IN can be used almost as-is.
     1716        } elseif ( 'NOT IN' === $operator ) {
     1717            $clause = $sql_clauses['where'];
     1718
     1719        // IN clauses must be converted to a subquery.
     1720        } elseif ( preg_match( '/' . $wpdb->term_relationships . '\.term_taxonomy_id IN \([0-9, ]+\)/', $sql_clauses['where'], $matches ) ) {
     1721            $clause = " AND g.id IN ( SELECT object_id FROM $wpdb->term_relationships WHERE {$matches[0]} )";
     1722        }
     1723
     1724        return $clause;
     1725    }
    16271726}
Note: See TracChangeset for help on using the changeset viewer.