[ 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                      break;
  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                  break;
 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                      // If editing...
 177                      if ( bbp_is_topic_edit() ) {
 178  
 179                          // Only allow if not past the edit-lock period
 180                          $caps = ! bbp_past_edit_lock( $_post->post_date_gmt )
 181                              ? array( $post_type->cap->edit_posts )
 182                              : array( 'do_not_allow' );
 183  
 184                      // Otherwise...
 185                      } else {
 186                          $caps = array( $post_type->cap->edit_posts );
 187                      }
 188  
 189                  // Unknown, so map to edit_others_posts
 190                  } else {
 191                      $caps = array( $post_type->cap->edit_others_posts );
 192                  }
 193              }
 194  
 195              break;
 196  
 197          /** Deleting **********************************************************/
 198  
 199          case 'delete_topic' :
 200  
 201              // Bail if no post ID
 202              if ( empty( $args[0] ) ) {
 203                  break;
 204              }
 205  
 206              // Get the post.
 207              $_post = get_post( $args[0] );
 208              if ( ! empty( $_post ) ) {
 209  
 210                  // Get caps for post type object
 211                  $post_type = get_post_type_object( $_post->post_type );
 212  
 213                  // Add 'do_not_allow' cap if user is spam or deleted
 214                  if ( bbp_is_user_inactive( $user_id ) ) {
 215                      $caps = array( 'do_not_allow' );
 216  
 217                  // Moderators can always edit forum content
 218                  } elseif ( user_can( $user_id, 'moderate', $_post->ID ) ) {
 219                      $caps = array( 'spectate' );
 220  
 221                  // User is author so allow delete if not in admin
 222                  } elseif ( ! is_admin() && ( (int) $user_id === (int) $_post->post_author ) ) {
 223                      $caps = array( $post_type->cap->delete_posts );
 224  
 225                  // Unknown so map to delete_others_posts
 226                  } else {
 227                      $caps = array( $post_type->cap->delete_others_posts );
 228                  }
 229              }
 230  
 231              break;
 232  
 233          // Moderation override
 234          case 'delete_topics'         :
 235          case 'delete_others_topics'  :
 236  
 237              // Moderators can always delete
 238              if ( user_can( $user_id, 'moderate' ) ) {
 239                  $caps = array( $cap );
 240              }
 241  
 242              break;
 243  
 244          /** Admin *************************************************************/
 245  
 246          case 'bbp_topics_admin' :
 247              $caps = array( 'edit_topics' );
 248              break;
 249      }
 250  
 251      // Filter & return
 252      return (array) apply_filters( 'bbp_map_topic_meta_caps', $caps, $cap, $user_id, $args );
 253  }
 254  
 255  /**
 256   * Maps topic tag capabilities
 257   *
 258   * @since 2.2.0 bbPress (r4242)
 259   *
 260   * @param array $caps Capabilities for meta capability
 261   * @param string $cap Capability name
 262   * @param int $user_id User id
 263   * @param array $args Arguments
 264   *
 265   * @return array Actual capabilities for meta capability
 266   */
 267  function bbp_map_topic_tag_meta_caps( $caps, $cap, $user_id, $args ) {
 268  
 269      // What capability is being checked?
 270      switch ( $cap ) {
 271  
 272          /** Assignment ********************************************************/
 273  
 274          case 'assign_topic_tags' :
 275  
 276              // Get post
 277              $post_id = ! empty( $args[0] )
 278                  ? get_post( $args[0] )->ID
 279                  : 0;
 280  
 281              // Add 'do_not_allow' cap if user is spam or deleted
 282              if ( bbp_is_user_inactive( $user_id ) ) {
 283                  $caps = array( 'do_not_allow' );
 284  
 285              // Moderators can always assign
 286              } elseif ( user_can( $user_id, 'moderate', $post_id ) ) {
 287                  $caps = array( 'moderate' );
 288  
 289              // Do not allow if topic tags are disabled
 290              } elseif ( ! bbp_allow_topic_tags() ) {
 291                  $caps = array( 'do_not_allow' );
 292              }
 293  
 294              break;
 295  
 296          /** Management ********************************************************/
 297  
 298          case 'manage_topic_tags' :
 299  
 300              // Moderators can always edit
 301              if ( user_can( $user_id, 'moderate' ) ) {
 302                  $caps = array( 'moderate' );
 303              }
 304  
 305              break;
 306  
 307          /** Editing ***********************************************************/
 308  
 309          case 'edit_topic_tags' :
 310  
 311              // Moderators can always edit
 312              if ( user_can( $user_id, 'moderate' ) ) {
 313                  $caps = array( 'moderate' );
 314              }
 315  
 316              break;
 317  
 318          case 'edit_topic_tag' :
 319  
 320              // Get the term
 321              $_tag = get_term( $args[0], bbp_get_topic_tag_tax_id() );
 322              if ( ! empty( $_tag ) ) {
 323  
 324                  // Add 'do_not_allow' cap if user is spam or deleted
 325                  if ( bbp_is_user_inactive( $user_id ) ) {
 326                      $caps = array( 'do_not_allow' );
 327  
 328                  // Moderators can always edit topic tags
 329                  } elseif ( user_can( $user_id, 'moderate', $_tag->term_id ) ) {
 330                      $caps = array( 'spectate' );
 331  
 332                  // Fallback to edit_terms.
 333                  } else {
 334                      $taxonomy = get_taxonomy( bbp_get_topic_tag_tax_id() );
 335                      $caps     = array( $taxonomy->cap->edit_terms );
 336                  }
 337              }
 338  
 339              break;
 340  
 341          /** Deleting **********************************************************/
 342  
 343          case 'delete_topic_tags' :
 344  
 345              // Moderators can always edit
 346              if ( user_can( $user_id, 'moderate' ) ) {
 347                  $caps = array( 'moderate' );
 348              }
 349  
 350              break;
 351  
 352          case 'delete_topic_tag' :
 353  
 354              // Get the term
 355              $_tag = get_term( $args[0], bbp_get_topic_tag_tax_id() );
 356              if ( ! empty( $_tag ) ) {
 357  
 358                  // Add 'do_not_allow' cap if user is spam or deleted
 359                  if ( bbp_is_user_inactive( $user_id ) ) {
 360                      $caps = array( 'do_not_allow' );
 361  
 362                  // Moderators can always delete topic tags
 363                  } elseif ( user_can( $user_id, 'moderate', $_tag->term_id ) ) {
 364                      $caps = array( 'spectate' );
 365  
 366                  // Fallback to delete_terms.
 367                  } else {
 368                      $taxonomy = get_taxonomy( $_tag->post_type );
 369                      $caps     = array( $taxonomy->cap->delete_terms );
 370                  }
 371              }
 372  
 373              break;
 374  
 375          /** Admin *************************************************************/
 376  
 377          case 'bbp_topic_tags_admin' :
 378  
 379              // Moderators can always edit
 380              if ( user_can( $user_id, 'moderate' ) ) {
 381                  $caps = array( 'moderate' );
 382              }
 383  
 384              break;
 385      }
 386  
 387      // Filter & return
 388      return (array) apply_filters( 'bbp_map_topic_tag_meta_caps', $caps, $cap, $user_id, $args );
 389  }


Generated: Tue Dec 10 01:01:28 2019 Cross-referenced by PHPXref 0.7.1