[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * BuddyPress Activity Streams Loader.
   4   *
   5   * An activity stream component, for users, groups, and site tracking.
   6   *
   7   * @package BuddyPress
   8   * @subpackage ActivityCore
   9   * @since 1.5.0
  10   */
  11  
  12  // Exit if accessed directly.
  13  defined( 'ABSPATH' ) || exit;
  14  
  15  /**
  16   * Main Activity Class.
  17   *
  18   * @since 1.5.0
  19   */
  20  class BP_Activity_Component extends BP_Component {
  21  
  22      /**
  23       * Start the activity component setup process.
  24       *
  25       * @since 1.5.0
  26       */
  27  	public function __construct() {
  28          parent::start(
  29              'activity',
  30              __( 'Activity Streams', 'buddypress' ),
  31              buddypress()->plugin_dir,
  32              array(
  33                  'adminbar_myaccount_order' => 10,
  34                  'search_query_arg' => 'activity_search',
  35                  'features' => array( 'embeds' )
  36              )
  37          );
  38      }
  39  
  40      /**
  41       * Include component files.
  42       *
  43       * @since 1.5.0
  44       *
  45       * @see BP_Component::includes() for a description of arguments.
  46       *
  47       * @param array $includes See BP_Component::includes() for a description.
  48       */
  49  	public function includes( $includes = array() ) {
  50  
  51          // Files to include.
  52          $includes = array(
  53              'cssjs',
  54              'filters',
  55              'adminbar',
  56              'template',
  57              'functions',
  58              'cache'
  59          );
  60  
  61          // Notifications support.
  62          if ( bp_is_active( 'notifications' ) ) {
  63              $includes[] = 'notifications';
  64          }
  65  
  66          // Load Akismet support if Akismet is configured.
  67          $akismet_key = bp_get_option( 'wordpress_api_key' );
  68  
  69          /** This filter is documented in bp-activity/bp-activity-akismet.php */
  70          if ( defined( 'AKISMET_VERSION' ) && class_exists( 'Akismet' ) && ( ! empty( $akismet_key ) || defined( 'WPCOM_API_KEY' ) ) && apply_filters( 'bp_activity_use_akismet', bp_is_akismet_active() ) ) {
  71              $includes[] = 'akismet';
  72          }
  73  
  74          // Embeds
  75          if ( bp_is_active( $this->id, 'embeds' ) ) {
  76              $includes[] = 'embeds';
  77          }
  78  
  79          if ( is_admin() ) {
  80              $includes[] = 'admin';
  81          }
  82  
  83          parent::includes( $includes );
  84      }
  85  
  86      /**
  87       * Late includes method.
  88       *
  89       * Only load up certain code when on specific pages.
  90       *
  91       * @since 3.0.0
  92       */
  93  	public function late_includes() {
  94          // Bail if PHPUnit is running.
  95          if ( defined( 'BP_TESTS_DIR' ) ) {
  96              return;
  97          }
  98  
  99          /*
 100           * Load activity action and screen code if PHPUnit isn't running.
 101           *
 102           * For PHPUnit, we load these files in tests/phpunit/includes/install.php.
 103           */
 104          if ( bp_is_current_component( 'activity' ) ) {
 105              // Authenticated actions - Only fires when JS is disabled.
 106              if ( is_user_logged_in() &&
 107                  in_array( bp_current_action(), array( 'delete', 'spam', 'post', 'reply', 'favorite', 'unfavorite' ), true )
 108              ) {
 109                  require $this->path . 'bp-activity/actions/' . bp_current_action() . '.php';
 110              }
 111  
 112              // RSS feeds.
 113              if ( bp_is_current_action( 'feed' ) || bp_is_action_variable( 'feed', 0 ) ) {
 114                  require $this->path . 'bp-activity/actions/feeds.php';
 115              }
 116  
 117              // Screens - Directory.
 118              if ( bp_is_activity_directory() ) {
 119                  require $this->path . 'bp-activity/screens/directory.php';
 120              }
 121  
 122              // Screens - User main nav.
 123              if ( bp_is_user() ) {
 124                  require $this->path . 'bp-activity/screens/just-me.php';
 125              }
 126  
 127              /**
 128               * Screens - User secondary nav.
 129               *
 130               * For these specific actions, slugs can be customized using `BP_{COMPONENT}_SLUGS`.
 131               * As a result, we need to map filenames with slugs.
 132               */
 133              $filenames = array(
 134                  'favorites' => 'favorites',
 135                  'mentions'  => 'mentions',
 136              );
 137  
 138              if ( bp_is_active( 'friends' ) ) {
 139                  $filenames[bp_get_friends_slug()] = 'friends';
 140              }
 141  
 142              if ( bp_is_active( 'groups' ) ) {
 143                  $filenames[bp_get_groups_slug()] = 'groups';
 144              }
 145  
 146              // The slug is the current action requested.
 147              $slug = bp_current_action();
 148  
 149              if ( bp_is_user() && isset( $filenames[ $slug ] ) ) {
 150                  require $this->path . 'bp-activity/screens/' . $filenames[ $slug ] . '.php';
 151              }
 152  
 153              // Screens - Single permalink.
 154              if ( bp_is_current_action( 'p' ) || is_numeric( bp_current_action() ) ) {
 155                  require $this->path . 'bp-activity/screens/permalink.php';
 156              }
 157  
 158              // Theme compatibility.
 159              new BP_Activity_Theme_Compat();
 160          }
 161      }
 162  
 163      /**
 164       * Set up component global variables.
 165       *
 166       * The BP_ACTIVITY_SLUG constant is deprecated, and only used here for
 167       * backwards compatibility.
 168       *
 169       * @since 1.5.0
 170       *
 171       * @see BP_Component::setup_globals() for a description of arguments.
 172       *
 173       * @param array $args See BP_Component::setup_globals() for a description.
 174       */
 175  	public function setup_globals( $args = array() ) {
 176          $bp = buddypress();
 177  
 178          // Define a slug, if necessary.
 179          if ( ! defined( 'BP_ACTIVITY_SLUG' ) ) {
 180              define( 'BP_ACTIVITY_SLUG', $this->id );
 181          }
 182  
 183          // Global tables for activity component.
 184          $global_tables = array(
 185              'table_name'      => $bp->table_prefix . 'bp_activity',
 186              'table_name_meta' => $bp->table_prefix . 'bp_activity_meta',
 187          );
 188  
 189          // Metadata tables for groups component.
 190          $meta_tables = array(
 191              'activity' => $bp->table_prefix . 'bp_activity_meta',
 192          );
 193  
 194          // Fetch the default directory title.
 195          $default_directory_titles = bp_core_get_directory_page_default_titles();
 196          $default_directory_title  = $default_directory_titles[$this->id];
 197  
 198          // All globals for activity component.
 199          // Note that global_tables is included in this array.
 200          $args = array(
 201              'slug'                  => BP_ACTIVITY_SLUG,
 202              'root_slug'             => isset( $bp->pages->activity->slug ) ? $bp->pages->activity->slug : BP_ACTIVITY_SLUG,
 203              'has_directory'         => true,
 204              'directory_title'       => isset( $bp->pages->activity->title ) ? $bp->pages->activity->title : $default_directory_title,
 205              'notification_callback' => 'bp_activity_format_notifications',
 206              'search_string'         => __( 'Search Activity...', 'buddypress' ),
 207              'global_tables'         => $global_tables,
 208              'meta_tables'           => $meta_tables,
 209          );
 210  
 211          parent::setup_globals( $args );
 212      }
 213  
 214      /**
 215       * Set up component navigation.
 216       *
 217       * @since 1.5.0
 218       *
 219       * @see BP_Component::setup_nav() for a description of arguments.
 220       *
 221       * @param array $main_nav Optional. See BP_Component::setup_nav() for description.
 222       * @param array $sub_nav  Optional. See BP_Component::setup_nav() for description.
 223       */
 224  	public function setup_nav( $main_nav = array(), $sub_nav = array() ) {
 225  
 226          // Stop if there is no user displayed or logged in.
 227          if ( ! is_user_logged_in() && ! bp_displayed_user_id() ) {
 228              return;
 229          }
 230  
 231          // Determine user to use.
 232          if ( bp_displayed_user_domain() ) {
 233              $user_domain = bp_displayed_user_domain();
 234          } elseif ( bp_loggedin_user_domain() ) {
 235              $user_domain = bp_loggedin_user_domain();
 236          } else {
 237              return;
 238          }
 239  
 240          $slug          = bp_get_activity_slug();
 241          $activity_link = trailingslashit( $user_domain . $slug );
 242  
 243          // Add 'Activity' to the main navigation.
 244          $main_nav = array(
 245              'name'                => _x( 'Activity', 'Profile activity screen nav', 'buddypress' ),
 246              'slug'                => $slug,
 247              'position'            => 10,
 248              'screen_function'     => 'bp_activity_screen_my_activity',
 249              'default_subnav_slug' => 'just-me',
 250              'item_css_id'         => $this->id
 251          );
 252  
 253          // Add the subnav items to the activity nav item if we are using a theme that supports this.
 254          $sub_nav[] = array(
 255              'name'            => _x( 'Personal', 'Profile activity screen sub nav', 'buddypress' ),
 256              'slug'            => 'just-me',
 257              'parent_url'      => $activity_link,
 258              'parent_slug'     => $slug,
 259              'screen_function' => 'bp_activity_screen_my_activity',
 260              'position'        => 10
 261          );
 262  
 263          // Check @mentions.
 264          if ( bp_activity_do_mentions() ) {
 265              $sub_nav[] = array(
 266                  'name'            => _x( 'Mentions', 'Profile activity screen sub nav', 'buddypress' ),
 267                  'slug'            => 'mentions',
 268                  'parent_url'      => $activity_link,
 269                  'parent_slug'     => $slug,
 270                  'screen_function' => 'bp_activity_screen_mentions',
 271                  'position'        => 20,
 272                  'item_css_id'     => 'activity-mentions'
 273              );
 274          }
 275  
 276          // Favorite activity items.
 277          if ( bp_activity_can_favorite() ) {
 278              $sub_nav[] = array(
 279                  'name'            => _x( 'Favorites', 'Profile activity screen sub nav', 'buddypress' ),
 280                  'slug'            => 'favorites',
 281                  'parent_url'      => $activity_link,
 282                  'parent_slug'     => $slug,
 283                  'screen_function' => 'bp_activity_screen_favorites',
 284                  'position'        => 30,
 285                  'item_css_id'     => 'activity-favs'
 286              );
 287          }
 288  
 289          // Additional menu if friends is active.
 290          if ( bp_is_active( 'friends' ) ) {
 291              $sub_nav[] = array(
 292                  'name'            => _x( 'Friends', 'Profile activity screen sub nav', 'buddypress' ),
 293                  'slug'            => bp_get_friends_slug(),
 294                  'parent_url'      => $activity_link,
 295                  'parent_slug'     => $slug,
 296                  'screen_function' => 'bp_activity_screen_friends',
 297                  'position'        => 40,
 298                  'item_css_id'     => 'activity-friends'
 299              ) ;
 300          }
 301  
 302          // Additional menu if groups is active.
 303          if ( bp_is_active( 'groups' ) ) {
 304              $sub_nav[] = array(
 305                  'name'            => _x( 'Groups', 'Profile activity screen sub nav', 'buddypress' ),
 306                  'slug'            => bp_get_groups_slug(),
 307                  'parent_url'      => $activity_link,
 308                  'parent_slug'     => $slug,
 309                  'screen_function' => 'bp_activity_screen_groups',
 310                  'position'        => 50,
 311                  'item_css_id'     => 'activity-groups'
 312              );
 313          }
 314  
 315          parent::setup_nav( $main_nav, $sub_nav );
 316      }
 317  
 318      /**
 319       * Set up the component entries in the WordPress Admin Bar.
 320       *
 321       * @since 1.5.0
 322       *
 323       * @see BP_Component::setup_nav() for a description of the $wp_admin_nav
 324       *      parameter array.
 325       *
 326       * @param array $wp_admin_nav See BP_Component::setup_admin_bar() for a
 327       *                            description.
 328       */
 329  	public function setup_admin_bar( $wp_admin_nav = array() ) {
 330  
 331          // Menus for logged in user.
 332          if ( is_user_logged_in() ) {
 333  
 334              // Setup the logged in user variables.
 335              $activity_link = trailingslashit( bp_loggedin_user_domain() . bp_get_activity_slug() );
 336  
 337              // Unread message count.
 338              if ( bp_activity_do_mentions() ) {
 339                  $count = bp_get_total_mention_count_for_user( bp_loggedin_user_id() );
 340                  if ( !empty( $count ) ) {
 341                      $title = sprintf(
 342                          /* translators: %s: Unread mention count for the current user */
 343                          _x( 'Mentions %s', 'Toolbar Mention logged in user', 'buddypress' ),
 344                          '<span class="count">' . bp_core_number_format( $count ) . '</span>'
 345                      );
 346                  } else {
 347                      $title = _x( 'Mentions', 'Toolbar Mention logged in user', 'buddypress' );
 348                  }
 349              }
 350  
 351              // Add the "Activity" sub menu.
 352              $wp_admin_nav[] = array(
 353                  'parent' => buddypress()->my_account_menu_id,
 354                  'id'     => 'my-account-' . $this->id,
 355                  'title'  => _x( 'Activity', 'My Account Activity sub nav', 'buddypress' ),
 356                  'href'   => $activity_link
 357              );
 358  
 359              // Personal.
 360              $wp_admin_nav[] = array(
 361                  'parent'   => 'my-account-' . $this->id,
 362                  'id'       => 'my-account-' . $this->id . '-personal',
 363                  'title'    => _x( 'Personal', 'My Account Activity sub nav', 'buddypress' ),
 364                  'href'     => $activity_link,
 365                  'position' => 10
 366              );
 367  
 368              // Mentions.
 369              if ( bp_activity_do_mentions() ) {
 370                  $wp_admin_nav[] = array(
 371                      'parent'   => 'my-account-' . $this->id,
 372                      'id'       => 'my-account-' . $this->id . '-mentions',
 373                      'title'    => $title,
 374                      'href'     => trailingslashit( $activity_link . 'mentions' ),
 375                      'position' => 20
 376                  );
 377              }
 378  
 379              // Favorite activity items.
 380              if ( bp_activity_can_favorite() ) {
 381                  $wp_admin_nav[] = array(
 382                      'parent'   => 'my-account-' . $this->id,
 383                      'id'       => 'my-account-' . $this->id . '-favorites',
 384                      'title'    => _x( 'Favorites', 'My Account Activity sub nav', 'buddypress' ),
 385                      'href'     => trailingslashit( $activity_link . 'favorites' ),
 386                      'position' => 30
 387                  );
 388              }
 389  
 390              // Friends?
 391              if ( bp_is_active( 'friends' ) ) {
 392                  $wp_admin_nav[] = array(
 393                      'parent'   => 'my-account-' . $this->id,
 394                      'id'       => 'my-account-' . $this->id . '-friends',
 395                      'title'    => _x( 'Friends', 'My Account Activity sub nav', 'buddypress' ),
 396                      'href'     => trailingslashit( $activity_link . bp_get_friends_slug() ),
 397                      'position' => 40
 398                  );
 399              }
 400  
 401              // Groups?
 402              if ( bp_is_active( 'groups' ) ) {
 403                  $wp_admin_nav[] = array(
 404                      'parent'   => 'my-account-' . $this->id,
 405                      'id'       => 'my-account-' . $this->id . '-groups',
 406                      'title'    => _x( 'Groups', 'My Account Activity sub nav', 'buddypress' ),
 407                      'href'     => trailingslashit( $activity_link . bp_get_groups_slug() ),
 408                      'position' => 50
 409                  );
 410              }
 411          }
 412  
 413          parent::setup_admin_bar( $wp_admin_nav );
 414      }
 415  
 416      /**
 417       * Set up the title for pages and <title>.
 418       *
 419       * @since 1.5.0
 420       *
 421       */
 422  	public function setup_title() {
 423  
 424          // Adjust title based on view.
 425          if ( bp_is_activity_component() ) {
 426              $bp = buddypress();
 427  
 428              if ( bp_is_my_profile() ) {
 429                  $bp->bp_options_title = _x( 'My Activity', 'Page and <title>', 'buddypress' );
 430              } else {
 431                  $bp->bp_options_avatar = bp_core_fetch_avatar( array(
 432                      'item_id' => bp_displayed_user_id(),
 433                      'type'    => 'thumb',
 434                      'alt'      => sprintf( __( 'Profile picture of %s', 'buddypress' ), bp_get_displayed_user_fullname() )
 435                  ) );
 436                  $bp->bp_options_title  = bp_get_displayed_user_fullname();
 437              }
 438          }
 439  
 440          parent::setup_title();
 441      }
 442  
 443      /**
 444       * Setup cache groups.
 445       *
 446       * @since 2.2.0
 447       */
 448  	public function setup_cache_groups() {
 449  
 450          // Global groups.
 451          wp_cache_add_global_groups( array(
 452              'bp_activity',
 453              'bp_activity_comments',
 454              'activity_meta'
 455          ) );
 456  
 457          parent::setup_cache_groups();
 458      }
 459  
 460      /**
 461       * Init the BP REST API.
 462       *
 463       * @since 5.0.0
 464       *
 465       * @param array $controllers Optional. See BP_Component::rest_api_init() for
 466       *                           description.
 467       */
 468  	public function rest_api_init( $controllers = array() ) {
 469          parent::rest_api_init( array( 'BP_REST_Activity_Endpoint' ) );
 470      }
 471  }


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