[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * BuddyPress Groups Activity Functions.
   4   *
   5   * These functions handle the recording, deleting and formatting of activity
   6   * for the user and for this specific component.
   7   *
   8   * @package BuddyPress
   9   * @subpackage GroupsActivity
  10   * @since 1.5.0
  11   */
  12  
  13  // Exit if accessed directly.
  14  defined( 'ABSPATH' ) || exit;
  15  
  16  /**
  17   * Register activity actions for the Groups component.
  18   *
  19   * @since 1.1.0
  20   *
  21   * @return false|null False on failure.
  22   */
  23  function groups_register_activity_actions() {
  24      $bp = buddypress();
  25  
  26      if ( ! bp_is_active( 'activity' ) ) {
  27          return false;
  28      }
  29  
  30      bp_activity_set_action(
  31          $bp->groups->id,
  32          'created_group',
  33          __( 'Created a group', 'buddypress' ),
  34          'bp_groups_format_activity_action_created_group',
  35          __( 'New Groups', 'buddypress' ),
  36          array( 'activity', 'member', 'member_groups' )
  37      );
  38  
  39      bp_activity_set_action(
  40          $bp->groups->id,
  41          'joined_group',
  42          __( 'Joined a group', 'buddypress' ),
  43          'bp_groups_format_activity_action_joined_group',
  44          __( 'Group Memberships', 'buddypress' ),
  45          array( 'activity', 'group', 'member', 'member_groups' )
  46      );
  47  
  48      bp_activity_set_action(
  49          $bp->groups->id,
  50          'group_details_updated',
  51          __( 'Group details edited', 'buddypress' ),
  52          'bp_groups_format_activity_action_group_details_updated',
  53          __( 'Group Updates', 'buddypress' ),
  54          array( 'activity', 'group', 'member', 'member_groups' )
  55      );
  56  
  57      bp_activity_set_action(
  58          $bp->groups->id,
  59          'activity_update',
  60          __( 'Posted a status update in a Group', 'buddypress' ),
  61          'bp_groups_format_activity_action_group_activity_update',
  62          __( 'Group Activity Updates', 'buddypress' ),
  63          array( 'activity', 'group', 'member', 'member_groups' )
  64      );
  65  
  66      /**
  67       * Fires at end of registration of the default activity actions for the Groups component.
  68       *
  69       * @since 1.1.0
  70       */
  71      do_action( 'groups_register_activity_actions' );
  72  }
  73  add_action( 'bp_register_activity_actions', 'groups_register_activity_actions' );
  74  
  75  /**
  76   * Get the group object the activity belongs to.
  77   *
  78   * @since 5.0.0
  79   *
  80   * @param integer $group_id The group ID the activity is linked to.
  81   * @return BP_Groups_Group  The group object the activity belongs to.
  82   */
  83  function bp_groups_get_activity_group( $group_id = 0 ) {
  84      // If displaying a specific group, check the activity belongs to it.
  85      if ( bp_is_group() && bp_get_current_group_id() === (int) $group_id ) {
  86          $group = groups_get_current_group();
  87  
  88          // Otherwise get the group the activity belongs to.
  89      } else {
  90          $group = groups_get_group( $group_id );
  91      }
  92  
  93      return $group;
  94  }
  95  
  96  /**
  97   * Format 'created_group' activity actions.
  98   *
  99   * @since 2.0.0
 100   *
 101   * @param string $action   Static activity action.
 102   * @param object $activity Activity data object.
 103   * @return string
 104   */
 105  function bp_groups_format_activity_action_created_group( $action, $activity ) {
 106      $user_link = bp_core_get_userlink( $activity->user_id );
 107  
 108      $group      = bp_groups_get_activity_group( $activity->item_id );
 109      $group_link = '<a href="' . esc_url( bp_get_group_permalink( $group ) ) . '">' . esc_html( $group->name ) . '</a>';
 110  
 111      $action = sprintf( esc_html__( '%1$s created the group %2$s', 'buddypress'), $user_link, $group_link );
 112  
 113      /**
 114       * Filters the 'created_group' activity actions.
 115       *
 116       * @since 1.2.0
 117       *
 118       * @param string $action   The 'created_group' activity action.
 119       * @param object $activity Activity data object.
 120       */
 121      return apply_filters( 'groups_activity_created_group_action', $action, $activity );
 122  }
 123  
 124  /**
 125   * Format 'joined_group' activity actions.
 126   *
 127   * @since 2.0.0
 128   *
 129   * @param string $action   Static activity action.
 130   * @param object $activity Activity data object.
 131   * @return string
 132   */
 133  function bp_groups_format_activity_action_joined_group( $action, $activity ) {
 134      $user_link = bp_core_get_userlink( $activity->user_id );
 135  
 136      $group      = bp_groups_get_activity_group( $activity->item_id );
 137      $group_link = '<a href="' . esc_url( bp_get_group_permalink( $group ) ) . '">' . esc_html( $group->name ) . '</a>';
 138  
 139      $action = sprintf( esc_html__( '%1$s joined the group %2$s', 'buddypress' ), $user_link, $group_link );
 140  
 141      // Legacy filters (do not follow parameter patterns of other activity
 142      // action filters, and requires apply_filters_ref_array()).
 143      if ( has_filter( 'groups_activity_membership_accepted_action' ) ) {
 144          $action = apply_filters_ref_array( 'groups_activity_membership_accepted_action', array( $action, $user_link, &$group ) );
 145      }
 146  
 147      // Another legacy filter.
 148      if ( has_filter( 'groups_activity_accepted_invite_action' ) ) {
 149          $action = apply_filters_ref_array( 'groups_activity_accepted_invite_action', array( $action, $activity->user_id, &$group ) );
 150      }
 151  
 152      /**
 153       * Filters the 'joined_group' activity actions.
 154       *
 155       * @since 2.0.0
 156       *
 157       * @param string $action   The 'joined_group' activity actions.
 158       * @param object $activity Activity data object.
 159       */
 160      return apply_filters( 'bp_groups_format_activity_action_joined_group', $action, $activity );
 161  }
 162  
 163  /**
 164   * Format 'group_details_updated' activity actions.
 165   *
 166   * @since 2.2.0
 167   *
 168   * @param  string $action   Static activity action.
 169   * @param  object $activity Activity data object.
 170   * @return string
 171   */
 172  function bp_groups_format_activity_action_group_details_updated( $action, $activity ) {
 173      $user_link = bp_core_get_userlink( $activity->user_id );
 174  
 175      $group      = bp_groups_get_activity_group( $activity->item_id );
 176      $group_link = '<a href="' . esc_url( bp_get_group_permalink( $group ) ) . '">' . esc_html( $group->name ) . '</a>';
 177  
 178      /*
 179       * Changed group details are stored in groupmeta, keyed by the activity
 180       * timestamp. See {@link bp_groups_group_details_updated_add_activity()}.
 181       */
 182      $changed = groups_get_groupmeta( $activity->item_id, 'updated_details_' . $activity->date_recorded );
 183  
 184      // No changed details were found, so use a generic message.
 185      if ( empty( $changed ) ) {
 186          $action = sprintf( esc_html__( '%1$s updated details for the group %2$s', 'buddypress' ), $user_link, $group_link );
 187  
 188      // Name and description changed - to keep things short, don't describe changes in detail.
 189      } elseif ( isset( $changed['name'] ) && isset( $changed['description'] ) ) {
 190          $action = sprintf( esc_html__( '%1$s changed the name and description of the group %2$s', 'buddypress' ), $user_link, $group_link );
 191  
 192      // Name only.
 193      } elseif ( ! empty( $changed['name']['old'] ) && ! empty( $changed['name']['new'] ) ) {
 194          $action = sprintf( esc_html__( '%1$s changed the name of the group %2$s from "%3$s" to "%4$s"', 'buddypress' ), $user_link, $group_link, esc_html( $changed['name']['old'] ), esc_html( $changed['name']['new'] ) );
 195  
 196      // Description only.
 197      } elseif ( ! empty( $changed['description']['old'] ) && ! empty( $changed['description']['new'] ) ) {
 198          $action = sprintf( esc_html__( '%1$s changed the description of the group %2$s from "%3$s" to "%4$s"', 'buddypress' ), $user_link, $group_link, esc_html( $changed['description']['old'] ), esc_html( $changed['description']['new'] ) );
 199  
 200      } elseif ( ! empty( $changed['slug']['old'] ) && ! empty( $changed['slug']['new'] ) ) {
 201          $action = sprintf( esc_html__( '%1$s changed the permalink of the group %2$s.', 'buddypress' ), $user_link, $group_link );
 202  
 203      }
 204  
 205      /**
 206       * Filters the 'group_details_updated' activity actions.
 207       *
 208       * @since 2.0.0
 209       *
 210       * @param string $action   The 'group_details_updated' activity actions.
 211       * @param object $activity Activity data object.
 212       */
 213      return apply_filters( 'bp_groups_format_activity_action_joined_group', $action, $activity );
 214  }
 215  
 216  /**
 217   * Format the action for activity updates posted in a Group.
 218   *
 219   * @since 5.0.0
 220   *
 221   * @param string $action   Static activity action.
 222   * @param object $activity Activity data object.
 223   * @return string          The formatted action for activity updates posted in a Group.
 224   */
 225  function bp_groups_format_activity_action_group_activity_update( $action, $activity ) {
 226      $user_link = bp_core_get_userlink( $activity->user_id );
 227      $group     = bp_groups_get_activity_group( $activity->item_id );
 228  
 229      $group_link = '<a href="' . esc_url( bp_get_group_permalink( $group ) ) . '">' . esc_html( $group->name ) . '</a>';
 230  
 231      // Set the Activity update posted in a Group action.
 232      $action = sprintf( esc_html__( '%1$s posted an update in the group %2$s', 'buddypress' ), $user_link, $group_link );
 233  
 234      /** This filter is documented in wp-includes/deprecated.php */
 235      $action = apply_filters_deprecated( 'groups_activity_new_update_action', array( $action ), '5.0.0', 'bp_groups_format_activity_action_group_activity_update' );
 236  
 237      /**
 238       * Filters the Group's activity update action.
 239       *
 240       * @since 5.0.0
 241       *
 242       * @param string $action   The Group's activity update action.
 243       * @param object $activity Activity data object.
 244       */
 245      return apply_filters( 'bp_groups_format_activity_action_group_activity_update', $action, $activity );
 246  }
 247  
 248  /**
 249   * Fetch data related to groups at the beginning of an activity loop.
 250   *
 251   * This reduces database overhead during the activity loop.
 252   *
 253   * @since 2.0.0
 254   *
 255   * @param array $activities Array of activity items.
 256   * @return array
 257   */
 258  function bp_groups_prefetch_activity_object_data( $activities ) {
 259      $group_ids = array();
 260  
 261      if ( empty( $activities ) ) {
 262          return $activities;
 263      }
 264  
 265      foreach ( $activities as $activity ) {
 266          if ( buddypress()->groups->id !== $activity->component ) {
 267              continue;
 268          }
 269  
 270          $group_ids[] = $activity->item_id;
 271      }
 272  
 273      if ( ! empty( $group_ids ) ) {
 274  
 275          // TEMPORARY - Once the 'populate_extras' issue is solved
 276          // in the groups component, we can do this with groups_get_groups()
 277          // rather than manually.
 278          $uncached_ids = array();
 279          foreach ( $group_ids as $group_id ) {
 280              if ( false === wp_cache_get( $group_id, 'bp_groups' ) ) {
 281                  $uncached_ids[] = $group_id;
 282              }
 283          }
 284  
 285          if ( ! empty( $uncached_ids ) ) {
 286              global $wpdb;
 287              $bp = buddypress();
 288              $uncached_ids_sql = implode( ',', wp_parse_id_list( $uncached_ids ) );
 289              $groups = $wpdb->get_results( "SELECT * FROM {$bp->groups->table_name} WHERE id IN ({$uncached_ids_sql})" );
 290              foreach ( $groups as $group ) {
 291                  wp_cache_set( $group->id, $group, 'bp_groups' );
 292              }
 293          }
 294      }
 295  
 296      return $activities;
 297  }
 298  add_filter( 'bp_activity_prefetch_object_data', 'bp_groups_prefetch_activity_object_data' );
 299  
 300  /**
 301   * Set up activity arguments for use with the 'groups' scope.
 302   *
 303   * @since 2.2.0
 304   *
 305   * @param array $retval Empty array by default.
 306   * @param array $filter Current activity arguments.
 307   * @return array
 308   */
 309  function bp_groups_filter_activity_scope( $retval = array(), $filter = array() ) {
 310  
 311      // Determine the user_id.
 312      if ( ! empty( $filter['user_id'] ) ) {
 313          $user_id = $filter['user_id'];
 314      } else {
 315          $user_id = bp_displayed_user_id()
 316              ? bp_displayed_user_id()
 317              : bp_loggedin_user_id();
 318      }
 319  
 320      // Determine groups of user.
 321      $groups = groups_get_user_groups( $user_id );
 322      if ( empty( $groups['groups'] ) ) {
 323          $groups = array( 'groups' => 0 );
 324      }
 325  
 326      // Should we show all items regardless of sitewide visibility?
 327      $show_hidden = array();
 328      if ( ! empty( $user_id ) && ( $user_id !== bp_loggedin_user_id() ) ) {
 329          $show_hidden = array(
 330              'column' => 'hide_sitewide',
 331              'value'  => 0
 332          );
 333      }
 334  
 335      $retval = array(
 336          'relation' => 'AND',
 337          array(
 338              'relation' => 'AND',
 339              array(
 340                  'column' => 'component',
 341                  'value'  => buddypress()->groups->id
 342              ),
 343              array(
 344                  'column'  => 'item_id',
 345                  'compare' => 'IN',
 346                  'value'   => (array) $groups['groups']
 347              ),
 348          ),
 349          $show_hidden,
 350  
 351          // Overrides.
 352          'override' => array(
 353              'filter'      => array( 'user_id' => 0 ),
 354              'show_hidden' => true
 355          ),
 356      );
 357  
 358      return $retval;
 359  }
 360  add_filter( 'bp_activity_set_groups_scope_args', 'bp_groups_filter_activity_scope', 10, 2 );
 361  
 362  /**
 363   * Enforces group membership restrictions on activity favorite queries.
 364   *
 365   * @since 4.3.0
 366  
 367   * @param array $retval Query arguments.
 368   * @param array $filter
 369   * @return array
 370   */
 371  function bp_groups_filter_activity_favorites_scope( $retval, $filter ) {
 372      // Only process for viewers looking at their own favorites feed.
 373      if ( ! empty( $filter['user_id'] ) ) {
 374          $user_id = (int) $filter['user_id'];
 375      } else {
 376          $user_id = bp_displayed_user_id() ? bp_displayed_user_id() : bp_loggedin_user_id();
 377      }
 378  
 379      if ( ! $user_id || ! is_user_logged_in() || $user_id !== bp_loggedin_user_id() ) {
 380          return $retval;
 381      }
 382  
 383      $favs = bp_activity_get_user_favorites( $user_id );
 384      if ( empty( $favs ) ) {
 385          return $retval;
 386      }
 387  
 388      $user_groups = bp_get_user_groups(
 389          $user_id,
 390          array(
 391              'is_admin' => null,
 392              'is_mod'   => null,
 393          )
 394      );
 395  
 396      $retval = array(
 397          'relation' => 'OR',
 398  
 399          // Allow hidden items for items unconnected to groups.
 400          'non_groups' => array(
 401              'relation' => 'AND',
 402              array(
 403                  'column'  => 'component',
 404                  'compare' => '!=',
 405                  'value'   => buddypress()->groups->id,
 406              ),
 407              array(
 408                  'column'  => 'hide_sitewide',
 409                  'compare' => 'IN',
 410                  'value'   => array( 1, 0 ),
 411              ),
 412              array(
 413                  'column'  => 'id',
 414                  'compare' => 'IN',
 415                  'value'   => $favs,
 416              ),
 417          ),
 418  
 419          // Trust the favorites list for group items that are not hidden sitewide.
 420          'non_hidden_groups' => array(
 421              'relation' => 'AND',
 422              array(
 423                  'column'  => 'component',
 424                  'compare' => '=',
 425                  'value'   => buddypress()->groups->id,
 426              ),
 427              array(
 428                  'column'  => 'hide_sitewide',
 429                  'compare' => '=',
 430                  'value'   => 0,
 431              ),
 432              array(
 433                  'column'  => 'id',
 434                  'compare' => 'IN',
 435                  'value'   => $favs,
 436              ),
 437          ),
 438  
 439          // For hidden group items, limit to those in the user's groups.
 440          'hidden_groups' => array(
 441              'relation' => 'AND',
 442              array(
 443                  'column'  => 'component',
 444                  'compare' => '=',
 445                  'value'   => buddypress()->groups->id,
 446              ),
 447              array(
 448                  'column'  => 'hide_sitewide',
 449                  'compare' => '=',
 450                  'value'   => 1,
 451              ),
 452              array(
 453                  'column'  => 'id',
 454                  'compare' => 'IN',
 455                  'value'   => $favs,
 456              ),
 457              array(
 458                  'column'  => 'item_id',
 459                  'compare' => 'IN',
 460                  'value'   => wp_list_pluck( $user_groups, 'group_id' ),
 461              ),
 462          ),
 463  
 464          'override' => array(
 465              'display_comments' => true,
 466              'filter'           => array( 'user_id' => 0 ),
 467              'show_hidden'      => true,
 468          ),
 469      );
 470  
 471      return $retval;
 472  }
 473  add_filter( 'bp_activity_set_favorites_scope_args', 'bp_groups_filter_activity_favorites_scope', 20, 2 );
 474  
 475  /**
 476   * Record an activity item related to the Groups component.
 477   *
 478   * A wrapper for {@link bp_activity_add()} that provides some Groups-specific
 479   * defaults.
 480   *
 481   * @since 1.0.0
 482   *
 483   * @see bp_activity_add() for more detailed description of parameters and
 484   *      return values.
 485   *
 486   * @param array|string $args {
 487   *     An array of arguments for the new activity item. Accepts all parameters
 488   *     of {@link bp_activity_add()}. However, this wrapper provides some
 489   *     additional defaults, as described below:
 490   *     @type string $component     Default: the id of your Groups component
 491   *                                 (usually 'groups').
 492   *     @type bool   $hide_sitewide Default: True if the current group is not
 493   *                                 public, otherwise false.
 494   * }
 495   * @return WP_Error|bool|int See {@link bp_activity_add()}.
 496   */
 497  function groups_record_activity( $args = '' ) {
 498  
 499      if ( ! bp_is_active( 'activity' ) ) {
 500          return false;
 501      }
 502  
 503      // Set the default for hide_sitewide by checking the status of the group.
 504      $hide_sitewide = false;
 505      if ( ! empty( $args['item_id'] ) ) {
 506          $group = bp_groups_get_activity_group( $args['item_id'] );
 507  
 508          if ( isset( $group->status ) && 'public' != $group->status ) {
 509              $hide_sitewide = true;
 510          }
 511      }
 512  
 513      $r = bp_parse_args( $args, array(
 514          'id'                => false,
 515          'user_id'           => bp_loggedin_user_id(),
 516          'action'            => '',
 517          'content'           => '',
 518          'primary_link'      => '',
 519          'component'         => buddypress()->groups->id,
 520          'type'              => false,
 521          'item_id'           => false,
 522          'secondary_item_id' => false,
 523          'recorded_time'     => bp_core_current_time(),
 524          'hide_sitewide'     => $hide_sitewide,
 525          'error_type'        => 'bool'
 526      ), 'groups_record_activity' );
 527  
 528      return bp_activity_add( $r );
 529  }
 530  
 531  /**
 532   * Post an Activity status update affiliated with a group.
 533   *
 534   * @since 1.2.0
 535   * @since 2.6.0 Added 'error_type' parameter to $args.
 536   *
 537   * @param array|string $args {
 538   *     Array of arguments.
 539   *     @type string $content  The content of the update.
 540   *     @type int    $user_id  Optional. ID of the user posting the update. Default:
 541   *                            ID of the logged-in user.
 542   *     @type int    $group_id Optional. ID of the group to be affiliated with the
 543   *                            update. Default: ID of the current group.
 544   * }
 545   * @return WP_Error|bool|int Returns the ID of the new activity item on success, or false on failure.
 546   */
 547  function groups_post_update( $args = '' ) {
 548      $bp = buddypress();
 549  
 550      $r = bp_parse_args( $args, array(
 551          'content'    => false,
 552          'user_id'    => bp_loggedin_user_id(),
 553          'group_id'   => 0,
 554          'error_type' => 'bool'
 555      ), 'groups_post_update' );
 556  
 557      $group_id = (int) $r['group_id'];
 558      if ( ! $group_id && ! empty( $bp->groups->current_group->id ) ) {
 559          $group_id = (int) $bp->groups->current_group->id;
 560      }
 561  
 562      $content = $r['content'];
 563      $user_id = (int) $r['user_id'];
 564      if ( ! $content || ! strlen( trim( $content ) ) || ! $user_id || ! $group_id ) {
 565          return false;
 566      }
 567  
 568      $bp->groups->current_group = groups_get_group( $group_id );
 569  
 570      // Be sure the user is a member of the group before posting.
 571      if ( ! bp_current_user_can( 'bp_moderate' ) && ! groups_is_user_member( $user_id, $group_id ) ) {
 572          return false;
 573      }
 574  
 575      /**
 576       * Filters the content for the new group activity update.
 577       *
 578       * @since 1.2.0
 579       *
 580       * @param string $content The content of the update.
 581       */
 582      $content_filtered = apply_filters( 'groups_activity_new_update_content', $content );
 583  
 584      $activity_id = groups_record_activity( array(
 585          'user_id'    => $user_id,
 586          'content'    => $content_filtered,
 587          'type'       => 'activity_update',
 588          'item_id'    => $group_id,
 589          'error_type' => $r['error_type'],
 590      ) );
 591  
 592      groups_update_groupmeta( $group_id, 'last_activity', bp_core_current_time() );
 593  
 594      /**
 595       * Fires after posting of an Activity status update affiliated with a group.
 596       *
 597       * @since 1.2.0
 598       *
 599       * @param string $content     The content of the update.
 600       * @param int    $user_id     ID of the user posting the update.
 601       * @param int    $group_id    ID of the group being posted to.
 602       * @param bool   $activity_id Whether or not the activity recording succeeded.
 603       */
 604      do_action( 'bp_groups_posted_update', $content, $user_id, $group_id, $activity_id );
 605  
 606      return $activity_id;
 607  }
 608  
 609  /**
 610   * Function used to determine if a user can comment on a group activity item.
 611   *
 612   * Used as a filter callback to 'bp_activity_can_comment'.
 613   *
 614   * @since 3.0.0
 615   *
 616   * @param  bool                      $retval   True if item can receive comments.
 617   * @param  null|BP_Activity_Activity $activity Null by default. Pass an activity object to check against that instead.
 618   * @return bool
 619   */
 620  function bp_groups_filter_activity_can_comment( $retval, $activity = null ) {
 621      // Bail if item cannot receive comments or if no current user.
 622      if ( empty( $retval ) || ! is_user_logged_in() ) {
 623          return $retval;
 624      }
 625  
 626      // Use passed activity object, if available.
 627      if ( is_a( $activity, 'BP_Activity_Activity' ) ) {
 628          $component = $activity->component;
 629          $group_id  = $activity->item_id;
 630  
 631      // Use activity info from current activity item in the loop.
 632      } else {
 633          $component = bp_get_activity_object_name();
 634          $group_id  = bp_get_activity_item_id();
 635      }
 636  
 637      // If not a group activity item, bail.
 638      if ( 'groups' !== $component ) {
 639          return $retval;
 640      }
 641  
 642      // If current user is not a group member or is banned, user cannot comment.
 643      if ( ! bp_current_user_can( 'bp_moderate' ) &&
 644          ( ! groups_is_user_member( bp_loggedin_user_id(), $group_id ) || groups_is_user_banned( bp_loggedin_user_id(), $group_id ) )
 645      ) {
 646          $retval = false;
 647      }
 648  
 649      return $retval;
 650  }
 651  add_filter( 'bp_activity_can_comment', 'bp_groups_filter_activity_can_comment', 99, 1 );
 652  
 653  /**
 654   * Function used to determine if a user can reply on a group activity comment.
 655   *
 656   * Used as a filter callback to 'bp_activity_can_comment_reply'.
 657   *
 658   * @since 3.0.0
 659   *
 660   * @param  bool        $retval  True if activity comment can be replied to.
 661   * @param  object|bool $comment Current activity comment object. If empty, parameter is boolean false.
 662   * @return bool
 663   */
 664  function bp_groups_filter_activity_can_comment_reply( $retval, $comment ) {
 665      // Bail if no current user, if comment is empty or if retval is already empty.
 666      if ( ! is_user_logged_in() || empty( $comment ) || empty( $retval ) ) {
 667          return $retval;
 668      }
 669  
 670      // Grab parent activity item.
 671      $parent = new BP_Activity_Activity( $comment->item_id );
 672  
 673      // Check to see if user can reply to parent group activity item.
 674      return bp_groups_filter_activity_can_comment( $retval, $parent );
 675  }
 676  add_filter( 'bp_activity_can_comment_reply', 'bp_groups_filter_activity_can_comment_reply', 99, 2 );
 677  
 678  /**
 679   * Add an activity stream item when a member joins a group.
 680   *
 681   * @since 1.9.0
 682   *
 683   * @param int $user_id  ID of the user joining the group.
 684   * @param int $group_id ID of the group.
 685   * @return false|null False on failure.
 686   */
 687  function bp_groups_membership_accepted_add_activity( $user_id, $group_id ) {
 688  
 689      // Bail if Activity is not active.
 690      if ( ! bp_is_active( 'activity' ) ) {
 691          return false;
 692      }
 693  
 694      // Get the group so we can get it's name.
 695      $group = groups_get_group( $group_id );
 696  
 697      /**
 698       * Filters the 'membership_accepted' activity actions.
 699       *
 700       * @since 1.2.0
 701       *
 702       * @param string $value    The 'membership_accepted' activity action.
 703       * @param int    $user_id  ID of the user joining the group.
 704       * @param int    $group_id ID of the group. Passed by reference.
 705       */
 706      $action = apply_filters_ref_array( 'groups_activity_membership_accepted_action', array( sprintf( __( '%1$s joined the group %2$s', 'buddypress' ), bp_core_get_userlink( $user_id ), '<a href="' . bp_get_group_permalink( $group ) . '">' . esc_attr( $group->name ) . '</a>' ), $user_id, &$group ) );
 707  
 708      // Record in activity streams.
 709      groups_record_activity( array(
 710          'action'  => $action,
 711          'type'    => 'joined_group',
 712          'item_id' => $group_id,
 713          'user_id' => $user_id
 714      ) );
 715  }
 716  add_action( 'groups_membership_accepted', 'bp_groups_membership_accepted_add_activity', 10, 2 );
 717  
 718  /**
 719   * Add an activity item when a group's details are updated.
 720   *
 721   * @since 2.2.0
 722   *
 723   * @param  int             $group_id       ID of the group.
 724   * @param  BP_Groups_Group $old_group      Group object before the details had been changed.
 725   * @param  bool            $notify_members True if the admin has opted to notify group members, otherwise false.
 726   * @return null|WP_Error|bool|int The ID of the activity on success. False on error.
 727   */
 728  function bp_groups_group_details_updated_add_activity( $group_id, $old_group, $notify_members ) {
 729  
 730      // Bail if Activity is not active.
 731      if ( ! bp_is_active( 'activity' ) ) {
 732          return false;
 733      }
 734  
 735      if ( ! isset( $old_group->name ) || ! isset( $old_group->slug ) || ! isset( $old_group->description ) ) {
 736          return false;
 737      }
 738  
 739      // If the admin has opted not to notify members, don't post an activity item either.
 740      if ( empty( $notify_members ) ) {
 741          return;
 742      }
 743  
 744      $group = groups_get_group( array(
 745          'group_id' => $group_id,
 746      ) );
 747  
 748      /*
 749       * Store the changed data, which will be used to generate the activity
 750       * action. Since we haven't yet created the activity item, we store the
 751       * old group data in groupmeta, keyed by the timestamp that we'll put
 752       * on the activity item.
 753       */
 754      $changed = array();
 755  
 756      if ( $group->name !== $old_group->name ) {
 757          $changed['name'] = array(
 758              'old' => $old_group->name,
 759              'new' => $group->name,
 760          );
 761      }
 762  
 763      if ( $group->slug !== $old_group->slug ) {
 764          $changed['slug'] = array(
 765              'old' => $old_group->slug,
 766              'new' => $group->slug,
 767          );
 768      }
 769  
 770      if ( $group->description !== $old_group->description ) {
 771          $changed['description'] = array(
 772              'old' => $old_group->description,
 773              'new' => $group->description,
 774          );
 775      }
 776  
 777      // If there are no changes, don't post an activity item.
 778      if ( empty( $changed ) ) {
 779          return;
 780      }
 781  
 782      $time = bp_core_current_time();
 783      groups_update_groupmeta( $group_id, 'updated_details_' . $time, $changed );
 784  
 785      // Record in activity streams.
 786      return groups_record_activity( array(
 787          'type'          => 'group_details_updated',
 788          'item_id'       => $group_id,
 789          'user_id'       => bp_loggedin_user_id(),
 790          'recorded_time' => $time,
 791  
 792      ) );
 793  
 794  }
 795  add_action( 'groups_details_updated', 'bp_groups_group_details_updated_add_activity', 10, 3 );
 796  
 797  /**
 798   * Delete all activity items related to a specific group.
 799   *
 800   * @since 1.9.0
 801   *
 802   * @param int $group_id ID of the group.
 803   */
 804  function bp_groups_delete_group_delete_all_activity( $group_id ) {
 805      if ( bp_is_active( 'activity' ) ) {
 806          bp_activity_delete_by_item_id( array(
 807              'item_id'   => $group_id,
 808              'component' => buddypress()->groups->id
 809          ) );
 810      }
 811  }
 812  add_action( 'groups_delete_group', 'bp_groups_delete_group_delete_all_activity', 10 );
 813  
 814  /**
 815   * Delete group member activity if they leave or are removed within 5 minutes of membership modification.
 816   *
 817   * If the user joined this group less than five minutes ago, remove the
 818   * joined_group activity so users cannot flood the activity stream by
 819   * joining/leaving the group in quick succession.
 820   *
 821   * @since 1.9.0
 822   *
 823   * @param int $group_id ID of the group.
 824   * @param int $user_id  ID of the user leaving the group.
 825   */
 826  function bp_groups_leave_group_delete_recent_activity( $group_id, $user_id ) {
 827  
 828      // Bail if Activity component is not active.
 829      if ( ! bp_is_active( 'activity' ) ) {
 830          return;
 831      }
 832  
 833      // Get the member's group membership information.
 834      $membership = new BP_Groups_Member( $user_id, $group_id );
 835  
 836      // Check the time period, and maybe delete their recent group activity.
 837      if ( time() <= strtotime( '+5 minutes', (int) strtotime( $membership->date_modified ) ) ) {
 838          bp_activity_delete( array(
 839              'component' => buddypress()->groups->id,
 840              'type'      => 'joined_group',
 841              'user_id'   => $user_id,
 842              'item_id'   => $group_id
 843          ) );
 844      }
 845  }
 846  add_action( 'groups_leave_group',   'bp_groups_leave_group_delete_recent_activity', 10, 2 );
 847  add_action( 'groups_remove_member', 'bp_groups_leave_group_delete_recent_activity', 10, 2 );
 848  add_action( 'groups_ban_member',    'bp_groups_leave_group_delete_recent_activity', 10, 2 );


Generated: Sat Sep 21 01:01:46 2019 Cross-referenced by PHPXref 0.7.1