简体   繁体   中英

WooCommerce Product Vendors - add editable roles

I'm working with the WooCommerce Product Vendors plugin. I want to allow users with the role of Vendor Admin to add new users with the role of Vendor Manager. I already enabled the capability to list, create, etc. ( $role->add_cap( 'create_users') and such). But, it turns out they can only create new Customers or Subscribers.

The restriction appears to be coming from inside a class in one of the plugin files - it's very long so I'm just showing a bit of it here:

class WC_Product_Vendors_Vendor_Admin {

    public static $self;

    public static function init() {
        self::$self = new self();
        // filter the user roles
        add_filter( 'editable_roles', array( self::$self, 'filter_user_roles' ) );
    }

    public function filter_user_roles( $roles ) {
        $filtered_roles['customer'] = $roles['customer'];
        $filtered_roles['subscriber'] = $roles['subscriber'];
        return $filtered_roles;
    }
}

If I add in $filtered_roles['wc_product_vendors_manager_vendor'] = $roles['wc_product_vendors_manager_vendor']; then Vendor Manager shows up in the role dropdown for Add Users, and it works.

But instead of modifying the plugin directly, I want to do it in my custom plugin. At first I thought to add a filter to the "filter_user_roles" function, but I don't know how to target it since it's in a class. Is this possible? I also tried various ways of making my own filter for 'editable_roles', but I haven't been able to get it right. (The only examples I've found with it are of how to remove roles, not add them back when something else is removing them).

I was able to figure it out with help from here and here .

I had to include the capabilities for the roles, even though they're already defined in the plugin. I made a function to return the capabilities from it, and then used it in my add_editable_roles function. This is what worked:

function get_role_caps_1() {
    class WC_Product_Vendors_Roles_Caps_2 extends WC_Product_Vendors_Roles_Caps {
      public function default_manager_vendor_caps(){
        return parent::default_manager_vendor_caps();
      }
    }
    $Class_Inst = new WC_Product_Vendors_Roles_Caps_2();
    return $Class_Inst->default_manager_vendor_caps();
}

function add_editable_roles ( $roles ) {
    if ( current_user_has_role('wc_product_vendors_admin_vendor')) {        
        $filtered_roles = array(
        'wc_product_vendors_manager_vendor' => array(
            "name" => "Vendor Manager",
            "capabilities" => get_role_caps_1(),
            ),
        );
        return $filtered_roles; 
    } else {
        return $roles;
    }
}

add_filter( 'editable_roles', 'add_editable_roles', 12);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM