[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * BuddyPress Messages Loader.
   4   *
   5   * A private messages component, for users to send messages to each other.
   6   *
   7   * @package BuddyPress
   8   * @subpackage MessagesLoader
   9   * @since 1.5.0
  10   */
  11  
  12  // Exit if accessed directly.
  13  defined( 'ABSPATH' ) || exit;
  14  
  15  /**
  16   * Implementation of BP_Component for the Messages component.
  17   *
  18   * @since 1.5.0
  19   */
  20  class BP_Messages_Component extends BP_Component {
  21  
  22      /**
  23       * If this is true, the Message autocomplete will return friends only, unless
  24       * this is set to false, in which any matching users will be returned.
  25       *
  26       * @since 1.5.0
  27       * @var bool
  28       */
  29      public $autocomplete_all;
  30  
  31      /**
  32       * Start the messages component creation process.
  33       *
  34       * @since 1.5.0
  35       */
  36  	public function __construct() {
  37          parent::start(
  38              'messages',
  39              __( 'Private Messages', 'buddypress' ),
  40              buddypress()->plugin_dir,
  41              array(
  42                  'adminbar_myaccount_order' => 50,
  43                  'features'                 => array( 'star' )
  44              )
  45          );
  46      }
  47  
  48      /**
  49       * Include files.
  50       *
  51       * @since 1.5.0
  52       *
  53       * @param array $includes See {BP_Component::includes()} for details.
  54       */
  55  	public function includes( $includes = array() ) {
  56  
  57          // Files to include.
  58          $includes = array(
  59              'cssjs',
  60              'cache',
  61              'filters',
  62              'template',
  63              'functions',
  64              'widgets',
  65          );
  66  
  67          // Conditional includes.
  68          if ( bp_is_active( 'notifications' ) ) {
  69              $includes[] = 'notifications';
  70          }
  71          if ( bp_is_active( $this->id, 'star' ) ) {
  72              $includes[] = 'star';
  73          }
  74          if ( is_admin() ) {
  75              $includes[] = 'admin';
  76          }
  77  
  78          parent::includes( $includes );
  79      }
  80  
  81      /**
  82       * Late includes method.
  83       *
  84       * Only load up certain code when on specific pages.
  85       *
  86       * @since 3.0.0
  87       */
  88  	public function late_includes() {
  89          // Bail if PHPUnit is running.
  90          if ( defined( 'BP_TESTS_DIR' ) ) {
  91              return;
  92          }
  93  
  94          if ( bp_is_messages_component() ) {
  95              // Authenticated actions.
  96              if ( is_user_logged_in() &&
  97                  in_array( bp_current_action(), array( 'compose', 'notices', 'view' ), true )
  98              ) {
  99                  require $this->path . 'bp-messages/actions/' . bp_current_action() . '.php';
 100              }
 101  
 102              // Authenticated action variables.
 103              if ( is_user_logged_in() && bp_action_variable( 0 ) &&
 104                  in_array( bp_action_variable( 0 ), array( 'delete', 'read', 'unread', 'bulk-manage', 'bulk-delete' ), true )
 105              ) {
 106                  require $this->path . 'bp-messages/actions/' . bp_action_variable( 0 ) . '.php';
 107              }
 108  
 109              // Authenticated actions - Star.
 110              if ( is_user_logged_in() && bp_is_active( $this->id, 'star' ) ) {
 111                  // Single action.
 112                  if ( in_array( bp_current_action(), array( 'star', 'unstar' ), true ) ) {
 113                      require $this->path . 'bp-messages/actions/star.php';
 114                  }
 115  
 116                  // Bulk-manage.
 117                  if ( bp_is_action_variable( 'bulk-manage' ) ) {
 118                      require $this->path . 'bp-messages/actions/bulk-manage-star.php';
 119                  }
 120              }
 121  
 122              // Screens - User profile integration.
 123              if ( bp_is_user() ) {
 124                  require $this->path . 'bp-messages/screens/inbox.php';
 125  
 126                  /*
 127                   * Nav items.
 128                   *
 129                   * 'view' is not a registered nav item, but we add a screen handler manually.
 130                   */
 131                  if ( bp_is_user_messages() && in_array( bp_current_action(), array( 'sentbox', 'compose', 'notices', 'view' ), true ) ) {
 132                      require $this->path . 'bp-messages/screens/' . bp_current_action() . '.php';
 133                  }
 134  
 135                  // Nav item - Starred.
 136                  if ( bp_is_active( $this->id, 'star' ) && bp_is_current_action( bp_get_messages_starred_slug() ) ) {
 137                      require $this->path . 'bp-messages/screens/starred.php';
 138                  }
 139              }
 140          }
 141      }
 142  
 143      /**
 144       * Set up globals for the Messages component.
 145       *
 146       * The BP_MESSAGES_SLUG constant is deprecated, and only used here for
 147       * backwards compatibility.
 148       *
 149       * @since 1.5.0
 150       *
 151       * @param array $args Not used.
 152       */
 153  	public function setup_globals( $args = array() ) {
 154          $bp = buddypress();
 155  
 156          // Define a slug, if necessary.
 157          if ( ! defined( 'BP_MESSAGES_SLUG' ) ) {
 158              define( 'BP_MESSAGES_SLUG', $this->id );
 159          }
 160  
 161          // Global tables for messaging component.
 162          $global_tables = array(
 163              'table_name_notices'    => $bp->table_prefix . 'bp_messages_notices',
 164              'table_name_messages'   => $bp->table_prefix . 'bp_messages_messages',
 165              'table_name_recipients' => $bp->table_prefix . 'bp_messages_recipients',
 166              'table_name_meta'       => $bp->table_prefix . 'bp_messages_meta',
 167          );
 168  
 169          // Metadata tables for messaging component.
 170          $meta_tables = array(
 171              'message' => $bp->table_prefix . 'bp_messages_meta',
 172          );
 173  
 174          $this->autocomplete_all = defined( 'BP_MESSAGES_AUTOCOMPLETE_ALL' );
 175  
 176          // All globals for messaging component.
 177          // Note that global_tables is included in this array.
 178          parent::setup_globals( array(
 179              'slug'                  => BP_MESSAGES_SLUG,
 180              'has_directory'         => false,
 181              'notification_callback' => 'messages_format_notifications',
 182              'search_string'         => __( 'Search Messages...', 'buddypress' ),
 183              'global_tables'         => $global_tables,
 184              'meta_tables'           => $meta_tables
 185          ) );
 186      }
 187  
 188      /**
 189       * Set up navigation for user pages.
 190       *
 191       * @param array $main_nav See {BP_Component::setup_nav()} for details.
 192       * @param array $sub_nav  See {BP_Component::setup_nav()} for details.
 193       */
 194  	public function setup_nav( $main_nav = array(), $sub_nav = array() ) {
 195  
 196          // Determine user to use.
 197          if ( bp_displayed_user_domain() ) {
 198              $user_domain = bp_displayed_user_domain();
 199          } elseif ( bp_loggedin_user_domain() ) {
 200              $user_domain = bp_loggedin_user_domain();
 201          } else {
 202              return;
 203          }
 204  
 205          $access        = bp_core_can_edit_settings();
 206          $slug          = bp_get_messages_slug();
 207          $messages_link = trailingslashit( $user_domain . $slug );
 208  
 209          // Only grab count if we're on a user page and current user has access.
 210          if ( bp_is_user() && bp_user_has_access() ) {
 211              $count    = bp_get_total_unread_messages_count( bp_displayed_user_id() );
 212              $class    = ( 0 === $count ) ? 'no-count' : 'count';
 213              $nav_name = sprintf(
 214                  /* translators: %s: Unread message count for the current user */
 215                  __( 'Messages %s', 'buddypress' ),
 216                  sprintf(
 217                      '<span class="%s">%s</span>',
 218                      esc_attr( $class ),
 219                      bp_core_number_format( $count )
 220                  )
 221              );
 222          } else {
 223              $nav_name = __( 'Messages', 'buddypress' );
 224          }
 225  
 226          // Add 'Messages' to the main navigation.
 227          $main_nav = array(
 228              'name'                    => $nav_name,
 229              'slug'                    => $slug,
 230              'position'                => 50,
 231              'show_for_displayed_user' => $access,
 232              'screen_function'         => 'messages_screen_inbox',
 233              'default_subnav_slug'     => 'inbox',
 234              'item_css_id'             => $this->id
 235          );
 236  
 237          // Add the subnav items to the profile.
 238          $sub_nav[] = array(
 239              'name'            => __( 'Inbox', 'buddypress' ),
 240              'slug'            => 'inbox',
 241              'parent_url'      => $messages_link,
 242              'parent_slug'     => $slug,
 243              'screen_function' => 'messages_screen_inbox',
 244              'position'        => 10,
 245              'user_has_access' => $access
 246          );
 247  
 248          if ( bp_is_active( $this->id, 'star' ) ) {
 249              $sub_nav[] = array(
 250                  'name'            => __( 'Starred', 'buddypress' ),
 251                  'slug'            => bp_get_messages_starred_slug(),
 252                  'parent_url'      => $messages_link,
 253                  'parent_slug'     => $slug,
 254                  'screen_function' => 'bp_messages_star_screen',
 255                  'position'        => 11,
 256                  'user_has_access' => $access
 257              );
 258          }
 259  
 260          $sub_nav[] = array(
 261              'name'            => __( 'Sent', 'buddypress' ),
 262              'slug'            => 'sentbox',
 263              'parent_url'      => $messages_link,
 264              'parent_slug'     => $slug,
 265              'screen_function' => 'messages_screen_sentbox',
 266              'position'        => 20,
 267              'user_has_access' => $access
 268          );
 269  
 270          // Show certain screens only if the current user is the displayed user.
 271          if ( bp_is_my_profile() ) {
 272  
 273              // Show "Compose" on the logged-in user's profile only.
 274              $sub_nav[] = array(
 275                  'name'            => __( 'Compose', 'buddypress' ),
 276                  'slug'            => 'compose',
 277                  'parent_url'      => $messages_link,
 278                  'parent_slug'     => $slug,
 279                  'screen_function' => 'messages_screen_compose',
 280                  'position'        => 30,
 281                  'user_has_access' => $access
 282              );
 283  
 284              /*
 285               * Show "Notices" on the logged-in user's profile only
 286               * and then only if the user can create notices.
 287               */
 288              if ( bp_current_user_can( 'bp_moderate' ) ) {
 289                  $sub_nav[] = array(
 290                      'name'            => __( 'Notices', 'buddypress' ),
 291                      'slug'            => 'notices',
 292                      'parent_url'      => $messages_link,
 293                      'parent_slug'     => $slug,
 294                      'screen_function' => 'messages_screen_notices',
 295                      'position'        => 90,
 296                      'user_has_access' => true
 297                  );
 298              }
 299          }
 300  
 301          parent::setup_nav( $main_nav, $sub_nav );
 302      }
 303  
 304      /**
 305       * Set up the Toolbar.
 306       *
 307       * @param array $wp_admin_nav See {BP_Component::setup_admin_bar()} for details.
 308       */
 309  	public function setup_admin_bar( $wp_admin_nav = array() ) {
 310  
 311          // Menus for logged in user.
 312          if ( is_user_logged_in() ) {
 313  
 314              // Setup the logged in user variables.
 315              $messages_link = trailingslashit( bp_loggedin_user_domain() . bp_get_messages_slug() );
 316  
 317              // Unread message count.
 318              $count = messages_get_unread_count( bp_loggedin_user_id() );
 319              if ( !empty( $count ) ) {
 320                  $title = sprintf(
 321                      /* translators: %s: Unread message count for the current user */
 322                      __( 'Messages %s', 'buddypress' ),
 323                      '<span class="count">' . bp_core_number_format( $count ) . '</span>'
 324                  );
 325                  $inbox = sprintf(
 326                      /* translators: %s: Unread message count for the current user */
 327                      __( 'Inbox %s', 'buddypress' ),
 328                      '<span class="count">' . bp_core_number_format( $count ) . '</span>'
 329                  );
 330              } else {
 331                  $title = __( 'Messages', 'buddypress' );
 332                  $inbox = __( 'Inbox',    'buddypress' );
 333              }
 334  
 335              // Add main Messages menu.
 336              $wp_admin_nav[] = array(
 337                  'parent' => buddypress()->my_account_menu_id,
 338                  'id'     => 'my-account-' . $this->id,
 339                  'title'  => $title,
 340                  'href'   => $messages_link
 341              );
 342  
 343              // Inbox.
 344              $wp_admin_nav[] = array(
 345                  'parent'   => 'my-account-' . $this->id,
 346                  'id'       => 'my-account-' . $this->id . '-inbox',
 347                  'title'    => $inbox,
 348                  'href'     => $messages_link,
 349                  'position' => 10
 350              );
 351  
 352              // Starred.
 353              if ( bp_is_active( $this->id, 'star' ) ) {
 354                  $wp_admin_nav[] = array(
 355                      'parent'   => 'my-account-' . $this->id,
 356                      'id'       => 'my-account-' . $this->id . '-starred',
 357                      'title'    => __( 'Starred', 'buddypress' ),
 358                      'href'     => trailingslashit( $messages_link . bp_get_messages_starred_slug() ),
 359                      'position' => 11
 360                  );
 361              }
 362  
 363              // Sent Messages.
 364              $wp_admin_nav[] = array(
 365                  'parent'   => 'my-account-' . $this->id,
 366                  'id'       => 'my-account-' . $this->id . '-sentbox',
 367                  'title'    => __( 'Sent', 'buddypress' ),
 368                  'href'     => trailingslashit( $messages_link . 'sentbox' ),
 369                  'position' => 20
 370              );
 371  
 372              // Compose Message.
 373              $wp_admin_nav[] = array(
 374                  'parent'   => 'my-account-' . $this->id,
 375                  'id'       => 'my-account-' . $this->id . '-compose',
 376                  'title'    => __( 'Compose', 'buddypress' ),
 377                  'href'     => trailingslashit( $messages_link . 'compose' ),
 378                  'position' => 30
 379              );
 380  
 381              // Site Wide Notices.
 382              if ( bp_current_user_can( 'bp_moderate' ) ) {
 383                  $wp_admin_nav[] = array(
 384                      'parent'   => 'my-account-' . $this->id,
 385                      'id'       => 'my-account-' . $this->id . '-notices',
 386                      'title'    => __( 'Site Notices', 'buddypress' ),
 387                      'href'     => trailingslashit( $messages_link . 'notices' ),
 388                      'position' => 90
 389                  );
 390              }
 391          }
 392  
 393          parent::setup_admin_bar( $wp_admin_nav );
 394      }
 395  
 396      /**
 397       * Set up the title for pages and <title>.
 398       */
 399  	public function setup_title() {
 400  
 401          if ( bp_is_messages_component() ) {
 402              $bp = buddypress();
 403  
 404              if ( bp_is_my_profile() ) {
 405                  $bp->bp_options_title = __( 'My Messages', 'buddypress' );
 406              } else {
 407                  $bp->bp_options_avatar = bp_core_fetch_avatar( array(
 408                      'item_id' => bp_displayed_user_id(),
 409                      'type'    => 'thumb',
 410                      'alt'     => sprintf( __( 'Profile picture of %s', 'buddypress' ), bp_get_displayed_user_fullname() )
 411                  ) );
 412                  $bp->bp_options_title = bp_get_displayed_user_fullname();
 413              }
 414          }
 415  
 416          parent::setup_title();
 417      }
 418  
 419      /**
 420       * Setup cache groups
 421       *
 422       * @since 2.2.0
 423       */
 424  	public function setup_cache_groups() {
 425  
 426          // Global groups.
 427          wp_cache_add_global_groups( array(
 428              'bp_messages',
 429              'bp_messages_threads',
 430              'bp_messages_unread_count',
 431              'message_meta'
 432          ) );
 433  
 434          parent::setup_cache_groups();
 435      }
 436  
 437      /**
 438       * Init the BP REST API.
 439       *
 440       * @since 5.0.0
 441       *
 442       * @param array $controllers Optional. See BP_Component::rest_api_init() for
 443       *                           description.
 444       */
 445  	public function rest_api_init( $controllers = array() ) {
 446          parent::rest_api_init( array( 'BP_REST_Messages_Endpoint' ) );
 447      }
 448  }


Generated: Wed Nov 20 01:01:38 2019 Cross-referenced by PHPXref 0.7.1