Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
11/03/2024 10:39:48 PM (2 years ago)
Author:
espellcaste
Message:

Groups: add support to query for groups matching a taxonomy term.

Support was added (with unit tests) to pass a tax_query to bp_has_groups() and friends, to query for groups matching a taxonomy term.

Props boonebgorges, DJPaul, and imath.

Closes ​https://github.com/buddypress/buddypress/pull/402/
See #4017
Fixes #7877

File:
1 edited

Legend:

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

    r14077 r14078  
    10561056         *     @type array|string $group_type__not_in Array or comma-separated list of group types that will be
    10571057         *                                            excluded from results.
     1058         *     @type array        $tax_query          Optional. An array of tax_query conditions. See
     1059         *                                            {@link WP_Tax_Query::__construct()} for syntax.
    10581060         *     @type array        $meta_query         Optional. An array of meta_query conditions.
    10591061         *                                            See {@link WP_Meta_Query::queries} for description.
    … …  
    11331135                        'group_type__in'     => '',
    11341136                        'group_type__not_in' => '',
     1137                        'tax_query'          => false,
    11351138                        'meta_query'         => false,
    11361139                        'date_query'         => false,
    … …  
    12441247                        $group_type_clause = self::get_sql_clause_for_group_types( $r['group_type__not_in'], 'NOT IN' );
    12451248
    1246                 // Group types to include.
     1249                        // Group types to include.
    12471250                } elseif ( ! empty( $r['group_type'] ) ) {
    12481251                        $group_type_clause = self::get_sql_clause_for_group_types( $r['group_type'], 'IN' );
    … …  
    12511254                if ( ! empty( $group_type_clause ) ) {
    12521255                        $where_conditions['group_type'] = $group_type_clause;
     1256                }
     1257
     1258                $tax_query_sql = self::get_tax_query_sql( $r['tax_query'] );
     1259
     1260                if ( ! empty( $tax_query_sql['join'] ) ) {
     1261                        $sql['from'] .= $tax_query_sql['join'];
     1262                }
     1263
     1264                if ( ! empty( $tax_query_sql['where'] ) ) {
     1265                        $where_conditions['tax_query'] = $tax_query_sql['where'];
    12531266                }
    12541267
    … …  
    12921305                         * @since 2.1.0
    12931306                         *
    1294                          * @param array  $value Converted 'type' value for order and orderby.
    1295                          * @param string $value Parsed 'type' value for the get method.
     1307                         * @param array  $converted_type Converted 'type' value for order and orderby.
     1308                         * @param string $type          Parsed 'type' value for the get method.
    12961309                         */
    12971310                        $order_orderby = apply_filters( 'bp_groups_get_orderby', self::convert_type_to_order_orderby( $r['type'] ), $r['type'] );
    … …  
    13411354                 * @since 2.1.0
    13421355                 *
    1343                  * @param string $value  Converted 'orderby' term.
    1344                  * @param string $orderby Original orderby value.
    1345                  * @param string $value   Parsed 'type' value for the get method.
     1356                 * @param string $converted_orderby Converted 'orderby' term.
     1357                 * @param string $orderby           Original orderby value.
     1358                 * @param string $type              Parsed 'type' value for the get method.
    13461359                 */
    13471360                $orderby = apply_filters( 'bp_groups_get_orderby_converted_by_term', self::convert_orderby_to_order_by_term( $orderby ), $orderby, $r['type'] );
    … …  
    13711384                 * @since 1.5.0
    13721385                 *
    1373                  * @param string $value Concatenated SQL statement.
    1374                  * @param array  $sql   Array of SQL parts before concatenation.
    1375                  * @param array  $r     Array of parsed arguments for the get method.
     1386                 * @param string $paged_groups_sql Concatenated SQL statement.
     1387                 * @param array  $sql              Array of SQL parts before concatenation.
     1388                 * @param array  $r                Array of parsed arguments for the get method.
    13761389                 */
    13771390                $paged_groups_sql = apply_filters( 'bp_groups_get_paged_groups_sql', $paged_groups_sql, $sql, $r );
    … …  
    14501463                 * @since 1.5.0
    14511464                 *
    1452                  * @param string $t_sql     Concatenated SQL statement used for retrieving total group results.
    1453                  * @param array  $total_sql Array of SQL parts for the query.
    1454                  * @param array  $r         Array of parsed arguments for the get method.
     1465                 * @param string $total_groups_sql  Concatenated SQL statement used for retrieving total group results.
     1466                 * @param array  $sql              Array of SQL parts for the query.
     1467                 * @param array  $r                 Array of parsed arguments for the get method.
    14551468                 */
    14561469                $total_groups_sql = apply_filters( 'bp_groups_get_total_groups_sql', $total_groups_sql, $sql, $r );
    … …  
    14831496         *
    14841497         * @param array $meta_query An array of meta_query filters. See the
    1485          *                          documentation for {@link WP_Meta_Query} for details.
    1486          * @return array $sql_array 'join' and 'where' clauses.
     1498         *                          documentation for {@link WP_Meta_Query} for more details.
     1499         * @return array
    14871500         */
    14881501        protected static function get_meta_query_sql( $meta_query = array() ) {
    … …  
    14941507                );
    14951508
    1496                 if ( ! empty( $meta_query ) ) {
    1497                         $groups_meta_query = new WP_Meta_Query( $meta_query );
    1498 
    1499                         // WP_Meta_Query expects the table name at
    1500                         // $wpdb->group.
    1501                         $wpdb->groupmeta = buddypress()->groups->table_name_groupmeta;
    1502 
    1503                         $meta_sql = $groups_meta_query->get_sql( 'group', 'g', 'id' );
    1504                         $sql_array['join']  = $meta_sql['join'];
    1505                         $sql_array['where'] = self::strip_leading_and( $meta_sql['where'] );
    1506                 }
     1509                if ( empty( $meta_query ) ) {
     1510                        return $sql_array;
     1511                }
     1512
     1513                $groups_meta_query = new WP_Meta_Query( $meta_query );
     1514
     1515                // WP_Meta_Query expects the table name at $wpdb->group.
     1516                $wpdb->groupmeta = buddypress()->groups->table_name_groupmeta;
     1517
     1518                $meta_sql           = $groups_meta_query->get_sql( 'group', 'g', 'id' );
     1519                $sql_array['join']  = $meta_sql['join'];
     1520                $sql_array['where'] = self::strip_leading_and( $meta_sql['where'] );
    15071521
    15081522                return $sql_array;
     1523        }
     1524
     1525        /**
     1526         * Get the SQL for the 'tax_query' param in BP_Groups_Group::get().
     1527         *
     1528         * @param array $tax_query An array of tax query arguments. See the
     1529         *                         documentation for {@link WP_Tax_Query} for more details.
     1530         * @return array
     1531         */
     1532        protected static function get_tax_query_sql( $tax_query = array() ) {
     1533                $sql_array = array(
     1534                        'join'  => '',
     1535                        'where' => '',
     1536                );
     1537
     1538                if ( empty( $tax_query ) || ! is_array( $tax_query ) ) {
     1539                        return $sql_array;
     1540                }
     1541
     1542                $tax_query          = new WP_Tax_Query( $tax_query );
     1543                $tax_query_sql      = $tax_query->get_sql( 'g', 'id' );
     1544                $sql_array['join']  = $tax_query_sql['join'];
     1545                $sql_array['where'] = self::strip_leading_and( $tax_query_sql['where'] );
     1546
     1547                return $sql_array;
     1548        }
     1549
     1550        /**
     1551         * Get SQL clause for group type(s).
     1552         *
     1553         * @since 2.6.0
     1554         *
     1555         * @global wpdb $wpdb WordPress database object.
     1556         *
     1557         * @param  string|array $group_types Group type(s).
     1558         * @param  string       $operator    'IN' or 'NOT IN'.
     1559         *
     1560         * @return string SQL clause.
     1561         */
     1562        protected static function get_sql_clause_for_group_types( $group_types, $operator ) {
     1563                global $wpdb;
     1564
     1565                // Sanitize operator.
     1566                if ( 'NOT IN' !== $operator ) {
     1567                        $operator = 'IN';
     1568                }
     1569
     1570                // Parse and sanitize types.
     1571                if ( ! is_array( $group_types ) ) {
     1572                        $group_types = preg_split( '/[,\s+]/', $group_types );
     1573                }
     1574
     1575                $types = array();
     1576                foreach ( $group_types as $gt ) {
     1577                        if ( bp_groups_get_group_type_object( $gt ) ) {
     1578                                $types[] = $gt;
     1579                        }
     1580                }
     1581
     1582                $tax_query = new WP_Tax_Query(
     1583                        array(
     1584                                array(
     1585                                        'taxonomy' => bp_get_group_type_tax_name(),
     1586                                        'field'    => 'name',
     1587                                        'operator' => $operator,
     1588                                        'terms'    => $types,
     1589                                ),
     1590                        )
     1591                 );
     1592
     1593                $site_id  = bp_get_taxonomy_term_site_id( bp_get_group_type_tax_name() );
     1594                $switched = false;
     1595                if ( $site_id !== get_current_blog_id() ) {
     1596                        switch_to_blog( $site_id );
     1597                        $switched = true;
     1598                }
     1599
     1600                $sql_clauses = $tax_query->get_sql( 'g', 'id' );
     1601                $clause      = '';
     1602
     1603                // The no_results clauses are the same between IN and NOT IN.
     1604                if ( str_contains( $sql_clauses['where'], '0 = 1' ) ) {
     1605                        $clause = self::strip_leading_and( $sql_clauses['where'] );
     1606
     1607                        // The tax_query clause generated for NOT IN can be used almost as-is.
     1608                } elseif ( 'NOT IN' === $operator ) {
     1609                        $clause = self::strip_leading_and( $sql_clauses['where'] );
     1610
     1611                        // IN clauses must be converted to a subquery.
     1612                } elseif ( preg_match( '/' . $wpdb->term_relationships . '\.term_taxonomy_id IN \([0-9, ]+\)/', $sql_clauses['where'], $matches ) ) {
     1613                        $clause = " g.id IN ( SELECT object_id FROM $wpdb->term_relationships WHERE {$matches[0]} )";
     1614                }
     1615
     1616                if ( $switched ) {
     1617                        restore_current_blog();
     1618                }
     1619
     1620                return $clause;
    15091621        }
    15101622
    … …  
    15511663                }
    15521664
    1553                 return array( 'order' => $order, 'orderby' => $orderby );
     1665                return array(
     1666                        'order' => $order,
     1667                        'orderby' => $orderby
     1668                );
    15541669        }
    15551670
    … …  
    15601675         *
    15611676         * @param string $orderby Orderby term as passed to get().
    1562          * @return string $order_by_term SQL-friendly orderby term.
     1677         *
     1678         * @return string SQL-friendly orderby term.
    15631679         */
    15641680        protected static function convert_orderby_to_order_by_term( $orderby ) {
    1565                 $order_by_term = '';
    1566 
    15671681                switch ( $orderby ) {
    15681682                        case 'date_created' :
    … …  
    18461960         * @since 1.7.0
    18471961         *
     1962         * @global wpdb $wpdb WordPress database object.
     1963         *
    18481964         * @return array
    18491965         */
    … …  
    18631979
    18641980        /**
    1865          * Get SQL clause for group type(s).
    1866          *
    1867          * @since 2.6.0
    1868          *
    1869          * @param  string|array $group_types Group type(s).
    1870          * @param  string       $operator    'IN' or 'NOT IN'.
    1871          * @return string       $clause      SQL clause.
    1872          */
    1873         protected static function get_sql_clause_for_group_types( $group_types, $operator ) {
    1874                 global $wpdb;
    1875 
    1876                 // Sanitize operator.
    1877                 if ( 'NOT IN' !== $operator ) {
    1878                         $operator = 'IN';
    1879                 }
    1880 
    1881                 // Parse and sanitize types.
    1882                 if ( ! is_array( $group_types ) ) {
    1883                         $group_types = preg_split( '/[,\s+]/', $group_types );
    1884                 }
    1885 
    1886                 $types = array();
    1887                 foreach ( $group_types as $gt ) {
    1888                         if ( bp_groups_get_group_type_object( $gt ) ) {
    1889                                 $types[] = $gt;
    1890                         }
    1891                 }
    1892 
    1893                 $tax_query = new WP_Tax_Query( array(
    1894                         array(
    1895                                 'taxonomy' => bp_get_group_type_tax_name(),
    1896                                 'field'    => 'name',
    1897                                 'operator' => $operator,
    1898                                 'terms'    => $types,
    1899                         ),
    1900                 ) );
    1901 
    1902                 $site_id  = bp_get_taxonomy_term_site_id( bp_get_group_type_tax_name() );
    1903                 $switched = false;
    1904                 if ( $site_id !== get_current_blog_id() ) {
    1905                         switch_to_blog( $site_id );
    1906                         $switched = true;
    1907                 }
    1908 
    1909                 $sql_clauses = $tax_query->get_sql( 'g', 'id' );
    1910 
    1911                 $clause = '';
    1912 
    1913                 // The no_results clauses are the same between IN and NOT IN.
    1914                 if ( false !== strpos( $sql_clauses['where'], '0 = 1' ) ) {
    1915                         $clause = self::strip_leading_and( $sql_clauses['where'] );
    1916 
    1917                 // The tax_query clause generated for NOT IN can be used almost as-is.
    1918                 } elseif ( 'NOT IN' === $operator ) {
    1919                         $clause = self::strip_leading_and( $sql_clauses['where'] );
    1920 
    1921                 // IN clauses must be converted to a subquery.
    1922                 } elseif ( preg_match( '/' . $wpdb->term_relationships . '\.term_taxonomy_id IN \([0-9, ]+\)/', $sql_clauses['where'], $matches ) ) {
    1923                         $clause = " g.id IN ( SELECT object_id FROM $wpdb->term_relationships WHERE {$matches[0]} )";
    1924                 }
    1925 
    1926                 if ( $switched ) {
    1927                         restore_current_blog();
    1928                 }
    1929 
    1930                 return $clause;
    1931         }
    1932 
    1933         /**
    19341981         * Strips the leading AND and any surrounding whitespace from a string.
    19351982         *
Note: See TracChangeset for help on using the changeset viewer.