| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @copyright © Melograno Venture Studio. All rights reserved. |
| 5 |
* @licence See COPYING.md for license details. |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace IvyForms\Infrastructure\WP\MCP; |
| 9 |
|
| 10 |
use IvyForms\Services\Settings\SettingsService; |
| 11 |
|
| 12 |
/** |
| 13 |
* Central feature gate for IvyForms MCP. |
| 14 |
* |
| 15 |
* MCP exposes IvyForms capabilities to AI clients. It is ON by default; site owners can |
| 16 |
* turn it off via IvyForms Settings → General ("Enable MCP for AI assistants") or force a |
| 17 |
* value with the {@code ivyforms/mcp/enabled} filter. Access is always scoped to each |
| 18 |
* user's IvyForms permissions (see {@see McpAbilityPermissions}). |
| 19 |
* |
| 20 |
* Hard prerequisites (always enforced): |
| 21 |
* - The Abilities API must be loaded ({@see wp_register_ability}). |
| 22 |
* - WordPress core must be at least {@see IVYFORMS_MCP_MIN_WP_VERSION}. |
| 23 |
*/ |
| 24 |
class McpFeatureGate |
| 25 |
{ |
| 26 |
public const FILTER_ENABLED = 'ivyforms/mcp/enabled'; |
| 27 |
|
| 28 |
/** |
| 29 |
* Whether MCP should bootstrap and register routes/abilities. |
| 30 |
*/ |
| 31 |
public static function isEnabled(): bool |
| 32 |
{ |
| 33 |
if (!function_exists('wp_register_ability')) { |
| 34 |
return false; |
| 35 |
} |
| 36 |
|
| 37 |
if (!self::wordPressVersionSatisfiesMinimum()) { |
| 38 |
return false; |
| 39 |
} |
| 40 |
|
| 41 |
return (bool) apply_filters(self::FILTER_ENABLED, self::isEnabledInStoredSettings()); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Whether MCP is enabled via IvyForms Settings → General (ivyforms_settings option). |
| 46 |
* |
| 47 |
* Enabled by default: when no explicit choice has been stored we treat MCP as ON, so |
| 48 |
* only an explicit opt-out (mcp_enabled = false) disables it. |
| 49 |
*/ |
| 50 |
private static function isEnabledInStoredSettings(): bool |
| 51 |
{ |
| 52 |
if (!function_exists('get_option')) { |
| 53 |
return true; |
| 54 |
} |
| 55 |
|
| 56 |
$stored = (new SettingsService())->getSetting('general', 'mcp_enabled'); |
| 57 |
|
| 58 |
return self::coerceStoredEnabled($stored); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Coerce a stored mcp_enabled value to a boolean, defaulting to enabled. |
| 63 |
* |
| 64 |
* @param mixed $rawEnabled |
| 65 |
*/ |
| 66 |
public static function coerceStoredEnabled($rawEnabled): bool |
| 67 |
{ |
| 68 |
if (is_bool($rawEnabled)) { |
| 69 |
return $rawEnabled; |
| 70 |
} |
| 71 |
|
| 72 |
if (is_string($rawEnabled) && trim($rawEnabled) === '') { |
| 73 |
return true; |
| 74 |
} |
| 75 |
|
| 76 |
if (is_string($rawEnabled) || is_int($rawEnabled)) { |
| 77 |
return filter_var($rawEnabled, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) ?? true; |
| 78 |
} |
| 79 |
|
| 80 |
return true; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* True when the running WordPress version is at least the documented minimum. |
| 85 |
*/ |
| 86 |
private static function wordPressVersionSatisfiesMinimum(): bool |
| 87 |
{ |
| 88 |
$minimum = defined('IVYFORMS_MCP_MIN_WP_VERSION') ? (string) IVYFORMS_MCP_MIN_WP_VERSION : '6.9'; |
| 89 |
|
| 90 |
return version_compare((string) get_bloginfo('version'), $minimum, '>='); |
| 91 |
} |
| 92 |
} |
| 93 |
|