|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Admin page controller for the AI Experiments settings screen. |
| 4 | + * |
| 5 | + * @package WordPress\AI |
| 6 | + */ |
| 7 | + |
| 8 | +namespace WordPress\AI\Admin; |
| 9 | + |
| 10 | +use WordPress\AI\Admin\Settings\Settings_Registry; |
| 11 | +use WordPress\AI\Admin\Settings\Settings_Section; |
| 12 | +use WordPress\AI\Admin\Settings\Settings_Toggle; |
| 13 | + |
| 14 | +/** |
| 15 | + * Handles menu registration, asset loading, and fallback rendering. |
| 16 | + * |
| 17 | + * @since 0.1.0 |
| 18 | + */ |
| 19 | +class Admin_Settings_Page { |
| 20 | + /** |
| 21 | + * Menu slug for the settings page. |
| 22 | + */ |
| 23 | + private const MENU_SLUG = 'ai-experiments'; |
| 24 | + |
| 25 | + /** |
| 26 | + * Toggle service. |
| 27 | + * |
| 28 | + * @var \WordPress\AI\Admin\Settings\Settings_Toggle |
| 29 | + */ |
| 30 | + private $toggle; |
| 31 | + |
| 32 | + /** |
| 33 | + * Settings registry. |
| 34 | + * |
| 35 | + * @var \WordPress\AI\Admin\Settings\Settings_Registry |
| 36 | + */ |
| 37 | + private $registry; |
| 38 | + |
| 39 | + /** |
| 40 | + * Settings page assets handler. |
| 41 | + * |
| 42 | + * @var \WordPress\AI\Admin\Settings_Page_Assets |
| 43 | + */ |
| 44 | + private $assets; |
| 45 | + |
| 46 | + /** |
| 47 | + * Settings payload builder. |
| 48 | + * |
| 49 | + * @var \WordPress\AI\Admin\Settings_Payload_Builder |
| 50 | + */ |
| 51 | + private $payload_builder; |
| 52 | + |
| 53 | + /** |
| 54 | + * Hook suffix returned by add_options_page. |
| 55 | + * |
| 56 | + * @var string|false |
| 57 | + */ |
| 58 | + private $hook_suffix = ''; |
| 59 | + |
| 60 | + /** |
| 61 | + * Constructor. |
| 62 | + * |
| 63 | + * @since 0.1.0 |
| 64 | + * |
| 65 | + * @param \WordPress\AI\Admin\Settings\Settings_Toggle $toggle Toggle service. |
| 66 | + * @param \WordPress\AI\Admin\Settings\Settings_Registry $registry Settings registry. |
| 67 | + * @param \WordPress\AI\Admin\Settings_Page_Assets $assets Assets handler. |
| 68 | + * @param \WordPress\AI\Admin\Settings_Payload_Builder $payload_builder Payload builder. |
| 69 | + */ |
| 70 | + public function __construct( |
| 71 | + Settings_Toggle $toggle, |
| 72 | + Settings_Registry $registry, |
| 73 | + Settings_Page_Assets $assets, |
| 74 | + Settings_Payload_Builder $payload_builder |
| 75 | + ) { |
| 76 | + $this->toggle = $toggle; |
| 77 | + $this->registry = $registry; |
| 78 | + $this->assets = $assets; |
| 79 | + $this->payload_builder = $payload_builder; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Registers the submenu item under Settings. |
| 84 | + * |
| 85 | + * @since 0.1.0 |
| 86 | + */ |
| 87 | + public function register_menu(): void { |
| 88 | + $this->hook_suffix = add_options_page( |
| 89 | + __( 'AI Experiments', 'ai' ), |
| 90 | + __( 'AI Experiments', 'ai' ), |
| 91 | + 'manage_options', |
| 92 | + self::MENU_SLUG, |
| 93 | + array( $this, 'render' ) |
| 94 | + ); |
| 95 | + |
| 96 | + if ( ! $this->hook_suffix ) { |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + // Pass hook suffix to assets handler for conditional enqueueing. |
| 101 | + $this->assets->set_hook_suffix( $this->hook_suffix ); |
| 102 | + |
| 103 | + add_action( |
| 104 | + 'admin_enqueue_scripts', |
| 105 | + array( $this->assets, 'enqueue_assets' ) |
| 106 | + ); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Renders the settings page markup. |
| 111 | + * |
| 112 | + * @since 0.1.0 |
| 113 | + */ |
| 114 | + public function render(): void { |
| 115 | + if ( ! current_user_can( 'manage_options' ) ) { |
| 116 | + wp_die( esc_html__( 'Sorry, you are not allowed to access this page.', 'ai' ) ); |
| 117 | + } |
| 118 | + |
| 119 | + $payload = $this->payload_builder->build(); |
| 120 | + ?> |
| 121 | + <div class="wrap ai-experiments-settings"> |
| 122 | + <h1><?php esc_html_e( 'AI Experiments', 'ai' ); ?></h1> |
| 123 | + <p class="description"> |
| 124 | + <?php esc_html_e( 'Manage access to experimental AI functionality and review feature-specific settings.', 'ai' ); ?> |
| 125 | + </p> |
| 126 | + <div id="ai-experiments-settings-root" data-settings="<?php echo esc_attr( (string) wp_json_encode( $payload ) ); ?>"></div> |
| 127 | + <div class="ai-experiments-settings__fallback"> |
| 128 | + <?php $this->render_sections(); ?> |
| 129 | + </div> |
| 130 | + </div> |
| 131 | + <?php |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Renders registered sections for the fallback experience. |
| 136 | + * |
| 137 | + * @since 0.1.0 |
| 138 | + */ |
| 139 | + private function render_sections(): void { |
| 140 | + foreach ( $this->registry->get_sections() as $section ) { |
| 141 | + $this->render_section( $section ); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Renders an individual section. |
| 147 | + * |
| 148 | + * @since 0.1.0 |
| 149 | + * |
| 150 | + * @param \WordPress\AI\Admin\Settings\Settings_Section $section Section metadata. |
| 151 | + */ |
| 152 | + private function render_section( Settings_Section $section ): void { |
| 153 | + $section_id = $section->get_id(); |
| 154 | + ?> |
| 155 | + <section |
| 156 | + id="ai-experiments-section-<?php echo esc_attr( $section_id ); ?>" |
| 157 | + class="ai-experiments-settings__section" |
| 158 | + > |
| 159 | + <h2><?php echo esc_html( $section->get_title() ); ?></h2> |
| 160 | + <?php if ( $section->get_description() ) : ?> |
| 161 | + <p><?php echo esc_html( $section->get_description() ); ?></p> |
| 162 | + <?php endif; ?> |
| 163 | + <div class="ai-experiments-settings__section-content"> |
| 164 | + <?php call_user_func( $section->get_render_callback(), $this->toggle, $section ); ?> |
| 165 | + </div> |
| 166 | + </section> |
| 167 | + <?php |
| 168 | + } |
| 169 | +} |
0 commit comments