| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @copyright © Melograno Venture Studio. All rights reserved. |
| 5 |
*/ |
| 6 |
|
| 7 |
namespace IvyForms\Services\InstallActions; |
| 8 |
|
| 9 |
use IvyForms\Services\Permissions\IvyFormsAccess; |
| 10 |
use IvyForms\Services\Permissions\PermissionCatalog; |
| 11 |
|
| 12 |
/** |
| 13 |
* Registers IvyForms-prefixed capabilities on the Administrator role. |
| 14 |
*/ |
| 15 |
class ActivationCapabilitiesHook |
| 16 |
{ |
| 17 |
/** |
| 18 |
* Idempotent: safe to call on every activation and new subsite. |
| 19 |
*/ |
| 20 |
public static function ensureAdministratorCaps(): void |
| 21 |
{ |
| 22 |
$role = get_role('administrator'); |
| 23 |
if ($role === null) { |
| 24 |
return; |
| 25 |
} |
| 26 |
|
| 27 |
foreach (PermissionCatalog::keys() as $key) { |
| 28 |
$role->add_cap(PermissionCatalog::wpCapability($key)); |
| 29 |
} |
| 30 |
|
| 31 |
$role->add_cap(IvyFormsAccess::CAP_MANAGE_ALL); |
| 32 |
$role->add_cap(IvyFormsAccess::CAP_ACCESS_PLUGIN); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Backfill caps for sites upgraded without re-running the activation hook. |
| 37 |
*/ |
| 38 |
public static function ensureAdministratorCapsIfMissing(): void |
| 39 |
{ |
| 40 |
$role = get_role('administrator'); |
| 41 |
if ($role === null || $role->has_cap(IvyFormsAccess::CAP_MANAGE_ALL)) { |
| 42 |
return; |
| 43 |
} |
| 44 |
|
| 45 |
self::ensureAdministratorCaps(); |
| 46 |
} |
| 47 |
} |
| 48 |
|