*/
function _init () {
global $wpdb, $wp_user_roles;
- $this->role_key = $wpdb->prefix . 'user_roles';
+ $this->role_key = $wpdb->get_blog_prefix() . 'user_roles';
if ( ! empty( $wp_user_roles ) ) {
$this->roles = $wp_user_roles;
$this->use_db = false;
global $wpdb, $wp_user_roles;
// Duplicated from _init() to avoid an extra function call.
- $this->role_key = $wpdb->prefix . 'user_roles';
+ $this->role_key = $wpdb->get_blog_prefix() . 'user_roles';
$this->roles = get_option( $this->role_key );
if ( empty( $this->roles ) )
return;
* @param string $role Role name.
* @param string $display_name Role display name.
* @param array $capabilities List of role capabilities in the above format.
- * @return null|WP_Role WP_Role object if role is added, null if already exists.
+ * @return WP_Role|null WP_Role object if role is added, null if already exists.
*/
function add_role( $role, $display_name, $capabilities = array() ) {
if ( isset( $this->roles[$role] ) )
if ( $this->use_db )
update_option( $this->role_key, $this->roles );
+
+ if ( get_option( 'default_role' ) == $role )
+ update_option( 'default_role', 'subscriber' );
}
/**
* @access public
*
* @param string $role Role name.
- * @return object|null Null, if role does not exist. WP_Role object, if found.
+ * @return WP_Role|null WP_Role object if found, null if the role does not exist.
*/
function get_role( $role ) {
if ( isset( $this->role_objects[$role] ) )
// to int 1.
if ( ! is_numeric( $value ) )
return false;
- $value = absint( $value );
+ $value = intval( $value );
+ if ( $value < 1 )
+ return false;
} else {
$value = trim( $value );
}
global $wpdb;
if ( empty($cap_key) )
- $this->cap_key = $wpdb->prefix . 'capabilities';
+ $this->cap_key = $wpdb->get_blog_prefix() . 'capabilities';
else
$this->cap_key = $cap_key;
* @since 2.0.0
* @uses $wp_roles
* @access public
+ *
+ * @return array List of all capabilities for the user.
*/
function get_role_caps() {
global $wp_roles;
$this->allcaps = array_merge( (array) $this->allcaps, (array) $the_role->capabilities );
}
$this->allcaps = array_merge( (array) $this->allcaps, (array) $this->caps );
+
+ return $this->allcaps;
}
/**
foreach ( (array) $this->roles as $oldrole )
unset( $this->caps[$oldrole] );
+ $old_roles = $this->roles;
if ( !empty( $role ) ) {
$this->caps[$role] = true;
$this->roles = array( $role => true );
update_user_meta( $this->ID, $this->cap_key, $this->caps );
$this->get_role_caps();
$this->update_user_level_from_caps();
- do_action( 'set_user_role', $this->ID, $role );
+ do_action( 'set_user_role', $this->ID, $role, $old_roles );
}
/**
function update_user_level_from_caps() {
global $wpdb;
$this->user_level = array_reduce( array_keys( $this->allcaps ), array( $this, 'level_reduction' ), 0 );
- update_user_meta( $this->ID, $wpdb->prefix . 'user_level', $this->user_level );
+ update_user_meta( $this->ID, $wpdb->get_blog_prefix() . 'user_level', $this->user_level );
}
/**
global $wpdb;
$this->caps = array();
delete_user_meta( $this->ID, $this->cap_key );
- delete_user_meta( $this->ID, $wpdb->prefix . 'user_level' );
+ delete_user_meta( $this->ID, $wpdb->get_blog_prefix() . 'user_level' );
$this->get_role_caps();
}
}
// Must have ALL requested caps
- $capabilities = apply_filters( 'user_has_cap', $this->allcaps, $caps, $args );
+ $capabilities = apply_filters( 'user_has_cap', $this->allcaps, $caps, $args, $this );
$capabilities['exist'] = true; // Everyone is allowed to exist
foreach ( (array) $caps as $cap ) {
if ( empty( $capabilities[ $cap ] ) )
if ( ! $post_author_id )
$post_author_id = $user_id;
- $post_author_data = $post_author_id == get_current_user_id() ? wp_get_current_user() : get_userdata( $post_author_id );
-
// If the user is the author...
- if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID ) {
+ if ( $user_id == $post_author_id ) {
// If the post is published...
if ( 'publish' == $post->post_status ) {
$caps[] = $post_type->cap->delete_published_posts;
case 'edit_post':
case 'edit_page':
$post = get_post( $args[0] );
+ if ( empty( $post ) )
+ break;
if ( 'revision' == $post->post_type ) {
$post = get_post( $post->post_parent );
if ( ! $post_author_id )
$post_author_id = $user_id;
- $post_author_data = $post_author_id == get_current_user_id() ? wp_get_current_user() : get_userdata( $post_author_id );
-
// If the user is the author...
- if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID ) {
+ if ( $user_id == $post_author_id ) {
// If the post is published...
if ( 'publish' == $post->post_status ) {
$caps[] = $post_type->cap->edit_published_posts;
if ( ! $post_author_id )
$post_author_id = $user_id;
- $post_author_data = $post_author_id == get_current_user_id() ? wp_get_current_user() : get_userdata( $post_author_id );
-
- if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID )
+ if ( $user_id == $post_author_id )
$caps[] = $post_type->cap->read;
elseif ( $status_obj->private )
$caps[] = $post_type->cap->read_private_posts;
case 'delete_post_meta':
case 'add_post_meta':
$post = get_post( $args[0] );
- $post_type_object = get_post_type_object( $post->post_type );
- $caps = map_meta_cap( $post_type_object->cap->edit_post, $user_id, $post->ID );
+ $caps = map_meta_cap( 'edit_post', $user_id, $post->ID );
$meta_key = isset( $args[ 1 ] ) ? $args[ 1 ] : false;
break;
case 'edit_comment':
$comment = get_comment( $args[0] );
+ if ( empty( $comment ) )
+ break;
$post = get_post( $comment->comment_post_ID );
- $post_type_object = get_post_type_object( $post->post_type );
-
- $caps = map_meta_cap( $post_type_object->cap->edit_post, $user_id, $post->ID );
+ $caps = map_meta_cap( 'edit_post', $user_id, $post->ID );
break;
case 'unfiltered_upload':
if ( defined('ALLOW_UNFILTERED_UPLOADS') && ALLOW_UNFILTERED_UPLOADS && ( !is_multisite() || is_super_admin( $user_id ) ) )
* @since 2.0.0
*
* @param string $role Role name.
- * @return object
+ * @return WP_Role|null WP_Role object if found, null if the role does not exist.
*/
function get_role( $role ) {
global $wp_roles;
* @param string $role Role name.
* @param string $display_name Display name for role.
* @param array $capabilities List of capabilities, e.g. array( 'edit_posts' => true, 'delete_posts' => false );
- * @return null|WP_Role WP_Role object if role is added, null if already exists.
+ * @return WP_Role|null WP_Role object if role is added, null if already exists.
*/
function add_role( $role, $display_name, $capabilities = array() ) {
global $wp_roles;
* @since 2.0.0
*
* @param string $role Role name.
- * @return null
*/
function remove_role( $role ) {
global $wp_roles;
if ( ! isset( $wp_roles ) )
$wp_roles = new WP_Roles();
- return $wp_roles->remove_role( $role );
+ $wp_roles->remove_role( $role );
}
/**