array_values( array_unique( $ids ) ), 'host' => 'chatgpt.com', ); return $publishers; } /** * Save a connection-specific ChatGPT client URL. */ public static function save() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'Sorry, you are not allowed to do that.', 'admindra-mcp' ), '', array( 'response' => 403 ) ); } check_admin_referer( 'admindra_mcp_client' ); $url = isset( $_POST['client_url'] ) ? trim( sanitize_text_field( wp_unslash( $_POST['client_url'] ) ) ) : ''; if ( '' !== $url && ! self::valid_client( $url ) ) { wp_die( esc_html__( 'Use the exact https://chatgpt.com/oauth/…/client.json URL that ChatGPT shows.', 'admindra-mcp' ), '', array( 'response' => 400 ) ); } update_option( self::OPT_CLIENT, $url, false ); wp_safe_redirect( AdMindra_MCP::page_url() ); exit; } /** * Refuse malformed requests before the library sees them. * * OAuth requests come from the AI client, not from a WordPress form, so * there is no nonce to check: PKCE, the state parameter and the consent * screen's own nonce protect the flow. Every value read here is only * validated (type, length, exact pattern), never stored or printed. */ public static function guard_oauth() { $action = (string) get_query_var( 'mcp_oauth_endpoint', '' ); if ( '' === $action ) { return; } if ( ! is_ssl() ) { wp_die( esc_html__( 'OAuth requires HTTPS.', 'admindra-mcp' ), '', array( 'response' => 400 ) ); } if ( in_array( $action, array( 'authorize-callback', 'consent' ), true ) && is_user_logged_in() && ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'Only a WordPress administrator can connect an AI client to this site.', 'admindra-mcp' ), '', array( 'response' => 403 ) ); } // phpcs:disable WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput -- OAuth protocol input, validated below; see the docblock. $query = wp_unslash( $_GET ); $body = wp_unslash( $_POST ); $type = isset( $_SERVER['CONTENT_TYPE'] ) ? sanitize_text_field( wp_unslash( $_SERVER['CONTENT_TYPE'] ) ) : ''; // phpcs:enable if ( false !== strpos( $type, 'application/json' ) ) { // Same 32 KiB ceiling as the library's own body parser. $raw = (string) file_get_contents( 'php://input', false, null, 0, 32768 ); $body = json_decode( $raw, true ); if ( ! is_array( $body ) ) { $body = array(); } } foreach ( self::FIELDS as $field ) { foreach ( array( $query, $body ) as $input ) { if ( isset( $input[ $field ] ) && ( ! is_string( $input[ $field ] ) || strlen( $input[ $field ] ) > 8192 ) ) { wp_send_json( array( 'error' => 'invalid_request' ), 400 ); } } } if ( 'authorize' === $action ) { $scope = trim( (string) ( $query['scope'] ?? 'mcp' ) ); if ( '' !== $scope && 'mcp' !== $scope ) { wp_send_json( array( 'error' => 'invalid_scope' ), 400 ); } if ( isset( $query['code_challenge'] ) && ! preg_match( '/\A[a-zA-Z0-9_-]{43}\z/D', $query['code_challenge'] ) ) { wp_send_json( array( 'error' => 'invalid_request' ), 400 ); } } if ( 'token' === $action && isset( $body['code_verifier'] ) && ! preg_match( '/\A[a-zA-Z0-9._~-]{43,128}\z/D', $body['code_verifier'] ) ) { wp_send_json( array( 'error' => 'invalid_request' ), 400 ); } $resource = $query['resource'] ?? $body['resource'] ?? null; if ( null !== $resource && ( ! is_string( $resource ) || rest_url( 'mcp/mcp-oauth-server' ) !== $resource ) ) { nocache_headers(); wp_send_json( array( 'error' => 'invalid_target' ), 400 ); } } /** * "Connect your AI" on the settings screen. */ public static function panel() { $mcp_url = rest_url( 'mcp/mcp-oauth-server' ); $server = sanitize_title( get_bloginfo( 'name' ) ); $cmd = 'claude mcp add --transport http ' . ( '' !== $server ? $server : 'wordpress' ) . ' ' . $mcp_url; echo '

' . esc_html__( 'Connect your AI', 'admindra-mcp' ) . '

'; echo '

' . esc_html__( 'No client secret and no WordPress password: you sign in as an administrator and approve a consent screen that names the assistant.', 'admindra-mcp' ) . '

'; echo '
'; echo '
'; echo '
' . esc_html__( 'Claude (claude.ai, desktop and mobile)', 'admindra-mcp' ) . '
    '; echo '
  1. ' . esc_html__( 'Settings → Connectors → Add custom connector.', 'admindra-mcp' ) . '
  2. '; echo '
  3. ' . esc_html__( 'Paste the MCP URL. Leave the OAuth client ID and secret under Advanced settings empty.', 'admindra-mcp' ) . '
  4. '; echo '
  5. ' . esc_html__( 'Connect, sign in to WordPress and approve. Then ask for the site status.', 'admindra-mcp' ) . '
'; echo '
' . esc_html__( 'Claude Code (terminal)', 'admindra-mcp' ) . ''; echo '
'; echo '

' . esc_html__( 'Then run /mcp in Claude Code and choose Authenticate. The browser opens the WordPress sign-in and consent screen.', 'admindra-mcp' ) . '

'; echo '
' . esc_html__( 'ChatGPT', 'admindra-mcp' ) . '
    '; echo '
  1. ' . esc_html__( 'Add a custom MCP connector, paste the MCP URL and choose OAuth.', 'admindra-mcp' ) . '
  2. '; echo '
  3. ' . esc_html__( 'Sign in to WordPress as an administrator and approve.', 'admindra-mcp' ) . '
'; AdMindra_MCP_Key::panel(); echo '
'; echo '

' . esc_html__( 'To disconnect Claude or ChatGPT, delete its entry under Users → Profile → Application Passwords. Its access ends at once.', 'admindra-mcp' ) . '

'; } /** * What only a few sites need: a ChatGPT client URL and the discovery checks. */ public static function advanced() { echo '

' . esc_html__( 'Advanced', 'admindra-mcp' ) . '

'; echo '

' . esc_html__( 'OAuth discovery', 'admindra-mcp' ) . '

'; foreach ( array( 'oauth-protected-resource', 'oauth-authorization-server' ) as $doc ) { echo '' . esc_html( $doc ) . ' '; } echo '

' . esc_html__( 'Both must show JSON, not a login page or a 404. If they do not, save your permalinks again and exclude /.well-known/ and /wp-json/mcp/ from page caches.', 'admindra-mcp' ) . '

'; echo '

' . esc_html__( 'ChatGPT client URL (only if ChatGPT shows one)', 'admindra-mcp' ) . '

'; echo '

' . esc_html__( 'ChatGPT\'s standard client and Claude are trusted already. If ChatGPT shows a connection-specific client metadata URL, paste that exact URL here and reconnect.', 'admindra-mcp' ) . '

'; echo '
'; wp_nonce_field( 'admindra_mcp_client' ); echo ''; echo '
'; } }