[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * BuddyPress Friends Streams Loader.
   4   *
   5   * The friends component is for users to create relationships with each other.
   6   *
   7   * @package BuddyPress
   8   * @subpackage FriendsComponent
   9   * @since 1.5.0
  10   */
  11  
  12  // Exit if accessed directly.
  13  defined( 'ABSPATH' ) || exit;
  14  
  15  /**
  16   * Defines the BuddyPress Friends Component.
  17   *
  18   * @since 1.5.0
  19   */
  20  class BP_Friends_Component extends BP_Component {
  21  
  22      /**
  23       * Start the friends component creation process.
  24       *
  25       * @since 1.5.0
  26       */
  27  	public function __construct() {
  28          parent::start(
  29              'friends',
  30              _x( 'Friend Connections', 'Friends screen page <title>', 'buddypress' ),
  31              buddypress()->plugin_dir,
  32              array(
  33                  'adminbar_myaccount_order' => 60,
  34              )
  35          );
  36      }
  37  
  38      /**
  39       * Include bp-friends files.
  40       *
  41       * @since 1.5.0
  42       *
  43       * @see BP_Component::includes() for description of parameters.
  44       *
  45       * @param array $includes See {@link BP_Component::includes()}.
  46       */
  47  	public function includes( $includes = array() ) {
  48          $includes = array(
  49              'cssjs',
  50              'cache',
  51              'filters',
  52              'template',
  53              'functions',
  54              'widgets',
  55              'blocks',
  56          );
  57  
  58          // Conditional includes.
  59          if ( bp_is_active( 'activity' ) ) {
  60              $includes[] = 'activity';
  61          }
  62          if ( bp_is_active( 'notifications' ) ) {
  63              $includes[] = 'notifications';
  64          }
  65  
  66          parent::includes( $includes );
  67      }
  68  
  69      /**
  70       * Late includes method.
  71       *
  72       * Only load up certain code when on specific pages.
  73       *
  74       * @since 3.0.0
  75       */
  76  	public function late_includes() {
  77          // Bail if PHPUnit is running.
  78          if ( defined( 'BP_TESTS_DIR' ) ) {
  79              return;
  80          }
  81  
  82          // Friends.
  83          if ( bp_is_user_friends() ) {
  84              // Authenticated actions.
  85              if ( is_user_logged_in() &&
  86                  in_array( bp_current_action(), array( 'add-friend', 'remove-friend' ), true )
  87              ) {
  88                  require $this->path . 'bp-friends/actions/' . bp_current_action() . '.php';
  89              }
  90  
  91              // User nav.
  92              require $this->path . 'bp-friends/screens/my-friends.php';
  93              if ( is_user_logged_in() && bp_is_user_friend_requests() ) {
  94                  require $this->path . 'bp-friends/screens/requests.php';
  95              }
  96          }
  97      }
  98  
  99      /**
 100       * Set up bp-friends global settings.
 101       *
 102       * The BP_FRIENDS_SLUG constant is deprecated, and only used here for
 103       * backwards compatibility.
 104       *
 105       * @since 1.5.0
 106       *
 107       * @global BuddyPress $bp The one true BuddyPress instance.
 108       *
 109       * @see BP_Component::setup_globals() for description of parameters.
 110       *
 111       * @param array $args See {@link BP_Component::setup_globals()}.
 112       */
 113  	public function setup_globals( $args = array() ) {
 114          $bp = buddypress();
 115  
 116          // Deprecated. Do not use.
 117          // Defined conditionally to support unit tests.
 118          if ( ! defined( 'BP_FRIENDS_DB_VERSION' ) ) {
 119              define( 'BP_FRIENDS_DB_VERSION', '1800' );
 120          }
 121  
 122          // Define a slug, if necessary.
 123          if ( ! defined( 'BP_FRIENDS_SLUG' ) ) {
 124              define( 'BP_FRIENDS_SLUG', $this->id );
 125          }
 126  
 127          // Global tables for the friends component.
 128          $global_tables = array(
 129              'table_name'      => $bp->table_prefix . 'bp_friends',
 130              'table_name_meta' => $bp->table_prefix . 'bp_friends_meta',
 131          );
 132  
 133          // All globals for the friends component.
 134          // Note that global_tables is included in this array.
 135          $args = array(
 136              'slug'                  => BP_FRIENDS_SLUG,
 137              'has_directory'         => false,
 138              'search_string'         => __( 'Search Friends...', 'buddypress' ),
 139              'notification_callback' => 'friends_format_notifications',
 140              'global_tables'         => $global_tables,
 141              'block_globals'         => array(
 142                  'bp/friends' => array(
 143                      'widget_classnames' => array( 'widget_bp_core_friends_widget', 'buddypress' ),
 144                  )
 145              ),
 146          );
 147  
 148          parent::setup_globals( $args );
 149      }
 150  
 151      /**
 152       * Set up component navigation.
 153       *
 154       * @since 1.5.0
 155       *
 156       * @see BP_Component::setup_nav() for a description of arguments.
 157       *
 158       * @param array $main_nav Optional. See BP_Component::setup_nav() for
 159       *                        description.
 160       * @param array $sub_nav  Optional. See BP_Component::setup_nav() for
 161       *                        description.
 162       */
 163  	public function setup_nav( $main_nav = array(), $sub_nav = array() ) {
 164  
 165          // Determine user to use.
 166          if ( bp_displayed_user_domain() ) {
 167              $user_domain = bp_displayed_user_domain();
 168          } elseif ( bp_loggedin_user_domain() ) {
 169              $user_domain = bp_loggedin_user_domain();
 170          } else {
 171              return;
 172          }
 173  
 174          $access       = bp_core_can_edit_settings();
 175          $slug         = bp_get_friends_slug();
 176          $friends_link = trailingslashit( $user_domain . $slug );
 177  
 178          // Add 'Friends' to the main navigation.
 179          $count = friends_get_total_friend_count();
 180          $class = ( 0 === $count ) ? 'no-count' : 'count';
 181  
 182          $main_nav_name = sprintf(
 183              /* translators: %s: Friend count for the current user */
 184              __( 'Friends %s', 'buddypress' ),
 185              sprintf(
 186                  '<span class="%s">%s</span>',
 187                  esc_attr( $class ),
 188                  esc_html( $count )
 189              )
 190          );
 191  
 192          $main_nav = array(
 193              'name'                => $main_nav_name,
 194              'slug'                => $slug,
 195              'position'            => 60,
 196              'screen_function'     => 'friends_screen_my_friends',
 197              'default_subnav_slug' => 'my-friends',
 198              'item_css_id'         => $this->id,
 199          );
 200  
 201          // Add the subnav items to the friends nav item.
 202          $sub_nav[] = array(
 203              'name'            => _x( 'Friendships', 'Friends screen sub nav', 'buddypress' ),
 204              'slug'            => 'my-friends',
 205              'parent_url'      => $friends_link,
 206              'parent_slug'     => $slug,
 207              'screen_function' => 'friends_screen_my_friends',
 208              'position'        => 10,
 209              'item_css_id'     => 'friends-my-friends',
 210          );
 211  
 212          $sub_nav[] = array(
 213              'name'            => _x( 'Requests', 'Friends screen sub nav', 'buddypress' ),
 214              'slug'            => 'requests',
 215              'parent_url'      => $friends_link,
 216              'parent_slug'     => $slug,
 217              'screen_function' => 'friends_screen_requests',
 218              'position'        => 20,
 219              'user_has_access' => $access,
 220          );
 221  
 222          parent::setup_nav( $main_nav, $sub_nav );
 223      }
 224  
 225      /**
 226       * Set up bp-friends integration with the WordPress admin bar.
 227       *
 228       * @since 1.5.0
 229       *
 230       * @see BP_Component::setup_admin_bar() for a description of arguments.
 231       *
 232       * @param array $wp_admin_nav See BP_Component::setup_admin_bar()
 233       *                            for description.
 234       */
 235  	public function setup_admin_bar( $wp_admin_nav = array() ) {
 236  
 237          // Menus for logged in user.
 238          if ( is_user_logged_in() ) {
 239  
 240              // Setup the logged in user variables.
 241              $friends_link = trailingslashit( bp_loggedin_user_domain() . bp_get_friends_slug() );
 242  
 243              // Pending friend requests.
 244              $count = count( friends_get_friendship_request_user_ids( bp_loggedin_user_id() ) );
 245              if ( ! empty( $count ) ) {
 246                  $title = sprintf(
 247                      /* translators: %s: Pending friend request count for the current user */
 248                      _x( 'Friends %s', 'My Account Friends menu', 'buddypress' ),
 249                      '<span class="count">' . bp_core_number_format( $count ) . '</span>'
 250                  );
 251                  $pending = sprintf(
 252                      /* translators: %s: Pending friend request count for the current user */
 253                      _x( 'Pending Requests %s', 'My Account Friends menu sub nav', 'buddypress' ),
 254                      '<span class="count">' . bp_core_number_format( $count ) . '</span>'
 255                  );
 256              } else {
 257                  $title   = _x( 'Friends', 'My Account Friends menu', 'buddypress' );
 258                  $pending = _x( 'No Pending Requests', 'My Account Friends menu sub nav', 'buddypress' );
 259              }
 260  
 261              // Add the "My Account" sub menus.
 262              $wp_admin_nav[] = array(
 263                  'parent' => buddypress()->my_account_menu_id,
 264                  'id'     => 'my-account-' . $this->id,
 265                  'title'  => $title,
 266                  'href'   => $friends_link,
 267              );
 268  
 269              // My Friends.
 270              $wp_admin_nav[] = array(
 271                  'parent'   => 'my-account-' . $this->id,
 272                  'id'       => 'my-account-' . $this->id . '-friendships',
 273                  'title'    => _x( 'Friendships', 'My Account Friends menu sub nav', 'buddypress' ),
 274                  'href'     => trailingslashit( $friends_link . 'my-friends' ),
 275                  'position' => 10,
 276              );
 277  
 278              // Requests.
 279              $wp_admin_nav[] = array(
 280                  'parent'   => 'my-account-' . $this->id,
 281                  'id'       => 'my-account-' . $this->id . '-requests',
 282                  'title'    => $pending,
 283                  'href'     => trailingslashit( $friends_link . 'requests' ),
 284                  'position' => 20,
 285              );
 286          }
 287  
 288          parent::setup_admin_bar( $wp_admin_nav );
 289      }
 290  
 291      /**
 292       * Set up the title for pages and <title>.
 293       *
 294       * @since 1.5.0
 295       *
 296       * @global BuddyPress $bp The one true BuddyPress instance.
 297       */
 298  	public function setup_title() {
 299  
 300          // Adjust title.
 301          if ( bp_is_friends_component() ) {
 302              $bp = buddypress();
 303  
 304              if ( bp_is_my_profile() ) {
 305                  $bp->bp_options_title = __( 'Friendships', 'buddypress' );
 306              } else {
 307                  $bp->bp_options_avatar = bp_core_fetch_avatar( array(
 308                      'item_id' => bp_displayed_user_id(),
 309                      'type'    => 'thumb',
 310                      'alt'     => sprintf(
 311                          /* translators: %s: member name */
 312                          __( 'Profile picture of %s', 'buddypress' ),
 313                          bp_get_displayed_user_fullname()
 314                      ),
 315                  ) );
 316                  $bp->bp_options_title = bp_get_displayed_user_fullname();
 317              }
 318          }
 319  
 320          parent::setup_title();
 321      }
 322  
 323      /**
 324       * Setup cache groups.
 325       *
 326       * @since 2.2.0
 327       */
 328  	public function setup_cache_groups() {
 329  
 330          // Global groups.
 331          wp_cache_add_global_groups( array(
 332              'bp_friends_requests',
 333              'bp_friends_friendships', // Individual friendship objects are cached here by ID.
 334              'bp_friends_friendships_for_user' // All friendship IDs for a single user.
 335          ) );
 336  
 337          parent::setup_cache_groups();
 338      }
 339  
 340      /**
 341       * Init the BP REST API.
 342       *
 343       * @since 6.0.0
 344       *
 345       * @param array $controllers Optional. See BP_Component::rest_api_init() for
 346       *                           description.
 347       */
 348  	public function rest_api_init( $controllers = array() ) {
 349          parent::rest_api_init( array( 'BP_REST_Friends_Endpoint' ) );
 350      }
 351  
 352      /**
 353       * Register the BP Friends Blocks.
 354       *
 355       * @since 9.0.0
 356       *
 357       * @param array $blocks Optional. See BP_Component::blocks_init() for
 358       *                      description.
 359       */
 360  	public function blocks_init( $blocks = array() ) {
 361          parent::blocks_init(
 362              array(
 363                  'bp/friends' => array(
 364                      'name'               => 'bp/friends',
 365                      'editor_script'      => 'bp-friends-block',
 366                      'editor_script_url'  => plugins_url( 'js/blocks/friends.js', dirname( __FILE__ ) ),
 367                      'editor_script_deps' => array(
 368                          'wp-blocks',
 369                          'wp-element',
 370                          'wp-components',
 371                          'wp-i18n',
 372                          'wp-block-editor',
 373                          'wp-server-side-render',
 374                          'bp-block-data',
 375                      ),
 376                      'style'              => 'bp-friends-block',
 377                      'style_url'          => plugins_url( 'css/blocks/friends.css', dirname( __FILE__ ) ),
 378                      'attributes'         => array(
 379                          'maxFriends'    => array(
 380                              'type'    => 'number',
 381                              'default' => 5,
 382                          ),
 383                          'friendDefault' => array(
 384                              'type'    => 'string',
 385                              'default' => 'active',
 386                          ),
 387                          'linkTitle'     => array(
 388                              'type'    => 'boolean',
 389                              'default' => false,
 390                          ),
 391                          'postId'        => array(
 392                              'type'    => 'number',
 393                              'default' => 0,
 394                          ),
 395                      ),
 396                      'render_callback'    => 'bp_friends_render_friends_block',
 397                  ),
 398              )
 399          );
 400      }
 401  }


Generated: Sun Dec 22 01:00:54 2024 Cross-referenced by PHPXref 0.7.1