| 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 |
* Site-wide access helpers for IvyForms admin (REST, menus, previews). |
| 16 |
* |
| 17 |
* Implicit elevated access (no IvyForms caps required): |
| 18 |
* - Multisite: super admins only. |
| 19 |
* - Single site: users who can manage_options (typically Administrators). |
| 20 |
*/ |
| 21 |
final class IvyFormsAccess |
| 22 |
{ |
| 23 |
public const CAP_MANAGE_ALL = 'ivyforms_manage_all'; |
| 24 |
|
| 25 |
public const CAP_ACCESS_PLUGIN = 'ivyforms_access_plugin'; |
| 26 |
|
| 27 |
public static function hasImplicitElevatedAccess(): bool |
| 28 |
{ |
| 29 |
if (!function_exists('wp_get_current_user')) { |
| 30 |
return false; |
| 31 |
} |
| 32 |
|
| 33 |
if (function_exists('is_multisite') && is_multisite()) { |
| 34 |
return function_exists('is_super_admin') && is_super_admin(); |
| 35 |
} |
| 36 |
|
| 37 |
return current_user_can('manage_options'); |
| 38 |
} |
| 39 |
|
| 40 |
public static function userHasImplicitElevatedAccess(\WP_User $user): bool |
| 41 |
{ |
| 42 |
if (function_exists('is_multisite') && is_multisite()) { |
| 43 |
return function_exists('is_super_admin') && is_super_admin((int) $user->ID); |
| 44 |
} |
| 45 |
|
| 46 |
return user_can($user, 'manage_options'); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Users who already have site-wide / full IvyForms admin access (not worth listing for user-targeted rules). |
| 51 |
* |
| 52 |
* Do not exclude based on ivyforms_access_plugin: that cap is granted to delegated roles (e.g. Editor) |
| 53 |
* and would hide every member of those roles from the picker. |
| 54 |
*/ |
| 55 |
public static function userShouldBeExcludedFromPermissionsPicker(\WP_User $user): bool |
| 56 |
{ |
| 57 |
if (self::userHasImplicitElevatedAccess($user)) { |
| 58 |
return true; |
| 59 |
} |
| 60 |
|
| 61 |
return user_can($user, self::CAP_MANAGE_ALL); |
| 62 |
} |
| 63 |
|
| 64 |
public static function canAccessPluginAdminRoutes(): bool |
| 65 |
{ |
| 66 |
return self::hasImplicitElevatedAccess() |
| 67 |
|| current_user_can(self::CAP_MANAGE_ALL) |
| 68 |
|| current_user_can(PermissionCatalog::wpCapability('access_settings_page')) |
| 69 |
|| current_user_can(self::CAP_ACCESS_PLUGIN); |
| 70 |
} |
| 71 |
|
| 72 |
public static function canAccessFormPreview(): bool |
| 73 |
{ |
| 74 |
return self::hasImplicitElevatedAccess() |
| 75 |
|| current_user_can(self::CAP_MANAGE_ALL) |
| 76 |
|| current_user_can(PermissionCatalog::wpCapability('add_edit_forms')) |
| 77 |
|| current_user_can(self::CAP_ACCESS_PLUGIN); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Whether the current user may access IvyForms permissions management (settings tab and REST). |
| 82 |
* |
| 83 |
* Matches WordPress site administrators: manage_options on single site, super admins on |
| 84 |
* multisite, or the administrator role. Delegated users with only access_settings_page |
| 85 |
* are excluded. |
| 86 |
*/ |
| 87 |
public static function currentUserCanAccessPermissionsSettings(): bool |
| 88 |
{ |
| 89 |
if (self::hasImplicitElevatedAccess()) { |
| 90 |
return true; |
| 91 |
} |
| 92 |
|
| 93 |
if (!function_exists('wp_get_current_user')) { |
| 94 |
return false; |
| 95 |
} |
| 96 |
|
| 97 |
return self::userHasAdministratorRole(wp_get_current_user()); |
| 98 |
} |
| 99 |
|
| 100 |
public static function userHasAdministratorRole(\WP_User $user): bool |
| 101 |
{ |
| 102 |
if ((int) $user->ID <= 0) { |
| 103 |
return false; |
| 104 |
} |
| 105 |
|
| 106 |
return in_array('administrator', (array) $user->roles, true); |
| 107 |
} |
| 108 |
} |
| 109 |
|