[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-groups/classes/ -> class-bp-groups-component.php (source)

   1  <?php
   2  /**
   3   * BuddyPress Groups Component Class.
   4   *
   5   * @package BuddyPress
   6   * @subpackage GroupsLoader
   7   * @since 1.5.0
   8   */
   9  
  10  // Exit if accessed directly.
  11  defined( 'ABSPATH' ) || exit;
  12  
  13  /**
  14   * Creates our Groups component.
  15   *
  16   * @since 1.5.0
  17   */
  18  class BP_Groups_Component extends BP_Component {
  19  
  20      /**
  21       * Auto-join group when non group member performs group activity.
  22       *
  23       * @since 1.5.0
  24       * @var bool
  25       */
  26      public $auto_join;
  27  
  28      /**
  29       * The group being currently accessed.
  30       *
  31       * @since 1.5.0
  32       * @var BP_Groups_Group
  33       */
  34      public $current_group;
  35  
  36      /**
  37       * Default group extension.
  38       *
  39       * @since 1.6.0
  40       * @todo Is this used anywhere? Is this a duplicate of $default_extension?
  41       * @var string
  42       */
  43      var $default_component;
  44  
  45      /**
  46       * Default group extension.
  47       *
  48       * @since 1.6.0
  49       * @var string
  50       */
  51      public $default_extension;
  52  
  53      /**
  54       * Illegal group names/slugs.
  55       *
  56       * @since 1.5.0
  57       * @var array
  58       */
  59      public $forbidden_names;
  60  
  61      /**
  62       * Group creation/edit steps (e.g. Details, Settings, Avatar, Invites).
  63       *
  64       * @since 1.5.0
  65       * @var array
  66       */
  67      public $group_creation_steps;
  68  
  69      /**
  70       * Types of group statuses (Public, Private, Hidden).
  71       *
  72       * @since 1.5.0
  73       * @var array
  74       */
  75      public $valid_status;
  76  
  77      /**
  78       * Group types.
  79       *
  80       * @see bp_groups_register_group_type()
  81       *
  82       * @since 2.6.0
  83       * @var array
  84       */
  85      public $types = array();
  86  
  87      /**
  88       * Current directory group type.
  89       *
  90       * @see groups_directory_groups_setup()
  91       *
  92       * @since 2.7.0
  93       * @var string
  94       */
  95      public $current_directory_type = '';
  96  
  97      /**
  98       * List of registered Group extensions.
  99       *
 100       * @see bp_register_group_extension()
 101       *
 102       * @since 10.0.0
 103       * @var array
 104       */
 105      public $group_extensions = array();
 106  
 107      /**
 108       * Start the groups component creation process.
 109       *
 110       * @since 1.5.0
 111       */
 112  	public function __construct() {
 113          $features = array();
 114          if ( bp_is_active( 'friends' ) ) {
 115              $features[] = 'invitations';
 116          }
 117  
 118          parent::start(
 119              'groups',
 120              _x( 'User Groups', 'Group screen page <title>', 'buddypress' ),
 121              buddypress()->plugin_dir,
 122              array(
 123                  'adminbar_myaccount_order' => 70,
 124                  'search_query_arg'         => 'groups_search',
 125                  'features'                 => $features,
 126              )
 127          );
 128      }
 129  
 130      /**
 131       * Include Groups component files.
 132       *
 133       * @since 1.5.0
 134       *
 135       * @see BP_Component::includes() for a description of arguments.
 136       *
 137       * @param array $includes See BP_Component::includes() for a description.
 138       */
 139  	public function includes( $includes = array() ) {
 140          $includes = array(
 141              'cache',
 142              'filters',
 143              'widgets',
 144              'template',
 145              'adminbar',
 146              'functions',
 147              'notifications',
 148              'cssjs',
 149              'blocks',
 150          );
 151  
 152          // Conditional includes.
 153          if ( bp_is_active( 'activity' ) ) {
 154              $includes[] = 'activity';
 155          }
 156          if ( is_admin() ) {
 157              $includes[] = 'admin';
 158          }
 159  
 160          parent::includes( $includes );
 161      }
 162  
 163      /**
 164       * Late includes method.
 165       *
 166       * Only load up certain code when on specific pages.
 167       *
 168       * @since 3.0.0
 169       */
 170  	public function late_includes() {
 171          // Bail if PHPUnit is running.
 172          if ( defined( 'BP_TESTS_DIR' ) ) {
 173              return;
 174          }
 175  
 176          if ( bp_is_groups_component() ) {
 177              // Authenticated actions.
 178              if ( is_user_logged_in() &&
 179                  in_array( bp_current_action(), array( 'create', 'join', 'leave-group' ), true )
 180              ) {
 181                  require $this->path . 'bp-groups/actions/' . bp_current_action() . '.php';
 182              }
 183  
 184              // Actions - RSS feed handler.
 185              if ( bp_is_active( 'activity' ) && bp_is_current_action( 'feed' ) ) {
 186                  require $this->path . 'bp-groups/actions/feed.php';
 187              }
 188  
 189              // Actions - Random group handler.
 190              if ( isset( $_GET['random-group'] ) ) {
 191                  require $this->path . 'bp-groups/actions/random.php';
 192              }
 193  
 194              // Screens - Directory.
 195              if ( bp_is_groups_directory() ) {
 196                  require $this->path . 'bp-groups/screens/directory.php';
 197              }
 198  
 199              // Screens - User profile integration.
 200              if ( bp_is_user() ) {
 201                  require $this->path . 'bp-groups/screens/user/my-groups.php';
 202  
 203                  if ( bp_is_current_action( 'invites' ) ) {
 204                      require $this->path . 'bp-groups/screens/user/invites.php';
 205                  }
 206              }
 207  
 208              // Single group.
 209              if ( bp_is_group() ) {
 210                  // Actions - Access protection.
 211                  require $this->path . 'bp-groups/actions/access.php';
 212  
 213                  // Public nav items.
 214                  if ( in_array( bp_current_action(), array( 'home', 'request-membership', 'activity', 'members', 'send-invites' ), true ) ) {
 215                      require $this->path . 'bp-groups/screens/single/' . bp_current_action() . '.php';
 216                  }
 217  
 218                  // Admin nav items.
 219                  if ( bp_is_item_admin() && is_user_logged_in() ) {
 220                      require $this->path . 'bp-groups/screens/single/admin.php';
 221  
 222                      if ( in_array( bp_get_group_current_admin_tab(), array( 'edit-details', 'group-settings', 'group-avatar', 'group-cover-image', 'manage-members', 'membership-requests', 'delete-group' ), true ) ) {
 223                          require $this->path . 'bp-groups/screens/single/admin/' . bp_get_group_current_admin_tab() . '.php';
 224                      }
 225                  }
 226              }
 227  
 228              // Theme compatibility.
 229              new BP_Groups_Theme_Compat();
 230          }
 231      }
 232  
 233      /**
 234       * Set up additional globals for the component.
 235       *
 236       * @since 10.0.0
 237       */
 238  	public function setup_additional_globals() {
 239          $bp = buddypress();
 240  
 241          /* Single Group Globals **********************************************/
 242  
 243          // Are we viewing a single group?
 244          if ( bp_is_groups_component()
 245              && ( ( $group_id = BP_Groups_Group::group_exists( bp_current_action() ) )
 246                  || ( $group_id = BP_Groups_Group::get_id_by_previous_slug( bp_current_action() ) ) )
 247              ) {
 248              $bp->is_single_item  = true;
 249  
 250              /**
 251               * Filters the current PHP Class being used.
 252               *
 253               * @since 1.5.0
 254               *
 255               * @param string $value Name of the class being used.
 256               */
 257              $current_group_class = apply_filters( 'bp_groups_current_group_class', 'BP_Groups_Group' );
 258  
 259              if ( $current_group_class == 'BP_Groups_Group' ) {
 260                  $this->current_group = groups_get_group( $group_id );
 261  
 262              } else {
 263  
 264                  /**
 265                   * Filters the current group object being instantiated from previous filter.
 266                   *
 267                   * @since 1.5.0
 268                   *
 269                   * @param object $value Newly instantiated object for the group.
 270                   */
 271                  $this->current_group = apply_filters( 'bp_groups_current_group_object', new $current_group_class( $group_id ) );
 272              }
 273  
 274              // Make sure the Group ID is an integer.
 275              $this->current_group->id = (int) $this->current_group->id;
 276  
 277              // When in a single group, the first action is bumped down one because of the
 278              // group name, so we need to adjust this and set the group name to current_item.
 279              $bp->current_item   = bp_current_action();
 280              $bp->current_action = bp_action_variable( 0 );
 281              array_shift( $bp->action_variables );
 282  
 283              // Using "item" not "group" for generic support in other components.
 284              if ( bp_current_user_can( 'bp_moderate' ) ) {
 285                  bp_update_is_item_admin( true, 'groups' );
 286              } else {
 287                  bp_update_is_item_admin( groups_is_user_admin( bp_loggedin_user_id(), $this->current_group->id ), 'groups' );
 288              }
 289  
 290              // If the user is not an admin, check if they are a moderator.
 291              if ( ! bp_is_item_admin() ) {
 292                  bp_update_is_item_mod( groups_is_user_mod( bp_loggedin_user_id(), $this->current_group->id ), 'groups' );
 293              }
 294  
 295              // Check once if the current group has a custom front template.
 296              $this->current_group->front_template = bp_groups_get_front_template( $this->current_group );
 297  
 298              /**
 299               * Fires once the `current_group` global is fully set.
 300               *
 301               * @since 10.0.0
 302               *
 303               * @param BP_Groups_Group|object $current_group The current group object.
 304               */
 305              do_action_ref_array( 'bp_groups_set_current_group', array( $this->current_group ) );
 306  
 307              // Initialize the nav for the groups component.
 308              $this->nav = new BP_Core_Nav( $this->current_group->id );
 309  
 310          // Set current_group to 0 to prevent debug errors.
 311          } else {
 312              $this->current_group = 0;
 313          }
 314  
 315          // Set group type if available.
 316          if ( bp_is_groups_directory() && bp_is_current_action( bp_get_groups_group_type_base() ) && bp_action_variable() ) {
 317              $matched_types = bp_groups_get_group_types( array(
 318                  'has_directory'  => true,
 319                  'directory_slug' => bp_action_variable(),
 320              ) );
 321  
 322              // Set 404 if we do not have a valid group type.
 323              if ( empty( $matched_types ) ) {
 324                  bp_do_404();
 325                  return;
 326              }
 327  
 328              // Set our directory type marker.
 329              $this->current_directory_type = reset( $matched_types );
 330          }
 331  
 332          // Set up variables specific to the group creation process.
 333          if ( bp_is_groups_component() && bp_is_current_action( 'create' ) && bp_user_can_create_groups() && isset( $_COOKIE['bp_new_group_id'] ) ) {
 334              $bp->groups->new_group_id = (int) $_COOKIE['bp_new_group_id'];
 335          }
 336  
 337          /**
 338           * Filters the list of illegal groups names/slugs.
 339           *
 340           * @since 1.0.0
 341           *
 342           * @param array $value Array of illegal group names/slugs.
 343           */
 344          $this->forbidden_names = apply_filters( 'groups_forbidden_names', array(
 345              'my-groups',
 346              'create',
 347              'invites',
 348              'send-invites',
 349              'forum',
 350              'delete',
 351              'add',
 352              'admin',
 353              'request-membership',
 354              'members',
 355              'settings',
 356              'avatar',
 357              $this->slug,
 358              $this->root_slug,
 359          ) );
 360  
 361          // If the user was attempting to access a group, but no group by that name was found, 404.
 362          if ( bp_is_groups_component() && empty( $this->current_group ) && empty( $this->current_directory_type ) && bp_current_action() && ! in_array( bp_current_action(), $this->forbidden_names ) ) {
 363              bp_do_404();
 364              return;
 365          }
 366  
 367          // Set default Group creation steps.
 368          $group_creation_steps = array(
 369              'group-details'  => array(
 370                  'name'       => _x( 'Details', 'Group screen nav', 'buddypress' ),
 371                  'position'   => 0
 372              ),
 373              'group-settings' => array(
 374                  'name'       => _x( 'Settings', 'Group screen nav', 'buddypress' ),
 375                  'position'   => 10
 376              )
 377          );
 378  
 379          // If avatar uploads are not disabled, add avatar option.
 380          $disabled_avatar_uploads = (int) bp_disable_group_avatar_uploads();
 381          if ( ! $disabled_avatar_uploads && $bp->avatar->show_avatars ) {
 382              $group_creation_steps['group-avatar'] = array(
 383                  'name'     => _x( 'Photo', 'Group screen nav', 'buddypress' ),
 384                  'position' => 20
 385              );
 386          }
 387  
 388          if ( bp_group_use_cover_image_header() ) {
 389              $group_creation_steps['group-cover-image'] = array(
 390                  'name'     => _x( 'Cover Image', 'Group screen nav', 'buddypress' ),
 391                  'position' => 25
 392              );
 393          }
 394  
 395          // If invitations are enabled, add invitations.
 396          if ( bp_is_active( 'groups', 'invitations' ) ) {
 397              $group_creation_steps['group-invites'] = array(
 398                  'name'     => _x( 'Invites',  'Group screen nav', 'buddypress' ),
 399                  'position' => 30
 400              );
 401          }
 402  
 403          /**
 404           * Filters the preconfigured groups creation steps.
 405           *
 406           * @since 1.1.0
 407           *
 408           * @param array $value Array of preconfigured group creation steps.
 409           */
 410          $this->group_creation_steps = apply_filters( 'groups_create_group_steps', $group_creation_steps );
 411  
 412          /**
 413           * Filters the list of valid groups statuses.
 414           *
 415           * @since 1.1.0
 416           *
 417           * @param array $value Array of valid group statuses.
 418           */
 419          $this->valid_status = apply_filters( 'groups_valid_status', array(
 420              'public',
 421              'private',
 422              'hidden'
 423          ) );
 424  
 425          // Auto join group when non group member performs group activity.
 426          $this->auto_join = defined( 'BP_DISABLE_AUTO_GROUP_JOIN' ) && BP_DISABLE_AUTO_GROUP_JOIN ? false : true;
 427      }
 428  
 429      /**
 430       * Set up component global data.
 431       *
 432       * The BP_GROUPS_SLUG constant is deprecated, and only used here for
 433       * backwards compatibility.
 434       *
 435       * @since 1.5.0
 436       *
 437       * @see BP_Component::setup_globals() for a description of arguments.
 438       *
 439       * @param array $args See BP_Component::setup_globals() for a description.
 440       */
 441  	public function setup_globals( $args = array() ) {
 442          $bp = buddypress();
 443  
 444          // Define a slug, if necessary.
 445          if ( ! defined( 'BP_GROUPS_SLUG' ) ) {
 446              define( 'BP_GROUPS_SLUG', $this->id );
 447          }
 448  
 449          // Global tables for groups component.
 450          $global_tables = array(
 451              'table_name'           => $bp->table_prefix . 'bp_groups',
 452              'table_name_members'   => $bp->table_prefix . 'bp_groups_members',
 453              'table_name_groupmeta' => $bp->table_prefix . 'bp_groups_groupmeta'
 454          );
 455  
 456          // Metadata tables for groups component.
 457          $meta_tables = array(
 458              'group' => $bp->table_prefix . 'bp_groups_groupmeta',
 459          );
 460  
 461          // Fetch the default directory title.
 462          $default_directory_titles = bp_core_get_directory_page_default_titles();
 463          $default_directory_title  = $default_directory_titles[$this->id];
 464  
 465          // All globals for groups component.
 466          // Note that global_tables is included in this array.
 467          $args = array(
 468              'slug'                  => BP_GROUPS_SLUG,
 469              'root_slug'             => isset( $bp->pages->groups->slug ) ? $bp->pages->groups->slug : BP_GROUPS_SLUG,
 470              'has_directory'         => true,
 471              'directory_title'       => isset( $bp->pages->groups->title ) ? $bp->pages->groups->title : $default_directory_title,
 472              'notification_callback' => 'groups_format_notifications',
 473              'search_string'         => _x( 'Search Groups...', 'Component directory search', 'buddypress' ),
 474              'global_tables'         => $global_tables,
 475              'meta_tables'           => $meta_tables,
 476              'block_globals'         => array(
 477                  'bp/dynamic-groups' => array(
 478                      'widget_classnames' => array( 'widget_bp_groups_widget', 'buddypress' ),
 479                  ),
 480              ),
 481          );
 482  
 483          parent::setup_globals( $args );
 484  
 485          // Additional globals.
 486          $this->setup_additional_globals();
 487      }
 488  
 489      /**
 490       * Set up canonical stack for this component.
 491       *
 492       * @since 2.1.0
 493       */
 494  	public function setup_canonical_stack() {
 495          if ( ! bp_is_groups_component() ) {
 496              return;
 497          }
 498  
 499          if ( empty( $this->current_group ) ) {
 500              return;
 501          }
 502  
 503          /**
 504           * Filters the default groups extension.
 505           *
 506           * @since 1.6.0
 507           *
 508           * @param string $value BP_GROUPS_DEFAULT_EXTENSION constant if defined,
 509           *                      else 'home'.
 510           */
 511          $this->default_extension = apply_filters( 'bp_groups_default_extension', defined( 'BP_GROUPS_DEFAULT_EXTENSION' ) ? BP_GROUPS_DEFAULT_EXTENSION : 'home' );
 512  
 513          $bp = buddypress();
 514  
 515          // If the activity component is not active and the current group has no custom front, members are displayed in the home nav.
 516          if ( 'members' === $this->default_extension && ! bp_is_active( 'activity' ) && ! $this->current_group->front_template ) {
 517              $this->default_extension = 'home';
 518          }
 519  
 520          if ( ! bp_current_action() ) {
 521              $bp->current_action = $this->default_extension;
 522          }
 523  
 524          // Prepare for a redirect to the canonical URL.
 525          $bp->canonical_stack['base_url'] = bp_get_group_permalink( $this->current_group );
 526  
 527          if ( bp_current_action() ) {
 528              $bp->canonical_stack['action'] = bp_current_action();
 529          }
 530  
 531          /**
 532           * If there's no custom front.php template for the group, we need to make sure the canonical stack action
 533           * is set to 'home' in these 2 cases:
 534           *
 535           * - the current action is 'activity' (eg: site.url/groups/single/activity) and the Activity component is active
 536           * - the current action is 'members' (eg: site.url/groups/single/members) and the Activity component is *not* active.
 537           */
 538          if ( ! $this->current_group->front_template && ( bp_is_current_action( 'activity' ) || ( ! bp_is_active( 'activity' ) && bp_is_current_action( 'members' ) ) ) ) {
 539              $bp->canonical_stack['action'] = 'home';
 540          }
 541  
 542          if ( ! empty( $bp->action_variables ) ) {
 543              $bp->canonical_stack['action_variables'] = bp_action_variables();
 544          }
 545  
 546          // When viewing the default extension, the canonical URL should not have
 547          // that extension's slug, unless more has been tacked onto the URL via
 548          // action variables.
 549          if ( bp_is_current_action( $this->default_extension ) && empty( $bp->action_variables ) )  {
 550              unset( $bp->canonical_stack['action'] );
 551          }
 552      }
 553  
 554      /**
 555       * Set up component navigation.
 556       *
 557       * @since 1.5.0
 558       *
 559       * @see BP_Component::setup_nav() for a description of arguments.
 560       *
 561       * @param array $main_nav Optional. See BP_Component::setup_nav() for description.
 562       * @param array $sub_nav  Optional. See BP_Component::setup_nav() for description.
 563       */
 564  	public function setup_nav( $main_nav = array(), $sub_nav = array() ) {
 565  
 566          // Determine user to use.
 567          if ( bp_displayed_user_domain() ) {
 568              $user_domain = bp_displayed_user_domain();
 569          } elseif ( bp_loggedin_user_domain() ) {
 570              $user_domain = bp_loggedin_user_domain();
 571          } else {
 572              $user_domain = false;
 573          }
 574  
 575          // Only grab count if we're on a user page.
 576          if ( bp_is_user() ) {
 577              $class = ( 0 === groups_total_groups_for_user( bp_displayed_user_id() ) ) ? 'no-count' : 'count';
 578  
 579              $nav_name = sprintf(
 580                  /* translators: %s: Group count for the current user */
 581                  _x( 'Groups %s', 'Group screen nav with counter', 'buddypress' ),
 582                  sprintf(
 583                      '<span class="%s">%s</span>',
 584                      esc_attr( $class ),
 585                      bp_get_total_group_count_for_user()
 586                  )
 587              );
 588          } else {
 589              $nav_name = _x( 'Groups', 'Group screen nav without counter', 'buddypress' );
 590          }
 591  
 592          $slug = bp_get_groups_slug();
 593  
 594          // Add 'Groups' to the main navigation.
 595          $main_nav = array(
 596              'name'                => $nav_name,
 597              'slug'                => $slug,
 598              'position'            => 70,
 599              'screen_function'     => 'groups_screen_my_groups',
 600              'default_subnav_slug' => 'my-groups',
 601              'item_css_id'         => $this->id
 602          );
 603  
 604          if ( ! empty( $user_domain ) ) {
 605              $access      = bp_core_can_edit_settings();
 606              $groups_link = trailingslashit( $user_domain . $slug );
 607  
 608              // Add the My Groups nav item.
 609              $sub_nav[] = array(
 610                  'name'            => __( 'Memberships', 'buddypress' ),
 611                  'slug'            => 'my-groups',
 612                  'parent_url'      => $groups_link,
 613                  'parent_slug'     => $slug,
 614                  'screen_function' => 'groups_screen_my_groups',
 615                  'position'        => 10,
 616                  'item_css_id'     => 'groups-my-groups'
 617              );
 618  
 619              if ( bp_is_active( 'groups', 'invitations' ) ) {
 620                  // Add the Group Invites nav item.
 621                  $sub_nav[] = array(
 622                      'name'            => __( 'Invitations', 'buddypress' ),
 623                      'slug'            => 'invites',
 624                      'parent_url'      => $groups_link,
 625                      'parent_slug'     => $slug,
 626                      'screen_function' => 'groups_screen_group_invites',
 627                      'user_has_access' => $access,
 628                      'position'        => 30
 629                  );
 630              }
 631  
 632              parent::setup_nav( $main_nav, $sub_nav );
 633          }
 634  
 635          if ( bp_is_groups_component() && bp_is_single_item() ) {
 636  
 637              // Reset sub nav.
 638              $sub_nav = array();
 639  
 640              /*
 641               * The top-level Groups item is called 'Memberships' for legacy reasons.
 642               * It does not appear in the interface.
 643               */
 644              bp_core_new_nav_item( array(
 645                  'name'                => __( 'Memberships', 'buddypress' ),
 646                  'slug'                => $this->current_group->slug,
 647                  'position'            => -1, // Do not show into the navigation.
 648                  'screen_function'     => 'groups_screen_group_home',
 649                  'default_subnav_slug' => $this->default_extension,
 650                  'item_css_id'         => $this->id
 651              ), 'groups' );
 652  
 653              $group_link = bp_get_group_permalink( $this->current_group );
 654  
 655              // Add the "Home" subnav item, as this will always be present.
 656              $sub_nav[] = array(
 657                  'name'            =>  _x( 'Home', 'Group screen navigation title', 'buddypress' ),
 658                  'slug'            => 'home',
 659                  'parent_url'      => $group_link,
 660                  'parent_slug'     => $this->current_group->slug,
 661                  'screen_function' => 'groups_screen_group_home',
 662                  'position'        => 10,
 663                  'item_css_id'     => 'home'
 664              );
 665  
 666              // If this is a private group, and the user is not a
 667              // member and does not have an outstanding invitation,
 668              // show a "Request Membership" nav item.
 669              if ( bp_current_user_can( 'groups_request_membership', array( 'group_id' => $this->current_group->id ) ) ) {
 670  
 671                  $sub_nav[] = array(
 672                      'name'            => _x( 'Request Membership','Group screen nav', 'buddypress' ),
 673                      'slug'            => 'request-membership',
 674                      'parent_url'      => $group_link,
 675                      'parent_slug'     => $this->current_group->slug,
 676                      'screen_function' => 'groups_screen_group_request_membership',
 677                      'position'        => 30
 678                  );
 679              }
 680  
 681              if ( $this->current_group->front_template || bp_is_active( 'activity' ) ) {
 682                  /**
 683                   * If the theme is using a custom front, create activity subnav.
 684                   */
 685                  if ( $this->current_group->front_template && bp_is_active( 'activity' ) ) {
 686                      $sub_nav[] = array(
 687                          'name'            => _x( 'Activity', 'My Group screen nav', 'buddypress' ),
 688                          'slug'            => 'activity',
 689                          'parent_url'      => $group_link,
 690                          'parent_slug'     => $this->current_group->slug,
 691                          'screen_function' => 'groups_screen_group_activity',
 692                          'position'        => 11,
 693                          'user_has_access' => $this->current_group->user_has_access,
 694                          'item_css_id'     => 'activity',
 695                          'no_access_url'   => $group_link,
 696                      );
 697                  }
 698  
 699                  /**
 700                   * Only add the members subnav if it's not the home's nav.
 701                   */
 702                  $sub_nav[] = array(
 703                      'name'            => sprintf(
 704                          /* translators: %s: total member count */
 705                          _x( 'Members %s', 'My Group screen nav', 'buddypress' ),
 706                          '<span>' . number_format( $this->current_group->total_member_count ) . '</span>'
 707                      ),
 708                      'slug'            => 'members',
 709                      'parent_url'      => $group_link,
 710                      'parent_slug'     => $this->current_group->slug,
 711                      'screen_function' => 'groups_screen_group_members',
 712                      'position'        => 60,
 713                      'user_has_access' => $this->current_group->user_has_access,
 714                      'item_css_id'     => 'members',
 715                      'no_access_url'   => $group_link,
 716                  );
 717              }
 718  
 719              if ( bp_is_active( 'groups', 'invitations' ) ) {
 720                  $sub_nav[] = array(
 721                      'name'            => _x( 'Send Invites', 'My Group screen nav', 'buddypress' ),
 722                      'slug'            => 'send-invites',
 723                      'parent_url'      => $group_link,
 724                      'parent_slug'     => $this->current_group->slug,
 725                      'screen_function' => 'groups_screen_group_invite',
 726                      'item_css_id'     => 'invite',
 727                      'position'        => 70,
 728                      'user_has_access' => $this->current_group->user_has_access,
 729                      'no_access_url'   => $group_link,
 730                  );
 731              }
 732  
 733              // If the user is a group admin, then show the group admin nav item.
 734              if ( bp_is_item_admin() ) {
 735                  $sub_nav[] = array(
 736                      'name'            => _x( 'Manage', 'My Group screen nav', 'buddypress' ),
 737                      'slug'            => 'admin',
 738                      'parent_url'      => $group_link,
 739                      'parent_slug'     => $this->current_group->slug,
 740                      'screen_function' => 'groups_screen_group_admin',
 741                      'position'        => 1000,
 742                      'user_has_access' => true,
 743                      'item_css_id'     => 'admin',
 744                      'no_access_url'   => $group_link,
 745                  );
 746  
 747                  $admin_link = trailingslashit( $group_link . 'admin' );
 748  
 749                  // Common params to all nav items.
 750                  $default_params = array(
 751                      'parent_url'        => $admin_link,
 752                      'parent_slug'       => $this->current_group->slug . '_manage',
 753                      'screen_function'   => 'groups_screen_group_admin',
 754                      'user_has_access'   => bp_is_item_admin(),
 755                      'show_in_admin_bar' => true,
 756                  );
 757  
 758                  $sub_nav[] = array_merge( array(
 759                      'name'     => __( 'Details', 'buddypress' ),
 760                      'slug'     => 'edit-details',
 761                      'position' => 0,
 762                  ), $default_params );
 763  
 764                  $sub_nav[] = array_merge( array(
 765                      'name'     => __( 'Settings', 'buddypress' ),
 766                      'slug'     => 'group-settings',
 767                      'position' => 10,
 768                  ), $default_params );
 769  
 770                  if ( ! bp_disable_group_avatar_uploads() && buddypress()->avatar->show_avatars ) {
 771                      $sub_nav[] = array_merge( array(
 772                          'name'     => __( 'Photo', 'buddypress' ),
 773                          'slug'     => 'group-avatar',
 774                          'position' => 20,
 775                      ), $default_params );
 776                  }
 777  
 778                  if ( bp_group_use_cover_image_header() ) {
 779                      $sub_nav[] = array_merge( array(
 780                          'name'     => __( 'Cover Image', 'buddypress' ),
 781                          'slug'     => 'group-cover-image',
 782                          'position' => 25,
 783                      ), $default_params );
 784                  }
 785  
 786                  $sub_nav[] = array_merge( array(
 787                      'name'     => __( 'Members', 'buddypress' ),
 788                      'slug'     => 'manage-members',
 789                      'position' => 30,
 790                  ), $default_params );
 791  
 792                  if ( 'private' == $this->current_group->status ) {
 793                      $sub_nav[] = array_merge( array(
 794                          'name'     => __( 'Requests', 'buddypress' ),
 795                          'slug'     => 'membership-requests',
 796                          'position' => 40,
 797                      ), $default_params );
 798                  }
 799  
 800                  $sub_nav[] = array_merge( array(
 801                      'name'     => __( 'Delete', 'buddypress' ),
 802                      'slug'     => 'delete-group',
 803                      'position' => 1000,
 804                  ), $default_params );
 805              }
 806  
 807              foreach ( $sub_nav as $nav ) {
 808                  bp_core_new_subnav_item( $nav, 'groups' );
 809              }
 810          }
 811  
 812          if ( isset( $this->current_group->user_has_access ) ) {
 813  
 814              /**
 815               * Fires at the end of the groups navigation setup if user has access.
 816               *
 817               * @since 1.0.2
 818               *
 819               * @param bool $user_has_access Whether or not user has access.
 820               */
 821              do_action( 'groups_setup_nav', $this->current_group->user_has_access );
 822          } else {
 823  
 824              /** This action is documented in bp-groups/bp-groups-loader.php */
 825              do_action( 'groups_setup_nav');
 826          }
 827      }
 828  
 829      /**
 830       * Set up the component entries in the WordPress Admin Bar.
 831       *
 832       * @since 1.5.0
 833       *
 834       * @see BP_Component::setup_nav() for a description of the $wp_admin_nav
 835       *      parameter array.
 836       *
 837       * @param array $wp_admin_nav See BP_Component::setup_admin_bar() for a description.
 838       */
 839  	public function setup_admin_bar( $wp_admin_nav = array() ) {
 840  
 841          // Menus for logged in user.
 842          if ( is_user_logged_in() ) {
 843  
 844              // Setup the logged in user variables.
 845              $groups_link = trailingslashit( bp_loggedin_user_domain() . bp_get_groups_slug() );
 846  
 847              $title   = _x( 'Groups', 'My Account Groups', 'buddypress' );
 848              $pending = _x( 'No Pending Invites', 'My Account Groups sub nav', 'buddypress' );
 849  
 850              if ( bp_is_active( 'groups', 'invitations' ) ) {
 851                  // Pending group invites.
 852                  $count   = groups_get_invite_count_for_user();
 853                  if ( $count ) {
 854                      $title = sprintf(
 855                          /* translators: %s: Group invitation count for the current user */
 856                          _x( 'Groups %s', 'My Account Groups nav', 'buddypress' ),
 857                          '<span class="count">' . bp_core_number_format( $count ) . '</span>'
 858                      );
 859  
 860                      $pending = sprintf(
 861                          /* translators: %s: Group invitation count for the current user */
 862                          _x( 'Pending Invites %s', 'My Account Groups sub nav', 'buddypress' ),
 863                          '<span class="count">' . bp_core_number_format( $count ) . '</span>'
 864                      );
 865                  }
 866              }
 867  
 868              // Add the "My Account" sub menus.
 869              $wp_admin_nav[] = array(
 870                  'parent' => buddypress()->my_account_menu_id,
 871                  'id'     => 'my-account-' . $this->id,
 872                  'title'  => $title,
 873                  'href'   => $groups_link
 874              );
 875  
 876              // My Groups.
 877              $wp_admin_nav[] = array(
 878                  'parent'   => 'my-account-' . $this->id,
 879                  'id'       => 'my-account-' . $this->id . '-memberships',
 880                  'title'    => _x( 'Memberships', 'My Account Groups sub nav', 'buddypress' ),
 881                  'href'     => trailingslashit( $groups_link . 'my-groups' ),
 882                  'position' => 10
 883              );
 884  
 885              // Invitations.
 886              if ( bp_is_active( 'groups', 'invitations' ) ) {
 887                  $wp_admin_nav[] = array(
 888                      'parent'   => 'my-account-' . $this->id,
 889                      'id'       => 'my-account-' . $this->id . '-invites',
 890                      'title'    => $pending,
 891                      'href'     => trailingslashit( $groups_link . 'invites' ),
 892                      'position' => 30
 893                  );
 894              }
 895  
 896              // Create a Group.
 897              if ( bp_user_can_create_groups() ) {
 898                  $wp_admin_nav[] = array(
 899                      'parent'   => 'my-account-' . $this->id,
 900                      'id'       => 'my-account-' . $this->id . '-create',
 901                      'title'    => _x( 'Create a Group', 'My Account Groups sub nav', 'buddypress' ),
 902                      'href'     => trailingslashit( bp_get_groups_directory_permalink() . 'create' ),
 903                      'position' => 90
 904                  );
 905              }
 906          }
 907  
 908          parent::setup_admin_bar( $wp_admin_nav );
 909      }
 910  
 911      /**
 912       * Set up the title for pages and <title>.
 913       *
 914       * @since 1.5.0
 915       */
 916  	public function setup_title() {
 917  
 918          if ( bp_is_groups_component() ) {
 919              $bp = buddypress();
 920  
 921              if ( bp_is_my_profile() && !bp_is_single_item() ) {
 922                  $bp->bp_options_title = _x( 'Memberships', 'My Groups page <title>', 'buddypress' );
 923  
 924              } elseif ( !bp_is_my_profile() && !bp_is_single_item() ) {
 925                  $bp->bp_options_avatar = bp_core_fetch_avatar( array(
 926                      'item_id' => bp_displayed_user_id(),
 927                      'type'    => 'thumb',
 928                      'alt'     => sprintf(
 929                          /* translators: %s: member name */
 930                          __( 'Profile picture of %s', 'buddypress' ),
 931                          bp_get_displayed_user_fullname()
 932                      ),
 933                  ) );
 934                  $bp->bp_options_title = bp_get_displayed_user_fullname();
 935  
 936              // We are viewing a single group, so set up the
 937              // group navigation menu using the $this->current_group global.
 938              } elseif ( bp_is_single_item() ) {
 939                  $bp->bp_options_title  = $this->current_group->name;
 940                  $bp->bp_options_avatar = bp_core_fetch_avatar( array(
 941                      'item_id'    => $this->current_group->id,
 942                      'object'     => 'group',
 943                      'type'       => 'thumb',
 944                      'avatar_dir' => 'group-avatars',
 945                      'alt'        => __( 'Group Profile Photo', 'buddypress' )
 946                  ) );
 947  
 948                  if ( empty( $bp->bp_options_avatar ) ) {
 949                      $bp->bp_options_avatar = '<img loading="lazy" src="' . esc_url( bp_core_avatar_default_thumb() ) . '" alt="' . esc_attr__( 'No Group Profile Photo', 'buddypress' ) . '" class="avatar" />';
 950                  }
 951              }
 952          }
 953  
 954          parent::setup_title();
 955      }
 956  
 957      /**
 958       * Setup cache groups
 959       *
 960       * @since 2.2.0
 961       */
 962  	public function setup_cache_groups() {
 963  
 964          // Global groups.
 965          wp_cache_add_global_groups( array(
 966              'bp_groups',
 967              'bp_group_admins',
 968              'bp_group_invite_count',
 969              'group_meta',
 970              'bp_groups_memberships',
 971              'bp_groups_memberships_for_user',
 972          ) );
 973  
 974          parent::setup_cache_groups();
 975      }
 976  
 977      /**
 978       * Set up taxonomies.
 979       *
 980       * @since 2.6.0
 981       * @since 7.0.0 The Group Type taxonomy is registered using the `bp_groups_register_group_type_taxonomy()` function.
 982       */
 983  	public function register_taxonomies() {
 984  
 985          // Just let BP Component fire 'bp_groups_register_taxonomies'.
 986          return parent::register_taxonomies();
 987      }
 988  
 989      /**
 990       * Init the BP REST API.
 991       *
 992       * @since 5.0.0
 993       * @since 6.0.0 Adds the Group Cover REST endpoint.
 994       *
 995       * @param array $controllers Optional. See BP_Component::rest_api_init() for
 996       *                           description.
 997       */
 998  	public function rest_api_init( $controllers = array() ) {
 999          $controllers = array(
1000              'BP_REST_Groups_Endpoint',
1001              'BP_REST_Group_Membership_Endpoint',
1002              'BP_REST_Group_Invites_Endpoint',
1003              'BP_REST_Group_Membership_Request_Endpoint',
1004              'BP_REST_Attachments_Group_Avatar_Endpoint',
1005          );
1006  
1007          // Support to Group Cover.
1008          if ( bp_is_active( 'groups', 'cover_image' ) ) {
1009              $controllers[] = 'BP_REST_Attachments_Group_Cover_Endpoint';
1010          }
1011  
1012          parent::rest_api_init( $controllers );
1013      }
1014  
1015      /**
1016       * Register the BP Groups Blocks.
1017       *
1018       * @since 6.0.0
1019       *
1020       * @param array $blocks Optional. See BP_Component::blocks_init() for
1021       *                      description.
1022       */
1023  	public function blocks_init( $blocks = array() ) {
1024          parent::blocks_init(
1025              array(
1026                  'bp/group' => array(
1027                      'name'               => 'bp/group',
1028                      'editor_script'      => 'bp-group-block',
1029                      'editor_script_url'  => plugins_url( 'js/blocks/group.js', dirname(  __FILE__ ) ),
1030                      'editor_script_deps' => array(
1031                          'wp-blocks',
1032                          'wp-element',
1033                          'wp-components',
1034                          'wp-i18n',
1035                          'wp-block-editor',
1036                          'wp-server-side-render',
1037                          'bp-block-components',
1038                          'bp-block-data',
1039                      ),
1040                      'style'              => 'bp-group-block',
1041                      'style_url'          => plugins_url( 'css/blocks/group.css', dirname( __FILE__ ) ),
1042                      'render_callback'    => 'bp_groups_render_group_block',
1043                      'attributes'         => array(
1044                          'itemID'              => array(
1045                              'type'    => 'integer',
1046                              'default' => 0,
1047                          ),
1048                          'avatarSize'          => array(
1049                              'type'    => 'string',
1050                              'default' => 'full',
1051                          ),
1052                          'displayDescription'  => array(
1053                              'type'    => 'boolean',
1054                              'default' => true,
1055                          ),
1056                          'displayActionButton' => array(
1057                              'type'    => 'boolean',
1058                              'default' => true,
1059                          ),
1060                          'displayCoverImage'   => array(
1061                              'type'    => 'boolean',
1062                              'default' => true,
1063                          ),
1064                      ),
1065                  ),
1066                  'bp/groups' => array(
1067                      'name'               => 'bp/groups',
1068                      'editor_script'      => 'bp-groups-block',
1069                      'editor_script_url'  => plugins_url( 'js/blocks/groups.js', dirname( __FILE__ ) ),
1070                      'editor_script_deps' => array(
1071                          'wp-blocks',
1072                          'wp-element',
1073                          'wp-components',
1074                          'wp-i18n',
1075                          'wp-api-fetch',
1076                          'wp-url',
1077                          'wp-block-editor',
1078                          'bp-block-components',
1079                          'bp-block-data',
1080                          'lodash',
1081                      ),
1082                      'style'              => 'bp-groups-block',
1083                      'style_url'          => plugins_url( 'css/blocks/groups.css', dirname( __FILE__ ) ),
1084                      'attributes'         => array(
1085                          'itemIDs'          => array(
1086                              'type'  => 'array',
1087                              'items' => array(
1088                                  'type' => 'integer',
1089                              ),
1090                          ),
1091                          'avatarSize'       => array(
1092                              'type'    => 'string',
1093                              'default' => 'full',
1094                          ),
1095                          'displayGroupName' => array(
1096                              'type'    => 'boolean',
1097                              'default' => true,
1098                          ),
1099                          'extraInfo'        => array(
1100                              'type'    => 'string',
1101                              'default' => 'none',
1102                              'enum'    => array( 'description', 'popular', 'active', 'none' ),
1103                          ),
1104                          'layoutPreference' => array(
1105                              'type'    => 'string',
1106                              'default' => 'list',
1107                              'enum'    => array( 'list', 'grid' ),
1108                          ),
1109                          'columns'          => array(
1110                              'type'    => 'number',
1111                              'default' => 2,
1112                          ),
1113                      ),
1114                      'render_callback'    => 'bp_groups_render_groups_block',
1115                  ),
1116                  'bp/dynamic-groups' => array(
1117                      'name'               => 'bp/dynamic-groups',
1118                      'editor_script'      => 'bp-dynamic-groups-block',
1119                      'editor_script_url'  => plugins_url( 'js/blocks/dynamic-groups.js', dirname( __FILE__ ) ),
1120                      'editor_script_deps' => array(
1121                          'wp-blocks',
1122                          'wp-element',
1123                          'wp-components',
1124                          'wp-i18n',
1125                          'wp-block-editor',
1126                          'wp-server-side-render',
1127                      ),
1128                      'style'              => 'bp-dynamic-groups-block',
1129                      'style_url'          => plugins_url( 'css/blocks/dynamic-groups.css', dirname( __FILE__ ) ),
1130                      'attributes'         => array(
1131                          'title'        => array(
1132                              'type'    => 'string',
1133                              'default' => __( 'Groups', 'buddypress' ),
1134                          ),
1135                          'maxGroups'    => array(
1136                              'type'    => 'number',
1137                              'default' => 5,
1138                          ),
1139                          'groupDefault' => array(
1140                              'type'    => 'string',
1141                              'default' => 'active',
1142                          ),
1143                          'linkTitle'    => array(
1144                              'type'    => 'boolean',
1145                              'default' => false,
1146                          ),
1147                      ),
1148                      'render_callback'    => 'bp_groups_render_dynamic_groups_block',
1149                  ),
1150              )
1151          );
1152      }
1153  
1154      /**
1155       * Add the Groups directory states.
1156       *
1157       * @since 10.0.0
1158       *
1159       * @param array   $states Optional. See BP_Component::admin_directory_states() for description.
1160       * @param WP_Post $post   Optional. See BP_Component::admin_directory_states() for description.
1161       * @return array          See BP_Component::admin_directory_states() for description.
1162       */
1163  	public function admin_directory_states( $states = array(), $post = null ) {
1164          $bp = buddypress();
1165  
1166          if ( isset( $bp->pages->groups->id ) && (int) $bp->pages->groups->id === (int) $post->ID ) {
1167              $states['page_for_groups_directory'] = _x( 'BP Groups Page', 'page label', 'buddypress' );
1168          }
1169  
1170          return parent::admin_directory_states( $states, $post );
1171      }
1172  }


Generated: Tue Mar 19 01:01:09 2024 Cross-referenced by PHPXref 0.7.1