Changeset 10766 for trunk/src/bp-groups/classes/class-bp-groups-group.php
- Timestamp:
- 05/15/2016 02:21:54 AM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-groups/classes/class-bp-groups-group.php
r10487 r10766 684 684 * 685 685 * @since 1.6.0 686 * @since 2.6.0 Added `$group_type`, `$group_type__in`, and `$group_type__not_in` parameters. 686 687 * 687 688 * @param array $args { … … 705 706 * or descriptions match the search terms will be 706 707 * 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. 707 712 * @type array $meta_query Optional. An array of meta_query conditions. 708 713 * See {@link WP_Meta_Query::queries} for description. … … 756 761 'user_id' => 0, 757 762 'search_terms' => false, 763 'group_type' => '', 764 'group_type__in' => '', 765 'group_type__not_in' => '', 758 766 'meta_query' => false, 759 767 'include' => false, … … 803 811 if ( ! empty( $meta_query_sql['where'] ) ) { 804 812 $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; 805 831 } 806 832 … … 914 940 $meta_query_clause = preg_replace( '/^\s*AND/', '', $meta_query_sql['where'] ); 915 941 $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 ); 916 947 } 917 948 … … 1625 1656 return $ids; 1626 1657 } 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 } 1627 1726 }
Note: See TracChangeset
for help on using the changeset viewer.