/includes/replies/capabilities.php

  1. <?php 
  2.  
  3. /** 
  4. * bbPress Reply Capabilites 
  5. * 
  6. * Used to map reply capabilities to WordPress's existing capabilities. 
  7. * 
  8. * @package bbPress 
  9. * @subpackage Capabilities 
  10. */ 
  11.  
  12. /** 
  13. * Return reply capabilities 
  14. * 
  15. * @since bbPress (r2593) 
  16. * 
  17. * @uses apply_filters() Calls 'bbp_get_reply_caps' with the capabilities 
  18. * @return array Reply capabilities 
  19. */ 
  20. function bbp_get_reply_caps() { 
  21. return apply_filters( 'bbp_get_reply_caps', array ( 
  22. 'edit_posts' => 'edit_replies',  
  23. 'edit_others_posts' => 'edit_others_replies',  
  24. 'publish_posts' => 'publish_replies',  
  25. 'read_private_posts' => 'read_private_replies',  
  26. 'delete_posts' => 'delete_replies',  
  27. 'delete_others_posts' => 'delete_others_replies' 
  28. ) ); 
  29.  
  30. /** 
  31. * Maps topic capabilities 
  32. * 
  33. * @since bbPress (r4242) 
  34. * 
  35. * @param array $caps Capabilities for meta capability 
  36. * @param string $cap Capability name 
  37. * @param int $user_id User id 
  38. * @param mixed $args Arguments 
  39. * @uses get_post() To get the post 
  40. * @uses get_post_type_object() To get the post type object 
  41. * @uses apply_filters() Filter mapped results 
  42. * @return array Actual capabilities for meta capability 
  43. */ 
  44. function bbp_map_reply_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_reply' : 
  52.  
  53. // User cannot spectate 
  54. if ( ! user_can( $user_id, 'spectate' ) ) { 
  55. $caps = array( 'do_not_allow' ); 
  56.  
  57. // Do some post ID based logic 
  58. } else { 
  59.  
  60. // Get the post 
  61. $_post = get_post( $args[0] ); 
  62. if ( !empty( $_post ) ) { 
  63.  
  64. // Get caps for post type object 
  65. $post_type = get_post_type_object( $_post->post_type ); 
  66.  
  67. // Post is public 
  68. if ( bbp_get_public_status_id() === $_post->post_status ) { 
  69. $caps = array( 'spectate' ); 
  70.  
  71. // User is author so allow read 
  72. } elseif ( (int) $user_id === (int) $_post->post_author ) { 
  73. $caps = array( 'spectate' ); 
  74.  
  75. // Unknown so map to private posts 
  76. } else { 
  77. $caps = array( $post_type->cap->read_private_posts ); 
  78.  
  79. break; 
  80.  
  81. /** Publishing ********************************************************/ 
  82.  
  83. case 'publish_replies' : 
  84.  
  85. // Moderators can always publish 
  86. if ( user_can( $user_id, 'moderate' ) ) { 
  87. $caps = array( 'moderate' ); 
  88.  
  89. break; 
  90.  
  91. /** Editing ***********************************************************/ 
  92.  
  93. // Used primarily in wp-admin 
  94. case 'edit_replies' : 
  95. case 'edit_others_replies' : 
  96.  
  97. // Moderators can always edit 
  98. if ( user_can( $user_id, 'moderate' ) ) { 
  99. $caps = array( 'moderate' ); 
  100.  
  101. // Otherwise, block 
  102. } else { 
  103. $caps = array( 'do_not_allow' ); 
  104.  
  105. break; 
  106.  
  107. // Used everywhere 
  108. case 'edit_reply' : 
  109.  
  110. // Get the post 
  111. $_post = get_post( $args[0] ); 
  112. if ( !empty( $_post ) ) { 
  113.  
  114. // Get caps for post type object 
  115. $post_type = get_post_type_object( $_post->post_type ); 
  116. $caps = array(); 
  117.  
  118. // Add 'do_not_allow' cap if user is spam or deleted 
  119. if ( bbp_is_user_inactive( $user_id ) ) { 
  120. $caps[] = 'do_not_allow'; 
  121.  
  122. // User is author so allow edit if not in admin 
  123. } elseif ( !is_admin() && ( (int) $user_id === (int) $_post->post_author ) ) { 
  124. $caps[] = $post_type->cap->edit_posts; 
  125.  
  126. // Unknown, so map to edit_others_posts 
  127. } else { 
  128. $caps[] = $post_type->cap->edit_others_posts; 
  129.  
  130. break; 
  131.  
  132. /** Deleting **********************************************************/ 
  133.  
  134. case 'delete_reply' : 
  135.  
  136. // Get the post 
  137. $_post = get_post( $args[0] ); 
  138. if ( !empty( $_post ) ) { 
  139.  
  140. // Get caps for post type object 
  141. $post_type = get_post_type_object( $_post->post_type ); 
  142. $caps = array(); 
  143.  
  144. // Add 'do_not_allow' cap if user is spam or deleted 
  145. if ( bbp_is_user_inactive( $user_id ) ) { 
  146. $caps[] = 'do_not_allow'; 
  147.  
  148. // Moderators can always edit forum content 
  149. } elseif ( user_can( $user_id, 'moderate' ) ) { 
  150. $caps[] = 'moderate'; 
  151.  
  152. // Unknown so map to delete_others_posts 
  153. } else { 
  154. $caps[] = $post_type->cap->delete_others_posts; 
  155.  
  156. break; 
  157.  
  158. // Moderation override 
  159. case 'delete_replies' : 
  160. case 'delete_others_replies' : 
  161.  
  162. // Moderators can always delete 
  163. if ( user_can( $user_id, 'moderate' ) ) { 
  164. $caps = array( 'moderate' ); 
  165.  
  166. break; 
  167.  
  168. /** Admin *************************************************************/ 
  169.  
  170. case 'bbp_replies_admin' : 
  171. $caps = array( 'moderate' ); 
  172. break; 
  173.  
  174. return apply_filters( 'bbp_map_reply_meta_caps', $caps, $cap, $user_id, $args ); 
.