[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * BuddyPress Member Loader.
   4   *
   5   * @package BuddyPress
   6   * @subpackage Members
   7   * @since 1.5.0
   8   */
   9  
  10  // Exit if accessed directly.
  11  defined( 'ABSPATH' ) || exit;
  12  
  13  /**
  14   * Defines the BuddyPress Members Component.
  15   *
  16   * @since 1.5.0
  17   */
  18  class BP_Members_Component extends BP_Component {
  19  
  20      /**
  21       * Member types.
  22       *
  23       * @see bp_register_member_type()
  24       *
  25       * @since 2.2.0
  26       * @var array
  27       */
  28      public $types = array();
  29  
  30      /**
  31       * Start the members component creation process.
  32       *
  33       * @since 1.5.0
  34       */
  35  	public function __construct() {
  36          parent::start(
  37              'members',
  38              __( 'Members', 'buddypress' ),
  39              buddypress()->plugin_dir,
  40              array(
  41                  'adminbar_myaccount_order' => 20,
  42                  'search_query_arg' => 'members_search',
  43              )
  44          );
  45      }
  46  
  47      /**
  48       * Include bp-members files.
  49       *
  50       * @since 1.5.0
  51       *
  52       * @see BP_Component::includes() for description of parameters.
  53       *
  54       * @param array $includes See {@link BP_Component::includes()}.
  55       */
  56  	public function includes( $includes = array() ) {
  57  
  58          // Always include these files.
  59          $includes = array(
  60              'filters',
  61              'template',
  62              'adminbar',
  63              'functions',
  64              'widgets',
  65              'cache',
  66          );
  67  
  68          if ( bp_is_active( 'activity' ) ) {
  69              $includes[] = 'activity';
  70          }
  71  
  72          // Include these only if in admin.
  73          if ( is_admin() ) {
  74              $includes[] = 'admin';
  75          }
  76  
  77          parent::includes( $includes );
  78      }
  79  
  80      /**
  81       * Late includes method.
  82       *
  83       * Only load up certain code when on specific pages.
  84       *
  85       * @since 3.0.0
  86       */
  87  	public function late_includes() {
  88          // Bail if PHPUnit is running.
  89          if ( defined( 'BP_TESTS_DIR' ) ) {
  90              return;
  91          }
  92  
  93          // Members.
  94          if ( bp_is_members_component() ) {
  95              // Actions - Random member handler.
  96              if ( isset( $_GET['random-member'] ) ) {
  97                  require $this->path . 'bp-members/actions/random.php';
  98              }
  99  
 100              // Screens - Directory.
 101              if ( bp_is_members_directory() ) {
 102                  require $this->path . 'bp-members/screens/directory.php';
 103              }
 104          }
 105  
 106          // Members - User main nav screen.
 107          if ( bp_is_user() ) {
 108              require $this->path . 'bp-members/screens/profile.php';
 109          }
 110  
 111          // Members - Theme compatibility.
 112          if ( bp_is_members_component() || bp_is_user() ) {
 113              new BP_Members_Theme_Compat();
 114          }
 115  
 116          // Registration / Activation.
 117          if ( bp_is_register_page() || bp_is_activation_page() ) {
 118              if ( bp_is_register_page() ) {
 119                  require $this->path . 'bp-members/screens/register.php';
 120              } else {
 121                  require $this->path . 'bp-members/screens/activate.php';
 122              }
 123  
 124              // Theme compatibility.
 125              new BP_Registration_Theme_Compat();
 126          }
 127      }
 128  
 129      /**
 130       * Set up bp-members global settings.
 131       *
 132       * The BP_MEMBERS_SLUG constant is deprecated, and only used here for
 133       * backwards compatibility.
 134       *
 135       * @since 1.5.0
 136       *
 137       * @see BP_Component::setup_globals() for description of parameters.
 138       *
 139       * @param array $args See {@link BP_Component::setup_globals()}.
 140       */
 141  	public function setup_globals( $args = array() ) {
 142          global $wpdb;
 143  
 144          $bp = buddypress();
 145  
 146          /** Component Globals ************************************************
 147           */
 148  
 149          // Define a slug, as a fallback for backpat.
 150          if ( !defined( 'BP_MEMBERS_SLUG' ) ) {
 151              define( 'BP_MEMBERS_SLUG', $this->id );
 152          }
 153  
 154          // Fetch the default directory title.
 155          $default_directory_titles = bp_core_get_directory_page_default_titles();
 156          $default_directory_title  = $default_directory_titles[$this->id];
 157  
 158          // Override any passed args.
 159          $args = array(
 160              'slug'            => BP_MEMBERS_SLUG,
 161              'root_slug'       => isset( $bp->pages->members->slug ) ? $bp->pages->members->slug : BP_MEMBERS_SLUG,
 162              'has_directory'   => true,
 163              'directory_title' => isset( $bp->pages->members->title ) ? $bp->pages->members->title : $default_directory_title,
 164              'search_string'   => __( 'Search Members...', 'buddypress' ),
 165              'global_tables'   => array(
 166                  'table_name_last_activity' => bp_core_get_table_prefix() . 'bp_activity',
 167                  'table_name_signups'       => $wpdb->base_prefix . 'signups', // Signups is a global WordPress table.
 168              )
 169          );
 170  
 171          parent::setup_globals( $args );
 172  
 173          /** Logged in user ***************************************************
 174           */
 175  
 176          // The core userdata of the user who is currently logged in.
 177          $bp->loggedin_user->userdata       = bp_core_get_core_userdata( bp_loggedin_user_id() );
 178  
 179          // Fetch the full name for the logged in user.
 180          $bp->loggedin_user->fullname       = isset( $bp->loggedin_user->userdata->display_name ) ? $bp->loggedin_user->userdata->display_name : '';
 181  
 182          // Hits the DB on single WP installs so get this separately.
 183          $bp->loggedin_user->is_super_admin = $bp->loggedin_user->is_site_admin = is_super_admin( bp_loggedin_user_id() );
 184  
 185          // The domain for the user currently logged in. eg: http://example.com/members/andy.
 186          $bp->loggedin_user->domain         = bp_core_get_user_domain( bp_loggedin_user_id() );
 187  
 188          /** Displayed user ***************************************************
 189           */
 190  
 191          // The core userdata of the user who is currently being displayed.
 192          $bp->displayed_user->userdata = bp_core_get_core_userdata( bp_displayed_user_id() );
 193  
 194          // Fetch the full name displayed user.
 195          $bp->displayed_user->fullname = isset( $bp->displayed_user->userdata->display_name ) ? $bp->displayed_user->userdata->display_name : '';
 196  
 197          // The domain for the user currently being displayed.
 198          $bp->displayed_user->domain   = bp_core_get_user_domain( bp_displayed_user_id() );
 199  
 200          // Initialize the nav for the members component.
 201          $this->nav = new BP_Core_Nav();
 202  
 203          // If A user is displayed, check if there is a front template
 204          if ( bp_get_displayed_user() ) {
 205              $bp->displayed_user->front_template = bp_displayed_user_get_front_template();
 206          }
 207  
 208          /** Signup ***********************************************************
 209           */
 210  
 211          $bp->signup = new stdClass;
 212  
 213          /** Profiles Fallback ************************************************
 214           */
 215  
 216          if ( ! bp_is_active( 'xprofile' ) ) {
 217              $bp->profile       = new stdClass;
 218              $bp->profile->slug = 'profile';
 219              $bp->profile->id   = 'profile';
 220          }
 221      }
 222  
 223      /**
 224       * Set up canonical stack for this component.
 225       *
 226       * @since 2.1.0
 227       */
 228  	public function setup_canonical_stack() {
 229          $bp = buddypress();
 230  
 231          /** Default Profile Component ****************************************
 232           */
 233          if ( bp_displayed_user_has_front_template() ) {
 234              $bp->default_component = 'front';
 235          } elseif ( bp_is_active( 'activity' ) && isset( $bp->pages->activity ) ) {
 236              $bp->default_component = bp_get_activity_slug();
 237          } else {
 238              $bp->default_component = ( 'xprofile' === $bp->profile->id ) ? 'profile' : $bp->profile->id;
 239          }
 240  
 241          if ( defined( 'BP_DEFAULT_COMPONENT' ) && BP_DEFAULT_COMPONENT ) {
 242              $default_component = BP_DEFAULT_COMPONENT;
 243              if ( 'profile' === $default_component ) {
 244                  $default_component = 'xprofile';
 245              }
 246  
 247              if ( bp_is_active( $default_component ) ) {
 248                  $bp->default_component = BP_DEFAULT_COMPONENT;
 249              }
 250          }
 251  
 252          /** Canonical Component Stack ****************************************
 253           */
 254  
 255          if ( bp_displayed_user_id() ) {
 256              $bp->canonical_stack['base_url'] = bp_displayed_user_domain();
 257  
 258              if ( bp_current_component() ) {
 259                  $bp->canonical_stack['component'] = bp_current_component();
 260              }
 261  
 262              if ( bp_current_action() ) {
 263                  $bp->canonical_stack['action'] = bp_current_action();
 264              }
 265  
 266              if ( !empty( $bp->action_variables ) ) {
 267                  $bp->canonical_stack['action_variables'] = bp_action_variables();
 268              }
 269  
 270              // Looking at the single member root/home, so assume the default.
 271              if ( ! bp_current_component() ) {
 272                  $bp->current_component = $bp->default_component;
 273  
 274              // The canonical URL will not contain the default component.
 275              } elseif ( bp_is_current_component( $bp->default_component ) && ! bp_current_action() ) {
 276                  unset( $bp->canonical_stack['component'] );
 277              }
 278  
 279              // If we're on a spammer's profile page, only users with the 'bp_moderate' cap
 280              // can view subpages on the spammer's profile.
 281              //
 282              // users without the cap trying to access a spammer's subnav page will get
 283              // redirected to the root of the spammer's profile page.  this occurs by
 284              // by removing the component in the canonical stack.
 285              if ( bp_is_user_spammer( bp_displayed_user_id() ) && ! bp_current_user_can( 'bp_moderate' ) ) {
 286                  unset( $bp->canonical_stack['component'] );
 287              }
 288          }
 289      }
 290  
 291      /**
 292       * Set up fall-back component navigation if XProfile is inactive.
 293       *
 294       * @since 1.5.0
 295       *
 296       * @see BP_Component::setup_nav() for a description of arguments.
 297       *
 298       * @param array $main_nav Optional. See BP_Component::setup_nav() for
 299       *                        description.
 300       * @param array $sub_nav  Optional. See BP_Component::setup_nav() for
 301       *                        description.
 302       */
 303  	public function setup_nav( $main_nav = array(), $sub_nav = array() ) {
 304  
 305          // Don't set up navigation if there's no member.
 306          if ( ! is_user_logged_in() && ! bp_is_user() ) {
 307              return;
 308          }
 309  
 310          $is_xprofile_active = bp_is_active( 'xprofile' );
 311  
 312          // Bail if XProfile component is active and there's no custom front page for the user.
 313          if ( ! bp_displayed_user_has_front_template() && $is_xprofile_active ) {
 314              return;
 315          }
 316  
 317          // Determine user to use.
 318          if ( bp_displayed_user_domain() ) {
 319              $user_domain = bp_displayed_user_domain();
 320          } elseif ( bp_loggedin_user_domain() ) {
 321              $user_domain = bp_loggedin_user_domain();
 322          } else {
 323              return;
 324          }
 325  
 326          // Set slug to profile in case the xProfile component is not active
 327          $slug = bp_get_profile_slug();
 328  
 329          // Defaults to empty navs
 330          $this->main_nav = array();
 331          $this->sub_nav  = array();
 332  
 333          if ( ! $is_xprofile_active ) {
 334              $this->main_nav = array(
 335                  'name'                => _x( 'Profile', 'Member profile main navigation', 'buddypress' ),
 336                  'slug'                => $slug,
 337                  'position'            => 20,
 338                  'screen_function'     => 'bp_members_screen_display_profile',
 339                  'default_subnav_slug' => 'public',
 340                  'item_css_id'         => buddypress()->profile->id
 341              );
 342          }
 343  
 344          /**
 345           * Setup the subnav items for the member profile.
 346           *
 347           * This is required in case there's a custom front or in case the xprofile component
 348           * is not active.
 349           */
 350          $this->sub_nav = array(
 351              'name'            => _x( 'View', 'Member profile view', 'buddypress' ),
 352              'slug'            => 'public',
 353              'parent_url'      => trailingslashit( $user_domain . $slug ),
 354              'parent_slug'     => $slug,
 355              'screen_function' => 'bp_members_screen_display_profile',
 356              'position'        => 10
 357          );
 358  
 359          /**
 360           * If there's a front template the members component nav
 361           * will be there to display the user's front page.
 362           */
 363          if ( bp_displayed_user_has_front_template() ) {
 364              $main_nav = array(
 365                  'name'                => _x( 'Home', 'Member Home page', 'buddypress' ),
 366                  'slug'                => 'front',
 367                  'position'            => 5,
 368                  'screen_function'     => 'bp_members_screen_display_profile',
 369                  'default_subnav_slug' => 'public',
 370              );
 371  
 372              // We need a dummy subnav for the front page to load.
 373              $front_subnav = $this->sub_nav;
 374              $front_subnav['parent_slug'] = 'front';
 375  
 376              // In case the subnav is displayed in the front template
 377              $front_subnav['parent_url'] = trailingslashit( $user_domain . 'front' );
 378  
 379              // Set the subnav
 380              $sub_nav[] = $front_subnav;
 381  
 382              /**
 383               * If the profile component is not active, we need to create a new
 384               * nav to display the WordPress profile.
 385               */
 386              if ( ! $is_xprofile_active ) {
 387                  add_action( 'bp_members_setup_nav', array( $this, 'setup_profile_nav' ) );
 388              }
 389  
 390          /**
 391           * If there's no front template and xProfile is not active, the members
 392           * component nav will be there to display the WordPress profile
 393           */
 394          } else {
 395              $main_nav  = $this->main_nav;
 396              $sub_nav[] = $this->sub_nav;
 397          }
 398  
 399  
 400          parent::setup_nav( $main_nav, $sub_nav );
 401      }
 402  
 403      /**
 404       * Set up a profile nav in case the xProfile
 405       * component is not active and a front template is
 406       * used.
 407       *
 408       * @since 2.6.0
 409       */
 410  	public function setup_profile_nav() {
 411          if ( empty( $this->main_nav ) || empty( $this->sub_nav ) ) {
 412              return;
 413          }
 414  
 415          // Add the main nav
 416          bp_core_new_nav_item( $this->main_nav, 'members' );
 417  
 418          // Add the sub nav item.
 419          bp_core_new_subnav_item( $this->sub_nav, 'members' );
 420      }
 421  
 422      /**
 423       * Set up the title for pages and <title>.
 424       *
 425       * @since 1.5.0
 426       */
 427  	public function setup_title() {
 428          $bp = buddypress();
 429  
 430          if ( bp_is_my_profile() ) {
 431              $bp->bp_options_title = __( 'You', 'buddypress' );
 432          } elseif ( bp_is_user() ) {
 433              $bp->bp_options_title  = bp_get_displayed_user_fullname();
 434              $bp->bp_options_avatar = bp_core_fetch_avatar( array(
 435                  'item_id' => bp_displayed_user_id(),
 436                  'type'    => 'thumb',
 437                  'alt'     => sprintf( __( 'Profile picture of %s', 'buddypress' ), $bp->bp_options_title )
 438              ) );
 439          }
 440  
 441          parent::setup_title();
 442      }
 443  
 444      /**
 445       * Setup cache groups.
 446       *
 447       * @since 2.2.0
 448       */
 449  	public function setup_cache_groups() {
 450  
 451          // Global groups.
 452          wp_cache_add_global_groups( array(
 453              'bp_last_activity',
 454              'bp_member_type'
 455          ) );
 456  
 457          parent::setup_cache_groups();
 458      }
 459  }


Generated: Thu Jul 18 01:01:42 2019 Cross-referenced by PHPXref 0.7.1