/includes/forums/capabilities.php

  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 bbPress (r2593) 
  16. * 
  17. * @uses apply_filters() Calls 'bbp_get_forum_caps' with the capabilities 
  18. * @return array Forum capabilities 
  19. */ 
  20. function bbp_get_forum_caps() { 
  21. return apply_filters( 'bbp_get_forum_caps', array ( 
  22. 'edit_posts' => 'edit_forums',  
  23. 'edit_others_posts' => 'edit_others_forums',  
  24. 'publish_posts' => 'publish_forums',  
  25. 'read_private_posts' => 'read_private_forums',  
  26. 'read_hidden_posts' => 'read_hidden_forums',  
  27. 'delete_posts' => 'delete_forums',  
  28. 'delete_others_posts' => 'delete_others_forums' 
  29. ) ); 
  30.  
  31. /** 
  32. * Maps forum capabilities 
  33. * 
  34. * @since bbPress (r4242) 
  35. * 
  36. * @param array $caps Capabilities for meta capability 
  37. * @param string $cap Capability name 
  38. * @param int $user_id User id 
  39. * @param mixed $args Arguments 
  40. * @uses get_post() To get the post 
  41. * @uses get_post_type_object() To get the post type object 
  42. * @uses apply_filters() Filter capability map results 
  43. * @return array Actual capabilities for meta capability 
  44. */ 
  45. function bbp_map_forum_meta_caps( $caps = array(), $cap = '', $user_id = 0, $args = array() ) { 
  46.  
  47. // What capability is being checked? 
  48. switch ( $cap ) { 
  49.  
  50. /** Reading ***********************************************************/ 
  51.  
  52. case 'read_private_forums' : 
  53. case 'read_hidden_forums' : 
  54.  
  55. // Moderators can always read private/hidden forums 
  56. if ( user_can( $user_id, 'moderate' ) ) { 
  57. $caps = array( 'moderate' ); 
  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. // Get the post 
  71. $_post = get_post( $args[0] ); 
  72. if ( !empty( $_post ) ) { 
  73.  
  74. // Get caps for post type object 
  75. $post_type = get_post_type_object( $_post->post_type ); 
  76.  
  77. // Post is public 
  78. if ( bbp_get_public_status_id() === $_post->post_status ) { 
  79. $caps = array( 'spectate' ); 
  80.  
  81. // User is author so allow read 
  82. } elseif ( (int) $user_id === (int) $_post->post_author ) { 
  83. $caps = array( 'spectate' ); 
  84.  
  85. // Unknown so map to private posts 
  86. } else { 
  87. $caps = array( $post_type->cap->read_private_posts ); 
  88.  
  89. break; 
  90.  
  91. /** Publishing ********************************************************/ 
  92.  
  93. case 'publish_forums' : 
  94.  
  95. // Moderators can always edit 
  96. if ( user_can( $user_id, 'moderate' ) ) { 
  97. $caps = array( 'moderate' ); 
  98.  
  99. break; 
  100.  
  101. /** Editing ***********************************************************/ 
  102.  
  103. // Used primarily in wp-admin 
  104. case 'edit_forums' : 
  105. case 'edit_others_forums' : 
  106.  
  107. // Moderators can always edit 
  108. if ( user_can( $user_id, 'keep_gate' ) ) { 
  109. $caps = array( 'keep_gate' ); 
  110.  
  111. // Otherwise, block 
  112. } else { 
  113. $caps = array( 'do_not_allow' ); 
  114.  
  115. break; 
  116.  
  117. // Used everywhere 
  118. case 'edit_forum' : 
  119.  
  120. // Get the post 
  121. $_post = get_post( $args[0] ); 
  122. if ( !empty( $_post ) ) { 
  123.  
  124. // Get caps for post type object 
  125. $post_type = get_post_type_object( $_post->post_type ); 
  126. $caps = array(); 
  127.  
  128. // Add 'do_not_allow' cap if user is spam or deleted 
  129. if ( bbp_is_user_inactive( $user_id ) ) { 
  130. $caps[] = 'do_not_allow'; 
  131.  
  132. // User is author so allow edit if not in admin 
  133. } elseif ( !is_admin() && ( (int) $user_id === (int) $_post->post_author ) ) { 
  134. $caps[] = $post_type->cap->edit_posts; 
  135.  
  136. // Unknown, so map to edit_others_posts 
  137. } else { 
  138. $caps[] = $post_type->cap->edit_others_posts; 
  139.  
  140. break; 
  141.  
  142. /** Deleting **********************************************************/ 
  143.  
  144. // Allow forum authors to delete forums (for BuddyPress groups, etc) 
  145. case 'delete_forum' : 
  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. $caps = array(); 
  154.  
  155. // Add 'do_not_allow' cap if user is spam or deleted 
  156. if ( bbp_is_user_inactive( $user_id ) ) { 
  157. $caps[] = 'do_not_allow'; 
  158.  
  159. // User is author so allow to delete 
  160. } elseif ( (int) $user_id === (int) $_post->post_author ) { 
  161. $caps[] = $post_type->cap->delete_posts; 
  162.  
  163. // Unknown so map to delete_others_posts 
  164. } else { 
  165. $caps[] = $post_type->cap->delete_others_posts; 
  166.  
  167. break; 
  168.  
  169. /** Admin *************************************************************/ 
  170.  
  171. case 'bbp_forums_admin' : 
  172. $caps = array( 'keep_gate' ); 
  173. break; 
  174.  
  175. return apply_filters( 'bbp_map_forum_meta_caps', $caps, $cap, $user_id, $args ); 
.