简体   繁体   English

WooCommerce 产品供应商 - 添加可编辑的角色

[英]WooCommerce Product Vendors - add editable roles

I'm working with the WooCommerce Product Vendors plugin.我正在使用 WooCommerce 产品供应商插件。 I want to allow users with the role of Vendor Admin to add new users with the role of Vendor Manager.我想允许具有 Vendor Admin 角色的用户添加具有 Vendor Manager 角色的新用户。 I already enabled the capability to list, create, etc. ( $role->add_cap( 'create_users') and such).我已经启用了列出、创建等功能( $role->add_cap( 'create_users')等)。 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'];如果我添加$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.起初我想在“filter_user_roles”函数中添加一个过滤器,但我不知道如何定位它,因为它在一个类中。 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.我还尝试了各种方法来为“editable_roles”制作我自己的过滤器,但我一直无法做到正确。 (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.我创建了一个函数来从中返回功能,然后在我的 add_editable_roles 函数中使用它。 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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