[ 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' ), 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 certain screens only if the current user is the displayed user.
 272          if ( bp_is_my_profile() ) {
 273  
 274              // Show "Compose" on the logged-in user's profile only.
 275              $sub_nav[] = array(
 276                  'name'            => __( 'Compose', 'buddypress' ),
 277                  'slug'            => 'compose',
 278                  'parent_url'      => $messages_link,
 279                  'parent_slug'     => $slug,
 280                  'screen_function' => 'messages_screen_compose',
 281                  'position'        => 30,
 282                  'user_has_access' => $access
 283              );
 284  
 285              /*
 286               * Show "Notices" on the logged-in user's profile only
 287               * and then only if the user can create notices.
 288               */
 289              if ( bp_current_user_can( 'bp_moderate' ) ) {
 290                  $sub_nav[] = array(
 291                      'name'            => __( 'Notices', 'buddypress' ),
 292                      'slug'            => 'notices',
 293                      'parent_url'      => $messages_link,
 294                      'parent_slug'     => $slug,
 295                      'screen_function' => 'messages_screen_notices',
 296                      'position'        => 90,
 297                      'user_has_access' => true
 298                  );
 299              }
 300          }
 301  
 302          parent::setup_nav( $main_nav, $sub_nav );
 303      }
 304  
 305      /**
 306       * Set up the Toolbar.
 307       *
 308       * @param array $wp_admin_nav See {BP_Component::setup_admin_bar()} for details.
 309       */
 310  	public function setup_admin_bar( $wp_admin_nav = array() ) {
 311  
 312          // Menus for logged in user.
 313          if ( is_user_logged_in() ) {
 314  
 315              // Setup the logged in user variables.
 316              $messages_link = trailingslashit( bp_loggedin_user_domain() . bp_get_messages_slug() );
 317  
 318              // Unread message count.
 319              $count = messages_get_unread_count( bp_loggedin_user_id() );
 320              if ( !empty( $count ) ) {
 321                  $title = sprintf(
 322                      /* translators: %s: Unread message count for the current user */
 323                      __( 'Messages %s', 'buddypress' ),
 324                      '<span class="count">' . bp_core_number_format( $count ) . '</span>'
 325                  );
 326                  $inbox = sprintf(
 327                      /* translators: %s: Unread message count for the current user */
 328                      __( 'Inbox %s', 'buddypress' ),
 329                      '<span class="count">' . bp_core_number_format( $count ) . '</span>'
 330                  );
 331              } else {
 332                  $title = __( 'Messages', 'buddypress' );
 333                  $inbox = __( 'Inbox',    'buddypress' );
 334              }
 335  
 336              // Add main Messages menu.
 337              $wp_admin_nav[] = array(
 338                  'parent' => buddypress()->my_account_menu_id,
 339                  'id'     => 'my-account-' . $this->id,
 340                  'title'  => $title,
 341                  'href'   => $messages_link
 342              );
 343  
 344              // Inbox.
 345              $wp_admin_nav[] = array(
 346                  'parent'   => 'my-account-' . $this->id,
 347                  'id'       => 'my-account-' . $this->id . '-inbox',
 348                  'title'    => $inbox,
 349                  'href'     => trailingslashit( $messages_link . 'inbox' ),
 350                  'position' => 10
 351              );
 352  
 353              // Starred.
 354              if ( bp_is_active( $this->id, 'star' ) ) {
 355                  $wp_admin_nav[] = array(
 356                      'parent'   => 'my-account-' . $this->id,
 357                      'id'       => 'my-account-' . $this->id . '-starred',
 358                      'title'    => __( 'Starred', 'buddypress' ),
 359                      'href'     => trailingslashit( $messages_link . bp_get_messages_starred_slug() ),
 360                      'position' => 11
 361                  );
 362              }
 363  
 364              // Sent Messages.
 365              $wp_admin_nav[] = array(
 366                  'parent'   => 'my-account-' . $this->id,
 367                  'id'       => 'my-account-' . $this->id . '-sentbox',
 368                  'title'    => __( 'Sent', 'buddypress' ),
 369                  'href'     => trailingslashit( $messages_link . 'sentbox' ),
 370                  'position' => 20
 371              );
 372  
 373              // Compose Message.
 374              $wp_admin_nav[] = array(
 375                  'parent'   => 'my-account-' . $this->id,
 376                  'id'       => 'my-account-' . $this->id . '-compose',
 377                  'title'    => __( 'Compose', 'buddypress' ),
 378                  'href'     => trailingslashit( $messages_link . 'compose' ),
 379                  'position' => 30
 380              );
 381  
 382              // Site Wide Notices.
 383              if ( bp_current_user_can( 'bp_moderate' ) ) {
 384                  $wp_admin_nav[] = array(
 385                      'parent'   => 'my-account-' . $this->id,
 386                      'id'       => 'my-account-' . $this->id . '-notices',
 387                      'title'    => __( 'Site Notices', 'buddypress' ),
 388                      'href'     => trailingslashit( $messages_link . 'notices' ),
 389                      'position' => 90
 390                  );
 391              }
 392          }
 393  
 394          parent::setup_admin_bar( $wp_admin_nav );
 395      }
 396  
 397      /**
 398       * Set up the title for pages and <title>.
 399       */
 400  	public function setup_title() {
 401  
 402          if ( bp_is_messages_component() ) {
 403              $bp = buddypress();
 404  
 405              if ( bp_is_my_profile() ) {
 406                  $bp->bp_options_title = __( 'My Messages', 'buddypress' );
 407              } else {
 408                  $bp->bp_options_avatar = bp_core_fetch_avatar( array(
 409                      'item_id' => bp_displayed_user_id(),
 410                      'type'    => 'thumb',
 411                      'alt'     => sprintf( __( 'Profile picture of %s', 'buddypress' ), bp_get_displayed_user_fullname() )
 412                  ) );
 413                  $bp->bp_options_title = bp_get_displayed_user_fullname();
 414              }
 415          }
 416  
 417          parent::setup_title();
 418      }
 419  
 420      /**
 421       * Setup cache groups
 422       *
 423       * @since 2.2.0
 424       */
 425  	public function setup_cache_groups() {
 426  
 427          // Global groups.
 428          wp_cache_add_global_groups( array(
 429              'bp_messages',
 430              'bp_messages_threads',
 431              'bp_messages_unread_count',
 432              'message_meta'
 433          ) );
 434  
 435          parent::setup_cache_groups();
 436      }
 437  
 438      /**
 439       * Init the BP REST API.
 440       *
 441       * @since 5.0.0
 442       *
 443       * @param array $controllers Optional. See BP_Component::rest_api_init() for
 444       *                           description.
 445       */
 446  	public function rest_api_init( $controllers = array() ) {
 447          parent::rest_api_init( array( 'BP_REST_Messages_Endpoint' ) );
 448      }
 449  
 450      /**
 451       * Register the BP Messages Blocks.
 452       *
 453       * @since 9.0.0
 454       *
 455       * @param array $blocks Optional. See BP_Component::blocks_init() for
 456       *                      description.
 457       */
 458  	public function blocks_init( $blocks = array() ) {
 459          parent::blocks_init(
 460              array(
 461                  'bp/sitewide-notices' => array(
 462                      'name'               => 'bp/sitewide-notices',
 463                      'editor_script'      => 'bp-sitewide-notices-block',
 464                      'editor_script_url'  => plugins_url( 'js/blocks/sitewide-notices.js', dirname( __FILE__ ) ),
 465                      'editor_script_deps' => array(
 466                          'wp-blocks',
 467                          'wp-element',
 468                          'wp-components',
 469                          'wp-i18n',
 470                          'wp-block-editor',
 471                          'bp-block-data',
 472                          'bp-block-components',
 473                      ),
 474                      'style'              => 'bp-sitewide-notices-block',
 475                      'style_url'          => plugins_url( 'css/blocks/sitewide-notices.css', dirname( __FILE__ ) ),
 476                      'attributes'         => array(
 477                          'title' => array(
 478                              'type'    => 'string',
 479                              'default' => '',
 480                          ),
 481                      ),
 482                      'render_callback'    => 'bp_messages_render_sitewide_notices_block',
 483                  ),
 484              )
 485          );
 486      }
 487  }


Generated: Mon Oct 25 01:00:54 2021 Cross-referenced by PHPXref 0.7.1