PHP code example of pollora / roles

1. Go to this page and download the library: Download pollora/roles library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

pollora / roles example snippets


use Pollora\Attributes\Role;
use Pollora\Attributes\Role\Grants;
use Pollora\Attributes\Role\GrantsPostType;
use Pollora\Attributes\Role\GrantsTaxonomy;
use Pollora\Attributes\Role\Without;
use Pollora\Role\Domain\Enums\Access;

#[Role('event_manager', label: 'Event manager', inherits: 'author')]
#[GrantsPostType(Event::class, Access::Editor)]
#[GrantsTaxonomy(Genre::class)]
#[Grants('export_attendees', 'upload_files')]
#[Without('publish_posts')]
final class EventManager {}

use Pollora\Role\Domain\Enums\Access;
use Pollora\Role\Role;

Role::make('event_manager', 'Event manager')      // #[Role('event_manager', label: 'Event manager')]
    ->inherits('author')                           // a slug, or the class of a #[Role]
    ->grantsPostType(Event::class, Access::Editor) // #[GrantsPostType]: a class or a slug
    ->grantsTaxonomy(Genre::class)                 // #[GrantsTaxonomy]
    ->grants('export_attendees', EventCap::ScanTickets) // #[Grants]: names or backed enums
    ->without('publish_posts')                     // #[Without]
    ->allowSensitive()                             // allowSensitive: true
    ->textDomain('my-theme');                      // textDomain: 'my-theme'

Role::modify('editor')                             // #[ModifyRole('editor')]
    ->grants('scan_tickets')
    ->without('edit_theme_options');

Role::capabilitySet(EventCap::class);              // #[CapabilitySet]: a backed enum, or a list of names

use Pollora\Discovery\Domain\Models\DiscoveryLocation;
use Pollora\Discovery\Infrastructure\Services\DiscoveryEngine;
use Pollora\Role\Application\Services\CapabilityOwnerReader;
use Pollora\Role\Application\Services\RoleDefinitionBuilder;
use Pollora\Role\Application\Services\RoleRegistry;
use Pollora\Role\Domain\Services\PostTypeCapabilityMap;
use Pollora\Role\Domain\Services\RoleCompiler;
use Pollora\Role\Infrastructure\Adapters\WordPressRoleInjector;
use Pollora\Role\Infrastructure\Services\RoleDiscovery;

$reader = new CapabilityOwnerReader;
$registry = new RoleRegistry($reader, new PostTypeCapabilityMap);
$injector = new WordPressRoleInjector($registry, new RoleCompiler, ['administrator']);

// Before WordPress builds WP_Roles: the first thing a plugin loads, or an mu-plugin
add_action('wp_roles_init', $injector->inject(...), 1);
add_action('init', $injector->reportWarnings(...), PHP_INT_MAX);

(new DiscoveryEngine)
    ->addLocation(new DiscoveryLocation('MyPlugin\\', __DIR__.'/src'))
    ->addDiscovery('roles', new RoleDiscovery(new RoleDefinitionBuilder($reader, ['administrator']), $registry, $injector))
    ->run();