[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * BuddyPress Groups Filters.
   4   *
   5   * @package BuddyPress
   6   * @subpackage GroupsFilters
   7   * @since 1.0.0
   8   */
   9  
  10  // Exit if accessed directly.
  11  defined( 'ABSPATH' ) || exit;
  12  
  13  // Filter BuddyPress template locations.
  14  add_filter( 'bp_groups_get_directory_template', 'bp_add_template_locations' );
  15  add_filter( 'bp_get_single_group_template',    'bp_add_template_locations' );
  16  
  17  /* Apply WordPress defined filters */
  18  add_filter( 'bp_get_group_description',         'wptexturize' );
  19  add_filter( 'bp_get_group_description_excerpt', 'wptexturize' );
  20  add_filter( 'bp_get_group_name',                'wptexturize' );
  21  
  22  add_filter( 'bp_get_group_description',         'convert_smilies' );
  23  add_filter( 'bp_get_group_description_excerpt', 'convert_smilies' );
  24  
  25  add_filter( 'bp_get_group_description',         'convert_chars' );
  26  add_filter( 'bp_get_group_description_excerpt', 'convert_chars' );
  27  add_filter( 'bp_get_group_name',                'convert_chars' );
  28  
  29  add_filter( 'bp_get_group_description',         'wpautop' );
  30  add_filter( 'bp_get_group_description_excerpt', 'wpautop' );
  31  
  32  add_filter( 'bp_get_group_description',         'make_clickable', 9 );
  33  add_filter( 'bp_get_group_description_excerpt', 'make_clickable', 9 );
  34  
  35  add_filter( 'bp_get_group_name',                    'wp_filter_kses',        1 );
  36  add_filter( 'bp_get_group_permalink',               'wp_filter_kses',        1 );
  37  add_filter( 'bp_get_group_description',             'bp_groups_filter_kses', 1 );
  38  add_filter( 'bp_get_group_description_excerpt',     'wp_filter_kses',        1 );
  39  add_filter( 'groups_group_name_before_save',        'wp_filter_kses',        1 );
  40  add_filter( 'groups_group_description_before_save', 'wp_filter_kses',        1 );
  41  
  42  add_filter( 'bp_get_group_description',         'stripslashes' );
  43  add_filter( 'bp_get_group_description_excerpt', 'stripslashes' );
  44  add_filter( 'bp_get_group_name',                'stripslashes' );
  45  add_filter( 'bp_get_group_member_name',         'stripslashes' );
  46  add_filter( 'bp_get_group_member_link',         'stripslashes' );
  47  
  48  add_filter( 'groups_group_name_before_save',        'force_balance_tags' );
  49  add_filter( 'groups_group_description_before_save', 'force_balance_tags' );
  50  
  51  // Trim trailing spaces from name and description when saving.
  52  add_filter( 'groups_group_name_before_save',        'trim' );
  53  add_filter( 'groups_group_description_before_save', 'trim' );
  54  
  55  // Support emoji.
  56  add_filter( 'groups_group_description_before_save', 'wp_encode_emoji' );
  57  
  58  // Escape output of new group creation details.
  59  add_filter( 'bp_get_new_group_name',        'esc_attr'     );
  60  add_filter( 'bp_get_new_group_description', 'esc_textarea' );
  61  
  62  // Format numerical output.
  63  add_filter( 'bp_get_total_group_count',          'bp_core_number_format' );
  64  add_filter( 'bp_get_group_total_for_member',     'bp_core_number_format' );
  65  add_filter( 'bp_get_group_total_members',        'bp_core_number_format' );
  66  add_filter( 'bp_get_total_group_count_for_user', 'bp_core_number_format' );
  67  
  68  // Activity component integration.
  69  add_filter( 'bp_activity_at_name_do_notifications', 'bp_groups_disable_at_mention_notification_for_non_public_groups', 10, 4 );
  70  
  71  // Default group avatar.
  72  add_filter( 'bp_core_default_avatar',       'bp_groups_default_avatar', 10, 3 );
  73  add_filter( 'bp_core_avatar_default_thumb', 'bp_groups_default_avatar', 10, 3 );
  74  
  75  // Personal data export.
  76  add_filter( 'wp_privacy_personal_data_exporters', 'bp_groups_register_personal_data_exporters' );
  77  
  78  /**
  79   * Filter output of Group Description through WordPress's KSES API.
  80   *
  81   * @since 1.1.0
  82   *
  83   * @param string $content Content to filter.
  84   * @return string
  85   */
  86  function bp_groups_filter_kses( $content = '' ) {
  87  
  88      /**
  89       * Note that we don't immediately bail if $content is empty. This is because
  90       * WordPress's KSES API calls several other filters that might be relevant
  91       * to someone's workflow (like `pre_kses`)
  92       */
  93  
  94      // Get allowed tags using core WordPress API allowing third party plugins
  95      // to target the specific `buddypress-groups` context.
  96      $allowed_tags = wp_kses_allowed_html( 'buddypress-groups' );
  97  
  98      // Add our own tags allowed in group descriptions.
  99      $allowed_tags['a']['class']    = array();
 100      $allowed_tags['img']           = array();
 101      $allowed_tags['img']['src']    = array();
 102      $allowed_tags['img']['alt']    = array();
 103      $allowed_tags['img']['width']  = array();
 104      $allowed_tags['img']['height'] = array();
 105      $allowed_tags['img']['class']  = array();
 106      $allowed_tags['img']['id']     = array();
 107      $allowed_tags['code']          = array();
 108  
 109      /**
 110       * Filters the HTML elements allowed for a given context.
 111       *
 112       * @since 1.2.0
 113       *
 114       * @param string $allowed_tags Allowed tags, attributes, and/or entities.
 115       */
 116      $tags = apply_filters( 'bp_groups_filter_kses', $allowed_tags );
 117  
 118      // Return KSES'ed content, allowing the above tags.
 119      return wp_kses( $content, $tags );
 120  }
 121  
 122  /**
 123   * Should BuddyPress load the mentions scripts and related assets, including results to prime the
 124   * mentions suggestions?
 125   *
 126   * @since 2.2.0
 127   *
 128   * @param bool $load_mentions    True to load mentions assets, false otherwise.
 129   * @param bool $mentions_enabled True if mentions are enabled.
 130   * @return bool True if mentions scripts should be loaded.
 131   */
 132  function bp_groups_maybe_load_mentions_scripts( $load_mentions, $mentions_enabled ) {
 133      if ( ! $mentions_enabled ) {
 134          return $load_mentions;
 135      }
 136  
 137      if ( $load_mentions || bp_is_group_activity() ) {
 138          return true;
 139      }
 140  
 141      return $load_mentions;
 142  }
 143  add_filter( 'bp_activity_maybe_load_mentions_scripts', 'bp_groups_maybe_load_mentions_scripts', 10, 2 );
 144  
 145  /**
 146   * Disable at-mention notifications for users who are not a member of the non-public group where the activity appears.
 147   *
 148   * @since 2.5.0
 149   *
 150   * @param bool                 $send      Whether to send the notification.
 151   * @param array                $usernames Array of all usernames being notified.
 152   * @param int                  $user_id   ID of the user to be notified.
 153   * @param BP_Activity_Activity $activity  Activity object.
 154   * @return bool
 155   */
 156  function bp_groups_disable_at_mention_notification_for_non_public_groups( $send, $usernames, $user_id, BP_Activity_Activity $activity ) {
 157      // Skip the check for administrators, who can get notifications from non-public groups.
 158      if ( bp_user_can( $user_id, 'bp_moderate' ) ) {
 159          return $send;
 160      }
 161  
 162      if ( 'groups' === $activity->component && ! bp_user_can( $user_id, 'groups_access_group', array( 'group_id' => $activity->item_id ) ) ) {
 163          $send = false;
 164      }
 165  
 166      return $send;
 167  }
 168  
 169  /**
 170   * Use the mystery group avatar for groups.
 171   *
 172   * @since 2.6.0
 173   *
 174   * @param string $avatar Current avatar src.
 175   * @param array  $params Avatar params.
 176   * @return string
 177   */
 178  function bp_groups_default_avatar( $avatar, $params ) {
 179      if ( isset( $params['object'] ) && 'group' === $params['object'] ) {
 180          if ( isset( $params['type'] ) && 'thumb' === $params['type'] ) {
 181              $file = 'mystery-group-50.png';
 182          } else {
 183              $file = 'mystery-group.png';
 184          }
 185  
 186          $avatar = buddypress()->plugin_url . "bp-core/images/$file";
 187      }
 188  
 189      return $avatar;
 190  }
 191  
 192  /**
 193   * Filter the bp_user_can value to determine what the user can do
 194   * with regards to a specific group.
 195   *
 196   * @since 3.0.0
 197   *
 198   * @param bool   $retval     Whether or not the current user has the capability.
 199   * @param int    $user_id
 200   * @param string $capability The capability being checked for.
 201   * @param int    $site_id    Site ID. Defaults to the BP root blog.
 202   * @param array  $args       Array of extra arguments passed.
 203   *
 204   * @return bool
 205   */
 206  function bp_groups_user_can_filter( $retval, $user_id, $capability, $site_id, $args ) {
 207      if ( empty( $args['group_id'] ) ) {
 208          $group_id = bp_get_current_group_id();
 209      } else {
 210          $group_id = (int) $args['group_id'];
 211      }
 212  
 213      switch ( $capability ) {
 214          case 'groups_join_group':
 215              // Return early if the user isn't logged in or the group ID is unknown.
 216              if ( ! $user_id || ! $group_id ) {
 217                  break;
 218              }
 219  
 220              // Set to false to begin with.
 221              $retval = false;
 222  
 223              // The group must allow joining, and the user should not currently be a member.
 224              $group = groups_get_group( $group_id );
 225              if ( ( 'public' === bp_get_group_status( $group )
 226                  && ! groups_is_user_member( $user_id, $group->id )
 227                  && ! groups_is_user_banned( $user_id, $group->id ) )
 228                  // Site admins can join any group they are not a member of.
 229                  || ( bp_user_can( $user_id, 'bp_moderate' )
 230                  && ! groups_is_user_member( $user_id, $group->id ) )
 231              ) {
 232                  $retval = true;
 233              }
 234              break;
 235  
 236          case 'groups_request_membership':
 237              // Return early if the user isn't logged in or the group ID is unknown.
 238              if ( ! $user_id || ! $group_id ) {
 239                  break;
 240              }
 241  
 242              // Set to false to begin with.
 243              $retval = false;
 244  
 245              /*
 246              * The group must accept membership requests, and the user should not
 247              * currently be a member, have an active request, or be banned.
 248              */
 249              $group = groups_get_group( $group_id );
 250              if ( 'private' === bp_get_group_status( $group )
 251                  && ! groups_is_user_member( $user_id, $group->id )
 252                  && ! groups_check_for_membership_request( $user_id, $group->id )
 253                  && ! groups_is_user_banned( $user_id, $group->id )
 254              ) {
 255                  $retval = true;
 256              }
 257              break;
 258  
 259          case 'groups_send_invitation':
 260              // Return early if the user isn't logged in or the group ID is unknown.
 261              if ( ! $user_id || ! $group_id ) {
 262                  break;
 263              }
 264  
 265              /*
 266              * The group must allow invitations, and the user should not
 267              * currently be a member or be banned from the group.
 268              */
 269              // Users with the 'bp_moderate' cap can always send invitations.
 270              if ( bp_user_can( $user_id, 'bp_moderate' ) ) {
 271                  $retval = true;
 272              } else {
 273                  $invite_status = bp_group_get_invite_status( $group_id );
 274  
 275                  switch ( $invite_status ) {
 276                      case 'admins' :
 277                          if ( groups_is_user_admin( $user_id, $group_id ) ) {
 278                              $retval = true;
 279                          }
 280                          break;
 281  
 282                      case 'mods' :
 283                          if ( groups_is_user_mod( $user_id, $group_id ) || groups_is_user_admin( $user_id, $group_id ) ) {
 284                              $retval = true;
 285                          }
 286                          break;
 287  
 288                      case 'members' :
 289                          if ( groups_is_user_member( $user_id, $group_id ) ) {
 290                              $retval = true;
 291                          }
 292                          break;
 293                  }
 294              }
 295              break;
 296  
 297          case 'groups_receive_invitation':
 298              // Return early if the user isn't logged in or the group ID is unknown.
 299              if ( ! $user_id || ! $group_id ) {
 300                  break;
 301              }
 302  
 303              // Set to false to begin with.
 304              $retval = false;
 305  
 306              /*
 307              * The group must allow invitations, and the user should not
 308              * currently be a member or be banned from the group.
 309              */
 310              $group = groups_get_group( $group_id );
 311              if ( ! groups_is_user_member( $user_id, $group->id ) && ! groups_is_user_banned( $user_id, $group->id )    ) {
 312                  $retval = true;
 313              }
 314              break;
 315  
 316          case 'groups_access_group':
 317              // Return early if the group ID is unknown.
 318              if ( ! $group_id ) {
 319                  break;
 320              }
 321  
 322              $group = groups_get_group( $group_id );
 323  
 324              // If the check is for the logged-in user, use the BP_Groups_Group property.
 325              if ( $user_id === bp_loggedin_user_id() ) {
 326                  $retval = $group->user_has_access;
 327  
 328              /*
 329               * If the check is for a specified user who is not the logged-in user
 330               * run the check manually.
 331               */
 332              } elseif ( 'public' === bp_get_group_status( $group ) || groups_is_user_member( $user_id, $group->id ) ) {
 333                  $retval = true;
 334              }
 335              break;
 336  
 337          case 'groups_see_group':
 338              // Return early if the group ID is unknown.
 339              if ( ! $group_id ) {
 340                  break;
 341              }
 342  
 343              $group = groups_get_group( $group_id );
 344  
 345              // If the check is for the logged-in user, use the BP_Groups_Group property.
 346              if ( $user_id === bp_loggedin_user_id() ) {
 347                  $retval = $group->is_visible;
 348  
 349              /*
 350               * If the check is for a specified user who is not the logged-in user
 351               * run the check manually.
 352               */
 353              } elseif ( 'hidden' !== bp_get_group_status( $group ) || groups_is_user_member( $user_id, $group->id ) ) {
 354                  $retval = true;
 355              }
 356              break;
 357      }
 358  
 359      return $retval;
 360  
 361  }
 362  add_filter( 'bp_user_can', 'bp_groups_user_can_filter', 10, 5 );
 363  
 364  /**
 365   * Registers Groups personal data exporters.
 366   *
 367   * @since 4.0.0
 368   * @since 5.0.0 adds an `exporter_bp_friendly_name` param to exporters.
 369   *
 370   * @param array $exporters  An array of personal data exporters.
 371   * @return array An array of personal data exporters.
 372   */
 373  function bp_groups_register_personal_data_exporters( $exporters ) {
 374      $exporters['buddypress-groups-memberships'] = array(
 375          'exporter_friendly_name'    => __( 'BuddyPress Group Memberships', 'buddypress' ),
 376          'callback'                  => 'bp_groups_memberships_personal_data_exporter',
 377          'exporter_bp_friendly_name' => _x( 'Group Memberships', 'BuddyPress Group Memberships data exporter friendly name', 'buddypress' ),
 378      );
 379  
 380      $exporters['buddypress-groups-pending-requests'] = array(
 381          'exporter_friendly_name'    => __( 'BuddyPress Pending Group Membership Requests', 'buddypress' ),
 382          'callback'                  => 'bp_groups_pending_requests_personal_data_exporter',
 383          'exporter_bp_friendly_name' => _x( 'Pending Group Membership Requests', 'BuddyPress Pending Group Membership Requests data exporter friendly name', 'buddypress' ),
 384      );
 385  
 386      $exporters['buddypress-groups-pending-received-invitations'] = array(
 387          'exporter_friendly_name'    => __( 'BuddyPress Pending Group Invitations (Received)', 'buddypress' ),
 388          'callback'                  => 'bp_groups_pending_received_invitations_personal_data_exporter',
 389          'exporter_bp_friendly_name' => _x( 'Pending Group Invitations (Received)', 'BuddyPress Pending Group Invitations data exporter friendly name', 'buddypress' ),
 390      );
 391  
 392      $exporters['buddypress-groups-pending-sent-invitations'] = array(
 393          'exporter_friendly_name'    => __( 'BuddyPress Pending Group Invitations (Sent)', 'buddypress' ),
 394          'callback'                  => 'bp_groups_pending_sent_invitations_personal_data_exporter',
 395          'exporter_bp_friendly_name' => _x( 'Pending Group Invitations (Sent)', 'BuddyPress Pending Group Invitations data exporter friendly name', 'buddypress' ),
 396      );
 397  
 398      return $exporters;
 399  }


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