/includes/topics/capabilities.php

  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 bbPress (r2593) 
  16. * 
  17. * @uses apply_filters() Calls 'bbp_get_topic_caps' with the capabilities 
  18. * @return array Topic capabilities 
  19. */ 
  20. function bbp_get_topic_caps() { 
  21. return apply_filters( 'bbp_get_topic_caps', array ( 
  22. 'edit_posts' => 'edit_topics',  
  23. 'edit_others_posts' => 'edit_others_topics',  
  24. 'publish_posts' => 'publish_topics',  
  25. 'read_private_posts' => 'read_private_topics',  
  26. 'read_hidden_posts' => 'read_hidden_topics',  
  27. 'delete_posts' => 'delete_topics',  
  28. 'delete_others_posts' => 'delete_others_topics' 
  29. ) ); 
  30.  
  31. /** 
  32. * Return topic tag capabilities 
  33. * 
  34. * @since bbPress (r2593) 
  35. * 
  36. * @uses apply_filters() Calls 'bbp_get_topic_tag_caps' with the capabilities 
  37. * @return array Topic tag capabilities 
  38. */ 
  39. function bbp_get_topic_tag_caps() { 
  40. return apply_filters( 'bbp_get_topic_tag_caps', array ( 
  41. 'manage_terms' => 'manage_topic_tags',  
  42. 'edit_terms' => 'edit_topic_tags',  
  43. 'delete_terms' => 'delete_topic_tags',  
  44. 'assign_terms' => 'assign_topic_tags' 
  45. ) ); 
  46.  
  47. /** 
  48. * Maps topic capabilities 
  49. * 
  50. * @since bbPress (r4242) 
  51. * 
  52. * @param array $caps Capabilities for meta capability 
  53. * @param string $cap Capability name 
  54. * @param int $user_id User id 
  55. * @param mixed $args Arguments 
  56. * @uses get_post() To get the post 
  57. * @uses get_post_type_object() To get the post type object 
  58. * @uses apply_filters() Filter capability map results 
  59. * @return array Actual capabilities for meta capability 
  60. */ 
  61. function bbp_map_topic_meta_caps( $caps = array(), $cap = '', $user_id = 0, $args = array() ) { 
  62.  
  63. // What capability is being checked? 
  64. switch ( $cap ) { 
  65.  
  66. /** Reading ***********************************************************/ 
  67.  
  68. case 'read_topic' : 
  69.  
  70. // User cannot spectate 
  71. if ( ! user_can( $user_id, 'spectate' ) ) { 
  72. $caps = array( 'do_not_allow' ); 
  73.  
  74. // Do some post ID based logic 
  75. } else { 
  76.  
  77. // Get the post 
  78. $_post = get_post( $args[0] ); 
  79. if ( !empty( $_post ) ) { 
  80.  
  81. // Get caps for post type object 
  82. $post_type = get_post_type_object( $_post->post_type ); 
  83.  
  84. // Post is public 
  85. if ( bbp_get_public_status_id() === $_post->post_status ) { 
  86. $caps = array( 'spectate' ); 
  87.  
  88. // User is author so allow read 
  89. } elseif ( (int) $user_id === (int) $_post->post_author ) { 
  90. $caps = array( 'spectate' ); 
  91.  
  92. // Unknown so map to private posts 
  93. } else { 
  94. $caps = array( $post_type->cap->read_private_posts ); 
  95.  
  96. break; 
  97.  
  98. /** Publishing ********************************************************/ 
  99.  
  100. case 'publish_topics' : 
  101.  
  102. // Moderators can always publish 
  103. if ( user_can( $user_id, 'moderate' ) ) { 
  104. $caps = array( 'moderate' ); 
  105.  
  106. break; 
  107.  
  108. /** Editing ***********************************************************/ 
  109.  
  110. // Used primarily in wp-admin 
  111. case 'edit_topics' : 
  112. case 'edit_others_topics' : 
  113.  
  114. // Moderators can always edit 
  115. if ( user_can( $user_id, 'moderate' ) ) { 
  116. $caps = array( $cap ); 
  117.  
  118. // Otherwise, block 
  119. } else { 
  120. $caps = array( 'do_not_allow' ); 
  121.  
  122. break; 
  123.  
  124. // Used everywhere 
  125. case 'edit_topic' : 
  126.  
  127. // Get the post 
  128. $_post = get_post( $args[0] ); 
  129. if ( !empty( $_post ) ) { 
  130.  
  131. // Get caps for post type object 
  132. $post_type = get_post_type_object( $_post->post_type ); 
  133. $caps = array(); 
  134.  
  135. // Add 'do_not_allow' cap if user is spam or deleted 
  136. if ( bbp_is_user_inactive( $user_id ) ) { 
  137. $caps[] = 'do_not_allow'; 
  138.  
  139. // User is author so allow edit if not in admin 
  140. } elseif ( !is_admin() && ( (int) $user_id === (int) $_post->post_author ) ) { 
  141. $caps[] = $post_type->cap->edit_posts; 
  142.  
  143. // Unknown, so map to edit_others_posts 
  144. } else { 
  145. $caps[] = $post_type->cap->edit_others_posts; 
  146.  
  147. break; 
  148.  
  149. /** Deleting **********************************************************/ 
  150.  
  151. case 'delete_topic' : 
  152.  
  153. // Get the post 
  154. $_post = get_post( $args[0] ); 
  155. if ( !empty( $_post ) ) { 
  156.  
  157. // Get caps for post type object 
  158. $post_type = get_post_type_object( $_post->post_type ); 
  159. $caps = array(); 
  160.  
  161. // Add 'do_not_allow' cap if user is spam or deleted 
  162. if ( bbp_is_user_inactive( $user_id ) ) { 
  163. $caps[] = 'do_not_allow'; 
  164.  
  165. // Moderators can always edit forum content 
  166. } elseif ( user_can( $user_id, 'moderate' ) ) { 
  167. $caps[] = 'moderate'; 
  168.  
  169. // Unknown so map to delete_others_posts 
  170. } else { 
  171. $caps[] = $post_type->cap->delete_others_posts; 
  172.  
  173. break; 
  174.  
  175. // Moderation override 
  176. case 'delete_topics' : 
  177. case 'delete_others_topics' : 
  178.  
  179. // Moderators can always delete 
  180. if ( user_can( $user_id, 'moderate' ) ) { 
  181. $caps = array( $cap ); 
  182.  
  183. break; 
  184.  
  185. /** Admin *************************************************************/ 
  186.  
  187. case 'bbp_topics_admin' : 
  188. $caps = array( 'moderate' ); 
  189. break; 
  190.  
  191. return apply_filters( 'bbp_map_topic_meta_caps', $caps, $cap, $user_id, $args ); 
  192.  
  193. /** 
  194. * Maps topic tag capabilities 
  195. * 
  196. * @since bbPress (r4242) 
  197. * 
  198. * @param array $caps Capabilities for meta capability 
  199. * @param string $cap Capability name 
  200. * @param int $user_id User id 
  201. * @param mixed $args Arguments 
  202. * @uses apply_filters() Filter capability map results 
  203. * @return array Actual capabilities for meta capability 
  204. */ 
  205. function bbp_map_topic_tag_meta_caps( $caps, $cap, $user_id, $args ) { 
  206.  
  207. // What capability is being checked? 
  208. switch ( $cap ) { 
  209. case 'manage_topic_tags' : 
  210. case 'edit_topic_tags' : 
  211. case 'delete_topic_tags' : 
  212. case 'assign_topic_tags' : 
  213. case 'bbp_topic_tags_admin' : 
  214.  
  215. // Moderators can always edit 
  216. if ( user_can( $user_id, 'moderate' ) ) { 
  217. $caps = array( 'moderate' ); 
  218.  
  219. return apply_filters( 'bbp_map_topic_tag_meta_caps', $caps, $cap, $user_id, $args ); 
.