|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Settings page that lets administrators select which registered WordPress |
| 4 | + * abilities are exposed to the default MCP server. |
| 5 | + * |
| 6 | + * @package WP\MCP\Admin |
| 7 | + */ |
| 8 | + |
| 9 | +declare( strict_types=1 ); |
| 10 | + |
| 11 | +namespace WP\MCP\Admin; |
| 12 | + |
| 13 | +/** |
| 14 | + * Class - SettingsPage |
| 15 | + * |
| 16 | + * Renders Settings > MCP Adapter. Scope is intentionally narrow: |
| 17 | + * - single column, native wp-admin chrome |
| 18 | + * - one option, `mcp_adapter_public_abilities` |
| 19 | + * - one filter, `wp_register_ability_args` |
| 20 | + * - no React, no annotation-aware UI, no bulk actions, no source filter |
| 21 | + * |
| 22 | + * A more feature-complete reference UI lives at |
| 23 | + * https://github.com/respira-press/inhale-mcp-abilities for site owners who |
| 24 | + * want the additional affordances while this PR goes through review. |
| 25 | + */ |
| 26 | +final class SettingsPage { |
| 27 | + |
| 28 | + private const MENU_SLUG = 'mcp-adapter'; |
| 29 | + private const OPTION_GROUP = 'mcp_adapter_settings'; |
| 30 | + private const CAPABILITY = 'manage_options'; |
| 31 | + |
| 32 | + /** |
| 33 | + * Wire admin hooks. |
| 34 | + */ |
| 35 | + public function register(): void { |
| 36 | + add_action( 'admin_menu', array( $this, 'register_menu' ) ); |
| 37 | + add_action( 'admin_init', array( $this, 'register_setting' ) ); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Add the page under Settings. |
| 42 | + */ |
| 43 | + public function register_menu(): void { |
| 44 | + add_options_page( |
| 45 | + __( 'MCP Adapter', 'mcp-adapter' ), |
| 46 | + __( 'MCP Adapter', 'mcp-adapter' ), |
| 47 | + self::CAPABILITY, |
| 48 | + self::MENU_SLUG, |
| 49 | + array( $this, 'render_page' ) |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Register the option backing the page. |
| 55 | + */ |
| 56 | + public function register_setting(): void { |
| 57 | + register_setting( |
| 58 | + self::OPTION_GROUP, |
| 59 | + AbilityExposureFilter::OPTION, |
| 60 | + array( |
| 61 | + 'type' => 'array', |
| 62 | + 'description' => __( 'Abilities exposed to the default MCP server.', 'mcp-adapter' ), |
| 63 | + 'sanitize_callback' => array( $this, 'sanitize_option' ), |
| 64 | + 'default' => array(), |
| 65 | + 'show_in_rest' => false, |
| 66 | + ) |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Whitelist-based sanitize callback. Only accepts entries that match an |
| 72 | + * ability currently registered on this site and is not in the adapter's |
| 73 | + * managed namespace. |
| 74 | + * |
| 75 | + * @param mixed $value Submitted value. |
| 76 | + * @return array<int, string> |
| 77 | + */ |
| 78 | + public function sanitize_option( $value ): array { |
| 79 | + if ( ! is_array( $value ) ) { |
| 80 | + return array(); |
| 81 | + } |
| 82 | + |
| 83 | + $known = array_map( |
| 84 | + static function ( $name ): string { |
| 85 | + return (string) $name; |
| 86 | + }, |
| 87 | + array_keys( $this->discover_abilities() ) |
| 88 | + ); |
| 89 | + |
| 90 | + $out = array(); |
| 91 | + foreach ( $value as $entry ) { |
| 92 | + if ( ! is_string( $entry ) ) { |
| 93 | + continue; |
| 94 | + } |
| 95 | + $entry = sanitize_text_field( wp_unslash( $entry ) ); |
| 96 | + if ( '' === $entry || 0 === strpos( $entry, 'mcp-adapter/' ) ) { |
| 97 | + continue; |
| 98 | + } |
| 99 | + if ( ! in_array( $entry, $known, true ) ) { |
| 100 | + continue; |
| 101 | + } |
| 102 | + if ( in_array( $entry, $out, true ) ) { |
| 103 | + continue; |
| 104 | + } |
| 105 | + $out[] = $entry; |
| 106 | + } |
| 107 | + return $out; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Discover registered abilities on this site, keyed by ability name. |
| 112 | + * |
| 113 | + * @return array<string, array{label: string, description: string, managed: bool}> |
| 114 | + */ |
| 115 | + public function discover_abilities(): array { |
| 116 | + $abilities = array(); |
| 117 | + if ( ! function_exists( 'wp_get_abilities' ) ) { |
| 118 | + return $abilities; |
| 119 | + } |
| 120 | + |
| 121 | + $registered = wp_get_abilities(); |
| 122 | + if ( ! is_array( $registered ) ) { |
| 123 | + return $abilities; |
| 124 | + } |
| 125 | + |
| 126 | + foreach ( $registered as $ability ) { |
| 127 | + $name = ''; |
| 128 | + $label = ''; |
| 129 | + $description = ''; |
| 130 | + |
| 131 | + if ( is_object( $ability ) ) { |
| 132 | + $name = method_exists( $ability, 'get_name' ) ? (string) $ability->get_name() : ''; |
| 133 | + $label = method_exists( $ability, 'get_label' ) ? (string) $ability->get_label() : ''; |
| 134 | + $description = method_exists( $ability, 'get_description' ) ? (string) $ability->get_description() : ''; |
| 135 | + } elseif ( is_array( $ability ) ) { |
| 136 | + $name = isset( $ability['name'] ) ? (string) $ability['name'] : ''; |
| 137 | + $label = isset( $ability['label'] ) ? (string) $ability['label'] : ''; |
| 138 | + $description = isset( $ability['description'] ) ? (string) $ability['description'] : ''; |
| 139 | + } |
| 140 | + |
| 141 | + if ( '' === $name ) { |
| 142 | + continue; |
| 143 | + } |
| 144 | + |
| 145 | + $abilities[ $name ] = array( |
| 146 | + 'label' => $label, |
| 147 | + 'description' => $description, |
| 148 | + 'managed' => 0 === strpos( $name, 'mcp-adapter/' ), |
| 149 | + ); |
| 150 | + } |
| 151 | + ksort( $abilities ); |
| 152 | + return $abilities; |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Render the settings page. |
| 157 | + */ |
| 158 | + public function render_page(): void { |
| 159 | + if ( ! current_user_can( self::CAPABILITY ) ) { |
| 160 | + wp_die( esc_html__( 'You do not have permission to access this page.', 'mcp-adapter' ) ); |
| 161 | + } |
| 162 | + |
| 163 | + $abilities = $this->discover_abilities(); |
| 164 | + $exposed = get_option( AbilityExposureFilter::OPTION, array() ); |
| 165 | + if ( ! is_array( $exposed ) ) { |
| 166 | + $exposed = array(); |
| 167 | + } |
| 168 | + ?> |
| 169 | + <div class="wrap"> |
| 170 | + <h1><?php esc_html_e( 'MCP Adapter', 'mcp-adapter' ); ?></h1> |
| 171 | + <p> |
| 172 | + <?php |
| 173 | + esc_html_e( |
| 174 | + 'Choose which registered WordPress abilities are exposed to the default MCP server. Abilities are hidden by default; checking one sets meta.mcp.public to true on it at registration time.', |
| 175 | + 'mcp-adapter' |
| 176 | + ); |
| 177 | + ?> |
| 178 | + </p> |
| 179 | + <?php settings_errors(); ?> |
| 180 | + <form method="post" action="options.php"> |
| 181 | + <?php settings_fields( self::OPTION_GROUP ); ?> |
| 182 | + <?php if ( empty( $abilities ) ) : ?> |
| 183 | + <p> |
| 184 | + <em> |
| 185 | + <?php |
| 186 | + esc_html_e( |
| 187 | + 'No abilities are currently registered on this site. Activate the plugins or themes that register abilities to see them here.', |
| 188 | + 'mcp-adapter' |
| 189 | + ); |
| 190 | + ?> |
| 191 | + </em> |
| 192 | + </p> |
| 193 | + <?php else : ?> |
| 194 | + <table class="wp-list-table widefat fixed striped"> |
| 195 | + <thead> |
| 196 | + <tr> |
| 197 | + <th scope="col" style="width:40px;"><span class="screen-reader-text"><?php esc_html_e( 'Expose', 'mcp-adapter' ); ?></span></th> |
| 198 | + <th scope="col"><?php esc_html_e( 'Ability', 'mcp-adapter' ); ?></th> |
| 199 | + <th scope="col"><?php esc_html_e( 'Description', 'mcp-adapter' ); ?></th> |
| 200 | + </tr> |
| 201 | + </thead> |
| 202 | + <tbody> |
| 203 | + <?php foreach ( $abilities as $name => $info ) : ?> |
| 204 | + <tr<?php echo $info['managed'] ? ' class="disabled"' : ''; ?>> |
| 205 | + <td> |
| 206 | + <?php if ( $info['managed'] ) : ?> |
| 207 | + <input type="checkbox" disabled aria-label="<?php echo esc_attr( sprintf( /* translators: %s: ability name. */ __( 'Managed by adapter: %s', 'mcp-adapter' ), $name ) ); ?>" /> |
| 208 | + <?php else : ?> |
| 209 | + <input |
| 210 | + type="checkbox" |
| 211 | + name="<?php echo esc_attr( AbilityExposureFilter::OPTION ); ?>[]" |
| 212 | + value="<?php echo esc_attr( $name ); ?>" |
| 213 | + <?php checked( in_array( $name, $exposed, true ) ); ?> |
| 214 | + aria-label="<?php echo esc_attr( sprintf( /* translators: %s: ability name. */ __( 'Expose %s to the default MCP server', 'mcp-adapter' ), $name ) ); ?>" |
| 215 | + /> |
| 216 | + <?php endif; ?> |
| 217 | + </td> |
| 218 | + <td> |
| 219 | + <code><?php echo esc_html( $name ); ?></code> |
| 220 | + </td> |
| 221 | + <td> |
| 222 | + <?php |
| 223 | + if ( $info['managed'] ) { |
| 224 | + echo '<em>' . esc_html__( '(managed by the adapter)', 'mcp-adapter' ) . '</em>'; |
| 225 | + } else { |
| 226 | + echo esc_html( $info['description'] ); |
| 227 | + } |
| 228 | + ?> |
| 229 | + </td> |
| 230 | + </tr> |
| 231 | + <?php endforeach; ?> |
| 232 | + </tbody> |
| 233 | + </table> |
| 234 | + <?php endif; ?> |
| 235 | + <?php submit_button( __( 'Save changes', 'mcp-adapter' ) ); ?> |
| 236 | + </form> |
| 237 | + </div> |
| 238 | + <?php |
| 239 | + } |
| 240 | +} |
0 commit comments