| @@ -58,9 +58,9 @@ | ||
| 58 | 58 | * |
| 59 | 59 | * @return void |
| 60 | 60 | */ |
| 61 | 61 | public function localize_admin_script(): void { |
| 62 | - if ( ! $this->is_mcp_adapter_active() || ! wp_script_is( 'lp-admin-mcp-api-keys', 'enqueued' ) ) { | |
| 62 | + if ( ! $this->is_mcp_integration_enabled() || ! wp_script_is( 'lp-admin-mcp-api-keys', 'enqueued' ) ) { | |
| 63 | 63 | return; |
| 64 | 64 | } |
| 65 | 65 | |
| 66 | 66 | wp_localize_script( |
| @@ -67,18 +67,18 @@ | ||
| 67 | 67 | 'lp-admin-mcp-api-keys', |
| 68 | 68 | 'lpMcpApiKeysSettings', |
| 69 | 69 | array( |
| 70 | 70 | 'is_mcp_keys_section' => $this->is_mcp_keys_settings_screen(), |
| 71 | - 'actions' => array( | |
| 71 | + 'actions' => array( | |
| 72 | 72 | 'create' => 'mcp_create_api_key', |
| 73 | 73 | ), |
| 74 | - 'i18n' => array( | |
| 75 | - 'processing' => __( 'Processing...', 'learnpress' ), | |
| 76 | - 'created' => __( 'API key created.', 'learnpress' ), | |
| 77 | - 'request_failed' => __( 'Request failed. Please try again.', 'learnpress' ), | |
| 78 | - 'confirm_revoke' => __( 'Revoke this API key?', 'learnpress' ), | |
| 79 | - 'copy_success' => __( 'Copied.', 'learnpress' ), | |
| 80 | - 'copy_fallback' => __( 'Copy this value manually.', 'learnpress' ), | |
| 74 | + 'i18n' => array( | |
| 75 | + 'processing' => __( 'Processing...', 'learnpress' ), | |
| 76 | + 'created' => __( 'API key created.', 'learnpress' ), | |
| 77 | + 'request_failed' => __( 'Request failed. Please try again.', 'learnpress' ), | |
| 78 | + 'confirm_revoke' => __( 'Revoke this API key?', 'learnpress' ), | |
| 79 | + 'copy_success' => __( 'Copied.', 'learnpress' ), | |
| 80 | + 'copy_fallback' => __( 'Copy this value manually.', 'learnpress' ), | |
| 81 | 81 | ), |
| 82 | 82 | ) |
| 83 | 83 | ); |
| 84 | 84 | } |
| @@ -93,9 +93,9 @@ | ||
| 93 | 93 | public function render_page(): void { |
| 94 | 94 | if ( ! current_user_can( $this->required_capability ) ) { |
| 95 | 95 | wp_die( esc_html__( 'Sorry, you are not allowed to manage MCP API keys.', 'learnpress' ) ); |
| 96 | 96 | } |
| 97 | - if ( ! $this->is_mcp_adapter_active() ) { | |
| 97 | + if ( ! $this->is_mcp_integration_enabled() ) { | |
| 98 | 98 | return; |
| 99 | 99 | } |
| 100 | 100 | |
| 101 | 101 | $table = new LP_Admin_MCP_API_Keys_Table_List( $this->repository ); |
| @@ -124,9 +124,9 @@ | ||
| 124 | 124 | * |
| 125 | 125 | * @return void |
| 126 | 126 | */ |
| 127 | 127 | public function handle_admin_actions(): void { |
| 128 | - if ( ! $this->is_mcp_keys_settings_screen() || ! current_user_can( $this->required_capability ) || ! $this->is_mcp_adapter_active() ) { | |
| 128 | + if ( ! $this->is_mcp_keys_settings_screen() || ! current_user_can( $this->required_capability ) || ! $this->is_mcp_integration_enabled() ) { | |
| 129 | 129 | return; |
| 130 | 130 | } |
| 131 | 131 | |
| 132 | 132 | $action = sanitize_key( $_REQUEST['lp_mcp_key_action'] ?? '' ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| @@ -171,9 +171,10 @@ | ||
| 171 | 171 | protected function redirect_with_notice( string $notice_code ): void { |
| 172 | 172 | $url = add_query_arg( |
| 173 | 173 | array( |
| 174 | 174 | 'page' => 'learn-press-settings', |
| 175 | - 'tab' => 'mcp', | |
| 175 | + 'tab' => 'advanced', | |
| 176 | + 'section' => 'mcp', | |
| 176 | 177 | 'lp_mcp_notice' => $notice_code, |
| 177 | 178 | ), |
| 178 | 179 | admin_url( 'admin.php' ) |
| 179 | 180 | ); |
| @@ -190,11 +191,20 @@ | ||
| 190 | 191 | * @return array<string, string>|null |
| 191 | 192 | */ |
| 192 | 193 | protected function notice_from_code( string $code ): ?array { |
| 193 | 194 | $map = array( |
| 194 | - 'revoked' => array( 'type' => 'success', 'message' => __( 'API key revoked.', 'learnpress' ) ), | |
| 195 | - 'bulk_revoked' => array( 'type' => 'success', 'message' => __( 'Selected API keys revoked.', 'learnpress' ) ), | |
| 196 | - 'no_selection' => array( 'type' => 'warning', 'message' => __( 'No API keys selected.', 'learnpress' ) ), | |
| 195 | + 'revoked' => array( | |
| 196 | + 'type' => 'success', | |
| 197 | + 'message' => __( 'API key revoked.', 'learnpress' ), | |
| 198 | + ), | |
| 199 | + 'bulk_revoked' => array( | |
| 200 | + 'type' => 'success', | |
| 201 | + 'message' => __( 'Selected API keys revoked.', 'learnpress' ), | |
| 202 | + ), | |
| 203 | + 'no_selection' => array( | |
| 204 | + 'type' => 'warning', | |
| 205 | + 'message' => __( 'No API keys selected.', 'learnpress' ), | |
| 206 | + ), | |
| 197 | 207 | ); |
| 198 | 208 | |
| 199 | 209 | return $map[ $code ] ?? null; |
| 200 | 210 | } |
| @@ -199,30 +209,31 @@ | ||
| 199 | 209 | return $map[ $code ] ?? null; |
| 200 | 210 | } |
| 201 | 211 | |
| 202 | 212 | /** |
| 203 | - * Check whether MCP Adapter plugin is active. | |
| 213 | + * Is current request LearnPress MCP settings screen. | |
| 204 | 214 | * |
| 205 | 215 | * @return bool |
| 206 | 216 | */ |
| 207 | - protected function is_mcp_adapter_active(): bool { | |
| 208 | - if ( ! function_exists( 'is_plugin_active' ) ) { | |
| 209 | - require_once ABSPATH . 'wp-admin/includes/plugin.php'; | |
| 217 | + protected function is_mcp_keys_settings_screen(): bool { | |
| 218 | + | |
| 219 | + $page = sanitize_key( $_REQUEST['page'] ?? '' ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 220 | + $tab = sanitize_key( $_REQUEST['tab'] ?? '' ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 221 | + $section = sanitize_key( $_REQUEST['section'] ?? '' ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 222 | + | |
| 223 | + if ( 'learn-press-settings' !== $page ) { | |
| 224 | + return false; | |
| 210 | 225 | } |
| 211 | 226 | |
| 212 | - return function_exists( 'is_plugin_active' ) && is_plugin_active( 'mcp-adapter/mcp-adapter.php' ); | |
| 227 | + return ( 'advanced' === $tab && 'mcp' === $section ) | |
| 228 | + || 'mcp' === $tab; | |
| 213 | 229 | } |
| 214 | 230 | |
| 215 | 231 | /** |
| 216 | - * Is current request LearnPress MCP settings screen. | |
| 232 | + * Check whether MCP integration is enabled. | |
| 217 | 233 | * |
| 218 | 234 | * @return bool |
| 219 | 235 | */ |
| 220 | - protected function is_mcp_keys_settings_screen(): bool { | |
| 221 | - $page = sanitize_key( $_REQUEST['page'] ?? '' ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 222 | - $tab = sanitize_key( $_REQUEST['tab'] ?? '' ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 223 | - | |
| 224 | - return 'learn-press-settings' === $page | |
| 225 | - && 'mcp' === $tab; | |
| 236 | + protected function is_mcp_integration_enabled(): bool { | |
| 237 | + return 'yes' === LP_Settings::get_option( 'enable_mcp_integration', 'no' ); | |
| 226 | 238 | } |
| 227 | - | |
| 228 | 239 | } |