[ 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 = bp_parse_args(
 443          $args,
 444          array(
 445              'ending' => __( '&hellip;', 'buddypress' ),
 446          )
 447      );
 448  
 449      // Run the text through the excerpt function. If it's too short, the original text will be returned.
 450      $excerpt        = bp_create_excerpt( $text, $excerpt_length, $args );
 451  
 452      /*
 453       * If the text returned by bp_create_excerpt() is different from the original text (ie it's
 454       * been truncated), add the "Read More" link. Note that bp_create_excerpt() is stripping
 455       * shortcodes, so we have strip them from the $text before the comparison.
 456       */
 457      if ( strlen( $excerpt ) < strlen( strip_shortcodes( $text ) ) ) {
 458          $id = !empty( $activities_template->activity->current_comment->id ) ? 'acomment-read-more-' . $activities_template->activity->current_comment->id : 'activity-read-more-' . bp_get_activity_id();
 459  
 460          $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 );
 461      }
 462  
 463      /**
 464       * Filters the composite activity excerpt entry.
 465       *
 466       * @since 1.5.0
 467       *
 468       * @param string $excerpt     Excerpt text and markup to be displayed.
 469       * @param string $text        The original activity entry text.
 470       * @param string $append_text The final append text applied.
 471       */
 472      return apply_filters( 'bp_activity_truncate_entry', $excerpt, $text, $append_text );
 473  }
 474  
 475  /**
 476   * Include extra JavaScript dependencies for activity component.
 477   *
 478   * @since 2.0.0
 479   *
 480   * @param array $js_handles The original dependencies.
 481   * @return array $js_handles The new dependencies.
 482   */
 483  function bp_activity_get_js_dependencies( $js_handles = array() ) {
 484      if ( bp_activity_do_heartbeat() ) {
 485          $js_handles[] = 'heartbeat';
 486      }
 487  
 488      return $js_handles;
 489  }
 490  add_filter( 'bp_core_get_js_dependencies', 'bp_activity_get_js_dependencies', 10, 1 );
 491  
 492  /**
 493   * Add a just-posted classes to the most recent activity item.
 494   *
 495   * We use these classes to avoid pagination issues when items are loaded
 496   * dynamically into the activity stream.
 497   *
 498   * @since 2.0.0
 499   *
 500   * @param string $classes Array of classes for most recent activity item.
 501   * @return string $classes
 502   */
 503  function bp_activity_newest_class( $classes = '' ) {
 504      $bp = buddypress();
 505  
 506      if ( ! empty( $bp->activity->last_recorded ) && $bp->activity->last_recorded == bp_get_activity_date_recorded() ) {
 507          $classes .= ' new-update';
 508      }
 509  
 510      $classes .= ' just-posted';
 511      return $classes;
 512  }
 513  
 514  /**
 515   * Check if Activity Heartbeat feature i on to add a timestamp class.
 516   *
 517   * @since 2.0.0
 518   *
 519   * @param string $classes Array of classes for timestamp.
 520   * @return string $classes
 521   */
 522  function bp_activity_timestamp_class( $classes = '' ) {
 523  
 524      if ( ! bp_activity_do_heartbeat() ) {
 525          return $classes;
 526      }
 527  
 528      $activity_date = bp_get_activity_date_recorded();
 529  
 530      if ( empty( $activity_date ) ) {
 531          return $classes;
 532      }
 533  
 534      $classes .= ' date-recorded-' . strtotime( $activity_date );
 535  
 536      return $classes;
 537  }
 538  add_filter( 'bp_get_activity_css_class', 'bp_activity_timestamp_class', 9, 1 );
 539  
 540  /**
 541   * Use WordPress Heartbeat API to check for latest activity update.
 542   *
 543   * @since 2.0.0
 544   *
 545   * @param array $response Array containing Heartbeat API response.
 546   * @param array $data     Array containing data for Heartbeat API response.
 547   * @return array $response
 548   */
 549  function bp_activity_heartbeat_last_recorded( $response = array(), $data = array() ) {
 550      if ( empty( $data['bp_activity_last_recorded'] ) ) {
 551          return $response;
 552      }
 553  
 554      // Use the querystring argument stored in the cookie (to preserve
 555      // filters), but force the offset to get only new items.
 556      $activity_latest_args = bp_parse_args(
 557          bp_ajax_querystring( 'activity' ),
 558          array( 'since' => date( 'Y-m-d H:i:s', $data['bp_activity_last_recorded'] ) ),
 559          'activity_latest_args'
 560      );
 561  
 562      if ( ! empty( $data['bp_activity_last_recorded_search_terms'] ) && empty( $activity_latest_args['search_terms'] ) ) {
 563          $activity_latest_args['search_terms'] = addslashes( $data['bp_activity_last_recorded_search_terms'] );
 564      }
 565  
 566      $newest_activities = array();
 567      $last_activity_recorded = 0;
 568  
 569      // Temporarily add a just-posted class for new activity items.
 570      add_filter( 'bp_get_activity_css_class', 'bp_activity_newest_class', 10, 1 );
 571  
 572      ob_start();
 573      if ( bp_has_activities( $activity_latest_args ) ) {
 574          while ( bp_activities() ) {
 575              bp_the_activity();
 576  
 577              $atime = strtotime( bp_get_activity_date_recorded() );
 578              if ( $last_activity_recorded < $atime ) {
 579                  $last_activity_recorded = $atime;
 580              }
 581  
 582              bp_get_template_part( 'activity/entry' );
 583          }
 584      }
 585  
 586      $newest_activities['activities']    = ob_get_contents();
 587      $newest_activities['last_recorded'] = $last_activity_recorded;
 588      ob_end_clean();
 589  
 590      // Remove the temporary filter.
 591      remove_filter( 'bp_get_activity_css_class', 'bp_activity_newest_class', 10 );
 592  
 593      if ( ! empty( $newest_activities['last_recorded'] ) ) {
 594          $response['bp_activity_newest_activities'] = $newest_activities;
 595      }
 596  
 597      return $response;
 598  }
 599  add_filter( 'heartbeat_received', 'bp_activity_heartbeat_last_recorded', 10, 2 );
 600  add_filter( 'heartbeat_nopriv_received', 'bp_activity_heartbeat_last_recorded', 10, 2 );
 601  
 602  /**
 603   * Set the strings for WP HeartBeat API where needed.
 604   *
 605   * @since 2.0.0
 606   *
 607   * @param array $strings Localized strings.
 608   * @return array $strings
 609   */
 610  function bp_activity_heartbeat_strings( $strings = array() ) {
 611  
 612      if ( ! bp_activity_do_heartbeat() ) {
 613          return $strings;
 614      }
 615  
 616      $global_pulse = 0;
 617  
 618      /**
 619       * Filter that checks whether the global heartbeat settings already exist.
 620       *
 621       * @since 2.0.0
 622       *
 623       * @param array $value Heartbeat settings array.
 624       */
 625      $heartbeat_settings = apply_filters( 'heartbeat_settings', array() );
 626      if ( ! empty( $heartbeat_settings['interval'] ) ) {
 627          // 'Fast' is 5.
 628          $global_pulse = is_numeric( $heartbeat_settings['interval'] ) ? absint( $heartbeat_settings['interval'] ) : 5;
 629      }
 630  
 631      /**
 632       * Filters the pulse frequency to be used for the BuddyPress Activity heartbeat.
 633       *
 634       * @since 2.0.0
 635       *
 636       * @param int $value The frequency in seconds between pulses.
 637       */
 638      $bp_activity_pulse = apply_filters( 'bp_activity_heartbeat_pulse', 15 );
 639  
 640      /**
 641       * Use the global pulse value unless:
 642       * a. the BP-specific value has been specifically filtered, or
 643       * b. it doesn't exist (ie, BP will be the only one using the heartbeat,
 644       *    so we're responsible for enabling it)
 645       */
 646      if ( has_filter( 'bp_activity_heartbeat_pulse' ) || empty( $global_pulse ) ) {
 647          $pulse = $bp_activity_pulse;
 648      } else {
 649          $pulse = $global_pulse;
 650      }
 651  
 652      $strings = array_merge( $strings, array(
 653          'newest' => __( 'Load Newest', 'buddypress' ),
 654          'pulse'  => absint( $pulse ),
 655      ) );
 656  
 657      return $strings;
 658  }
 659  add_filter( 'bp_core_get_js_strings', 'bp_activity_heartbeat_strings', 10, 1 );
 660  
 661  /** Scopes ********************************************************************/
 662  
 663  /**
 664   * Set up activity arguments for use with the 'just-me' scope.
 665   *
 666   * @since 2.2.0
 667   *
 668   * @param array $retval Empty array by default.
 669   * @param array $filter Current activity arguments.
 670   * @return array $retval
 671   */
 672  function bp_activity_filter_just_me_scope( $retval = array(), $filter = array() ) {
 673  
 674      // Determine the user_id.
 675      if ( ! empty( $filter['user_id'] ) ) {
 676          $user_id = $filter['user_id'];
 677      } else {
 678          $user_id = bp_displayed_user_id()
 679              ? bp_displayed_user_id()
 680              : bp_loggedin_user_id();
 681      }
 682  
 683      // Should we show all items regardless of sitewide visibility?
 684      $show_hidden = array();
 685      if ( ! empty( $user_id ) && $user_id !== bp_loggedin_user_id() ) {
 686          $show_hidden = array(
 687              'column' => 'hide_sitewide',
 688              'value'  => 0
 689          );
 690      }
 691  
 692      $retval = array(
 693          'relation' => 'AND',
 694          array(
 695              'column' => 'user_id',
 696              'value'  => $user_id
 697          ),
 698          $show_hidden,
 699  
 700          // Overrides.
 701          'override' => array(
 702              'display_comments' => 'stream',
 703              'filter'           => array( 'user_id' => 0 ),
 704              'show_hidden'      => true
 705          ),
 706      );
 707  
 708      return $retval;
 709  }
 710  add_filter( 'bp_activity_set_just-me_scope_args', 'bp_activity_filter_just_me_scope', 10, 2 );
 711  
 712  /**
 713   * Set up activity arguments for use with the 'favorites' scope.
 714   *
 715   * @since 2.2.0
 716   *
 717   * @param array $retval Empty array by default.
 718   * @param array $filter Current activity arguments.
 719   * @return array $retval
 720   */
 721  function bp_activity_filter_favorites_scope( $retval = array(), $filter = array() ) {
 722  
 723      // Determine the user_id.
 724      if ( ! empty( $filter['user_id'] ) ) {
 725          $user_id = $filter['user_id'];
 726      } else {
 727          $user_id = bp_displayed_user_id()
 728              ? bp_displayed_user_id()
 729              : bp_loggedin_user_id();
 730      }
 731  
 732      // Determine the favorites.
 733      $favs = bp_activity_get_user_favorites( $user_id );
 734      if ( empty( $favs ) ) {
 735          $favs = array( 0 );
 736      }
 737  
 738      // Should we show all items regardless of sitewide visibility?
 739      $show_hidden = array();
 740      if ( ! empty( $user_id ) && ( $user_id !== bp_loggedin_user_id() ) ) {
 741          $show_hidden = array(
 742              'column' => 'hide_sitewide',
 743              'value'  => 0
 744          );
 745      }
 746  
 747      $retval = array(
 748          'relation' => 'AND',
 749          array(
 750              'column'  => 'id',
 751              'compare' => 'IN',
 752              'value'   => (array) $favs
 753          ),
 754          $show_hidden,
 755  
 756          // Overrides.
 757          'override' => array(
 758              'display_comments' => true,
 759              'filter'           => array( 'user_id' => 0 ),
 760              'show_hidden'      => true
 761          ),
 762      );
 763  
 764      return $retval;
 765  }
 766  add_filter( 'bp_activity_set_favorites_scope_args', 'bp_activity_filter_favorites_scope', 10, 2 );
 767  
 768  
 769  /**
 770   * Set up activity arguments for use with the 'favorites' scope.
 771   *
 772   * @since 2.2.0
 773   *
 774   * @param array $retval Empty array by default.
 775   * @param array $filter Current activity arguments.
 776   * @return array $retval
 777   */
 778  function bp_activity_filter_mentions_scope( $retval = array(), $filter = array() ) {
 779  
 780      // Are mentions disabled?
 781      if ( ! bp_activity_do_mentions() ) {
 782          return $retval;
 783      }
 784  
 785      // Determine the user_id.
 786      if ( ! empty( $filter['user_id'] ) ) {
 787          $user_id = $filter['user_id'];
 788      } else {
 789          $user_id = bp_displayed_user_id()
 790              ? bp_displayed_user_id()
 791              : bp_loggedin_user_id();
 792      }
 793  
 794      // Should we show all items regardless of sitewide visibility?
 795      $show_hidden = array();
 796      if ( ! empty( $user_id ) && $user_id !== bp_loggedin_user_id() ) {
 797          $show_hidden = array(
 798              'column' => 'hide_sitewide',
 799              'value'  => 0
 800          );
 801      }
 802  
 803      $retval = array(
 804          'relation' => 'AND',
 805          array(
 806              'column'  => 'content',
 807              'compare' => 'LIKE',
 808  
 809              // Start search at @ symbol and stop search at closing tag delimiter.
 810              'value'   => '@' . bp_activity_get_user_mentionname( $user_id ) . '<'
 811          ),
 812          $show_hidden,
 813  
 814          // Overrides.
 815          'override' => array(
 816              'display_comments' => 'stream',
 817              'filter'           => array( 'user_id' => 0 ),
 818              'show_hidden'      => true
 819          ),
 820      );
 821  
 822      return $retval;
 823  }
 824  add_filter( 'bp_activity_set_mentions_scope_args', 'bp_activity_filter_mentions_scope', 10, 2 );
 825  
 826  /**
 827   * Registers Activity personal data exporter.
 828   *
 829   * @since 4.0.0
 830   * @since 5.0.0 adds an `exporter_bp_friendly_name` param to exporters.
 831   *
 832   * @param array $exporters  An array of personal data exporters.
 833   * @return array An array of personal data exporters.
 834   */
 835  function bp_activity_register_personal_data_exporter( $exporters ) {
 836      $exporters['buddypress-activity'] = array(
 837          'exporter_friendly_name'    => __( 'BuddyPress Activity Data', 'buddypress' ),
 838          'callback'                  => 'bp_activity_personal_data_exporter',
 839          'exporter_bp_friendly_name' => _x( 'Activity Data', 'BuddyPress Activity data exporter friendly name', 'buddypress' ),
 840      );
 841  
 842      return $exporters;
 843  }


Generated: Sat Apr 27 01:00:55 2024 Cross-referenced by PHPXref 0.7.1