BP_Group_Extension
API for creating group extensions without having to hardcode the content into the theme.
Description
To implement, extend this class. In your constructor, pass an optional array of arguments to parent::init() to configure your widget. The config array supports the following values:
- ‘slug’ A unique identifier for your extension. This value will be used to build URLs, so make it URL-safe.
- ‘name’ A translatable name for your extension. This value is used to populate the navigation tab, as well as the default titles for admin/ edit/create tabs.
- ‘visibility’ Set to ‘public’ (default) for your extension (the main tab as well as the widget) to be available to anyone who can access the group, ‘private’ otherwise.
- ‘nav_item_position’ An integer explaining where the nav item should appear in the tab list.
- ‘enable_nav_item’ Set to true for your extension’s main tab to be available to anyone who can access the group.
- ‘nav_item_name’ The translatable text you want to appear in the nav tab. Defaults to the value of ‘name’.
- ‘display_hook’ The WordPress action that the widget_display() method is hooked to.
- ‘template_file’ The template file that will be used to load the content of your main extension tab. Defaults to ‘groups/single/plugins.php’.
- ‘screens’ A multi-dimensional array, described below.
- ‘access’ Which users can visit the plugin’s tab.
- ‘show_tab’ Which users can see the plugin’s navigation tab.
BP_Group_Extension uses the concept of "settings screens". There are three contexts for settings screens:
- ‘create’, which inserts a new step into the group creation process
- ‘edit’, which adds a tab for your extension into the Admin section of a group
- ‘admin’, which adds a metabox to the Groups administration panel in the WordPress Dashboard Each of these settings screens is populated by a pair of methods: one that creates the markup for the screen, and one that processes form data submitted from the screen. If your plugin needs screens in all three contexts, and if the markup and form processing logic will be the same in each case, you can define two methods to handle all of the screens: function settings_screen() {} function settings_screen_save() {} If one or more of your settings screen needs separate logic, you may define context-specific methods, for example: function edit_screen() {} function edit_screen_save() {} BP_Group_Extension will use the more specific methods if they are available.
You can further customize the settings screens (tab names, etc) by passing an optional ‘screens’ parameter to the init array. The format is as follows: ‘screens’ => array( ‘create’ => array( ‘slug’ => ‘foo’, ‘name’ => ‘Foo’, ‘position’ => 55, ‘screen_callback’ => ‘my_create_screen_callback’, ‘screen_save_callback’ => ‘my_create_screen_save_callback’, ), ‘edit’ => array( // … ), Only provide those arguments that you actually want to change from the default configuration. BP_Group_Extension will do the rest.
Note that the ‘edit’ screen accepts an additional parameter: ‘submit_text’, which defines the text of the Submit button automatically added to the Edit screen of the extension (defaults to ‘Save Changes’). Also, the ‘admin’ screen accepts two additional parameters: ‘metabox_priority’ and ‘metabox_context’. See the docs for add_meta_box() for more details on these arguments.
Prior to BuddyPress 1.7, group extension configurations were set slightly differently. The legacy method is still supported, though deprecated.
Source
File: bp-groups/classes/class-bp-group-extension.php
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 | class BP_Group_Extension { /** Public ************************************************************/ /** * Information about this extension's screens. * * @since BuddyPress 1.8.0 * @var array */ public $screens = array (); /** * The name of the extending class. * * @since BuddyPress 1.8.0 * @var string */ public $class_name = '' ; /** * A ReflectionClass object of the current extension. * * @since BuddyPress 1.8.0 * @var ReflectionClass */ public $class_reflection = null; /** * Parsed configuration parameters for the extension. * * @since BuddyPress 1.8.0 * @var array */ public $params = array (); /** * Raw config params, as passed by the extending class. * * @since BuddyPress 2.1.0 * @var array */ public $params_raw = array (); /** * The ID of the current group. * * @since BuddyPress 1.8.0 * @var int */ public $group_id = 0; /** * The slug of the current extension. * * @since BuddyPress 1.1.0 * @var string */ public $slug = '' ; /** * The translatable name of the current extension. * * @since BuddyPress 1.1.0 * @var string */ public $name = '' ; /** * The visibility of the extension tab. 'public' or 'private'. * * @since BuddyPress 1.1.0 * @var string */ public $visibility = 'public' ; /** * The numeric position of the main nav item. * * @since BuddyPress 1.1.0 * @var int */ public $nav_item_position = 81; /** * Whether to show the nav item. * * @since BuddyPress 1.1.0 * @var bool */ public $enable_nav_item = true; /** * Whether the current user should see the navigation item. * * @since BuddyPress 2.1.0 * @var bool */ public $user_can_see_nav_item ; /** * Whether the current user can visit the tab. * * @since BuddyPress 2.1.0 * @var bool */ public $user_can_visit ; /** * The text of the nav item. Defaults to self::name. * * @since BuddyPress 1.1.0 * @var string */ public $nav_item_name = '' ; /** * The WP action that self::widget_display() is attached to. * * Default: 'groups_custom_group_boxes'. * * @since BuddyPress 1.1.0 * @var string */ public $display_hook = 'groups_custom_group_boxes' ; /** * The template file used to load the plugin content. * * Default: 'groups/single/plugins'. * * @since BuddyPress 1.1.0 * @var string */ public $template_file = 'groups/single/plugins' ; /** Protected *********************************************************/ /** * Has the extension been initialized? * * @since BuddyPress 1.8.0 * @var bool */ protected $initialized = false; /** * Extension properties as set by legacy extensions. * * @since BuddyPress 1.8.0 * @var array */ protected $legacy_properties = array (); /** * Converted legacy parameters. * * These are the extension properties as set by legacy extensions, but * then converted to match the new format for params. * * @since BuddyPress 1.8.0 * @var array */ protected $legacy_properties_converted = array (); /** * Redirect location as defined by post-edit save callback. * * @since BuddyPress 2.1.0 * @var string */ protected $post_save_redirect ; /** * Miscellaneous data as set by the __set() magic method. * * @since BuddyPress 1.8.0 * @var array */ protected $data = array (); /** Screen Overrides **************************************************/ /* * Screen override methods are how your extension will display content * and handle form submits. Your extension should only override those * methods that it needs for its purposes. */ /** * The content of the group tab. * * @since BuddyPress 1.1.0 * * @param int|null $group_id ID of the group to display. */ public function display( $group_id = null ) {} /** * Content displayed in a widget sidebar, if applicable. * * @since BuddyPress 1.1.0 */ public function widget_display() {} /* * *_screen() displays the settings form for the given context * *_screen_save() processes data submitted via the settings form * The settings_* methods are generic fallbacks, which can optionally * be overridden by the more specific edit_*, create_*, and admin_* * versions. */ public function settings_screen( $group_id = null ) {} public function settings_screen_save( $group_id = null ) {} public function edit_screen( $group_id = null ) {} public function edit_screen_save( $group_id = null ) {} public function create_screen( $group_id = null ) {} public function create_screen_save( $group_id = null ) {} public function admin_screen( $group_id = null ) {} public function admin_screen_save( $group_id = null ) {} /** Setup *************************************************************/ /** * Initialize the extension, using your config settings. * * Your plugin should call this method at the very end of its * constructor, like so: * * public function __construct() { * $args = array( * 'slug' => 'my-group-extension', * 'name' => 'My Group Extension', * // ... * ); * * parent::init( $args ); * } * * @since BuddyPress 1.8.0 * @since BuddyPress 2.1.0 Added 'access' and 'show_tab' arguments to `$args`. * * @param array $args { * Array of initialization arguments. * @type string $slug Unique, URL-safe identifier for your extension. * @type string $name Translatable name for your extension. Used to populate * navigation items. * @type string $visibility Optional. Set to 'public' for your extension (the main tab as well * as the widget) to be available to anyone who can access the group; * set to 'private' otherwise. Default: 'public'. * @type int $nav_item_position Optional. Location of the nav item in the tab list. * Default: 81. * @type bool $enable_nav_item Optional. Whether the extension's tab should be accessible to * anyone who can view the group. Default: true. * @type string $nav_item_name Optional. The translatable text you want to appear in the nav tab. * Default: the value of `$name`. * @type string $display_hook Optional. The WordPress action that the widget_display() method is * hooked to. Default: 'groups_custom_group_boxes'. * @type string $template_file Optional. Theme-relative path to the template file BP should use * to load the content of your main extension tab. * Default: 'groups/single/plugins.php'. * @type array $screens A multi-dimensional array of configuration information for the * extension screens. See docblock of {@link BP_Group_Extension} * for more details. * @type string|array $access Which users can visit the plugin's tab. Possible values: 'anyone', * 'loggedin', 'member', 'mod', 'admin' or 'noone'. ('member', 'mod', * 'admin' refer to user's role in group.) Note that 'mod' targets * only group moderators. If you want to allow access to group moderators * and admins, specify `array( 'mod', 'admin' )`. Defaults to 'anyone' * for public groups and 'member' for private groups. * @type string|array $show_tab Which users can see the plugin's navigation tab. Possible values: * 'anyone', 'loggedin', 'member', 'mod', 'admin' or 'noone'. * ('member', 'mod', 'admin' refer to user's role in group.) Note * that 'mod' targets only group moderators. If you want to show the * tab to group moderators and admins, specify * `array( 'mod', 'admin' )`. Defaults to 'anyone' for public groups * and 'member' for private groups. * } */ public function init( $args = array () ) { // Store the raw arguments. $this ->params_raw = $args ; // Before this init() method was introduced, plugins were // encouraged to set their config directly. For backward // compatibility with these plugins, we detect whether this is // one of those legacy plugins, and parse any legacy arguments // with those passed to init(). $this ->parse_legacy_properties(); $args = $this ->parse_args_r( $args , $this ->legacy_properties_converted ); // Parse with defaults. $this ->params = $this ->parse_args_r( $args , array ( 'slug' => $this ->slug, 'name' => $this ->name, 'visibility' => $this ->visibility, 'nav_item_position' => $this ->nav_item_position, 'enable_nav_item' => (bool) $this ->enable_nav_item, 'nav_item_name' => $this ->nav_item_name, 'display_hook' => $this ->display_hook, 'template_file' => $this ->template_file, 'screens' => $this ->get_default_screens(), 'access' => null, 'show_tab' => null, ) ); $this ->initialized = true; } /** * The main setup routine for the extension. * * This method contains the primary logic for setting up an extension's * configuration, setting up backward compatibility for legacy plugins, * and hooking the extension's screen functions into WP and BP. * * Marked 'public' because it must be accessible to add_action(). * However, you should never need to invoke this method yourself - it * is called automatically at the right point in the load order by * bp_register_group_extension(). * * @since BuddyPress 1.1.0 */ public function _register() { // Detect and parse properties set by legacy extensions. $this ->parse_legacy_properties(); // Initialize, if necessary. This should only happen for // legacy extensions that don't call parent::init() themselves. if ( true !== $this ->initialized ) { $this ->init(); } // Set some config values, based on the parsed params. $this ->group_id = $this ->get_group_id(); $this ->slug = $this ->params[ 'slug' ]; $this ->name = $this ->params[ 'name' ]; $this ->visibility = $this ->params[ 'visibility' ]; $this ->nav_item_position = $this ->params[ 'nav_item_position' ]; $this ->nav_item_name = $this ->params[ 'nav_item_name' ]; $this ->display_hook = $this ->params[ 'display_hook' ]; $this ->template_file = $this ->params[ 'template_file' ]; // Configure 'screens': create, admin, and edit contexts. $this ->setup_screens(); // Configure access-related settings. $this ->setup_access_settings(); // Mirror configuration data so it's accessible to plugins // that look for it in its old locations. $this ->setup_legacy_properties(); // Hook the extension into BuddyPress. $this ->setup_display_hooks(); $this ->setup_create_hooks(); $this ->setup_edit_hooks(); $this ->setup_admin_hooks(); } /** * Set up some basic info about the Extension. * * Here we collect the name of the extending class, as well as a * ReflectionClass that is used in get_screen_callback() to determine * whether your extension overrides certain callback methods. * * @since BuddyPress 1.8.0 */ protected function setup_class_info() { if ( empty ( $this ->class_name ) ) { $this ->class_name = get_class( $this ); } if ( is_null ( $this ->class_reflection ) ) { $this ->class_reflection = new ReflectionClass( $this ->class_name ); } } /** * Get the current group ID. * * Check for: * - current group * - new group * - group admin * * @since BuddyPress 1.8.0 * * @return int */ public static function get_group_id() { // Usually this will work. $group_id = bp_get_current_group_id(); // On the admin, get the group id out of the $_GET params. if ( empty ( $group_id ) && is_admin() && ( isset( $_GET [ 'page' ] ) && ( 'bp-groups' === $_GET [ 'page' ] ) ) && ! empty ( $_GET [ 'gid' ] ) ) { $group_id = (int) $_GET [ 'gid' ]; } // This fallback will only be hit when the create step is very // early. if ( empty ( $group_id ) && bp_get_new_group_id() ) { $group_id = bp_get_new_group_id(); } // On some setups, the group id has to be fetched out of the // $_POST array // @todo Figure out why this is happening during group creation. if ( empty ( $group_id ) && isset( $_POST [ 'group_id' ] ) ) { $group_id = (int) $_POST [ 'group_id' ]; } return $group_id ; } /** * Gather configuration data about your screens. * * @since BuddyPress 1.8.0 * * @return array */ protected function get_default_screens() { $this ->setup_class_info(); $screens = array ( 'create' => array ( 'position' => 81, ), 'edit' => array ( 'submit_text' => __( 'Save Changes' , 'buddyboss' ), ), 'admin' => array ( 'metabox_context' => 'normal' , 'metabox_priority' => 'core' , ), ); foreach ( $screens as $context => & $screen ) { $screen [ 'enabled' ] = true; $screen [ 'name' ] = $this ->name; $screen [ 'slug' ] = $this ->slug; $screen [ 'screen_callback' ] = $this ->get_screen_callback( $context , 'screen' ); $screen [ 'screen_save_callback' ] = $this ->get_screen_callback( $context , 'screen_save' ); } return $screens ; } /** * Set up screens array based on params. * * @since BuddyPress 1.8.0 */ protected function setup_screens() { foreach ( ( array ) $this ->params[ 'screens' ] as $context => $screen ) { if ( empty ( $screen [ 'slug' ] ) ) { $screen [ 'slug' ] = $this ->slug; } if ( empty ( $screen [ 'name' ] ) ) { $screen [ 'name' ] = $this ->name; } $this ->screens[ $context ] = $screen ; } } /** * Set up access-related settings for this extension. * * @since BuddyPress 2.1.0 */ protected function setup_access_settings() { // Bail if no group ID is available. if ( empty ( $this ->group_id ) ) { return ; } // Backward compatibility. if ( isset( $this ->params[ 'enable_nav_item' ] ) ) { $this ->enable_nav_item = (bool) $this ->params[ 'enable_nav_item' ]; } // Tab Access. $this ->user_can_visit = false; // Backward compatibility for components that do not provide // explicit 'access' parameter. if ( empty ( $this ->params[ 'access' ] ) ) { if ( false === $this ->params[ 'enable_nav_item' ] ) { $this ->params[ 'access' ] = 'noone' ; } else { $group = groups_get_group( $this ->group_id ); if ( ! empty ( $group ->status ) && 'public' === $group ->status ) { // Tabs in public groups are accessible to anyone by default. $this ->params[ 'access' ] = 'anyone' ; } else { // All other groups have members-only as the default. $this ->params[ 'access' ] = 'member' ; } } } // Parse multiple access conditions into an array. $access_conditions = $this ->params[ 'access' ]; if ( ! is_array ( $access_conditions ) ) { $access_conditions = explode ( ',' , $access_conditions ); } // If the current user meets at least one condition, the // get access. foreach ( $access_conditions as $access_condition ) { if ( $this ->user_meets_access_condition( $access_condition ) ) { $this ->user_can_visit = true; break ; } } // Tab Visibility. $this ->user_can_see_nav_item = false; // Backward compatibility for components that do not provide // explicit 'show_tab' parameter. if ( empty ( $this ->params[ 'show_tab' ] ) ) { if ( false === $this ->params[ 'enable_nav_item' ] ) { // The enable_nav_item index is only false if it's been // defined explicitly as such in the // constructor. So we always trust this value. $this ->params[ 'show_tab' ] = 'noone' ; } elseif ( isset( $this ->params_raw[ 'enable_nav_item' ] ) || isset( $this ->params_raw[ 'visibility' ] ) ) { // If enable_nav_item or visibility is passed, // we assume this is a legacy extension. // Legacy behavior is that enable_nav_item=true + // visibility=private implies members-only. if ( 'public' !== $this ->visibility ) { $this ->params[ 'show_tab' ] = 'member' ; } else { $this ->params[ 'show_tab' ] = 'anyone' ; } } else { // No show_tab or enable_nav_item value is // available, so match the value of 'access'. $this ->params[ 'show_tab' ] = $this ->params[ 'access' ]; } } // Parse multiple access conditions into an array. $access_conditions = $this ->params[ 'show_tab' ]; if ( ! is_array ( $access_conditions ) ) { $access_conditions = explode ( ',' , $access_conditions ); } // If the current user meets at least one condition, the // get access. foreach ( $access_conditions as $access_condition ) { if ( $this ->user_meets_access_condition( $access_condition ) ) { $this ->user_can_see_nav_item = true; break ; } } } /** * Check whether the current user meets an access condition. * * @since BuddyPress 2.1.0 * * @param string $access_condition 'anyone', 'loggedin', 'member', * 'mod', 'admin' or 'noone'. * @return bool */ protected function user_meets_access_condition( $access_condition ) { switch ( $access_condition ) { case 'admin' : $meets_condition = groups_is_user_admin( bp_loggedin_user_id(), $this ->group_id ); break ; case 'mod' : $meets_condition = groups_is_user_mod( bp_loggedin_user_id(), $this ->group_id ); break ; case 'member' : $meets_condition = groups_is_user_member( bp_loggedin_user_id(), $this ->group_id ); break ; case 'loggedin' : $meets_condition = is_user_logged_in(); break ; case 'noone' : $meets_condition = false; break ; case 'anyone' : default : $meets_condition = true; break ; } return $meets_condition ; } /** Display ***********************************************************/ /** * Hook this extension's group tab into BuddyPress, if necessary. * * @since BuddyPress 1.8.0 */ protected function setup_display_hooks() { // Bail if not a group. if ( ! bp_is_group() ) { return ; } // Backward compatibility only. if ( ( 'public' !== $this ->visibility ) && ! buddypress()->groups->current_group->user_has_access ) { return ; } // If the user can see the nav item, we create it. $user_can_see_nav_item = $this ->user_can_see_nav_item(); if ( $user_can_see_nav_item ) { $group_permalink = bp_get_group_permalink( groups_get_current_group() ); bp_core_create_subnav_link( array ( 'name' => ! $this ->nav_item_name ? $this ->name : $this ->nav_item_name, 'slug' => $this ->slug, 'parent_slug' => bp_get_current_group_slug(), 'parent_url' => $group_permalink , 'position' => $this ->nav_item_position, 'item_css_id' => 'nav-' . $this ->slug, 'screen_function' => array ( & $this , '_display_hook' ), 'user_has_access' => $user_can_see_nav_item , 'no_access_url' => $group_permalink , ), 'groups' ); } // If the user can visit the screen, we register it. $user_can_visit = $this ->user_can_visit(); if ( $user_can_visit ) { $group_permalink = bp_get_group_permalink( groups_get_current_group() ); bp_core_register_subnav_screen_function( array ( 'slug' => $this ->slug, 'parent_slug' => bp_get_current_group_slug(), 'screen_function' => array ( & $this , '_display_hook' ), 'user_has_access' => $user_can_visit , 'no_access_url' => $group_permalink , ), 'groups' ); // When we are viewing the extension display page, set the title and options title. if ( bp_is_current_action( $this ->slug ) ) { add_filter( 'bp_group_user_has_access' , array ( $this , 'group_access_protection' ), 10, 2 ); $extension_name = $this ->name; add_action( 'bp_template_content_header' , function () use ( $extension_name ) { echo esc_attr( $extension_name ); } ); add_action( 'bp_template_title' , function () use ( $extension_name ) { echo esc_attr( $extension_name ); } ); } } // Hook the group home widget. if ( bp_is_group_home() ) { add_action( $this ->display_hook, array ( & $this , 'widget_display' ) ); } } /** * Hook the main display method, and loads the template file. * * @since BuddyPress 1.1.0 */ public function _display_hook() { add_action( 'bp_template_content' , array ( & $this , 'call_display' ) ); /** * Filters the template to load for the main display method. * * @since BuddyPress 1.0.0 * * @param string $template_file Path to the template to load. */ bp_core_load_template( apply_filters( 'bp_core_template_plugin' , $this ->template_file ) ); } /** * Call the display() method. * * We use this wrapper so that we can pass the group_id to the * display() callback. * * @since BuddyPress 2.1.1 */ public function call_display() { $this ->display( $this ->group_id ); } /** * Determine whether the current user should see this nav tab. * * Note that this controls only the display of the navigation item. * Access to the tab is controlled by the user_can_visit() check. * * @since BuddyPress 2.1.0 * * @param bool $user_can_see_nav_item Whether or not the user can see the nav item. * @return bool */ public function user_can_see_nav_item( $user_can_see_nav_item = false ) { // Always allow moderators to see nav items, even if explicitly 'noone' if ( ( 'noone' !== $this ->params[ 'show_tab' ] ) && bp_current_user_can( 'bp_moderate' ) ) { return true; } return $this ->user_can_see_nav_item; } /** * Determine whether the current user has access to visit this tab. * * Note that this controls the ability of a user to access a tab. * Display of the navigation item is controlled by user_can_see_nav_item(). * * @since BuddyPress 2.1.0 * * @param bool $user_can_visit Whether or not the user can visit the tab. * @return bool */ public function user_can_visit( $user_can_visit = false ) { // Always allow moderators to visit a tab, even if explicitly 'noone' if ( ( 'noone' !== $this ->params[ 'access' ] ) && bp_current_user_can( 'bp_moderate' ) ) { return true; } return $this ->user_can_visit; } /** * Filter the access check in bp_groups_group_access_protection() for this extension. * * Note that $no_access_args is passed by reference, as there are some * circumstances where the bp_core_no_access() arguments need to be * modified before the redirect takes place. * * @since BuddyPress 2.1.0 * * @param bool $user_can_visit Whether or not the user can visit the tab. * @param array $no_access_args Array of args to help determine access. * @return bool */ public function group_access_protection( $user_can_visit , & $no_access_args ) { $user_can_visit = $this ->user_can_visit(); if ( ! $user_can_visit && is_user_logged_in() ) { $current_group = groups_get_group( $this ->group_id ); $no_access_args [ 'message' ] = __( 'You do not have access to this content.' , 'buddyboss' ); $no_access_args [ 'root' ] = bp_get_group_permalink( $current_group ) . 'home/' ; $no_access_args [ 'redirect' ] = false; } return $user_can_visit ; } /** Create ************************************************************/ /** * Hook this extension's Create step into BuddyPress, if necessary. * * @since BuddyPress 1.8.0 */ protected function setup_create_hooks() { if ( ! $this ->is_screen_enabled( 'create' ) ) { return ; } $screen = $this ->screens[ 'create' ]; // Insert the group creation step for the new group extension. buddypress()->groups->group_creation_steps[ $screen [ 'slug' ] ] = array ( 'name' => $screen [ 'name' ], 'slug' => $screen [ 'slug' ], 'position' => $screen [ 'position' ], ); // The maybe_ methods check to see whether the create_* // callbacks should be invoked (ie, are we on the // correct group creation step). Hooked in separate // methods because current creation step info not yet // available at this point. add_action( 'groups_custom_create_steps' , array ( $this , 'maybe_create_screen' ) ); add_action( 'groups_create_group_step_save_' . $screen [ 'slug' ], array ( $this , 'maybe_create_screen_save' ) ); } /** * Call the create_screen() method, if we're on the right page. * * @since BuddyPress 1.8.0 */ public function maybe_create_screen() { if ( ! bp_is_group_creation_step( $this ->screens[ 'create' ][ 'slug' ] ) ) { return ; } call_user_func( $this ->screens[ 'create' ][ 'screen_callback' ], $this ->group_id ); $this ->nonce_field( 'create' ); // The create screen requires an additional nonce field // due to a quirk in the way the templates are built. wp_nonce_field( 'groups_create_save_' . bp_get_groups_current_create_step(), '_wpnonce' , false ); } /** * Call the create_screen_save() method, if we're on the right page. * * @since BuddyPress 1.8.0 */ public function maybe_create_screen_save() { if ( ! bp_is_group_creation_step( $this ->screens[ 'create' ][ 'slug' ] ) ) { return ; } $this ->check_nonce( 'create' ); call_user_func( $this ->screens[ 'create' ][ 'screen_save_callback' ], $this ->group_id ); } /** Edit **************************************************************/ /** * Hook this extension's Edit panel into BuddyPress, if necessary. * * @since BuddyPress 1.8.0 */ protected function setup_edit_hooks() { // Bail if not in a group. if ( ! bp_is_group() ) { return ; } // Bail if not an edit screen. if ( ! $this ->is_screen_enabled( 'edit' ) || ! bp_is_item_admin() ) { return ; } $screen = $this ->screens[ 'edit' ]; $position = isset( $screen [ 'position' ] ) ? (int) $screen [ 'position' ] : 10; $position += 40; $current_group = groups_get_current_group(); $admin_link = trailingslashit( bp_get_group_permalink( $current_group ) . 'admin' ); $subnav_args = array ( 'name' => $screen [ 'name' ], 'slug' => $screen [ 'slug' ], 'parent_slug' => $current_group ->slug . '_manage' , 'parent_url' => $admin_link , 'user_has_access' => bp_is_item_admin(), 'position' => $position , 'screen_function' => 'groups_screen_group_admin' , ); // Should we add a menu to the Group's WP Admin Bar. if ( ! empty ( $screen [ 'show_in_admin_bar' ] ) ) { $subnav_args [ 'show_in_admin_bar' ] = true; } // Add the tab to the manage navigation. bp_core_new_subnav_item( $subnav_args , 'groups' ); // Catch the edit screen and forward it to the plugin template. if ( bp_is_groups_component() && bp_is_current_action( 'admin' ) && bp_is_action_variable( $screen [ 'slug' ], 0 ) ) { $this ->call_edit_screen_save( $this ->group_id ); add_action( 'groups_custom_edit_steps' , array ( & $this , 'call_edit_screen' ) ); // Determine the proper template and save for later // loading. if ( '' !== bp_locate_template( array ( 'groups/single/home.php' ), false ) ) { $this ->edit_screen_template = '/groups/single/home' ; } else { add_action( 'bp_template_content_header' , function () { echo '<ul class="content-header-nav">' ; bp_group_admin_tabs(); echo '</ul>' ; } ); add_action( 'bp_template_content' , array ( & $this , 'call_edit_screen' ) ); $this ->edit_screen_template = '/groups/single/plugins' ; } // We load the template at bp_screens, to give all // extensions a chance to load. add_action( 'bp_screens' , array ( $this , 'call_edit_screen_template_loader' ) ); } } /** * Call the edit_screen() method. * * Previous versions of BP_Group_Extension required plugins to provide * their own Submit button and nonce fields when building markup. In * BP 1.8, this requirement was lifted - BP_Group_Extension now handles * all required submit buttons and nonces. * * We put the edit screen markup into an output buffer before echoing. * This is so that we can check for the presence of a hardcoded submit * button, as would be present in legacy plugins; if one is found, we * do not auto-add our own button. * * @since BuddyPress 1.8.0 */ public function call_edit_screen() { ob_start(); call_user_func( $this ->screens[ 'edit' ][ 'screen_callback' ], $this ->group_id ); $screen = ob_get_contents(); ob_end_clean(); echo $this ->maybe_add_submit_button( $screen ); $this ->nonce_field( 'edit' ); } /** * Check the nonce, and call the edit_screen_save() method. * * @since BuddyPress 1.8.0 */ public function call_edit_screen_save() { if ( empty ( $_POST ) ) { return ; } // When DOING_AJAX, the POST global will be populated, but we // should assume it's a save. if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { return ; } $this ->check_nonce( 'edit' ); // Detect whether the screen_save_callback is performing a // redirect, so that we don't do one of our own. add_filter( 'wp_redirect' , array ( $this , 'detect_post_save_redirect' ) ); // Call the extension's save routine. call_user_func( $this ->screens[ 'edit' ][ 'screen_save_callback' ], $this ->group_id ); // Clean up detection filters. remove_filter( 'wp_redirect' , array ( $this , 'detect_post_save_redirect' ) ); // Perform a redirect only if one has not already taken place. if ( empty ( $this ->post_save_redirect ) ) { /** * Filters the URL to redirect to after group edit screen save. * * Only runs if a redirect has not already occurred. * * @since BuddyPress 2.1.0 * * @param string $value URL to redirect to. */ $redirect_to = apply_filters( 'bp_group_extension_edit_screen_save_redirect' , bp_get_requested_url( ) ); bp_core_redirect( $redirect_to ); die (); } } /** * Load the template that houses the Edit screen. * * Separated out into a callback so that it can run after all other * Group Extensions have had a chance to register their navigation, to * avoid missing tabs. * * Hooked to 'bp_screens'. * * @since BuddyPress 1.8.0 * * @see BP_Group_Extension::setup_edit_hooks() */ public function call_edit_screen_template_loader() { bp_core_load_template( $this ->edit_screen_template ); } /** * Add a submit button to the edit form, if it needs one. * * There's an inconsistency in the way that the group Edit and Create * screens are rendered: the Create screen has a submit button built * in, but the Edit screen does not. This function allows plugin * authors to write markup that does not contain the submit button for * use on both the Create and Edit screens - BP will provide the button * if one is not found. * * @since BuddyPress 1.8.0 * * @param string $screen The screen markup, captured in the output * buffer. * @return string $screen The same markup, with a submit button added. */ protected function maybe_add_submit_button( $screen = '' ) { if ( $this ->has_submit_button( $screen ) ) { return $screen ; } return $screen . sprintf( '<div id="%s"><input type="submit" name="save" value="%s" id="%s"></div>' , 'bp-group-edit-' . $this ->slug . '-submit-wrapper' , $this ->screens[ 'edit' ][ 'submit_text' ], 'bp-group-edit-' . $this ->slug . '-submit' ); } /** * Does the given markup have a submit button? * * @since BuddyPress 1.8.0 * * @param string $screen The markup to check. * @return bool True if a Submit button is found, otherwise false. */ public static function has_submit_button( $screen = '' ) { $pattern = "/<input[^>]+type=[\'\"]submit[\'\"]/" ; preg_match( $pattern , $screen , $matches ); return ! empty ( $matches [0] ); } /** * Detect redirects hardcoded into edit_screen_save() callbacks. * * @since BuddyPress 2.1.0 * * @param string $redirect Redirect string. * @return string */ public function detect_post_save_redirect( $redirect = '' ) { if ( ! empty ( $redirect ) ) { $this ->post_save_redirect = $redirect ; } return $redirect ; } /** Admin *************************************************************/ /** * Hook this extension's Admin metabox into BuddyPress, if necessary. * * @since BuddyPress 1.8.0 */ protected function setup_admin_hooks() { if ( ! $this ->is_screen_enabled( 'admin' ) || ! is_admin() ) { return ; } // Hook the admin screen markup function to the content hook. add_action( 'bp_groups_admin_meta_box_content_' . $this ->slug, array ( $this , 'call_admin_screen' ) ); // Initialize the metabox. add_action( 'bp_groups_admin_meta_boxes' , array ( $this , '_meta_box_display_callback' ) ); // Catch the metabox save. add_action( 'bp_group_admin_edit_after' , array ( $this , 'call_admin_screen_save' ), 10 ); } /** * Call the admin_screen() method, and add a nonce field. * * @since BuddyPress 1.8.0 */ public function call_admin_screen() { call_user_func( $this ->screens[ 'admin' ][ 'screen_callback' ], $this ->group_id ); $this ->nonce_field( 'admin' ); } /** * Check the nonce, and call the admin_screen_save() method. * * @since BuddyPress 1.8.0 */ public function call_admin_screen_save() { $this ->check_nonce( 'admin' ); call_user_func( $this ->screens[ 'admin' ][ 'screen_save_callback' ], $this ->group_id ); } /** * Create the Dashboard meta box for this extension. * * @since BuddyPress 1.7.0 */ public function _meta_box_display_callback() { $group_id = isset( $_GET [ 'gid' ] ) ? (int) $_GET [ 'gid' ] : 0; $screen = $this ->screens[ 'admin' ]; $extension_slug = $this ->slug; $callback = function () use ( $extension_slug , $group_id ) { do_action( 'bp_groups_admin_meta_box_content_' . $extension_slug , $group_id ); }; add_meta_box( $screen [ 'slug' ], $screen [ 'name' ], $callback , get_current_screen()->id, $screen [ 'metabox_context' ], $screen [ 'metabox_priority' ] ); } /** Utilities *********************************************************/ /** * Generate the nonce fields for a settings form. * * The nonce field name (the second param passed to wp_nonce_field) * contains this extension's slug and is thus unique to this extension. * This is necessary because in some cases (namely, the Dashboard), * more than one extension may generate nonces on the same page, and we * must avoid name clashes. * * @since BuddyPress 1.8.0 * * @param string $context Screen context. 'create', 'edit', or 'admin'. */ public function nonce_field( $context = '' ) { wp_nonce_field( 'bp_group_extension_' . $this ->slug . '_' . $context , '_bp_group_' . $context . '_nonce_' . $this ->slug ); } /** * Check the nonce on a submitted settings form. * * @since BuddyPress 1.8.0 * * @param string $context Screen context. 'create', 'edit', or 'admin'. */ public function check_nonce( $context = '' ) { check_admin_referer( 'bp_group_extension_' . $this ->slug . '_' . $context , '_bp_group_' . $context . '_nonce_' . $this ->slug ); } /** * Is the specified screen enabled? * * To be enabled, a screen must both have the 'enabled' key set to true * (legacy: $this->enable_create_step, etc), and its screen_callback * must also exist and be callable. * * @since BuddyPress 1.8.0 * * @param string $context Screen context. 'create', 'edit', or 'admin'. * @return bool True if the screen is enabled, otherwise false. */ public function is_screen_enabled( $context = '' ) { $enabled = false; if ( isset( $this ->screens[ $context ] ) ) { $enabled = $this ->screens[ $context ][ 'enabled' ] && is_callable ( $this ->screens[ $context ][ 'screen_callback' ] ); } return (bool) $enabled ; } /** * Get the appropriate screen callback for the specified context/type. * * BP Group Extensions have three special "screen contexts": create, * admin, and edit. Each of these contexts has a corresponding * _screen() and _screen_save() method, which allow group extension * plugins to define different markup and logic for each context. * * BP also supports fallback settings_screen() and * settings_screen_save() methods, which can be used to define markup * and logic that is shared between context. For each context, you may * either provide context-specific methods, or you can let BP fall back * on the shared settings_* callbacks. * * For example, consider a BP_Group_Extension implementation that looks * like this: * * // ... * function create_screen( $group_id ) { ... } * function create_screen_save( $group_id ) { ... } * function settings_screen( $group_id ) { ... } * function settings_screen_save( $group_id ) { ... } * // ... * * BP_Group_Extension will use your create_* methods for the Create * steps, and will use your generic settings_* methods for the Edit * and Admin contexts. This schema allows plugin authors maximum * flexibility without having to repeat themselves. * * The get_screen_callback() method uses a ReflectionClass object to * determine whether your extension has provided a given callback. * * @since BuddyPress 1.8.0 * * @param string $context Screen context. 'create', 'edit', or 'admin'. * @param string $type Screen type. 'screen' or 'screen_save'. Default: * 'screen'. * @return callable A callable function handle. */ public function get_screen_callback( $context = '' , $type = 'screen' ) { $callback = '' ; // Try the context-specific callback first. $method = $context . '_' . $type ; $rmethod = $this ->class_reflection->getMethod( $method ); if ( isset( $rmethod -> class ) && $this ->class_name === $rmethod -> class ) { $callback = array ( $this , $method ); } if ( empty ( $callback ) ) { $fallback_method = 'settings_' . $type ; $rfallback_method = $this ->class_reflection->getMethod( $fallback_method ); if ( isset( $rfallback_method -> class ) && $this ->class_name === $rfallback_method -> class ) { $callback = array ( $this , $fallback_method ); } } return $callback ; } /** * Recursive argument parsing. * * This acts like a multi-dimensional version of wp_parse_args() (minus * the querystring parsing - you must pass arrays). * * Values from $a override those from $b; keys in $b that don't exist * in $a are passed through. * * This is different from array_merge_recursive(), both because of the * order of preference ($a overrides $b) and because of the fact that * array_merge_recursive() combines arrays deep in the tree, rather * than overwriting the b array with the a array. * * The implementation of this function is specific to the needs of * BP_Group_Extension, where we know that arrays will always be * associative, and that an argument under a given key in one array * will be matched by a value of identical depth in the other one. The * function is NOT designed for general use, and will probably result * in unexpected results when used with data in the wild. See, eg, * * @since BuddyPress 1.8.0 * * @param array $a First set of arguments. * @param array $b Second set of arguments. * @return array Parsed arguments. */ public static function parse_args_r( & $a , $b ) { $a = ( array ) $a ; $b = ( array ) $b ; $r = $b ; foreach ( $a as $k => & $v ) { if ( is_array ( $v ) && isset( $r [ $k ] ) ) { $r [ $k ] = self::parse_args_r( $v , $r [ $k ] ); } else { $r [ $k ] = $v ; } } return $r ; } /** Legacy Support ********************************************************/ /* * In BuddyPress 1.8, the recommended technique for configuring * extensions changed from directly setting various object properties * in the class constructor, to passing a configuration array to * parent::init(). The following methods ensure that extensions created * in the old way continue to work, by converting legacy configuration * data to the new format. */ /** * Provide access to otherwise unavailable object properties. * * This magic method is here for backward compatibility with plugins * that refer to config properties that have moved to a different * location (such as enable_create_step, which is now at * $this->screens['create']['enabled'] * * The legacy_properties array is set up in * self::setup_legacy_properties(). * * @since BuddyPress 1.8.0 * * @param string $key Property name. * @return mixed The value if found, otherwise null. */ public function __get( $key ) { if ( isset( $this ->legacy_properties[ $key ] ) ) { return $this ->legacy_properties[ $key ]; } elseif ( isset( $this ->data[ $key ] ) ) { return $this ->data[ $key ]; } else { return null; } } /** * Provide a fallback for isset( $this->foo ) when foo is unavailable. * * This magic method is here for backward compatibility with plugins * that have set their class config options directly in the class * constructor. The parse_legacy_properties() method of the current * class needs to check whether any legacy keys have been put into the * $this->data array. * * @since BuddyPress 1.8.0 * * @param string $key Property name. * @return bool True if the value is set, otherwise false. */ public function __isset( $key ) { if ( isset( $this ->legacy_properties[ $key ] ) ) { return true; } elseif ( isset( $this ->data[ $key ] ) ) { return true; } else { return false; } } /** * Allow plugins to set otherwise unavailable object properties. * * This magic method is here for backward compatibility with plugins * that may attempt to modify the group extension by manually assigning * a value to an object property that no longer exists, such as * $this->enable_create_step. * * @since BuddyPress 1.8.0 * * @param string $key Property name. * @param mixed $value Property value. */ public function __set( $key , $value ) { if ( empty ( $this ->initialized ) ) { $this ->data[ $key ] = $value ; } switch ( $key ) { case 'enable_create_step' : $this ->screens[ 'create' ][ 'enabled' ] = $value ; break ; case 'enable_edit_item' : $this ->screens[ 'edit' ][ 'enabled' ] = $value ; break ; case 'enable_admin_item' : $this ->screens[ 'admin' ][ 'enabled' ] = $value ; break ; case 'create_step_position' : $this ->screens[ 'create' ][ 'position' ] = $value ; break ; // Note: 'admin' becomes 'edit' to distinguish from Dashboard 'admin'. case 'admin_name' : $this ->screens[ 'edit' ][ 'name' ] = $value ; break ; case 'admin_slug' : $this ->screens[ 'edit' ][ 'slug' ] = $value ; break ; case 'create_name' : $this ->screens[ 'create' ][ 'name' ] = $value ; break ; case 'create_slug' : $this ->screens[ 'create' ][ 'slug' ] = $value ; break ; case 'admin_metabox_context' : $this ->screens[ 'admin' ][ 'metabox_context' ] = $value ; break ; case 'admin_metabox_priority' : $this ->screens[ 'admin' ][ 'metabox_priority' ] = $value ; break ; default : $this ->data[ $key ] = $value ; break ; } } /** * Return a list of legacy properties. * * The legacy implementation of BP_Group_Extension used all of these * object properties for configuration. Some have been moved. * * @since BuddyPress 1.8.0 * * @return array List of legacy property keys. */ protected function get_legacy_property_list() { return array ( 'name' , 'slug' , 'admin_name' , 'admin_slug' , 'create_name' , 'create_slug' , 'visibility' , 'create_step_position' , 'nav_item_position' , 'admin_metabox_context' , 'admin_metabox_priority' , 'enable_create_step' , 'enable_nav_item' , 'enable_edit_item' , 'enable_admin_item' , 'nav_item_name' , 'display_hook' , 'template_file' , ); } /** * Parse legacy properties. * * The old standard for BP_Group_Extension was for plugins to register * their settings as properties in their constructor. The new method is * to pass a config array to the init() method. In order to support * legacy plugins, we slurp up legacy properties, and later on we'll * parse them into the new init() array. * * @since BuddyPress 1.8.0 */ protected function parse_legacy_properties() { // Only run this one time. if ( ! empty ( $this ->legacy_properties_converted ) ) { return ; } $properties = $this ->get_legacy_property_list(); // By-reference variable for convenience. $lpc =& $this ->legacy_properties_converted; foreach ( $properties as $property ) { // No legacy config exists for this key. if ( ! isset( $this ->{ $property } ) ) { continue ; } // Grab the value and record it as appropriate. $value = $this ->{ $property }; switch ( $property ) { case 'enable_create_step' : $lpc [ 'screens' ][ 'create' ][ 'enabled' ] = (bool) $value ; break ; case 'enable_edit_item' : $lpc [ 'screens' ][ 'edit' ][ 'enabled' ] = (bool) $value ; break ; case 'enable_admin_item' : $lpc [ 'screens' ][ 'admin' ][ 'enabled' ] = (bool) $value ; break ; case 'create_step_position' : $lpc [ 'screens' ][ 'create' ][ 'position' ] = $value ; break ; // Note: 'admin' becomes 'edit' to distinguish from Dashboard 'admin'. case 'admin_name' : $lpc [ 'screens' ][ 'edit' ][ 'name' ] = $value ; break ; case 'admin_slug' : $lpc [ 'screens' ][ 'edit' ][ 'slug' ] = $value ; break ; case 'create_name' : $lpc [ 'screens' ][ 'create' ][ 'name' ] = $value ; break ; case 'create_slug' : $lpc [ 'screens' ][ 'create' ][ 'slug' ] = $value ; break ; case 'admin_metabox_context' : $lpc [ 'screens' ][ 'admin' ][ 'metabox_context' ] = $value ; break ; case 'admin_metabox_priority' : $lpc [ 'screens' ][ 'admin' ][ 'metabox_priority' ] = $value ; break ; default : $lpc [ $property ] = $value ; break ; } } } /** * Set up legacy properties. * * This method is responsible for ensuring that all legacy config * properties are stored in an array $this->legacy_properties, so that * they remain available to plugins that reference the variables at * their old locations. * * @since BuddyPress 1.8.0 * * @see BP_Group_Extension::__get() */ protected function setup_legacy_properties() { // Only run this one time. if ( ! empty ( $this ->legacy_properties ) ) { return ; } $properties = $this ->get_legacy_property_list(); $params = $this ->params; $lp =& $this ->legacy_properties; foreach ( $properties as $property ) { switch ( $property ) { case 'enable_create_step' : $lp [ 'enable_create_step' ] = $params [ 'screens' ][ 'create' ][ 'enabled' ]; break ; case 'enable_edit_item' : $lp [ 'enable_edit_item' ] = $params [ 'screens' ][ 'edit' ][ 'enabled' ]; break ; case 'enable_admin_item' : $lp [ 'enable_admin_item' ] = $params [ 'screens' ][ 'admin' ][ 'enabled' ]; break ; case 'create_step_position' : $lp [ 'create_step_position' ] = $params [ 'screens' ][ 'create' ][ 'position' ]; break ; // Note: 'admin' becomes 'edit' to distinguish from Dashboard 'admin'. case 'admin_name' : $lp [ 'admin_name' ] = $params [ 'screens' ][ 'edit' ][ 'name' ]; break ; case 'admin_slug' : $lp [ 'admin_slug' ] = $params [ 'screens' ][ 'edit' ][ 'slug' ]; break ; case 'create_name' : $lp [ 'create_name' ] = $params [ 'screens' ][ 'create' ][ 'name' ]; break ; case 'create_slug' : $lp [ 'create_slug' ] = $params [ 'screens' ][ 'create' ][ 'slug' ]; break ; case 'admin_metabox_context' : $lp [ 'admin_metabox_context' ] = $params [ 'screens' ][ 'admin' ][ 'metabox_context' ]; break ; case 'admin_metabox_priority' : $lp [ 'admin_metabox_priority' ] = $params [ 'screens' ][ 'admin' ][ 'metabox_priority' ]; break ; default : // All other items get moved over. $lp [ $property ] = $params [ $property ]; // Also reapply to the object, for backpat. $this ->{ $property } = $params [ $property ]; break ; } } } } |
Changelog
Version | Description |
---|---|
BuddyPress 1.1.0 | Introduced. |
Methods
- __get — Provide access to otherwise unavailable object properties.
- __isset — Provide a fallback for isset( $this->foo ) when foo is unavailable.
- __set — Allow plugins to set otherwise unavailable object properties.
- _display_hook — Hook the main display method, and loads the template file.
- _meta_box_display_callback — Create the Dashboard meta box for this extension.
- _register — The main setup routine for the extension.
- admin_screen
- admin_screen_save
- call_admin_screen — Call the admin_screen() method, and add a nonce field.
- call_admin_screen_save — Check the nonce, and call the admin_screen_save() method.
- call_display — Call the display() method.
- call_edit_screen — Call the edit_screen() method.
- call_edit_screen_save — Check the nonce, and call the edit_screen_save() method.
- call_edit_screen_template_loader — Load the template that houses the Edit screen.
- check_nonce — Check the nonce on a submitted settings form.
- create_screen
- create_screen_save
- detect_post_save_redirect — Detect redirects hardcoded into edit_screen_save() callbacks.
- display — The content of the group tab.
- edit_screen
- edit_screen_save
- get_default_screens — Gather configuration data about your screens.
- get_group_id — Get the current group ID.
- get_legacy_property_list — Return a list of legacy properties.
- get_screen_callback — Get the appropriate screen callback for the specified context/type.
- group_access_protection — Filter the access check in bp_groups_group_access_protection() for this extension.
- has_submit_button — Does the given markup have a submit button?
- init — Initialize the extension, using your config settings.
- is_screen_enabled — Is the specified screen enabled?
- maybe_add_submit_button — Add a submit button to the edit form, if it needs one.
- maybe_create_screen — Call the create_screen() method, if we're on the right page.
- maybe_create_screen_save — Call the create_screen_save() method, if we're on the right page.
- nonce_field — Generate the nonce fields for a settings form.
- parse_args_r — Recursive argument parsing.
- parse_legacy_properties — Parse legacy properties.
- settings_screen
- settings_screen_save
- setup_access_settings — Set up access-related settings for this extension.
- setup_admin_hooks — Hook this extension's Admin metabox into BuddyPress, if necessary.
- setup_class_info — Set up some basic info about the Extension.
- setup_create_hooks — Hook this extension's Create step into BuddyPress, if necessary.
- setup_display_hooks — Hook this extension's group tab into BuddyPress, if necessary.
- setup_edit_hooks — Hook this extension's Edit panel into BuddyPress, if necessary.
- setup_legacy_properties — Set up legacy properties.
- setup_screens — Set up screens array based on params.
- user_can_see_nav_item — Determine whether the current user should see this nav tab.
- user_can_visit — Determine whether the current user has access to visit this tab.
- user_meets_access_condition — Check whether the current user meets an access condition.
- widget_display — Content displayed in a widget sidebar, if applicable.
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.