| 1 |
<?php |
| 2 |
/** |
| 3 |
* Settings registration for AI Experiments. |
| 4 |
* |
| 5 |
* @package WordPress\AI |
| 6 |
* |
| 7 |
* @since 0.1.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
declare(strict_types=1); |
| 11 |
|
| 12 |
namespace WordPress\AI\Settings; |
| 13 |
|
| 14 |
use WordPress\AI\Experiment_Registry; |
| 15 |
|
| 16 |
/** |
| 17 |
* Handles registration of settings for AI experiments. |
| 18 |
* |
| 19 |
* @since 0.1.0 |
| 20 |
*/ |
| 21 |
class Settings_Registration { |
| 22 |
|
| 23 |
/** |
| 24 |
* The experiment registry instance. |
| 25 |
* |
| 26 |
* @since 0.1.0 |
| 27 |
* |
| 28 |
* @var \WordPress\AI\Experiment_Registry |
| 29 |
*/ |
| 30 |
private Experiment_Registry $registry; |
| 31 |
|
| 32 |
/** |
| 33 |
* The option group name for settings registration. |
| 34 |
* |
| 35 |
* @since 0.1.0 |
| 36 |
* |
| 37 |
* @var string |
| 38 |
*/ |
| 39 |
public const OPTION_GROUP = 'ai_experiments'; |
| 40 |
|
| 41 |
/** |
| 42 |
* The option name for the global experiments toggle. |
| 43 |
* |
| 44 |
* @since 0.1.0 |
| 45 |
* |
| 46 |
* @var string |
| 47 |
*/ |
| 48 |
public const GLOBAL_OPTION = 'ai_experiments_enabled'; |
| 49 |
|
| 50 |
/** |
| 51 |
* Constructor. |
| 52 |
* |
| 53 |
* @since 0.1.0 |
| 54 |
* |
| 55 |
* @param \WordPress\AI\Experiment_Registry $registry The experiment registry. |
| 56 |
*/ |
| 57 |
public function __construct( Experiment_Registry $registry ) { |
| 58 |
$this->registry = $registry; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Initializes the settings registration hooks. |
| 63 |
* |
| 64 |
* @since 0.1.0 |
| 65 |
* |
| 66 |
* @return void |
| 67 |
*/ |
| 68 |
public function init(): void { |
| 69 |
$this->register_settings(); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Registers all settings for experiments. |
| 74 |
* |
| 75 |
* @since 0.1.0 |
| 76 |
* |
| 77 |
* @return void |
| 78 |
*/ |
| 79 |
public function register_settings(): void { |
| 80 |
// Register the global toggle. |
| 81 |
register_setting( |
| 82 |
self::OPTION_GROUP, |
| 83 |
self::GLOBAL_OPTION, |
| 84 |
array( |
| 85 |
'type' => 'boolean', |
| 86 |
'default' => false, |
| 87 |
'sanitize_callback' => 'rest_sanitize_boolean', |
| 88 |
) |
| 89 |
); |
| 90 |
|
| 91 |
// Register settings for each experiment. |
| 92 |
foreach ( $this->registry->get_all_experiments() as $experiment ) { |
| 93 |
$experiment_id = $experiment->get_id(); |
| 94 |
$experiment_option = "ai_experiment_{$experiment_id}_enabled"; |
| 95 |
|
| 96 |
register_setting( |
| 97 |
self::OPTION_GROUP, |
| 98 |
$experiment_option, |
| 99 |
array( |
| 100 |
'type' => 'boolean', |
| 101 |
'default' => false, |
| 102 |
'sanitize_callback' => 'rest_sanitize_boolean', |
| 103 |
) |
| 104 |
); |
| 105 |
|
| 106 |
// Allow experiments to register their own custom settings. |
| 107 |
if ( ! method_exists( $experiment, 'register_settings' ) ) { |
| 108 |
continue; |
| 109 |
} |
| 110 |
|
| 111 |
$experiment->register_settings(); |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
|