[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * BuddyPress XProfile Loader.
   4   *
   5   * An extended profile component for users. This allows site admins to create
   6   * groups of fields for users to enter information about themselves.
   7   *
   8   * @package BuddyPress
   9   * @subpackage XProfileLoader
  10   * @since 1.5.0
  11   */
  12  
  13  // Exit if accessed directly.
  14  defined( 'ABSPATH' ) || exit;
  15  
  16  /**
  17   * Creates our XProfile component.
  18   *
  19   * @since 1.5.0
  20   */
  21  class BP_XProfile_Component extends BP_Component {
  22  
  23      /**
  24       * Profile field types.
  25       *
  26       * @since 1.5.0
  27       * @var array
  28       */
  29      public $field_types;
  30  
  31      /**
  32       * The acceptable visibility levels for xprofile fields.
  33       *
  34       * @see bp_xprofile_get_visibility_levels()
  35       *
  36       * @since 1.6.0
  37       * @var array
  38       */
  39      public $visibility_levels = array();
  40  
  41      /**
  42       * Start the xprofile component creation process.
  43       *
  44       * @since 1.5.0
  45       */
  46  	public function __construct() {
  47          parent::start(
  48              'xprofile',
  49              _x( 'Extended Profiles', 'Component page <title>', 'buddypress' ),
  50              buddypress()->plugin_dir,
  51              array(
  52                  'adminbar_myaccount_order' => 20
  53              )
  54          );
  55  
  56          $this->setup_hooks();
  57      }
  58  
  59      /**
  60       * Include files.
  61       *
  62       * @since 1.5.0
  63       *
  64       * @param array $includes Array of files to include.
  65       */
  66  	public function includes( $includes = array() ) {
  67          $includes = array(
  68              'cssjs',
  69              'cache',
  70              'caps',
  71              'filters',
  72              'template',
  73              'functions',
  74          );
  75  
  76          // Conditional includes.
  77          if ( bp_is_active( 'activity' ) ) {
  78              $includes[] = 'activity';
  79          }
  80          if ( bp_is_active( 'notifications' ) ) {
  81              $includes[] = 'notifications';
  82          }
  83          if ( bp_is_active( 'settings' ) ) {
  84              $includes[] = 'settings';
  85          }
  86          if ( is_admin() ) {
  87              $includes[] = 'admin';
  88          }
  89  
  90          parent::includes( $includes );
  91      }
  92  
  93      /**
  94       * Late includes method.
  95       *
  96       * Only load up certain code when on specific pages.
  97       *
  98       * @since 3.0.0
  99       */
 100  	public function late_includes() {
 101          // Bail if PHPUnit is running.
 102          if ( defined( 'BP_TESTS_DIR' ) ) {
 103              return;
 104          }
 105  
 106          // Bail if not on a user page.
 107          if ( ! bp_is_user() ) {
 108              return;
 109          }
 110  
 111          // User nav.
 112          if ( bp_is_profile_component() ) {
 113              require $this->path . 'bp-xprofile/screens/public.php';
 114  
 115              // Sub-nav items.
 116              if ( is_user_logged_in() && 'edit' === bp_current_action() ) {
 117                  require $this->path . 'bp-xprofile/screens/edit.php';
 118              }
 119          }
 120  
 121          // Settings.
 122          if ( is_user_logged_in() && bp_is_user_settings_profile() ) {
 123              require $this->path . 'bp-xprofile/screens/settings-profile.php';
 124          }
 125      }
 126  
 127      /**
 128       * Setup globals.
 129       *
 130       * The BP_XPROFILE_SLUG constant is deprecated, and only used here for
 131       * backwards compatibility.
 132       *
 133       * @since 1.5.0
 134       *
 135       * @param array $args Array of globals to set up.
 136       */
 137  	public function setup_globals( $args = array() ) {
 138          $bp = buddypress();
 139  
 140          // Define a slug, if necessary.
 141          if ( !defined( 'BP_XPROFILE_SLUG' ) ) {
 142              define( 'BP_XPROFILE_SLUG', 'profile' );
 143          }
 144  
 145          // Assign the base group and fullname field names to constants
 146          // to use in SQL statements.
 147          // Defined conditionally to accommodate unit tests.
 148          if ( ! defined( 'BP_XPROFILE_BASE_GROUP_NAME' ) ) {
 149              define( 'BP_XPROFILE_BASE_GROUP_NAME', stripslashes( bp_core_get_root_option( 'avatar_default' ) ) );
 150          }
 151  
 152          if ( ! defined( 'BP_XPROFILE_FULLNAME_FIELD_NAME' ) ) {
 153              define( 'BP_XPROFILE_FULLNAME_FIELD_NAME', stripslashes( bp_core_get_root_option( 'bp-xprofile-fullname-field-name' ) ) );
 154          }
 155  
 156          /**
 157           * Filters the supported field type IDs.
 158           *
 159           * @since 1.1.0
 160           *
 161           * @param array $value Array of IDs for the supported field types.
 162           */
 163          $this->field_types = apply_filters( 'xprofile_field_types', array_keys( bp_xprofile_get_field_types() ) );
 164  
 165          /*
 166           * 'option' is a special case. It is not a top-level field, so
 167           * does not have an associated BP_XProfile_Field_Type class,
 168           * but it must be explicitly allowed.
 169           */
 170          $this->field_types[] = 'option';
 171  
 172          // Register the visibility levels. See bp_xprofile_get_visibility_levels() to filter.
 173          $this->visibility_levels = array(
 174              'public' => array(
 175                  'id'      => 'public',
 176                  'label' => _x( 'Everyone', 'Visibility level setting', 'buddypress' )
 177              ),
 178              'adminsonly' => array(
 179                  'id'      => 'adminsonly',
 180                  'label' => _x( 'Only Me', 'Visibility level setting', 'buddypress' )
 181              ),
 182              'loggedin' => array(
 183                  'id'      => 'loggedin',
 184                  'label' => _x( 'All Members', 'Visibility level setting', 'buddypress' )
 185              )
 186          );
 187  
 188          if ( bp_is_active( 'friends' ) ) {
 189              $this->visibility_levels['friends'] = array(
 190                  'id'    => 'friends',
 191                  'label'    => _x( 'My Friends', 'Visibility level setting', 'buddypress' )
 192              );
 193          }
 194  
 195          // Tables.
 196          $global_tables = array(
 197              'table_name_data'   => $bp->table_prefix . 'bp_xprofile_data',
 198              'table_name_groups' => $bp->table_prefix . 'bp_xprofile_groups',
 199              'table_name_fields' => $bp->table_prefix . 'bp_xprofile_fields',
 200              'table_name_meta'   => $bp->table_prefix . 'bp_xprofile_meta',
 201          );
 202  
 203          $meta_tables = array(
 204              'xprofile_group' => $bp->table_prefix . 'bp_xprofile_meta',
 205              'xprofile_field' => $bp->table_prefix . 'bp_xprofile_meta',
 206              'xprofile_data'  => $bp->table_prefix . 'bp_xprofile_meta',
 207          );
 208  
 209          $globals = array(
 210              'slug'                  => BP_XPROFILE_SLUG,
 211              'has_directory'         => false,
 212              'notification_callback' => 'xprofile_format_notifications',
 213              'global_tables'         => $global_tables,
 214              'meta_tables'           => $meta_tables,
 215          );
 216  
 217          parent::setup_globals( $globals );
 218      }
 219  
 220      /**
 221       * Set up navigation.
 222       *
 223       * @since 1.5.0
 224       *
 225       * @global BuddyPress $bp The one true BuddyPress instance
 226       *
 227       * @param array $main_nav Array of main nav items to set up.
 228       * @param array $sub_nav  Array of sub nav items to set up.
 229       */
 230  	public function setup_nav( $main_nav = array(), $sub_nav = array() ) {
 231  
 232          // Determine user to use.
 233          if ( bp_displayed_user_domain() ) {
 234              $user_domain = bp_displayed_user_domain();
 235          } elseif ( bp_loggedin_user_domain() ) {
 236              $user_domain = bp_loggedin_user_domain();
 237          } else {
 238              return;
 239          }
 240  
 241          $access       = bp_core_can_edit_settings();
 242          $slug         = bp_get_profile_slug();
 243          $profile_link = trailingslashit( $user_domain . $slug );
 244  
 245          // Add 'Profile' to the main navigation.
 246          $main_nav = array(
 247              'name'                => _x( 'Profile', 'Profile header menu', 'buddypress' ),
 248              'slug'                => $slug,
 249              'position'            => 20,
 250              'screen_function'     => 'xprofile_screen_display_profile',
 251              'default_subnav_slug' => 'public',
 252              'item_css_id'         => $this->id
 253          );
 254  
 255          // Add the subnav items to the profile.
 256          $sub_nav[] = array(
 257              'name'            => _x( 'View', 'Profile header sub menu', 'buddypress' ),
 258              'slug'            => 'public',
 259              'parent_url'      => $profile_link,
 260              'parent_slug'     => $slug,
 261              'screen_function' => 'xprofile_screen_display_profile',
 262              'position'        => 10
 263          );
 264  
 265          // Edit Profile.
 266          $sub_nav[] = array(
 267              'name'            => _x( 'Edit','Profile header sub menu', 'buddypress' ),
 268              'slug'            => 'edit',
 269              'parent_url'      => $profile_link,
 270              'parent_slug'     => $slug,
 271              'screen_function' => 'xprofile_screen_edit_profile',
 272              'position'        => 20,
 273              'user_has_access' => $access
 274          );
 275  
 276          // The Settings > Profile nav item can only be set up after
 277          // the Settings component has run its own nav routine.
 278          add_action( 'bp_settings_setup_nav', array( $this, 'setup_settings_nav' ) );
 279  
 280          parent::setup_nav( $main_nav, $sub_nav );
 281      }
 282  
 283      /**
 284       * Set up the Settings > Profile nav item.
 285       *
 286       * Loaded in a separate method because the Settings component may not
 287       * be loaded in time for BP_XProfile_Component::setup_nav().
 288       *
 289       * @since 2.1.0
 290       */
 291  	public function setup_settings_nav() {
 292          if ( ! bp_is_active( 'settings' ) ) {
 293              return;
 294          }
 295  
 296          // Determine user to use.
 297          if ( bp_displayed_user_domain() ) {
 298              $user_domain = bp_displayed_user_domain();
 299          } elseif ( bp_loggedin_user_domain() ) {
 300              $user_domain = bp_loggedin_user_domain();
 301          } else {
 302              return;
 303          }
 304  
 305          // Get the settings slug.
 306          $settings_slug = bp_get_settings_slug();
 307  
 308          bp_core_new_subnav_item( array(
 309              'name'            => _x( 'Profile Visibility', 'Profile settings sub nav', 'buddypress' ),
 310              'slug'            => 'profile',
 311              'parent_url'      => trailingslashit( $user_domain . $settings_slug ),
 312              'parent_slug'     => $settings_slug,
 313              'screen_function' => 'bp_xprofile_screen_settings',
 314              'position'        => 30,
 315              'user_has_access' => bp_core_can_edit_settings()
 316          ), 'members' );
 317      }
 318  
 319      /**
 320       * Set up the Admin Bar.
 321       *
 322       * @since 1.5.0
 323       *
 324       * @param array $wp_admin_nav Admin Bar items.
 325       */
 326  	public function setup_admin_bar( $wp_admin_nav = array() ) {
 327  
 328          // Menus for logged in user.
 329          if ( is_user_logged_in() ) {
 330  
 331              // Profile link.
 332              $profile_link = trailingslashit( bp_loggedin_user_domain() . bp_get_profile_slug() );
 333  
 334              // Add the "Profile" sub menu.
 335              $wp_admin_nav[] = array(
 336                  'parent' => buddypress()->my_account_menu_id,
 337                  'id'     => 'my-account-' . $this->id,
 338                  'title'  => _x( 'Profile', 'My Account Profile', 'buddypress' ),
 339                  'href'   => $profile_link
 340              );
 341  
 342              // View Profile.
 343              $wp_admin_nav[] = array(
 344                  'parent'   => 'my-account-' . $this->id,
 345                  'id'       => 'my-account-' . $this->id . '-public',
 346                  'title'    => _x( 'View', 'My Account Profile sub nav', 'buddypress' ),
 347                  'href'     => trailingslashit( $profile_link . 'public' ),
 348                  'position' => 10
 349              );
 350  
 351              // Edit Profile.
 352              $wp_admin_nav[] = array(
 353                  'parent'   => 'my-account-' . $this->id,
 354                  'id'       => 'my-account-' . $this->id . '-edit',
 355                  'title'    => _x( 'Edit', 'My Account Profile sub nav', 'buddypress' ),
 356                  'href'     => trailingslashit( $profile_link . 'edit' ),
 357                  'position' => 20
 358              );
 359          }
 360  
 361          parent::setup_admin_bar( $wp_admin_nav );
 362      }
 363  
 364      /**
 365       * Add custom hooks.
 366       *
 367       * @since 2.0.0
 368       */
 369  	public function setup_hooks() {
 370          add_filter( 'bp_settings_admin_nav', array( $this, 'setup_settings_admin_nav' ), 2 );
 371      }
 372  
 373      /**
 374       * Sets up the title for pages and <title>.
 375       *
 376       * @since 1.5.0
 377       */
 378  	public function setup_title() {
 379  
 380          if ( bp_is_profile_component() ) {
 381              $bp = buddypress();
 382  
 383              if ( bp_is_my_profile() ) {
 384                  $bp->bp_options_title = _x( 'My Profile', 'Page title', 'buddypress' );
 385              } else {
 386                  $bp->bp_options_avatar = bp_core_fetch_avatar( array(
 387                      'item_id' => bp_displayed_user_id(),
 388                      'type'    => 'thumb',
 389  
 390                      /* translators: %s: member name */
 391                      'alt'      => sprintf( _x( 'Profile picture of %s', 'Avatar alt', 'buddypress' ), bp_get_displayed_user_fullname() )
 392                  ) );
 393                  $bp->bp_options_title = bp_get_displayed_user_fullname();
 394              }
 395          }
 396  
 397          parent::setup_title();
 398      }
 399  
 400      /**
 401       * Setup cache groups.
 402       *
 403       * @since 2.2.0
 404       */
 405  	public function setup_cache_groups() {
 406  
 407          // Global groups.
 408          wp_cache_add_global_groups( array(
 409              'bp_xprofile',
 410              'bp_xprofile_data',
 411              'bp_xprofile_fields',
 412              'bp_xprofile_groups',
 413              'xprofile_meta',
 414              'bp_user_mid',
 415          ) );
 416  
 417          parent::setup_cache_groups();
 418      }
 419  
 420      /**
 421       * Adds "Settings > Profile" subnav item under the "Settings" adminbar menu.
 422       *
 423       * @since 2.0.0
 424       *
 425       * @param array $wp_admin_nav The settings adminbar nav array.
 426       * @return array
 427       */
 428  	public function setup_settings_admin_nav( $wp_admin_nav ) {
 429  
 430          // Setup the logged in user variables.
 431          $settings_link = trailingslashit( bp_loggedin_user_domain() . bp_get_settings_slug() );
 432  
 433          // Add the "Profile" subnav item.
 434          $wp_admin_nav[] = array(
 435              'parent' => 'my-account-' . buddypress()->settings->id,
 436              'id'     => 'my-account-' . buddypress()->settings->id . '-profile',
 437              'title'  => _x( 'Profile', 'My Account Settings sub nav', 'buddypress' ),
 438              'href'   => trailingslashit( $settings_link . 'profile' )
 439          );
 440  
 441          return $wp_admin_nav;
 442      }
 443  
 444      /**
 445       * Init the BP REST API.
 446       *
 447       * @since 5.0.0
 448       *
 449       * @param array $controllers Optional. See BP_Component::rest_api_init() for
 450       *                           description.
 451       */
 452  	public function rest_api_init( $controllers = array() ) {
 453          parent::rest_api_init( array(
 454              'BP_REST_XProfile_Fields_Endpoint',
 455              'BP_REST_XProfile_Field_Groups_Endpoint',
 456              'BP_REST_XProfile_Data_Endpoint',
 457          ) );
 458      }
 459  
 460      /**
 461       * Register the BP xProfile Blocks.
 462       *
 463       * @since 9.0.0
 464       *
 465       * @param array $blocks Optional. See BP_Component::blocks_init() for
 466       *                      description.
 467       */
 468  	public function blocks_init( $blocks = array() ) {
 469          parent::blocks_init( array() );
 470      }
 471  }


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