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


Generated: Sun Sep 26 01:01:02 2021 Cross-referenced by PHPXref 0.7.1