|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * Feature-flag registry. |
| 6 | + * |
| 7 | + * Each feature is backed by a PHP constant that site owners (or resellers) |
| 8 | + * can define in wp-config.php before the plugin loads. The constants default |
| 9 | + * to `true` so stock installations retain all functionality; setting one to |
| 10 | + * `false` disables the corresponding UI and REST surface. |
| 11 | + * |
| 12 | + * Defined constants (all default true): |
| 13 | + * - GRATIS_AI_AGENT_FEATURE_BRANDING — White-label / branding settings: |
| 14 | + * agent name, brand colours, logo URL, greeting message. |
| 15 | + * - GRATIS_AI_AGENT_FEATURE_ACCESS_CONTROL — Role-based access control: |
| 16 | + * the Role Permissions manager and its /role-permissions REST routes. |
| 17 | + * |
| 18 | + * Usage example (wp-config.php): |
| 19 | + * define( 'GRATIS_AI_AGENT_FEATURE_BRANDING', false ); |
| 20 | + * |
| 21 | + * @package GratisAiAgent |
| 22 | + * @license GPL-2.0-or-later |
| 23 | + */ |
| 24 | + |
| 25 | +namespace GratisAiAgent\Core; |
| 26 | + |
| 27 | +if ( ! defined( 'ABSPATH' ) ) { |
| 28 | + exit; |
| 29 | +} |
| 30 | + |
| 31 | +final class Features { |
| 32 | + |
| 33 | + /** |
| 34 | + * Feature: white-label branding (agent name, colours, logo). |
| 35 | + * Constant: GRATIS_AI_AGENT_FEATURE_BRANDING |
| 36 | + */ |
| 37 | + const BRANDING = 'branding'; |
| 38 | + |
| 39 | + /** |
| 40 | + * Feature: role-based access control (Role Permissions manager). |
| 41 | + * Constant: GRATIS_AI_AGENT_FEATURE_ACCESS_CONTROL |
| 42 | + */ |
| 43 | + const ACCESS_CONTROL = 'access_control'; |
| 44 | + |
| 45 | + /** |
| 46 | + * Map of feature name → backing constant name. |
| 47 | + * |
| 48 | + * @var array<string, string> |
| 49 | + */ |
| 50 | + private const CONSTANT_MAP = array( |
| 51 | + self::BRANDING => 'GRATIS_AI_AGENT_FEATURE_BRANDING', |
| 52 | + self::ACCESS_CONTROL => 'GRATIS_AI_AGENT_FEATURE_ACCESS_CONTROL', |
| 53 | + ); |
| 54 | + |
| 55 | + /** |
| 56 | + * Check whether a feature is enabled. |
| 57 | + * |
| 58 | + * Returns `true` when the backing constant is not defined (default-on). |
| 59 | + * Returns `(bool) CONSTANT_VALUE` when the constant is defined. |
| 60 | + * |
| 61 | + * @param string $feature One of the Features::* class constants. |
| 62 | + * @return bool |
| 63 | + */ |
| 64 | + public static function is_enabled( string $feature ): bool { |
| 65 | + $constant = self::CONSTANT_MAP[ $feature ] ?? null; |
| 66 | + |
| 67 | + if ( null === $constant ) { |
| 68 | + // Unknown feature — fail open (enabled) to avoid breaking valid calls. |
| 69 | + return true; |
| 70 | + } |
| 71 | + |
| 72 | + if ( ! defined( $constant ) ) { |
| 73 | + // Constant not set by the site owner → default enabled. |
| 74 | + return true; |
| 75 | + } |
| 76 | + |
| 77 | + return (bool) constant( $constant ); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Return a map of all features and their current enabled state. |
| 82 | + * |
| 83 | + * Suitable for serialising into REST responses or wp_localize_script data. |
| 84 | + * |
| 85 | + * @return array<string, bool> |
| 86 | + */ |
| 87 | + public static function all(): array { |
| 88 | + $result = array(); |
| 89 | + foreach ( array_keys( self::CONSTANT_MAP ) as $feature ) { |
| 90 | + $result[ $feature ] = self::is_enabled( $feature ); |
| 91 | + } |
| 92 | + return $result; |
| 93 | + } |
| 94 | +} |
0 commit comments