0

I am trying to bring new capabilities to the bookings plugin from WooThemes. While combining the bookings and the vendors plugins, vendor users aren't allowed to manage resources (resources are a custom post).

I added the new capabilities to the resources custom post and then added the capabilities to the vendor role (through the User Role plugin)

Now the resources are showing up in the admin menu for a vendor role, but when I try to add a new resource, I get the "You are not authorized to access this page" error.

The new capabilities I added : https://i.stack.imgur.com/OCDlV.png

Capabilities added to the role : https://i.stack.imgur.com/5t696.png

Resources showing up when logged in with the role : https://i.stack.imgur.com/go0ZZ.png

But clicking the add button isn't working : https://i.stack.imgur.com/9jD5k.png

=======================================================

Edit :

Thanks to @mmm comment, I identified that the capabilities are called "manage_booking_resource". Then after more readings, I found a (far from perfect) solution which is below.

  • 1
    try this code to see which capabilities are needed by this post type $objetDuPostType = get_post_type_object("bookable_ressource"); var_export($objetDuPostType->cap); – mmm May 9 '18 at 12:21
0

It's a little hackish and should be completed but it works for now.

Also I had a chat with the people from WooThemes and they told me this feature should be available in the following months within the plugin, so this will become obsolete.

//Hack - Shows only user's own resources if the user does not have the edit_others_manage_booking_resources capability
function bookable_resource_limit_posts_to_author($query) {

    global $pagenow;

    if( 'edit.php' != $pagenow || !$query->is_admin ) return $query;

    if( !current_user_can('administrator') && !current_user_can( 'edit_others_manage_booking_resources' ) && ( $_GET['post_type'] == 'bookable_resource' )  ) {

        global $user_ID;
        $query->set('author', $user_ID );

        echo '<style>.subsubsub, .icl_subsubsub {display:none !important;}</style>'; //Fugly - hide table quick links
    }

    return $query;
}
add_filter('pre_get_posts', 'bookable_resource_limit_posts_to_author');

add_filter('views_edit-bookable_resource','bookable_resource_quicklinks');
function bookable_resource_quicklinks($views) {

    unset($views['all']);
    unset($views['publish']);
    unset($views['trash']);

    return $views;
}


//Show Resources in menu and redefine Vendor Admin Role to be able to manage resource
function modify_bookable_resource_custom_post_type()
{
    //Modify Custom post type to allow access and show up in menu
    global $wp_post_types;
    $p = 'bookable_resource';

    // Someone has changed this post type, always check for that!
    if ( empty ( $wp_post_types[ $p ] )
        or ! is_object( $wp_post_types[ $p ] )
        or empty ( $wp_post_types[ $p ]->labels )
        )
        return;

    $wp_post_types[ $p ]->show_in_menu  = true;
    $wp_post_types[ $p ]->map_meta_cap  = false;
    $wp_post_types[ $p ]->supports      = array( 'title', 'author' ); //Not working, why ? -> modified in web/app/plugins/woocommerce-bookings/woocommerce-bookings.php - line 406


    //Redefine Vendor Amin role
    remove_role( 'wc_product_vendors_admin_vendor' );
    add_role( 'wc_product_vendors_admin_vendor', 'Vendor Admin', admin_vendor_caps() );
}
add_action( 'wp_loaded', 'modify_bookable_resource_custom_post_type', 1002 );


//Redefine Admin Vendor capabilities 
//Copied and modified from plugins/woocommerce-product-vendors/includes/class-wc-product-vendors-roles-caps.php
function admin_vendor_caps() {
    return apply_filters( 'wcpv_default_admin_vendor_role_caps', array(
        'read_product'              => true,
        'manage_product'            => true,
        'edit_products'             => true,
        'edit_product'              => true,
        'edit_published_products'   => true,
        'edit_shop_orders'          => true,
        'assign_product_terms'      => true,
        'upload_files'              => true,
        'read'                      => true,
        'manage_bookings'           => true,
        'edit_others_products'      => false,
        'view_vendor_sales_widget'  => true,
        'delete_published_products' => true,
        'delete_others_products'    => false,
        'delete_posts'              => true,
        'delete_others_posts'       => false,
        'edit_comment'              => false,
        'edit_comments'             => false,
        'view_woocommerce_reports'  => false,
        'publish_products'          => false,

        //Add resource capabilities to the Vendor Admin role
        'edit_manage_booking_resource'             => true,
        'read_manage_booking_resource'             => true,
        'delete_manage_booking_resource'           => true,
        'edit_manage_booking_resources'            => true,
        'edit_others_manage_booking_resources'     => false, //WHY IS IT SHOWING OTHERS' RESOURCES ??
        'publish_manage_booking_resources'         => true,
        'read_private_manage_booking_resources'    => true,
        'edit_manage_booking_resources'            => true,
        'delete_others_manage_booking_resources'   => false,

    ) );
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.