[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-activity/ -> bp-activity-filters.php (source)

   1  <?php
   2  /**
   3   * Filters related to the Activity component.
   4   *
   5   * @package BuddyPress
   6   * @subpackage ActivityFilters
   7   * @since 1.0.0
   8   */
   9  
  10  // Exit if accessed directly.
  11  defined( 'ABSPATH' ) || exit;
  12  
  13  /* Filters *******************************************************************/
  14  
  15  // Apply WordPress defined filters.
  16  add_filter( 'bp_get_activity_content_body',          'bp_activity_filter_kses', 1 );
  17  add_filter( 'bp_get_activity_parent_content',        'bp_activity_filter_kses', 1 );
  18  add_filter( 'bp_get_activity_latest_update',         'bp_activity_filter_kses', 1 );
  19  add_filter( 'bp_get_activity_latest_update_excerpt', 'bp_activity_filter_kses', 1 );
  20  add_filter( 'bp_get_activity_feed_item_description', 'bp_activity_filter_kses', 1 );
  21  add_filter( 'bp_activity_content_before_save',       'bp_activity_filter_kses', 1 );
  22  add_filter( 'bp_activity_action_before_save',        'bp_activity_filter_kses', 1 );
  23  add_filter( 'bp_activity_latest_update_content',     'bp_activity_filter_kses', 1 );
  24  
  25  add_filter( 'bp_get_activity_action',                'force_balance_tags' );
  26  add_filter( 'bp_get_activity_content_body',          'force_balance_tags' );
  27  add_filter( 'bp_get_activity_content',               'force_balance_tags' );
  28  add_filter( 'bp_get_activity_latest_update',         'force_balance_tags' );
  29  add_filter( 'bp_get_activity_latest_update_excerpt', 'force_balance_tags' );
  30  add_filter( 'bp_get_activity_feed_item_description', 'force_balance_tags' );
  31  add_filter( 'bp_activity_content_before_save',       'force_balance_tags' );
  32  add_filter( 'bp_activity_action_before_save',        'force_balance_tags' );
  33  
  34  add_filter( 'bp_activity_content_before_save', 'wp_encode_emoji' );
  35  
  36  add_filter( 'bp_get_activity_action',                'wptexturize' );
  37  add_filter( 'bp_get_activity_content_body',          'wptexturize' );
  38  add_filter( 'bp_get_activity_content',               'wptexturize' );
  39  add_filter( 'bp_get_activity_parent_content',        'wptexturize' );
  40  add_filter( 'bp_get_activity_latest_update',         'wptexturize' );
  41  add_filter( 'bp_get_activity_latest_update_excerpt', 'wptexturize' );
  42  add_filter( 'bp_activity_get_embed_excerpt',         'wptexturize' );
  43  
  44  add_filter( 'bp_get_activity_action',                'convert_smilies' );
  45  add_filter( 'bp_get_activity_content_body',          'convert_smilies' );
  46  add_filter( 'bp_get_activity_content',               'convert_smilies' );
  47  add_filter( 'bp_get_activity_parent_content',        'convert_smilies' );
  48  add_filter( 'bp_get_activity_latest_update',         'convert_smilies' );
  49  add_filter( 'bp_get_activity_latest_update_excerpt', 'convert_smilies' );
  50  add_filter( 'bp_activity_get_embed_excerpt',         'convert_smilies' );
  51  
  52  add_filter( 'bp_get_activity_action',                'convert_chars' );
  53  add_filter( 'bp_get_activity_content_body',          'convert_chars' );
  54  add_filter( 'bp_get_activity_content',               'convert_chars' );
  55  add_filter( 'bp_get_activity_parent_content',        'convert_chars' );
  56  add_filter( 'bp_get_activity_latest_update',         'convert_chars' );
  57  add_filter( 'bp_get_activity_latest_update_excerpt', 'convert_chars' );
  58  add_filter( 'bp_activity_get_embed_excerpt',         'convert_chars' );
  59  
  60  add_filter( 'bp_get_activity_action',                'wpautop' );
  61  add_filter( 'bp_get_activity_content_body',          'wpautop' );
  62  add_filter( 'bp_get_activity_content',               'wpautop' );
  63  add_filter( 'bp_get_activity_feed_item_description', 'wpautop' );
  64  add_filter( 'bp_activity_get_embed_excerpt',         'wpautop' );
  65  
  66  add_filter( 'bp_get_activity_action',                'make_clickable', 9 );
  67  add_filter( 'bp_get_activity_content_body',          'make_clickable', 9 );
  68  add_filter( 'bp_get_activity_content',               'make_clickable', 9 );
  69  add_filter( 'bp_get_activity_parent_content',        'make_clickable', 9 );
  70  add_filter( 'bp_get_activity_latest_update',         'make_clickable', 9 );
  71  add_filter( 'bp_get_activity_latest_update_excerpt', 'make_clickable', 9 );
  72  add_filter( 'bp_get_activity_feed_item_description', 'make_clickable', 9 );
  73  add_filter( 'bp_activity_get_embed_excerpt',         'make_clickable', 9 );
  74  
  75  add_filter( 'bp_acomment_name',                      'stripslashes_deep', 5 );
  76  add_filter( 'bp_get_activity_action',                'stripslashes_deep', 5 );
  77  add_filter( 'bp_get_activity_content',               'stripslashes_deep', 5 );
  78  add_filter( 'bp_get_activity_content_body',          'stripslashes_deep', 5 );
  79  add_filter( 'bp_get_activity_parent_content',        'stripslashes_deep', 5 );
  80  add_filter( 'bp_get_activity_latest_update',         'stripslashes_deep', 5 );
  81  add_filter( 'bp_get_activity_latest_update_excerpt', 'stripslashes_deep', 5 );
  82  add_filter( 'bp_get_activity_feed_item_description', 'stripslashes_deep', 5 );
  83  
  84  add_filter( 'bp_activity_primary_link_before_save',  'esc_url_raw' );
  85  
  86  // Apply BuddyPress-defined filters.
  87  add_filter( 'bp_get_activity_content',               'bp_activity_make_nofollow_filter' );
  88  add_filter( 'bp_get_activity_content_body',          'bp_activity_make_nofollow_filter' );
  89  add_filter( 'bp_get_activity_parent_content',        'bp_activity_make_nofollow_filter' );
  90  add_filter( 'bp_get_activity_latest_update',         'bp_activity_make_nofollow_filter' );
  91  add_filter( 'bp_get_activity_latest_update_excerpt', 'bp_activity_make_nofollow_filter' );
  92  add_filter( 'bp_get_activity_feed_item_description', 'bp_activity_make_nofollow_filter' );
  93  
  94  add_filter( 'bp_get_activity_content_body', 'bp_core_add_loading_lazy_attribute' );
  95  add_filter( 'bp_activity_comment_content',  'bp_core_add_loading_lazy_attribute' );
  96  
  97  add_filter( 'pre_comment_content',                   'bp_activity_at_name_filter' );
  98  add_filter( 'the_content',                           'bp_activity_at_name_filter' );
  99  add_filter( 'bp_activity_get_embed_excerpt',         'bp_activity_at_name_filter' );
 100  
 101  add_filter( 'bp_get_activity_parent_content',        'bp_create_excerpt' );
 102  
 103  add_filter( 'bp_get_activity_content_body', 'bp_activity_truncate_entry', 5 );
 104  add_filter( 'bp_get_activity_content',      'bp_activity_truncate_entry', 5 );
 105  
 106  add_filter( 'bp_get_total_favorite_count_for_user', 'bp_core_number_format' );
 107  add_filter( 'bp_get_total_mention_count_for_user',  'bp_core_number_format' );
 108  
 109  add_filter( 'bp_activity_get_embed_excerpt', 'bp_activity_embed_excerpt_onclick_location_filter', 9 );
 110  
 111  // Personal data export.
 112  add_filter( 'wp_privacy_personal_data_exporters', 'bp_activity_register_personal_data_exporter' );
 113  
 114  /* Actions *******************************************************************/
 115  
 116  // At-name filter.
 117  add_action( 'bp_activity_before_save', 'bp_activity_at_name_filter_updates' );
 118  
 119  // Activity stream moderation.
 120  add_action( 'bp_activity_before_save', 'bp_activity_check_moderation_keys', 2, 1 );
 121  add_action( 'bp_activity_before_save', 'bp_activity_check_disallowed_keys',  2, 1 );
 122  
 123  /** Functions *****************************************************************/
 124  
 125  /**
 126   * Types of activity stream items to moderate.
 127   *
 128   * @since 1.6.0
 129   *
 130   * @return array $types List of the activity types to moderate.
 131   */
 132  function bp_activity_get_moderated_activity_types() {
 133      $types = array(
 134          'activity_comment',
 135          'activity_update'
 136      );
 137  
 138      /**
 139       * Filters the default activity types that BuddyPress should moderate.
 140       *
 141       * @since 1.6.0
 142       *
 143       * @param array $types Default activity types to moderate.
 144       */
 145      return apply_filters( 'bp_activity_check_activity_types', $types );
 146  }
 147  
 148  /**
 149   * Moderate the posted activity item, if it contains moderate keywords.
 150   *
 151   * @since 1.6.0
 152   *
 153   * @param BP_Activity_Activity $activity The activity object to check.
 154   */
 155  function bp_activity_check_moderation_keys( $activity ) {
 156  
 157      // Only check specific types of activity updates.
 158      if ( ! in_array( $activity->type, bp_activity_get_moderated_activity_types() ) ) {
 159          return;
 160      }
 161  
 162      // Send back the error so activity update fails.
 163      // @todo This is temporary until some kind of moderation is built.
 164      $moderate = bp_core_check_for_moderation( $activity->user_id, '', $activity->content, 'wp_error' );
 165      if ( is_wp_error( $moderate ) ) {
 166          $activity->errors = $moderate;
 167  
 168          // Backpat.
 169          $activity->component = false;
 170      }
 171  }
 172  
 173  /**
 174   * Mark the posted activity as spam, if it contains disallowed keywords.
 175   *
 176   * @since 7.0.0
 177   *
 178   * @param BP_Activity_Activity $activity The activity object to check.
 179   */
 180  function bp_activity_check_disallowed_keys( $activity ) {
 181  
 182      // Only check specific types of activity updates.
 183      if ( ! in_array( $activity->type, bp_activity_get_moderated_activity_types() ) ) {
 184          return;
 185      }
 186  
 187      // Send back the error so activity update fails.
 188      // @todo This is temporary until some kind of trash status is built.
 189      $disallowed = bp_core_check_for_disallowed_keys( $activity->user_id, '', $activity->content, 'wp_error' );
 190      if ( is_wp_error( $disallowed ) ) {
 191          $activity->errors = $disallowed;
 192  
 193          // Backpat.
 194          $activity->component = false;
 195      }
 196  }
 197  
 198  /**
 199   * Custom kses filtering for activity content.
 200   *
 201   * @since 1.1.0
 202   *
 203   * @param string $content The activity content.
 204   * @return string $content Filtered activity content.
 205   */
 206  function bp_activity_filter_kses( $content ) {
 207      $activity_allowedtags = bp_get_allowedtags();
 208  
 209      // Don't allow 'class' or 'id'.
 210      foreach ( $activity_allowedtags as $el => &$atts ) {
 211          unset( $atts['class'] );
 212          unset( $atts['id'] );
 213      }
 214  
 215      /**
 216       * Filters the allowed HTML tags for BuddyPress Activity content.
 217       *
 218       * @since 1.2.0
 219       *
 220       * @param array $value Array of allowed HTML tags and attributes.
 221       */
 222      $activity_allowedtags = apply_filters( 'bp_activity_allowed_tags', $activity_allowedtags );
 223      return wp_kses( $content, $activity_allowedtags );
 224  }
 225  
 226  /**
 227   * Find and link @-mentioned users in the contents of a given item.
 228   *
 229   * @since 1.2.0
 230   *
 231   * @param string $content     The contents of a given item.
 232   * @param int    $activity_id The activity id. Deprecated.
 233   * @return string $content Content filtered for mentions.
 234   */
 235  function bp_activity_at_name_filter( $content, $activity_id = 0 ) {
 236  
 237      // Are mentions disabled?
 238      if ( ! bp_activity_do_mentions() ) {
 239          return $content;
 240      }
 241  
 242      // Try to find mentions.
 243      $usernames = bp_activity_find_mentions( $content );
 244  
 245      // No mentions? Stop now!
 246      if ( empty( $usernames ) )
 247          return $content;
 248  
 249      // We don't want to link @mentions that are inside of links, so we
 250      // temporarily remove them.
 251      $replace_count = 0;
 252      $replacements = array();
 253      foreach ( $usernames as $username ) {
 254          // Prevent @ name linking inside <a> tags.
 255          preg_match_all( '/(<a.*?(?!<\/a>)@' . $username . '.*?<\/a>)/', $content, $content_matches );
 256          if ( ! empty( $content_matches[1] ) ) {
 257              foreach ( $content_matches[1] as $replacement ) {
 258                  $replacements[ '#BPAN' . $replace_count ] = $replacement;
 259                  $content = str_replace( $replacement, '#BPAN' . $replace_count, $content );
 260                  $replace_count++;
 261              }
 262          }
 263      }
 264  
 265      // Linkify the mentions with the username.
 266      foreach ( (array) $usernames as $user_id => $username ) {
 267          $content = preg_replace( '/(@' . $username . '\b)/', "<a class='bp-suggestions-mention' href='" . bp_core_get_user_domain( $user_id ) . "' rel='nofollow'>@$username</a>", $content );
 268      }
 269  
 270      // Put everything back.
 271      if ( ! empty( $replacements ) ) {
 272          foreach ( $replacements as $placeholder => $original ) {
 273              $content = str_replace( $placeholder, $original, $content );
 274          }
 275      }
 276  
 277      // Return the content.
 278      return $content;
 279  }
 280  
 281  /**
 282   * Catch mentions in an activity item before it is saved into the database.
 283   *
 284   * If mentions are found, replace @mention text with user links and add our
 285   * hook to send mention notifications after the activity item is saved.
 286   *
 287   * @since 1.5.0
 288   *
 289   * @param BP_Activity_Activity $activity Activity Object.
 290   */
 291  function bp_activity_at_name_filter_updates( $activity ) {
 292      // Are mentions disabled?
 293      if ( ! bp_activity_do_mentions() ) {
 294          return;
 295      }
 296  
 297      // If activity was marked as spam, stop the rest of this function.
 298      if ( ! empty( $activity->is_spam ) )
 299          return;
 300  
 301      // Try to find mentions.
 302      $usernames = bp_activity_find_mentions( $activity->content );
 303  
 304      // We have mentions!
 305      if ( ! empty( $usernames ) ) {
 306          // Replace @mention text with userlinks.
 307          foreach( (array) $usernames as $user_id => $username ) {
 308              $activity->content = preg_replace( '/(@' . $username . '\b)/', "<a class='bp-suggestions-mention' href='" . bp_core_get_user_domain( $user_id ) . "' rel='nofollow'>@$username</a>", $activity->content );
 309          }
 310  
 311          // Add our hook to send @mention emails after the activity item is saved.
 312          add_action( 'bp_activity_after_save', 'bp_activity_at_name_send_emails' );
 313  
 314          // Temporary variable to avoid having to run bp_activity_find_mentions() again.
 315          buddypress()->activity->mentioned_users = $usernames;
 316      }
 317  }
 318  
 319  /**
 320   * Sends emails and BP notifications for users @-mentioned in an activity item.
 321   *
 322   * @since 1.7.0
 323   *
 324   * @param BP_Activity_Activity $activity The BP_Activity_Activity object.
 325   */
 326  function bp_activity_at_name_send_emails( $activity ) {
 327      // Are mentions disabled?
 328      if ( ! bp_activity_do_mentions() ) {
 329          return;
 330      }
 331  
 332      $bp = buddypress();
 333  
 334      // If our temporary variable doesn't exist, stop now.
 335      if ( empty( $bp->activity->mentioned_users ) )
 336          return;
 337  
 338      // Grab our temporary variable from bp_activity_at_name_filter_updates().
 339      $usernames = $bp->activity->mentioned_users;
 340  
 341      // Get rid of temporary variable.
 342      unset( $bp->activity->mentioned_users );
 343  
 344      // Send @mentions and setup BP notifications.
 345      foreach( (array) $usernames as $user_id => $username ) {
 346  
 347          /**
 348           * Filters BuddyPress' ability to send email notifications for @mentions.
 349           *
 350           * @since 1.6.0
 351           * @since 2.5.0 Introduced `$user_id` and `$activity` parameters.
 352           *
 353           * @param bool                 $value     Whether or not BuddyPress should send a notification to the mentioned users.
 354           * @param array                $usernames Array of users potentially notified.
 355           * @param int                  $user_id   ID of the current user being notified.
 356           * @param BP_Activity_Activity $activity  Activity object.
 357           */
 358          if ( apply_filters( 'bp_activity_at_name_do_notifications', true, $usernames, $user_id, $activity ) ) {
 359              bp_activity_at_message_notification( $activity->id, $user_id );
 360          }
 361  
 362          // Updates mention count for the user.
 363          bp_activity_update_mention_count_for_user( $user_id, $activity->id );
 364      }
 365  }
 366  
 367  /**
 368   * Catch links in activity text so rel=nofollow can be added.
 369   *
 370   * @since 1.2.0
 371   *
 372   * @param string $text Activity text.
 373   * @return string $text Text with rel=nofollow added to any links.
 374   */
 375  function bp_activity_make_nofollow_filter( $text ) {
 376      return preg_replace_callback( '|<a (.+?)>|i', 'bp_activity_make_nofollow_filter_callback', $text );
 377  }
 378  
 379      /**
 380       * Adds `rel="nofollow ugc"` to a link.
 381       *
 382       * @since 1.2.0 Adds the nofollow rel attribute.
 383       * @since 7.0.0 Adds the ugc rel attribute.
 384       *
 385       * @param array $matches Items matched by preg_replace_callback() in bp_activity_make_nofollow_filter().
 386       * @return string $text Link with rel=nofollow added.
 387       */
 388  	function bp_activity_make_nofollow_filter_callback( $matches ) {
 389          $text = $matches[1];
 390  
 391          // The WP `make_clickable()` formatting function is adding the rel="nofollow" attribute.
 392          $text = str_replace( array( ' rel="nofollow"', " rel='nofollow'" ), '', $text );
 393          return "<a $text rel=\"nofollow ugc\">";
 394      }
 395  
 396  /**
 397   * Truncate long activity entries when viewed in activity streams.
 398   *
 399   * This method can only be used inside the Activity loop.
 400   *
 401   * @since 1.5.0
 402   * @since 2.6.0 Added $args parameter.
 403   *
 404   * @param string $text The original activity entry text.
 405   * @param array  $args {
 406   *     Optional parameters. See $options argument of {@link bp_create_excerpt()}
 407   *     for all available parameters.
 408   * }
 409   * @return string $excerpt The truncated text.
 410   */
 411  function bp_activity_truncate_entry( $text, $args = array() ) {
 412      global $activities_template;
 413  
 414      /**
 415       * Provides a filter that lets you choose whether to skip this filter on a per-activity basis.
 416       *
 417       * @since 2.3.0
 418       *
 419       * @param bool $value If true, text should be checked to see if it needs truncating.
 420       */
 421      $maybe_truncate_text = apply_filters(
 422          'bp_activity_maybe_truncate_entry',
 423          isset( $activities_template->activity->type ) && ! in_array( $activities_template->activity->type, array( 'new_blog_post', ), true )
 424      );
 425  
 426      // The full text of the activity update should always show on the single activity screen.
 427      if ( empty( $args['force_truncate'] ) && ( ! $maybe_truncate_text || bp_is_single_activity() ) ) {
 428          return $text;
 429      }
 430  
 431      /**
 432       * Filters the appended text for the activity excerpt.
 433       *
 434       * @since 1.5.0
 435       *
 436       * @param string $value Internationalized "Read more" text.
 437       */
 438      $append_text    = apply_filters( 'bp_activity_excerpt_append_text', __( '[Read more]', 'buddypress' ) );
 439  
 440      $excerpt_length = bp_activity_get_excerpt_length();
 441  
 442      $args = wp_parse_args( $args, array( 'ending' => __( '&hellip;', 'buddypress' ) ) );
 443  
 444      // Run the text through the excerpt function. If it's too short, the original text will be returned.
 445      $excerpt        = bp_create_excerpt( $text, $excerpt_length, $args );
 446  
 447      /*
 448       * If the text returned by bp_create_excerpt() is different from the original text (ie it's
 449       * been truncated), add the "Read More" link. Note that bp_create_excerpt() is stripping
 450       * shortcodes, so we have strip them from the $text before the comparison.
 451       */
 452      if ( strlen( $excerpt ) < strlen( strip_shortcodes( $text ) ) ) {
 453          $id = !empty( $activities_template->activity->current_comment->id ) ? 'acomment-read-more-' . $activities_template->activity->current_comment->id : 'activity-read-more-' . bp_get_activity_id();
 454  
 455          $excerpt = sprintf( '%1$s<span class="activity-read-more" id="%2$s"><a href="%3$s" rel="nofollow">%4$s</a></span>', $excerpt, $id, bp_get_activity_thread_permalink(), $append_text );
 456      }
 457  
 458      /**
 459       * Filters the composite activity excerpt entry.
 460       *
 461       * @since 1.5.0
 462       *
 463       * @param string $excerpt     Excerpt text and markup to be displayed.
 464       * @param string $text        The original activity entry text.
 465       * @param string $append_text The final append text applied.
 466       */
 467      return apply_filters( 'bp_activity_truncate_entry', $excerpt, $text, $append_text );
 468  }
 469  
 470  /**
 471   * Include extra JavaScript dependencies for activity component.
 472   *
 473   * @since 2.0.0
 474   *
 475   * @param array $js_handles The original dependencies.
 476   * @return array $js_handles The new dependencies.
 477   */
 478  function bp_activity_get_js_dependencies( $js_handles = array() ) {
 479      if ( bp_activity_do_heartbeat() ) {
 480          $js_handles[] = 'heartbeat';
 481      }
 482  
 483      return $js_handles;
 484  }
 485  add_filter( 'bp_core_get_js_dependencies', 'bp_activity_get_js_dependencies', 10, 1 );
 486  
 487  /**
 488   * Add a just-posted classes to the most recent activity item.
 489   *
 490   * We use these classes to avoid pagination issues when items are loaded
 491   * dynamically into the activity stream.
 492   *
 493   * @since 2.0.0
 494   *
 495   * @param string $classes Array of classes for most recent activity item.
 496   * @return string $classes
 497   */
 498  function bp_activity_newest_class( $classes = '' ) {
 499      $bp = buddypress();
 500  
 501      if ( ! empty( $bp->activity->last_recorded ) && $bp->activity->last_recorded == bp_get_activity_date_recorded() ) {
 502          $classes .= ' new-update';
 503      }
 504  
 505      $classes .= ' just-posted';
 506      return $classes;
 507  }
 508  
 509  /**
 510   * Check if Activity Heartbeat feature i on to add a timestamp class.
 511   *
 512   * @since 2.0.0
 513   *
 514   * @param string $classes Array of classes for timestamp.
 515   * @return string $classes
 516   */
 517  function bp_activity_timestamp_class( $classes = '' ) {
 518  
 519      if ( ! bp_activity_do_heartbeat() ) {
 520          return $classes;
 521      }
 522  
 523      $activity_date = bp_get_activity_date_recorded();
 524  
 525      if ( empty( $activity_date ) ) {
 526          return $classes;
 527      }
 528  
 529      $classes .= ' date-recorded-' . strtotime( $activity_date );
 530  
 531      return $classes;
 532  }
 533  add_filter( 'bp_get_activity_css_class', 'bp_activity_timestamp_class', 9, 1 );
 534  
 535  /**
 536   * Use WordPress Heartbeat API to check for latest activity update.
 537   *
 538   * @since 2.0.0
 539   *
 540   * @param array $response Array containing Heartbeat API response.
 541   * @param array $data     Array containing data for Heartbeat API response.
 542   * @return array $response
 543   */
 544  function bp_activity_heartbeat_last_recorded( $response = array(), $data = array() ) {
 545      if ( empty( $data['bp_activity_last_recorded'] ) ) {
 546          return $response;
 547      }
 548  
 549      // Use the querystring argument stored in the cookie (to preserve
 550      // filters), but force the offset to get only new items.
 551      $activity_latest_args = bp_parse_args(
 552          bp_ajax_querystring( 'activity' ),
 553          array( 'since' => date( 'Y-m-d H:i:s', $data['bp_activity_last_recorded'] ) ),
 554          'activity_latest_args'
 555      );
 556  
 557      if ( ! empty( $data['bp_activity_last_recorded_search_terms'] ) && empty( $activity_latest_args['search_terms'] ) ) {
 558          $activity_latest_args['search_terms'] = addslashes( $data['bp_activity_last_recorded_search_terms'] );
 559      }
 560  
 561      $newest_activities = array();
 562      $last_activity_recorded = 0;
 563  
 564      // Temporarily add a just-posted class for new activity items.
 565      add_filter( 'bp_get_activity_css_class', 'bp_activity_newest_class', 10, 1 );
 566  
 567      ob_start();
 568      if ( bp_has_activities( $activity_latest_args ) ) {
 569          while ( bp_activities() ) {
 570              bp_the_activity();
 571  
 572              $atime = strtotime( bp_get_activity_date_recorded() );
 573              if ( $last_activity_recorded < $atime ) {
 574                  $last_activity_recorded = $atime;
 575              }
 576  
 577              bp_get_template_part( 'activity/entry' );
 578          }
 579      }
 580  
 581      $newest_activities['activities']    = ob_get_contents();
 582      $newest_activities['last_recorded'] = $last_activity_recorded;
 583      ob_end_clean();
 584  
 585      // Remove the temporary filter.
 586      remove_filter( 'bp_get_activity_css_class', 'bp_activity_newest_class', 10 );
 587  
 588      if ( ! empty( $newest_activities['last_recorded'] ) ) {
 589          $response['bp_activity_newest_activities'] = $newest_activities;
 590      }
 591  
 592      return $response;
 593  }
 594  add_filter( 'heartbeat_received', 'bp_activity_heartbeat_last_recorded', 10, 2 );
 595  add_filter( 'heartbeat_nopriv_received', 'bp_activity_heartbeat_last_recorded', 10, 2 );
 596  
 597  /**
 598   * Set the strings for WP HeartBeat API where needed.
 599   *
 600   * @since 2.0.0
 601   *
 602   * @param array $strings Localized strings.
 603   * @return array $strings
 604   */
 605  function bp_activity_heartbeat_strings( $strings = array() ) {
 606  
 607      if ( ! bp_activity_do_heartbeat() ) {
 608          return $strings;
 609      }
 610  
 611      $global_pulse = 0;
 612  
 613      /**
 614       * Filter that checks whether the global heartbeat settings already exist.
 615       *
 616       * @since 2.0.0
 617       *
 618       * @param array $value Heartbeat settings array.
 619       */
 620      $heartbeat_settings = apply_filters( 'heartbeat_settings', array() );
 621      if ( ! empty( $heartbeat_settings['interval'] ) ) {
 622          // 'Fast' is 5.
 623          $global_pulse = is_numeric( $heartbeat_settings['interval'] ) ? absint( $heartbeat_settings['interval'] ) : 5;
 624      }
 625  
 626      /**
 627       * Filters the pulse frequency to be used for the BuddyPress Activity heartbeat.
 628       *
 629       * @since 2.0.0
 630       *
 631       * @param int $value The frequency in seconds between pulses.
 632       */
 633      $bp_activity_pulse = apply_filters( 'bp_activity_heartbeat_pulse', 15 );
 634  
 635      /**
 636       * Use the global pulse value unless:
 637       * a. the BP-specific value has been specifically filtered, or
 638       * b. it doesn't exist (ie, BP will be the only one using the heartbeat,
 639       *    so we're responsible for enabling it)
 640       */
 641      if ( has_filter( 'bp_activity_heartbeat_pulse' ) || empty( $global_pulse ) ) {
 642          $pulse = $bp_activity_pulse;
 643      } else {
 644          $pulse = $global_pulse;
 645      }
 646  
 647      $strings = array_merge( $strings, array(
 648          'newest' => __( 'Load Newest', 'buddypress' ),
 649          'pulse'  => absint( $pulse ),
 650      ) );
 651  
 652      return $strings;
 653  }
 654  add_filter( 'bp_core_get_js_strings', 'bp_activity_heartbeat_strings', 10, 1 );
 655  
 656  /** Scopes ********************************************************************/
 657  
 658  /**
 659   * Set up activity arguments for use with the 'just-me' scope.
 660   *
 661   * @since 2.2.0
 662   *
 663   * @param array $retval Empty array by default.
 664   * @param array $filter Current activity arguments.
 665   * @return array $retval
 666   */
 667  function bp_activity_filter_just_me_scope( $retval = array(), $filter = array() ) {
 668  
 669      // Determine the user_id.
 670      if ( ! empty( $filter['user_id'] ) ) {
 671          $user_id = $filter['user_id'];
 672      } else {
 673          $user_id = bp_displayed_user_id()
 674              ? bp_displayed_user_id()
 675              : bp_loggedin_user_id();
 676      }
 677  
 678      // Should we show all items regardless of sitewide visibility?
 679      $show_hidden = array();
 680      if ( ! empty( $user_id ) && $user_id !== bp_loggedin_user_id() ) {
 681          $show_hidden = array(
 682              'column' => 'hide_sitewide',
 683              'value'  => 0
 684          );
 685      }
 686  
 687      $retval = array(
 688          'relation' => 'AND',
 689          array(
 690              'column' => 'user_id',
 691              'value'  => $user_id
 692          ),
 693          $show_hidden,
 694  
 695          // Overrides.
 696          'override' => array(
 697              'display_comments' => 'stream',
 698              'filter'           => array( 'user_id' => 0 ),
 699              'show_hidden'      => true
 700          ),
 701      );
 702  
 703      return $retval;
 704  }
 705  add_filter( 'bp_activity_set_just-me_scope_args', 'bp_activity_filter_just_me_scope', 10, 2 );
 706  
 707  /**
 708   * Set up activity arguments for use with the 'favorites' scope.
 709   *
 710   * @since 2.2.0
 711   *
 712   * @param array $retval Empty array by default.
 713   * @param array $filter Current activity arguments.
 714   * @return array $retval
 715   */
 716  function bp_activity_filter_favorites_scope( $retval = array(), $filter = array() ) {
 717  
 718      // Determine the user_id.
 719      if ( ! empty( $filter['user_id'] ) ) {
 720          $user_id = $filter['user_id'];
 721      } else {
 722          $user_id = bp_displayed_user_id()
 723              ? bp_displayed_user_id()
 724              : bp_loggedin_user_id();
 725      }
 726  
 727      // Determine the favorites.
 728      $favs = bp_activity_get_user_favorites( $user_id );
 729      if ( empty( $favs ) ) {
 730          $favs = array( 0 );
 731      }
 732  
 733      // Should we show all items regardless of sitewide visibility?
 734      $show_hidden = array();
 735      if ( ! empty( $user_id ) && ( $user_id !== bp_loggedin_user_id() ) ) {
 736          $show_hidden = array(
 737              'column' => 'hide_sitewide',
 738              'value'  => 0
 739          );
 740      }
 741  
 742      $retval = array(
 743          'relation' => 'AND',
 744          array(
 745              'column'  => 'id',
 746              'compare' => 'IN',
 747              'value'   => (array) $favs
 748          ),
 749          $show_hidden,
 750  
 751          // Overrides.
 752          'override' => array(
 753              'display_comments' => true,
 754              'filter'           => array( 'user_id' => 0 ),
 755              'show_hidden'      => true
 756          ),
 757      );
 758  
 759      return $retval;
 760  }
 761  add_filter( 'bp_activity_set_favorites_scope_args', 'bp_activity_filter_favorites_scope', 10, 2 );
 762  
 763  
 764  /**
 765   * Set up activity arguments for use with the 'favorites' scope.
 766   *
 767   * @since 2.2.0
 768   *
 769   * @param array $retval Empty array by default.
 770   * @param array $filter Current activity arguments.
 771   * @return array $retval
 772   */
 773  function bp_activity_filter_mentions_scope( $retval = array(), $filter = array() ) {
 774  
 775      // Are mentions disabled?
 776      if ( ! bp_activity_do_mentions() ) {
 777          return $retval;
 778      }
 779  
 780      // Determine the user_id.
 781      if ( ! empty( $filter['user_id'] ) ) {
 782          $user_id = $filter['user_id'];
 783      } else {
 784          $user_id = bp_displayed_user_id()
 785              ? bp_displayed_user_id()
 786              : bp_loggedin_user_id();
 787      }
 788  
 789      // Should we show all items regardless of sitewide visibility?
 790      $show_hidden = array();
 791      if ( ! empty( $user_id ) && $user_id !== bp_loggedin_user_id() ) {
 792          $show_hidden = array(
 793              'column' => 'hide_sitewide',
 794              'value'  => 0
 795          );
 796      }
 797  
 798      $retval = array(
 799          'relation' => 'AND',
 800          array(
 801              'column'  => 'content',
 802              'compare' => 'LIKE',
 803  
 804              // Start search at @ symbol and stop search at closing tag delimiter.
 805              'value'   => '@' . bp_activity_get_user_mentionname( $user_id ) . '<'
 806          ),
 807          $show_hidden,
 808  
 809          // Overrides.
 810          'override' => array(
 811              'display_comments' => 'stream',
 812              'filter'           => array( 'user_id' => 0 ),
 813              'show_hidden'      => true
 814          ),
 815      );
 816  
 817      return $retval;
 818  }
 819  add_filter( 'bp_activity_set_mentions_scope_args', 'bp_activity_filter_mentions_scope', 10, 2 );
 820  
 821  /**
 822   * Registers Activity personal data exporter.
 823   *
 824   * @since 4.0.0
 825   * @since 5.0.0 adds an `exporter_bp_friendly_name` param to exporters.
 826   *
 827   * @param array $exporters  An array of personal data exporters.
 828   * @return array An array of personal data exporters.
 829   */
 830  function bp_activity_register_personal_data_exporter( $exporters ) {
 831      $exporters['buddypress-activity'] = array(
 832          'exporter_friendly_name'    => __( 'BuddyPress Activity Data', 'buddypress' ),
 833          'callback'                  => 'bp_activity_personal_data_exporter',
 834          'exporter_bp_friendly_name' => _x( 'Activity Data', 'BuddyPress Activity data exporter friendly name', 'buddypress' ),
 835      );
 836  
 837      return $exporters;
 838  }


Generated: Wed Nov 25 01:01:39 2020 Cross-referenced by PHPXref 0.7.1