Changeset 14078 for trunk/src/bp-groups/classes/class-bp-groups-group.php
- Timestamp:
- 11/03/2024 10:39:48 PM (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-groups/classes/class-bp-groups-group.php
r14077 r14078 1056 1056 * @type array|string $group_type__not_in Array or comma-separated list of group types that will be 1057 1057 * excluded from results. 1058 * @type array $tax_query Optional. An array of tax_query conditions. See 1059 * {@link WP_Tax_Query::__construct()} for syntax. 1058 1060 * @type array $meta_query Optional. An array of meta_query conditions. 1059 1061 * See {@link WP_Meta_Query::queries} for description. … … 1133 1135 'group_type__in' => '', 1134 1136 'group_type__not_in' => '', 1137 'tax_query' => false, 1135 1138 'meta_query' => false, 1136 1139 'date_query' => false, … … 1244 1247 $group_type_clause = self::get_sql_clause_for_group_types( $r['group_type__not_in'], 'NOT IN' ); 1245 1248 1246 // Group types to include.1249 // Group types to include. 1247 1250 } elseif ( ! empty( $r['group_type'] ) ) { 1248 1251 $group_type_clause = self::get_sql_clause_for_group_types( $r['group_type'], 'IN' ); … … 1251 1254 if ( ! empty( $group_type_clause ) ) { 1252 1255 $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']; 1253 1266 } 1254 1267 … … 1292 1305 * @since 2.1.0 1293 1306 * 1294 * @param array $ value Converted 'type' value for order and orderby.1295 * @param string $ valueParsed '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. 1296 1309 */ 1297 1310 $order_orderby = apply_filters( 'bp_groups_get_orderby', self::convert_type_to_order_orderby( $r['type'] ), $r['type'] ); … … 1341 1354 * @since 2.1.0 1342 1355 * 1343 * @param string $ valueConverted 'orderby' term.1344 * @param string $orderby Original orderby value.1345 * @param string $ valueParsed '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. 1346 1359 */ 1347 1360 $orderby = apply_filters( 'bp_groups_get_orderby_converted_by_term', self::convert_orderby_to_order_by_term( $orderby ), $orderby, $r['type'] ); … … 1371 1384 * @since 1.5.0 1372 1385 * 1373 * @param string $ valueConcatenated 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. 1376 1389 */ 1377 1390 $paged_groups_sql = apply_filters( 'bp_groups_get_paged_groups_sql', $paged_groups_sql, $sql, $r ); … … 1450 1463 * @since 1.5.0 1451 1464 * 1452 * @param string $t _sqlConcatenated SQL statement used for retrieving total group results.1453 * @param array $ total_sqlArray 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. 1455 1468 */ 1456 1469 $total_groups_sql = apply_filters( 'bp_groups_get_total_groups_sql', $total_groups_sql, $sql, $r ); … … 1483 1496 * 1484 1497 * @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 1487 1500 */ 1488 1501 protected static function get_meta_query_sql( $meta_query = array() ) { … … 1494 1507 ); 1495 1508 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'] ); 1507 1521 1508 1522 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; 1509 1621 } 1510 1622 … … 1551 1663 } 1552 1664 1553 return array( 'order' => $order, 'orderby' => $orderby ); 1665 return array( 1666 'order' => $order, 1667 'orderby' => $orderby 1668 ); 1554 1669 } 1555 1670 … … 1560 1675 * 1561 1676 * @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. 1563 1679 */ 1564 1680 protected static function convert_orderby_to_order_by_term( $orderby ) { 1565 $order_by_term = '';1566 1567 1681 switch ( $orderby ) { 1568 1682 case 'date_created' : … … 1846 1960 * @since 1.7.0 1847 1961 * 1962 * @global wpdb $wpdb WordPress database object. 1963 * 1848 1964 * @return array 1849 1965 */ … … 1863 1979 1864 1980 /** 1865 * Get SQL clause for group type(s).1866 *1867 * @since 2.6.01868 *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 /**1934 1981 * Strips the leading AND and any surrounding whitespace from a string. 1935 1982 *
Note:
See TracChangeset
for help on using the changeset viewer.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)