| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @copyright © Melograno Venture Studio. All rights reserved. |
| 5 |
*/ |
| 6 |
|
| 7 |
namespace IvyForms\Services\Permissions; |
| 8 |
|
| 9 |
// phpcs:disable PSR1.Files.SideEffects |
| 10 |
if (!defined('ABSPATH')) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* Canonical permission keys stored on access rules and mirrored to WordPress capabilities |
| 16 |
* as ivyforms_{key} (see wpCapability()). |
| 17 |
*/ |
| 18 |
class PermissionCatalog |
| 19 |
{ |
| 20 |
/** @var array<string, string>|null */ |
| 21 |
private static ?array $labelMap = null; |
| 22 |
|
| 23 |
/** |
| 24 |
* WordPress capability name for a canonical IvyForms permission key. |
| 25 |
*/ |
| 26 |
public static function wpCapability(string $logicalKey): string |
| 27 |
{ |
| 28 |
return 'ivyforms_' . $logicalKey; |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* @return list<string> |
| 33 |
*/ |
| 34 |
public static function keys(): array |
| 35 |
{ |
| 36 |
return [ |
| 37 |
'view_forms_list', |
| 38 |
'add_edit_forms', |
| 39 |
'delete_forms', |
| 40 |
'access_settings_page', |
| 41 |
'view_entries_admin', |
| 42 |
'delete_entries_admin', |
| 43 |
]; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* @return list<array{key:string,label:string}> |
| 48 |
*/ |
| 49 |
public static function definitions(): array |
| 50 |
{ |
| 51 |
return [ |
| 52 |
['key' => 'view_forms_list', 'label' => __('View forms list', 'ivyforms')], |
| 53 |
['key' => 'add_edit_forms', 'label' => __('Add and edit forms', 'ivyforms')], |
| 54 |
['key' => 'delete_forms', 'label' => __('Delete forms', 'ivyforms')], |
| 55 |
['key' => 'access_settings_page', 'label' => __('Access this settings page', 'ivyforms')], |
| 56 |
['key' => 'view_entries_admin', 'label' => __('View entries from the admin area', 'ivyforms')], |
| 57 |
['key' => 'delete_entries_admin', 'label' => __('Delete entries from the admin area', 'ivyforms')], |
| 58 |
]; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* @return array<string, string> |
| 63 |
*/ |
| 64 |
public static function labelMap(): array |
| 65 |
{ |
| 66 |
if (self::$labelMap !== null) { |
| 67 |
return self::$labelMap; |
| 68 |
} |
| 69 |
|
| 70 |
self::$labelMap = []; |
| 71 |
foreach (self::definitions() as $definition) { |
| 72 |
self::$labelMap[ $definition['key'] ] = $definition['label']; |
| 73 |
} |
| 74 |
|
| 75 |
return self::$labelMap; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Permissions implied by other grants (expanded on save so REST and UI stay consistent). |
| 80 |
* |
| 81 |
* @return array<string, list<string>> |
| 82 |
*/ |
| 83 |
public static function dependencyMap(): array |
| 84 |
{ |
| 85 |
return [ |
| 86 |
'add_edit_forms' => ['view_forms_list'], |
| 87 |
'delete_forms' => ['view_forms_list'], |
| 88 |
'delete_entries_admin' => ['view_entries_admin'], |
| 89 |
]; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* @param mixed[] $permissions |
| 94 |
* @return list<string> |
| 95 |
*/ |
| 96 |
public static function sanitize(array $permissions): array |
| 97 |
{ |
| 98 |
$allowed = array_flip(self::keys()); |
| 99 |
$out = []; |
| 100 |
foreach ($permissions as $p) { |
| 101 |
if (!is_string($p)) { |
| 102 |
continue; |
| 103 |
} |
| 104 |
if (isset($allowed[$p])) { |
| 105 |
$out[] = $p; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
return array_values(array_unique($out)); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Sanitize grants and auto-include implied permissions (canonical key order). |
| 114 |
* |
| 115 |
* @param mixed[] $permissions |
| 116 |
* @return list<string> |
| 117 |
*/ |
| 118 |
public static function sanitizeWithDependencies(array $permissions): array |
| 119 |
{ |
| 120 |
$granted = array_flip(self::sanitize($permissions)); |
| 121 |
|
| 122 |
foreach (self::dependencyMap() as $grant => $implied) { |
| 123 |
if (!isset($granted[$grant])) { |
| 124 |
continue; |
| 125 |
} |
| 126 |
foreach ($implied as $key) { |
| 127 |
$granted[$key] = true; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
$out = []; |
| 132 |
foreach (self::keys() as $key) { |
| 133 |
if (isset($granted[$key])) { |
| 134 |
$out[] = $key; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
return $out; |
| 139 |
} |
| 140 |
} |
| 141 |
|