Download the PHP package pollora/roles without Composer
On this page you can find all versions of the php package pollora/roles. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download pollora/roles
More information about pollora/roles
Files in pollora/roles
Package roles
Short Description WordPress roles and capabilities declared with attributes: a #[Role] class, #[ModifyRole] adjustments, post type and taxonomy capabilities, compiled into the roles WordPress stores. Runs with or without Laravel.
License MIT
Homepage https://pollora.dev/advanced/roles-capabilities/
Informations about the package roles
WordPress roles and capabilities declared in code: a class with #[Role] is a role, #[ModifyRole] adjusts a role WordPress already has, and the capabilities of your post types and taxonomies are granted by naming them. The package compiles the declarations into the roles WordPress stores and puts them in WP_Roles when it loads, so the code is the source of truth and the database follows.
Part of Pollora, the Laravel framework for WordPress, where it is installed and wired for you: see Roles & capabilities. It runs without Laravel too: in a plugin, a Bedrock site or any PHP project. The Laravel authorization bridge (
$user->can(),@can, therolemiddleware and the@roledirective) stays in the framework.
Installation
Requires PHP 8.3+ and WordPress. It builds on pollora/attributes (the #[Role] family) and pollora/discovery.
Declaring
The role starts from the capabilities of the role it inherits, adds what it grants (a post type's or a taxonomy's own capabilities by class or slug, or any capability by name or from a backed enum marked #[CapabilitySet]), and drops what it is Without. Without a label, it is the class name as words (Event Manager). #[ModifyRole('editor')] takes the same attributes to adjust a role that is not yours. The super roles (['administrator'] above, roles.super_roles in a Pollora application) receive every capability the code declares.
Declaring with the fluent API
The same roles can be declared by calling Role instead of writing a class. It takes the names of the attributes and of their arguments, and it is checked by the same code: a mistake is refused with the same reason.
There is nothing to register and nothing to end the chain with: a declaration is checked when WordPress builds its roles, so a chain can be written in any order. A role declared after that (in a theme's functions.php) reaches WordPress on init; one declared after wp_loaded waits for the next load. A declaration that is refused is left out, and the reason, naming the call (Role::make('meddler') at /path/to/file.php:12), is logged as a warning.
Role::manager() gives the registry the declarations go into, Role::useLogger() sets where warnings go (before the first call), and Role::reset() forgets everything, for tests. In a Pollora application the Role facade does the same and the framework wires it for you.
Wiring it outside Pollora
Point the discovery engine at your code and add RoleDiscovery, then hand the roles to WordPress when it builds WP_Roles:
Declarations are read before WordPress loads its roles, so run the engine early. The stored roles themselves are read and written through WordPressRoleStore (RoleStoreInterface) and the users who carry a role counted through WordPressRoleUsage (RoleUsageInterface).
Testing
License
MIT. See LICENSE.
All versions of roles with dependencies
pollora/attributes Version ^0.1.1 || ^1.0
pollora/discovery Version ^0.1 || ^0.2 || ^0.3 || ^1.0
psr/log Version ^3.0