-
+ B07604A76E0D73BD72C467A08AA102871D21007C00BD84A6AE135AF2EF33A4E3CFC9BE873512BC9213F679DB7066437159C77F9B07056520F297DB12529EB13E
mp-wp/wp-includes/capabilities.php
(0 . 0)(1 . 987)
73186 <?php
73187 /**
73188 * WordPress Roles and Capabilities.
73189 *
73190 * @package WordPress
73191 * @subpackage User
73192 */
73193
73194 /**
73195 * WordPress User Roles.
73196 *
73197 * The role option is simple, the structure is organized by role name that store
73198 * the name in value of the 'name' key. The capabilities are stored as an array
73199 * in the value of the 'capability' key.
73200 *
73201 * <code>
73202 * array (
73203 * 'rolename' => array (
73204 * 'name' => 'rolename',
73205 * 'capabilities' => array()
73206 * )
73207 * )
73208 * </code>
73209 *
73210 * @since 2.0.0
73211 * @package WordPress
73212 * @subpackage User
73213 */
73214 class WP_Roles {
73215 /**
73216 * List of roles and capabilities.
73217 *
73218 * @since 2.0.0
73219 * @access public
73220 * @var array
73221 */
73222 var $roles;
73223
73224 /**
73225 * List of the role objects.
73226 *
73227 * @since 2.0.0
73228 * @access public
73229 * @var array
73230 */
73231 var $role_objects = array();
73232
73233 /**
73234 * List of role names.
73235 *
73236 * @since 2.0.0
73237 * @access public
73238 * @var array
73239 */
73240 var $role_names = array();
73241
73242 /**
73243 * Option name for storing role list.
73244 *
73245 * @since 2.0.0
73246 * @access public
73247 * @var string
73248 */
73249 var $role_key;
73250
73251 /**
73252 * Whether to use the database for retrieval and storage.
73253 *
73254 * @since 2.1.0
73255 * @access public
73256 * @var bool
73257 */
73258 var $use_db = true;
73259
73260 /**
73261 * PHP4 Constructor - Call {@link WP_Roles::_init()} method.
73262 *
73263 * @since 2.0.0
73264 * @access public
73265 *
73266 * @return WP_Roles
73267 */
73268 function WP_Roles() {
73269 $this->_init();
73270 }
73271
73272 /**
73273 * Setup the object properties.
73274 *
73275 * The role key is set to the current prefix for the $wpdb object with
73276 * 'user_roles' appended. If the $wp_user_roles global is set, then it will
73277 * be used and the role option will not be updated or used.
73278 *
73279 * @since 2.1.0
73280 * @access protected
73281 * @uses $wpdb Used to get the database prefix.
73282 * @global array $wp_user_roles Used to set the 'roles' property value.
73283 */
73284 function _init () {
73285 global $wpdb;
73286 global $wp_user_roles;
73287 $this->role_key = $wpdb->prefix . 'user_roles';
73288 if ( ! empty( $wp_user_roles ) ) {
73289 $this->roles = $wp_user_roles;
73290 $this->use_db = false;
73291 } else {
73292 $this->roles = get_option( $this->role_key );
73293 }
73294
73295 if ( empty( $this->roles ) )
73296 return;
73297
73298 $this->role_objects = array();
73299 $this->role_names = array();
73300 foreach ( (array) $this->roles as $role => $data ) {
73301 $this->role_objects[$role] = new WP_Role( $role, $this->roles[$role]['capabilities'] );
73302 $this->role_names[$role] = $this->roles[$role]['name'];
73303 }
73304 }
73305
73306 /**
73307 * Add role name with capabilities to list.
73308 *
73309 * Updates the list of roles, if the role doesn't already exist.
73310 *
73311 * @since 2.0.0
73312 * @access public
73313 *
73314 * @param string $role Role name.
73315 * @param string $display_name Role display name.
73316 * @param array $capabilities List of role capabilities.
73317 * @return null|WP_Role WP_Role object if role is added, null if already exists.
73318 */
73319 function add_role( $role, $display_name, $capabilities = array() ) {
73320 if ( isset( $this->roles[$role] ) )
73321 return;
73322
73323 $this->roles[$role] = array(
73324 'name' => $display_name,
73325 'capabilities' => $capabilities
73326 );
73327 if ( $this->use_db )
73328 update_option( $this->role_key, $this->roles );
73329 $this->role_objects[$role] = new WP_Role( $role, $capabilities );
73330 $this->role_names[$role] = $display_name;
73331 return $this->role_objects[$role];
73332 }
73333
73334 /**
73335 * Remove role by name.
73336 *
73337 * @since 2.0.0
73338 * @access public
73339 *
73340 * @param string $role Role name.
73341 */
73342 function remove_role( $role ) {
73343 if ( ! isset( $this->role_objects[$role] ) )
73344 return;
73345
73346 unset( $this->role_objects[$role] );
73347 unset( $this->role_names[$role] );
73348 unset( $this->roles[$role] );
73349
73350 if ( $this->use_db )
73351 update_option( $this->role_key, $this->roles );
73352 }
73353
73354 /**
73355 * Add capability to role.
73356 *
73357 * @since 2.0.0
73358 * @access public
73359 *
73360 * @param string $role Role name.
73361 * @param string $cap Capability name.
73362 * @param bool $grant Optional, default is true. Whether role is capable of preforming capability.
73363 */
73364 function add_cap( $role, $cap, $grant = true ) {
73365 $this->roles[$role]['capabilities'][$cap] = $grant;
73366 if ( $this->use_db )
73367 update_option( $this->role_key, $this->roles );
73368 }
73369
73370 /**
73371 * Remove capability from role.
73372 *
73373 * @since 2.0.0
73374 * @access public
73375 *
73376 * @param string $role Role name.
73377 * @param string $cap Capability name.
73378 */
73379 function remove_cap( $role, $cap ) {
73380 unset( $this->roles[$role]['capabilities'][$cap] );
73381 if ( $this->use_db )
73382 update_option( $this->role_key, $this->roles );
73383 }
73384
73385 /**
73386 * Retrieve role object by name.
73387 *
73388 * @since 2.0.0
73389 * @access public
73390 *
73391 * @param string $role Role name.
73392 * @return object|null Null, if role does not exist. WP_Role object, if found.
73393 */
73394 function &get_role( $role ) {
73395 if ( isset( $this->role_objects[$role] ) )
73396 return $this->role_objects[$role];
73397 else
73398 return null;
73399 }
73400
73401 /**
73402 * Retrieve list of role names.
73403 *
73404 * @since 2.0.0
73405 * @access public
73406 *
73407 * @return array List of role names.
73408 */
73409 function get_names() {
73410 return $this->role_names;
73411 }
73412
73413 /**
73414 * Whether role name is currently in the list of available roles.
73415 *
73416 * @since 2.0.0
73417 * @access public
73418 *
73419 * @param string $role Role name to look up.
73420 * @return bool
73421 */
73422 function is_role( $role )
73423 {
73424 return isset( $this->role_names[$role] );
73425 }
73426 }
73427
73428 /**
73429 * WordPress Role class.
73430 *
73431 * @since 2.0.0
73432 * @package WordPress
73433 * @subpackage User
73434 */
73435 class WP_Role {
73436 /**
73437 * Role name.
73438 *
73439 * @since 2.0.0
73440 * @access public
73441 * @var string
73442 */
73443 var $name;
73444
73445 /**
73446 * List of capabilities the role contains.
73447 *
73448 * @since 2.0.0
73449 * @access public
73450 * @var array
73451 */
73452 var $capabilities;
73453
73454 /**
73455 * PHP4 Constructor - Setup object properties.
73456 *
73457 * The list of capabilities, must have the key as the name of the capability
73458 * and the value a boolean of whether it is granted to the role or not.
73459 *
73460 * @since 2.0.0
73461 * @access public
73462 *
73463 * @param string $role Role name.
73464 * @param array $capabilities List of capabilities.
73465 * @return WP_Role
73466 */
73467 function WP_Role( $role, $capabilities ) {
73468 $this->name = $role;
73469 $this->capabilities = $capabilities;
73470 }
73471
73472 /**
73473 * Assign role a capability.
73474 *
73475 * @see WP_Roles::add_cap() Method uses implementation for role.
73476 * @since 2.0.0
73477 * @access public
73478 *
73479 * @param string $cap Capability name.
73480 * @param bool $grant Whether role has capability privilege.
73481 */
73482 function add_cap( $cap, $grant = true ) {
73483 global $wp_roles;
73484
73485 if ( ! isset( $wp_roles ) )
73486 $wp_roles = new WP_Roles();
73487
73488 $this->capabilities[$cap] = $grant;
73489 $wp_roles->add_cap( $this->name, $cap, $grant );
73490 }
73491
73492 /**
73493 * Remove capability from role.
73494 *
73495 * This is a container for {@link WP_Roles::remove_cap()} to remove the
73496 * capability from the role. That is to say, that {@link
73497 * WP_Roles::remove_cap()} implements the functionality, but it also makes
73498 * sense to use this class, because you don't need to enter the role name.
73499 *
73500 * @since 2.0.0
73501 * @access public
73502 *
73503 * @param string $cap Capability name.
73504 */
73505 function remove_cap( $cap ) {
73506 global $wp_roles;
73507
73508 if ( ! isset( $wp_roles ) )
73509 $wp_roles = new WP_Roles();
73510
73511 unset( $this->capabilities[$cap] );
73512 $wp_roles->remove_cap( $this->name, $cap );
73513 }
73514
73515 /**
73516 * Whether role has capability.
73517 *
73518 * The capabilities is passed through the 'role_has_cap' filter. The first
73519 * parameter for the hook is the list of capabilities the class has
73520 * assigned. The second parameter is the capability name to look for. The
73521 * third and final parameter for the hook is the role name.
73522 *
73523 * @since 2.0.0
73524 * @access public
73525 *
73526 * @param string $cap Capability name.
73527 * @return bool True, if user has capability. False, if doesn't have capability.
73528 */
73529 function has_cap( $cap ) {
73530 $capabilities = apply_filters( 'role_has_cap', $this->capabilities, $cap, $this->name );
73531 if ( !empty( $capabilities[$cap] ) )
73532 return $capabilities[$cap];
73533 else
73534 return false;
73535 }
73536
73537 }
73538
73539 /**
73540 * WordPress User class.
73541 *
73542 * @since 2.0.0
73543 * @package WordPress
73544 * @subpackage User
73545 */
73546 class WP_User {
73547 /**
73548 * User data container.
73549 *
73550 * This will be set as properties of the object.
73551 *
73552 * @since 2.0.0
73553 * @access private
73554 * @var array
73555 */
73556 var $data;
73557
73558 /**
73559 * The user's ID.
73560 *
73561 * @since 2.1.0
73562 * @access public
73563 * @var int
73564 */
73565 var $ID = 0;
73566
73567 /**
73568 * The deprecated user's ID.
73569 *
73570 * @since 2.0.0
73571 * @access public
73572 * @deprecated Use WP_User::$ID
73573 * @see WP_User::$ID
73574 * @var int
73575 */
73576 var $id = 0;
73577
73578 /**
73579 * The individual capabilities the user has been given.
73580 *
73581 * @since 2.0.0
73582 * @access public
73583 * @var array
73584 */
73585 var $caps = array();
73586
73587 /**
73588 * User metadata option name.
73589 *
73590 * @since 2.0.0
73591 * @access public
73592 * @var string
73593 */
73594 var $cap_key;
73595
73596 /**
73597 * The roles the user is part of.
73598 *
73599 * @since 2.0.0
73600 * @access public
73601 * @var array
73602 */
73603 var $roles = array();
73604
73605 /**
73606 * All capabilities the user has, including individual and role based.
73607 *
73608 * @since 2.0.0
73609 * @access public
73610 * @var array
73611 */
73612 var $allcaps = array();
73613
73614 /**
73615 * First name of the user.
73616 *
73617 * Created to prevent notices.
73618 *
73619 * @since 2.7.0
73620 * @access public
73621 * @var string
73622 */
73623 var $first_name = '';
73624
73625 /**
73626 * Last name of the user.
73627 *
73628 * Created to prevent notices.
73629 *
73630 * @since 2.7.0
73631 * @access public
73632 * @var string
73633 */
73634 var $last_name = '';
73635
73636 /**
73637 * PHP4 Constructor - Sets up the object properties.
73638 *
73639 * Retrieves the userdata and then assigns all of the data keys to direct
73640 * properties of the object. Calls {@link WP_User::_init_caps()} after
73641 * setting up the object's user data properties.
73642 *
73643 * @since 2.0.0
73644 * @access public
73645 *
73646 * @param int|string $id User's ID or username
73647 * @param int $name Optional. User's username
73648 * @return WP_User
73649 */
73650 function WP_User( $id, $name = '' ) {
73651
73652 if ( empty( $id ) && empty( $name ) )
73653 return;
73654
73655 if ( ! is_numeric( $id ) ) {
73656 $name = $id;
73657 $id = 0;
73658 }
73659
73660 if ( ! empty( $id ) )
73661 $this->data = get_userdata( $id );
73662 else
73663 $this->data = get_userdatabylogin( $name );
73664
73665 if ( empty( $this->data->ID ) )
73666 return;
73667
73668 foreach ( get_object_vars( $this->data ) as $key => $value ) {
73669 $this->{$key} = $value;
73670 }
73671
73672 $this->id = $this->ID;
73673 $this->_init_caps();
73674 }
73675
73676 /**
73677 * Setup capability object properties.
73678 *
73679 * Will set the value for the 'cap_key' property to current database table
73680 * prefix, followed by 'capabilities'. Will then check to see if the
73681 * property matching the 'cap_key' exists and is an array. If so, it will be
73682 * used.
73683 *
73684 * @since 2.1.0
73685 * @access protected
73686 */
73687 function _init_caps() {
73688 global $wpdb;
73689 $this->cap_key = $wpdb->prefix . 'capabilities';
73690 $this->caps = &$this->{$this->cap_key};
73691 if ( ! is_array( $this->caps ) )
73692 $this->caps = array();
73693 $this->get_role_caps();
73694 }
73695
73696 /**
73697 * Retrieve all of the role capabilities and merge with individual capabilities.
73698 *
73699 * All of the capabilities of the roles the user belongs to are merged with
73700 * the users individual roles. This also means that the user can be denied
73701 * specific roles that their role might have, but the specific user isn't
73702 * granted permission to.
73703 *
73704 * @since 2.0.0
73705 * @uses $wp_roles
73706 * @access public
73707 */
73708 function get_role_caps() {
73709 global $wp_roles;
73710
73711 if ( ! isset( $wp_roles ) )
73712 $wp_roles = new WP_Roles();
73713
73714 //Filter out caps that are not role names and assign to $this->roles
73715 if ( is_array( $this->caps ) )
73716 $this->roles = array_filter( array_keys( $this->caps ), array( &$wp_roles, 'is_role' ) );
73717
73718 //Build $allcaps from role caps, overlay user's $caps
73719 $this->allcaps = array();
73720 foreach ( (array) $this->roles as $role ) {
73721 $role = $wp_roles->get_role( $role );
73722 $this->allcaps = array_merge( $this->allcaps, $role->capabilities );
73723 }
73724 $this->allcaps = array_merge( $this->allcaps, $this->caps );
73725 }
73726
73727 /**
73728 * Add role to user.
73729 *
73730 * Updates the user's meta data option with capabilities and roles.
73731 *
73732 * @since 2.0.0
73733 * @access public
73734 *
73735 * @param string $role Role name.
73736 */
73737 function add_role( $role ) {
73738 $this->caps[$role] = true;
73739 update_usermeta( $this->ID, $this->cap_key, $this->caps );
73740 $this->get_role_caps();
73741 $this->update_user_level_from_caps();
73742 }
73743
73744 /**
73745 * Remove role from user.
73746 *
73747 * @since 2.0.0
73748 * @access public
73749 *
73750 * @param string $role Role name.
73751 */
73752 function remove_role( $role ) {
73753 if ( empty( $this->roles[$role] ) || ( count( $this->roles ) <= 1 ) )
73754 return;
73755 unset( $this->caps[$role] );
73756 update_usermeta( $this->ID, $this->cap_key, $this->caps );
73757 $this->get_role_caps();
73758 }
73759
73760 /**
73761 * Set the role of the user.
73762 *
73763 * This will remove the previous roles of the user and assign the user the
73764 * new one. You can set the role to an empty string and it will remove all
73765 * of the roles from the user.
73766 *
73767 * @since 2.0.0
73768 * @access public
73769 *
73770 * @param string $role Role name.
73771 */
73772 function set_role( $role ) {
73773 foreach ( (array) $this->roles as $oldrole )
73774 unset( $this->caps[$oldrole] );
73775 if ( !empty( $role ) ) {
73776 $this->caps[$role] = true;
73777 $this->roles = array( $role => true );
73778 } else {
73779 $this->roles = false;
73780 }
73781 update_usermeta( $this->ID, $this->cap_key, $this->caps );
73782 $this->get_role_caps();
73783 $this->update_user_level_from_caps();
73784 }
73785
73786 /**
73787 * Choose the maximum level the user has.
73788 *
73789 * Will compare the level from the $item parameter against the $max
73790 * parameter. If the item is incorrect, then just the $max parameter value
73791 * will be returned.
73792 *
73793 * Used to get the max level based on the capabilities the user has. This
73794 * is also based on roles, so if the user is assigned the Administrator role
73795 * then the capability 'level_10' will exist and the user will get that
73796 * value.
73797 *
73798 * @since 2.0.0
73799 * @access public
73800 *
73801 * @param int $max Max level of user.
73802 * @param string $item Level capability name.
73803 * @return int Max Level.
73804 */
73805 function level_reduction( $max, $item ) {
73806 if ( preg_match( '/^level_(10|[0-9])$/i', $item, $matches ) ) {
73807 $level = intval( $matches[1] );
73808 return max( $max, $level );
73809 } else {
73810 return $max;
73811 }
73812 }
73813
73814 /**
73815 * Update the maximum user level for the user.
73816 *
73817 * Updates the 'user_level' user metadata (includes prefix that is the
73818 * database table prefix) with the maximum user level. Gets the value from
73819 * the all of the capabilities that the user has.
73820 *
73821 * @since 2.0.0
73822 * @access public
73823 */
73824 function update_user_level_from_caps() {
73825 global $wpdb;
73826 $this->user_level = array_reduce( array_keys( $this->allcaps ), array( &$this, 'level_reduction' ), 0 );
73827 update_usermeta( $this->ID, $wpdb->prefix.'user_level', $this->user_level );
73828 }
73829
73830 /**
73831 * Add capability and grant or deny access to capability.
73832 *
73833 * @since 2.0.0
73834 * @access public
73835 *
73836 * @param string $cap Capability name.
73837 * @param bool $grant Whether to grant capability to user.
73838 */
73839 function add_cap( $cap, $grant = true ) {
73840 $this->caps[$cap] = $grant;
73841 update_usermeta( $this->ID, $this->cap_key, $this->caps );
73842 }
73843
73844 /**
73845 * Remove capability from user.
73846 *
73847 * @since 2.0.0
73848 * @access public
73849 *
73850 * @param string $cap Capability name.
73851 */
73852 function remove_cap( $cap ) {
73853 if ( empty( $this->caps[$cap] ) ) return;
73854 unset( $this->caps[$cap] );
73855 update_usermeta( $this->ID, $this->cap_key, $this->caps );
73856 }
73857
73858 /**
73859 * Remove all of the capabilities of the user.
73860 *
73861 * @since 2.1.0
73862 * @access public
73863 */
73864 function remove_all_caps() {
73865 global $wpdb;
73866 $this->caps = array();
73867 update_usermeta( $this->ID, $this->cap_key, '' );
73868 update_usermeta( $this->ID, $wpdb->prefix.'user_level', '' );
73869 $this->get_role_caps();
73870 }
73871
73872 /**
73873 * Whether user has capability or role name.
73874 *
73875 * This is useful for looking up whether the user has a specific role
73876 * assigned to the user. The second optional parameter can also be used to
73877 * check for capabilities against a specfic post.
73878 *
73879 * @since 2.0.0
73880 * @access public
73881 *
73882 * @param string|int $cap Capability or role name to search.
73883 * @param int $post_id Optional. Post ID to check capability against specific post.
73884 * @return bool True, if user has capability; false, if user does not have capability.
73885 */
73886 function has_cap( $cap ) {
73887 if ( is_numeric( $cap ) )
73888 $cap = $this->translate_level_to_cap( $cap );
73889
73890 $args = array_slice( func_get_args(), 1 );
73891 $args = array_merge( array( $cap, $this->ID ), $args );
73892 $caps = call_user_func_array( 'map_meta_cap', $args );
73893 // Must have ALL requested caps
73894 $capabilities = apply_filters( 'user_has_cap', $this->allcaps, $caps, $args );
73895 foreach ( (array) $caps as $cap ) {
73896 //echo "Checking cap $cap<br />";
73897 if ( empty( $capabilities[$cap] ) || !$capabilities[$cap] )
73898 return false;
73899 }
73900
73901 return true;
73902 }
73903
73904 /**
73905 * Convert numeric level to level capability name.
73906 *
73907 * Prepends 'level_' to level number.
73908 *
73909 * @since 2.0.0
73910 * @access public
73911 *
73912 * @param int $level Level number, 1 to 10.
73913 * @return string
73914 */
73915 function translate_level_to_cap( $level ) {
73916 return 'level_' . $level;
73917 }
73918
73919 }
73920
73921 /**
73922 * Map meta capabilities to primitive capabilities.
73923 *
73924 * This does not actually compare whether the user ID has the actual capability,
73925 * just what the capability or capabilities are. Meta capability list value can
73926 * be 'delete_user', 'edit_user', 'delete_post', 'delete_page', 'edit_post',
73927 * 'edit_page', 'read_post', or 'read_page'.
73928 *
73929 * @since 2.0.0
73930 *
73931 * @param string $cap Capability name.
73932 * @param int $user_id User ID.
73933 * @return array Actual capabilities for meta capability.
73934 */
73935 function map_meta_cap( $cap, $user_id ) {
73936 $args = array_slice( func_get_args(), 2 );
73937 $caps = array();
73938
73939 switch ( $cap ) {
73940 case 'delete_user':
73941 $caps[] = 'delete_users';
73942 break;
73943 case 'edit_user':
73944 if ( !isset( $args[0] ) || $user_id != $args[0] ) {
73945 $caps[] = 'edit_users';
73946 }
73947 break;
73948 case 'delete_post':
73949 $author_data = get_userdata( $user_id );
73950 //echo "post ID: {$args[0]}<br />";
73951 $post = get_post( $args[0] );
73952 if ( 'page' == $post->post_type ) {
73953 $args = array_merge( array( 'delete_page', $user_id ), $args );
73954 return call_user_func_array( 'map_meta_cap', $args );
73955 }
73956 $post_author_data = get_userdata( $post->post_author );
73957 //echo "current user id : $user_id, post author id: " . $post_author_data->ID . "<br />";
73958 // If the user is the author...
73959 if ( $user_id == $post_author_data->ID ) {
73960 // If the post is published...
73961 if ( 'publish' == $post->post_status )
73962 $caps[] = 'delete_published_posts';
73963 else
73964 // If the post is draft...
73965 $caps[] = 'delete_posts';
73966 } else {
73967 // The user is trying to edit someone else's post.
73968 $caps[] = 'delete_others_posts';
73969 // The post is published, extra cap required.
73970 if ( 'publish' == $post->post_status )
73971 $caps[] = 'delete_published_posts';
73972 elseif ( 'private' == $post->post_status )
73973 $caps[] = 'delete_private_posts';
73974 }
73975 break;
73976 case 'delete_page':
73977 $author_data = get_userdata( $user_id );
73978 //echo "post ID: {$args[0]}<br />";
73979 $page = get_page( $args[0] );
73980 $page_author_data = get_userdata( $page->post_author );
73981 //echo "current user id : $user_id, page author id: " . $page_author_data->ID . "<br />";
73982 // If the user is the author...
73983 if ( $user_id == $page_author_data->ID ) {
73984 // If the page is published...
73985 if ( $page->post_status == 'publish' )
73986 $caps[] = 'delete_published_pages';
73987 else
73988 // If the page is draft...
73989 $caps[] = 'delete_pages';
73990 } else {
73991 // The user is trying to edit someone else's page.
73992 $caps[] = 'delete_others_pages';
73993 // The page is published, extra cap required.
73994 if ( $page->post_status == 'publish' )
73995 $caps[] = 'delete_published_pages';
73996 elseif ( $page->post_status == 'private' )
73997 $caps[] = 'delete_private_pages';
73998 }
73999 break;
74000 // edit_post breaks down to edit_posts, edit_published_posts, or
74001 // edit_others_posts
74002 case 'edit_post':
74003 $author_data = get_userdata( $user_id );
74004 //echo "post ID: {$args[0]}<br />";
74005 $post = get_post( $args[0] );
74006 if ( 'page' == $post->post_type ) {
74007 $args = array_merge( array( 'edit_page', $user_id ), $args );
74008 return call_user_func_array( 'map_meta_cap', $args );
74009 }
74010 $post_author_data = get_userdata( $post->post_author );
74011 //echo "current user id : $user_id, post author id: " . $post_author_data->ID . "<br />";
74012 // If the user is the author...
74013 if ( $user_id == $post_author_data->ID ) {
74014 // If the post is published...
74015 if ( 'publish' == $post->post_status )
74016 $caps[] = 'edit_published_posts';
74017 else
74018 // If the post is draft...
74019 $caps[] = 'edit_posts';
74020 } else {
74021 // The user is trying to edit someone else's post.
74022 $caps[] = 'edit_others_posts';
74023 // The post is published, extra cap required.
74024 if ( 'publish' == $post->post_status )
74025 $caps[] = 'edit_published_posts';
74026 elseif ( 'private' == $post->post_status )
74027 $caps[] = 'edit_private_posts';
74028 }
74029 break;
74030 case 'edit_page':
74031 $author_data = get_userdata( $user_id );
74032 //echo "post ID: {$args[0]}<br />";
74033 $page = get_page( $args[0] );
74034 $page_author_data = get_userdata( $page->post_author );
74035 //echo "current user id : $user_id, page author id: " . $page_author_data->ID . "<br />";
74036 // If the user is the author...
74037 if ( $user_id == $page_author_data->ID ) {
74038 // If the page is published...
74039 if ( 'publish' == $page->post_status )
74040 $caps[] = 'edit_published_pages';
74041 else
74042 // If the page is draft...
74043 $caps[] = 'edit_pages';
74044 } else {
74045 // The user is trying to edit someone else's page.
74046 $caps[] = 'edit_others_pages';
74047 // The page is published, extra cap required.
74048 if ( 'publish' == $page->post_status )
74049 $caps[] = 'edit_published_pages';
74050 elseif ( 'private' == $page->post_status )
74051 $caps[] = 'edit_private_pages';
74052 }
74053 break;
74054 case 'read_post':
74055 $post = get_post( $args[0] );
74056 if ( 'page' == $post->post_type ) {
74057 $args = array_merge( array( 'read_page', $user_id ), $args );
74058 return call_user_func_array( 'map_meta_cap', $args );
74059 }
74060
74061 if ( 'private' != $post->post_status ) {
74062 $caps[] = 'read';
74063 break;
74064 }
74065
74066 $author_data = get_userdata( $user_id );
74067 $post_author_data = get_userdata( $post->post_author );
74068 if ( $user_id == $post_author_data->ID )
74069 $caps[] = 'read';
74070 else
74071 $caps[] = 'read_private_posts';
74072 break;
74073 case 'read_page':
74074 $page = get_page( $args[0] );
74075
74076 if ( 'private' != $page->post_status ) {
74077 $caps[] = 'read';
74078 break;
74079 }
74080
74081 $author_data = get_userdata( $user_id );
74082 $page_author_data = get_userdata( $page->post_author );
74083 if ( $user_id == $page_author_data->ID )
74084 $caps[] = 'read';
74085 else
74086 $caps[] = 'read_private_pages';
74087 break;
74088 default:
74089 // If no meta caps match, return the original cap.
74090 $caps[] = $cap;
74091 }
74092
74093 return $caps;
74094 }
74095
74096 /**
74097 * Whether current user has capability or role.
74098 *
74099 * @since 2.0.0
74100 *
74101 * @param string $capability Capability or role name.
74102 * @return bool
74103 */
74104 function current_user_can( $capability ) {
74105 $current_user = wp_get_current_user();
74106
74107 if ( empty( $current_user ) )
74108 return false;
74109
74110 $args = array_slice( func_get_args(), 1 );
74111 $args = array_merge( array( $capability ), $args );
74112
74113 return call_user_func_array( array( &$current_user, 'has_cap' ), $args );
74114 }
74115
74116 /**
74117 * Retrieve role object.
74118 *
74119 * @see WP_Roles::get_role() Uses method to retrieve role object.
74120 * @since 2.0.0
74121 *
74122 * @param string $role Role name.
74123 * @return object
74124 */
74125 function get_role( $role ) {
74126 global $wp_roles;
74127
74128 if ( ! isset( $wp_roles ) )
74129 $wp_roles = new WP_Roles();
74130
74131 return $wp_roles->get_role( $role );
74132 }
74133
74134 /**
74135 * Add role, if it does not exist.
74136 *
74137 * @see WP_Roles::add_role() Uses method to add role.
74138 * @since 2.0.0
74139 *
74140 * @param string $role Role name.
74141 * @param string $display_name Display name for role.
74142 * @param array $capabilities List of capabilities.
74143 * @return null|WP_Role WP_Role object if role is added, null if already exists.
74144 */
74145 function add_role( $role, $display_name, $capabilities = array() ) {
74146 global $wp_roles;
74147
74148 if ( ! isset( $wp_roles ) )
74149 $wp_roles = new WP_Roles();
74150
74151 return $wp_roles->add_role( $role, $display_name, $capabilities );
74152 }
74153
74154 /**
74155 * Remove role, if it exists.
74156 *
74157 * @see WP_Roles::remove_role() Uses method to remove role.
74158 * @since 2.0.0
74159 *
74160 * @param string $role Role name.
74161 * @return null
74162 */
74163 function remove_role( $role ) {
74164 global $wp_roles;
74165
74166 if ( ! isset( $wp_roles ) )
74167 $wp_roles = new WP_Roles();
74168
74169 return $wp_roles->remove_role( $role );
74170 }
74171
74172 ?>