Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress Role Custom Capability is set true but returns false

I try to create a user role with some custom capabilities. That works so far. But if I want to check the user permissions of one specific capability with the function current_user_can() it returns false. But inside the capabilities array of the new created role the specific capability is set to true.

So to give my words a little bit more coding background:

THE ROLE OBJECT

WP_Role {
  ["name"]=> "organizer"
  ["capabilities"]=> {
       ["edit_posts"]              => bool(false)
       ["delete_posts"]            => bool(false)
       ["publish_posts"]           => bool(false)
       ["upload_files"]            => bool(true)
       ["edit_event"]              => bool(true)
       ["read_event"]              => bool(true)
       ["delete_event"]            => bool(true)
       ["edit_events"]             => bool(true)
       ["edit_others_events"]      => bool(false)
       ["publish_events"]          => bool(false)
       ["read_private_events"]     => bool(true)
       ["read"]                    => bool(true)
       ["delete_events"]           => bool(true)
       ["delete_private_events"]   => bool(false)
       ["delete_published_events"] => bool(true)
       ["delete_others_events"]    => bool(false)
       ["edit_private_events"]     => bool(false)
       ["edit_published_events"]   => bool(true)
       ["manage_event_terms"]      => bool(true)
       ["edit_event_terms"]        => bool(true)
       ["delete_event_terms"]      => bool(true)
       ["assign_event_terms"]      => bool(true)
  }


MY ADD ROLE FUNCTION

add_role( 'organizer', __( 'Organizer', 'eventtool' ), array(
            // General
            'edit_posts'              => false,
            'delete_posts'            => false,
            'publish_posts'           => false,
            'upload_files'            => true,
            'edit_event'              => true,
            'read_event'              => true,
            'delete_event'            => true,

            // Primitive capabilities used outside of map_meta_cap():
            'edit_events'             => true,
            'edit_others_events'      => false,
            'publish_events'          => false,
            'read_private_events'     => true,

            // Primitive capabilities used within map_meta_cap():
            'read'                    => true,
            'delete_events'           => true,
            'delete_private_events'   => false,
            'delete_published_events' => true,
            'delete_others_events'    => false,
            'edit_private_events'     => false,
            'edit_published_events'   => true,
            'edit_events'             => true,

            // Terms
            'manage_event_terms'      => true,
            'edit_event_terms'        => true,
            'delete_event_terms'      => true,
            'assign_event_terms'      => true
        )
);


CUSTOM POST TYPE ARGS

register_post_type( 'event', array(
                'labels'              => $labels,
                'description'         => __( 'This is where you can add new events to your page.', 'eventtool' ),
                'public'              => true,
                'show_ui'             => true,
                'capability_type'     => 'event',
                'map_meta_cap'        => true,
                'publicly_queryable'  => true,
                'exclude_from_search' => false,
                'hierarchical'        => false,
                'rewrite'             => _x( 'event', 'slug', 'eventtool' ),
                'query_var'           => true,
                'supports'            => array( 'title', 'editor', 'excerpt', 'thumbnail' ),
                'show_in_nav_menus'   => true
            )
        );


[UPDATE]

function et_modify_map_meta_cap( $caps, $cap, $user_id, $args ) {

    var_dump($cap)

}
add_filter( 'map_meta_cap', 'et_modify_map_meta_cap', 10, 4 );

Outputs 'edit_post' instead of 'edit_event'



Any suggestions, why this wrong behaviour comes up?

like image 709
kindisch Avatar asked Jan 18 '16 20:01

kindisch


1 Answers

Today I solved it.

It is neccessary to assign an object id for the second parameter to the current_user_can() function to get the correct return of single pointed capabilities (eg. 'edit_post'). Otherwise the function will return false, if this parameter is not set.

As inside the WordPress Documentation:

If omitted you may receive an 'Undefined offset: 0' warning (this is because the current_user_can function eventually calls map_meta_cap which when checking against meta capabilities expects an array but is only supplied a single value)

like image 165
kindisch Avatar answered Oct 19 '22 03:10

kindisch