[ 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 MessagesClasses
   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              'blocks',
  66          );
  67  
  68          // Conditional includes.
  69          if ( bp_is_active( 'notifications' ) ) {
  70              $includes[] = 'notifications';
  71          }
  72          if ( bp_is_active( $this->id, 'star' ) ) {
  73              $includes[] = 'star';
  74          }
  75          if ( is_admin() ) {
  76              $includes[] = 'admin';
  77          }
  78  
  79          parent::includes( $includes );
  80      }
  81  
  82      /**
  83       * Late includes method.
  84       *
  85       * Only load up certain code when on specific pages.
  86       *
  87       * @since 3.0.0
  88       */
  89  	public function late_includes() {
  90          // Bail if PHPUnit is running.
  91          if ( defined( 'BP_TESTS_DIR' ) ) {
  92              return;
  93          }
  94  
  95          if ( bp_is_messages_component() ) {
  96              // Authenticated actions.
  97              if ( is_user_logged_in() &&
  98                  in_array( bp_current_action(), array( 'compose', 'notices', 'view' ), true )
  99              ) {
 100                  require $this->path . 'bp-messages/actions/' . bp_current_action() . '.php';
 101              }
 102  
 103              // Authenticated action variables.
 104              if ( is_user_logged_in() && bp_action_variable( 0 ) &&
 105                  in_array( bp_action_variable( 0 ), array( 'delete', 'read', 'unread', 'bulk-manage', 'bulk-delete', 'exit' ), true )
 106              ) {
 107                  require $this->path . 'bp-messages/actions/' . bp_action_variable( 0 ) . '.php';
 108              }
 109  
 110              // Authenticated actions - Star.
 111              if ( is_user_logged_in() && bp_is_active( $this->id, 'star' ) ) {
 112                  // Single action.
 113                  if ( in_array( bp_current_action(), array( 'star', 'unstar' ), true ) ) {
 114                      require $this->path . 'bp-messages/actions/star.php';
 115                  }
 116  
 117                  // Bulk-manage.
 118                  if ( bp_is_action_variable( 'bulk-manage' ) ) {
 119                      require $this->path . 'bp-messages/actions/bulk-manage-star.php';
 120                  }
 121              }
 122  
 123              // Screens - User profile integration.
 124              if ( bp_is_user() ) {
 125                  require $this->path . 'bp-messages/screens/inbox.php';
 126  
 127                  /*
 128                   * Nav items.
 129                   *
 130                   * 'view' is not a registered nav item, but we add a screen handler manually.
 131                   */
 132                  if ( bp_is_user_messages() && in_array( bp_current_action(), array( 'sentbox', 'compose', 'notices', 'view' ), true ) ) {
 133                      require $this->path . 'bp-messages/screens/' . bp_current_action() . '.php';
 134                  }
 135  
 136                  // Nav item - Starred.
 137                  if ( bp_is_active( $this->id, 'star' ) && bp_is_current_action( bp_get_messages_starred_slug() ) ) {
 138                      require $this->path . 'bp-messages/screens/starred.php';
 139                  }
 140              }
 141          }
 142      }
 143  
 144      /**
 145       * Set up globals for the Messages component.
 146       *
 147       * The BP_MESSAGES_SLUG constant is deprecated, and only used here for
 148       * backwards compatibility.
 149       *
 150       * @since 1.5.0
 151       *
 152       * @param array $args Not used.
 153       */
 154  	public function setup_globals( $args = array() ) {
 155          $bp = buddypress();
 156  
 157          // Define a slug, if necessary.
 158          if ( ! defined( 'BP_MESSAGES_SLUG' ) ) {
 159              define( 'BP_MESSAGES_SLUG', $this->id );
 160          }
 161  
 162          // Global tables for messaging component.
 163          $global_tables = array(
 164              'table_name_notices'    => $bp->table_prefix . 'bp_messages_notices',
 165              'table_name_messages'   => $bp->table_prefix . 'bp_messages_messages',
 166              'table_name_recipients' => $bp->table_prefix . 'bp_messages_recipients',
 167              'table_name_meta'       => $bp->table_prefix . 'bp_messages_meta',
 168          );
 169  
 170          // Metadata tables for messaging component.
 171          $meta_tables = array(
 172              'message' => $bp->table_prefix . 'bp_messages_meta',
 173          );
 174  
 175          $this->autocomplete_all = defined( 'BP_MESSAGES_AUTOCOMPLETE_ALL' );
 176  
 177          // All globals for messaging component.
 178          // Note that global_tables is included in this array.
 179          parent::setup_globals( array(
 180              'slug'                  => BP_MESSAGES_SLUG,
 181              'has_directory'         => false,
 182              'notification_callback' => 'messages_format_notifications',
 183              'search_string'         => __( 'Search Messages...', 'buddypress' ),
 184              'global_tables'         => $global_tables,
 185              'meta_tables'           => $meta_tables
 186          ) );
 187      }
 188  
 189      /**
 190       * Set up navigation for user pages.
 191       *
 192       * @param array $main_nav See {BP_Component::setup_nav()} for details.
 193       * @param array $sub_nav  See {BP_Component::setup_nav()} for details.
 194       */
 195  	public function setup_nav( $main_nav = array(), $sub_nav = array() ) {
 196  
 197          // Determine user to use.
 198          if ( bp_displayed_user_domain() ) {
 199              $user_domain = bp_displayed_user_domain();
 200          } elseif ( bp_loggedin_user_domain() ) {
 201              $user_domain = bp_loggedin_user_domain();
 202          } else {
 203              return;
 204          }
 205  
 206          $access        = bp_core_can_edit_settings();
 207          $slug          = bp_get_messages_slug();
 208          $messages_link = trailingslashit( $user_domain . $slug );
 209  
 210          // Only grab count if we're on a user page and current user has access.
 211          if ( bp_is_user() && bp_user_has_access() ) {
 212              $count    = bp_get_total_unread_messages_count( bp_displayed_user_id() );
 213              $class    = ( 0 === $count ) ? 'no-count' : 'count';
 214              $nav_name = sprintf(
 215                  /* translators: %s: Unread message count for the current user */
 216                  __( 'Messages %s', 'buddypress' ),
 217                  sprintf(
 218                      '<span class="%s">%s</span>',
 219                      esc_attr( $class ),
 220                      bp_core_number_format( $count )
 221                  )
 222              );
 223          } else {
 224              $nav_name = __( 'Messages', 'buddypress' );
 225          }
 226  
 227          // Add 'Messages' to the main navigation.
 228          $main_nav = array(
 229              'name'                    => $nav_name,
 230              'slug'                    => $slug,
 231              'position'                => 50,
 232              'show_for_displayed_user' => $access,
 233              'screen_function'         => 'messages_screen_inbox',
 234              'default_subnav_slug'     => 'inbox',
 235              'item_css_id'             => $this->id
 236          );
 237  
 238          // Add the subnav items to the profile.
 239          $sub_nav[] = array(
 240              'name'            => __( 'Inbox', 'buddypress' ),
 241              'slug'            => 'inbox',
 242              'parent_url'      => $messages_link,
 243              'parent_slug'     => $slug,
 244              'screen_function' => 'messages_screen_inbox',
 245              'position'        => 10,
 246              'user_has_access' => $access
 247          );
 248  
 249          if ( bp_is_active( $this->id, 'star' ) ) {
 250              $sub_nav[] = array(
 251                  'name'            => __( 'Starred', 'buddypress' ),
 252                  'slug'            => bp_get_messages_starred_slug(),
 253                  'parent_url'      => $messages_link,
 254                  'parent_slug'     => $slug,
 255                  'screen_function' => 'bp_messages_star_screen',
 256                  'position'        => 11,
 257                  'user_has_access' => $access
 258              );
 259          }
 260  
 261          $sub_nav[] = array(
 262              'name'            => __( 'Sent', 'buddypress' ),
 263              'slug'            => 'sentbox',
 264              'parent_url'      => $messages_link,
 265              'parent_slug'     => $slug,
 266              'screen_function' => 'messages_screen_sentbox',
 267              'position'        => 20,
 268              'user_has_access' => $access
 269          );
 270  
 271          // Show "Compose" on the logged-in user's profile only.
 272          $sub_nav[] = array(
 273              'name'            => __( 'Compose', 'buddypress' ),
 274              'slug'            => 'compose',
 275              'parent_url'      => $messages_link,
 276              'parent_slug'     => $slug,
 277              'screen_function' => 'messages_screen_compose',
 278              'position'        => 30,
 279              'user_has_access' => bp_is_my_profile(),
 280          );
 281  
 282          // Show "Notices" to community admins only.
 283          $sub_nav[] = array(
 284              'name'            => __( 'Notices', 'buddypress' ),
 285              'slug'            => 'notices',
 286              'parent_url'      => $messages_link,
 287              'parent_slug'     => $slug,
 288              'screen_function' => 'messages_screen_notices',
 289              'position'        => 90,
 290              'user_has_access' => bp_current_user_can( 'bp_moderate' )
 291          );
 292  
 293          parent::setup_nav( $main_nav, $sub_nav );
 294      }
 295  
 296      /**
 297       * Set up the Toolbar.
 298       *
 299       * @param array $wp_admin_nav See {BP_Component::setup_admin_bar()} for details.
 300       */
 301  	public function setup_admin_bar( $wp_admin_nav = array() ) {
 302  
 303          // Menus for logged in user.
 304          if ( is_user_logged_in() ) {
 305  
 306              // Setup the logged in user variables.
 307              $messages_link = trailingslashit( bp_loggedin_user_domain() . bp_get_messages_slug() );
 308  
 309              // Unread message count.
 310              $count = messages_get_unread_count( bp_loggedin_user_id() );
 311              if ( !empty( $count ) ) {
 312                  $title = sprintf(
 313                      /* translators: %s: Unread message count for the current user */
 314                      __( 'Messages %s', 'buddypress' ),
 315                      '<span class="count">' . bp_core_number_format( $count ) . '</span>'
 316                  );
 317                  $inbox = sprintf(
 318                      /* translators: %s: Unread message count for the current user */
 319                      __( 'Inbox %s', 'buddypress' ),
 320                      '<span class="count">' . bp_core_number_format( $count ) . '</span>'
 321                  );
 322              } else {
 323                  $title = __( 'Messages', 'buddypress' );
 324                  $inbox = __( 'Inbox',    'buddypress' );
 325              }
 326  
 327              // Add main Messages menu.
 328              $wp_admin_nav[] = array(
 329                  'parent' => buddypress()->my_account_menu_id,
 330                  'id'     => 'my-account-' . $this->id,
 331                  'title'  => $title,
 332                  'href'   => $messages_link
 333              );
 334  
 335              // Inbox.
 336              $wp_admin_nav[] = array(
 337                  'parent'   => 'my-account-' . $this->id,
 338                  'id'       => 'my-account-' . $this->id . '-inbox',
 339                  'title'    => $inbox,
 340                  'href'     => trailingslashit( $messages_link . 'inbox' ),
 341                  'position' => 10
 342              );
 343  
 344              // Starred.
 345              if ( bp_is_active( $this->id, 'star' ) ) {
 346                  $wp_admin_nav[] = array(
 347                      'parent'   => 'my-account-' . $this->id,
 348                      'id'       => 'my-account-' . $this->id . '-starred',
 349                      'title'    => __( 'Starred', 'buddypress' ),
 350                      'href'     => trailingslashit( $messages_link . bp_get_messages_starred_slug() ),
 351                      'position' => 11
 352                  );
 353              }
 354  
 355              // Sent Messages.
 356              $wp_admin_nav[] = array(
 357                  'parent'   => 'my-account-' . $this->id,
 358                  'id'       => 'my-account-' . $this->id . '-sentbox',
 359                  'title'    => __( 'Sent', 'buddypress' ),
 360                  'href'     => trailingslashit( $messages_link . 'sentbox' ),
 361                  'position' => 20
 362              );
 363  
 364              // Compose Message.
 365              $wp_admin_nav[] = array(
 366                  'parent'   => 'my-account-' . $this->id,
 367                  'id'       => 'my-account-' . $this->id . '-compose',
 368                  'title'    => __( 'Compose', 'buddypress' ),
 369                  'href'     => trailingslashit( $messages_link . 'compose' ),
 370                  'position' => 30
 371              );
 372  
 373              // Site Wide Notices.
 374              if ( bp_current_user_can( 'bp_moderate' ) ) {
 375                  $wp_admin_nav[] = array(
 376                      'parent'   => 'my-account-' . $this->id,
 377                      'id'       => 'my-account-' . $this->id . '-notices',
 378                      'title'    => __( 'Site Notices', 'buddypress' ),
 379                      'href'     => trailingslashit( $messages_link . 'notices' ),
 380                      'position' => 90
 381                  );
 382              }
 383          }
 384  
 385          parent::setup_admin_bar( $wp_admin_nav );
 386      }
 387  
 388      /**
 389       * Set up the title for pages and <title>.
 390       */
 391  	public function setup_title() {
 392  
 393          if ( bp_is_messages_component() ) {
 394              $bp = buddypress();
 395  
 396              if ( bp_is_my_profile() ) {
 397                  $bp->bp_options_title = __( 'My Messages', 'buddypress' );
 398              } else {
 399                  $bp->bp_options_avatar = bp_core_fetch_avatar( array(
 400                      'item_id' => bp_displayed_user_id(),
 401                      'type'    => 'thumb',
 402                      /* translators: %s: member name */
 403                      'alt'     => sprintf( __( 'Profile picture of %s', 'buddypress' ), bp_get_displayed_user_fullname() )
 404                  ) );
 405                  $bp->bp_options_title = bp_get_displayed_user_fullname();
 406              }
 407          }
 408  
 409          parent::setup_title();
 410      }
 411  
 412      /**
 413       * Setup cache groups
 414       *
 415       * @since 2.2.0
 416       */
 417  	public function setup_cache_groups() {
 418  
 419          // Global groups.
 420          wp_cache_add_global_groups( array(
 421              'bp_messages',
 422              'bp_messages_threads',
 423              'bp_messages_unread_count',
 424              'message_meta'
 425          ) );
 426  
 427          parent::setup_cache_groups();
 428      }
 429  
 430      /**
 431       * Init the BP REST API.
 432       *
 433       * @since 5.0.0
 434       *
 435       * @param array $controllers Optional. See BP_Component::rest_api_init() for
 436       *                           description.
 437       */
 438  	public function rest_api_init( $controllers = array() ) {
 439          parent::rest_api_init( array( 'BP_REST_Messages_Endpoint' ) );
 440      }
 441  
 442      /**
 443       * Register the BP Messages Blocks.
 444       *
 445       * @since 9.0.0
 446       *
 447       * @param array $blocks Optional. See BP_Component::blocks_init() for
 448       *                      description.
 449       */
 450  	public function blocks_init( $blocks = array() ) {
 451          parent::blocks_init(
 452              array(
 453                  'bp/sitewide-notices' => array(
 454                      'name'               => 'bp/sitewide-notices',
 455                      'editor_script'      => 'bp-sitewide-notices-block',
 456                      'editor_script_url'  => plugins_url( 'js/blocks/sitewide-notices.js', dirname( __FILE__ ) ),
 457                      'editor_script_deps' => array(
 458                          'wp-blocks',
 459                          'wp-element',
 460                          'wp-components',
 461                          'wp-i18n',
 462                          'wp-block-editor',
 463                          'wp-server-side-render',
 464                          'bp-block-data',
 465                      ),
 466                      'style'              => 'bp-sitewide-notices-block',
 467                      'style_url'          => plugins_url( 'css/blocks/sitewide-notices.css', dirname( __FILE__ ) ),
 468                      'attributes'         => array(
 469                          'title' => array(
 470                              'type'    => 'string',
 471                              'default' => '',
 472                          ),
 473                      ),
 474                      'render_callback'    => 'bp_messages_render_sitewide_notices_block',
 475                  ),
 476              )
 477          );
 478      }
 479  }


Generated: Sun May 29 01:00:55 2022 Cross-referenced by PHPXref 0.7.1