| 1 |
<?php |
| 2 |
|
| 3 |
namespace Booktics\Admin; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 6 |
|
| 7 |
use Booktics\Contracts\Hookable_Service_Contract; |
| 8 |
|
| 9 |
class Menu_Permission_Hook implements Hookable_Service_Contract { |
| 10 |
|
| 11 |
/** |
| 12 |
* Register the menu permission hook. |
| 13 |
* |
| 14 |
* Adds the menu_permissions method to the 'booktics_menu' action hook. |
| 15 |
* |
| 16 |
* @return void |
| 17 |
*/ |
| 18 |
public function register() { |
| 19 |
add_action( 'booktics_menu', array( |
| 20 |
$this, |
| 21 |
'menu_permissions' |
| 22 |
), 11, 1 ); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Filter menu items based on the current user's role. |
| 27 |
* |
| 28 |
* Removes certain menu items for users with the 'booktics_team_member' role, |
| 29 |
* while allowing administrators to see all menu items. |
| 30 |
* |
| 31 |
* @param array $menu_items The array of menu items to filter. |
| 32 |
* |
| 33 |
* @return array The filtered array of menu items. |
| 34 |
*/ |
| 35 |
public function menu_permissions( $menu_items ) { |
| 36 |
$current_user = wp_get_current_user(); |
| 37 |
$roles = $current_user->roles; |
| 38 |
if ( in_array( 'administrator', $roles ) ) { |
| 39 |
return $menu_items; |
| 40 |
} |
| 41 |
|
| 42 |
if ( in_array( 'booktics_team_member', $roles ) ) { |
| 43 |
$menu_items = array_filter( $menu_items, function ( $item ) { |
| 44 |
return ! in_array( $item['id'], $this->team_member_menu_items() ); |
| 45 |
} ); |
| 46 |
$menu_items[] = array( |
| 47 |
'id' => 'my-profile', |
| 48 |
'title' => esc_html__( 'My Profile', 'booktics' ), |
| 49 |
'link' => '/my-profile', |
| 50 |
'capability' => apply_filters( 'booktics_menu_permission_my_profile', 'manage_options' ), |
| 51 |
'position' => apply_filters( 'booktics_menu_position_my_profile', 11 ), |
| 52 |
); |
| 53 |
|
| 54 |
return array_values( $menu_items ); |
| 55 |
} |
| 56 |
|
| 57 |
return $menu_items; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Get the menu item IDs that should be hidden from team members. |
| 62 |
* |
| 63 |
* @return array List of menu item IDs restricted for team members. |
| 64 |
*/ |
| 65 |
public function team_member_menu_items() { |
| 66 |
return array( |
| 67 |
'team', |
| 68 |
'license', |
| 69 |
'location', |
| 70 |
'settings', |
| 71 |
'modules', |
| 72 |
'customers', |
| 73 |
'short-code', |
| 74 |
'booktics-automation', |
| 75 |
); |
| 76 |
} |
| 77 |
} |