[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-xprofile/ -> bp-xprofile-caps.php (source)

   1  <?php
   2  /**
   3   * Roles and capabilities logic for the XProfile component.
   4   *
   5   * @package BuddyPress
   6   * @subpackage XPRofileCaps
   7   * @since 1.6.0
   8   */
   9  
  10  // Exit if accessed directly.
  11  defined( 'ABSPATH' ) || exit;
  12  
  13  /**
  14   * Maps XProfile caps to built in WordPress caps.
  15   *
  16   * @since 1.6.0
  17   *
  18   * @param array  $caps    Capabilities for meta capability.
  19   * @param string $cap     Capability name.
  20   * @param int    $user_id User id.
  21   * @param mixed  $args    Arguments.
  22   *
  23   * @return array Actual capabilities for meta capability.
  24   */
  25  function bp_xprofile_map_meta_caps( $caps, $cap, $user_id, $args ) {
  26      switch ( $cap ) {
  27          case 'bp_xprofile_change_field_visibility' :
  28              $caps = array( 'exist' );
  29  
  30              // You may pass args manually: $field_id, $profile_user_id.
  31              $field_id        = 0;
  32              $profile_user_id = isset( $args[1] ) ? (int) $args[1] : bp_displayed_user_id();
  33  
  34              if ( ! empty( $args[0] ) ) {
  35                  $field_id = (int) $args[0];
  36              } elseif ( isset( $GLOBALS['profile_template'] ) && $GLOBALS['profile_template']->in_the_loop ) {
  37                  $field_id = bp_get_the_profile_field_id();
  38              }
  39  
  40              // Visibility on the fullname field is not editable.
  41              if ( 1 == $field_id ) {
  42                  $caps[] = 'do_not_allow';
  43                  break;
  44              }
  45  
  46              // Has the admin disabled visibility modification for this field?
  47              if ( 'disabled' == bp_xprofile_get_meta( $field_id, 'field', 'allow_custom_visibility' ) ) {
  48                  $caps[] = 'do_not_allow';
  49                  break;
  50              }
  51  
  52              // Friends don't let friends edit each other's visibility.
  53              if ( $profile_user_id != bp_displayed_user_id() && !bp_current_user_can( 'bp_moderate' ) ) {
  54                  $caps[] = 'do_not_allow';
  55                  break;
  56              }
  57  
  58              break;
  59      }
  60  
  61      /**
  62       * Filters the XProfile caps to built in WordPress caps.
  63       *
  64       * @since 1.6.0
  65       *
  66       * @param array  $caps    Capabilities for meta capability.
  67       * @param string $cap     Capability name.
  68       * @param int    $user_id User ID being mapped.
  69       * @param mixed  $args    Capability arguments.
  70       */
  71      return apply_filters( 'bp_xprofile_map_meta_caps', $caps, $cap, $user_id, $args );
  72  }
  73  add_filter( 'bp_map_meta_caps', 'bp_xprofile_map_meta_caps', 10, 4 );
  74  
  75  /**
  76   * Grant the 'bp_xprofile_change_field_visibility' cap to logged-out users.
  77   *
  78   * @since 2.7.1
  79   *
  80   * @param bool   $user_can
  81   * @param int    $user_id
  82   * @param string $capability
  83   * @return bool
  84   */
  85  function bp_xprofile_grant_bp_xprofile_change_field_visibility_for_logged_out_users( $user_can, $user_id, $capability ) {
  86      if ( 'bp_xprofile_change_field_visibility' === $capability && 0 === $user_id ) {
  87          $field_id = bp_get_the_profile_field_id();
  88          if ( $field_id && $field = xprofile_get_field( $field_id, null, false ) ) {
  89              $user_can = 'allowed' === $field->allow_custom_visibility;
  90          }
  91      }
  92  
  93      return $user_can;
  94  }
  95  add_filter( 'bp_user_can', 'bp_xprofile_grant_bp_xprofile_change_field_visibility_for_logged_out_users', 10, 3 );


Generated: Thu Apr 25 01:01:12 2024 Cross-referenced by PHPXref 0.7.1