BP_Groups_Group
BuddyPress Group object.
Description
Source
File: bp-groups/classes/class-bp-groups-group.php
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 | class BP_Groups_Group { /** * ID of the group. * * @since BuddyPress 1.6.0 * @var int */ public $id ; /** * User ID of the group's creator. * * @since BuddyPress 1.6.0 * @var int */ public $creator_id ; /** * Name of the group. * * @since BuddyPress 1.6.0 * @var string */ public $name ; /** * Group slug. * * @since BuddyPress 1.6.0 * @var string */ public $slug ; /** * Group description. * * @since BuddyPress 1.6.0 * @var string */ public $description ; /** * Group status. * * Core statuses are 'public', 'private', and 'hidden'. * * @since BuddyPress 1.6.0 * @var string */ public $status ; /** * Parent ID. * * ID of parent group, if applicable. * * @since BuddyPress 2.7.0 * @var int */ public $parent_id ; /** * Controls whether the group has a forum enabled. * * @since BuddyPress 1.6.0 * @since BuddyPress 3.0.0 Previously, this referred to Legacy Forums. It's still used by bbPress 2 for integration. * * @var int */ public $enable_forum ; /** * Date the group was created. * * @since BuddyPress 1.6.0 * @var string */ public $date_created ; /** * Data about the group's admins. * * @since BuddyPress 1.6.0 * @var array */ protected $admins ; /** * Data about the group's moderators. * * @since BuddyPress 1.6.0 * @var array */ protected $mods ; /** * Total count of group members. * * @since BuddyPress 1.6.0 * @var int */ protected $total_member_count ; /** * Is the current user a member of this group? * * @since BuddyPress 1.2.0 * @var bool */ protected $is_member ; /** * Is the current user a member of this group? * Alias of $is_member for backward compatibility. * * @since BuddyPress 2.9.0 * @var bool */ protected $is_user_member ; /** * Does the current user have an outstanding invitation to this group? * * @since BuddyPress 1.9.0 * @var bool */ protected $is_invited ; /** * Does the current user have a pending membership request to this group? * * @since BuddyPress 1.9.0 * @var bool */ protected $is_pending ; /** * Timestamp of the last activity that happened in this group. * * @since BuddyPress 1.2.0 * @var string */ protected $last_activity ; /** * If this is a private or hidden group, does the current user have access? * * @since BuddyPress 1.6.0 * @var bool */ protected $user_has_access ; /** * Can the current user know that this group exists? * * @since BuddyPress 2.9.0 * @var bool */ protected $is_visible ; /** * Raw arguments passed to the constructor. * * @since BuddyPress 2.0.0 * @var array */ public $args ; /** * Constructor method. * * @since BuddyPress 1.6.0 * * @param int|null $id Optional. If the ID of an existing group is provided, * the object will be pre-populated with info about that group. * @param array $args { * Array of optional arguments. * @type bool $populate_extras Deprecated. * } */ public function __construct( $id = null, $args = array () ) { if ( ! empty ( $id ) ) { $this ->id = (int) $id ; $this ->populate(); } } /** * Set up data about the current group. * * @since BuddyPress 1.6.0 */ public function populate() { global $wpdb ; // Get BuddyPress. $bp = buddypress(); // Check cache for group data. $group = wp_cache_get( $this ->id, 'bp_groups' ); // Cache missed, so query the DB. if ( false === $group ) { $group = $wpdb ->get_row( $wpdb ->prepare( "SELECT g.* FROM {$bp->groups->table_name} g WHERE g.id = %d" , $this ->id ) ); wp_cache_set( $this ->id, $group , 'bp_groups' ); } // No group found so set the ID and bail. if ( empty ( $group ) || is_wp_error( $group ) ) { $this ->id = 0; return ; } // Group found so setup the object variables. $this ->id = (int) $group ->id; $this ->creator_id = (int) $group ->creator_id; $this ->name = stripslashes ( $group ->name ); $this ->slug = $group ->slug; $this ->description = stripslashes ( $group ->description ); $this ->status = $group ->status; $this ->parent_id = (int) $group ->parent_id; $this ->enable_forum = (int) $group ->enable_forum; $this ->date_created = $group ->date_created; } /** * Save the current group to the database. * * @since BuddyPress 1.6.0 * * @return bool True on success, false on failure. */ public function save() { global $wpdb ; $bp = buddypress(); $this ->creator_id = apply_filters( 'groups_group_creator_id_before_save' , $this ->creator_id, $this ->id ); $this ->name = apply_filters( 'groups_group_name_before_save' , $this ->name, $this ->id ); $this ->slug = apply_filters( 'groups_group_slug_before_save' , $this ->slug, $this ->id ); $this ->description = apply_filters( 'groups_group_description_before_save' , $this ->description, $this ->id ); $this ->status = apply_filters( 'groups_group_status_before_save' , $this ->status, $this ->id ); $this ->parent_id = apply_filters( 'groups_group_parent_id_before_save' , $this ->parent_id, $this ->id ); $this ->enable_forum = apply_filters( 'groups_group_enable_forum_before_save' , $this ->enable_forum, $this ->id ); $this ->date_created = apply_filters( 'groups_group_date_created_before_save' , $this ->date_created, $this ->id ); /** * Fires before the current group item gets saved. * * Please use this hook to filter the properties above. Each part will be passed in. * * @since BuddyPress 1.0.0 * * @param BP_Groups_Group $this Current instance of the group item being saved. Passed by reference. */ do_action_ref_array( 'groups_group_before_save' , array ( & $this ) ); // Groups need at least a name. if ( empty ( $this ->name ) ) { return false; } // Set slug with group title if not passed. if ( empty ( $this ->slug ) ) { $this ->slug = sanitize_title( $this ->name ); } // Sanity check. if ( empty ( $this ->slug ) ) { return false; } // Check for slug conflicts if creating new group. if ( empty ( $this ->id ) ) { $this ->slug = groups_check_slug( $this ->slug ); } if ( ! empty ( $this ->id ) ) { $sql = $wpdb ->prepare( "UPDATE { $bp ->groups->table_name} SET creator_id = %d, name = %s, slug = %s, description = %s, status = %s, parent_id = %d, enable_forum = %d, date_created = %s WHERE id = %d ", $this ->creator_id, $this ->name, $this ->slug, $this ->description, $this ->status, $this ->parent_id, $this ->enable_forum, $this ->date_created, $this ->id ); } else { $sql = $wpdb ->prepare( "INSERT INTO { $bp ->groups->table_name} ( creator_id, name, slug, description, status, parent_id, enable_forum, date_created ) VALUES ( %d, %s, %s, %s, %s, %d, %d, %s )", $this ->creator_id, $this ->name, $this ->slug, $this ->description, $this ->status, $this ->parent_id, $this ->enable_forum, $this ->date_created ); } if ( false === $wpdb ->query( $sql ) ) return false; if ( empty ( $this ->id ) ) $this ->id = $wpdb ->insert_id; /** * Fires after the current group item has been saved. * * @since BuddyPress 1.0.0 * * @param BP_Groups_Group $this Current instance of the group item that was saved. Passed by reference. */ do_action_ref_array( 'groups_group_after_save' , array ( & $this ) ); wp_cache_delete( $this ->id, 'bp_groups' ); return true; } /** * Delete the current group. * * @since BuddyPress 1.6.0 * * @return bool True on success, false on failure. */ public function delete () { global $wpdb ; // Delete groupmeta for the group. groups_delete_groupmeta( $this ->id ); // Fetch the user IDs of all the members of the group. $user_ids = BP_Groups_Member::get_group_member_ids( $this ->id ); $user_id_str = esc_sql( implode( ',' , wp_parse_id_list( $user_ids ) ) ); // Modify group count usermeta for members. $wpdb ->query( "UPDATE {$wpdb->usermeta} SET meta_value = meta_value - 1 WHERE meta_key = 'total_group_count' AND user_id IN ( {$user_id_str} )" ); // Now delete all group member entries. BP_Groups_Member::delete_all( $this ->id ); /** * Fires before the deletion of a group. * * @since BuddyPress 1.2.0 * * @param BP_Groups_Group $this Current instance of the group item being deleted. Passed by reference. * @param array $user_ids Array of user IDs that were members of the group. */ do_action_ref_array( 'bp_groups_delete_group' , array ( & $this , $user_ids ) ); wp_cache_delete( $this ->id, 'bp_groups' ); $bp = buddypress(); // Finally remove the group entry from the DB. if ( ! $wpdb ->query( $wpdb ->prepare( "DELETE FROM {$bp->groups->table_name} WHERE id = %d" , $this ->id ) ) ) { return false; } // delete group avatars $upload_path = bp_core_avatar_upload_path(); system( "rm -rf " . escapeshellarg ( $upload_path . '/group-avatars/' . $this ->id )); // delete group avatars $bp_attachments_uploads_dir = bp_attachments_uploads_dir_get(); $type_dir = trailingslashit( $bp_attachments_uploads_dir [ 'basedir' ] ); system( "rm -rf " . escapeshellarg ( $type_dir . 'groups/' . $this ->id ) ); return true; } /** * Magic getter. * * @since BuddyPress 2.7.0 * * @param string $key Property name. * @return mixed */ public function __get( $key ) { switch ( $key ) { case 'last_activity' : case 'total_member_count' : case 'forum_id' : $retval = groups_get_groupmeta( $this ->id, $key ); if ( 'last_activity' !== $key ) { $retval = (int) $retval ; } return $retval ; case 'admins' : return $this ->get_admins(); case 'mods' : return $this ->get_mods(); case 'is_member' : case 'is_user_member' : return $this ->get_is_member(); case 'is_invited' : return groups_check_user_has_invite( bp_loggedin_user_id(), $this ->id ); case 'is_pending' : return groups_check_for_membership_request( bp_loggedin_user_id(), $this ->id ); case 'user_has_access' : return $this ->get_user_has_access(); case 'is_visible' : return $this ->is_visible(); default : return isset( $this ->{ $key } ) ? $this ->{ $key } : null; } } /** * Magic issetter. * * Used to maintain backward compatibility for properties that are now * accessible only via magic method. * * @since BuddyPress 2.7.0 * * @param string $key Property name. * @return bool */ public function __isset( $key ) { switch ( $key ) { case 'admins' : case 'is_invited' : case 'is_member' : case 'is_user_member' : case 'is_pending' : case 'last_activity' : case 'mods' : case 'total_member_count' : case 'user_has_access' : case 'is_visible' : case 'forum_id' : return true; default : return isset( $this ->{ $key } ); } } /** * Magic setter. * * Used to maintain backward compatibility for properties that are now * accessible only via magic method. * * @since BuddyPress 2.7.0 * * @param string $key Property name. * @param mixed $value Property value. * @return bool */ public function __set( $key , $value ) { switch ( $key ) { case 'user_has_access' : return $this ->user_has_access = (bool) $value ; default : $this ->{ $key } = $value ; } } /** * Get a list of the group's admins. * * Used to provide cache-friendly access to the 'admins' property of * the group object. * * @since BuddyPress 2.7.0 * * @return array|null */ protected function get_admins() { if ( isset( $this ->admins ) ) { return $this ->admins; } $this ->set_up_admins_and_mods(); return $this ->admins; } /** * Get a list of the group's mods. * * Used to provide cache-friendly access to the 'mods' property of * the group object. * * @since BuddyPress 2.7.0 * * @return array|null */ protected function get_mods() { if ( isset( $this ->mods ) ) { return $this ->mods; } $this ->set_up_admins_and_mods(); return $this ->mods; } /** * Set up admins and mods for the current group object. * * Called only when the 'admins' or 'mods' property is accessed. * * @since BuddyPress 2.7.0 */ protected function set_up_admins_and_mods() { $admin_ids = BP_Groups_Member::get_group_administrator_ids( $this ->id ); $admin_ids_plucked = wp_list_pluck( $admin_ids , 'user_id' ); $mod_ids = BP_Groups_Member::get_group_moderator_ids( $this ->id ); $mod_ids_plucked = wp_list_pluck( $mod_ids , 'user_id' ); $admin_mod_ids = array_merge ( $admin_ids_plucked , $mod_ids_plucked ); $admin_mod_users = array (); if ( ! empty ( $admin_mod_ids ) ) { $admin_mod_users = get_users( array ( 'include' => $admin_mod_ids , 'blog_id' => null, ) ); } $admin_objects = $mod_objects = array (); foreach ( $admin_mod_users as $admin_mod_user ) { $obj = new stdClass(); $obj ->user_id = $admin_mod_user ->ID; $obj ->user_login = $admin_mod_user ->user_login; $obj ->user_email = $admin_mod_user ->user_email; $obj ->user_nicename = $admin_mod_user ->user_nicename; if ( in_array( $admin_mod_user ->ID, $admin_ids_plucked , true ) ) { $obj ->is_admin = 1; $obj ->is_mod = 0; $admin_objects [] = $obj ; } else { $obj ->is_admin = 0; $obj ->is_mod = 1; $mod_objects [] = $obj ; } } $this ->admins = $admin_objects ; $this ->mods = $mod_objects ; } /** * Checks whether the logged-in user is a member of the group. * * @since BuddyPress 2.7.0 * * @return bool|int */ protected function get_is_member() { if ( isset( $this ->is_member ) ) { return $this ->is_member; } $this ->is_member = groups_is_user_member( bp_loggedin_user_id(), $this ->id ); return $this ->is_member; } /** * Checks whether the logged-in user has access to the group. * * @since BuddyPress 2.7.0 * * @return bool */ protected function get_user_has_access() { if ( isset( $this ->user_has_access ) ) { return $this ->user_has_access; } if ( ( 'private' === $this ->status ) || ( 'hidden' === $this ->status ) ) { // Assume user does not have access to hidden/private groups. $this ->user_has_access = false; // Group members or community moderators have access. if ( ( is_user_logged_in() && $this ->get_is_member() ) || bp_current_user_can( 'bp_moderate' ) ) { $this ->user_has_access = true; } } else { $this ->user_has_access = true; } return $this ->user_has_access; } /** * Checks whether the current user can know the group exists. * * @since BuddyPress 2.9.0 * * @return bool */ protected function is_visible() { if ( isset( $this ->is_visible ) ) { return $this ->is_visible; } if ( 'hidden' === $this ->status ) { // Assume user can not know about hidden groups. $this ->is_visible = false; // Group members or community moderators have access. if ( ( is_user_logged_in() && $this ->get_is_member() ) || bp_current_user_can( 'bp_moderate' ) ) { $this ->is_visible = true; } } else { $this ->is_visible = true; } return $this ->is_visible; } /** Static Methods ****************************************************/ /** * Get whether a group exists for a given slug. * * @since BuddyPress 1.6.0 * * @param string $slug Slug to check. * @param string|bool $table_name Deprecated. * @return int|null Group ID if found; null if not. */ public static function group_exists( $slug , $table_name = false ) { global $wpdb ; if ( empty ( $slug ) ) { return false; } $args = array ( 'slug' => $slug , 'per_page' => 1, 'page' => 1, 'update_meta_cache' => false, 'show_hidden' => true, ); $groups = BP_Groups_Group::get( $args ); $group_id = null; if ( $groups [ 'groups' ] ) { $group_id = current( $groups [ 'groups' ] )->id; } return $group_id ; } /** * Get the ID of a group by the group's slug. * * Alias of {@link BP_Groups_Group::group_exists()}. * * @since BuddyPress 1.6.0 * * @param string $slug See {@link BP_Groups_Group::group_exists()}. * @return int|null See {@link BP_Groups_Group::group_exists()}. */ public static function get_id_from_slug( $slug ) { return BP_Groups_Group::group_exists( $slug ); } /** * Get whether a group exists for an old slug. * * @since BuddyPress 2.9.0 * * @param string $slug Slug to check. * * @return int|null|false Group ID if found; null if not; false if missing parameters. */ public static function get_id_by_previous_slug( $slug ) { global $wpdb ; if ( empty ( $slug ) ) { return false; } $args = array ( 'meta_query' => array ( array ( 'key' => 'previous_slug' , 'value' => $slug ), ), 'orderby' => 'meta_id' , 'order' => 'DESC' , 'per_page' => 1, 'page' => 1, 'update_meta_cache' => false, 'show_hidden' => true, ); $groups = BP_Groups_Group::get( $args ); $group_id = null; if ( $groups [ 'groups' ] ) { $group_id = current( $groups [ 'groups' ] )->id; } return $group_id ; } /** * Get IDs of users with outstanding invites to a given group from a specified user. * * @since BuddyPress 1.6.0 * @since BuddyPress 2.9.0 Added $sent as a parameter. * * @param int $user_id ID of the inviting user. * @param int $group_id ID of the group. * @param int|null $sent Query for a specific invite sent status. If 0, this will query for users * that haven't had an invite sent to them yet. If 1, this will query for * users that have had an invite sent to them. If null, no invite status will * queried. Default: null. * @return array IDs of users who have been invited to the group by the user but have not * yet accepted. */ public static function get_invites( $user_id , $group_id , $sent = null ) { global $wpdb ; $bp = buddypress(); $sql = $wpdb ->prepare( "SELECT user_id FROM {$bp->groups->table_name_members} WHERE group_id = %d and is_confirmed = 0 AND inviter_id = %d" , $group_id , $user_id ); // Query for a specific invite sent status. if ( ! is_null ( $sent ) ) { $sql .= $wpdb ->prepare( ' AND invite_sent = %d' , $sent ); } return $wpdb ->get_col( $sql ); } /** * Get a list of a user's groups, filtered by a search string. * * @since BuddyPress 1.6.0 * * @param string $filter Search term. Matches against 'name' and * 'description' fields. * @param int $user_id ID of the user whose groups are being searched. * Default: the displayed user. * @param mixed $order Not used. * @param int|null $limit Optional. The max number of results to return. * Default: null (no limit). * @param int|null $page Optional. The page offset of results to return. * Default: null (no limit). * @return false|array { * @type array $groups Array of matched and paginated group IDs. * @type int $total Total count of groups matching the query. * } */ public static function filter_user_groups( $filter , $user_id = 0, $order = false, $limit = null, $page = null ) { if ( empty ( $user_id ) ) { $user_id = bp_displayed_user_id(); } $args = array ( 'search_terms' => $filter , 'user_id' => $user_id , 'per_page' => $limit , 'page' => $page , 'order' => $order , ); $groups = BP_Groups_Group::get( $args ); // Modify the results to match the old format. $paged_groups = array (); $i = 0; foreach ( $groups [ 'groups' ] as $group ) { $paged_groups [ $i ] = new stdClass; $paged_groups [ $i ]->group_id = $group ->id; $i ++; } return array ( 'groups' => $paged_groups , 'total' => $groups [ 'total' ] ); } /** * Get a list of groups, filtered by a search string. * * @since BuddyPress 1.6.0 * * @param string $filter Search term. Matches against 'name' and * 'description' fields. * @param int|null $limit Optional. The max number of results to return. * Default: null (no limit). * @param int|null $page Optional. The page offset of results to return. * Default: null (no limit). * @param string|bool $sort_by Column to sort by. Default: false (default * sort). * @param string|bool $order ASC or DESC. Default: false (default sort). * @return array { * @type array $groups Array of matched and paginated group IDs. * @type int $total Total count of groups matching the query. * } */ public static function search_groups( $filter , $limit = null, $page = null, $sort_by = false, $order = false ) { $args = array ( 'search_terms' => $filter , 'per_page' => $limit , 'page' => $page , 'orderby' => $sort_by , 'order' => $order , ); $groups = BP_Groups_Group::get( $args ); // Modify the results to match the old format. $paged_groups = array (); $i = 0; foreach ( $groups [ 'groups' ] as $group ) { $paged_groups [ $i ] = new stdClass; $paged_groups [ $i ]->group_id = $group ->id; $i ++; } return array ( 'groups' => $paged_groups , 'total' => $groups [ 'total' ] ); } /** * Check for the existence of a slug. * * @since BuddyPress 1.6.0 * * @param string $slug Slug to check. * @return string|null The slug, if found. Otherwise null. */ public static function check_slug( $slug ) { global $wpdb ; $bp = buddypress(); return $wpdb ->get_var( $wpdb ->prepare( "SELECT slug FROM {$bp->groups->table_name} WHERE slug = %s" , $slug ) ); } /** * Get the slug for a given group ID. * * @since BuddyPress 1.6.0 * * @param int $group_id ID of the group. * @return string|null The slug, if found. Otherwise null. */ public static function get_slug( $group_id ) { global $wpdb ; $bp = buddypress(); return $wpdb ->get_var( $wpdb ->prepare( "SELECT slug FROM {$bp->groups->table_name} WHERE id = %d" , $group_id ) ); } /** * Check whether a given group has any members. * * @since BuddyPress 1.6.0 * * @param int $group_id ID of the group. * @return bool True if the group has members, otherwise false. */ public static function has_members( $group_id ) { global $wpdb ; $bp = buddypress(); $members = $wpdb ->get_var( $wpdb ->prepare( "SELECT COUNT(id) FROM {$bp->groups->table_name_members} WHERE group_id = %d" , $group_id ) ); if ( empty ( $members ) ) return false; return true; } /** * Check whether a group has outstanding membership requests. * * @since BuddyPress 1.6.0 * * @param int $group_id ID of the group. * @return int|null The number of outstanding requests, or null if * none are found. */ public static function has_membership_requests( $group_id ) { global $wpdb ; $bp = buddypress(); return $wpdb ->get_var( $wpdb ->prepare( "SELECT COUNT(id) FROM {$bp->groups->table_name_members} WHERE group_id = %d AND is_confirmed = 0" , $group_id ) ); } /** * Get outstanding membership requests for a group. * * @since BuddyPress 1.6.0 * * @param int $group_id ID of the group. * @param int|null $limit Optional. Max number of results to return. * Default: null (no limit). * @param int|null $page Optional. Page offset of results returned. Default: * null (no limit). * @return array { * @type array $requests The requested page of located requests. * @type int $total Total number of requests outstanding for the * group. * } */ public static function get_membership_requests( $group_id , $limit = null, $page = null ) { global $wpdb ; if ( ! empty ( $limit ) && ! empty ( $page ) ) { $pag_sql = $wpdb ->prepare( " LIMIT %d, %d" , intval ( ( $page - 1 ) * $limit ), intval ( $limit ) ); } $bp = buddypress(); $paged_requests = $wpdb ->get_results( $wpdb ->prepare( "SELECT * FROM {$bp->groups->table_name_members} WHERE group_id = %d AND is_confirmed = 0 AND inviter_id = 0{$pag_sql}" , $group_id ) ); $total_requests = $wpdb ->get_var( $wpdb ->prepare( "SELECT COUNT(id) FROM {$bp->groups->table_name_members} WHERE group_id = %d AND is_confirmed = 0 AND inviter_id = 0" , $group_id ) ); return array ( 'requests' => $paged_requests , 'total' => $total_requests ); } /** * Query for groups. * * @see WP_Meta_Query::queries for a description of the 'meta_query' * parameter format. * * @since BuddyPress 1.6.0 * @since BuddyPress 2.6.0 Added `$group_type`, `$group_type__in`, and `$group_type__not_in` parameters. * @since BuddyPress 2.7.0 Added `$update_admin_cache` and `$parent_id` parameters. * @since BuddyPress 2.8.0 Changed `$search_terms` parameter handling and added `$search_columns` parameter. * @since BuddyPress 2.9.0 Added `$slug` parameter. * * @param array $args { * Array of parameters. All items are optional. * @type string $type Optional. Shorthand for certain orderby/order combinations. * 'newest', 'active', 'popular', 'alphabetical', 'random'. * When present, will override orderby and order params. * Default: null. * @type string $orderby Optional. Property to sort by. 'date_created', 'last_activity', * 'total_member_count', 'name', 'random', 'meta_id'. * Default: 'date_created'. * @type string $order Optional. Sort order. 'ASC' or 'DESC'. Default: 'DESC'. * @type int $per_page Optional. Number of items to return per page of results. * Default: null (no limit). * @type int $page Optional. Page offset of results to return. * Default: null (no limit). * @type int $user_id Optional. If provided, results will be limited to groups * of which the specified user is a member. Default: null. * @type array|string $slug Optional. Array or comma-separated list of group slugs to limit * results to. * Default: false. * @type string $search_terms Optional. If provided, only groups whose names or descriptions * match the search terms will be returned. Allows specifying the * wildcard position using a '*' character before or after the * string or both. Works in concert with $search_columns. * Default: false. * @type string $search_columns Optional. If provided, only apply the search terms to the * specified columns. Works in concert with $search_terms. * Default: empty array. * @type array|string $group_type Array or comma-separated list of group types to limit results to. * @type array|string $group_type__in Array or comma-separated list of group types to limit results to. * @type array|string $group_type__not_in Array or comma-separated list of group types that will be * excluded from results. * @type array $meta_query Optional. An array of meta_query conditions. * See {@link WP_Meta_Query::queries} for description. * @type array|string $value Optional. Array or comma-separated list of group IDs. Results * will be limited to groups within the list. Default: false. * @type array|string $parent_id Optional. Array or comma-separated list of group IDs. Results * will be limited to children of the specified groups. Default: null. * @type array|string $exclude Optional. Array or comma-separated list of group IDs. * Results will exclude the listed groups. Default: false. * @type bool $update_meta_cache Whether to pre-fetch groupmeta for the returned groups. * Default: true. * @type bool $update_admin_cache Whether to pre-fetch administrator IDs for the returned * groups. Default: false. * @type bool $show_hidden Whether to include hidden groups in results. Default: false. * @type array|string $status Optional. Array or comma-separated list of group statuses to limit * results to. If specified, $show_hidden is ignored. * Default: empty array. * @type string $fields Which fields to return. Specify 'ids' to fetch a list of IDs. * Default: 'all' (return BP_Groups_Group objects). * If set, meta and admin caches will not be prefetched. * } * @return array { * @type array $groups Array of group objects returned by the * paginated query. (IDs only if `fields` is set to `ids`.) * @type int $total Total count of all groups matching non- * paginated query params. * } */ public static function get( $args = array () ) { global $wpdb ; // Backward compatibility with old method of passing arguments. if ( ! is_array ( $args ) || func_num_args() > 1 ) { _deprecated_argument( __METHOD__ , '1.7' , sprintf( __( 'Arguments passed to %1$s should be in an associative array. See the inline documentation at %2$s for more details.' , 'buddyboss' ), __METHOD__ , __FILE__ ) ); $old_args_keys = array ( 0 => 'type' , 1 => 'per_page' , 2 => 'page' , 3 => 'user_id' , 4 => 'search_terms' , 5 => 'include' , 6 => 'populate_extras' , 7 => 'exclude' , 8 => 'show_hidden' , ); $args = bp_core_parse_args_array( $old_args_keys , func_get_args() ); } $defaults = array ( 'type' => null, 'orderby' => 'date_created' , 'order' => 'DESC' , 'per_page' => null, 'page' => null, 'user_id' => 0, 'slug' => array (), 'search_terms' => false, 'search_columns' => array (), 'group_type' => '' , 'group_type__in' => '' , 'group_type__not_in' => '' , 'meta_query' => false, 'include' => false, 'parent_id' => null, 'update_meta_cache' => true, 'update_admin_cache' => false, 'exclude' => false, 'show_hidden' => false, 'status' => array (), 'fields' => 'all' , ); $r = bp_parse_args( $args , $defaults , 'bp_groups_group_get' ); $bp = buddypress(); $sql = array ( 'select' => "SELECT DISTINCT g.id" , 'from' => "{$bp->groups->table_name} g" , 'where' => '' , 'orderby' => '' , 'pagination' => '' , ); if ( ! empty ( $r [ 'user_id' ] ) ) { $sql [ 'from' ] .= " JOIN {$bp->groups->table_name_members} m ON ( g.id = m.group_id )" ; } $where_conditions = array (); if ( ! empty ( $r [ 'status' ] ) ) { if ( ! is_array ( $r [ 'status' ] ) ) { $r [ 'status' ] = preg_split( '/[\s,]+/' , $r [ 'status' ] ); } $r [ 'status' ] = array_map ( 'sanitize_title' , $r [ 'status' ] ); $status_in = "'" . implode( "','" , $r ['status '] ) . "' "; $where_conditions [ 'status' ] = "g.status IN ({$status_in})" ; } elseif ( empty ( $r [ 'show_hidden' ] ) ) { $where_conditions [ 'hidden' ] = "g.status != 'hidden'" ; } if ( ! empty ( $r [ 'slug' ] ) ) { if ( ! is_array ( $r [ 'slug' ] ) ) { $r [ 'slug' ] = preg_split( '/[\s,]+/' , $r [ 'slug' ] ); } $r [ 'slug' ] = array_map ( 'sanitize_title' , $r [ 'slug' ] ); $slug_in = "'" . implode( "','" , $r ['slug '] ) . "' "; $where_conditions [ 'slug' ] = "g.slug IN ({$slug_in})" ; } $search = '' ; if ( isset( $r [ 'search_terms' ] ) ) { $search = trim( $r [ 'search_terms' ] ); } if ( $search ) { $leading_wild = ( ltrim( $search , '*' ) != $search ); $trailing_wild = ( rtrim( $search , '*' ) != $search ); if ( $leading_wild && $trailing_wild ) { $wild = 'both' ; } elseif ( $leading_wild ) { $wild = 'leading' ; } elseif ( $trailing_wild ) { $wild = 'trailing' ; } else { // Default is to wrap in wildcard characters. $wild = 'both' ; } $search = trim( $search , '*' ); $searches = array (); $leading_wild = ( 'leading' == $wild || 'both' == $wild ) ? '%' : '' ; $trailing_wild = ( 'trailing' == $wild || 'both' == $wild ) ? '%' : '' ; $wildcarded = $leading_wild . bp_esc_like( $search ) . $trailing_wild ; $search_columns = array ( 'name' , 'description' ); if ( $r [ 'search_columns' ] ) { $search_columns = array_intersect ( $r [ 'search_columns' ], $search_columns ); } foreach ( $search_columns as $search_column ) { $searches [] = $wpdb ->prepare( "$search_column LIKE %s" , $wildcarded ); } $where_conditions [ 'search' ] = '(' . implode( ' OR ' , $searches ) . ')' ; } $meta_query_sql = self::get_meta_query_sql( $r [ 'meta_query' ] ); if ( ! empty ( $meta_query_sql [ 'join' ] ) ) { $sql [ 'from' ] .= $meta_query_sql [ 'join' ]; } if ( ! empty ( $meta_query_sql [ 'where' ] ) ) { $where_conditions [ 'meta' ] = $meta_query_sql [ 'where' ]; } // Only use 'group_type__in', if 'group_type' is not set. if ( empty ( $r [ 'group_type' ] ) && ! empty ( $r [ 'group_type__in' ]) ) { $r [ 'group_type' ] = $r [ 'group_type__in' ]; } // Group types to exclude. This has priority over inclusions. if ( ! empty ( $r [ 'group_type__not_in' ] ) ) { $group_type_clause = self::get_sql_clause_for_group_types( $r [ 'group_type__not_in' ], 'NOT IN' ); // Group types to include. } elseif ( ! empty ( $r [ 'group_type' ] ) ) { $group_type_clause = self::get_sql_clause_for_group_types( $r [ 'group_type' ], 'IN' ); } if ( ! empty ( $group_type_clause ) ) { $where_conditions [ 'group_type' ] = $group_type_clause ; } if ( ! empty ( $r [ 'user_id' ] ) ) { $where_conditions [ 'user' ] = $wpdb ->prepare( "m.user_id = %d AND m.is_confirmed = 1 AND m.is_banned = 0" , $r [ 'user_id' ] ); } if ( ! empty ( $r [ 'include' ] ) ) { $include = implode( ',' , wp_parse_id_list( $r [ 'include' ] ) ); $where_conditions [ 'include' ] = "g.id IN ({$include})" ; } if ( ! is_null ( $r [ 'parent_id' ] ) ) { // For legacy reasons, `false` means groups with no parent. if ( false === $r [ 'parent_id' ] ) { $parent_id = 0; } else { $parent_id = implode( ',' , wp_parse_id_list( $r [ 'parent_id' ] ) ); } $where_conditions [ 'parent_id' ] = "g.parent_id IN ({$parent_id})" ; } if ( ! empty ( $r [ 'exclude' ] ) ) { $exclude = implode( ',' , wp_parse_id_list( $r [ 'exclude' ] ) ); $where_conditions [ 'exclude' ] = "g.id NOT IN ({$exclude})" ; } /* Order/orderby ********************************************/ $order = $r [ 'order' ]; $orderby = $r [ 'orderby' ]; // If a 'type' parameter was passed, parse it and overwrite // 'order' and 'orderby' params passed to the function. if ( ! empty ( $r [ 'type' ] ) ) { /** * Filters the 'type' parameter used to overwrite 'order' and 'orderby' values. * * @since BuddyPress 2.1.0 * * @param array $value Converted 'type' value for order and orderby. * @param string $value Parsed 'type' value for the get method. */ $order_orderby = apply_filters( 'bp_groups_get_orderby' , self::convert_type_to_order_orderby( $r [ 'type' ] ), $r [ 'type' ] ); // If an invalid type is passed, $order_orderby will be // an array with empty values. In this case, we stick // with the default values of $order and $orderby. if ( ! empty ( $order_orderby [ 'order' ] ) ) { $order = $order_orderby [ 'order' ]; } if ( ! empty ( $order_orderby [ 'orderby' ] ) ) { $orderby = $order_orderby [ 'orderby' ]; } } // 'total_member_count' and 'last_activity' sorts require additional table joins. if ( 'total_member_count' === $orderby ) { $sql [ 'from' ] .= " JOIN {$bp->groups->table_name_groupmeta} gm_total_member_count ON ( g.id = gm_total_member_count.group_id )" ; $where_conditions [ 'total_member_count' ] = "gm_total_member_count.meta_key = 'total_member_count'" ; } elseif ( 'last_activity' === $orderby ) { $sql [ 'from' ] .= " JOIN {$bp->groups->table_name_groupmeta} gm_last_activity on ( g.id = gm_last_activity.group_id )" ; $where_conditions [ 'last_activity' ] = "gm_last_activity.meta_key = 'last_activity'" ; } // If 'meta_id' is the requested order, and there's no meta query, fall back to the default. if ( 'meta_id' === $orderby && empty ( $meta_query_sql [ 'join' ] ) ) { $orderby = 'date_created' ; } // Sanitize 'order'. $order = bp_esc_sql_order( $order ); /** * Filters the converted 'orderby' term. * * @since BuddyPress 2.1.0 * * @param string $value Converted 'orderby' term. * @param string $orderby Original orderby value. * @param string $value Parsed 'type' value for the get method. */ $orderby = apply_filters( 'bp_groups_get_orderby_converted_by_term' , self::convert_orderby_to_order_by_term( $orderby ), $orderby , $r [ 'type' ] ); // Random order is a special case. if ( 'rand()' === $orderby ) { $sql [ 'orderby' ] = "ORDER BY rand()" ; } else { $sql [ 'orderby' ] = "ORDER BY {$orderby} {$order}" ; } if ( ! empty ( $r [ 'per_page' ] ) && ! empty ( $r [ 'page' ] ) && $r [ 'per_page' ] != -1 ) { $sql [ 'pagination' ] = $wpdb ->prepare( "LIMIT %d, %d" , intval ( ( $r [ 'page' ] - 1 ) * $r [ 'per_page' ]), intval ( $r [ 'per_page' ] ) ); } $where = '' ; if ( ! empty ( $where_conditions ) ) { $sql [ 'where' ] = implode( ' AND ' , $where_conditions ); $where = "WHERE {$sql['where']}" ; } $paged_groups_sql = "{$sql['select']} FROM {$sql['from']} {$where} {$sql['orderby']} {$sql['pagination']}" ; /** * Filters the pagination SQL statement. * * @since BuddyPress 1.5.0 * * @param string $value Concatenated SQL statement. * @param array $sql Array of SQL parts before concatenation. * @param array $r Array of parsed arguments for the get method. */ $paged_groups_sql = apply_filters( 'bp_groups_get_paged_groups_sql' , $paged_groups_sql , $sql , $r ); $cached = bp_core_get_incremented_cache( $paged_groups_sql , 'bp_groups' ); if ( false === $cached ) { $paged_group_ids = $wpdb ->get_col( $paged_groups_sql ); bp_core_set_incremented_cache( $paged_groups_sql , 'bp_groups' , $paged_group_ids ); } else { $paged_group_ids = $cached ; } if ( 'ids' === $r [ 'fields' ] ) { // We only want the IDs. $paged_groups = array_map ( 'intval' , $paged_group_ids ); } else { $uncached_group_ids = bp_get_non_cached_ids( $paged_group_ids , 'bp_groups' ); if ( $uncached_group_ids ) { $group_ids_sql = implode( ',' , array_map ( 'intval' , $uncached_group_ids ) ); $group_data_objects = $wpdb ->get_results( "SELECT g.* FROM {$bp->groups->table_name} g WHERE g.id IN ({$group_ids_sql})" ); foreach ( $group_data_objects as $group_data_object ) { wp_cache_set( $group_data_object ->id, $group_data_object , 'bp_groups' ); } } $paged_groups = array (); foreach ( $paged_group_ids as $paged_group_id ) { $paged_groups [] = new BP_Groups_Group( $paged_group_id ); } $group_ids = array (); foreach ( ( array ) $paged_groups as $group ) { $group_ids [] = $group ->id; } // Grab all groupmeta. if ( ! empty ( $r [ 'update_meta_cache' ] ) ) { bp_groups_update_meta_cache( $group_ids ); } // Prefetch all administrator IDs, if requested. if ( $r [ 'update_admin_cache' ] ) { BP_Groups_Member::prime_group_admins_mods_cache( $group_ids ); } // Set up integer properties needing casting. $int_props = array ( 'id' , 'creator_id' , 'enable_forum' ); // Integer casting. foreach ( $paged_groups as $key => $g ) { foreach ( $int_props as $int_prop ) { $paged_groups [ $key ]->{ $int_prop } = (int) $paged_groups [ $key ]->{ $int_prop }; } } } // Find the total number of groups in the results set. $total_groups_sql = "SELECT COUNT(DISTINCT g.id) FROM {$sql['from']} $where" ; /** * Filters the SQL used to retrieve total group results. * * @since BuddyPress 1.5.0 * * @param string $t_sql Concatenated SQL statement used for retrieving total group results. * @param array $total_sql Array of SQL parts for the query. * @param array $r Array of parsed arguments for the get method. */ $total_groups_sql = apply_filters( 'bp_groups_get_total_groups_sql' , $total_groups_sql , $sql , $r ); $cached = bp_core_get_incremented_cache( $total_groups_sql , 'bp_groups' ); if ( false === $cached ) { $total_groups = (int) $wpdb ->get_var( $total_groups_sql ); bp_core_set_incremented_cache( $total_groups_sql , 'bp_groups' , $total_groups ); } else { $total_groups = (int) $cached ; } return array ( 'groups' => $paged_groups , 'total' => $total_groups ); } /** * Get the SQL for the 'meta_query' param in BP_Activity_Activity::get() * * We use WP_Meta_Query to do the heavy lifting of parsing the * meta_query array and creating the necessary SQL clauses. * * @since BuddyPress 1.8.0 * * @param array $meta_query An array of meta_query filters. See the * documentation for {@link WP_Meta_Query} for details. * @return array $sql_array 'join' and 'where' clauses. */ protected static function get_meta_query_sql( $meta_query = array () ) { global $wpdb ; $sql_array = array ( 'join' => '' , 'where' => '' , ); if ( ! empty ( $meta_query ) ) { $groups_meta_query = new WP_Meta_Query( $meta_query ); // WP_Meta_Query expects the table name at // $wpdb->group. $wpdb ->groupmeta = buddypress()->groups->table_name_groupmeta; $meta_sql = $groups_meta_query ->get_sql( 'group' , 'g' , 'id' ); $sql_array [ 'join' ] = $meta_sql [ 'join' ]; $sql_array [ 'where' ] = self::strip_leading_and( $meta_sql [ 'where' ] ); } return $sql_array ; } /** * Convert the 'type' parameter to 'order' and 'orderby'. * * @since BuddyPress 1.8.0 * * @param string $type The 'type' shorthand param. * * @return array { * @type string $order SQL-friendly order string. * @type string $orderby SQL-friendly orderby column name. * } */ protected static function convert_type_to_order_orderby( $type = '' ) { $order = $orderby = '' ; switch ( $type ) { case 'newest' : $order = 'DESC' ; $orderby = 'date_created' ; break ; case 'active' : $order = 'DESC' ; $orderby = 'last_activity' ; break ; case 'popular' : $order = 'DESC' ; $orderby = 'total_member_count' ; break ; case 'alphabetical' : $order = 'ASC' ; $orderby = 'name' ; break ; case 'random' : $order = '' ; $orderby = 'random' ; break ; } return array ( 'order' => $order , 'orderby' => $orderby ); } /** * Convert the 'orderby' param into a proper SQL term/column. * * @since BuddyPress 1.8.0 * * @param string $orderby Orderby term as passed to get(). * @return string $order_by_term SQL-friendly orderby term. */ protected static function convert_orderby_to_order_by_term( $orderby ) { $order_by_term = '' ; switch ( $orderby ) { case 'date_created' : default : $order_by_term = 'g.date_created' ; break ; case 'last_activity' : $order_by_term = 'gm_last_activity.meta_value' ; break ; case 'total_member_count' : $order_by_term = 'CONVERT(gm_total_member_count.meta_value, SIGNED)' ; break ; case 'name' : $order_by_term = 'g.name' ; break ; case 'random' : $order_by_term = 'rand()' ; break ; case 'meta_id' : $order_by_term = buddypress()->groups->table_name_groupmeta . '.id' ; break ; } return $order_by_term ; } /** * Get a list of groups whose names start with a given letter. * * @since BuddyPress 1.6.0 * * @param string $letter The letter. * @param int|null $limit Optional. The max number of results to return. * Default: null (no limit). * @param int|null $page Optional. The page offset of results to return. * Default: null (no limit). * @param bool $populate_extras Deprecated. * @param string|array|bool $exclude Optional. Array or comma-separated list of group * IDs to exclude from results. * @return false|array { * @type array $groups Array of group objects returned by the * paginated query. * @type int $total Total count of all groups matching non- * paginated query params. * } */ public static function get_by_letter( $letter , $limit = null, $page = null, $populate_extras = true, $exclude = false ) { global $wpdb ; $pag_sql = $hidden_sql = $exclude_sql = '' ; // Multibyte compliance. if ( function_exists( 'mb_strlen' ) ) { if ( mb_strlen( $letter , 'UTF-8' ) > 1 || is_numeric ( $letter ) || ! $letter ) { return false; } } else { if ( strlen ( $letter ) > 1 || is_numeric ( $letter ) || ! $letter ) { return false; } } $args = array ( 'per_page' => $limit , 'page' => $page , 'search_terms' => $letter . '*' , 'search_columns' => array ( 'name' ), 'exclude' => $exclude , ); return BP_Groups_Group::get( $args ); } /** * Get a list of random groups. * * Use BP_Groups_Group::get() with 'type' = 'random' instead. * * @since BuddyPress 1.6.0 * * @param int|null $limit Optional. The max number of results to return. * Default: null (no limit). * @param int|null $page Optional. The page offset of results to return. * Default: null (no limit). * @param int $user_id Optional. If present, groups will be limited to * those of which the specified user is a member. * @param string|bool $search_terms Optional. Limit groups to those whose name * or description field contain the search string. * @param bool $populate_extras Optional. Whether to fetch extra * information about the groups. Default: true. * @param string|array|bool $exclude Optional. Array or comma-separated list of group * IDs to exclude from results. * @return array { * @type array $groups Array of group objects returned by the * paginated query. * @type int $total Total count of all groups matching non- * paginated query params. * } */ public static function get_random( $limit = null, $page = null, $user_id = 0, $search_terms = false, $populate_extras = true, $exclude = false ) { $args = array ( 'type' => 'random' , 'per_page' => $limit , 'page' => $page , 'user_id' => $user_id , 'search_terms' => $search_terms , 'exclude' => $exclude , ); return BP_Groups_Group::get( $args ); } /** * Fetch extra data for a list of groups. * * This method is used throughout the class, by methods that take a * $populate_extras parameter. * * Data fetched: * - Logged-in user's status within each group (is_member, * is_confirmed, is_pending, is_banned) * * @since BuddyPress 1.6.0 * * @param array $paged_groups Array of groups. * @param string|array $group_ids Array or comma-separated list of IDs matching * $paged_groups. * @param string|bool $type Not used. * @return array $paged_groups */ public static function get_group_extras( & $paged_groups , & $group_ids , $type = false ) { $user_id = bp_loggedin_user_id(); foreach ( $paged_groups as & $group ) { $group ->is_member = groups_is_user_member( $user_id , $group ->id ) ? 1 : 0; $group ->is_invited = groups_is_user_invited( $user_id , $group ->id ) ? 1 : 0; $group ->is_pending = groups_is_user_pending( $user_id , $group ->id ) ? 1 : 0; $group ->is_banned = (bool) groups_is_user_banned( $user_id , $group ->id ); } return $paged_groups ; } /** * Delete all invitations to a given group. * * @since BuddyPress 1.6.0 * * @param int $group_id ID of the group whose invitations are being deleted. * @return int|null Number of rows records deleted on success, null on * failure. */ public static function delete_all_invites( $group_id ) { global $wpdb ; $bp = buddypress(); return $wpdb ->query( $wpdb ->prepare( "DELETE FROM {$bp->groups->table_name_members} WHERE group_id = %d AND invite_sent = 1" , $group_id ) ); } /** * Get a total group count for the site. * * Will include hidden groups in the count only if * bp_current_user_can( 'bp_moderate' ). * * @since BuddyPress 1.6.0 * * @return int Group count. */ public static function get_total_group_count() { global $wpdb ; $hidden_sql = '' ; if ( !bp_current_user_can( 'bp_moderate' ) ) $hidden_sql = "WHERE status != 'hidden'" ; $bp = buddypress(); return $wpdb ->get_var( "SELECT COUNT(id) FROM {$bp->groups->table_name} {$hidden_sql}" ); } /** * Get the member count for a group. * * @since BuddyPress 1.6.0 * * @param int $group_id Group ID. * @return int Count of confirmed members for the group. */ public static function get_total_member_count( $group_id ) { global $wpdb ; $bp = buddypress(); return $wpdb ->get_var( $wpdb ->prepare( "SELECT COUNT(id) FROM {$bp->groups->table_name_members} WHERE group_id = %d AND is_confirmed = 1 AND is_banned = 0" , $group_id ) ); } /** * Get an array containing ids for each group type. * * A bit of a kludge workaround for some issues * with bp_has_groups(). * * @since BuddyPress 1.7.0 * * @return array */ public static function get_group_type_ids() { global $wpdb ; $bp = buddypress(); $ids = array (); $ids [ 'all' ] = $wpdb ->get_col( "SELECT id FROM {$bp->groups->table_name}" ); $ids [ 'public' ] = $wpdb ->get_col( "SELECT id FROM {$bp->groups->table_name} WHERE status = 'public'" ); $ids [ 'private' ] = $wpdb ->get_col( "SELECT id FROM {$bp->groups->table_name} WHERE status = 'private'" ); $ids [ 'hidden' ] = $wpdb ->get_col( "SELECT id FROM {$bp->groups->table_name} WHERE status = 'hidden'" ); return $ids ; } /** * Get SQL clause for group type(s). * * @since BuddyPress 2.6.0 * * @param string|array $group_types Group type(s). * @param string $operator 'IN' or 'NOT IN'. * @return string $clause SQL clause. */ protected static function get_sql_clause_for_group_types( $group_types , $operator ) { global $wpdb ; // Sanitize operator. if ( 'NOT IN' !== $operator ) { $operator = 'IN' ; } // Parse and sanitize types. if ( ! is_array ( $group_types ) ) { $group_types = preg_split( '/[,\s+]/' , $group_types ); } $types = array (); foreach ( $group_types as $gt ) { if ( bp_groups_get_group_type_object( $gt ) ) { $types [] = $gt ; } } $tax_query = new WP_Tax_Query( array ( array ( 'taxonomy' => 'bp_group_type' , 'field' => 'name' , 'operator' => $operator , 'terms' => $types , ), ) ); $site_id = bp_get_taxonomy_term_site_id( 'bp_group_type' ); $switched = false; if ( $site_id !== get_current_blog_id() ) { switch_to_blog( $site_id ); $switched = true; } $sql_clauses = $tax_query ->get_sql( 'g' , 'id' ); $clause = '' ; // The no_results clauses are the same between IN and NOT IN. if ( false !== strpos ( $sql_clauses [ 'where' ], '0 = 1' ) ) { $clause = self::strip_leading_and( $sql_clauses [ 'where' ] ); // The tax_query clause generated for NOT IN can be used almost as-is. } elseif ( 'NOT IN' === $operator ) { $clause = self::strip_leading_and( $sql_clauses [ 'where' ] ); // IN clauses must be converted to a subquery. } elseif ( preg_match( '/' . $wpdb ->term_relationships . '\.term_taxonomy_id IN \([0-9, ]+\)/' , $sql_clauses [ 'where' ], $matches ) ) { $clause = " g.id IN ( SELECT object_id FROM $wpdb->term_relationships WHERE {$matches[0]} )" ; } if ( $switched ) { restore_current_blog(); } return $clause ; } /** * Strips the leading AND and any surrounding whitespace from a string. * * Used here to normalize SQL fragments generated by `WP_Meta_Query` and * other utility classes. * * @since BuddyPress 2.7.0 * * @param string $s String. * @return string */ protected static function strip_leading_and( $s ) { return preg_replace( '/^\s*AND\s*/' , '' , $s ); } } |
Changelog
Version | Description |
---|---|
BuddyPress 1.6.0 | Introduced. |
Methods
- __construct — Constructor method.
- __get — Magic getter.
- __isset — Magic issetter.
- __set — Magic setter.
- check_slug — Check for the existence of a slug.
- convert_orderby_to_order_by_term — Convert the 'orderby' param into a proper SQL term/column.
- convert_type_to_order_orderby — Convert the 'type' parameter to 'order' and 'orderby'.
- delete — Delete the current group.
- delete_all_invites — Delete all invitations to a given group.
- filter_user_groups — Get a list of a user's groups, filtered by a search string.
- get — Query for groups.
- get_admins — Get a list of the group's admins.
- get_by_letter — Get a list of groups whose names start with a given letter.
- get_group_extras — Fetch extra data for a list of groups.
- get_group_type_ids — Get an array containing ids for each group type.
- get_id_by_previous_slug — Get whether a group exists for an old slug.
- get_id_from_slug — Get the ID of a group by the group's slug.
- get_invites — Get IDs of users with outstanding invites to a given group from a specified user.
- get_is_member — Checks whether the logged-in user is a member of the group.
- get_membership_requests — Get outstanding membership requests for a group.
- get_meta_query_sql — Get the SQL for the 'meta_query' param in BP_Activity_Activity::get()
- get_mods — Get a list of the group's mods.
- get_random — Get a list of random groups.
- get_slug — Get the slug for a given group ID.
- get_sql_clause_for_group_types — Get SQL clause for group type(s).
- get_total_group_count — Get a total group count for the site.
- get_total_member_count — Get the member count for a group.
- get_user_has_access — Checks whether the logged-in user has access to the group.
- group_exists — Get whether a group exists for a given slug.
- has_members — Check whether a given group has any members.
- has_membership_requests — Check whether a group has outstanding membership requests.
- is_visible — Checks whether the current user can know the group exists.
- populate — Set up data about the current group.
- save — Save the current group to the database.
- search_groups — Get a list of groups, filtered by a search string.
- set_up_admins_and_mods — Set up admins and mods for the current group object.
- strip_leading_and — Strips the leading AND and any surrounding whitespace from a string.
Questions?
We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.