From 5f2b915f18c36965c637536eb2c4b1e172b347d7 Mon Sep 17 00:00:00 2001 From: claudiulodro Date: Wed, 10 Oct 2018 09:56:37 -0700 Subject: [PATCH] Add filter for ease of extensions --- includes/wc-user-functions.php | 11 ++++++----- .../unit-tests/util/class-wc-tests-user-functions.php | 7 ------- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/includes/wc-user-functions.php b/includes/wc-user-functions.php index 000c675f992..b7b7a59926d 100644 --- a/includes/wc-user-functions.php +++ b/includes/wc-user-functions.php @@ -353,11 +353,11 @@ function wc_modify_editable_roles( $roles ) { unset( $roles['administrator'] ); } - if ( current_user_can( 'shop_manager' ) && isset( $roles['customer'] ) ) { - return array( - 'customer' => $roles['customer'], - ); + if ( current_user_can( 'shop_manager' ) ) { + $shop_manager_editable_roles = apply_filters( 'woocommerce_shop_manager_editable_roles', array( 'customer' ) ); + return array_intersect_key( $roles, array_flip( $shop_manager_editable_roles ) ); } + return $roles; } add_filter( 'editable_roles', 'wc_modify_editable_roles' ); @@ -389,7 +389,8 @@ function wc_modify_map_meta_cap( $caps, $cap, $user_id, $args ) { // Shop managers can only edit customer info. if ( current_user_can( 'shop_manager' ) ) { $userdata = get_userdata( $args[0] ); - if ( property_exists( $userdata, 'roles' ) && ! empty( $userdata->roles ) && ! in_array( 'customer', $userdata->roles ) ) { + $shop_manager_editable_roles = apply_filters( 'woocommerce_shop_manager_editable_roles', array( 'customer' ) ); + if ( property_exists( $userdata, 'roles' ) && ! empty( $userdata->roles ) && ! array_intersect( $userdata->roles, $shop_manager_editable_roles ) ) { $caps[] = 'do_not_allow'; } } diff --git a/tests/unit-tests/util/class-wc-tests-user-functions.php b/tests/unit-tests/util/class-wc-tests-user-functions.php index 2f8975bb16a..f4668146886 100644 --- a/tests/unit-tests/util/class-wc-tests-user-functions.php +++ b/tests/unit-tests/util/class-wc-tests-user-functions.php @@ -113,12 +113,5 @@ class WC_Tests_User_Functions extends WC_Unit_Test_Case { $this->assertContains( 'do_not_allow', $caps ); $caps = map_meta_cap( 'edit_user', $manager_id, $customer_id ); $this->assertEquals( array( 'edit_users' ), $caps ); - - // Customers should not be able to edit or promote anyone. - wp_set_current_user( $customer_id ); - $caps = map_meta_cap( 'edit_user', $customer_id, $admin_id ); - $this->assertContains( 'do_not_allow', $caps ); - $caps = map_meta_cap( 'edit_user', $customer_id, $editor_id ); - $this->assertContains( 'do_not_allow', $caps ); } }