Try: Register a fake wc-admin homepage to for users without permission (https://github.com/woocommerce/woocommerce-admin/pull/7285)

* Register a fake wc-admin homepage to for users without permission

* Update removal cap to manage_woocommerce

* Fix WooCommerce menu item position and icon

* Fix up admin user checks

* Handle PR feedback

* Only disable redirection on wc-admin pages

* Remove woocommerce_prevent_admin_access filter

* Add changelog entry
This commit is contained in:
Joshua T Flowers 2021-07-23 09:01:47 -04:00 committed by GitHub
parent 5140106c39
commit 95ee8338c0
2 changed files with 45 additions and 1 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: Tweak
Register wc-admin page for all users and handle authorization in client

View File

@ -43,6 +43,7 @@ class Homescreen {
// In WC Core 5.1 $submenu manipulation occurs in admin_menu, not admin_head. See https://github.com/woocommerce/woocommerce/pull/29088.
if ( version_compare( WC_VERSION, '5.1', '>=' ) ) {
// priority is 20 to run after admin_menu hook for woocommerce runs, so that submenu is populated.
add_action( 'admin_menu', array( $this, 'possibly_remove_woocommerce_menu' ) );
add_action( 'admin_menu', array( $this, 'update_link_structure' ), 20 );
} else {
// priority is 20 to run after https://github.com/woocommerce/woocommerce/blob/a55ae325306fc2179149ba9b97e66f32f84fdd9c/includes/admin/class-wc-admin-menus.php#L165.
@ -74,6 +75,19 @@ class Homescreen {
* Registers home page.
*/
public function register_page() {
// Register a top-level item for users who cannot view the core WooCommerce menu.
if ( ! $this->is_admin_user() ) {
wc_admin_register_page(
array(
'id' => 'woocommerce-home',
'title' => __( 'WooCommerce', 'woocommerce-admin' ),
'path' => self::MENU_SLUG,
'capability' => 'read',
)
);
return;
}
wc_admin_register_page(
array(
'id' => 'woocommerce-home',
@ -81,11 +95,37 @@ class Homescreen {
'parent' => 'woocommerce',
'path' => self::MENU_SLUG,
'order' => 0,
'capability' => 'manage_woocommerce',
'capability' => 'read',
)
);
}
/**
* Check if the user can access the top-level WooCommerce item.
*/
public function is_admin_user() {
return current_user_can( 'edit_others_shop_orders' ) || current_user_can( 'manage_woocommerce' );
}
/**
* Possibly remove the WooCommerce menu item if it was purely used to access wc-admin pages.
*/
public function possibly_remove_woocommerce_menu() {
global $menu;
if ( $this->is_admin_user() ) {
return;
}
foreach ( $menu as $key => $menu_item ) {
if ( self::MENU_SLUG !== $menu_item[2] || 'read' !== $menu_item[1] ) {
continue;
}
unset( $menu[ $key ] );
}
}
/**
* Update the WooCommerce menu structure to make our main dashboard/handler
* the top level link for 'WooCommerce'.