true ); foreach ( self::staff_caps() as $cap ) { $manager_caps[ $cap ] = true; } $this->sync_role( self::ROLE_MANAGER, __( 'HostKhata Manager', 'hostkhata' ), $manager_caps ); $this->sync_role( self::ROLE_CUSTOMER, __( 'HostKhata Customer', 'hostkhata' ), array( 'read' => true, self::ACCESS_PORTAL => true, ) ); $administrator = get_role( 'administrator' ); if ( $administrator ) { foreach ( self::staff_caps() as $cap ) { $administrator->add_cap( $cap ); } } } /** * Removes the roles and capabilities. Used on uninstall. * * @since 0.1.0 * * @return void */ public function uninstall(): void { remove_role( self::ROLE_MANAGER ); remove_role( self::ROLE_CUSTOMER ); $administrator = get_role( 'administrator' ); if ( $administrator ) { foreach ( self::staff_caps() as $cap ) { $administrator->remove_cap( $cap ); } } } /** * Creates a role, or tops up an existing role with the given capabilities. * * @param string $role Role slug. * @param string $display_name Role label. * @param array $caps Capabilities. * @return void */ private function sync_role( string $role, string $display_name, array $caps ): void { $existing = get_role( $role ); if ( ! $existing ) { add_role( $role, $display_name, $caps ); return; } foreach ( $caps as $cap => $grant ) { $existing->add_cap( $cap, $grant ); } } }