[ Index ]

PHP Cross Reference of BBPress

title

Body

[close]

/src/includes/forums/ -> capabilities.php (source)

   1  <?php
   2  
   3  /**
   4   * bbPress Forum Capabilites
   5   *
   6   * Used to map forum capabilities to WordPress's existing capabilities.
   7   *
   8   * @package bbPress
   9   * @subpackage Capabilities
  10   */
  11  
  12  /**
  13   * Return forum capabilities
  14   *
  15   * @since 2.0.0 bbPress (r2593)
  16   *
  17   * @return array Forum capabilities
  18   */
  19  function bbp_get_forum_caps() {
  20  
  21      // Filter & return
  22      return (array) apply_filters( 'bbp_get_forum_caps', array(
  23          'edit_posts'          => 'edit_forums',
  24          'edit_others_posts'   => 'edit_others_forums',
  25          'publish_posts'       => 'publish_forums',
  26          'read_private_posts'  => 'read_private_forums',
  27          'read_hidden_posts'   => 'read_hidden_forums',
  28          'delete_posts'        => 'delete_forums',
  29          'delete_others_posts' => 'delete_others_forums'
  30      ) );
  31  }
  32  
  33  /**
  34   * Maps forum capabilities
  35   *
  36   * @since 2.2.0 bbPress (r4242)
  37   *
  38   * @param array $caps Capabilities for meta capability
  39   * @param string $cap Capability name
  40   * @param int $user_id User id
  41   * @param array $args Arguments
  42   * @return array Actual capabilities for meta capability
  43   */
  44  function bbp_map_forum_meta_caps( $caps = array(), $cap = '', $user_id = 0, $args = array() ) {
  45  
  46      // What capability is being checked?
  47      switch ( $cap ) {
  48  
  49          /** Reading ***********************************************************/
  50  
  51          case 'read_private_forums' :
  52          case 'read_hidden_forums'  :
  53  
  54              // Moderators can always read private/hidden forums
  55              if ( user_can( $user_id, 'moderate' ) ) {
  56                  $caps = array( 'moderate' );
  57              }
  58  
  59              break;
  60  
  61          case 'read_forum' :
  62  
  63              // User cannot spectate
  64              if ( ! user_can( $user_id, 'spectate' ) ) {
  65                  $caps = array( 'do_not_allow' );
  66  
  67              // Do some post ID based logic
  68              } else {
  69  
  70                  // Bail if no post ID
  71                  if ( empty( $args[0] ) ) {
  72                      break;
  73                  }
  74  
  75                  // Get the post.
  76                  $_post = get_post( $args[0] );
  77                  if ( ! empty( $_post ) ) {
  78  
  79                      // Get caps for post type object
  80                      $post_type = get_post_type_object( $_post->post_type );
  81  
  82                      // Post is public
  83                      if ( bbp_get_public_status_id() === $_post->post_status ) {
  84                          $caps = array( 'spectate' );
  85  
  86                      // User is author so allow read
  87                      } elseif ( (int) $user_id === (int) $_post->post_author ) {
  88                          $caps = array( 'spectate' );
  89  
  90                      // Moderators can always read forum content
  91                      } elseif ( user_can( $user_id, 'moderate', $_post->ID ) ) {
  92                          $caps = array( 'spectate' );
  93  
  94                      // Private
  95                      } elseif ( bbp_get_hidden_status_id() === $_post->post_status ) {
  96                          $caps = array( $post_type->cap->read_hidden_posts );
  97  
  98                      // Hidden
  99                      } elseif ( bbp_get_private_status_id() === $_post->post_status ) {
 100                          $caps = array( $post_type->cap->read_private_posts );
 101  
 102                      // Unknown, so map to private
 103                      } else {
 104                          $caps = array( $post_type->cap->read_private_posts );
 105                      }
 106                  }
 107              }
 108  
 109              break;
 110  
 111          /** Publishing ********************************************************/
 112  
 113          case 'publish_forums'  :
 114  
 115              // Moderators can always edit
 116              if ( user_can( $user_id, 'moderate' ) ) {
 117                  $caps = array( 'moderate' );
 118              }
 119  
 120              break;
 121  
 122          /** Editing ***********************************************************/
 123  
 124          // Used primarily in wp-admin
 125          case 'edit_forums'         :
 126          case 'edit_others_forums'  :
 127  
 128              // Moderators can always edit
 129              if ( bbp_is_user_keymaster( $user_id ) ) {
 130                  $caps = array( 'spectate' );
 131  
 132              // Otherwise, block
 133              } else {
 134                  $caps = array( 'do_not_allow' );
 135              }
 136  
 137              break;
 138  
 139          // Used everywhere
 140          case 'edit_forum' :
 141  
 142              // Bail if no post ID
 143              if ( empty( $args[0] ) ) {
 144                  break;
 145              }
 146  
 147              // Get the post.
 148              $_post = get_post( $args[0] );
 149              if ( ! empty( $_post ) ) {
 150  
 151                  // Get caps for post type object
 152                  $post_type = get_post_type_object( $_post->post_type );
 153  
 154                  // Add 'do_not_allow' cap if user is spam or deleted
 155                  if ( bbp_is_user_inactive( $user_id ) ) {
 156                      $caps = array( 'do_not_allow' );
 157  
 158                  // Moderators can always read forum content
 159                  } elseif ( user_can( $user_id, 'moderate', $_post->ID ) ) {
 160                      $caps = array( 'spectate' );
 161  
 162                  // User is author so allow edit if not in admin
 163                  } elseif ( ! is_admin() && ( (int) $user_id === (int) $_post->post_author ) ) {
 164                      $caps = array( $post_type->cap->edit_posts );
 165  
 166                  // Unknown, so map to edit_others_posts
 167                  } else {
 168                      $caps = array( $post_type->cap->edit_others_posts );
 169                  }
 170              }
 171  
 172              break;
 173  
 174          /** Deleting **********************************************************/
 175  
 176          // Allow forum authors to delete forums (for BuddyPress groups, etc)
 177          case 'delete_forum' :
 178  
 179              // Bail if no post ID
 180              if ( empty( $args[0] ) ) {
 181                  break;
 182              }
 183  
 184              // Get the post.
 185              $_post = get_post( $args[0] );
 186              if ( ! empty( $_post ) ) {
 187  
 188                  // Get caps for post type object
 189                  $post_type = get_post_type_object( $_post->post_type );
 190  
 191                  // Add 'do_not_allow' cap if user is spam or deleted
 192                  if ( bbp_is_user_inactive( $user_id ) ) {
 193                      $caps = array( 'do_not_allow' );
 194  
 195                  // User is author so allow to delete
 196                  } elseif ( (int) $user_id === (int) $_post->post_author ) {
 197                      $caps = array( $post_type->cap->delete_posts );
 198  
 199                  // Unknown so map to delete_others_posts
 200                  } else {
 201                      $caps = array( $post_type->cap->delete_others_posts );
 202                  }
 203              }
 204  
 205              break;
 206  
 207          /** Admin *************************************************************/
 208  
 209          // Forum admin area.
 210          case 'bbp_forums_admin' :
 211              $caps = array( 'edit_forums' );
 212              break;
 213      }
 214  
 215      // Filter & return
 216      return (array) apply_filters( 'bbp_map_forum_meta_caps', $caps, $cap, $user_id, $args );
 217  }
 218  
 219  /**
 220   * Can a user moderate a forum?
 221   *
 222   * @since 2.6.0 bbPress (r5834)
 223   *
 224   * @param int $user_id User id.
 225   * @param int $forum_id Forum id.
 226   *
 227   * @return bool Return true if user is moderator of forum
 228   */
 229  function bbp_is_user_forum_moderator( $user_id = 0, $forum_id = 0 ) {
 230      $user_id  = bbp_get_user_id( $user_id, false, empty( $user_id ) );
 231      $forum_id = bbp_get_forum_id( $forum_id );
 232      $retval   = user_can( $user_id, 'moderate', $forum_id );
 233  
 234      // Filter & return
 235      return (bool) apply_filters( 'bbp_is_user_forum_moderator', $retval, $user_id, $forum_id );
 236  }
 237  
 238  /**
 239   * Filter an array of forum IDs that are being excluded, and remove any forum
 240   * IDs a user explicitly has access to.
 241   *
 242   * This typically means private or hidden forums the user has moderation rights
 243   * to, but it can be filtered to mean just about anything.
 244   *
 245   * This function filters the return values of the following functions:
 246   * - `bbp_get_private_forum_ids()`
 247   * - `bbp_get_hidden_forum_ids()`
 248   *
 249   * @since 2.6.0 bbPress (r6426)
 250   *
 251   * @param array $forum_ids Forum IDs to check if the user ID is a moderator of
 252   * @param int   $user_id   User ID to check if is a moderator of forums
 253   *
 254   * @return array
 255   */
 256  function bbp_allow_forums_of_user( $forum_ids = array(), $user_id = 0 ) {
 257  
 258      // Store the original forum IDs
 259      $original_forum_ids = $forum_ids;
 260  
 261      // Per-forum Moderators
 262      if ( bbp_allow_forum_mods() ) {
 263  
 264          // Loop through forum IDs
 265          foreach ( $forum_ids as $key => $forum_id ) {
 266  
 267              // Unset forum ID if user is a moderator
 268              if ( bbp_is_user_forum_moderator( $user_id, $forum_id ) ) {
 269                  unset( $forum_ids[ $key ] );
 270              }
 271          }
 272      }
 273  
 274      // Filter & return
 275      return (array) apply_filters( 'bbp_allow_forums_of_user', $forum_ids, $user_id, $original_forum_ids );
 276  }


Generated: Thu Oct 1 01:01:22 2020 Cross-referenced by PHPXref 0.7.1