[ Index ]

PHP Cross Reference of BBPress

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * bbPress Topic Capabilites
   5   *
   6   * Used to map topic capabilities to WordPress's existing capabilities.
   7   *
   8   * @package bbPress
   9   * @subpackage Capabilities
  10   */
  11  
  12  /**
  13   * Return topic capabilities
  14   *
  15   * @since 2.0.0 bbPress (r2593)
  16   *
  17   * @return array Topic capabilities
  18   */
  19  function bbp_get_topic_caps() {
  20  
  21      // Filter & return
  22      return (array) apply_filters( 'bbp_get_topic_caps', array(
  23          'edit_posts'          => 'edit_topics',
  24          'edit_others_posts'   => 'edit_others_topics',
  25          'publish_posts'       => 'publish_topics',
  26          'read_private_posts'  => 'read_private_topics',
  27          'read_hidden_posts'   => 'read_hidden_topics',
  28          'delete_posts'        => 'delete_topics',
  29          'delete_others_posts' => 'delete_others_topics'
  30      ) );
  31  }
  32  
  33  /**
  34   * Return topic tag capabilities
  35   *
  36   * @since 2.0.0 bbPress (r2593)
  37   *
  38   *
  39   * @return array Topic tag capabilities
  40   */
  41  function bbp_get_topic_tag_caps() {
  42  
  43      // Filter & return
  44      return (array) apply_filters( 'bbp_get_topic_tag_caps', array(
  45          'manage_terms' => 'manage_topic_tags',
  46          'edit_terms'   => 'edit_topic_tags',
  47          'delete_terms' => 'delete_topic_tags',
  48          'assign_terms' => 'assign_topic_tags'
  49      ) );
  50  }
  51  
  52  /**
  53   * Maps topic capabilities
  54   *
  55   * @since 2.2.0 bbPress (r4242)
  56   *
  57   * @param array  $caps    Capabilities for meta capability.
  58   * @param string $cap     Capability name.
  59   * @param int    $user_id User id.
  60   * @param array  $args    Arguments.
  61   *
  62   * @return array Actual capabilities for meta capability
  63   */
  64  function bbp_map_topic_meta_caps( $caps = array(), $cap = '', $user_id = 0, $args = array() ) {
  65  
  66      // What capability is being checked?
  67      switch ( $cap ) {
  68  
  69          /** Reading ***********************************************************/
  70  
  71          case 'read_topic' :
  72  
  73              // User cannot spectate
  74              if ( ! user_can( $user_id, 'spectate' ) ) {
  75                  $caps = array( 'do_not_allow' );
  76  
  77              // Do some post ID based logic
  78              } else {
  79  
  80                  // Bail if no post ID
  81                  if ( empty( $args[0] ) ) {
  82                      return $caps;
  83                  }
  84  
  85                  // Get the post.
  86                  $_post = get_post( $args[0] );
  87                  if ( ! empty( $_post ) ) {
  88  
  89                      // Get caps for post type object
  90                      $post_type = get_post_type_object( $_post->post_type );
  91  
  92                      // Post is public
  93                      if ( bbp_get_public_status_id() === $_post->post_status ) {
  94                          $caps = array( 'spectate' );
  95  
  96                      // User is author so allow read
  97                      } elseif ( (int) $user_id === (int) $_post->post_author ) {
  98                          $caps = array( 'spectate' );
  99  
 100                      // Moderators can always edit forum content
 101                      } elseif ( user_can( $user_id, 'moderate', $_post->ID ) ) {
 102                          $caps = array( 'spectate' );
 103  
 104                      // Unknown so map to private posts
 105                      } else {
 106                          $caps = array( $post_type->cap->read_private_posts );
 107                      }
 108                  }
 109              }
 110  
 111              break;
 112  
 113          /** Publishing ********************************************************/
 114  
 115          case 'publish_topics'  :
 116  
 117              // Moderators can always publish
 118              if ( user_can( $user_id, 'moderate' ) ) {
 119                  $caps = array( 'moderate' );
 120              }
 121  
 122              break;
 123  
 124          /** Editing ***********************************************************/
 125  
 126          // Used primarily in wp-admin
 127          case 'edit_topics'        :
 128          case 'edit_others_topics' :
 129  
 130              // Moderators can always edit
 131              if ( user_can( $user_id, 'moderate' ) ) {
 132                  $caps = array( $cap );
 133  
 134              // Otherwise, check forum
 135              } else {
 136                  $forum_id = bbp_get_forum_id();
 137  
 138                  // Moderators can always edit forum content
 139                  if ( user_can( $user_id, 'moderate', $forum_id ) ) {
 140                      $caps = array( 'spectate' );
 141  
 142                  // Fallback to do_not_allow
 143                  } else {
 144                      $caps = array( 'do_not_allow' );
 145                  }
 146              }
 147  
 148              break;
 149  
 150          // Used everywhere
 151          case 'edit_topic' :
 152  
 153              // Bail if no post ID
 154              if ( empty( $args[0] ) ) {
 155                  return $caps;
 156              }
 157  
 158              // Get the post.
 159              $_post = get_post( $args[0] );
 160              if ( ! empty( $_post ) ) {
 161  
 162                  // Get caps for post type object
 163                  $post_type = get_post_type_object( $_post->post_type );
 164  
 165                  // Add 'do_not_allow' cap if user is spam or deleted
 166                  if ( bbp_is_user_inactive( $user_id ) ) {
 167                      $caps = array( 'do_not_allow' );
 168  
 169                  // Moderators can always edit forum content
 170                  } elseif ( user_can( $user_id, 'moderate', $_post->ID ) ) {
 171                      $caps = array( 'spectate' );
 172  
 173                  // User is author so allow edit if not in admin, unless it's past edit lock time
 174                  } elseif ( ! is_admin() && ( (int) $user_id === (int) $_post->post_author ) ) {
 175  
 176                      // Only allow if not past the edit-lock period
 177                      $caps = ! bbp_past_edit_lock( $_post->post_date_gmt )
 178                          ? array( $post_type->cap->edit_posts )
 179                          : array( 'do_not_allow' );
 180  
 181                  // Unknown, so map to edit_others_posts
 182                  } else {
 183                      $caps = array( $post_type->cap->edit_others_posts );
 184                  }
 185              }
 186  
 187              break;
 188  
 189          /** Deleting **********************************************************/
 190  
 191          case 'delete_topic' :
 192  
 193              // Bail if no post ID
 194              if ( empty( $args[0] ) ) {
 195                  return $caps;
 196              }
 197  
 198              // Get the post.
 199              $_post = get_post( $args[0] );
 200              if ( ! empty( $_post ) ) {
 201  
 202                  // Get caps for post type object
 203                  $post_type = get_post_type_object( $_post->post_type );
 204  
 205                  // Add 'do_not_allow' cap if user is spam or deleted
 206                  if ( bbp_is_user_inactive( $user_id ) ) {
 207                      $caps = array( 'do_not_allow' );
 208  
 209                  // Moderators can always edit forum content
 210                  } elseif ( user_can( $user_id, 'moderate', $_post->ID ) ) {
 211                      $caps = array( 'spectate' );
 212  
 213                  // User is author so allow delete if not in admin
 214                  } elseif ( ! is_admin() && ( (int) $user_id === (int) $_post->post_author ) ) {
 215                      $caps = array( $post_type->cap->delete_posts );
 216  
 217                  // Unknown so map to delete_others_posts
 218                  } else {
 219                      $caps = array( $post_type->cap->delete_others_posts );
 220                  }
 221              }
 222  
 223              break;
 224  
 225          // Moderation override
 226          case 'delete_topics'         :
 227          case 'delete_others_topics'  :
 228  
 229              // Moderators can always delete
 230              if ( user_can( $user_id, 'moderate' ) ) {
 231                  $caps = array( $cap );
 232              }
 233  
 234              break;
 235  
 236          /** Admin *************************************************************/
 237  
 238          case 'bbp_topics_admin' :
 239              $caps = array( 'edit_topics' );
 240              break;
 241      }
 242  
 243      // Filter & return
 244      return (array) apply_filters( 'bbp_map_topic_meta_caps', $caps, $cap, $user_id, $args );
 245  }
 246  
 247  /**
 248   * Maps topic tag capabilities
 249   *
 250   * @since 2.2.0 bbPress (r4242)
 251   *
 252   * @param array $caps Capabilities for meta capability
 253   * @param string $cap Capability name
 254   * @param int $user_id User id
 255   * @param array $args Arguments
 256   *
 257   * @return array Actual capabilities for meta capability
 258   */
 259  function bbp_map_topic_tag_meta_caps( $caps, $cap, $user_id, $args ) {
 260  
 261      // What capability is being checked?
 262      switch ( $cap ) {
 263  
 264          /** Assignment ********************************************************/
 265  
 266          case 'assign_topic_tags' :
 267  
 268              // Get post
 269              $post_id = ! empty( $args[0] )
 270                  ? get_post( $args[0] )->ID
 271                  : 0;
 272  
 273              // Add 'do_not_allow' cap if user is spam or deleted
 274              if ( bbp_is_user_inactive( $user_id ) ) {
 275                  $caps = array( 'do_not_allow' );
 276  
 277              // Moderators can always assign
 278              } elseif ( user_can( $user_id, 'moderate', $post_id ) ) {
 279                  $caps = array( 'moderate' );
 280  
 281              // Do not allow if topic tags are disabled
 282              } elseif ( ! bbp_allow_topic_tags() ) {
 283                  $caps = array( 'do_not_allow' );
 284              }
 285  
 286              break;
 287  
 288          /** Management ********************************************************/
 289  
 290          case 'manage_topic_tags' :
 291  
 292              // Moderators can always edit
 293              if ( user_can( $user_id, 'moderate' ) ) {
 294                  $caps = array( 'moderate' );
 295              }
 296  
 297              break;
 298  
 299          /** Editing ***********************************************************/
 300  
 301          case 'edit_topic_tags' :
 302  
 303              // Moderators can always edit
 304              if ( user_can( $user_id, 'moderate' ) ) {
 305                  $caps = array( 'moderate' );
 306              }
 307  
 308              break;
 309  
 310          case 'edit_topic_tag' :
 311  
 312              // Get the term
 313              $_tag = get_term( $args[0], bbp_get_topic_tag_tax_id() );
 314              if ( ! empty( $_tag ) ) {
 315  
 316                  // Add 'do_not_allow' cap if user is spam or deleted
 317                  if ( bbp_is_user_inactive( $user_id ) ) {
 318                      $caps = array( 'do_not_allow' );
 319  
 320                  // Moderators can always edit topic tags
 321                  } elseif ( user_can( $user_id, 'moderate', $_tag->term_id ) ) {
 322                      $caps = array( 'spectate' );
 323  
 324                  // Fallback to edit_terms.
 325                  } else {
 326                      $taxonomy = get_taxonomy( bbp_get_topic_tag_tax_id() );
 327                      $caps     = array( $taxonomy->cap->edit_terms );
 328                  }
 329              }
 330  
 331              break;
 332  
 333          /** Deleting **********************************************************/
 334  
 335          case 'delete_topic_tags' :
 336  
 337              // Moderators can always edit
 338              if ( user_can( $user_id, 'moderate' ) ) {
 339                  $caps = array( 'moderate' );
 340              }
 341  
 342              break;
 343  
 344          case 'delete_topic_tag' :
 345  
 346              // Get the term
 347              $_tag = get_term( $args[0], bbp_get_topic_tag_tax_id() );
 348              if ( ! empty( $_tag ) ) {
 349  
 350                  // Add 'do_not_allow' cap if user is spam or deleted
 351                  if ( bbp_is_user_inactive( $user_id ) ) {
 352                      $caps = array( 'do_not_allow' );
 353  
 354                  // Moderators can always delete topic tags
 355                  } elseif ( user_can( $user_id, 'moderate', $_tag->term_id ) ) {
 356                      $caps = array( 'spectate' );
 357  
 358                  // Fallback to delete_terms.
 359                  } else {
 360                      $taxonomy = get_taxonomy( $_tag->post_type );
 361                      $caps     = array( $taxonomy->cap->delete_terms );
 362                  }
 363              }
 364  
 365              break;
 366  
 367          /** Admin *************************************************************/
 368  
 369          case 'bbp_topic_tags_admin' :
 370  
 371              // Moderators can always edit
 372              if ( user_can( $user_id, 'moderate' ) ) {
 373                  $caps = array( 'moderate' );
 374              }
 375  
 376              break;
 377      }
 378  
 379      // Filter & return
 380      return (array) apply_filters( 'bbp_map_topic_tag_meta_caps', $caps, $cap, $user_id, $args );
 381  }


Generated: Wed May 22 01:01:34 2019 Cross-referenced by PHPXref 0.7.1