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


Generated: Fri Oct 18 01:01:36 2019 Cross-referenced by PHPXref 0.7.1