[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * BuddyPress Member Notifications Loader.
   4   *
   5   * Initializes the Notifications component.
   6   *
   7   * @package BuddyPress
   8   * @subpackage NotificationsLoader
   9   * @since 1.9.0
  10   */
  11  
  12  // Exit if accessed directly.
  13  defined( 'ABSPATH' ) || exit;
  14  
  15  /**
  16   * Extends the component class to set up the Notifications component.
  17   */
  18  class BP_Notifications_Component extends BP_Component {
  19  
  20      /**
  21       * Start the notifications component creation process.
  22       *
  23       * @since 1.9.0
  24       */
  25  	public function __construct() {
  26          parent::start(
  27              'notifications',
  28              _x( 'Notifications', 'Page <title>', 'buddypress' ),
  29              buddypress()->plugin_dir,
  30              array(
  31                  'adminbar_myaccount_order' => 30,
  32              )
  33          );
  34      }
  35  
  36      /**
  37       * Include notifications component files.
  38       *
  39       * @since 1.9.0
  40       *
  41       * @see BP_Component::includes() for a description of arguments.
  42       *
  43       * @param array $includes See BP_Component::includes() for a description.
  44       */
  45  	public function includes( $includes = array() ) {
  46          $includes = array(
  47              'adminbar',
  48              'template',
  49              'filters',
  50              'functions',
  51              'cache',
  52          );
  53  
  54          parent::includes( $includes );
  55      }
  56  
  57      /**
  58       * Late includes method.
  59       *
  60       * Only load up certain code when on specific pages.
  61       *
  62       * @since 3.0.0
  63       */
  64  	public function late_includes() {
  65          // Bail if PHPUnit is running.
  66          if ( defined( 'BP_TESTS_DIR' ) ) {
  67              return;
  68          }
  69  
  70          // Bail if not on a notifications page or logged in.
  71          if ( ! bp_is_user_notifications() || ! is_user_logged_in() ) {
  72              return;
  73          }
  74  
  75          // Actions.
  76          if ( bp_is_post_request() ) {
  77              require $this->path . 'bp-notifications/actions/bulk-manage.php';
  78          } elseif ( bp_is_get_request() ) {
  79              require $this->path . 'bp-notifications/actions/delete.php';
  80          }
  81  
  82          // Screens.
  83          require $this->path . 'bp-notifications/screens/unread.php';
  84          if ( bp_is_current_action( 'read' ) ) {
  85              require $this->path . 'bp-notifications/screens/read.php';
  86          }
  87      }
  88  
  89      /**
  90       * Set up component global data.
  91       *
  92       * @since 1.9.0
  93       *
  94       * @see BP_Component::setup_globals() for a description of arguments.
  95       *
  96       * @param array $args See BP_Component::setup_globals() for a description.
  97       */
  98  	public function setup_globals( $args = array() ) {
  99          $bp = buddypress();
 100  
 101          // Define a slug, if necessary.
 102          if ( ! defined( 'BP_NOTIFICATIONS_SLUG' ) ) {
 103              define( 'BP_NOTIFICATIONS_SLUG', $this->id );
 104          }
 105  
 106          // Global tables for the notifications component.
 107          $global_tables = array(
 108              'table_name'      => $bp->table_prefix . 'bp_notifications',
 109              'table_name_meta' => $bp->table_prefix . 'bp_notifications_meta',
 110          );
 111  
 112          // Metadata tables for notifications component.
 113          $meta_tables = array(
 114              'notification' => $bp->table_prefix . 'bp_notifications_meta',
 115          );
 116  
 117          // All globals for the notifications component.
 118          // Note that global_tables is included in this array.
 119          $args = array(
 120              'slug'          => BP_NOTIFICATIONS_SLUG,
 121              'has_directory' => false,
 122              'search_string' => __( 'Search Notifications...', 'buddypress' ),
 123              'global_tables' => $global_tables,
 124              'meta_tables'   => $meta_tables,
 125          );
 126  
 127          parent::setup_globals( $args );
 128      }
 129  
 130      /**
 131       * Set up component navigation.
 132       *
 133       * @since 1.9.0
 134       *
 135       * @see BP_Component::setup_nav() for a description of arguments.
 136       *
 137       * @param array $main_nav Optional. See BP_Component::setup_nav() for
 138       *                        description.
 139       * @param array $sub_nav  Optional. See BP_Component::setup_nav() for
 140       *                        description.
 141       */
 142  	public function setup_nav( $main_nav = array(), $sub_nav = array() ) {
 143  
 144          // Determine user to use.
 145          if ( bp_displayed_user_domain() ) {
 146              $user_domain = bp_displayed_user_domain();
 147          } elseif ( bp_loggedin_user_domain() ) {
 148              $user_domain = bp_loggedin_user_domain();
 149          } else {
 150              return;
 151          }
 152  
 153          $access             = bp_core_can_edit_settings();
 154          $slug               = bp_get_notifications_slug();
 155          $notifications_link = trailingslashit( $user_domain . $slug );
 156  
 157          // Only grab count if we're on a user page and current user has access.
 158          if ( bp_is_user() && bp_user_has_access() ) {
 159              $count    = bp_notifications_get_unread_notification_count( bp_displayed_user_id() );
 160              $class    = ( 0 === $count ) ? 'no-count' : 'count';
 161              $nav_name = sprintf(
 162                  /* translators: %s: Unread notification count for the current user */
 163                  _x( 'Notifications %s', 'Profile screen nav', 'buddypress' ),
 164                  sprintf(
 165                      '<span class="%s">%s</span>',
 166                      esc_attr( $class ),
 167                      esc_html( $count )
 168                  )
 169              );
 170          } else {
 171              $nav_name = _x( 'Notifications', 'Profile screen nav', 'buddypress' );
 172          }
 173  
 174          // Add 'Notifications' to the main navigation.
 175          $main_nav = array(
 176              'name'                    => $nav_name,
 177              'slug'                    => $slug,
 178              'position'                => 30,
 179              'show_for_displayed_user' => $access,
 180              'screen_function'         => 'bp_notifications_screen_unread',
 181              'default_subnav_slug'     => 'unread',
 182              'item_css_id'             => $this->id,
 183          );
 184  
 185          // Add the subnav items to the notifications nav item.
 186          $sub_nav[] = array(
 187              'name'            => _x( 'Unread', 'Notification screen nav', 'buddypress' ),
 188              'slug'            => 'unread',
 189              'parent_url'      => $notifications_link,
 190              'parent_slug'     => $slug,
 191              'screen_function' => 'bp_notifications_screen_unread',
 192              'position'        => 10,
 193              'item_css_id'     => 'notifications-my-notifications',
 194              'user_has_access' => $access,
 195          );
 196  
 197          $sub_nav[] = array(
 198              'name'            => _x( 'Read', 'Notification screen nav', 'buddypress' ),
 199              'slug'            => 'read',
 200              'parent_url'      => $notifications_link,
 201              'parent_slug'     => $slug,
 202              'screen_function' => 'bp_notifications_screen_read',
 203              'position'        => 20,
 204              'user_has_access' => $access,
 205          );
 206  
 207          parent::setup_nav( $main_nav, $sub_nav );
 208      }
 209  
 210      /**
 211       * Set up the component entries in the WordPress Admin Bar.
 212       *
 213       * @since 1.9.0
 214       *
 215       * @see BP_Component::setup_nav() for a description of the $wp_admin_nav
 216       *      parameter array.
 217       *
 218       * @param array $wp_admin_nav See BP_Component::setup_admin_bar() for a
 219       *                            description.
 220       */
 221  	public function setup_admin_bar( $wp_admin_nav = array() ) {
 222  
 223          // Menus for logged in user.
 224          if ( is_user_logged_in() ) {
 225  
 226              // Setup the logged in user variables.
 227              $notifications_link = trailingslashit( bp_loggedin_user_domain() . bp_get_notifications_slug() );
 228  
 229              // Pending notification requests.
 230              $count = bp_notifications_get_unread_notification_count( bp_loggedin_user_id() );
 231              if ( ! empty( $count ) ) {
 232                  $title = sprintf(
 233                      /* translators: %s: Unread notification count for the current user */
 234                      _x( 'Notifications %s', 'My Account Notification pending', 'buddypress' ),
 235                      '<span class="count">' . bp_core_number_format( $count ) . '</span>'
 236                  );
 237                  $unread = sprintf(
 238                      /* translators: %s: Unread notification count for the current user */
 239                      _x( 'Unread %s', 'My Account Notification pending', 'buddypress' ),
 240                      '<span class="count">' . bp_core_number_format( $count ) . '</span>'
 241                  );
 242              } else {
 243                  $title  = _x( 'Notifications', 'My Account Notification',         'buddypress' );
 244                  $unread = _x( 'Unread',        'My Account Notification sub nav', 'buddypress' );
 245              }
 246  
 247              // Add the "My Account" sub menus.
 248              $wp_admin_nav[] = array(
 249                  'parent' => buddypress()->my_account_menu_id,
 250                  'id'     => 'my-account-' . $this->id,
 251                  'title'  => $title,
 252                  'href'   => $notifications_link,
 253              );
 254  
 255              // Unread.
 256              $wp_admin_nav[] = array(
 257                  'parent'   => 'my-account-' . $this->id,
 258                  'id'       => 'my-account-' . $this->id . '-unread',
 259                  'title'    => $unread,
 260                  'href'     => trailingslashit( $notifications_link . 'unread' ),
 261                  'position' => 10,
 262              );
 263  
 264              // Read.
 265              $wp_admin_nav[] = array(
 266                  'parent'   => 'my-account-' . $this->id,
 267                  'id'       => 'my-account-' . $this->id . '-read',
 268                  'title'    => _x( 'Read', 'My Account Notification sub nav', 'buddypress' ),
 269                  'href'     => trailingslashit( $notifications_link . 'read' ),
 270                  'position' => 20,
 271              );
 272          }
 273  
 274          parent::setup_admin_bar( $wp_admin_nav );
 275      }
 276  
 277      /**
 278       * Set up the title for pages and <title>.
 279       *
 280       * @since 1.9.0
 281       */
 282  	public function setup_title() {
 283  
 284          // Adjust title.
 285          if ( bp_is_notifications_component() ) {
 286              $bp = buddypress();
 287  
 288              if ( bp_is_my_profile() ) {
 289                  $bp->bp_options_title = __( 'Notifications', 'buddypress' );
 290              } else {
 291                  $bp->bp_options_title  = bp_get_displayed_user_fullname();
 292                  $bp->bp_options_avatar = bp_core_fetch_avatar( array(
 293                      'item_id' => bp_displayed_user_id(),
 294                      'type'    => 'thumb',
 295                      /* translators: %s: member name */
 296                      'alt'     => sprintf( __( 'Profile picture of %s', 'buddypress' ), bp_get_displayed_user_fullname() )
 297                  ) );
 298              }
 299          }
 300  
 301          parent::setup_title();
 302      }
 303  
 304      /**
 305       * Setup cache groups.
 306       *
 307       * @since 2.2.0
 308       */
 309  	public function setup_cache_groups() {
 310  
 311          // Global groups.
 312          wp_cache_add_global_groups( array(
 313              'bp_notifications',
 314              'notification_meta'
 315          ) );
 316  
 317          parent::setup_cache_groups();
 318      }
 319  
 320      /**
 321       * Init the BP REST API.
 322       *
 323       * @since 5.0.0
 324       *
 325       * @param array $controllers Optional. See BP_Component::rest_api_init() for
 326       *                           description.
 327       */
 328  	public function rest_api_init( $controllers = array() ) {
 329          parent::rest_api_init( array( 'BP_REST_Notifications_Endpoint' ) );
 330      }
 331  }


Generated: Thu Nov 21 01:00:57 2024 Cross-referenced by PHPXref 0.7.1