|
| 1 | +<?php //phpcs:ignore |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace Automattic\WordpressMcp\Admin; |
| 5 | + |
| 6 | +/** |
| 7 | + * Class Settings |
| 8 | + * Handles the MCP settings page in WordPress admin. |
| 9 | + */ |
| 10 | +class Settings { |
| 11 | + /** |
| 12 | + * The option name in the WordPress options table. |
| 13 | + */ |
| 14 | + const OPTION_NAME = 'wordpress_mcp_settings'; |
| 15 | + |
| 16 | + /** |
| 17 | + * Initialize the settings page. |
| 18 | + */ |
| 19 | + public function __construct() { |
| 20 | + add_action( 'admin_menu', array( $this, 'add_settings_page' ) ); |
| 21 | + add_action( 'admin_init', array( $this, 'register_settings' ) ); |
| 22 | + add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
| 23 | + add_action( 'wp_ajax_wordpress_mcp_save_settings', array( $this, 'ajax_save_settings' ) ); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Add the settings page to the WordPress admin menu. |
| 28 | + */ |
| 29 | + public function add_settings_page(): void { |
| 30 | + add_options_page( |
| 31 | + __( 'MCP Settings', 'wordpress-mcp' ), |
| 32 | + __( 'MCP Settings', 'wordpress-mcp' ), |
| 33 | + 'manage_options', |
| 34 | + 'wordpress-mcp-settings', |
| 35 | + array( $this, 'render_settings_page' ) |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Register the settings and their sanitization callbacks. |
| 41 | + */ |
| 42 | + public function register_settings(): void { |
| 43 | + register_setting( |
| 44 | + 'wordpress_mcp_settings', |
| 45 | + self::OPTION_NAME, |
| 46 | + array( |
| 47 | + 'type' => 'array', |
| 48 | + 'sanitize_callback' => array( $this, 'sanitize_settings' ), |
| 49 | + ) |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Enqueue scripts and styles for the React app. |
| 55 | + * |
| 56 | + * @param string $hook The current admin page. |
| 57 | + */ |
| 58 | + public function enqueue_scripts( string $hook ): void { |
| 59 | + if ( 'settings_page_wordpress-mcp-settings' !== $hook ) { |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + $asset_file = include WORDPRESS_MCP_PATH . 'build/index.asset.php'; |
| 64 | + |
| 65 | + // Enqueue our React app. |
| 66 | + wp_enqueue_script( |
| 67 | + 'wordpress-mcp-settings', |
| 68 | + WORDPRESS_MCP_URL . 'build/index.js', |
| 69 | + $asset_file['dependencies'], |
| 70 | + $asset_file['version'], |
| 71 | + true |
| 72 | + ); |
| 73 | + |
| 74 | + // Enqueue the WordPress components styles CSS. |
| 75 | + wp_enqueue_style( |
| 76 | + 'wp-components', |
| 77 | + includes_url( 'css/dist/components/style.css' ), |
| 78 | + array(), |
| 79 | + $asset_file['version'], |
| 80 | + ); |
| 81 | + |
| 82 | + // Enqueue the WordPress MCP settings CSS. |
| 83 | + wp_enqueue_style( |
| 84 | + 'wp-mcp-settings', |
| 85 | + WORDPRESS_MCP_URL . 'build/style-index.css', |
| 86 | + array(), |
| 87 | + $asset_file['version'], |
| 88 | + ); |
| 89 | + |
| 90 | + // Localize the script with data needed by the React app. |
| 91 | + wp_localize_script( |
| 92 | + 'wordpress-mcp-settings', |
| 93 | + 'wordpressMcpSettings', |
| 94 | + array( |
| 95 | + 'apiUrl' => rest_url( 'wordpress-mcp/v1/settings' ), |
| 96 | + 'nonce' => wp_create_nonce( 'wordpress_mcp_settings' ), |
| 97 | + 'settings' => get_option( self::OPTION_NAME, array() ), |
| 98 | + 'strings' => array( |
| 99 | + 'enableMcp' => __( 'Enable MCP functionality', 'wordpress-mcp' ), |
| 100 | + 'enableMcpDescription' => __( 'Toggle to enable or disable the MCP plugin functionality.', 'wordpress-mcp' ), |
| 101 | + 'enableFeaturesAdapter' => __( 'Enable WordPress Features Adapter', 'wordpress-mcp' ), |
| 102 | + 'enableFeaturesAdapterDescription' => __( 'Enable or disable the WordPress Features Adapter. This option only works when MCP is enabled.', 'wordpress-mcp' ), |
| 103 | + 'saveSettings' => __( 'Save Settings', 'wordpress-mcp' ), |
| 104 | + 'settingsSaved' => __( 'Settings saved successfully!', 'wordpress-mcp' ), |
| 105 | + 'settingsError' => __( 'Error saving settings. Please try again.', 'wordpress-mcp' ), |
| 106 | + ), |
| 107 | + ) |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * AJAX handler for saving settings. |
| 113 | + */ |
| 114 | + public function ajax_save_settings(): void { |
| 115 | + if ( ! current_user_can( 'manage_options' ) ) { |
| 116 | + wp_send_json_error( array( 'message' => __( 'You do not have permission to perform this action.', 'wordpress-mcp' ) ) ); |
| 117 | + } |
| 118 | + |
| 119 | + $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : ''; |
| 120 | + if ( ! wp_verify_nonce( $nonce, 'wordpress_mcp_settings' ) ) { |
| 121 | + wp_send_json_error( array( 'message' => __( 'Invalid nonce. Please refresh the page and try again.', 'wordpress-mcp' ) ) ); |
| 122 | + } |
| 123 | + |
| 124 | + // Sanitize the settings input. |
| 125 | + $settings_raw = isset( $_POST['settings'] ) ? sanitize_text_field( wp_unslash( $_POST['settings'] ) ) : '{}'; |
| 126 | + $settings = $this->sanitize_settings( json_decode( $settings_raw, true ) ); |
| 127 | + update_option( self::OPTION_NAME, $settings ); |
| 128 | + |
| 129 | + wp_send_json_success( array( 'message' => __( 'Settings saved successfully!', 'wordpress-mcp' ) ) ); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Sanitize the settings before saving. |
| 134 | + * |
| 135 | + * @param array $input The input array. |
| 136 | + * @return array The sanitized input array. |
| 137 | + */ |
| 138 | + public function sanitize_settings( array $input ): array { |
| 139 | + $sanitized = array(); |
| 140 | + |
| 141 | + if ( isset( $input['enabled'] ) ) { |
| 142 | + $sanitized['enabled'] = (bool) $input['enabled']; |
| 143 | + } else { |
| 144 | + $sanitized['enabled'] = false; |
| 145 | + } |
| 146 | + |
| 147 | + if ( isset( $input['features_adapter_enabled'] ) ) { |
| 148 | + $sanitized['features_adapter_enabled'] = (bool) $input['features_adapter_enabled']; |
| 149 | + } else { |
| 150 | + $sanitized['features_adapter_enabled'] = false; |
| 151 | + } |
| 152 | + |
| 153 | + return $sanitized; |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Render the settings page. |
| 158 | + */ |
| 159 | + public function render_settings_page(): void { |
| 160 | + if ( ! current_user_can( 'manage_options' ) ) { |
| 161 | + return; |
| 162 | + } |
| 163 | + ?> |
| 164 | + <div class="wrap"> |
| 165 | + <h1><?php echo esc_html( get_admin_page_title() ); ?></h1> |
| 166 | + <div id="wordpress-mcp-settings-app"></div> |
| 167 | + </div> |
| 168 | + <?php |
| 169 | + } |
| 170 | +} |
0 commit comments