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


Generated: Thu Apr 2 01:01:33 2020 Cross-referenced by PHPXref 0.7.1