[ 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(
 525          $args,
 526          array(
 527              'id'                => false,
 528              'user_id'           => bp_loggedin_user_id(),
 529              'action'            => '',
 530              'content'           => '',
 531              'primary_link'      => '',
 532              'component'         => buddypress()->groups->id,
 533              'type'              => false,
 534              'item_id'           => false,
 535              'secondary_item_id' => false,
 536              'recorded_time'     => bp_core_current_time(),
 537              'hide_sitewide'     => $hide_sitewide,
 538              'error_type'        => 'bool',
 539          ),
 540          'groups_record_activity'
 541      );
 542  
 543      return bp_activity_add( $r );
 544  }
 545  
 546  /**
 547   * Post an Activity status update affiliated with a group.
 548   *
 549   * @since 1.2.0
 550   * @since 2.6.0 Added 'error_type' parameter to $args.
 551   *
 552   * @param array|string $args {
 553   *     Array of arguments.
 554   *     @type string $content  The content of the update.
 555   *     @type int    $user_id  Optional. ID of the user posting the update. Default:
 556   *                            ID of the logged-in user.
 557   *     @type int    $group_id Optional. ID of the group to be affiliated with the
 558   *                            update. Default: ID of the current group.
 559   * }
 560   * @return WP_Error|bool|int Returns the ID of the new activity item on success, or false on failure.
 561   */
 562  function groups_post_update( $args = '' ) {
 563      $bp = buddypress();
 564  
 565      $r = bp_parse_args(
 566          $args,
 567          array(
 568              'content'    => false,
 569              'user_id'    => bp_loggedin_user_id(),
 570              'group_id'   => 0,
 571              'error_type' => 'bool',
 572          ),
 573          'groups_post_update'
 574      );
 575  
 576      $group_id = (int) $r['group_id'];
 577      if ( ! $group_id && ! empty( $bp->groups->current_group->id ) ) {
 578          $group_id = (int) $bp->groups->current_group->id;
 579      }
 580  
 581      $content = $r['content'];
 582      $user_id = (int) $r['user_id'];
 583      if ( ! $content || ! strlen( trim( $content ) ) || ! $user_id || ! $group_id ) {
 584          return false;
 585      }
 586  
 587      $bp->groups->current_group = groups_get_group( $group_id );
 588  
 589      // Be sure the user is a member of the group before posting.
 590      if ( ! bp_current_user_can( 'bp_moderate' ) && ! groups_is_user_member( $user_id, $group_id ) ) {
 591          return false;
 592      }
 593  
 594      /**
 595       * Filters the content for the new group activity update.
 596       *
 597       * @since 1.2.0
 598       *
 599       * @param string $content The content of the update.
 600       */
 601      $content_filtered = apply_filters( 'groups_activity_new_update_content', $content );
 602  
 603      $activity_id = groups_record_activity( array(
 604          'user_id'    => $user_id,
 605          'content'    => $content_filtered,
 606          'type'       => 'activity_update',
 607          'item_id'    => $group_id,
 608          'error_type' => $r['error_type'],
 609      ) );
 610  
 611      groups_update_groupmeta( $group_id, 'last_activity', bp_core_current_time() );
 612  
 613      /**
 614       * Fires after posting of an Activity status update affiliated with a group.
 615       *
 616       * @since 1.2.0
 617       *
 618       * @param string $content     The content of the update.
 619       * @param int    $user_id     ID of the user posting the update.
 620       * @param int    $group_id    ID of the group being posted to.
 621       * @param bool   $activity_id Whether or not the activity recording succeeded.
 622       */
 623      do_action( 'bp_groups_posted_update', $content, $user_id, $group_id, $activity_id );
 624  
 625      return $activity_id;
 626  }
 627  
 628  /**
 629   * Function used to determine if a user can delete a group activity item.
 630   *
 631   * Used as a filter callback to 'bp_activity_user_can_delete'.
 632   *
 633   * @since 6.0.0
 634   *
 635   * @param  bool   $retval   True if item can receive comments.
 636   * @param  object $activity Activity item being checked.
 637   * @return bool
 638   */
 639  function bp_groups_filter_activity_user_can_delete( $retval, $activity ) {
 640      // Bail if no current user.
 641      if ( ! is_user_logged_in() ) {
 642          return $retval;
 643      }
 644  
 645      if ( isset( $activity->component ) || 'groups' !== $activity->component ) {
 646          return $retval;
 647      }
 648  
 649      // Trust the passed value for administrators.
 650      if ( bp_current_user_can( 'bp_moderate' ) ) {
 651          return $retval;
 652      }
 653  
 654      // Group administrators or moderators can delete content in that group that doesn't belong to them.
 655      $group_id = $activity->item_id;
 656      if ( groups_is_user_admin( bp_loggedin_user_id(), $group_id ) || groups_is_user_mod( bp_loggedin_user_id(), $group_id ) ) {
 657          $retval = true;
 658      }
 659  
 660      return $retval;
 661  }
 662  add_filter( 'bp_activity_user_can_delete', 'bp_groups_filter_activity_user_can_delete', 10, 2 );
 663  
 664  /**
 665   * Function used to determine if a user can comment on a group activity item.
 666   *
 667   * Used as a filter callback to 'bp_activity_can_comment'.
 668   *
 669   * @since 3.0.0
 670   *
 671   * @param  bool                      $retval   True if item can receive comments.
 672   * @param  null|BP_Activity_Activity $activity Null by default. Pass an activity object to check against that instead.
 673   * @return bool
 674   */
 675  function bp_groups_filter_activity_can_comment( $retval, $activity = null ) {
 676      // Bail if item cannot receive comments or if no current user.
 677      if ( empty( $retval ) || ! is_user_logged_in() ) {
 678          return $retval;
 679      }
 680  
 681      // Use passed activity object, if available.
 682      if ( is_a( $activity, 'BP_Activity_Activity' ) ) {
 683          $component = $activity->component;
 684          $group_id  = $activity->item_id;
 685  
 686      // Use activity info from current activity item in the loop.
 687      } else {
 688          $component = bp_get_activity_object_name();
 689          $group_id  = bp_get_activity_item_id();
 690      }
 691  
 692      // If not a group activity item, bail.
 693      if ( 'groups' !== $component ) {
 694          return $retval;
 695      }
 696  
 697      // If current user is not a group member or is banned, user cannot comment.
 698      if ( ! bp_current_user_can( 'bp_moderate' ) &&
 699          ( ! groups_is_user_member( bp_loggedin_user_id(), $group_id ) || groups_is_user_banned( bp_loggedin_user_id(), $group_id ) )
 700      ) {
 701          $retval = false;
 702      }
 703  
 704      return $retval;
 705  }
 706  add_filter( 'bp_activity_can_comment', 'bp_groups_filter_activity_can_comment', 99, 1 );
 707  
 708  /**
 709   * Function used to determine if a user can reply on a group activity comment.
 710   *
 711   * Used as a filter callback to 'bp_activity_can_comment_reply'.
 712   *
 713   * @since 3.0.0
 714   *
 715   * @param  bool        $retval  True if activity comment can be replied to.
 716   * @param  object|bool $comment Current activity comment object. If empty, parameter is boolean false.
 717   * @return bool
 718   */
 719  function bp_groups_filter_activity_can_comment_reply( $retval, $comment ) {
 720      // Bail if no current user, if comment is empty or if retval is already empty.
 721      if ( ! is_user_logged_in() || empty( $comment ) || empty( $retval ) ) {
 722          return $retval;
 723      }
 724  
 725      // Grab parent activity item.
 726      $parent = new BP_Activity_Activity( $comment->item_id );
 727  
 728      // Check to see if user can reply to parent group activity item.
 729      return bp_groups_filter_activity_can_comment( $retval, $parent );
 730  }
 731  add_filter( 'bp_activity_can_comment_reply', 'bp_groups_filter_activity_can_comment_reply', 99, 2 );
 732  
 733  /**
 734   * Add an activity stream item when a member joins a group.
 735   *
 736   * @since 1.9.0
 737   *
 738   * @param int $user_id  ID of the user joining the group.
 739   * @param int $group_id ID of the group.
 740   * @return false|null False on failure.
 741   */
 742  function bp_groups_membership_accepted_add_activity( $user_id, $group_id ) {
 743  
 744      // Bail if Activity is not active.
 745      if ( ! bp_is_active( 'activity' ) ) {
 746          return false;
 747      }
 748  
 749      // Get the group so we can get it's name.
 750      $group = groups_get_group( $group_id );
 751  
 752      /**
 753       * Filters the 'membership_accepted' activity actions.
 754       *
 755       * @since 1.2.0
 756       *
 757       * @param string $value    The 'membership_accepted' activity action.
 758       * @param int    $user_id  ID of the user joining the group.
 759       * @param int    $group_id ID of the group. Passed by reference.
 760       */
 761      $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 ) );
 762  
 763      // Record in activity streams.
 764      groups_record_activity( array(
 765          'action'  => $action,
 766          'type'    => 'joined_group',
 767          'item_id' => $group_id,
 768          'user_id' => $user_id
 769      ) );
 770  }
 771  add_action( 'groups_membership_accepted', 'bp_groups_membership_accepted_add_activity', 10, 2 );
 772  
 773  /**
 774   * Add an activity item when a group's details are updated.
 775   *
 776   * @since 2.2.0
 777   *
 778   * @param  int             $group_id       ID of the group.
 779   * @param  BP_Groups_Group $old_group      Group object before the details had been changed.
 780   * @param  bool            $notify_members True if the admin has opted to notify group members, otherwise false.
 781   * @return null|WP_Error|bool|int The ID of the activity on success. False on error.
 782   */
 783  function bp_groups_group_details_updated_add_activity( $group_id, $old_group, $notify_members ) {
 784  
 785      // Bail if Activity is not active.
 786      if ( ! bp_is_active( 'activity' ) ) {
 787          return false;
 788      }
 789  
 790      if ( ! isset( $old_group->name ) || ! isset( $old_group->slug ) || ! isset( $old_group->description ) ) {
 791          return false;
 792      }
 793  
 794      // If the admin has opted not to notify members, don't post an activity item either.
 795      if ( empty( $notify_members ) ) {
 796          return;
 797      }
 798  
 799      $group = groups_get_group( array(
 800          'group_id' => $group_id,
 801      ) );
 802  
 803      /*
 804       * Store the changed data, which will be used to generate the activity
 805       * action. Since we haven't yet created the activity item, we store the
 806       * old group data in groupmeta, keyed by the timestamp that we'll put
 807       * on the activity item.
 808       */
 809      $changed = array();
 810  
 811      if ( $group->name !== $old_group->name ) {
 812          $changed['name'] = array(
 813              'old' => $old_group->name,
 814              'new' => $group->name,
 815          );
 816      }
 817  
 818      if ( $group->slug !== $old_group->slug ) {
 819          $changed['slug'] = array(
 820              'old' => $old_group->slug,
 821              'new' => $group->slug,
 822          );
 823      }
 824  
 825      if ( $group->description !== $old_group->description ) {
 826          $changed['description'] = array(
 827              'old' => $old_group->description,
 828              'new' => $group->description,
 829          );
 830      }
 831  
 832      // If there are no changes, don't post an activity item.
 833      if ( empty( $changed ) ) {
 834          return;
 835      }
 836  
 837      $time = bp_core_current_time();
 838      groups_update_groupmeta( $group_id, 'updated_details_' . $time, $changed );
 839  
 840      // Record in activity streams.
 841      return groups_record_activity( array(
 842          'type'          => 'group_details_updated',
 843          'item_id'       => $group_id,
 844          'user_id'       => bp_loggedin_user_id(),
 845          'recorded_time' => $time,
 846  
 847      ) );
 848  
 849  }
 850  add_action( 'groups_details_updated', 'bp_groups_group_details_updated_add_activity', 10, 3 );
 851  
 852  /**
 853   * Delete all activity items related to a specific group.
 854   *
 855   * @since 1.9.0
 856   *
 857   * @param int $group_id ID of the group.
 858   */
 859  function bp_groups_delete_group_delete_all_activity( $group_id ) {
 860      if ( bp_is_active( 'activity' ) ) {
 861          bp_activity_delete_by_item_id( array(
 862              'item_id'   => $group_id,
 863              'component' => buddypress()->groups->id
 864          ) );
 865      }
 866  }
 867  add_action( 'groups_delete_group', 'bp_groups_delete_group_delete_all_activity', 10 );
 868  
 869  /**
 870   * Delete group member activity if they leave or are removed within 5 minutes of membership modification.
 871   *
 872   * If the user joined this group less than five minutes ago, remove the
 873   * joined_group activity so users cannot flood the activity stream by
 874   * joining/leaving the group in quick succession.
 875   *
 876   * @since 1.9.0
 877   *
 878   * @param int $group_id ID of the group.
 879   * @param int $user_id  ID of the user leaving the group.
 880   */
 881  function bp_groups_leave_group_delete_recent_activity( $group_id, $user_id ) {
 882  
 883      // Bail if Activity component is not active.
 884      if ( ! bp_is_active( 'activity' ) ) {
 885          return;
 886      }
 887  
 888      // Get the member's group membership information.
 889      $membership = new BP_Groups_Member( $user_id, $group_id );
 890  
 891      // Check the time period, and maybe delete their recent group activity.
 892      if ( time() <= strtotime( '+5 minutes', (int) strtotime( $membership->date_modified ) ) ) {
 893          bp_activity_delete( array(
 894              'component' => buddypress()->groups->id,
 895              'type'      => 'joined_group',
 896              'user_id'   => $user_id,
 897              'item_id'   => $group_id
 898          ) );
 899      }
 900  }
 901  add_action( 'groups_leave_group',   'bp_groups_leave_group_delete_recent_activity', 10, 2 );
 902  add_action( 'groups_remove_member', 'bp_groups_leave_group_delete_recent_activity', 10, 2 );
 903  add_action( 'groups_ban_member',    'bp_groups_leave_group_delete_recent_activity', 10, 2 );


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