web/wp-content/plugins/bbpress/includes/core/capabilities.php
changeset 196 5e8dcbe22c24
equal deleted inserted replaced
195:c7c0fbc09788 196:5e8dcbe22c24
       
     1 <?php
       
     2 
       
     3 /**
       
     4  * bbPress Capabilites
       
     5  *
       
     6  * The functions in this file are used primarily as convenient wrappers for
       
     7  * capability output in user profiles. This includes mapping capabilities and
       
     8  * groups to human readable strings,
       
     9  *
       
    10  * @package bbPress
       
    11  * @subpackage Capabilities
       
    12  */
       
    13 
       
    14 // Exit if accessed directly
       
    15 if ( !defined( 'ABSPATH' ) ) exit;
       
    16 
       
    17 /** Mapping *******************************************************************/
       
    18 
       
    19 /**
       
    20  * Returns an array of capabilities based on the role that is being requested.
       
    21  *
       
    22  * @since bbPress (r2994)
       
    23  *
       
    24  * @todo Map all of these and deprecate
       
    25  *
       
    26  * @param string $role Optional. Defaults to The role to load caps for
       
    27  * @uses apply_filters() Allow return value to be filtered
       
    28  *
       
    29  * @return array Capabilities for $role
       
    30  */
       
    31 function bbp_get_caps_for_role( $role = '' ) {
       
    32 
       
    33 	// Which role are we looking for?
       
    34 	switch ( $role ) {
       
    35 
       
    36 		// Keymaster
       
    37 		case bbp_get_keymaster_role() :
       
    38 			$caps = array(
       
    39 
       
    40 				// Keymasters only
       
    41 				'keep_gate'             => true,
       
    42 
       
    43 				// Primary caps
       
    44 				'spectate'              => true,
       
    45 				'participate'           => true,
       
    46 				'moderate'              => true,
       
    47 				'throttle'              => true,
       
    48 				'view_trash'            => true,
       
    49 
       
    50 				// Forum caps
       
    51 				'publish_forums'        => true,
       
    52 				'edit_forums'           => true,
       
    53 				'edit_others_forums'    => true,
       
    54 				'delete_forums'         => true,
       
    55 				'delete_others_forums'  => true,
       
    56 				'read_private_forums'   => true,
       
    57 				'read_hidden_forums'    => true,
       
    58 
       
    59 				// Topic caps
       
    60 				'publish_topics'        => true,
       
    61 				'edit_topics'           => true,
       
    62 				'edit_others_topics'    => true,
       
    63 				'delete_topics'         => true,
       
    64 				'delete_others_topics'  => true,
       
    65 				'read_private_topics'   => true,
       
    66 
       
    67 				// Reply caps
       
    68 				'publish_replies'       => true,
       
    69 				'edit_replies'          => true,
       
    70 				'edit_others_replies'   => true,
       
    71 				'delete_replies'        => true,
       
    72 				'delete_others_replies' => true,
       
    73 				'read_private_replies'  => true,
       
    74 
       
    75 				// Topic tag caps
       
    76 				'manage_topic_tags'     => true,
       
    77 				'edit_topic_tags'       => true,
       
    78 				'delete_topic_tags'     => true,
       
    79 				'assign_topic_tags'     => true
       
    80 			);
       
    81 
       
    82 			break;
       
    83 
       
    84 		// Moderator
       
    85 		case bbp_get_moderator_role() :
       
    86 			$caps = array(
       
    87 
       
    88 				// Primary caps
       
    89 				'spectate'              => true,
       
    90 				'participate'           => true,
       
    91 				'moderate'              => true,
       
    92 				'throttle'              => true,
       
    93 				'view_trash'            => false,
       
    94 
       
    95 				// Forum caps
       
    96 				'publish_forums'        => true,
       
    97 				'edit_forums'           => true,
       
    98 				'edit_others_forums'    => false,
       
    99 				'delete_forums'         => false,
       
   100 				'delete_others_forums'  => false,
       
   101 				'read_private_forums'   => true,
       
   102 				'read_hidden_forums'    => true,
       
   103 
       
   104 				// Topic caps
       
   105 				'publish_topics'        => true,
       
   106 				'edit_topics'           => true,
       
   107 				'edit_others_topics'    => true,
       
   108 				'delete_topics'         => true,
       
   109 				'delete_others_topics'  => true,
       
   110 				'read_private_topics'   => true,
       
   111 
       
   112 				// Reply caps
       
   113 				'publish_replies'       => true,
       
   114 				'edit_replies'          => true,
       
   115 				'edit_others_replies'   => true,
       
   116 				'delete_replies'        => true,
       
   117 				'delete_others_replies' => true,
       
   118 				'read_private_replies'  => true,
       
   119 
       
   120 				// Topic tag caps
       
   121 				'manage_topic_tags'     => true,
       
   122 				'edit_topic_tags'       => true,
       
   123 				'delete_topic_tags'     => true,
       
   124 				'assign_topic_tags'     => true,
       
   125 			);
       
   126 
       
   127 			break;
       
   128 
       
   129 		// Spectators can only read
       
   130 		case bbp_get_spectator_role()   :
       
   131 			$caps = array(
       
   132 
       
   133 				// Primary caps
       
   134 				'spectate'              => true,
       
   135 				'participate'           => false,
       
   136 				'moderate'              => false,
       
   137 				'throttle'              => false,
       
   138 				'view_trash'            => false,
       
   139 
       
   140 				// Forum caps
       
   141 				'publish_forums'        => false,
       
   142 				'edit_forums'           => false,
       
   143 				'edit_others_forums'    => false,
       
   144 				'delete_forums'         => false,
       
   145 				'delete_others_forums'  => false,
       
   146 				'read_private_forums'   => false,
       
   147 				'read_hidden_forums'    => false,
       
   148 
       
   149 				// Topic caps
       
   150 				'publish_topics'        => false,
       
   151 				'edit_topics'           => false,
       
   152 				'edit_others_topics'    => false,
       
   153 				'delete_topics'         => false,
       
   154 				'delete_others_topics'  => false,
       
   155 				'read_private_topics'   => false,
       
   156 
       
   157 				// Reply caps
       
   158 				'publish_replies'       => false,
       
   159 				'edit_replies'          => false,
       
   160 				'edit_others_replies'   => false,
       
   161 				'delete_replies'        => false,
       
   162 				'delete_others_replies' => false,
       
   163 				'read_private_replies'  => false,
       
   164 
       
   165 				// Topic tag caps
       
   166 				'manage_topic_tags'     => false,
       
   167 				'edit_topic_tags'       => false,
       
   168 				'delete_topic_tags'     => false,
       
   169 				'assign_topic_tags'     => false,
       
   170 			);
       
   171 
       
   172 			break;
       
   173 
       
   174 		// Explicitly blocked
       
   175 		case bbp_get_blocked_role() :
       
   176 			$caps = array(
       
   177 
       
   178 				// Primary caps
       
   179 				'spectate'              => false,
       
   180 				'participate'           => false,
       
   181 				'moderate'              => false,
       
   182 				'throttle'              => false,
       
   183 				'view_trash'            => false,
       
   184 
       
   185 				// Forum caps
       
   186 				'publish_forums'        => false,
       
   187 				'edit_forums'           => false,
       
   188 				'edit_others_forums'    => false,
       
   189 				'delete_forums'         => false,
       
   190 				'delete_others_forums'  => false,
       
   191 				'read_private_forums'   => false,
       
   192 				'read_hidden_forums'    => false,
       
   193 
       
   194 				// Topic caps
       
   195 				'publish_topics'        => false,
       
   196 				'edit_topics'           => false,
       
   197 				'edit_others_topics'    => false,
       
   198 				'delete_topics'         => false,
       
   199 				'delete_others_topics'  => false,
       
   200 				'read_private_topics'   => false,
       
   201 
       
   202 				// Reply caps
       
   203 				'publish_replies'       => false,
       
   204 				'edit_replies'          => false,
       
   205 				'edit_others_replies'   => false,
       
   206 				'delete_replies'        => false,
       
   207 				'delete_others_replies' => false,
       
   208 				'read_private_replies'  => false,
       
   209 
       
   210 				// Topic tag caps
       
   211 				'manage_topic_tags'     => false,
       
   212 				'edit_topic_tags'       => false,
       
   213 				'delete_topic_tags'     => false,
       
   214 				'assign_topic_tags'     => false,
       
   215 			);
       
   216 
       
   217 			break;
       
   218 
       
   219 		// Participant/Default
       
   220 		case bbp_get_participant_role() :
       
   221 		default :
       
   222 			$caps = array(
       
   223 
       
   224 				// Primary caps
       
   225 				'spectate'              => true,
       
   226 				'participate'           => true,
       
   227 				'moderate'              => false,
       
   228 				'throttle'              => false,
       
   229 				'view_trash'            => false,
       
   230 
       
   231 				// Forum caps
       
   232 				'publish_forums'        => false,
       
   233 				'edit_forums'           => false,
       
   234 				'edit_others_forums'    => false,
       
   235 				'delete_forums'         => false,
       
   236 				'delete_others_forums'  => false,
       
   237 				'read_private_forums'   => true,
       
   238 				'read_hidden_forums'    => false,
       
   239 
       
   240 				// Topic caps
       
   241 				'publish_topics'        => true,
       
   242 				'edit_topics'           => true,
       
   243 				'edit_others_topics'    => false,
       
   244 				'delete_topics'         => false,
       
   245 				'delete_others_topics'  => false,
       
   246 				'read_private_topics'   => false,
       
   247 
       
   248 				// Reply caps
       
   249 				'publish_replies'       => true,
       
   250 				'edit_replies'          => true,
       
   251 				'edit_others_replies'   => false,
       
   252 				'delete_replies'        => false,
       
   253 				'delete_others_replies' => false,
       
   254 				'read_private_replies'  => false,
       
   255 
       
   256 				// Topic tag caps
       
   257 				'manage_topic_tags'     => false,
       
   258 				'edit_topic_tags'       => false,
       
   259 				'delete_topic_tags'     => false,
       
   260 				'assign_topic_tags'     => true,
       
   261 			);
       
   262 
       
   263 			break;
       
   264 	}
       
   265 
       
   266 	return apply_filters( 'bbp_get_caps_for_role', $caps, $role );
       
   267 }
       
   268 
       
   269 /**
       
   270  * Adds capabilities to WordPress user roles.
       
   271  *
       
   272  * @since bbPress (r2608)
       
   273  */
       
   274 function bbp_add_caps() {
       
   275 
       
   276 	// Loop through available roles and add caps
       
   277 	foreach( bbp_get_wp_roles()->role_objects as $role ) {
       
   278 		foreach ( bbp_get_caps_for_role( $role->name ) as $cap => $value ) {
       
   279 			$role->add_cap( $cap, $value );
       
   280 		}
       
   281 	}
       
   282 
       
   283 	do_action( 'bbp_add_caps' );
       
   284 }
       
   285 
       
   286 /**
       
   287  * Removes capabilities from WordPress user roles.
       
   288  *
       
   289  * @since bbPress (r2608)
       
   290  */
       
   291 function bbp_remove_caps() {
       
   292 
       
   293 	// Loop through available roles and remove caps
       
   294 	foreach( bbp_get_wp_roles()->role_objects as $role ) {
       
   295 		foreach ( array_keys( bbp_get_caps_for_role( $role->name ) ) as $cap ) {
       
   296 			$role->remove_cap( $cap );
       
   297 		}
       
   298 	}
       
   299 
       
   300 	do_action( 'bbp_remove_caps' );
       
   301 }
       
   302 
       
   303 /**
       
   304  * Get the $wp_roles global without needing to declare it everywhere
       
   305  *
       
   306  * @since bbPress (r4293)
       
   307  *
       
   308  * @global WP_Roles $wp_roles
       
   309  * @return WP_Roles
       
   310  */
       
   311 function bbp_get_wp_roles() {
       
   312 	global $wp_roles;
       
   313 
       
   314 	// Load roles if not set
       
   315 	if ( ! isset( $wp_roles ) )
       
   316 		$wp_roles = new WP_Roles();
       
   317 
       
   318 	return $wp_roles;
       
   319 }
       
   320 
       
   321 /** Forum Roles ***************************************************************/
       
   322 
       
   323 /**
       
   324  * Add the bbPress roles to the $wp_roles global.
       
   325  *
       
   326  * We do this to avoid adding these values to the database.
       
   327  *
       
   328  * @since bbPress (r4290)
       
   329  */
       
   330 function bbp_add_forums_roles() {
       
   331 	$wp_roles = bbp_get_wp_roles();
       
   332 
       
   333 	foreach( bbp_get_dynamic_roles() as $role_id => $details ) {
       
   334 		$wp_roles->roles[$role_id]        = $details;
       
   335 		$wp_roles->role_objects[$role_id] = new WP_Role( $details['name'], $details['capabilities'] );
       
   336 		$wp_roles->role_names[$role_id]   = $details['name'];
       
   337 	}
       
   338 }
       
   339 
       
   340 /**
       
   341  * Helper function to add filter to option_wp_user_roles
       
   342  *
       
   343  * @since bbPress (r4363)
       
   344  *
       
   345  * @see _bbp_reinit_dynamic_roles()
       
   346  *
       
   347  * @global WPDB $wpdb Used to get the database prefix
       
   348  */
       
   349 function bbp_filter_user_roles_option() {
       
   350 	global $wpdb;
       
   351 
       
   352 	$role_key = $wpdb->prefix . 'user_roles';
       
   353 
       
   354 	add_filter( 'option_' . $role_key, '_bbp_reinit_dynamic_roles' );
       
   355 }
       
   356 
       
   357 /**
       
   358  * This is necessary because in a few places (noted below) WordPress initializes
       
   359  * a blog's roles directly from the database option. When this happens, the
       
   360  * $wp_roles global gets flushed, causing a user to magically lose any
       
   361  * dynamically assigned roles or capabilities when $current_user in refreshed.
       
   362  *
       
   363  * Because dynamic multiple roles is a new concept in WordPress, we work around
       
   364  * it here for now, knowing that improvements will come to WordPress core later.
       
   365  *
       
   366  * @see switch_to_blog()
       
   367  * @see restore_current_blog()
       
   368  * @see WP_Roles::_init()
       
   369  *
       
   370  * @since bbPress (r4363)
       
   371  *
       
   372  * @internal Used by bbPress to reinitialize dynamic roles on blog switch
       
   373  *
       
   374  * @param array $roles
       
   375  * @return array Combined array of database roles and dynamic bbPress roles
       
   376  */
       
   377 function _bbp_reinit_dynamic_roles( $roles = array() ) {
       
   378 	foreach( bbp_get_dynamic_roles() as $role_id => $details ) {
       
   379 		$roles[$role_id] = $details;
       
   380 	}
       
   381 	return $roles;
       
   382 }
       
   383 
       
   384 /**
       
   385  * Fetch a filtered list of forum roles that the current user is
       
   386  * allowed to have.
       
   387  *
       
   388  * Simple function who's main purpose is to allow filtering of the
       
   389  * list of forum roles so that plugins can remove inappropriate ones depending
       
   390  * on the situation or user making edits.
       
   391  *
       
   392  * Specifically because without filtering, anyone with the edit_users
       
   393  * capability can edit others to be administrators, even if they are
       
   394  * only editors or authors. This filter allows admins to delegate
       
   395  * user management.
       
   396  *
       
   397  * @since bbPress (r4284)
       
   398  *
       
   399  * @return array
       
   400  */
       
   401 function bbp_get_dynamic_roles() {
       
   402 	return (array) apply_filters( 'bbp_get_dynamic_roles', array(
       
   403 
       
   404 		// Keymaster
       
   405 		bbp_get_keymaster_role() => array(
       
   406 			'name'         => __( 'Keymaster', 'bbpress' ),
       
   407 			'capabilities' => bbp_get_caps_for_role( bbp_get_keymaster_role() )
       
   408 		),
       
   409 
       
   410 		// Moderator
       
   411 		bbp_get_moderator_role() => array(
       
   412 			'name'         => __( 'Moderator', 'bbpress' ),
       
   413 			'capabilities' => bbp_get_caps_for_role( bbp_get_moderator_role() )
       
   414 		),
       
   415 
       
   416 		// Participant
       
   417 		bbp_get_participant_role() => array(
       
   418 			'name'         => __( 'Participant', 'bbpress' ),
       
   419 			'capabilities' => bbp_get_caps_for_role( bbp_get_participant_role() )
       
   420 		),
       
   421 
       
   422 		// Spectator
       
   423 		bbp_get_spectator_role() => array(
       
   424 			'name'         => __( 'Spectator', 'bbpress' ),
       
   425 			'capabilities' => bbp_get_caps_for_role( bbp_get_spectator_role() )
       
   426 		),
       
   427 
       
   428 		// Blocked
       
   429 		bbp_get_blocked_role() => array(
       
   430 			'name'         => __( 'Blocked', 'bbpress' ),
       
   431 			'capabilities' => bbp_get_caps_for_role( bbp_get_blocked_role() )
       
   432 		)
       
   433 	) );
       
   434 }
       
   435 
       
   436 /**
       
   437  * Removes the bbPress roles from the editable roles array
       
   438  *
       
   439  * This used to use array_diff_assoc() but it randomly broke before 2.2 release.
       
   440  * Need to research what happened, and if there's a way to speed this up.
       
   441  *
       
   442  * @since bbPress (r4303)
       
   443  *
       
   444  * @param array $all_roles All registered roles
       
   445  * @return array 
       
   446  */
       
   447 function bbp_filter_blog_editable_roles( $all_roles = array() ) {
       
   448 
       
   449 	// Loop through bbPress roles
       
   450 	foreach ( array_keys( bbp_get_dynamic_roles() ) as $bbp_role ) {
       
   451 
       
   452 		// Loop through WordPress roles
       
   453 		foreach ( array_keys( $all_roles ) as $wp_role ) {
       
   454 
       
   455 			// If keys match, unset
       
   456 			if ( $wp_role == $bbp_role ) {
       
   457 				unset( $all_roles[$wp_role] );
       
   458 			}
       
   459 		}
       
   460 	}
       
   461 
       
   462 	return $all_roles;
       
   463 }
       
   464 
       
   465 /**
       
   466  * The keymaster role for bbPress users
       
   467  *
       
   468  * @since bbPress (r4284)
       
   469  *
       
   470  * @uses apply_filters() Allow override of hardcoded keymaster role
       
   471  * @return string
       
   472  */
       
   473 function bbp_get_keymaster_role() {
       
   474 	return apply_filters( 'bbp_get_keymaster_role', 'bbp_keymaster' );
       
   475 }
       
   476 
       
   477 /**
       
   478  * The moderator role for bbPress users
       
   479  *
       
   480  * @since bbPress (r3410)
       
   481  *
       
   482  * @uses apply_filters() Allow override of hardcoded moderator role
       
   483  * @return string
       
   484  */
       
   485 function bbp_get_moderator_role() {
       
   486 	return apply_filters( 'bbp_get_moderator_role', 'bbp_moderator' );
       
   487 }
       
   488 
       
   489 /**
       
   490  * The participant role for registered user that can participate in forums
       
   491  *
       
   492  * @since bbPress (r3410)
       
   493  *
       
   494  * @uses apply_filters() Allow override of hardcoded participant role
       
   495  * @return string
       
   496  */
       
   497 function bbp_get_participant_role() {
       
   498 	return apply_filters( 'bbp_get_participant_role', 'bbp_participant' );
       
   499 }
       
   500 
       
   501 /**
       
   502  * The spectator role is for registered users without any capabilities
       
   503  *
       
   504  * @since bbPress (r3860)
       
   505  *
       
   506  * @uses apply_filters() Allow override of hardcoded spectator role
       
   507  * @return string
       
   508  */
       
   509 function bbp_get_spectator_role() {
       
   510 	return apply_filters( 'bbp_get_spectator_role', 'bbp_spectator' );
       
   511 }
       
   512 
       
   513 /**
       
   514  * The blocked role is for registered users that cannot spectate or participate
       
   515  *
       
   516  * @since bbPress (r4284)
       
   517  *
       
   518  * @uses apply_filters() Allow override of hardcoded blocked role
       
   519  * @return string
       
   520  */
       
   521 function bbp_get_blocked_role() {
       
   522 	return apply_filters( 'bbp_get_blocked_role', 'bbp_blocked' );
       
   523 }
       
   524 
       
   525 /** Deprecated ****************************************************************/
       
   526 
       
   527 /**
       
   528  * Adds bbPress-specific user roles.
       
   529  *
       
   530  * @since bbPress (r2741)
       
   531  * @deprecated since version 2.2
       
   532  */
       
   533 function bbp_add_roles() {
       
   534 	_doing_it_wrong( 'bbp_add_roles', __( 'Editable forum roles no longer exist.', 'bbpress' ), '2.2' );
       
   535 }
       
   536 
       
   537 /**
       
   538  * Removes bbPress-specific user roles.
       
   539  *
       
   540  * @since bbPress (r2741)
       
   541  * @deprecated since version 2.2
       
   542  */
       
   543 function bbp_remove_roles() {
       
   544 	_doing_it_wrong( 'bbp_remove_roles', __( 'Editable forum roles no longer exist.', 'bbpress' ), '2.2' );
       
   545 }