[ 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              'blocks',
  60          );
  61  
  62          // Notifications support.
  63          if ( bp_is_active( 'notifications' ) ) {
  64              $includes[] = 'notifications';
  65          }
  66  
  67          // Load Akismet support if Akismet is configured.
  68          $akismet_key = bp_get_option( 'wordpress_api_key' );
  69  
  70          /** This filter is documented in bp-activity/bp-activity-akismet.php */
  71          if ( defined( 'AKISMET_VERSION' ) && class_exists( 'Akismet' ) && ( ! empty( $akismet_key ) || defined( 'WPCOM_API_KEY' ) ) && apply_filters( 'bp_activity_use_akismet', bp_is_akismet_active() ) ) {
  72              $includes[] = 'akismet';
  73          }
  74  
  75          // Embeds.
  76          if ( bp_is_active( $this->id, 'embeds' ) ) {
  77              $includes[] = 'embeds';
  78          }
  79  
  80          if ( is_admin() ) {
  81              $includes[] = 'admin';
  82          }
  83  
  84          parent::includes( $includes );
  85      }
  86  
  87      /**
  88       * Late includes method.
  89       *
  90       * Only load up certain code when on specific pages.
  91       *
  92       * @since 3.0.0
  93       */
  94  	public function late_includes() {
  95          // Bail if PHPUnit is running.
  96          if ( defined( 'BP_TESTS_DIR' ) ) {
  97              return;
  98          }
  99  
 100          /*
 101           * Load activity action and screen code if PHPUnit isn't running.
 102           *
 103           * For PHPUnit, we load these files in tests/phpunit/includes/install.php.
 104           */
 105          if ( bp_is_current_component( 'activity' ) ) {
 106              // Authenticated actions - Only fires when JS is disabled.
 107              if ( is_user_logged_in() &&
 108                  in_array( bp_current_action(), array( 'delete', 'spam', 'post', 'reply', 'favorite', 'unfavorite' ), true )
 109              ) {
 110                  require $this->path . 'bp-activity/actions/' . bp_current_action() . '.php';
 111              }
 112  
 113              // RSS feeds.
 114              if ( bp_is_current_action( 'feed' ) || bp_is_action_variable( 'feed', 0 ) ) {
 115                  require $this->path . 'bp-activity/actions/feeds.php';
 116              }
 117  
 118              // Screens - Directory.
 119              if ( bp_is_activity_directory() ) {
 120                  require $this->path . 'bp-activity/screens/directory.php';
 121              }
 122  
 123              // Screens - User main nav.
 124              if ( bp_is_user() ) {
 125                  require $this->path . 'bp-activity/screens/just-me.php';
 126              }
 127  
 128              /**
 129               * Screens - User secondary nav.
 130               *
 131               * For these specific actions, slugs can be customized using `BP_{COMPONENT}_SLUGS`.
 132               * As a result, we need to map filenames with slugs.
 133               */
 134              $filenames = array(
 135                  'favorites' => 'favorites',
 136                  'mentions'  => 'mentions',
 137              );
 138  
 139              if ( bp_is_active( 'friends' ) ) {
 140                  $filenames[bp_get_friends_slug()] = 'friends';
 141              }
 142  
 143              if ( bp_is_active( 'groups' ) ) {
 144                  $filenames[bp_get_groups_slug()] = 'groups';
 145              }
 146  
 147              // The slug is the current action requested.
 148              $slug = bp_current_action();
 149  
 150              if ( bp_is_user() && isset( $filenames[ $slug ] ) ) {
 151                  require $this->path . 'bp-activity/screens/' . $filenames[ $slug ] . '.php';
 152              }
 153  
 154              // Screens - Single permalink.
 155              if ( bp_is_current_action( 'p' ) || is_numeric( bp_current_action() ) ) {
 156                  require $this->path . 'bp-activity/screens/permalink.php';
 157              }
 158  
 159              // Theme compatibility.
 160              new BP_Activity_Theme_Compat();
 161          }
 162      }
 163  
 164      /**
 165       * Set up component global variables.
 166       *
 167       * The BP_ACTIVITY_SLUG constant is deprecated, and only used here for
 168       * backwards compatibility.
 169       *
 170       * @since 1.5.0
 171       *
 172       * @see BP_Component::setup_globals() for a description of arguments.
 173       *
 174       * @param array $args See BP_Component::setup_globals() for a description.
 175       */
 176  	public function setup_globals( $args = array() ) {
 177          $bp = buddypress();
 178  
 179          // Define a slug, if necessary.
 180          if ( ! defined( 'BP_ACTIVITY_SLUG' ) ) {
 181              define( 'BP_ACTIVITY_SLUG', $this->id );
 182          }
 183  
 184          // Global tables for activity component.
 185          $global_tables = array(
 186              'table_name'      => $bp->table_prefix . 'bp_activity',
 187              'table_name_meta' => $bp->table_prefix . 'bp_activity_meta',
 188          );
 189  
 190          // Metadata tables for groups component.
 191          $meta_tables = array(
 192              'activity' => $bp->table_prefix . 'bp_activity_meta',
 193          );
 194  
 195          // Fetch the default directory title.
 196          $default_directory_titles = bp_core_get_directory_page_default_titles();
 197          $default_directory_title  = $default_directory_titles[$this->id];
 198  
 199          // All globals for activity component.
 200          // Note that global_tables is included in this array.
 201          $args = array(
 202              'slug'                  => BP_ACTIVITY_SLUG,
 203              'root_slug'             => isset( $bp->pages->activity->slug ) ? $bp->pages->activity->slug : BP_ACTIVITY_SLUG,
 204              'has_directory'         => true,
 205              'directory_title'       => isset( $bp->pages->activity->title ) ? $bp->pages->activity->title : $default_directory_title,
 206              'notification_callback' => 'bp_activity_format_notifications',
 207              'search_string'         => __( 'Search Activity...', 'buddypress' ),
 208              'global_tables'         => $global_tables,
 209              'meta_tables'           => $meta_tables,
 210              'block_globals'         => array(
 211                  'bp/latest-activities' => array(
 212                      'widget_classnames' => array( 'wp-block-bp-latest-activities', 'buddypress' ),
 213                  )
 214              ),
 215          );
 216  
 217          parent::setup_globals( $args );
 218      }
 219  
 220      /**
 221       * Set up component navigation.
 222       *
 223       * @since 1.5.0
 224       *
 225       * @see BP_Component::setup_nav() for a description of arguments.
 226       *
 227       * @param array $main_nav Optional. See BP_Component::setup_nav() for description.
 228       * @param array $sub_nav  Optional. See BP_Component::setup_nav() for description.
 229       */
 230  	public function setup_nav( $main_nav = array(), $sub_nav = array() ) {
 231  
 232          // Stop if there is no user displayed or logged in.
 233          if ( ! is_user_logged_in() && ! bp_displayed_user_id() ) {
 234              return;
 235          }
 236  
 237          // Determine user to use.
 238          if ( bp_displayed_user_domain() ) {
 239              $user_domain = bp_displayed_user_domain();
 240          } elseif ( bp_loggedin_user_domain() ) {
 241              $user_domain = bp_loggedin_user_domain();
 242          } else {
 243              return;
 244          }
 245  
 246          $slug          = bp_get_activity_slug();
 247          $activity_link = trailingslashit( $user_domain . $slug );
 248  
 249          // Add 'Activity' to the main navigation.
 250          $main_nav = array(
 251              'name'                => _x( 'Activity', 'Profile activity screen nav', 'buddypress' ),
 252              'slug'                => $slug,
 253              'position'            => 10,
 254              'screen_function'     => 'bp_activity_screen_my_activity',
 255              'default_subnav_slug' => 'just-me',
 256              'item_css_id'         => $this->id
 257          );
 258  
 259          // Add the subnav items to the activity nav item if we are using a theme that supports this.
 260          $sub_nav[] = array(
 261              'name'            => _x( 'Personal', 'Profile activity screen sub nav', 'buddypress' ),
 262              'slug'            => 'just-me',
 263              'parent_url'      => $activity_link,
 264              'parent_slug'     => $slug,
 265              'screen_function' => 'bp_activity_screen_my_activity',
 266              'position'        => 10
 267          );
 268  
 269          // Check @mentions.
 270          if ( bp_activity_do_mentions() ) {
 271              $sub_nav[] = array(
 272                  'name'            => _x( 'Mentions', 'Profile activity screen sub nav', 'buddypress' ),
 273                  'slug'            => 'mentions',
 274                  'parent_url'      => $activity_link,
 275                  'parent_slug'     => $slug,
 276                  'screen_function' => 'bp_activity_screen_mentions',
 277                  'position'        => 20,
 278                  'item_css_id'     => 'activity-mentions'
 279              );
 280          }
 281  
 282          // Favorite activity items.
 283          if ( bp_activity_can_favorite() ) {
 284              $sub_nav[] = array(
 285                  'name'            => _x( 'Favorites', 'Profile activity screen sub nav', 'buddypress' ),
 286                  'slug'            => 'favorites',
 287                  'parent_url'      => $activity_link,
 288                  'parent_slug'     => $slug,
 289                  'screen_function' => 'bp_activity_screen_favorites',
 290                  'position'        => 30,
 291                  'item_css_id'     => 'activity-favs'
 292              );
 293          }
 294  
 295          // Additional menu if friends is active.
 296          if ( bp_is_active( 'friends' ) ) {
 297              $sub_nav[] = array(
 298                  'name'            => _x( 'Friends', 'Profile activity screen sub nav', 'buddypress' ),
 299                  'slug'            => bp_get_friends_slug(),
 300                  'parent_url'      => $activity_link,
 301                  'parent_slug'     => $slug,
 302                  'screen_function' => 'bp_activity_screen_friends',
 303                  'position'        => 40,
 304                  'item_css_id'     => 'activity-friends'
 305              ) ;
 306          }
 307  
 308          // Additional menu if groups is active.
 309          if ( bp_is_active( 'groups' ) ) {
 310              $sub_nav[] = array(
 311                  'name'            => _x( 'Groups', 'Profile activity screen sub nav', 'buddypress' ),
 312                  'slug'            => bp_get_groups_slug(),
 313                  'parent_url'      => $activity_link,
 314                  'parent_slug'     => $slug,
 315                  'screen_function' => 'bp_activity_screen_groups',
 316                  'position'        => 50,
 317                  'item_css_id'     => 'activity-groups'
 318              );
 319          }
 320  
 321          parent::setup_nav( $main_nav, $sub_nav );
 322      }
 323  
 324      /**
 325       * Set up the component entries in the WordPress Admin Bar.
 326       *
 327       * @since 1.5.0
 328       *
 329       * @see BP_Component::setup_nav() for a description of the $wp_admin_nav
 330       *      parameter array.
 331       *
 332       * @param array $wp_admin_nav See BP_Component::setup_admin_bar() for a
 333       *                            description.
 334       */
 335  	public function setup_admin_bar( $wp_admin_nav = array() ) {
 336  
 337          // Menus for logged in user.
 338          if ( is_user_logged_in() ) {
 339  
 340              // Setup the logged in user variables.
 341              $activity_link = trailingslashit( bp_loggedin_user_domain() . bp_get_activity_slug() );
 342  
 343              // Unread message count.
 344              if ( bp_activity_do_mentions() ) {
 345                  $count = bp_get_total_mention_count_for_user( bp_loggedin_user_id() );
 346                  if ( !empty( $count ) ) {
 347                      $title = sprintf(
 348                          /* translators: %s: Unread mention count for the current user */
 349                          _x( 'Mentions %s', 'Toolbar Mention logged in user', 'buddypress' ),
 350                          '<span class="count">' . bp_core_number_format( $count ) . '</span>'
 351                      );
 352                  } else {
 353                      $title = _x( 'Mentions', 'Toolbar Mention logged in user', 'buddypress' );
 354                  }
 355              }
 356  
 357              // Add the "Activity" sub menu.
 358              $wp_admin_nav[] = array(
 359                  'parent' => buddypress()->my_account_menu_id,
 360                  'id'     => 'my-account-' . $this->id,
 361                  'title'  => _x( 'Activity', 'My Account Activity sub nav', 'buddypress' ),
 362                  'href'   => $activity_link
 363              );
 364  
 365              // Personal.
 366              $wp_admin_nav[] = array(
 367                  'parent'   => 'my-account-' . $this->id,
 368                  'id'       => 'my-account-' . $this->id . '-personal',
 369                  'title'    => _x( 'Personal', 'My Account Activity sub nav', 'buddypress' ),
 370                  'href'     => trailingslashit( $activity_link . 'just-me' ),
 371                  'position' => 10
 372              );
 373  
 374              // Mentions.
 375              if ( bp_activity_do_mentions() ) {
 376                  $wp_admin_nav[] = array(
 377                      'parent'   => 'my-account-' . $this->id,
 378                      'id'       => 'my-account-' . $this->id . '-mentions',
 379                      'title'    => $title,
 380                      'href'     => trailingslashit( $activity_link . 'mentions' ),
 381                      'position' => 20
 382                  );
 383              }
 384  
 385              // Favorite activity items.
 386              if ( bp_activity_can_favorite() ) {
 387                  $wp_admin_nav[] = array(
 388                      'parent'   => 'my-account-' . $this->id,
 389                      'id'       => 'my-account-' . $this->id . '-favorites',
 390                      'title'    => _x( 'Favorites', 'My Account Activity sub nav', 'buddypress' ),
 391                      'href'     => trailingslashit( $activity_link . 'favorites' ),
 392                      'position' => 30
 393                  );
 394              }
 395  
 396              // Friends?
 397              if ( bp_is_active( 'friends' ) ) {
 398                  $wp_admin_nav[] = array(
 399                      'parent'   => 'my-account-' . $this->id,
 400                      'id'       => 'my-account-' . $this->id . '-friends',
 401                      'title'    => _x( 'Friends', 'My Account Activity sub nav', 'buddypress' ),
 402                      'href'     => trailingslashit( $activity_link . bp_get_friends_slug() ),
 403                      'position' => 40
 404                  );
 405              }
 406  
 407              // Groups?
 408              if ( bp_is_active( 'groups' ) ) {
 409                  $wp_admin_nav[] = array(
 410                      'parent'   => 'my-account-' . $this->id,
 411                      'id'       => 'my-account-' . $this->id . '-groups',
 412                      'title'    => _x( 'Groups', 'My Account Activity sub nav', 'buddypress' ),
 413                      'href'     => trailingslashit( $activity_link . bp_get_groups_slug() ),
 414                      'position' => 50
 415                  );
 416              }
 417          }
 418  
 419          parent::setup_admin_bar( $wp_admin_nav );
 420      }
 421  
 422      /**
 423       * Set up the title for pages and <title>.
 424       *
 425       * @since 1.5.0
 426       *
 427       */
 428  	public function setup_title() {
 429  
 430          // Adjust title based on view.
 431          if ( bp_is_activity_component() ) {
 432              $bp = buddypress();
 433  
 434              if ( bp_is_my_profile() ) {
 435                  $bp->bp_options_title = _x( 'My Activity', 'Page and <title>', 'buddypress' );
 436              } else {
 437                  $bp->bp_options_avatar = bp_core_fetch_avatar( array(
 438                      'item_id' => bp_displayed_user_id(),
 439                      'type'    => 'thumb',
 440                      'alt'      => sprintf(
 441                          /* translators: %s: member name */
 442                          __( 'Profile picture of %s', 'buddypress' ),
 443                          bp_get_displayed_user_fullname()
 444                      ),
 445                  ) );
 446                  $bp->bp_options_title  = bp_get_displayed_user_fullname();
 447              }
 448          }
 449  
 450          parent::setup_title();
 451      }
 452  
 453      /**
 454       * Setup cache groups.
 455       *
 456       * @since 2.2.0
 457       */
 458  	public function setup_cache_groups() {
 459  
 460          // Global groups.
 461          wp_cache_add_global_groups( array(
 462              'bp_activity',
 463              'bp_activity_comments',
 464              'activity_meta'
 465          ) );
 466  
 467          parent::setup_cache_groups();
 468      }
 469  
 470      /**
 471       * Init the BP REST API.
 472       *
 473       * @since 5.0.0
 474       *
 475       * @param array $controllers Optional. See BP_Component::rest_api_init() for
 476       *                           description.
 477       */
 478  	public function rest_api_init( $controllers = array() ) {
 479          parent::rest_api_init( array( 'BP_REST_Activity_Endpoint' ) );
 480      }
 481  
 482      /**
 483       * Register the BP Activity Blocks.
 484       *
 485       * @since 7.0.0
 486       *
 487       * @param array $blocks Optional. See BP_Component::blocks_init() for
 488       *                      description.
 489       */
 490  	public function blocks_init( $blocks = array() ) {
 491          $blocks = array(
 492              'bp/latest-activities' => array(
 493                  'name'               => 'bp/latest-activities',
 494                  'editor_script'      => 'bp-latest-activities-block',
 495                  'editor_script_url'  => plugins_url( 'js/blocks/latest-activities.js', dirname(  __FILE__ ) ),
 496                  'editor_script_deps' => array(
 497                      'wp-blocks',
 498                      'wp-element',
 499                      'wp-components',
 500                      'wp-i18n',
 501                      'wp-block-editor',
 502                      'wp-server-side-render',
 503                      'bp-block-data',
 504                  ),
 505                  'style'              => 'bp-latest-activities-block',
 506                  'style_url'          => plugins_url( 'css/blocks/latest-activities.css', dirname(  __FILE__ ) ),
 507                  'attributes'         => array(
 508                      'title'         => array(
 509                          'type'    => 'string',
 510                          'default' => __( 'Latest updates', 'buddypress' ),
 511                      ),
 512                      'maxActivities' => array(
 513                          'type'    => 'number',
 514                          'default' => 5,
 515                      ),
 516                      'type'          => array(
 517                          'type'    => 'array',
 518                          'default' => array( 'activity_update' ),
 519                      ),
 520                      'postId'        => array(
 521                          'type'    => 'number',
 522                          'default' => 0,
 523                      ),
 524                  ),
 525                  'render_callback'    => 'bp_activity_render_latest_activities_block',
 526              ),
 527          );
 528  
 529          if ( bp_is_active( $this->id, 'embeds' ) ) {
 530              $blocks['bp/embed-activity'] = array(
 531                  'name'               => 'bp/embed-activity',
 532                  'editor_script'      => 'bp-embed-activity-block',
 533                  'editor_script_url'  => plugins_url( 'js/blocks/embed-activity.js', dirname(  __FILE__ ) ),
 534                  'editor_script_deps' => array(
 535                      'wp-blocks',
 536                      'wp-element',
 537                      'wp-i18n',
 538                      'wp-components',
 539                      'wp-block-editor',
 540                      'wp-data',
 541                      'wp-compose',
 542                      'bp-block-data',
 543                  ),
 544              );
 545          }
 546  
 547          parent::blocks_init( $blocks );
 548      }
 549  
 550      /**
 551       * Add the Activity directory state.
 552       *
 553       * @since 10.0.0
 554       *
 555       * @param array   $states Optional. See BP_Component::admin_directory_states() for description.
 556       * @param WP_Post $post   Optional. See BP_Component::admin_directory_states() for description.
 557       * @return array          See BP_Component::admin_directory_states() for description.
 558       */
 559  	public function admin_directory_states( $states = array(), $post = null ) {
 560          $bp = buddypress();
 561  
 562          if ( isset( $bp->pages->activity->id ) && (int) $bp->pages->activity->id === (int) $post->ID ) {
 563              $states['page_for_activity_directory'] = _x( 'BP Activity Page', 'page label', 'buddypress' );
 564          }
 565  
 566          return parent::admin_directory_states( $states, $post );
 567      }
 568  }


Generated: Thu Dec 5 01:00:58 2024 Cross-referenced by PHPXref 0.7.1