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