Documentation

CapabilityRegistrar
in package
uses NormalizesCapability

Read onlyYes
FinalYes

Declarative custom-capability registration: describe which capabilities each role receives once, then apply on plugin activation and remove on deactivation/uninstall. Replaces the ad-hoc `add_cap()` loops every plugin reinvents.

$registrar = new CapabilityRegistrar([
    'administrator' => ['acme_manage_quotes', 'acme_view_reports'],
    'shop_manager'  => ['acme_manage_quotes'],
]);
$registrar->register();   // activation
$registrar->unregister(); // deactivation

Table of Contents

Properties

$capabilitiesByRole  : array<string|int, mixed>

Methods

__construct()  : mixed
register()  : array<string, bool>
Grant every declared capability. Missing roles are skipped (reported in the result map) so a plugin can declare caps for optional roles such as WooCommerce's `shop_manager`.
unregister()  : array<string, bool>
Revoke every declared capability.
apply()  : array<string, bool>

Properties

Methods

__construct()

public __construct(array<string, array<int, CapabilityInterface|string>> $capabilitiesByRole) : mixed
Parameters
$capabilitiesByRole : array<string, array<int, CapabilityInterface|string>>

role slug => capabilities to grant (raw strings or typed capabilities)

register()

Grant every declared capability. Missing roles are skipped (reported in the result map) so a plugin can declare caps for optional roles such as WooCommerce's `shop_manager`.

public register() : array<string, bool>
Return values
array<string, bool> —

"role:capability" => granted

unregister()

Revoke every declared capability.

public unregister() : array<string, bool>
Return values
array<string, bool> —

"role:capability" => revoked

apply()

private apply(bool $granting) : array<string, bool>
Parameters
$granting : bool
Return values
array<string, bool>
On this page

Search results